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.73 by tdb, Mon Nov 1 18:30:17 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 + #endif
91 +
92   static void disk_stat_init(sg_fs_stats *d) {
93          d->device_name = NULL;
94          d->fs_type = NULL;
# Line 106 | Line 119 | sg_fs_stats *sg_get_fs_stats(int *entries){
119  
120          int valid_type;
121          int num_disks=0;
122 < #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
122 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX)
123          FILE *f;
124   #endif
125  
# Line 116 | Line 129 | sg_fs_stats *sg_get_fs_stats(int *entries){
129          struct mnttab mp;
130          struct statvfs fs;
131   #endif
132 < #if defined(LINUX) || defined(CYGWIN)
132 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
133          struct mntent *mp;
134          struct statfs fs;
135   #endif
136   #ifdef ALLBSD
137          int nummnt;
138 + #ifdef HAVE_STATVFS
139 +        struct statvfs *mp;
140 + #else
141          struct statfs *mp;
142   #endif
143 + #endif
144  
145   #ifdef ALLBSD
146          nummnt=getmntinfo(&mp , MNT_LOCAL);
147          if (nummnt<=0){
148 +                sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL);
149                  return NULL;
150          }
151          for(;nummnt--; mp++){
152                  valid_type = is_valid_fs_type(mp->f_fstypename);
153   #endif
154  
155 < #if defined(LINUX) || defined(CYGWIN)
155 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
156 > #ifdef MNT_MNTTAB
157 >        if ((f=setmntent(MNT_MNTTAB, "r" ))==NULL){
158 > #else
159          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
160 + #endif
161 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
162                  return NULL;
163          }
164  
# Line 149 | Line 172 | sg_fs_stats *sg_get_fs_stats(int *entries){
172  
173   #ifdef SOLARIS
174          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
175 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/etc/mnttab");
176                  return NULL;
177          }
178          while((getmntent(f, &mp)) == 0){
# Line 184 | Line 208 | sg_fs_stats *sg_get_fs_stats(int *entries){
208                          /* Freebsd doesn't have a "available" inodes */
209                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
210   #endif
211 < #if defined(LINUX) || defined(CYGWIN)
211 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
212                          if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
213                                  return NULL;
214                          }
# Line 239 | Line 263 | sg_fs_stats *sg_get_fs_stats(int *entries){
263  
264          /* If this fails, there is very little i can do about it, so
265             I'll ignore it :) */
266 < #if defined(LINUX) || defined(CYGWIN)
266 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
267          endmntent(f);
268   #endif
269   #if defined(SOLARIS)
# Line 250 | Line 274 | sg_fs_stats *sg_get_fs_stats(int *entries){
274  
275   }
276  
277 + int sg_fs_compare_device_name(const void *va, const void *vb) {
278 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
279 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
280 +
281 +        return strcmp(a->device_name, b->device_name);
282 + }
283 +
284 + int sg_fs_compare_mnt_point(const void *va, const void *vb) {
285 +        const sg_fs_stats *a = (const sg_fs_stats *)va;
286 +        const sg_fs_stats *b = (const sg_fs_stats *)vb;
287 +
288 +        return strcmp(a->mnt_point, b->mnt_point);
289 + }
290 +
291   static void diskio_stat_init(sg_disk_io_stats *d) {
292          d->disk_name = NULL;
293   }
# Line 274 | Line 312 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
312          sg_disk_io_stats *diskio_stats_ptr;
313   #endif
314  
315 + #ifdef HPUX
316 +        long long rbytes = 0, wbytes = 0;
317 +        struct dirent *dinfo = NULL;
318 +        struct stat lstatinfo;
319 +        struct pst_diskinfo pstat_diskinfo;
320 +        char fullpathbuf[1024] = {0};
321 +        dev_t diskid;
322 +        DIR *dh = NULL;
323 +        int disknum = 0;
324 + #endif
325   #ifdef SOLARIS
326          kstat_ctl_t *kc;
327          kstat_t *ksp;
# Line 321 | Line 369 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
369  
370          num_diskio=0;
371  
372 + #ifdef HPUX
373 +
374 +        /* The "128" here is arbitrary, it can be increased to any number
375 +           at the expense of only more system calls to pstat(). */
376 +        for (disknum = 0; disknum < 128; disknum++) {
377 +                if (pstat_getdisk(&pstat_diskinfo, sizeof(pstat_diskinfo), 1, disknum) == -1) {
378 +                        break;
379 +                }
380 +
381 +                if (pstat_diskinfo.psd_idx != disknum) {
382 +                        continue;
383 +                }
384 +
385 +                /* Skip "disabled" disks.. */
386 +                if (pstat_diskinfo.psd_status == 0) {
387 +                        continue;
388 +                }
389 +
390 +                /* We can't seperate the reads from the writes, we'll
391 +                   just give half to each. */
392 +                rbytes = wbytes = (pstat_diskinfo.psd_dkwds * 64);
393 +
394 +                /* Skip unused disks. */
395 +                if (rbytes == 0 && wbytes == 0) {
396 +                        continue;
397 +                }
398 +
399 +                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
400 +                        return NULL;
401 +                }
402 +
403 +                diskio_stats_ptr = diskio_stats + num_diskio;
404 +
405 +                diskio_stats_ptr->read_bytes = rbytes;
406 +                diskio_stats_ptr->write_bytes = wbytes;
407 +
408 +                diskio_stats_ptr->systime = time(NULL);
409 +
410 +                num_diskio++;
411 +
412 +                if (diskio_stats_ptr->disk_name == NULL) {
413 +                        dh = opendir("/dev/dsk");
414 +                        if (dh == NULL) {
415 +                                continue;
416 +                        }
417 +
418 +                        diskid = (pstat_diskinfo.psd_dev.psd_major << 24) | pstat_diskinfo.psd_dev.psd_minor;
419 +                        while (1) {
420 +                                dinfo = readdir(dh);
421 +                                if (dinfo == NULL) {
422 +                                        break;
423 +                                }
424 +                                snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
425 +                                if (lstat(fullpathbuf, &lstatinfo) < 0) {
426 +                                        continue;
427 +                                }
428 +
429 +                                if (lstatinfo.st_rdev == diskid) {
430 +                                        if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
431 +                                                return NULL;
432 +                                        }
433 +                                        break;
434 +                                }
435 +                        }
436 +                        closedir(dh);
437 +
438 +                        if (diskio_stats_ptr->disk_name == NULL) {
439 +                                if (sg_update_string(&diskio_stats_ptr->disk_name, pstat_diskinfo.psd_hw_path.psh_name) < 0) {
440 +                                        return NULL;
441 +                                }
442 +                        }
443 +                }
444 +        }
445 + #endif
446   #ifdef OPENBSD
447          mib[0] = CTL_HW;
448          mib[1] = HW_DISKCOUNT;
449  
450          size = sizeof(diskcount);
451          if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
452 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
453                  return NULL;
454          }
455  
# Line 334 | Line 457 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
457          mib[1] = HW_DISKNAMES;
458  
459          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
460 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
461                  return NULL;
462          }
463  
# Line 343 | Line 467 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
467          }
468  
469          if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
470 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
471                  return NULL;
472          }
473  
# Line 361 | Line 486 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
486   #endif
487  
488          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
489 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
490                  return NULL;
491          }
492  
# Line 376 | Line 502 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
502          }
503  
504          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
505 +                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
506                  return NULL;
507          }
508  
# Line 388 | Line 515 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
515                  rbytes = stats[i].dk_rbytes;
516                  wbytes = stats[i].dk_wbytes;
517   #else
518 <                /* Before 1.7, NetBSD merged reads and writes. */
518 >                /* Before 2.0, NetBSD merged reads and writes. */
519                  rbytes = wbytes = stats[i].dk_bytes;
520   #endif
521   #else
522 + #ifdef HAVE_DS_RBYTES
523 +                rbytes = stats[i].ds_rbytes;
524 +                wbytes = stats[i].ds_wbytes;
525 + #else
526 +                /* Before 3.5, OpenBSD merged reads and writes */
527                  rbytes = wbytes = stats[i].ds_bytes;
528   #endif
529 + #endif
530  
531                  /* Don't keep stats for disks that have never been used. */
532                  if (rbytes == 0 && wbytes == 0) {
# Line 435 | Line 568 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
568                  stats_init = 1;
569          }
570   #ifdef FREEBSD5
571 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
571 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
572 >                /* FIXME devstat functions return a string error in
573 >                   devstat_errbuf */
574 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
575 >                return NULL;
576 >        }
577          /* Not aware of a get all devices, so i said 999. If we ever
578           * find a machine with more than 999 disks, then i'll change
579           * this number :)
580           */
581 <        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;
581 >        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) {
582 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
583 >                return NULL;
584 >        }
585   #else
586 <        if ((getdevs(&stats)) < 0) return NULL;
586 >        if ((getdevs(&stats)) < 0) {
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 (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 (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   #endif
599  
600          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 477 | Line 624 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
624   #endif
625                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
626                  if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
627 +                        sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL);
628                          return NULL;
629                  }
630                  diskio_stats_ptr->systime=time(NULL);
# Line 488 | Line 636 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
636   #endif
637   #ifdef SOLARIS
638          if ((kc = kstat_open()) == NULL) {
639 +                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
640                  return NULL;
641          }
642  
# Line 656 | Line 805 | out:
805   #endif
806  
807   #ifdef CYGWIN
808 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
809          return NULL;
810   #endif
811  
# Line 726 | Line 876 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
876  
877          *entries = diff_count;
878          return diff;
879 + }
880 +
881 + int sg_disk_io_compare_name(const void *va, const void *vb) {
882 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
883 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
884 +
885 +        return strcmp(a->disk_name, b->disk_name);
886   }
887  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines