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.51 by tdb, Thu Feb 12 23:04:52 2004 UTC vs.
Revision 1.70 by tdb, Sun Jun 20 20:32:24 2004 UTC

# Line 1 | Line 1
1   /*
2 < * i-scream central monitoring system
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4   * Copyright (C) 2000-2004 i-scream
5   *
# Line 30 | Line 30
30   #include <string.h>
31   #include <time.h>
32   #include "statgrab.h"
33 + #include "vector.h"
34 + #include "tools.h"
35  
36   #ifdef SOLARIS
37   #include <sys/mnttab.h>
# Line 41 | Line 43
43   #if defined(LINUX) || defined(CYGWIN)
44   #include <mntent.h>
45   #include <sys/vfs.h>
44 #include "tools.h"
46   #endif
47  
48   #ifdef LINUX
49   #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
50 <                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
51 <                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
52 <                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
53 <                        "umsdos", "vfat", "xfs", "jfs"}
50 >                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
51 >                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
52 >                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
53 >                        "umsdos", "vfat", "xfs", "jfs"}
54   #endif
55  
56   #ifdef CYGWIN
# Line 61 | Line 62
62   #include <sys/ucred.h>
63   #include <sys/mount.h>
64   #endif
65 < #ifdef FREEBSD
65 > #if defined(FREEBSD) || defined(DFBSD)
66   #include <sys/dkstat.h>
67   #include <devstat.h>
68   #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
69 <                        "ufs", "mfs"}
69 >                        "ufs", "mfs"}
70   #endif
71   #if defined(NETBSD) || defined(OPENBSD)
72   #include <sys/param.h>
73   #include <sys/sysctl.h>
74   #include <sys/disk.h>
75   #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
76 <                        "ntfs"}
76 >                        "ntfs"}
77   #endif
78  
79 < #define START_VAL 1
80 <
81 < char *copy_string(char *orig_ptr, const char *newtext){
82 <
82 <        /* Maybe free if not NULL, and strdup rather than realloc and strcpy? */
83 <        orig_ptr=realloc(orig_ptr, (1+strlen(newtext)));
84 <        if(orig_ptr==NULL){
85 <                return NULL;
86 <        }
87 <        strcpy(orig_ptr, newtext);
88 <
89 <        return orig_ptr;
79 > static void disk_stat_init(sg_fs_stats *d) {
80 >        d->device_name = NULL;
81 >        d->fs_type = NULL;
82 >        d->mnt_point = NULL;
83   }
84  
85 <
86 < void init_disk_stat(int start, int end, disk_stat_t *disk_stats){
87 <
88 <        for(disk_stats+=start; start<=end; start++){
96 <                disk_stats->device_name=NULL;
97 <                disk_stats->fs_type=NULL;
98 <                disk_stats->mnt_point=NULL;
99 <                
100 <                disk_stats++;
101 <        }
85 > static void disk_stat_destroy(sg_fs_stats *d) {
86 >        free(d->device_name);
87 >        free(d->fs_type);
88 >        free(d->mnt_point);
89   }
90  
91 < int is_valid_fs_type(const char *type) {
91 > static int is_valid_fs_type(const char *type) {
92          const char *types[] = VALID_FS_TYPES;
93          int i;
94  
# Line 113 | Line 100 | int is_valid_fs_type(const char *type) {
100          return 0;
101   }
102  
103 < disk_stat_t *get_disk_stats(int *entries){
103 > sg_fs_stats *sg_get_fs_stats(int *entries){
104 >        VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
105 >                              disk_stat_init, disk_stat_destroy);
106  
118        static disk_stat_t *disk_stats;
119        static int watermark=-1;
120
107          int valid_type;
108          int num_disks=0;
109   #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
110          FILE *f;
111   #endif
112  
113 <        disk_stat_t *disk_ptr;
113 >        sg_fs_stats *disk_ptr;
114  
115   #ifdef SOLARIS
116          struct mnttab mp;
# Line 136 | Line 122 | disk_stat_t *get_disk_stats(int *entries){
122   #endif
123   #ifdef ALLBSD
124          int nummnt;
125 + #ifdef HAVE_STATVFS
126 +        struct statvfs *mp;
127 + #else
128          struct statfs *mp;
129   #endif
130 + #endif
131  
142        if(watermark==-1){
143                disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
144                if(disk_stats==NULL){
145                        return NULL;
146                }
147                watermark=START_VAL;
148                init_disk_stat(0, watermark-1, disk_stats);
149        }
132   #ifdef ALLBSD
133          nummnt=getmntinfo(&mp , MNT_LOCAL);
134          if (nummnt<=0){
135 +                sg_set_error(SG_ERROR_GETMNTINFO, NULL);
136                  return NULL;
137          }
138          for(;nummnt--; mp++){
# Line 158 | Line 141 | disk_stat_t *get_disk_stats(int *entries){
141  
142   #if defined(LINUX) || defined(CYGWIN)
143          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
144 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
145                  return NULL;
146          }
147  
# Line 171 | Line 155 | disk_stat_t *get_disk_stats(int *entries){
155  
156   #ifdef SOLARIS
157          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
158 +                sg_set_error(SG_ERROR_OPEN, "/etc/mnttab");
159                  return NULL;
160          }
161          while((getmntent(f, &mp)) == 0){
# Line 181 | Line 166 | disk_stat_t *get_disk_stats(int *entries){
166   #endif
167  
168                  if(valid_type){
169 <                        if(num_disks>watermark-1){
170 <                                disk_ptr=disk_stats;
186 <                                if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
187 <                                        disk_stats=disk_ptr;
188 <                                        return NULL;
189 <                                }
190 <
191 <                                watermark=watermark*2;
192 <                                init_disk_stat(num_disks, watermark-1, disk_stats);
169 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
170 >                                return NULL;
171                          }
194
172                          disk_ptr=disk_stats+num_disks;
173 +
174   #ifdef ALLBSD
175 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
175 >                        if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) {
176                                  return NULL;
177                          }
178 <
201 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
178 >                        if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) {
179                                  return NULL;
180                          }
181 <
205 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
181 >                        if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) {
182                                  return NULL;
183                          }
184  
# Line 216 | Line 192 | disk_stat_t *get_disk_stats(int *entries){
192                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
193   #endif
194   #if defined(LINUX) || defined(CYGWIN)
195 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
195 >                        if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
196                                  return NULL;
197                          }
198                                  
199 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
199 >                        if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) {  
200                                  return NULL;
201                          }
202  
203 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
203 >                        if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) {
204                                  return NULL;
205                          }
206                          disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
# Line 238 | Line 214 | disk_stat_t *get_disk_stats(int *entries){
214   #endif
215  
216   #ifdef SOLARIS
241                        /* Memory leak in event of realloc failing */
217                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
218                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
219                           * be upwards of a k each
220                           */
221 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
221 >                        if (sg_update_string(&disk_ptr->device_name, mp.mnt_special) < 0) {
222                                  return NULL;
223                          }
224  
225 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
226 <                                return NULL;
227 <                        }
225 >                        if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
226 >                                return NULL;
227 >                        }
228          
229 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
230 <                                return NULL;
231 <                        }
229 >                        if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
230 >                                return NULL;
231 >                        }
232                          
233                          disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
234                          disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
# Line 270 | Line 245 | disk_stat_t *get_disk_stats(int *entries){
245          *entries=num_disks;    
246  
247          /* If this fails, there is very little i can do about it, so
248 <           I'll ignore it :) */
248 >           I'll ignore it :) */
249   #if defined(LINUX) || defined(CYGWIN)
250          endmntent(f);
251   #endif
# Line 281 | Line 256 | disk_stat_t *get_disk_stats(int *entries){
256          return disk_stats;
257  
258   }
284 void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
259  
260 <        for(diskio_stats+=start; start<end; start++){
261 <                diskio_stats->disk_name=NULL;
262 <                
263 <                diskio_stats++;
264 <        }
260 > int sg_fs_compare_device_name(const void *va, const void *vb) {
261 >        const sg_fs_stats *a = (const sg_fs_stats *)va;
262 >        const sg_fs_stats *b = (const sg_fs_stats *)vb;
263 >
264 >        return strcmp(a->device_name, b->device_name);
265   }
266  
267 < diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){
267 > int sg_fs_compare_mnt_point(const void *va, const void *vb) {
268 >        const sg_fs_stats *a = (const sg_fs_stats *)va;
269 >        const sg_fs_stats *b = (const sg_fs_stats *)vb;
270  
271 <        if(diskio_stats==NULL){
271 >        return strcmp(a->mnt_point, b->mnt_point);
272 > }
273  
274 <                if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){
275 <                        return NULL;
276 <                }
300 <                diskio_stat_init(0, needed_entries, diskio_stats);
301 <                *cur_entries=needed_entries;
274 > static void diskio_stat_init(sg_disk_io_stats *d) {
275 >        d->disk_name = NULL;
276 > }
277  
278 <                return diskio_stats;
279 <        }
305 <
306 <
307 <        if(*cur_entries<needed_entries){
308 <                diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries));
309 <                if(diskio_stats==NULL){
310 <                        return NULL;
311 <                }
312 <                diskio_stat_init(*cur_entries, needed_entries, diskio_stats);
313 <                *cur_entries=needed_entries;
314 <        }
315 <
316 <        return diskio_stats;
278 > static void diskio_stat_destroy(sg_disk_io_stats *d) {
279 >        free(d->disk_name);
280   }
281  
282 < static diskio_stat_t *diskio_stats=NULL;        
283 < static int num_diskio=0;        
282 > VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
283 >                      diskio_stat_init, diskio_stat_destroy);
284  
285   #ifdef LINUX
286   typedef struct {
# Line 326 | Line 289 | typedef struct {
289   } partition;
290   #endif
291  
292 < diskio_stat_t *get_diskio_stats(int *entries){
293 <
331 <        static int sizeof_diskio_stats=0;
292 > sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
293 >        int num_diskio;
294   #ifndef LINUX
295 <        diskio_stat_t *diskio_stats_ptr;
295 >        sg_disk_io_stats *diskio_stats_ptr;
296   #endif
297  
298   #ifdef SOLARIS
299 <        kstat_ctl_t *kc;
300 <        kstat_t *ksp;
299 >        kstat_ctl_t *kc;
300 >        kstat_t *ksp;
301          kstat_io_t kios;
302   #endif
303   #ifdef LINUX
# Line 343 | Line 305 | diskio_stat_t *get_diskio_stats(int *entries){
305          char *line_ptr;
306          int major, minor;
307          int has_pp_stats = 1;
308 <        static partition *parts = NULL;
347 <        static int alloc_parts = 0;
308 >        VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL);
309          int i, n;
310          time_t now;
311          const char *format;
312   #endif
313 < #ifdef FREEBSD
313 > #if defined(FREEBSD) || defined(DFBSD)
314          static struct statinfo stats;
315          static int stats_init = 0;
316          int counter;
# Line 362 | Line 323 | diskio_stat_t *get_diskio_stats(int *entries){
323          struct disk_sysctl *stats;
324   #endif
325   #ifdef OPENBSD
326 +        int diskcount;
327 +        char *disknames, *name, *bufpp;
328 +        char **dk_name;
329          struct diskstats *stats;
330   #endif
331   #ifdef NETBSD
# Line 378 | Line 342 | diskio_stat_t *get_diskio_stats(int *entries){
342  
343          num_diskio=0;
344  
345 + #ifdef OPENBSD
346 +        mib[0] = CTL_HW;
347 +        mib[1] = HW_DISKCOUNT;
348 +
349 +        size = sizeof(diskcount);
350 +        if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
351 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
352 +                return NULL;
353 +        }
354 +
355 +        mib[0] = CTL_HW;
356 +        mib[1] = HW_DISKNAMES;
357 +
358 +        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
359 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
360 +                return NULL;
361 +        }
362 +
363 +        disknames = sg_malloc(size);
364 +        if (disknames == NULL) {
365 +                return NULL;
366 +        }
367 +
368 +        if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
369 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
370 +                return NULL;
371 +        }
372 +
373 +        dk_name = sg_malloc(diskcount * sizeof(char *));
374 +        bufpp = disknames;
375 +        for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
376 +                dk_name[i] = name;
377 +        }
378 + #endif
379 +
380   #if defined(NETBSD) || defined(OPENBSD)
381          mib[0] = CTL_HW;
382          mib[1] = HW_DISKSTATS;
# Line 386 | Line 385 | diskio_stat_t *get_diskio_stats(int *entries){
385   #endif
386  
387          if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
388 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
389                  return NULL;
390          }
391  
# Line 395 | Line 395 | diskio_stat_t *get_diskio_stats(int *entries){
395          num_disks = size / sizeof(struct diskstats);
396   #endif
397  
398 <        stats = malloc(size);
398 >        stats = sg_malloc(size);
399          if (stats == NULL) {
400                  return NULL;
401          }
402  
403          if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
404 +                sg_set_error(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
405                  return NULL;
406          }
407  
408          for (i = 0; i < num_disks; i++) {
409 +                const char *name;
410                  u_int64_t rbytes, wbytes;
411  
412   #ifdef NETBSD
# Line 412 | Line 414 | diskio_stat_t *get_diskio_stats(int *entries){
414                  rbytes = stats[i].dk_rbytes;
415                  wbytes = stats[i].dk_wbytes;
416   #else
417 <                /* Before 1.7, NetBSD merged reads and writes. */
417 >                /* Before 2.0, NetBSD merged reads and writes. */
418                  rbytes = wbytes = stats[i].dk_bytes;
419   #endif
420   #else
421 + #ifdef HAVE_DS_RBYTES
422 +                rbytes = stats[i].ds_rbytes;
423 +                wbytes = stats[i].ds_wbytes;
424 + #else
425 +                /* Before 3.5, OpenBSD merged reads and writes */
426                  rbytes = wbytes = stats[i].ds_bytes;
427   #endif
428 + #endif
429  
430                  /* Don't keep stats for disks that have never been used. */
431                  if (rbytes == 0 && wbytes == 0) {
432                          continue;
433                  }
434  
435 <                diskio_stats = diskio_stat_malloc(num_diskio + 1,
428 <                                                  &sizeof_diskio_stats,
429 <                                                  diskio_stats);
430 <                if (diskio_stats == NULL) {
435 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
436                          return NULL;
437                  }
438                  diskio_stats_ptr = diskio_stats + num_diskio;
439                  
440                  diskio_stats_ptr->read_bytes = rbytes;
441                  diskio_stats_ptr->write_bytes = wbytes;
437                if (diskio_stats_ptr->disk_name != NULL) {
438                        free(diskio_stats_ptr->disk_name);
439                }
442   #ifdef NETBSD
443 <                diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
443 >                name = stats[i].dk_name;
444   #else
445 <                asprintf((&diskio_stats_ptr->disk_name), "%s%d", "disk", i);
445 >                name = dk_name[i];
446   #endif
447 +                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
448 +                        return NULL;
449 +                }
450                  diskio_stats_ptr->systime = time(NULL);
451          
452                  num_diskio++;  
453          }
454  
455          free(stats);
456 + #ifdef OPENBSD
457 +        free(dk_name);
458 +        free(disknames);
459   #endif
460 + #endif
461  
462 < #ifdef FREEBSD
462 > #if defined(FREEBSD) || defined(DFBSD)
463          if (!stats_init) {
464 <                stats.dinfo=malloc(sizeof(struct devinfo));
464 >                stats.dinfo=sg_malloc(sizeof(struct devinfo));
465                  if(stats.dinfo==NULL) return NULL;
466                  bzero(stats.dinfo, sizeof(struct devinfo));
467                  stats_init = 1;
468          }
469   #ifdef FREEBSD5
470 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
470 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
471 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
472 >                return NULL;
473 >        }
474          /* Not aware of a get all devices, so i said 999. If we ever
475           * find a machine with more than 999 disks, then i'll change
476           * this number :)
477           */
478 <        if (devstat_selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL;
478 >        if (devstat_selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) {
479 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
480 >                return NULL;
481 >        }
482   #else
483 <        if ((getdevs(&stats)) < 0) return NULL;
483 >        if ((getdevs(&stats)) < 0) {
484 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
485 >                return NULL;
486 >        }
487          /* Not aware of a get all devices, so i said 999. If we ever
488           * find a machine with more than 999 disks, then i'll change
489           * this number :)
490           */
491 <        if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) return NULL;
491 >        if (selectdevs(&dev_sel, &n_selected, &n_selections, &sel_gen, stats.dinfo->generation, stats.dinfo->devices, stats.dinfo->numdevs, NULL, 0, NULL, 0, DS_SELECT_ONLY, 999, 1) < 0) {
492 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
493 >                return NULL;
494 >        }
495   #endif
496  
497          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 485 | Line 506 | diskio_stat_t *get_diskio_stats(int *entries){
506   #else
507                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
508   #endif
509 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
509 >
510 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
511                          return NULL;
512                  }
513                  diskio_stats_ptr=diskio_stats+num_diskio;
# Line 498 | Line 520 | diskio_stat_t *get_diskio_stats(int *entries){
520                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
521   #endif
522                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
523 <                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
523 >                if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
524 >                        sg_set_error(SG_ERROR_ASPRINTF, NULL);
525 >                        return NULL;
526 >                }
527                  diskio_stats_ptr->systime=time(NULL);
528  
529                  num_diskio++;
# Line 508 | Line 533 | diskio_stat_t *get_diskio_stats(int *entries){
533   #endif
534   #ifdef SOLARIS
535          if ((kc = kstat_open()) == NULL) {
536 <                return NULL;
537 <        }
536 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
537 >                return NULL;
538 >        }
539  
540          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
541 <                if (!strcmp(ksp->ks_class, "disk")) {
541 >                if (!strcmp(ksp->ks_class, "disk")) {
542  
543                          if(ksp->ks_type != KSTAT_TYPE_IO) continue;
544                          /* We dont want metadevices appearins as num_diskio */
545                          if(strcmp(ksp->ks_module, "md")==0) continue;
546 <                        if((kstat_read(kc, ksp, &kios))==-1){  
546 >                        if((kstat_read(kc, ksp, &kios))==-1){  
547                          }
548                          
549 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
549 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
550                                  kstat_close(kc);
551                                  return NULL;
552                          }
553                          diskio_stats_ptr=diskio_stats+num_diskio;
554                          
555                          diskio_stats_ptr->read_bytes=kios.nread;
530                        
556                          diskio_stats_ptr->write_bytes=kios.nwritten;
557 <
558 <                        if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
559 <
560 <                        diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name));
557 >                        if (sg_update_string(&diskio_stats_ptr->disk_name,
558 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
559 >                                return NULL;
560 >                        }
561                          diskio_stats_ptr->systime=time(NULL);
562 +
563                          num_diskio++;
564                  }
565          }
# Line 551 | Line 577 | diskio_stat_t *get_diskio_stats(int *entries){
577             the same format. */
578  
579          f = fopen("/proc/diskstats", "r");
580 <        format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld";
580 >        format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld";
581          if (f == NULL) {
582                  f = fopen("/proc/partitions", "r");
583 <                format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld";
583 >                format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld";
584          }
585          if (f == NULL) goto out;
586          now = time(NULL);
587  
588 <        while ((line_ptr = f_read_line(f, "")) != NULL) {
589 <                char name[20];
588 >        while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
589 >                char name[100];
590                  char *s;
591                  long long rsect, wsect;
592  
# Line 581 | Line 607 | diskio_stat_t *get_diskio_stats(int *entries){
607                          wsect = 0;
608                  }
609  
610 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
611 <                        diskio_stats);
586 <                if (diskio_stats == NULL) goto out;
587 <                if (n >= alloc_parts) {
588 <                        alloc_parts += 16;
589 <                        parts = realloc(parts, alloc_parts * sizeof *parts);
590 <                        if (parts == NULL) {
591 <                                alloc_parts = 0;
592 <                                goto out;
593 <                        }
610 >                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
611 >                        goto out;
612                  }
613 +                if (VECTOR_RESIZE(parts, n + 1) < 0) {
614 +                        goto out;
615 +                }
616  
617 <                if (diskio_stats[n].disk_name != NULL)
618 <                        free(diskio_stats[n].disk_name);
619 <                diskio_stats[n].disk_name = strdup(name);
617 >                if (sg_update_string(&diskio_stats[n].disk_name, name) < 0) {
618 >                        goto out;
619 >                }
620                  diskio_stats[n].read_bytes = rsect * 512;
621                  diskio_stats[n].write_bytes = wsect * 512;
622                  diskio_stats[n].systime = now;
# Line 617 | Line 638 | diskio_stat_t *get_diskio_stats(int *entries){
638                  if (f == NULL) goto out;
639                  now = time(NULL);
640          
641 <                line_ptr = f_read_line(f, "disk_io:");
641 >                line_ptr = sg_f_read_line(f, "disk_io:");
642                  if (line_ptr == NULL) goto out;
643          
644                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
# Line 681 | Line 702 | out:
702   #endif
703  
704   #ifdef CYGWIN
705 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
706          return NULL;
707   #endif
708  
# Line 689 | Line 711 | out:
711          return diskio_stats;
712   }
713  
714 < diskio_stat_t *get_diskio_stats_diff(int *entries){
715 <        static diskio_stat_t *diff = NULL;
716 <        static int diff_count = 0;
717 <        diskio_stat_t *src, *dest;
718 <        int i, j, new_count;
714 > sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
715 >        VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
716 >                              diskio_stat_init, diskio_stat_destroy);
717 >        sg_disk_io_stats *src = NULL, *dest;
718 >        int i, j, diff_count, new_count;
719  
720          if (diskio_stats == NULL) {
721                  /* No previous stats, so we can't calculate a difference. */
722 <                return get_diskio_stats(entries);
722 >                return sg_get_disk_io_stats(entries);
723          }
724  
725          /* Resize the results array to match the previous stats. */
726 <        diff = diskio_stat_malloc(num_diskio, &diff_count, diff);
727 <        if (diff == NULL) {
726 >        diff_count = VECTOR_SIZE(diskio_stats);
727 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
728                  return NULL;
729          }
730  
# Line 711 | Line 733 | diskio_stat_t *get_diskio_stats_diff(int *entries){
733                  src = &diskio_stats[i];
734                  dest = &diff[i];
735  
736 <                if (dest->disk_name != NULL) {
737 <                        free(dest->disk_name);
736 >                if (sg_update_string(&dest->disk_name, src->disk_name) < 0) {
737 >                        return NULL;
738                  }
717                dest->disk_name = strdup(src->disk_name);
739                  dest->read_bytes = src->read_bytes;
740                  dest->write_bytes = src->write_bytes;
741                  dest->systime = src->systime;
742          }
743  
744          /* Get a new set of stats. */
745 <        if (get_diskio_stats(&new_count) == NULL) {
745 >        if (sg_get_disk_io_stats(&new_count) == NULL) {
746                  return NULL;
747          }
748  
# Line 752 | Line 773 | diskio_stat_t *get_diskio_stats_diff(int *entries){
773  
774          *entries = diff_count;
775          return diff;
776 + }
777 +
778 + int sg_disk_io_compare_name(const void *va, const void *vb) {
779 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
780 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
781 +
782 +        return strcmp(a->disk_name, b->disk_name);
783   }
784  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines