ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/disk_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/disk_stats.c (file contents):
Revision 1.65 by tdb, Wed Apr 7 14:53:40 2004 UTC vs.
Revision 1.79 by ats, Sun Nov 7 15:06:49 2004 UTC

# Line 37 | Line 37
37   #include <sys/mnttab.h>
38   #include <sys/statvfs.h>
39   #include <kstat.h>
40 < #define VALID_FS_TYPES {"ufs", "tmpfs"}
40 > #define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs"}
41   #endif
42  
43   #if defined(LINUX) || defined(CYGWIN)
# Line 76 | Line 76
76                          "ntfs"}
77   #endif
78  
79 + #ifdef HPUX
80 + #include <sys/param.h>
81 + #include <sys/pstat.h>
82 + #include <sys/types.h>
83 + #include <sys/stat.h>
84 + #include <sys/vfs.h>
85 + #include <mntent.h>
86 + #include <dirent.h>
87 + #include <stdio.h>
88 + #include <time.h>
89 + #define VALID_FS_TYPES {"vxfs", "hfs"}
90 + #define DISK_BATCH 30
91 + #endif
92 +
93   static void disk_stat_init(sg_fs_stats *d) {
94          d->device_name = NULL;
95          d->fs_type = NULL;
# Line 92 | Line 106 | static int is_valid_fs_type(const char *type) {
106          const char *types[] = VALID_FS_TYPES;
107          int i;
108  
109 <        for (i = 0; i < (sizeof types / sizeof *types); i++) {
109 >        for (i = 0; i < (int) (sizeof types / sizeof *types); i++) {
110                  if (strcmp(types[i], type) == 0) {
111                          return 1;
112                  }
# Line 106 | Line 120 | sg_fs_stats *sg_get_fs_stats(int *entries){
120  
121          int valid_type;
122          int num_disks=0;
123 < #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
123 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX)
124          FILE *f;
125   #endif
126  
# Line 116 | Line 130 | sg_fs_stats *sg_get_fs_stats(int *entries){
130          struct mnttab mp;
131          struct statvfs fs;
132   #endif
133 < #if defined(LINUX) || defined(CYGWIN)
133 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
134          struct mntent *mp;
135          struct statfs fs;
136   #endif
137   #ifdef ALLBSD
138          int nummnt;
139 + #ifdef HAVE_STATVFS
140 +        struct statvfs *mp;
141 + #else
142          struct statfs *mp;
143   #endif
144 + #endif
145  
146   #ifdef ALLBSD
147          nummnt=getmntinfo(&mp , MNT_LOCAL);
148          if (nummnt<=0){
149 +                sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL);
150                  return NULL;
151          }
152          for(;nummnt--; mp++){
153                  valid_type = is_valid_fs_type(mp->f_fstypename);
154   #endif
155  
156 < #if defined(LINUX) || defined(CYGWIN)
156 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
157 > #ifdef MNT_MNTTAB
158 >        if ((f=setmntent(MNT_MNTTAB, "r" ))==NULL){
159 > #else
160          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
161 + #endif
162 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
163                  return NULL;
164          }
165  
# Line 149 | Line 173 | sg_fs_stats *sg_get_fs_stats(int *entries){
173  
174   #ifdef SOLARIS
175          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
176 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/etc/mnttab");
177                  return NULL;
178          }
179          while((getmntent(f, &mp)) == 0){
# Line 184 | Line 209 | sg_fs_stats *sg_get_fs_stats(int *entries){
209                          /* Freebsd doesn't have a "available" inodes */
210                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
211   #endif
212 < #if defined(LINUX) || defined(CYGWIN)
212 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
213                          if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
214                                  return NULL;
215                          }
# Line 239 | Line 264 | sg_fs_stats *sg_get_fs_stats(int *entries){
264  
265          /* If this fails, there is very little i can do about it, so
266             I'll ignore it :) */
267 < #if defined(LINUX) || defined(CYGWIN)
267 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
268          endmntent(f);
269   #endif
270   #if defined(SOLARIS)
# Line 250 | Line 275 | sg_fs_stats *sg_get_fs_stats(int *entries){
275  
276   }
277  
278 + int sg_fs_compare_device_name(const void *va, const void *vb) {
279 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
280 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
281 +
282 +        return strcmp(a->device_name, b->device_name);
283 + }
284 +
285 + int sg_fs_compare_mnt_point(const void *va, const void *vb) {
286 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
287 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
288 +
289 +        return strcmp(a->mnt_point, b->mnt_point);
290 + }
291 +
292   static void diskio_stat_init(sg_disk_io_stats *d) {
293          d->disk_name = NULL;
294   }
# Line 274 | Line 313 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
313          sg_disk_io_stats *diskio_stats_ptr;
314   #endif
315  
316 + #ifdef HPUX
317 +        long long rbytes = 0, wbytes = 0;
318 +        struct dirent *dinfo = NULL;
319 +        struct stat lstatinfo;
320 +        struct pst_diskinfo pstat_diskinfo[DISK_BATCH];
321 +        char fullpathbuf[1024] = {0};
322 +        dev_t diskid;
323 +        DIR *dh = NULL;
324 +        int diskidx = 0;
325 +        int num, i;
326 + #endif
327   #ifdef SOLARIS
328          kstat_ctl_t *kc;
329          kstat_t *ksp;
# Line 321 | Line 371 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
371  
372          num_diskio=0;
373  
374 + #ifdef HPUX
375 +        while (1) {
376 +                num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
377 +                                    DISK_BATCH, diskidx);
378 +                if (num == -1) {
379 +                        sg_set_error_with_errno(SG_ERROR_PSTAT,
380 +                                                "pstat_getdisk");
381 +                        return NULL;
382 +                } else if (num == 0) {
383 +                        break;
384 +                }
385 +
386 +                for (i = 0; i < num; i++) {
387 +                        struct pst_diskinfo *di = &pstat_diskinfo[i];
388 +
389 +                        /* Skip "disabled" disks. */
390 +                        if (di->psd_status == 0) {
391 +                                continue;
392 +                        }
393 +        
394 +                        /* We can't seperate the reads from the writes, we'll
395 +                         * just give the same to each. (This value is in
396 +                         * 64-byte chunks according to the pstat header file,
397 +                         * and can wrap to be negative.)
398 +                         */
399 +                        rbytes = wbytes = ((unsigned long) di->psd_dkwds) * 64LL;
400 +        
401 +                        /* Skip unused disks. */
402 +                        if (rbytes == 0 && wbytes == 0) {
403 +                                continue;
404 +                        }
405 +        
406 +                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
407 +                                return NULL;
408 +                        }
409 +        
410 +                        diskio_stats_ptr = diskio_stats + num_diskio;
411 +        
412 +                        diskio_stats_ptr->read_bytes = rbytes;
413 +                        diskio_stats_ptr->write_bytes = wbytes;
414 +        
415 +                        diskio_stats_ptr->systime = time(NULL);
416 +        
417 +                        num_diskio++;
418 +        
419 +                        /* FIXME This should use a static cache, like the Linux
420 +                         * code below. */
421 +                        if (diskio_stats_ptr->disk_name == NULL) {
422 +                                dh = opendir("/dev/dsk");
423 +                                if (dh == NULL) {
424 +                                        continue;
425 +                                }
426 +        
427 +                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
428 +                                while (1) {
429 +                                        dinfo = readdir(dh);
430 +                                        if (dinfo == NULL) {
431 +                                                break;
432 +                                        }
433 +                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
434 +                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
435 +                                                continue;
436 +                                        }
437 +        
438 +                                        if (lstatinfo.st_rdev == diskid) {
439 +                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
440 +                                                        return NULL;
441 +                                                }
442 +                                                break;
443 +                                        }
444 +                                }
445 +                                closedir(dh);
446 +        
447 +                                if (diskio_stats_ptr->disk_name == NULL) {
448 +                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
449 +                                                return NULL;
450 +                                        }
451 +                                }
452 +                        }
453 +                }
454 +                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
455 +        }
456 + #endif
457   #ifdef OPENBSD
458          mib[0] = CTL_HW;
459          mib[1] = HW_DISKCOUNT;
460  
461          size = sizeof(diskcount);
462          if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
463 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
464                  return NULL;
465          }
466  
# Line 334 | Line 468 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
468          mib[1] = HW_DISKNAMES;
469  
470          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
471 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
472                  return NULL;
473          }
474  
# Line 343 | Line 478 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
478          }
479  
480          if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
481 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
482                  return NULL;
483          }
484  
# Line 361 | Line 497 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
497   #endif
498  
499          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
500 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
501                  return NULL;
502          }
503  
# Line 376 | Line 513 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
513          }
514  
515          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
516 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
517                  return NULL;
518          }
519  
# Line 388 | Line 526 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
526                  rbytes = stats[i].dk_rbytes;
527                  wbytes = stats[i].dk_wbytes;
528   #else
529 <                /* Before 1.7, NetBSD merged reads and writes. */
529 >                /* Before 2.0, NetBSD merged reads and writes. */
530                  rbytes = wbytes = stats[i].dk_bytes;
531   #endif
532   #else
533 + #ifdef HAVE_DS_RBYTES
534 +                rbytes = stats[i].ds_rbytes;
535 +                wbytes = stats[i].ds_wbytes;
536 + #else
537 +                /* Before 3.5, OpenBSD merged reads and writes */
538                  rbytes = wbytes = stats[i].ds_bytes;
539   #endif
540 + #endif
541  
542                  /* Don't keep stats for disks that have never been used. */
543                  if (rbytes == 0 && wbytes == 0) {
# Line 435 | Line 579 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
579                  stats_init = 1;
580          }
581   #ifdef FREEBSD5
582 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
582 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
583 >                /* FIXME devstat functions return a string error in
584 >                   devstat_errbuf */
585 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
586 >                return NULL;
587 >        }
588          /* Not aware of a get all devices, so i said 999. If we ever
589           * find a machine with more than 999 disks, then i'll change
590           * this number :)
591           */
592 <        if (devstat_selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL;
592 >        if (devstat_selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) {
593 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
594 >                return NULL;
595 >        }
596   #else
597 <        if ((getdevs(&stats)) < 0) return NULL;
597 >        if ((getdevs(&stats)) < 0) {
598 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
599 >                return NULL;
600 >        }
601          /* Not aware of a get all devices, so i said 999. If we ever
602           * find a machine with more than 999 disks, then i'll change
603           * this number :)
604           */
605 <        if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL;
605 >        if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) {
606 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
607 >                return NULL;
608 >        }
609   #endif
610  
611          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 477 | Line 635 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
635   #endif
636                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
637                  if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
638 +                        sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL);
639                          return NULL;
640                  }
641                  diskio_stats_ptr->systime=time(NULL);
# Line 488 | Line 647 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
647   #endif
648   #ifdef SOLARIS
649          if ((kc = kstat_open()) == NULL) {
650 +                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
651                  return NULL;
652          }
653  
# Line 656 | Line 816 | out:
816   #endif
817  
818   #ifdef CYGWIN
819 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
820          return NULL;
821   #endif
822  
# Line 726 | Line 887 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
887  
888          *entries = diff_count;
889          return diff;
890 + }
891 +
892 + int sg_disk_io_compare_name(const void *va, const void *vb) {
893 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
894 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
895 +
896 +        return strcmp(a->disk_name, b->disk_name);
897   }
898  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines