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.74 by ats, Mon Nov 1 20:36:46 2004 UTC vs.
Revision 1.88 by tdb, Fri Dec 4 22:09:30 2009 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)
44   #include <mntent.h>
45   #include <sys/vfs.h>
46 + #include <sys/statvfs.h>
47   #endif
48  
49   #ifdef LINUX
# Line 50 | Line 51
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"}
54 >                        "umsdos", "vfat", "xfs", "jfs", "nfs"}
55   #endif
56  
57   #ifdef CYGWIN
# Line 65 | Line 66
66   #if defined(FREEBSD) || defined(DFBSD)
67   #include <sys/dkstat.h>
68   #include <devstat.h>
69 < #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
70 <                        "ufs", "mfs"}
69 > #include <sys/param.h>
70 > #include <sys/mount.h>
71 > #include <sys/sysctl.h>
72 > /*#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
73 >                        "ufs", "mfs", "nfs", "zfs", "tmpfs", "reiserfs", \
74 >                        "xfs"}*/
75   #endif
76   #if defined(NETBSD) || defined(OPENBSD)
77   #include <sys/param.h>
78   #include <sys/sysctl.h>
79   #include <sys/disk.h>
80   #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
81 <                        "ntfs"}
81 >                        "ntfs", "nfs"}
82   #endif
83  
84   #ifdef HPUX
# Line 86 | Line 91
91   #include <dirent.h>
92   #include <stdio.h>
93   #include <time.h>
94 < #define VALID_FS_TYPES {"vxfs", "hfs"}
94 > #define VALID_FS_TYPES {"vxfs", "hfs", "nfs"}
95 > #define DISK_BATCH 30
96   #endif
97  
98 + #ifdef WIN32
99 + #include "win32.h"
100 + /*#define VALID_FS_TYPES {"NTFS", "FAT", "FAT32"} unused*/
101 + #define BUFSIZE 512
102 + #endif
103 +
104 + #ifdef ALLBSD
105 + #define SG_MP_FSTYPENAME(mp) (mp)->f_fstypename
106 + #define SG_MP_DEVNAME(mp)    (mp)->f_mntfromname
107 + #define SG_MP_MOUNTP(mp)     (mp)->f_mntonname
108 + #define SG_FS_FRSIZE(fs)     (long long) (*(fs))->f_bsize
109 + #define SG_FS_BSIZE(fs)      (long long) (*(fs))->f_iosize
110 + #define SG_FS_BLOCKS(fs)     (long long) (*(fs))->f_blocks
111 + #define SG_FS_BFREE(fs)      (long long) (*(fs))->f_bfree
112 + #define SG_FS_BAVAIL(fs)     (long long) (*(fs))->f_bavail
113 + #define SG_FS_FILES(fs)      (long long) (*(fs))->f_files
114 + #define SG_FS_FFREE(fs)      (long long) (*(fs))->f_ffree
115 + #define SG_FS_FAVAIL(fs)     -1LL
116 + #endif
117 + #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
118 + #define SG_MP_FSTYPENAME(mp) (mp)->mnt_type
119 + #define SG_MP_DEVNAME(mp)    (mp)->mnt_fsname
120 + #define SG_MP_MOUNTP(mp)     (mp)->mnt_dir
121 + #define SG_FS_FRSIZE(fs)     (long long) (fs).f_frsize
122 + #define SG_FS_BSIZE(fs)      (long long) (fs).f_bsize
123 + #define SG_FS_BLOCKS(fs)     (long long) (fs).f_blocks
124 + #define SG_FS_BFREE(fs)      (long long) (fs).f_bfree
125 + #define SG_FS_BAVAIL(fs)     (long long) (fs).f_bavail
126 + #define SG_FS_FILES(fs)      (long long) (fs).f_files
127 + #define SG_FS_FFREE(fs)      (long long) (fs).f_ffree
128 + #define SG_FS_FAVAIL(fs)     (long long) (fs).f_favail
129 + #endif
130 + #ifdef SOLARIS
131 + #define SG_MP_FSTYPENAME(mp) (mp).mnt_fstype
132 + #define SG_MP_DEVNAME(mp)    (mp).mnt_special
133 + #define SG_MP_MOUNTP(mp)     (mp).mnt_mountp
134 + #define SG_FS_FRSIZE(fs)     (long long) (fs).f_frsize
135 + #define SG_FS_BSIZE(fs)      (long long) (fs).f_bsize
136 + #define SG_FS_BLOCKS(fs)     (long long) (fs).f_blocks
137 + #define SG_FS_BFREE(fs)      (long long) (fs).f_bfree
138 + #define SG_FS_BAVAIL(fs)     (long long) (fs).f_bavail
139 + #define SG_FS_FILES(fs)      (long long) (fs).f_files
140 + #define SG_FS_FFREE(fs)      (long long) (fs).f_ffree
141 + #define SG_FS_FAVAIL(fs)     (long long) (fs).f_favail
142 + #endif
143 +
144   static void disk_stat_init(sg_fs_stats *d) {
145          d->device_name = NULL;
146          d->fs_type = NULL;
# Line 101 | Line 153 | static void disk_stat_destroy(sg_fs_stats *d) {
153          free(d->mnt_point);
154   }
155  
156 + #ifndef WIN32 /* not used by WIN32, so stop compiler throwing warnings */
157   static int is_valid_fs_type(const char *type) {
105        const char *types[] = VALID_FS_TYPES;
158          int i;
159  
160 <        for (i = 0; i < (sizeof types / sizeof *types); i++) {
160 > #if defined(FREEBSD) || defined(DFBSD)
161 >        struct xvfsconf *xvfsp;
162 >        size_t buflen;
163 >        int cnt, nbvfs = 0;
164 >
165 >        if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
166 >                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vfs.conflist");
167 >                return 0;
168 >        }
169 >        xvfsp = alloca(buflen);
170 >        if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
171 >                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vfs.conflist");
172 >                return 0;
173 >        }
174 >        cnt = buflen / sizeof(struct xvfsconf);
175 >        for (i = 0; i < cnt; i++) {
176 >                if (strcmp(xvfsp[i].vfc_name, type) == 0) {
177 >                        return 1;
178 >                }
179 >        }
180 > #else
181 >        const char *types[] = VALID_FS_TYPES;
182 >
183 >        for (i = 0; i < (int) (sizeof types / sizeof *types); i++) {
184                  if (strcmp(types[i], type) == 0) {
185                          return 1;
186                  }
187          }
188 + #endif
189          return 0;
190   }
191 + #endif
192  
193   sg_fs_stats *sg_get_fs_stats(int *entries){
194          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
195                                disk_stat_init, disk_stat_destroy);
196  
120        int valid_type;
197          int num_disks=0;
198   #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX)
199          FILE *f;
# Line 131 | Line 207 | sg_fs_stats *sg_get_fs_stats(int *entries){
207   #endif
208   #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
209          struct mntent *mp;
210 <        struct statfs fs;
210 >        struct statvfs fs;
211   #endif
212   #ifdef ALLBSD
213          int nummnt;
214   #ifdef HAVE_STATVFS
215 <        struct statvfs *mp;
215 >        struct statvfs *mp, **fs;
216   #else
217 <        struct statfs *mp;
217 >        struct statfs *mp, **fs;
218   #endif
219   #endif
220 + #ifdef WIN32
221 +        char lp_buf[MAX_PATH];
222 +        char volume_name_buf[BUFSIZE];
223 +        char filesys_name_buf[BUFSIZE];
224 +        char drive[4] = " :\\";
225 +        char *p;
226 +        lp_buf[0]='\0';
227 + #endif
228  
229   #ifdef ALLBSD
230 <        nummnt=getmntinfo(&mp , MNT_LOCAL);
230 >        nummnt=getmntinfo(&mp, MNT_WAIT);
231          if (nummnt<=0){
232                  sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL);
233                  return NULL;
234          }
235 <        for(;nummnt--; mp++){
152 <                valid_type = is_valid_fs_type(mp->f_fstypename);
235 >        for(fs = &mp; nummnt--; (*fs)++){
236   #endif
237  
238   #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
# Line 163 | Line 246 | sg_fs_stats *sg_get_fs_stats(int *entries){
246          }
247  
248          while((mp=getmntent(f))){
249 <                if((statfs(mp->mnt_dir, &fs)) !=0){
249 >                if((statvfs(mp->mnt_dir, &fs)) !=0){
250                          continue;
251                  }      
252  
170                valid_type = is_valid_fs_type(mp->mnt_type);
253   #endif
254  
255   #ifdef SOLARIS
# Line 179 | Line 261 | sg_fs_stats *sg_get_fs_stats(int *entries){
261                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
262                          continue;
263                  }
182                valid_type = is_valid_fs_type(mp.mnt_fstype);
264   #endif
265  
266 <                if(valid_type){
266 > #ifdef WIN32
267 >        if (!(GetLogicalDriveStrings(BUFSIZE-1, lp_buf))) {
268 >                sg_set_error(SG_ERROR_GETMNTINFO, "GetLogicalDriveStrings");
269 >                return NULL;
270 >        }
271 >        p = lp_buf;
272 >        do {
273 >                // Copy drive letter to template string
274 >                *drive = *p;
275 >                // Only interested in harddrives.
276 >                int drive_type = GetDriveType(drive);
277 >
278 >                if(drive_type == DRIVE_FIXED) {
279 > #else
280 >                if(is_valid_fs_type(SG_MP_FSTYPENAME(mp))){
281 > #endif
282                          if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
283                                  return NULL;
284                          }
285                          disk_ptr=disk_stats+num_disks;
286  
287 < #ifdef ALLBSD
288 <                        if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) {
287 > #ifndef WIN32
288 >                        /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
289 >                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
290 >                         * be upwards of a k each
291 >                         */
292 >                        if (sg_update_string(&disk_ptr->device_name, SG_MP_DEVNAME(mp)) < 0) {
293                                  return NULL;
294                          }
295 <                        if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) {
295 >                        if (sg_update_string(&disk_ptr->fs_type, SG_MP_FSTYPENAME(mp)) < 0) {
296                                  return NULL;
297                          }
298 <                        if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) {
298 >                        if (sg_update_string(&disk_ptr->mnt_point, SG_MP_MOUNTP(mp)) < 0) {
299                                  return NULL;
300                          }
301  
302 <                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
303 <                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
304 <                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
302 >                        disk_ptr->size  = SG_FS_FRSIZE(fs) * SG_FS_BLOCKS(fs);
303 >                        disk_ptr->avail = SG_FS_FRSIZE(fs) * SG_FS_BAVAIL(fs);
304 >                        disk_ptr->used  = (disk_ptr->size) - (SG_FS_FRSIZE(fs) * SG_FS_BFREE(fs));
305 >                
306 >                        disk_ptr->total_inodes = SG_FS_FILES(fs);
307 >                        disk_ptr->free_inodes  = SG_FS_FFREE(fs);
308 >                        /* Linux, FreeBSD don't have a "available" inodes */
309 >                        disk_ptr->used_inodes  = disk_ptr->total_inodes - disk_ptr->free_inodes;
310 >                        disk_ptr->avail_inodes = SG_FS_FAVAIL(fs);
311  
312 <                        disk_ptr->total_inodes=(long long)mp->f_files;
313 <                        disk_ptr->free_inodes=(long long)mp->f_ffree;
314 <                        /* Freebsd doesn't have a "available" inodes */
315 <                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
316 < #endif
317 < #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
318 <                        if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
312 >                        disk_ptr->io_size      = SG_FS_BSIZE(fs);
313 >                        disk_ptr->block_size   = SG_FS_FRSIZE(fs);
314 >                        disk_ptr->total_blocks = SG_FS_BLOCKS(fs);
315 >                        disk_ptr->free_blocks  = SG_FS_BFREE(fs);
316 >                        disk_ptr->avail_blocks = SG_FS_BAVAIL(fs);
317 >                        disk_ptr->used_blocks  = disk_ptr->total_blocks - disk_ptr->free_blocks;
318 > #else
319 >                        if(!GetVolumeInformation(drive, volume_name_buf, BUFSIZE,
320 >                                                NULL, NULL, NULL,
321 >                                                filesys_name_buf, BUFSIZE)) {
322 >                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
323 >                                        "GetVolumeInformation");
324                                  return NULL;
325                          }
215                                
216                        if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) {  
217                                return NULL;
218                        }
326  
327 <                        if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) {
327 >                        if (sg_update_string(&disk_ptr->device_name,
328 >                                                volume_name_buf) < 0) {
329                                  return NULL;
330                          }
331 <                        disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
332 <                        disk_ptr->avail = (long long)fs.f_bsize * (long long)fs.f_bavail;
225 <                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_bsize * (long long)fs.f_bfree);
226 <
227 <                        disk_ptr->total_inodes=(long long)fs.f_files;
228 <                        disk_ptr->free_inodes=(long long)fs.f_ffree;
229 <                        /* Linux doesn't have a "available" inodes */
230 <                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
231 < #endif
232 <
233 < #ifdef SOLARIS
234 <                        /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
235 <                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
236 <                         * be upwards of a k each
237 <                         */
238 <                        if (sg_update_string(&disk_ptr->device_name, mp.mnt_special) < 0) {
331 >                        if (sg_update_string(&disk_ptr->fs_type,
332 >                                                filesys_name_buf) < 0) {
333                                  return NULL;
334                          }
335 <
336 <                        if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
335 >                        if (sg_update_string(&disk_ptr->mnt_point,
336 >                                                drive) < 0) {
337                                  return NULL;
338                          }
339 <        
340 <                        if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
339 >                        if (!GetDiskFreeSpaceEx(drive, NULL,
340 >                                        (PULARGE_INTEGER)&disk_ptr->size,
341 >                                        (PULARGE_INTEGER)&disk_ptr->avail)) {
342 >                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
343 >                                        "GetDiskFreeSpaceEx");
344                                  return NULL;
345                          }
346 <                        
347 <                        disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
348 <                        disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
349 <                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_frsize * (long long)fs.f_bfree);
350 <                
351 <                        disk_ptr->total_inodes=(long long)fs.f_files;
352 <                        disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
353 <                        disk_ptr->free_inodes=(long long)fs.f_favail;
346 >                        disk_ptr->used = disk_ptr->size - disk_ptr->avail;
347 >                        disk_ptr->total_inodes = 0;
348 >                        disk_ptr->free_inodes  = 0;
349 >                        disk_ptr->used_inodes  = 0;
350 >                        disk_ptr->avail_inodes = 0;
351 >
352 >                        /* I dunno what to do with these... so have nothing */
353 >                        disk_ptr->io_size = 0;
354 >                        disk_ptr->block_size = 0;
355 >                        disk_ptr->total_blocks = 0;
356 >                        disk_ptr->free_blocks = 0;
357 >                        disk_ptr->avail_blocks = 0;
358 >                        disk_ptr->used_blocks = 0;
359   #endif
360                          num_disks++;
361                  }
362 + #ifdef WIN32
363 +                while(*p++);
364 +        } while(*p);
365 + #else
366          }
367 + #endif
368  
369          *entries=num_disks;    
370  
# Line 316 | Line 423 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
423          long long rbytes = 0, wbytes = 0;
424          struct dirent *dinfo = NULL;
425          struct stat lstatinfo;
426 <        struct pst_diskinfo pstat_diskinfo;
426 >        struct pst_diskinfo pstat_diskinfo[DISK_BATCH];
427          char fullpathbuf[1024] = {0};
428          dev_t diskid;
429          DIR *dh = NULL;
430 <        int disknum = 0;
430 >        int diskidx = 0;
431 >        int num, i;
432   #endif
433   #ifdef SOLARIS
434          kstat_ctl_t *kc;
# Line 336 | Line 444 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
444          int i, n;
445          time_t now;
446          const char *format;
447 +        static regex_t not_part_re, part_re;
448 +        static int re_compiled = 0;
449   #endif
450   #if defined(FREEBSD) || defined(DFBSD)
451          static struct statinfo stats;
# Line 366 | Line 476 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
476          int mib[MIBSIZE];
477          size_t size;
478   #endif
479 + #ifdef WIN32
480 +        char *name;
481 +        long long rbytes;
482 +        long long wbytes;
483 + #endif
484  
485          num_diskio=0;
486  
487   #ifdef HPUX
488 <
489 <        /* The "128" here is arbitrary, it can be increased to any number
490 <           at the expense of only more system calls to pstat(). */
491 <        for (disknum = 0; disknum < 128; disknum++) {
492 <                if (pstat_getdisk(&pstat_diskinfo, sizeof(pstat_diskinfo), 1, disknum) == -1) {
488 >        while (1) {
489 >                num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
490 >                                    DISK_BATCH, diskidx);
491 >                if (num == -1) {
492 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
493 >                                                "pstat_getdisk");
494 >                        return NULL;
495 >                } else if (num == 0) {
496                          break;
497                  }
498  
499 <                if (pstat_diskinfo.psd_idx != disknum) {
500 <                        continue;
383 <                }
499 >                for (i = 0; i < num; i++) {
500 >                        struct pst_diskinfo *di = &pstat_diskinfo[i];
501  
502 <                /* Skip "disabled" disks.. */
503 <                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 the same 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) {
502 >                        /* Skip "disabled" disks. */
503 >                        if (di->psd_status == 0) {
504                                  continue;
505                          }
506 <
507 <                        diskid = (pstat_diskinfo.psd_dev.psd_major << 24) | pstat_diskinfo.psd_dev.psd_minor;
508 <                        while (1) {
509 <                                dinfo = readdir(dh);
510 <                                if (dinfo == NULL) {
511 <                                        break;
512 <                                }
513 <                                snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
514 <                                if (lstat(fullpathbuf, &lstatinfo) < 0) {
506 >        
507 >                        /* We can't seperate the reads from the writes, we'll
508 >                         * just give the same to each. (This value is in
509 >                         * 64-byte chunks according to the pstat header file,
510 >                         * and can wrap to be negative.)
511 >                         */
512 >                        rbytes = wbytes = ((unsigned long) di->psd_dkwds) * 64LL;
513 >        
514 >                        /* Skip unused disks. */
515 >                        if (rbytes == 0 && wbytes == 0) {
516 >                                continue;
517 >                        }
518 >        
519 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
520 >                                return NULL;
521 >                        }
522 >        
523 >                        diskio_stats_ptr = diskio_stats + num_diskio;
524 >        
525 >                        diskio_stats_ptr->read_bytes = rbytes;
526 >                        diskio_stats_ptr->write_bytes = wbytes;
527 >        
528 >                        diskio_stats_ptr->systime = time(NULL);
529 >        
530 >                        num_diskio++;
531 >        
532 >                        /* FIXME This should use a static cache, like the Linux
533 >                         * code below. */
534 >                        if (diskio_stats_ptr->disk_name == NULL) {
535 >                                dh = opendir("/dev/dsk");
536 >                                if (dh == NULL) {
537                                          continue;
538                                  }
539 <
540 <                                if (lstatinfo.st_rdev == diskid) {
541 <                                        if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
539 >        
540 >                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
541 >                                while (1) {
542 >                                        dinfo = readdir(dh);
543 >                                        if (dinfo == NULL) {
544 >                                                break;
545 >                                        }
546 >                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
547 >                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
548 >                                                continue;
549 >                                        }
550 >        
551 >                                        if (lstatinfo.st_rdev == diskid) {
552 >                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
553 >                                                        return NULL;
554 >                                                }
555 >                                                break;
556 >                                        }
557 >                                }
558 >                                closedir(dh);
559 >        
560 >                                if (diskio_stats_ptr->disk_name == NULL) {
561 >                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
562                                                  return NULL;
563                                          }
433                                        break;
564                                  }
565                          }
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                        }
566                  }
567 +                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
568          }
569   #endif
570   #ifdef OPENBSD
# Line 659 | Line 783 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
783                          diskio_stats_ptr->write_bytes=kios.nwritten;
784                          if (sg_update_string(&diskio_stats_ptr->disk_name,
785                                               sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
786 +                                kstat_close(kc);
787                                  return NULL;
788                          }
789                          diskio_stats_ptr->systime=time(NULL);
# Line 688 | Line 813 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
813          if (f == NULL) goto out;
814          now = time(NULL);
815  
816 +        if (!re_compiled) {
817 +                if (regcomp(&part_re, "^(.*/)?[^/]*[0-9]$", REG_EXTENDED | REG_NOSUB) != 0) {
818 +                        sg_set_error(SG_ERROR_PARSE, NULL);
819 +                        goto out;
820 +                }
821 +                if (regcomp(&not_part_re, "^(.*/)?[^/0-9]+[0-9]+d[0-9]+$", REG_EXTENDED | REG_NOSUB) != 0) {
822 +                        sg_set_error(SG_ERROR_PARSE, NULL);
823 +                        goto out;
824 +                }
825 +                re_compiled = 1;
826 +        }
827 +
828          while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
829                  char name[100];
693                char *s;
830                  long long rsect, wsect;
831  
832                  int nr = sscanf(line_ptr, format,
# Line 698 | Line 834 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
834                  if (nr < 3) continue;
835  
836                  /* Skip device names ending in numbers, since they're
837 <                   partitions. */
838 <                s = name;
839 <                while (*s != '\0') s++;
840 <                --s;
841 <                if (*s >= '0' && *s <= '9') continue;
837 >                   partitions, unless they match the c0d0 pattern that some
838 >                   RAID devices use. */
839 >                /* FIXME: For 2.6+, we should probably be using sysfs to detect
840 >                   this... */
841 >                if ((regexec(&part_re, name, 0, NULL, 0) == 0)
842 >                    && (regexec(&not_part_re, name, 0, NULL, 0) != 0)) {
843 >                        continue;
844 >                }
845  
846                  if (nr < 5) {
847                          has_pp_stats = 0;
# Line 740 | Line 879 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
879                  f = fopen("/proc/stat", "r");
880                  if (f == NULL) goto out;
881                  now = time(NULL);
882 <        
882 >
883                  line_ptr = sg_f_read_line(f, "disk_io:");
884                  if (line_ptr == NULL) goto out;
885 <        
885 >
886                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
887                          long long rsect, wsect;
888  
889                          if (*++line_ptr == '\0') break;
890 <        
890 >
891                          if((sscanf(line_ptr,
892                                  "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
893                                  &major, &minor, &rsect, &wsect)) != 4) {
# Line 809 | Line 948 | out:
948          return NULL;
949   #endif
950  
951 + #ifdef WIN32
952 +        sg_set_error(SG_ERROR_NONE, NULL);
953 +
954 +        while((name = get_diskio(num_diskio, &rbytes, &wbytes)) != NULL) {
955 +                if (VECTOR_RESIZE(diskio_stats, num_diskio+1)) {
956 +                        return NULL;
957 +                }
958 +
959 +                diskio_stats_ptr = diskio_stats + num_diskio;
960 +
961 +                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
962 +                        return NULL;
963 +                }
964 +                sg_update_string(&name, NULL);
965 +                diskio_stats_ptr->read_bytes = rbytes;
966 +                diskio_stats_ptr->write_bytes = wbytes;
967 +
968 +                diskio_stats_ptr->systime = 0;
969 +
970 +                num_diskio++;
971 +        }
972 + #endif
973 +
974          *entries=num_diskio;
975  
976          return diskio_stats;
977   }
978  
979   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
980 + #ifndef WIN32
981          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
982                                diskio_stat_init, diskio_stat_destroy);
983          sg_disk_io_stats *src = NULL, *dest;
# Line 876 | Line 1039 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
1039  
1040          *entries = diff_count;
1041          return diff;
1042 + #else /* WIN32 */
1043 +        return sg_get_disk_io_stats(entries);
1044 + #endif
1045   }
1046  
1047   int sg_disk_io_compare_name(const void *va, const void *vb) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines