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.82 by tdb, Wed Jul 13 13:01:24 2005 UTC vs.
Revision 1.89 by tdb, Sun Oct 3 18:35:57 2010 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", "vxfs"}
40 > #define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs", "nfs", "zfs"}
41   #endif
42  
43   #if defined(LINUX) || defined(CYGWIN)
# Line 48 | Line 48
48  
49   #ifdef LINUX
50   #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
51 <                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
52 <                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
53 <                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
54 <                        "umsdos", "vfat", "xfs", "jfs", "nfs"}
51 >                        "ext3", "ext4", "vxfs", "hfs", "hfsplus", "hpfs", \
52 >                        "jffs", "jffs2", "minix", "msdos", "ntfs", "qnx4", \
53 >                        "ramfs", "rootfs", "reiserfs", "sysv", "v7", "udf", \
54 >                        "ufs", "umsdos", "vfat", "xfs", "jfs", "nfs", \
55 >                        "ocfs", "cifs"}
56   #endif
57  
58   #ifdef CYGWIN
# Line 66 | Line 67
67   #if defined(FREEBSD) || defined(DFBSD)
68   #include <sys/dkstat.h>
69   #include <devstat.h>
70 < #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
71 <                        "ufs", "mfs", "nfs"}
70 > #include <sys/param.h>
71 > #include <sys/mount.h>
72 > #include <sys/sysctl.h>
73 > /*#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
74 >                        "ufs", "mfs", "nfs", "zfs", "tmpfs", "reiserfs", \
75 >                        "xfs"}*/
76 > static char **VALID_FS_TYPES;
77 > static size_t n_valid_fs_types;
78   #endif
79   #if defined(NETBSD) || defined(OPENBSD)
80   #include <sys/param.h>
# Line 87 | Line 94
94   #include <dirent.h>
95   #include <stdio.h>
96   #include <time.h>
97 < #define VALID_FS_TYPES {"vxfs", "hfs"}
97 > #define VALID_FS_TYPES {"vxfs", "hfs", "nfs"}
98   #define DISK_BATCH 30
99   #endif
100  
101 + #ifdef AIX
102 + #include <unistd.h>
103 + #include <errno.h>
104 + #include <ctype.h>
105 + #include <fcntl.h>
106 + #include <fstab.h>
107 + #include <sys/statfs.h>
108 + #include <sys/mntctl.h>
109 + #include <sys/vmount.h>
110 + #include <libperfstat.h>
111 + #define KNOWN_VALID_FS_TYPES {          \
112 +        "jfs2",         /*  0 */        \
113 +        "namefs",       /*  1 */        \
114 +        "nfs",          /*  2 */        \
115 +        "jfs",          /*  3 */        \
116 +        NULL,           /*  4 */        \
117 +        "cdrom",        /*  5 */        \
118 +        "procfs",       /*  6 */        \
119 +        NULL,           /*  7 */        \
120 +        NULL,           /*  8 */        \
121 +        NULL,           /*  9 */        \
122 +        NULL,           /* 10 */        \
123 +        NULL,           /* 11 */        \
124 +        NULL,           /* 12 */        \
125 +        NULL,           /* 13 */        \
126 +        NULL,           /* 14 */        \
127 +        NULL,           /* 15 */        \
128 +        "sfs",          /* 16 */        \
129 +        "cachefs",      /* 17 */        \
130 +        "nfs3",         /* 18 */        \
131 +        "autofs",       /* 19 */        \
132 +        NULL,           /* 20 */        \
133 +        NULL,           /* 21 */        \
134 +        NULL,           /* 22 */        \
135 +        NULL,           /* 23 */        \
136 +        NULL,           /* 24 */        \
137 +        NULL,           /* 25 */        \
138 +        NULL,           /* 26 */        \
139 +        NULL,           /* 27 */        \
140 +        NULL,           /* 28 */        \
141 +        NULL,           /* 29 */        \
142 +        NULL,           /* 30 */        \
143 +        NULL,           /* 31 */        \
144 +        "vxfs",         /* 32 */        \
145 +        "vxodm",        /* 33 */        \
146 +        "udf",          /* 34 */        \
147 +        "nfs4",         /* 35 */        \
148 +        "rfs4",         /* 36 */        \
149 +        "cifs"          /* 37 */        \
150 + }
151 + static char *VALID_FS_TYPES[MNT_AIXLAST + 1] = KNOWN_VALID_FS_TYPES;
152 + extern int mntctl(int, int, void *);
153 + #endif
154 +
155 + #ifdef WIN32
156 + #include "win32.h"
157 + /*#define VALID_FS_TYPES {"NTFS", "FAT", "FAT32"} unused*/
158 + #define BUFSIZE 512
159 + #endif
160 +
161   #ifdef ALLBSD
162   #define SG_MP_FSTYPENAME(mp) (mp)->f_fstypename
163   #define SG_MP_DEVNAME(mp)    (mp)->f_mntfromname
# Line 130 | Line 197
197   #define SG_FS_FFREE(fs)      (long long) (fs).f_ffree
198   #define SG_FS_FAVAIL(fs)     (long long) (fs).f_favail
199   #endif
200 + #ifdef AIX
201 + #define SG_MP_FSTYPENAME(mp) VALID_FS_TYPES[(mp)->vmt_gfstype]
202 + #define SG_MP_DEVNAME(mp)    vmt2dataptr((mp),0)
203 + #define SG_MP_MOUNTP(mp)     vmt2dataptr((mp),1)
204 + #define SG_FS_FRSIZE(fs)     (fs).f_fsize
205 + #define SG_FS_BSIZE(fs)      (fs).f_blocks
206 + #define SG_FS_BLOCKS(fs)     (fs).f_blocks
207 + #define SG_FS_BFREE(fs)      (fs).f_bfree
208 + #define SG_FS_BAVAIL(fs)     (fs).f_bavail
209 + #define SG_FS_FILES(fs)      (fs).f_files
210 + #define SG_FS_FFREE(fs)      (fs).f_ffree
211 + #define SG_FS_FAVAIL(fs)     ((fs).f_files - (fs).f_ffree)
212 + #endif
213  
214   static void disk_stat_init(sg_fs_stats *d) {
215          d->device_name = NULL;
# Line 143 | Line 223 | static void disk_stat_destroy(sg_fs_stats *d) {
223          free(d->mnt_point);
224   }
225  
226 + static int valid_fs_types_initialized = 0;
227 +
228 + static int init_valid_fs_types() {
229 + #if defined(FREEBSD) || defined(DFBSD)
230 +        struct xvfsconf *xvfsp;
231 +        size_t buflen;
232 +        int nbvfs = 0;
233 +
234 +        if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
235 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vfs.conflist");
236 +                return 0;
237 +        }
238 +        xvfsp = alloca(buflen);
239 +        if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
240 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vfs.conflist");
241 +                return 0;
242 +        }
243 +        n_valid_fs_types = buflen / sizeof(struct xvfsconf);
244 +        VALID_FS_TYPES = malloc( n_valid_fs_types * sizeof(char **) );
245 +        if( 0 == VALID_FS_TYPES ) {
246 +                sg_set_error_with_errno(SG_ERROR_MALLOC, "init_valid_fs_types");
247 +                return 0;
248 +        }
249 +        for (i = 0; i < n_valid_fs_types; i++) {
250 +                VALID_FS_TYPES[i] = strdup(xvfsp[i].vfc_name);
251 +        }
252 + #elif defined(AIX)
253 +        FILE *fh;
254 +
255 +        fh = fopen("/etc/vfs", "r");
256 +        if( 0 != fh ) {
257 +                char line[4096];
258 +
259 +                while( fgets( line, 4096, fh ) ) {
260 +                        char fstype[16], mnt_helper[PATH_MAX], filesys_helper[PATH_MAX], remote[16];
261 +                        int  fstype_id;
262 +                        if( line[0] < 'a' || line[0] > 'z' ) {
263 +                                continue; /* not a valid fstype */
264 +                        }
265 +                        sscanf(line, "%s %d %s %s %s", fstype, &fstype_id, mnt_helper, filesys_helper, remote);
266 +                        if( VALID_FS_TYPES[fstype_id] != NULL ) {
267 +                                continue; /* we already know you ... */
268 +                        }
269 +                        VALID_FS_TYPES[fstype_id] = strdup(fstype);
270 +                }
271 +
272 +                fclose(fh);
273 +        }
274 +        return 1;
275 + #else
276 +        return 1;
277 + #endif
278 + }
279 +
280 + #ifndef WIN32 /* not used by WIN32, so stop compiler throwing warnings */
281   static int is_valid_fs_type(const char *type) {
282 +        size_t i;
283 + #if defined(AIX)
284 +        char **types = VALID_FS_TYPES;
285 +        size_t n_valid_fs_types = sizeof VALID_FS_TYPES / sizeof *VALID_FS_TYPES;
286 + #elif !defined(FREEBSD) && !defined(DFBSD)
287          const char *types[] = VALID_FS_TYPES;
288 <        int i;
288 >        size_t n_valid_fs_types = sizeof types / sizeof *types;
289 > #else
290 >        char **types = VALID_FS_TYPES;
291 > #endif
292  
293 <        for (i = 0; i < (int) (sizeof types / sizeof *types); i++) {
293 >        for (i = 0; i < n_valid_fs_types; i++) {
294 >                if( types[i] == NULL) {
295 >                        continue;
296 >                }
297                  if (strcmp(types[i], type) == 0) {
298                          return 1;
299                  }
300          }
301 +
302          return 0;
303   }
304 + #endif
305  
306   sg_fs_stats *sg_get_fs_stats(int *entries){
307          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
# Line 182 | Line 330 | sg_fs_stats *sg_get_fs_stats(int *entries){
330          struct statfs *mp, **fs;
331   #endif
332   #endif
333 + #ifdef WIN32
334 +        char lp_buf[MAX_PATH];
335 +        char volume_name_buf[BUFSIZE];
336 +        char filesys_name_buf[BUFSIZE];
337 +        char drive[4] = " :\\";
338 +        char *p;
339 +        lp_buf[0]='\0';
340 + #endif
341 + #ifdef AIX
342 +        struct vmount *buf, *mp;
343 +        int rc, i;
344 +        size_t bufsize = 4096;
345 +        struct statfs64 fs;
346 + #endif
347  
348 +        if( !valid_fs_types_initialized ) {
349 +                init_valid_fs_types();
350 +                valid_fs_types_initialized = 1;
351 +        }
352 +
353   #ifdef ALLBSD
354          nummnt=getmntinfo(&mp, MNT_WAIT);
355          if (nummnt<=0){
# Line 220 | Line 387 | sg_fs_stats *sg_get_fs_stats(int *entries){
387                  }
388   #endif
389  
390 + #ifdef AIX
391 +        buf = malloc( bufsize );
392 +        if( 0 == buf ) {
393 +                sg_set_error_with_errno(SG_ERROR_MALLOC, "mntctl");
394 +                return NULL;
395 +        }
396 +
397 +        rc = mntctl( MCTL_QUERY, bufsize, buf );
398 +        if( 0 == rc )
399 +        {
400 +                bufsize = buf->vmt_revision;
401 +                void *newbuf = realloc( buf, bufsize );
402 +                if( 0 == newbuf ) {
403 +                        sg_set_error_with_errno(SG_ERROR_MALLOC, "mntctl");
404 +                        free(buf);
405 +                        return NULL;
406 +                }
407 +                buf = newbuf;
408 +
409 +                rc = mntctl( MCTL_QUERY, bufsize, buf );
410 +        }
411 +
412 +        if( -1 == rc ) {
413 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "mntctl");
414 +                return NULL;
415 +        }
416 +
417 +        for( i = 0, mp = buf;
418 +             i < rc;
419 +             ++i, mp = (struct vmount *)(((char *)mp) + mp->vmt_length ) )
420 +        {
421 +                int fd;
422 +                if((fd = open(vmt2dataptr((mp),1), O_RDONLY)) == -1) {
423 +                        continue;
424 +                }
425 +                if(fstatfs64(fd, &fs) == -1) {
426 +                        continue;
427 +                }
428 +
429 + #endif
430 +
431 + #ifdef WIN32
432 +        if (!(GetLogicalDriveStrings(BUFSIZE-1, lp_buf))) {
433 +                sg_set_error(SG_ERROR_GETMNTINFO, "GetLogicalDriveStrings");
434 +                return NULL;
435 +        }
436 +        p = lp_buf;
437 +        do {
438 +                // Copy drive letter to template string
439 +                *drive = *p;
440 +                // Only interested in harddrives.
441 +                int drive_type = GetDriveType(drive);
442 +
443 +                if(drive_type == DRIVE_FIXED) {
444 + #else
445                  if(is_valid_fs_type(SG_MP_FSTYPENAME(mp))){
446 + #endif
447                          if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
448                                  return NULL;
449                          }
450                          disk_ptr=disk_stats+num_disks;
451  
452 + #ifndef WIN32
453                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
454                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
455                           * be upwards of a k each
# Line 243 | Line 467 | sg_fs_stats *sg_get_fs_stats(int *entries){
467                          disk_ptr->size  = SG_FS_FRSIZE(fs) * SG_FS_BLOCKS(fs);
468                          disk_ptr->avail = SG_FS_FRSIZE(fs) * SG_FS_BAVAIL(fs);
469                          disk_ptr->used  = (disk_ptr->size) - (SG_FS_FRSIZE(fs) * SG_FS_BFREE(fs));
470 <
470 >                
471                          disk_ptr->total_inodes = SG_FS_FILES(fs);
472                          disk_ptr->free_inodes  = SG_FS_FFREE(fs);
473                          /* Linux, FreeBSD don't have a "available" inodes */
# Line 256 | Line 480 | sg_fs_stats *sg_get_fs_stats(int *entries){
480                          disk_ptr->free_blocks  = SG_FS_BFREE(fs);
481                          disk_ptr->avail_blocks = SG_FS_BAVAIL(fs);
482                          disk_ptr->used_blocks  = disk_ptr->total_blocks - disk_ptr->free_blocks;
483 + #else
484 +                        if(!GetVolumeInformation(drive, volume_name_buf, BUFSIZE,
485 +                                                NULL, NULL, NULL,
486 +                                                filesys_name_buf, BUFSIZE)) {
487 +                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
488 +                                        "GetVolumeInformation");
489 +                                return NULL;
490 +                        }
491  
492 +                        if (sg_update_string(&disk_ptr->device_name,
493 +                                                volume_name_buf) < 0) {
494 +                                return NULL;
495 +                        }
496 +                        if (sg_update_string(&disk_ptr->fs_type,
497 +                                                filesys_name_buf) < 0) {
498 +                                return NULL;
499 +                        }
500 +                        if (sg_update_string(&disk_ptr->mnt_point,
501 +                                                drive) < 0) {
502 +                                return NULL;
503 +                        }
504 +                        if (!GetDiskFreeSpaceEx(drive, NULL,
505 +                                        (PULARGE_INTEGER)&disk_ptr->size,
506 +                                        (PULARGE_INTEGER)&disk_ptr->avail)) {
507 +                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
508 +                                        "GetDiskFreeSpaceEx");
509 +                                return NULL;
510 +                        }
511 +                        disk_ptr->used = disk_ptr->size - disk_ptr->avail;
512 +                        disk_ptr->total_inodes = 0;
513 +                        disk_ptr->free_inodes  = 0;
514 +                        disk_ptr->used_inodes  = 0;
515 +                        disk_ptr->avail_inodes = 0;
516 +
517 +                        /* I dunno what to do with these... so have nothing */
518 +                        disk_ptr->io_size = 0;
519 +                        disk_ptr->block_size = 0;
520 +                        disk_ptr->total_blocks = 0;
521 +                        disk_ptr->free_blocks = 0;
522 +                        disk_ptr->avail_blocks = 0;
523 +                        disk_ptr->used_blocks = 0;
524 + #endif
525                          num_disks++;
526                  }
527 + #ifdef WIN32
528 +                while(*p++);
529 +        } while(*p);
530 + #else
531          }
532 + #endif
533  
534          *entries=num_disks;    
535  
# Line 371 | Line 641 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
641          int mib[MIBSIZE];
642          size_t size;
643   #endif
644 + #ifdef AIX
645 +        int ret, disks;
646 +        perfstat_disk_t *dskperf;
647 +        perfstat_id_t name;
648 + #endif
649 + #ifdef WIN32
650 +        char *name;
651 +        long long rbytes;
652 +        long long wbytes;
653 + #endif
654  
655          num_diskio=0;
656  
657 + #ifdef AIX
658 +        /* check how many perfstat_disk_t structures are available */
659 +        disks = perfstat_disk(NULL, NULL, sizeof(perfstat_disk_t), 0);
660 +        if(disks==-1) {
661 +                sg_set_error_with_errno(SG_ERROR_PSTAT, "perfstat_disk(NULL)");
662 +                return NULL;
663 +        }
664 +
665 +        dskperf = malloc( sizeof(perfstat_disk_t) * disks);
666 +        if( 0 == dskperf ) {
667 +                sg_set_error_with_errno(SG_ERROR_MALLOC, "sg_get_disk_io_stats");
668 +                return NULL;
669 +        }
670 +
671 +        name.name[0]=0;
672 +        ret = perfstat_disk(&name, dskperf, sizeof(perfstat_disk_t), disks);
673 +        if(ret == -1) {
674 +                sg_set_error_with_errno(SG_ERROR_PSTAT, "perfstat_disk");
675 +                return NULL;
676 +        }
677 +
678 +        for (num_diskio = 0; num_diskio < ret; num_diskio++) {
679 +
680 +                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
681 +                        return NULL;
682 +                }
683 +
684 +                diskio_stats_ptr = diskio_stats + num_diskio;
685 +
686 +                diskio_stats_ptr->read_bytes = dskperf[num_diskio].bsize * dskperf[num_diskio].rblks;
687 +                diskio_stats_ptr->write_bytes = dskperf[num_diskio].bsize * dskperf[num_diskio].wblks;
688 +                diskio_stats_ptr->systime = dskperf[num_diskio].time;
689 +
690 +                if (diskio_stats_ptr->disk_name == NULL) {
691 +                        int i;
692 +                        for(i = 0; i < IDENTIFIER_LENGTH; ++i) {
693 +                                char *s = dskperf[num_diskio].name + i;
694 +                                if( !(isalpha(*s) || isdigit(*s) ||
695 +                                      *s == '-' || *s == '_' || *s == ' ') ) {
696 +                                        *s = 0;
697 +                                        break;
698 +                                }
699 +                        }
700 +                        if (sg_update_string(&diskio_stats_ptr->disk_name, dskperf[num_diskio].name) < 0) {
701 +                                return NULL;
702 +                        }
703 +                }
704 +        }
705 + #endif
706 +
707   #ifdef HPUX
708          while (1) {
709                  num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
# Line 673 | Line 1003 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
1003                          diskio_stats_ptr->write_bytes=kios.nwritten;
1004                          if (sg_update_string(&diskio_stats_ptr->disk_name,
1005                                               sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
1006 +                                kstat_close(kc);
1007                                  return NULL;
1008                          }
1009                          diskio_stats_ptr->systime=time(NULL);
# Line 768 | Line 1099 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
1099                  f = fopen("/proc/stat", "r");
1100                  if (f == NULL) goto out;
1101                  now = time(NULL);
1102 <        
1102 >
1103                  line_ptr = sg_f_read_line(f, "disk_io:");
1104                  if (line_ptr == NULL) goto out;
1105 <        
1105 >
1106                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
1107                          long long rsect, wsect;
1108  
1109                          if (*++line_ptr == '\0') break;
1110 <        
1110 >
1111                          if((sscanf(line_ptr,
1112                                  "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
1113                                  &major, &minor, &rsect, &wsect)) != 4) {
# Line 837 | Line 1168 | out:
1168          return NULL;
1169   #endif
1170  
1171 + #ifdef WIN32
1172 +        sg_set_error(SG_ERROR_NONE, NULL);
1173 +
1174 +        while((name = get_diskio(num_diskio, &rbytes, &wbytes)) != NULL) {
1175 +                if (VECTOR_RESIZE(diskio_stats, num_diskio+1)) {
1176 +                        return NULL;
1177 +                }
1178 +
1179 +                diskio_stats_ptr = diskio_stats + num_diskio;
1180 +
1181 +                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
1182 +                        return NULL;
1183 +                }
1184 +                sg_update_string(&name, NULL);
1185 +                diskio_stats_ptr->read_bytes = rbytes;
1186 +                diskio_stats_ptr->write_bytes = wbytes;
1187 +
1188 +                diskio_stats_ptr->systime = 0;
1189 +
1190 +                num_diskio++;
1191 +        }
1192 + #endif
1193 +
1194          *entries=num_diskio;
1195  
1196          return diskio_stats;
1197   }
1198  
1199   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
1200 + #ifndef WIN32
1201          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
1202                                diskio_stat_init, diskio_stat_destroy);
1203          sg_disk_io_stats *src = NULL, *dest;
# Line 904 | Line 1259 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
1259  
1260          *entries = diff_count;
1261          return diff;
1262 + #else /* WIN32 */
1263 +        return sg_get_disk_io_stats(entries);
1264 + #endif
1265   }
1266  
1267   int sg_disk_io_compare_name(const void *va, const void *vb) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines