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.46 by ats, Sat Jan 10 16:12:34 2004 UTC vs.
Revision 1.62 by ats, Mon Apr 5 18:10:11 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 > * 02111-1307 USA
20 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 27 | 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 38 | Line 43
43   #if defined(LINUX) || defined(CYGWIN)
44   #include <mntent.h>
45   #include <sys/vfs.h>
41 #include "tools.h"
46   #endif
47  
48   #ifdef LINUX
# Line 58 | 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"}
70   #endif
71 < #ifdef NETBSD
71 > #if defined(NETBSD) || defined(OPENBSD)
72   #include <sys/param.h>
73   #include <sys/sysctl.h>
74   #include <sys/disk.h>
# Line 72 | Line 76
76                          "ntfs"}
77   #endif
78  
79 < #define START_VAL 1
80 <
81 < char *copy_string(char *orig_ptr, const char *newtext){
82 <
79 <        /* Maybe free if not NULL, and strdup rather than realloc and strcpy? */
80 <        orig_ptr=realloc(orig_ptr, (1+strlen(newtext)));
81 <        if(orig_ptr==NULL){
82 <                return NULL;
83 <        }
84 <        strcpy(orig_ptr, newtext);
85 <
86 <        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++){
93 <                disk_stats->device_name=NULL;
94 <                disk_stats->fs_type=NULL;
95 <                disk_stats->mnt_point=NULL;
96 <                
97 <                disk_stats++;
98 <        }
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 110 | 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  
115        static disk_stat_t *disk_stats;
116        static int watermark=-1;
117
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 125 | disk_stat_t *get_disk_stats(int *entries){
125          struct statfs *mp;
126   #endif
127  
139        if(watermark==-1){
140                disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
141                if(disk_stats==NULL){
142                        return NULL;
143                }
144                watermark=START_VAL;
145                init_disk_stat(0, watermark-1, disk_stats);
146        }
128   #ifdef ALLBSD
129          nummnt=getmntinfo(&mp , MNT_LOCAL);
130          if (nummnt<=0){
# Line 178 | Line 159 | disk_stat_t *get_disk_stats(int *entries){
159   #endif
160  
161                  if(valid_type){
162 <                        if(num_disks>watermark-1){
163 <                                disk_ptr=disk_stats;
183 <                                if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
184 <                                        disk_stats=disk_ptr;
185 <                                        return NULL;
186 <                                }
187 <
188 <                                watermark=watermark*2;
189 <                                init_disk_stat(num_disks, watermark-1, disk_stats);
162 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
163 >                                return NULL;
164                          }
191
165                          disk_ptr=disk_stats+num_disks;
166 +
167   #ifdef ALLBSD
168 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
168 >                        if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) {
169                                  return NULL;
170                          }
171 <
198 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
171 >                        if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) {
172                                  return NULL;
173                          }
174 <
202 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
174 >                        if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) {
175                                  return NULL;
176                          }
177  
# Line 213 | Line 185 | disk_stat_t *get_disk_stats(int *entries){
185                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
186   #endif
187   #if defined(LINUX) || defined(CYGWIN)
188 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
188 >                        if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
189                                  return NULL;
190                          }
191                                  
192 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
192 >                        if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) {  
193                                  return NULL;
194                          }
195  
196 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
196 >                        if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) {
197                                  return NULL;
198                          }
199                          disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
# Line 235 | Line 207 | disk_stat_t *get_disk_stats(int *entries){
207   #endif
208  
209   #ifdef SOLARIS
238                        /* Memory leak in event of realloc failing */
210                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
211                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
212                           * be upwards of a k each
213                           */
214 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
214 >                        if (sg_update_string(&disk_ptr->device_name, mp.mnt_special) < 0) {
215                                  return NULL;
216                          }
217  
218 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
218 >                        if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
219                                  return NULL;
220                          }
221          
222 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
222 >                        if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
223                                  return NULL;
224                          }
225                          
# Line 278 | Line 249 | disk_stat_t *get_disk_stats(int *entries){
249          return disk_stats;
250  
251   }
281 void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
252  
253 <        for(diskio_stats+=start; start<end; start++){
254 <                diskio_stats->disk_name=NULL;
285 <                
286 <                diskio_stats++;
287 <        }
253 > static void diskio_stat_init(sg_disk_io_stats *d) {
254 >        d->disk_name = NULL;
255   }
256  
257 < diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){
258 <
292 <        if(diskio_stats==NULL){
293 <
294 <                if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){
295 <                        return NULL;
296 <                }
297 <                diskio_stat_init(0, needed_entries, diskio_stats);
298 <                *cur_entries=needed_entries;
299 <
300 <                return diskio_stats;
301 <        }
302 <
303 <
304 <        if(*cur_entries<needed_entries){
305 <                diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries));
306 <                if(diskio_stats==NULL){
307 <                        return NULL;
308 <                }
309 <                diskio_stat_init(*cur_entries, needed_entries, diskio_stats);
310 <                *cur_entries=needed_entries;
311 <        }
312 <
313 <        return diskio_stats;
257 > static void diskio_stat_destroy(sg_disk_io_stats *d) {
258 >        free(d->disk_name);
259   }
260  
261 < static diskio_stat_t *diskio_stats=NULL;        
262 < static int num_diskio=0;        
261 > VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
262 >                      diskio_stat_init, diskio_stat_destroy);
263  
264   #ifdef LINUX
265   typedef struct {
# Line 323 | Line 268 | typedef struct {
268   } partition;
269   #endif
270  
271 < diskio_stat_t *get_diskio_stats(int *entries){
272 <
328 <        static int sizeof_diskio_stats=0;
271 > sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
272 >        int num_diskio;
273   #ifndef LINUX
274 <        diskio_stat_t *diskio_stats_ptr;
274 >        sg_disk_io_stats *diskio_stats_ptr;
275   #endif
276  
277   #ifdef SOLARIS
# Line 340 | Line 284 | diskio_stat_t *get_diskio_stats(int *entries){
284          char *line_ptr;
285          int major, minor;
286          int has_pp_stats = 1;
287 <        static partition *parts = NULL;
344 <        static int alloc_parts = 0;
287 >        VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL);
288          int i, n;
289          time_t now;
290          const char *format;
291   #endif
292 < #ifdef FREEBSD
292 > #if defined(FREEBSD) || defined(DFBSD)
293          static struct statinfo stats;
294          static int stats_init = 0;
295          int counter;
# Line 357 | Line 300 | diskio_stat_t *get_diskio_stats(int *entries){
300   #endif
301   #ifdef NETBSD
302          struct disk_sysctl *stats;
303 + #endif
304 + #ifdef OPENBSD
305 +        int diskcount;
306 +        char *disknames, *name, *bufpp;
307 +        char **dk_name;
308 +        struct diskstats *stats;
309 + #endif
310 + #ifdef NETBSD
311 + #define MIBSIZE 3
312 + #endif
313 + #ifdef OPENBSD
314 + #define MIBSIZE 2
315 + #endif
316 + #if defined(NETBSD) || defined(OPENBSD)
317          int num_disks, i;
318 <        int mib[3];
318 >        int mib[MIBSIZE];
319          size_t size;
320   #endif
321  
322          num_diskio=0;
323  
324 < #ifdef NETBSD
324 > #ifdef OPENBSD
325          mib[0] = CTL_HW;
326 +        mib[1] = HW_DISKCOUNT;
327 +
328 +        size = sizeof(diskcount);
329 +        if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
330 +                return NULL;
331 +        }
332 +
333 +        mib[0] = CTL_HW;
334 +        mib[1] = HW_DISKNAMES;
335 +
336 +        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
337 +                return NULL;
338 +        }
339 +
340 +        disknames = malloc(size);
341 +        if (disknames == NULL) {
342 +                return NULL;
343 +        }
344 +
345 +        if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
346 +                return NULL;
347 +        }
348 +
349 +        dk_name = calloc(diskcount, sizeof(char *));
350 +        bufpp = disknames;
351 +        for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
352 +                dk_name[i] = name;
353 +        }
354 + #endif
355 +
356 + #if defined(NETBSD) || defined(OPENBSD)
357 +        mib[0] = CTL_HW;
358          mib[1] = HW_DISKSTATS;
359 + #ifdef NETBSD
360          mib[2] = sizeof(struct disk_sysctl);
361 + #endif
362  
363 <        if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
363 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
364                  return NULL;
365          }
366 +
367 + #ifdef NETBSD
368          num_disks = size / sizeof(struct disk_sysctl);
369 + #else
370 +        num_disks = size / sizeof(struct diskstats);
371 + #endif
372  
373          stats = malloc(size);
374          if (stats == NULL) {
375                  return NULL;
376          }
377  
378 <        if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) {
378 >        if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
379                  return NULL;
380          }
381  
382          for (i = 0; i < num_disks; i++) {
383 +                const char *name;
384                  u_int64_t rbytes, wbytes;
385  
386 + #ifdef NETBSD
387   #ifdef HAVE_DK_RBYTES
388                  rbytes = stats[i].dk_rbytes;
389                  wbytes = stats[i].dk_wbytes;
# Line 393 | Line 391 | diskio_stat_t *get_diskio_stats(int *entries){
391                  /* Before 1.7, NetBSD merged reads and writes. */
392                  rbytes = wbytes = stats[i].dk_bytes;
393   #endif
394 + #else
395 +                rbytes = wbytes = stats[i].ds_bytes;
396 + #endif
397  
398                  /* Don't keep stats for disks that have never been used. */
399                  if (rbytes == 0 && wbytes == 0) {
400                          continue;
401                  }
402  
403 <                diskio_stats = diskio_stat_malloc(num_diskio + 1,
403 <                                                  &sizeof_diskio_stats,
404 <                                                  diskio_stats);
405 <                if (diskio_stats == NULL) {
403 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
404                          return NULL;
405                  }
406                  diskio_stats_ptr = diskio_stats + num_diskio;
407                  
408                  diskio_stats_ptr->read_bytes = rbytes;
409                  diskio_stats_ptr->write_bytes = wbytes;
410 <                if (diskio_stats_ptr->disk_name != NULL) {
411 <                        free(diskio_stats_ptr->disk_name);
410 > #ifdef NETBSD
411 >                name = stats[i].dk_name;
412 > #else
413 >                name = dk_name[i];
414 > #endif
415 >                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
416 >                        return NULL;
417                  }
415                diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
418                  diskio_stats_ptr->systime = time(NULL);
419          
420                  num_diskio++;  
421          }
422  
423          free(stats);
424 + #ifdef OPENBSD
425 +        free(dk_name);
426 +        free(disknames);
427   #endif
428 + #endif
429  
430 < #ifdef FREEBSD
430 > #if defined(FREEBSD) || defined(DFBSD)
431          if (!stats_init) {
432                  stats.dinfo=malloc(sizeof(struct devinfo));
433                  if(stats.dinfo==NULL) return NULL;
# Line 456 | Line 462 | diskio_stat_t *get_diskio_stats(int *entries){
462   #else
463                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
464   #endif
465 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
465 >
466 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
467                          return NULL;
468                  }
469                  diskio_stats_ptr=diskio_stats+num_diskio;
# Line 469 | Line 476 | diskio_stat_t *get_diskio_stats(int *entries){
476                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
477   #endif
478                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
479 <                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
479 >                if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
480 >                        return NULL;
481 >                }
482                  diskio_stats_ptr->systime=time(NULL);
483  
484                  num_diskio++;
# Line 491 | Line 500 | diskio_stat_t *get_diskio_stats(int *entries){
500                          if((kstat_read(kc, ksp, &kios))==-1){  
501                          }
502                          
503 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
503 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
504                                  kstat_close(kc);
505                                  return NULL;
506                          }
507                          diskio_stats_ptr=diskio_stats+num_diskio;
508                          
509                          diskio_stats_ptr->read_bytes=kios.nread;
501                        
510                          diskio_stats_ptr->write_bytes=kios.nwritten;
511 <
512 <                        if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
513 <
514 <                        diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name));
511 >                        if (sg_update_string(&diskio_stats_ptr->disk_name,
512 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
513 >                                return NULL;
514 >                        }
515                          diskio_stats_ptr->systime=time(NULL);
516 +
517                          num_diskio++;
518                  }
519          }
# Line 522 | Line 531 | diskio_stat_t *get_diskio_stats(int *entries){
531             the same format. */
532  
533          f = fopen("/proc/diskstats", "r");
534 <        format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld";
534 >        format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld";
535          if (f == NULL) {
536                  f = fopen("/proc/partitions", "r");
537 <                format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld";
537 >                format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld";
538          }
539          if (f == NULL) goto out;
540          now = time(NULL);
541  
542 <        while ((line_ptr = f_read_line(f, "")) != NULL) {
543 <                char name[20];
542 >        while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
543 >                char name[100];
544                  char *s;
545                  long long rsect, wsect;
546  
# Line 552 | Line 561 | diskio_stat_t *get_diskio_stats(int *entries){
561                          wsect = 0;
562                  }
563  
564 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
565 <                        diskio_stats);
557 <                if (diskio_stats == NULL) goto out;
558 <                if (n >= alloc_parts) {
559 <                        alloc_parts += 16;
560 <                        parts = realloc(parts, alloc_parts * sizeof *parts);
561 <                        if (parts == NULL) {
562 <                                alloc_parts = 0;
563 <                                goto out;
564 <                        }
564 >                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
565 >                        goto out;
566                  }
567 +                if (VECTOR_RESIZE(parts, n + 1) < 0) {
568 +                        goto out;
569 +                }
570  
571 <                if (diskio_stats[n].disk_name != NULL)
572 <                        free(diskio_stats[n].disk_name);
573 <                diskio_stats[n].disk_name = strdup(name);
571 >                if (sg_update_string(&diskio_stats[n].disk_name, name) < 0) {
572 >                        goto out;
573 >                }
574                  diskio_stats[n].read_bytes = rsect * 512;
575                  diskio_stats[n].write_bytes = wsect * 512;
576                  diskio_stats[n].systime = now;
# Line 588 | Line 592 | diskio_stat_t *get_diskio_stats(int *entries){
592                  if (f == NULL) goto out;
593                  now = time(NULL);
594          
595 <                line_ptr = f_read_line(f, "disk_io:");
595 >                line_ptr = sg_f_read_line(f, "disk_io:");
596                  if (line_ptr == NULL) goto out;
597          
598                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
# Line 660 | Line 664 | out:
664          return diskio_stats;
665   }
666  
667 < diskio_stat_t *get_diskio_stats_diff(int *entries){
668 <        static diskio_stat_t *diskio_stats_diff=NULL;
669 <        static int sizeof_diskio_stats_diff=0;
670 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
671 <        int disks, x, y;
667 > sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
668 >        VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
669 >                              diskio_stat_init, diskio_stat_destroy);
670 >        sg_disk_io_stats *src = NULL, *dest;
671 >        int i, j, diff_count, new_count;
672  
673 <        if(diskio_stats==NULL){
674 <                diskio_stats_ptr=get_diskio_stats(&disks);
675 <                *entries=disks;
672 <                return diskio_stats_ptr;
673 >        if (diskio_stats == NULL) {
674 >                /* No previous stats, so we can't calculate a difference. */
675 >                return sg_get_disk_io_stats(entries);
676          }
677  
678 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
679 <        if(diskio_stats_diff==NULL){
678 >        /* Resize the results array to match the previous stats. */
679 >        diff_count = VECTOR_SIZE(diskio_stats);
680 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
681                  return NULL;
682          }
683  
684 <        diskio_stats_diff_ptr=diskio_stats_diff;
685 <        diskio_stats_ptr=diskio_stats;
684 >        /* Copy the previous stats into the result. */
685 >        for (i = 0; i < diff_count; i++) {
686 >                src = &diskio_stats[i];
687 >                dest = &diff[i];
688  
689 <        for(disks=0;disks<num_diskio;disks++){
690 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
685 <                        free(diskio_stats_diff_ptr->disk_name);
689 >                if (sg_update_string(&dest->disk_name, src->disk_name) < 0) {
690 >                        return NULL;
691                  }
692 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
693 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
694 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
695 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
692 >                dest->read_bytes = src->read_bytes;
693 >                dest->write_bytes = src->write_bytes;
694 >                dest->systime = src->systime;
695 >        }
696  
697 <                diskio_stats_diff_ptr++;
698 <                diskio_stats_ptr++;
697 >        /* Get a new set of stats. */
698 >        if (sg_get_disk_io_stats(&new_count) == NULL) {
699 >                return NULL;
700          }
701  
702 <        diskio_stats_ptr=get_diskio_stats(&disks);
703 <        diskio_stats_diff_ptr=diskio_stats_diff;
702 >        /* For each previous stat... */
703 >        for (i = 0; i < diff_count; i++) {
704 >                dest = &diff[i];
705  
706 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
707 <
708 <                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
709 <                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
710 <                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
711 <                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
712 <                }else{
706 <                        diskio_stats_ptr=diskio_stats;
707 <                        for(y=0;y<disks;y++){
708 <                                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
709 <                                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
710 <                                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
711 <                                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
712 <
713 <                                        break;
714 <                                }
715 <                                
716 <                                diskio_stats_ptr++;
706 >                /* ... find the corresponding new stat ... */
707 >                for (j = 0; j < new_count; j++) {
708 >                        /* Try the new stat in the same position first,
709 >                           since that's most likely to be it. */
710 >                        src = &diskio_stats[(i + j) % new_count];
711 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
712 >                                break;
713                          }
714                  }
715 +                if (j == new_count) {
716 +                        /* No match found. */
717 +                        continue;
718 +                }
719  
720 <                diskio_stats_ptr++;
721 <                diskio_stats_diff_ptr++;        
722 <
720 >                /* ... and subtract the previous stat from it to get the
721 >                   difference. */
722 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
723 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
724 >                dest->systime = src->systime - dest->systime;
725          }
726 <        
727 <        *entries=sizeof_diskio_stats_diff;
728 <        return diskio_stats_diff;
726 >
727 >        *entries = diff_count;
728 >        return diff;
729   }
730 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines