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.67 by ats, Thu Apr 8 13:44:44 2004 UTC vs.
Revision 1.77 by ats, Sat Nov 6 15:36:29 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 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(SG_ERROR_GETMNTINFO, NULL);
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          }
# Line 151 | 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(SG_ERROR_OPEN, "/etc/mnttab");
176 >                sg_set_error_with_errno(SG_ERROR_OPEN, "/etc/mnttab");
177                  return NULL;
178          }
179          while((getmntent(f, &mp)) == 0){
# Line 187 | 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 242 | 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 291 | 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 338 | 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. */
396 +                        rbytes = wbytes = di->psd_dkwds / 2;
397 +        
398 +                        /* Skip unused disks. */
399 +                        if (rbytes == 0 && wbytes == 0) {
400 +                                continue;
401 +                        }
402 +        
403 +                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
404 +                                return NULL;
405 +                        }
406 +        
407 +                        diskio_stats_ptr = diskio_stats + num_diskio;
408 +        
409 +                        diskio_stats_ptr->read_bytes = rbytes;
410 +                        diskio_stats_ptr->write_bytes = wbytes;
411 +        
412 +                        diskio_stats_ptr->systime = time(NULL);
413 +        
414 +                        num_diskio++;
415 +        
416 +                        /* FIXME This should use a static cache, like the Linux
417 +                         * code below. */
418 +                        if (diskio_stats_ptr->disk_name == NULL) {
419 +                                dh = opendir("/dev/dsk");
420 +                                if (dh == NULL) {
421 +                                        continue;
422 +                                }
423 +        
424 +                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
425 +                                while (1) {
426 +                                        dinfo = readdir(dh);
427 +                                        if (dinfo == NULL) {
428 +                                                break;
429 +                                        }
430 +                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
431 +                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
432 +                                                continue;
433 +                                        }
434 +        
435 +                                        if (lstatinfo.st_rdev == diskid) {
436 +                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
437 +                                                        return NULL;
438 +                                                }
439 +                                                break;
440 +                                        }
441 +                                }
442 +                                closedir(dh);
443 +        
444 +                                if (diskio_stats_ptr->disk_name == NULL) {
445 +                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
446 +                                                return NULL;
447 +                                        }
448 +                                }
449 +                        }
450 +                }
451 +                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
452 +        }
453 + #endif
454   #ifdef OPENBSD
455          mib[0] = CTL_HW;
456          mib[1] = HW_DISKCOUNT;
457  
458          size = sizeof(diskcount);
459          if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
460 <                sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
460 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
461                  return NULL;
462          }
463  
# Line 352 | Line 465 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
465          mib[1] = HW_DISKNAMES;
466  
467          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
468 <                sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
468 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
469                  return NULL;
470          }
471  
# Line 362 | Line 475 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
475          }
476  
477          if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
478 <                sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
478 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
479                  return NULL;
480          }
481  
# Line 381 | Line 494 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
494   #endif
495  
496          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
497 <                sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
497 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
498                  return NULL;
499          }
500  
# Line 397 | Line 510 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
510          }
511  
512          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
513 <                sg_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
513 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
514                  return NULL;
515          }
516  
# Line 410 | Line 523 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
523                  rbytes = stats[i].dk_rbytes;
524                  wbytes = stats[i].dk_wbytes;
525   #else
526 <                /* Before 1.7, NetBSD merged reads and writes. */
526 >                /* Before 2.0, NetBSD merged reads and writes. */
527                  rbytes = wbytes = stats[i].dk_bytes;
528   #endif
529   #else
530 + #ifdef HAVE_DS_RBYTES
531 +                rbytes = stats[i].ds_rbytes;
532 +                wbytes = stats[i].ds_wbytes;
533 + #else
534 +                /* Before 3.5, OpenBSD merged reads and writes */
535                  rbytes = wbytes = stats[i].ds_bytes;
536   #endif
537 + #endif
538  
539                  /* Don't keep stats for disks that have never been used. */
540                  if (rbytes == 0 && wbytes == 0) {
# Line 458 | Line 577 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
577          }
578   #ifdef FREEBSD5
579          if ((devstat_getdevs(NULL, &stats)) < 0) {
580 +                /* FIXME devstat functions return a string error in
581 +                   devstat_errbuf */
582                  sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
583                  return NULL;
584          }
# Line 511 | Line 632 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
632   #endif
633                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
634                  if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
635 <                        sg_set_error(SG_ERROR_ASPRINTF, NULL);
635 >                        sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL);
636                          return NULL;
637                  }
638                  diskio_stats_ptr->systime=time(NULL);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines