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.86 by tdb, Sun Aug 3 17:43:26 2008 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 66 | Line 67
67   #include <sys/dkstat.h>
68   #include <devstat.h>
69   #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
70 <                        "ufs", "mfs"}
70 >                        "ufs", "mfs", "nfs"}
71   #endif
72   #if defined(NETBSD) || defined(OPENBSD)
73   #include <sys/param.h>
74   #include <sys/sysctl.h>
75   #include <sys/disk.h>
76   #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
77 <                        "ntfs"}
77 >                        "ntfs", "nfs"}
78   #endif
79  
80   #ifdef HPUX
# Line 86 | Line 87
87   #include <dirent.h>
88   #include <stdio.h>
89   #include <time.h>
90 < #define VALID_FS_TYPES {"vxfs", "hfs"}
90 > #define VALID_FS_TYPES {"vxfs", "hfs", "nfs"}
91 > #define DISK_BATCH 30
92   #endif
93  
94 + #ifdef WIN32
95 + #include "win32.h"
96 + /*#define VALID_FS_TYPES {"NTFS", "FAT", "FAT32"} unused*/
97 + #define BUFSIZE 512
98 + #endif
99 +
100 + #ifdef ALLBSD
101 + #define SG_MP_FSTYPENAME(mp) (mp)->f_fstypename
102 + #define SG_MP_DEVNAME(mp)    (mp)->f_mntfromname
103 + #define SG_MP_MOUNTP(mp)     (mp)->f_mntonname
104 + #define SG_FS_FRSIZE(fs)     (long long) (*(fs))->f_bsize
105 + #define SG_FS_BSIZE(fs)      (long long) (*(fs))->f_iosize
106 + #define SG_FS_BLOCKS(fs)     (long long) (*(fs))->f_blocks
107 + #define SG_FS_BFREE(fs)      (long long) (*(fs))->f_bfree
108 + #define SG_FS_BAVAIL(fs)     (long long) (*(fs))->f_bavail
109 + #define SG_FS_FILES(fs)      (long long) (*(fs))->f_files
110 + #define SG_FS_FFREE(fs)      (long long) (*(fs))->f_ffree
111 + #define SG_FS_FAVAIL(fs)     -1LL
112 + #endif
113 + #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
114 + #define SG_MP_FSTYPENAME(mp) (mp)->mnt_type
115 + #define SG_MP_DEVNAME(mp)    (mp)->mnt_fsname
116 + #define SG_MP_MOUNTP(mp)     (mp)->mnt_dir
117 + #define SG_FS_FRSIZE(fs)     (long long) (fs).f_frsize
118 + #define SG_FS_BSIZE(fs)      (long long) (fs).f_bsize
119 + #define SG_FS_BLOCKS(fs)     (long long) (fs).f_blocks
120 + #define SG_FS_BFREE(fs)      (long long) (fs).f_bfree
121 + #define SG_FS_BAVAIL(fs)     (long long) (fs).f_bavail
122 + #define SG_FS_FILES(fs)      (long long) (fs).f_files
123 + #define SG_FS_FFREE(fs)      (long long) (fs).f_ffree
124 + #define SG_FS_FAVAIL(fs)     (long long) (fs).f_favail
125 + #endif
126 + #ifdef SOLARIS
127 + #define SG_MP_FSTYPENAME(mp) (mp).mnt_fstype
128 + #define SG_MP_DEVNAME(mp)    (mp).mnt_special
129 + #define SG_MP_MOUNTP(mp)     (mp).mnt_mountp
130 + #define SG_FS_FRSIZE(fs)     (long long) (fs).f_frsize
131 + #define SG_FS_BSIZE(fs)      (long long) (fs).f_bsize
132 + #define SG_FS_BLOCKS(fs)     (long long) (fs).f_blocks
133 + #define SG_FS_BFREE(fs)      (long long) (fs).f_bfree
134 + #define SG_FS_BAVAIL(fs)     (long long) (fs).f_bavail
135 + #define SG_FS_FILES(fs)      (long long) (fs).f_files
136 + #define SG_FS_FFREE(fs)      (long long) (fs).f_ffree
137 + #define SG_FS_FAVAIL(fs)     (long long) (fs).f_favail
138 + #endif
139 +
140   static void disk_stat_init(sg_fs_stats *d) {
141          d->device_name = NULL;
142          d->fs_type = NULL;
# Line 101 | Line 149 | static void disk_stat_destroy(sg_fs_stats *d) {
149          free(d->mnt_point);
150   }
151  
152 + #ifndef WIN32 /* not used by WIN32, so stop compiler throwing warnings */
153   static int is_valid_fs_type(const char *type) {
154          const char *types[] = VALID_FS_TYPES;
155          int i;
156  
157 <        for (i = 0; i < (sizeof types / sizeof *types); i++) {
157 >        for (i = 0; i < (int) (sizeof types / sizeof *types); i++) {
158                  if (strcmp(types[i], type) == 0) {
159                          return 1;
160                  }
161          }
162          return 0;
163   }
164 + #endif
165  
166   sg_fs_stats *sg_get_fs_stats(int *entries){
167          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
168                                disk_stat_init, disk_stat_destroy);
169  
120        int valid_type;
170          int num_disks=0;
171   #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX)
172          FILE *f;
# Line 131 | Line 180 | sg_fs_stats *sg_get_fs_stats(int *entries){
180   #endif
181   #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
182          struct mntent *mp;
183 <        struct statfs fs;
183 >        struct statvfs fs;
184   #endif
185   #ifdef ALLBSD
186          int nummnt;
187   #ifdef HAVE_STATVFS
188 <        struct statvfs *mp;
188 >        struct statvfs *mp, **fs;
189   #else
190 <        struct statfs *mp;
190 >        struct statfs *mp, **fs;
191   #endif
192   #endif
193 + #ifdef WIN32
194 +        char lp_buf[MAX_PATH];
195 +        char volume_name_buf[BUFSIZE];
196 +        char filesys_name_buf[BUFSIZE];
197 +        char drive[4] = " :\\";
198 +        char *p;
199 +        lp_buf[0]='\0';
200 + #endif
201  
202   #ifdef ALLBSD
203 <        nummnt=getmntinfo(&mp , MNT_LOCAL);
203 >        nummnt=getmntinfo(&mp, MNT_WAIT);
204          if (nummnt<=0){
205                  sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL);
206                  return NULL;
207          }
208 <        for(;nummnt--; mp++){
152 <                valid_type = is_valid_fs_type(mp->f_fstypename);
208 >        for(fs = &mp; nummnt--; (*fs)++){
209   #endif
210  
211   #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
# Line 163 | Line 219 | sg_fs_stats *sg_get_fs_stats(int *entries){
219          }
220  
221          while((mp=getmntent(f))){
222 <                if((statfs(mp->mnt_dir, &fs)) !=0){
222 >                if((statvfs(mp->mnt_dir, &fs)) !=0){
223                          continue;
224                  }      
225  
170                valid_type = is_valid_fs_type(mp->mnt_type);
226   #endif
227  
228   #ifdef SOLARIS
# Line 179 | Line 234 | sg_fs_stats *sg_get_fs_stats(int *entries){
234                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
235                          continue;
236                  }
182                valid_type = is_valid_fs_type(mp.mnt_fstype);
237   #endif
238  
239 <                if(valid_type){
239 > #ifdef WIN32
240 >        if (!(GetLogicalDriveStrings(BUFSIZE-1, lp_buf))) {
241 >                sg_set_error(SG_ERROR_GETMNTINFO, "GetLogicalDriveStrings");
242 >                return NULL;
243 >        }
244 >        p = lp_buf;
245 >        do {
246 >                // Copy drive letter to template string
247 >                *drive = *p;
248 >                // Only interested in harddrives.
249 >                int drive_type = GetDriveType(drive);
250 >
251 >                if(drive_type == DRIVE_FIXED) {
252 > #else
253 >                if(is_valid_fs_type(SG_MP_FSTYPENAME(mp))){
254 > #endif
255                          if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
256                                  return NULL;
257                          }
258                          disk_ptr=disk_stats+num_disks;
259  
260 < #ifdef ALLBSD
261 <                        if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) {
260 > #ifndef WIN32
261 >                        /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
262 >                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
263 >                         * be upwards of a k each
264 >                         */
265 >                        if (sg_update_string(&disk_ptr->device_name, SG_MP_DEVNAME(mp)) < 0) {
266                                  return NULL;
267                          }
268 <                        if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) {
268 >                        if (sg_update_string(&disk_ptr->fs_type, SG_MP_FSTYPENAME(mp)) < 0) {
269                                  return NULL;
270                          }
271 <                        if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) {
271 >                        if (sg_update_string(&disk_ptr->mnt_point, SG_MP_MOUNTP(mp)) < 0) {
272                                  return NULL;
273                          }
274  
275 <                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
276 <                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
277 <                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
275 >                        disk_ptr->size  = SG_FS_FRSIZE(fs) * SG_FS_BLOCKS(fs);
276 >                        disk_ptr->avail = SG_FS_FRSIZE(fs) * SG_FS_BAVAIL(fs);
277 >                        disk_ptr->used  = (disk_ptr->size) - (SG_FS_FRSIZE(fs) * SG_FS_BFREE(fs));
278 >                
279 >                        disk_ptr->total_inodes = SG_FS_FILES(fs);
280 >                        disk_ptr->free_inodes  = SG_FS_FFREE(fs);
281 >                        /* Linux, FreeBSD don't have a "available" inodes */
282 >                        disk_ptr->used_inodes  = disk_ptr->total_inodes - disk_ptr->free_inodes;
283 >                        disk_ptr->avail_inodes = SG_FS_FAVAIL(fs);
284  
285 <                        disk_ptr->total_inodes=(long long)mp->f_files;
286 <                        disk_ptr->free_inodes=(long long)mp->f_ffree;
287 <                        /* Freebsd doesn't have a "available" inodes */
288 <                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
289 < #endif
290 < #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
291 <                        if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
285 >                        disk_ptr->io_size      = SG_FS_BSIZE(fs);
286 >                        disk_ptr->block_size   = SG_FS_FRSIZE(fs);
287 >                        disk_ptr->total_blocks = SG_FS_BLOCKS(fs);
288 >                        disk_ptr->free_blocks  = SG_FS_BFREE(fs);
289 >                        disk_ptr->avail_blocks = SG_FS_BAVAIL(fs);
290 >                        disk_ptr->used_blocks  = disk_ptr->total_blocks - disk_ptr->free_blocks;
291 > #else
292 >                        if(!GetVolumeInformation(drive, volume_name_buf, BUFSIZE,
293 >                                                NULL, NULL, NULL,
294 >                                                filesys_name_buf, BUFSIZE)) {
295 >                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
296 >                                        "GetVolumeInformation");
297                                  return NULL;
298                          }
215                                
216                        if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) {  
217                                return NULL;
218                        }
299  
300 <                        if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) {
300 >                        if (sg_update_string(&disk_ptr->device_name,
301 >                                                volume_name_buf) < 0) {
302                                  return NULL;
303                          }
304 <                        disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
305 <                        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) {
304 >                        if (sg_update_string(&disk_ptr->fs_type,
305 >                                                filesys_name_buf) < 0) {
306                                  return NULL;
307                          }
308 <
309 <                        if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
308 >                        if (sg_update_string(&disk_ptr->mnt_point,
309 >                                                drive) < 0) {
310                                  return NULL;
311                          }
312 <        
313 <                        if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
312 >                        if (!GetDiskFreeSpaceEx(drive, NULL,
313 >                                        (PULARGE_INTEGER)&disk_ptr->size,
314 >                                        (PULARGE_INTEGER)&disk_ptr->avail)) {
315 >                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
316 >                                        "GetDiskFreeSpaceEx");
317                                  return NULL;
318                          }
319 <                        
320 <                        disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
321 <                        disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
322 <                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_frsize * (long long)fs.f_bfree);
323 <                
324 <                        disk_ptr->total_inodes=(long long)fs.f_files;
325 <                        disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
326 <                        disk_ptr->free_inodes=(long long)fs.f_favail;
319 >                        disk_ptr->used = disk_ptr->size - disk_ptr->avail;
320 >                        disk_ptr->total_inodes = 0;
321 >                        disk_ptr->free_inodes  = 0;
322 >                        disk_ptr->used_inodes  = 0;
323 >                        disk_ptr->avail_inodes = 0;
324 >
325 >                        /* I dunno what to do with these... so have nothing */
326 >                        disk_ptr->io_size = 0;
327 >                        disk_ptr->block_size = 0;
328 >                        disk_ptr->total_blocks = 0;
329 >                        disk_ptr->free_blocks = 0;
330 >                        disk_ptr->avail_blocks = 0;
331 >                        disk_ptr->used_blocks = 0;
332   #endif
333                          num_disks++;
334                  }
335 + #ifdef WIN32
336 +                while(*p++);
337 +        } while(*p);
338 + #else
339          }
340 + #endif
341  
342          *entries=num_disks;    
343  
# Line 316 | Line 396 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
396          long long rbytes = 0, wbytes = 0;
397          struct dirent *dinfo = NULL;
398          struct stat lstatinfo;
399 <        struct pst_diskinfo pstat_diskinfo;
399 >        struct pst_diskinfo pstat_diskinfo[DISK_BATCH];
400          char fullpathbuf[1024] = {0};
401          dev_t diskid;
402          DIR *dh = NULL;
403 <        int disknum = 0;
403 >        int diskidx = 0;
404 >        int num, i;
405   #endif
406   #ifdef SOLARIS
407          kstat_ctl_t *kc;
# Line 336 | Line 417 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
417          int i, n;
418          time_t now;
419          const char *format;
420 +        static regex_t not_part_re, part_re;
421 +        static int re_compiled = 0;
422   #endif
423   #if defined(FREEBSD) || defined(DFBSD)
424          static struct statinfo stats;
# Line 366 | Line 449 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
449          int mib[MIBSIZE];
450          size_t size;
451   #endif
452 + #ifdef WIN32
453 +        char *name;
454 +        long long rbytes;
455 +        long long wbytes;
456 + #endif
457  
458          num_diskio=0;
459  
460   #ifdef HPUX
461 <
462 <        /* The "128" here is arbitrary, it can be increased to any number
463 <           at the expense of only more system calls to pstat(). */
464 <        for (disknum = 0; disknum < 128; disknum++) {
465 <                if (pstat_getdisk(&pstat_diskinfo, sizeof(pstat_diskinfo), 1, disknum) == -1) {
461 >        while (1) {
462 >                num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
463 >                                    DISK_BATCH, diskidx);
464 >                if (num == -1) {
465 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
466 >                                                "pstat_getdisk");
467 >                        return NULL;
468 >                } else if (num == 0) {
469                          break;
470                  }
471  
472 <                if (pstat_diskinfo.psd_idx != disknum) {
473 <                        continue;
383 <                }
472 >                for (i = 0; i < num; i++) {
473 >                        struct pst_diskinfo *di = &pstat_diskinfo[i];
474  
475 <                /* Skip "disabled" disks.. */
476 <                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) {
475 >                        /* Skip "disabled" disks. */
476 >                        if (di->psd_status == 0) {
477                                  continue;
478                          }
479 <
480 <                        diskid = (pstat_diskinfo.psd_dev.psd_major << 24) | pstat_diskinfo.psd_dev.psd_minor;
481 <                        while (1) {
482 <                                dinfo = readdir(dh);
483 <                                if (dinfo == NULL) {
484 <                                        break;
485 <                                }
486 <                                snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
487 <                                if (lstat(fullpathbuf, &lstatinfo) < 0) {
479 >        
480 >                        /* We can't seperate the reads from the writes, we'll
481 >                         * just give the same to each. (This value is in
482 >                         * 64-byte chunks according to the pstat header file,
483 >                         * and can wrap to be negative.)
484 >                         */
485 >                        rbytes = wbytes = ((unsigned long) di->psd_dkwds) * 64LL;
486 >        
487 >                        /* Skip unused disks. */
488 >                        if (rbytes == 0 && wbytes == 0) {
489 >                                continue;
490 >                        }
491 >        
492 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
493 >                                return NULL;
494 >                        }
495 >        
496 >                        diskio_stats_ptr = diskio_stats + num_diskio;
497 >        
498 >                        diskio_stats_ptr->read_bytes = rbytes;
499 >                        diskio_stats_ptr->write_bytes = wbytes;
500 >        
501 >                        diskio_stats_ptr->systime = time(NULL);
502 >        
503 >                        num_diskio++;
504 >        
505 >                        /* FIXME This should use a static cache, like the Linux
506 >                         * code below. */
507 >                        if (diskio_stats_ptr->disk_name == NULL) {
508 >                                dh = opendir("/dev/dsk");
509 >                                if (dh == NULL) {
510                                          continue;
511                                  }
512 <
513 <                                if (lstatinfo.st_rdev == diskid) {
514 <                                        if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
512 >        
513 >                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
514 >                                while (1) {
515 >                                        dinfo = readdir(dh);
516 >                                        if (dinfo == NULL) {
517 >                                                break;
518 >                                        }
519 >                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
520 >                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
521 >                                                continue;
522 >                                        }
523 >        
524 >                                        if (lstatinfo.st_rdev == diskid) {
525 >                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
526 >                                                        return NULL;
527 >                                                }
528 >                                                break;
529 >                                        }
530 >                                }
531 >                                closedir(dh);
532 >        
533 >                                if (diskio_stats_ptr->disk_name == NULL) {
534 >                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
535                                                  return NULL;
536                                          }
433                                        break;
537                                  }
538                          }
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                        }
539                  }
540 +                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
541          }
542   #endif
543   #ifdef OPENBSD
# Line 659 | Line 756 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
756                          diskio_stats_ptr->write_bytes=kios.nwritten;
757                          if (sg_update_string(&diskio_stats_ptr->disk_name,
758                                               sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
759 +                                kstat_close(kc);
760                                  return NULL;
761                          }
762                          diskio_stats_ptr->systime=time(NULL);
# Line 688 | Line 786 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
786          if (f == NULL) goto out;
787          now = time(NULL);
788  
789 +        if (!re_compiled) {
790 +                if (regcomp(&part_re, "^(.*/)?[^/]*[0-9]$", REG_EXTENDED | REG_NOSUB) != 0) {
791 +                        sg_set_error(SG_ERROR_PARSE, NULL);
792 +                        goto out;
793 +                }
794 +                if (regcomp(&not_part_re, "^(.*/)?[^/0-9]+[0-9]+d[0-9]+$", REG_EXTENDED | REG_NOSUB) != 0) {
795 +                        sg_set_error(SG_ERROR_PARSE, NULL);
796 +                        goto out;
797 +                }
798 +                re_compiled = 1;
799 +        }
800 +
801          while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
802                  char name[100];
693                char *s;
803                  long long rsect, wsect;
804  
805                  int nr = sscanf(line_ptr, format,
# Line 698 | Line 807 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
807                  if (nr < 3) continue;
808  
809                  /* Skip device names ending in numbers, since they're
810 <                   partitions. */
811 <                s = name;
812 <                while (*s != '\0') s++;
813 <                --s;
814 <                if (*s >= '0' && *s <= '9') continue;
810 >                   partitions, unless they match the c0d0 pattern that some
811 >                   RAID devices use. */
812 >                /* FIXME: For 2.6+, we should probably be using sysfs to detect
813 >                   this... */
814 >                if ((regexec(&part_re, name, 0, NULL, 0) == 0)
815 >                    && (regexec(&not_part_re, name, 0, NULL, 0) != 0)) {
816 >                        continue;
817 >                }
818  
819                  if (nr < 5) {
820                          has_pp_stats = 0;
# Line 740 | Line 852 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
852                  f = fopen("/proc/stat", "r");
853                  if (f == NULL) goto out;
854                  now = time(NULL);
855 <        
855 >
856                  line_ptr = sg_f_read_line(f, "disk_io:");
857                  if (line_ptr == NULL) goto out;
858 <        
858 >
859                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
860                          long long rsect, wsect;
861  
862                          if (*++line_ptr == '\0') break;
863 <        
863 >
864                          if((sscanf(line_ptr,
865                                  "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
866                                  &major, &minor, &rsect, &wsect)) != 4) {
# Line 809 | Line 921 | out:
921          return NULL;
922   #endif
923  
924 + #ifdef WIN32
925 +        sg_set_error(SG_ERROR_NONE, NULL);
926 +
927 +        while((name = get_diskio(num_diskio, &rbytes, &wbytes)) != NULL) {
928 +                if (VECTOR_RESIZE(diskio_stats, num_diskio+1)) {
929 +                        return NULL;
930 +                }
931 +
932 +                diskio_stats_ptr = diskio_stats + num_diskio;
933 +
934 +                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
935 +                        return NULL;
936 +                }
937 +                sg_update_string(&name, NULL);
938 +                diskio_stats_ptr->read_bytes = rbytes;
939 +                diskio_stats_ptr->write_bytes = wbytes;
940 +
941 +                diskio_stats_ptr->systime = 0;
942 +
943 +                num_diskio++;
944 +        }
945 + #endif
946 +
947          *entries=num_diskio;
948  
949          return diskio_stats;
950   }
951  
952   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
953 + #ifndef WIN32
954          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
955                                diskio_stat_init, diskio_stat_destroy);
956          sg_disk_io_stats *src = NULL, *dest;
# Line 876 | Line 1012 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
1012  
1013          *entries = diff_count;
1014          return diff;
1015 + #else /* WIN32 */
1016 +        return sg_get_disk_io_stats(entries);
1017 + #endif
1018   }
1019  
1020   int sg_disk_io_compare_name(const void *va, const void *vb) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines