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.64 by ats, Wed Apr 7 10:33:50 2004 UTC vs.
Revision 1.81 by ats, Wed Apr 13 11:50:31 2005 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 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", "nfs"}
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", "nfs"}
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", "nfs"}
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 102 | Line 116 | static int is_valid_fs_type(const char *type) {
116  
117   sg_fs_stats *sg_get_fs_stats(int *entries){
118          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
119 <                              disk_stat_init, disk_stat_destroy);
119 >                              disk_stat_init, disk_stat_destroy);
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);
147 >        nummnt=getmntinfo(&mp, MNT_WAIT);
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 216 | Line 241 | sg_fs_stats *sg_get_fs_stats(int *entries){
241                          }
242  
243                          if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
244 <                                return NULL;
245 <                        }
244 >                                return NULL;
245 >                        }
246          
247                          if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
248 <                                return NULL;
249 <                        }
248 >                                return NULL;
249 >                        }
250                          
251                          disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
252                          disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
# Line 238 | Line 263 | sg_fs_stats *sg_get_fs_stats(int *entries){
263          *entries=num_disks;    
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)
266 >           I'll ignore it :) */
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 259 | Line 298 | static void diskio_stat_destroy(sg_disk_io_stats *d) {
298   }
299  
300   VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
301 <                      diskio_stat_init, diskio_stat_destroy);
301 >                      diskio_stat_init, diskio_stat_destroy);
302  
303   #ifdef LINUX
304   typedef struct {
# 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;
328 >        kstat_ctl_t *kc;
329 >        kstat_t *ksp;
330          kstat_io_t kios;
331   #endif
332   #ifdef LINUX
# Line 288 | Line 338 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
338          int i, n;
339          time_t now;
340          const char *format;
341 +        static regex_t not_part_re, part_re;
342 +        static int re_compiled = 0;
343   #endif
344   #if defined(FREEBSD) || defined(DFBSD)
345          static struct statinfo stats;
# Line 321 | Line 373 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
373  
374          num_diskio=0;
375  
376 + #ifdef HPUX
377 +        while (1) {
378 +                num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
379 +                                    DISK_BATCH, diskidx);
380 +                if (num == -1) {
381 +                        sg_set_error_with_errno(SG_ERROR_PSTAT,
382 +                                                "pstat_getdisk");
383 +                        return NULL;
384 +                } else if (num == 0) {
385 +                        break;
386 +                }
387 +
388 +                for (i = 0; i < num; i++) {
389 +                        struct pst_diskinfo *di = &pstat_diskinfo[i];
390 +
391 +                        /* Skip "disabled" disks. */
392 +                        if (di->psd_status == 0) {
393 +                                continue;
394 +                        }
395 +        
396 +                        /* We can't seperate the reads from the writes, we'll
397 +                         * just give the same to each. (This value is in
398 +                         * 64-byte chunks according to the pstat header file,
399 +                         * and can wrap to be negative.)
400 +                         */
401 +                        rbytes = wbytes = ((unsigned long) di->psd_dkwds) * 64LL;
402 +        
403 +                        /* Skip unused disks. */
404 +                        if (rbytes == 0 && wbytes == 0) {
405 +                                continue;
406 +                        }
407 +        
408 +                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
409 +                                return NULL;
410 +                        }
411 +        
412 +                        diskio_stats_ptr = diskio_stats + num_diskio;
413 +        
414 +                        diskio_stats_ptr->read_bytes = rbytes;
415 +                        diskio_stats_ptr->write_bytes = wbytes;
416 +        
417 +                        diskio_stats_ptr->systime = time(NULL);
418 +        
419 +                        num_diskio++;
420 +        
421 +                        /* FIXME This should use a static cache, like the Linux
422 +                         * code below. */
423 +                        if (diskio_stats_ptr->disk_name == NULL) {
424 +                                dh = opendir("/dev/dsk");
425 +                                if (dh == NULL) {
426 +                                        continue;
427 +                                }
428 +        
429 +                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
430 +                                while (1) {
431 +                                        dinfo = readdir(dh);
432 +                                        if (dinfo == NULL) {
433 +                                                break;
434 +                                        }
435 +                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
436 +                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
437 +                                                continue;
438 +                                        }
439 +        
440 +                                        if (lstatinfo.st_rdev == diskid) {
441 +                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
442 +                                                        return NULL;
443 +                                                }
444 +                                                break;
445 +                                        }
446 +                                }
447 +                                closedir(dh);
448 +        
449 +                                if (diskio_stats_ptr->disk_name == NULL) {
450 +                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
451 +                                                return NULL;
452 +                                        }
453 +                                }
454 +                        }
455 +                }
456 +                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
457 +        }
458 + #endif
459   #ifdef OPENBSD
460          mib[0] = CTL_HW;
461          mib[1] = HW_DISKCOUNT;
462  
463          size = sizeof(diskcount);
464          if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
465 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
466                  return NULL;
467          }
468  
# Line 334 | Line 470 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
470          mib[1] = HW_DISKNAMES;
471  
472          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
473 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
474                  return NULL;
475          }
476  
# Line 343 | Line 480 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
480          }
481  
482          if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
483 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
484                  return NULL;
485          }
486  
# Line 361 | Line 499 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
499   #endif
500  
501          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
502 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
503                  return NULL;
504          }
505  
# Line 376 | Line 515 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
515          }
516  
517          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
518 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
519                  return NULL;
520          }
521  
# Line 388 | Line 528 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
528                  rbytes = stats[i].dk_rbytes;
529                  wbytes = stats[i].dk_wbytes;
530   #else
531 <                /* Before 1.7, NetBSD merged reads and writes. */
531 >                /* Before 2.0, NetBSD merged reads and writes. */
532                  rbytes = wbytes = stats[i].dk_bytes;
533   #endif
534   #else
535 + #ifdef HAVE_DS_RBYTES
536 +                rbytes = stats[i].ds_rbytes;
537 +                wbytes = stats[i].ds_wbytes;
538 + #else
539 +                /* Before 3.5, OpenBSD merged reads and writes */
540                  rbytes = wbytes = stats[i].ds_bytes;
541   #endif
542 + #endif
543  
544                  /* Don't keep stats for disks that have never been used. */
545                  if (rbytes == 0 && wbytes == 0) {
# Line 435 | Line 581 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
581                  stats_init = 1;
582          }
583   #ifdef FREEBSD5
584 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
584 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
585 >                /* FIXME devstat functions return a string error in
586 >                   devstat_errbuf */
587 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
588 >                return NULL;
589 >        }
590          /* Not aware of a get all devices, so i said 999. If we ever
591           * find a machine with more than 999 disks, then i'll change
592           * this number :)
593           */
594 <        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;
594 >        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) {
595 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
596 >                return NULL;
597 >        }
598   #else
599 <        if ((getdevs(&stats)) < 0) return NULL;
599 >        if ((getdevs(&stats)) < 0) {
600 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
601 >                return NULL;
602 >        }
603          /* Not aware of a get all devices, so i said 999. If we ever
604           * find a machine with more than 999 disks, then i'll change
605           * this number :)
606           */
607 <        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;
607 >        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) {
608 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
609 >                return NULL;
610 >        }
611   #endif
612  
613          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 477 | Line 637 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
637   #endif
638                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
639                  if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
640 +                        sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL);
641                          return NULL;
642                  }
643                  diskio_stats_ptr->systime=time(NULL);
# Line 488 | Line 649 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
649   #endif
650   #ifdef SOLARIS
651          if ((kc = kstat_open()) == NULL) {
652 <                return NULL;
653 <        }
652 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
653 >                return NULL;
654 >        }
655  
656          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
657 <                if (!strcmp(ksp->ks_class, "disk")) {
657 >                if (!strcmp(ksp->ks_class, "disk")) {
658  
659                          if(ksp->ks_type != KSTAT_TYPE_IO) continue;
660                          /* We dont want metadevices appearins as num_diskio */
661                          if(strcmp(ksp->ks_module, "md")==0) continue;
662 <                        if((kstat_read(kc, ksp, &kios))==-1){  
662 >                        if((kstat_read(kc, ksp, &kios))==-1){  
663                          }
664                          
665                          if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
# Line 509 | Line 671 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
671                          diskio_stats_ptr->read_bytes=kios.nread;
672                          diskio_stats_ptr->write_bytes=kios.nwritten;
673                          if (sg_update_string(&diskio_stats_ptr->disk_name,
674 <                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
674 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
675                                  return NULL;
676                          }
677                          diskio_stats_ptr->systime=time(NULL);
# Line 539 | Line 701 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
701          if (f == NULL) goto out;
702          now = time(NULL);
703  
704 +        if (!re_compiled) {
705 +                if (regcomp(&part_re, "^(.*/)?[^/]*[0-9]$", REG_EXTENDED | REG_NOSUB) != 0) {
706 +                        sg_set_error(SG_ERROR_PARSE, NULL);
707 +                        goto out;
708 +                }
709 +                if (regcomp(&not_part_re, "^(.*/)?[^/0-9]+[0-9]+d[0-9]+$", REG_EXTENDED | REG_NOSUB) != 0) {
710 +                        sg_set_error(SG_ERROR_PARSE, NULL);
711 +                        goto out;
712 +                }
713 +                re_compiled = 1;
714 +        }
715 +
716          while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
717                  char name[100];
544                char *s;
718                  long long rsect, wsect;
719  
720                  int nr = sscanf(line_ptr, format,
# Line 549 | Line 722 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
722                  if (nr < 3) continue;
723  
724                  /* Skip device names ending in numbers, since they're
725 <                   partitions. */
726 <                s = name;
727 <                while (*s != '\0') s++;
728 <                --s;
729 <                if (*s >= '0' && *s <= '9') continue;
725 >                   partitions, unless they match the c0d0 pattern that some
726 >                   RAID devices use. */
727 >                /* FIXME: For 2.6+, we should probably be using sysfs to detect
728 >                   this... */
729 >                if ((regexec(&part_re, name, 0, NULL, 0) == 0)
730 >                    && (regexec(&not_part_re, name, 0, NULL, 0) != 0)) {
731 >                        continue;
732 >                }
733  
734                  if (nr < 5) {
735                          has_pp_stats = 0;
# Line 656 | Line 832 | out:
832   #endif
833  
834   #ifdef CYGWIN
835 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
836          return NULL;
837   #endif
838  
# Line 666 | Line 843 | out:
843  
844   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
845          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
846 <                              diskio_stat_init, diskio_stat_destroy);
846 >                              diskio_stat_init, diskio_stat_destroy);
847          sg_disk_io_stats *src = NULL, *dest;
848          int i, j, diff_count, new_count;
849  
# Line 726 | Line 903 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
903  
904          *entries = diff_count;
905          return diff;
906 + }
907 +
908 + int sg_disk_io_compare_name(const void *va, const void *vb) {
909 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
910 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
911 +
912 +        return strcmp(a->disk_name, b->disk_name);
913   }
914  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines