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.63 by tdb, Tue Apr 6 14:52:58 2004 UTC vs.
Revision 1.69 by tdb, Sun Jun 20 17:00:25 2004 UTC

# Line 47 | Line 47
47  
48   #ifdef LINUX
49   #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
50 <                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
51 <                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
52 <                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
53 <                        "umsdos", "vfat", "xfs", "jfs"}
50 >                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
51 >                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
52 >                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
53 >                        "umsdos", "vfat", "xfs", "jfs"}
54   #endif
55  
56   #ifdef CYGWIN
# Line 66 | Line 66
66   #include <sys/dkstat.h>
67   #include <devstat.h>
68   #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
69 <                        "ufs", "mfs"}
69 >                        "ufs", "mfs"}
70   #endif
71   #if defined(NETBSD) || defined(OPENBSD)
72   #include <sys/param.h>
73   #include <sys/sysctl.h>
74   #include <sys/disk.h>
75   #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
76 <                        "ntfs"}
76 >                        "ntfs"}
77   #endif
78  
79   static void disk_stat_init(sg_fs_stats *d) {
# Line 102 | Line 102 | static int is_valid_fs_type(const char *type) {
102  
103   sg_fs_stats *sg_get_fs_stats(int *entries){
104          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
105 <                              disk_stat_init, disk_stat_destroy);
105 >                              disk_stat_init, disk_stat_destroy);
106  
107          int valid_type;
108          int num_disks=0;
# Line 128 | Line 128 | sg_fs_stats *sg_get_fs_stats(int *entries){
128   #ifdef ALLBSD
129          nummnt=getmntinfo(&mp , MNT_LOCAL);
130          if (nummnt<=0){
131 +                sg_set_error(SG_ERROR_GETMNTINFO, NULL);
132                  return NULL;
133          }
134          for(;nummnt--; mp++){
# Line 136 | Line 137 | sg_fs_stats *sg_get_fs_stats(int *entries){
137  
138   #if defined(LINUX) || defined(CYGWIN)
139          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
140 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
141                  return NULL;
142          }
143  
# Line 149 | Line 151 | sg_fs_stats *sg_get_fs_stats(int *entries){
151  
152   #ifdef SOLARIS
153          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
154 +                sg_set_error(SG_ERROR_OPEN, "/etc/mnttab");
155                  return NULL;
156          }
157          while((getmntent(f, &mp)) == 0){
# Line 216 | Line 219 | sg_fs_stats *sg_get_fs_stats(int *entries){
219                          }
220  
221                          if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
222 <                                return NULL;
223 <                        }
222 >                                return NULL;
223 >                        }
224          
225                          if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
226 <                                return NULL;
227 <                        }
226 >                                return NULL;
227 >                        }
228                          
229                          disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
230                          disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
# Line 238 | Line 241 | sg_fs_stats *sg_get_fs_stats(int *entries){
241          *entries=num_disks;    
242  
243          /* If this fails, there is very little i can do about it, so
244 <           I'll ignore it :) */
244 >           I'll ignore it :) */
245   #if defined(LINUX) || defined(CYGWIN)
246          endmntent(f);
247   #endif
# Line 250 | Line 253 | sg_fs_stats *sg_get_fs_stats(int *entries){
253  
254   }
255  
256 + int sg_fs_compare_device_name(const void *va, const void *vb) {
257 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
258 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
259 +
260 +        return strcmp(a->device_name, b->device_name);
261 + }
262 +
263 + int sg_fs_compare_mnt_point(const void *va, const void *vb) {
264 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
265 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
266 +
267 +        return strcmp(a->mnt_point, b->mnt_point);
268 + }
269 +
270   static void diskio_stat_init(sg_disk_io_stats *d) {
271          d->disk_name = NULL;
272   }
# Line 259 | Line 276 | static void diskio_stat_destroy(sg_disk_io_stats *d) {
276   }
277  
278   VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
279 <                      diskio_stat_init, diskio_stat_destroy);
279 >                      diskio_stat_init, diskio_stat_destroy);
280  
281   #ifdef LINUX
282   typedef struct {
# Line 275 | Line 292 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
292   #endif
293  
294   #ifdef SOLARIS
295 <        kstat_ctl_t *kc;
296 <        kstat_t *ksp;
295 >        kstat_ctl_t *kc;
296 >        kstat_t *ksp;
297          kstat_io_t kios;
298   #endif
299   #ifdef LINUX
# Line 327 | Line 344 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
344  
345          size = sizeof(diskcount);
346          if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
347 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
348                  return NULL;
349          }
350  
# Line 334 | Line 352 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
352          mib[1] = HW_DISKNAMES;
353  
354          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
355 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
356                  return NULL;
357          }
358  
359 <        disknames = malloc(size);
359 >        disknames = sg_malloc(size);
360          if (disknames == NULL) {
361                  return NULL;
362          }
363  
364          if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
365 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
366                  return NULL;
367          }
368  
369 <        dk_name = calloc(diskcount, sizeof(char *));
369 >        dk_name = sg_malloc(diskcount * sizeof(char *));
370          bufpp = disknames;
371          for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
372                  dk_name[i] = name;
# Line 361 | Line 381 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
381   #endif
382  
383          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
384 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
385                  return NULL;
386          }
387  
# Line 370 | Line 391 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
391          num_disks = size / sizeof(struct diskstats);
392   #endif
393  
394 <        stats = malloc(size);
394 >        stats = sg_malloc(size);
395          if (stats == NULL) {
396                  return NULL;
397          }
398  
399          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
400 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
401                  return NULL;
402          }
403  
# Line 388 | Line 410 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
410                  rbytes = stats[i].dk_rbytes;
411                  wbytes = stats[i].dk_wbytes;
412   #else
413 <                /* Before 1.7, NetBSD merged reads and writes. */
413 >                /* Before 2.0, NetBSD merged reads and writes. */
414                  rbytes = wbytes = stats[i].dk_bytes;
415   #endif
416   #else
417 + #ifdef HAVE_DS_RBYTES
418 +                rbytes = stats[i].ds_rbytes;
419 +                wbytes = stats[i].ds_wbytes;
420 + #else
421 +                /* Before 3.5, OpenBSD merged reads and writes */
422                  rbytes = wbytes = stats[i].ds_bytes;
423   #endif
424 + #endif
425  
426                  /* Don't keep stats for disks that have never been used. */
427                  if (rbytes == 0 && wbytes == 0) {
# Line 429 | Line 457 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
457  
458   #if defined(FREEBSD) || defined(DFBSD)
459          if (!stats_init) {
460 <                stats.dinfo=malloc(sizeof(struct devinfo));
460 >                stats.dinfo=sg_malloc(sizeof(struct devinfo));
461                  if(stats.dinfo==NULL) return NULL;
462                  bzero(stats.dinfo, sizeof(struct devinfo));
463                  stats_init = 1;
464          }
465   #ifdef FREEBSD5
466 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
466 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
467 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
468 >                return NULL;
469 >        }
470          /* Not aware of a get all devices, so i said 999. If we ever
471           * find a machine with more than 999 disks, then i'll change
472           * this number :)
473           */
474 <        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;
474 >        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) {
475 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
476 >                return NULL;
477 >        }
478   #else
479 <        if ((getdevs(&stats)) < 0) return NULL;
479 >        if ((getdevs(&stats)) < 0) {
480 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
481 >                return NULL;
482 >        }
483          /* Not aware of a get all devices, so i said 999. If we ever
484           * find a machine with more than 999 disks, then i'll change
485           * this number :)
486           */
487 <        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;
487 >        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) {
488 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
489 >                return NULL;
490 >        }
491   #endif
492  
493          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 477 | Line 517 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
517   #endif
518                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
519                  if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
520 +                        sg_set_error(SG_ERROR_ASPRINTF, NULL);
521                          return NULL;
522                  }
523                  diskio_stats_ptr->systime=time(NULL);
# Line 488 | Line 529 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
529   #endif
530   #ifdef SOLARIS
531          if ((kc = kstat_open()) == NULL) {
532 <                return NULL;
533 <        }
532 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
533 >                return NULL;
534 >        }
535  
536          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
537 <                if (!strcmp(ksp->ks_class, "disk")) {
537 >                if (!strcmp(ksp->ks_class, "disk")) {
538  
539                          if(ksp->ks_type != KSTAT_TYPE_IO) continue;
540                          /* We dont want metadevices appearins as num_diskio */
541                          if(strcmp(ksp->ks_module, "md")==0) continue;
542 <                        if((kstat_read(kc, ksp, &kios))==-1){  
542 >                        if((kstat_read(kc, ksp, &kios))==-1){  
543                          }
544                          
545                          if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
# Line 509 | Line 551 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
551                          diskio_stats_ptr->read_bytes=kios.nread;
552                          diskio_stats_ptr->write_bytes=kios.nwritten;
553                          if (sg_update_string(&diskio_stats_ptr->disk_name,
554 <                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
554 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
555                                  return NULL;
556                          }
557                          diskio_stats_ptr->systime=time(NULL);
# Line 656 | Line 698 | out:
698   #endif
699  
700   #ifdef CYGWIN
701 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
702          return NULL;
703   #endif
704  
# Line 666 | Line 709 | out:
709  
710   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
711          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
712 <                              diskio_stat_init, diskio_stat_destroy);
712 >                              diskio_stat_init, diskio_stat_destroy);
713          sg_disk_io_stats *src = NULL, *dest;
714          int i, j, diff_count, new_count;
715  
# Line 726 | Line 769 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
769  
770          *entries = diff_count;
771          return diff;
772 + }
773 +
774 + int sg_disk_io_compare_name(const void *va, const void *vb) {
775 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
776 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
777 +
778 +        return strcmp(a->disk_name, b->disk_name);
779   }
780  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines