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.23 by pajs, Fri Sep 26 22:30:48 2003 UTC vs.
Revision 1.76 by ats, Sat Nov 6 15:33:40 2004 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
1 > /*
2 > * i-scream libstatgrab
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 25 | Line 28
28   #include <stdio.h>
29   #include <stdlib.h>
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>
38   #include <sys/statvfs.h>
39   #include <kstat.h>
40 < #include <sys/types.h>
35 < #include <dirent.h>
36 < #include <limits.h>
37 < #include <unistd.h>
38 < #define VALID_FS_TYPES {"ufs", "tmpfs"}
40 > #define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs"}
41   #endif
42  
43 < #ifdef LINUX
42 < #include <sys/vfs.h>
43 > #if defined(LINUX) || defined(CYGWIN)
44   #include <mntent.h>
45 < #include "tools.h"
45 < #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
45 > #include <sys/vfs.h>
46   #endif
47  
48 < #ifdef FREEBSD
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"}
54 > #endif
55 >
56 > #ifdef CYGWIN
57 > #define VALID_FS_TYPES {"user"}
58 > #endif
59 >
60 > #ifdef ALLBSD
61   #include <sys/param.h>
62   #include <sys/ucred.h>
63   #include <sys/mount.h>
64 + #endif
65 + #if defined(FREEBSD) || defined(DFBSD)
66   #include <sys/dkstat.h>
67   #include <devstat.h>
68 < #define VALID_FS_TYPES {"ufs", "mfs"}
68 > #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
69 >                        "ufs", "mfs"}
70   #endif
71 < #define START_VAL 1
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"}
77 > #endif
78  
79 < /* Solaris kernel stores the disk names in the old BSD format
80 < * and we would prefer it in th modern format.
81 < */
82 < #ifdef SOLARIS
83 < /* Lines from the path_to_inst file */
84 < typedef struct {
85 <        /* unlikely to get a line that long */
86 <        char line[256];
87 < }file_line_t;
88 <
89 < /* Linked list of the mappings from the older bsd-based sunos names
90 < * to the modern solaris names
70 < */
71 < struct disk_mapping_ent_t{
72 <        char *sunos_name;
73 <        char *solaris_name;
74 <        struct disk_mapping_ent_t *next;
75 < };
76 < typedef struct disk_mapping_ent_t disk_mapping_t;
79 > #ifdef HPUX
80 > #include <sys/param.h>
81 > #include <sys/pstat.h>
82 > #include <sys/types.h>
83 > #include <sys/stat.h>
84 > #include <sys/vfs.h>
85 > #include <mntent.h>
86 > #include <dirent.h>
87 > #include <stdio.h>
88 > #include <time.h>
89 > #define VALID_FS_TYPES {"vxfs", "hfs"}
90 > #define DISK_BATCH 30
91   #endif
92  
93 < char *copy_string(char *orig_ptr, const char *newtext){
93 > static void disk_stat_init(sg_fs_stats *d) {
94 >        d->device_name = NULL;
95 >        d->fs_type = NULL;
96 >        d->mnt_point = NULL;
97 > }
98  
99 <        /* Maybe free if not NULL, and strdup rather than realloc and strcpy? */
100 <        orig_ptr=realloc(orig_ptr, (1+strlen(newtext)));
101 <        if(orig_ptr==NULL){
102 <                return NULL;
85 <        }
86 <        strcpy(orig_ptr, newtext);
87 <
88 <        return orig_ptr;
99 > static void disk_stat_destroy(sg_fs_stats *d) {
100 >        free(d->device_name);
101 >        free(d->fs_type);
102 >        free(d->mnt_point);
103   }
104  
105 + static int is_valid_fs_type(const char *type) {
106 +        const char *types[] = VALID_FS_TYPES;
107 +        int i;
108  
109 < void init_disk_stat(int start, int end, disk_stat_t *disk_stats){
110 <
111 <        for(disk_stats+=start; start<=end; start++){
112 <                disk_stats->device_name=NULL;
96 <                disk_stats->fs_type=NULL;
97 <                disk_stats->mnt_point=NULL;
98 <                
99 <                disk_stats++;
109 >        for (i = 0; i < (sizeof types / sizeof *types); i++) {
110 >                if (strcmp(types[i], type) == 0) {
111 >                        return 1;
112 >                }
113          }
114 +        return 0;
115   }
116  
117 < disk_stat_t *get_disk_stats(int *entries){
117 > sg_fs_stats *sg_get_fs_stats(int *entries){
118 >        VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
119 >                              disk_stat_init, disk_stat_destroy);
120  
121 <        static disk_stat_t *disk_stats;
106 <        static int watermark=-1;
107 <
108 <        char *fs_types[] = VALID_FS_TYPES;
109 <        int x, valid_type;
121 >        int valid_type;
122          int num_disks=0;
123 < #if defined(LINUX) || defined (SOLARIS)
123 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX)
124          FILE *f;
125   #endif
126  
127 <        disk_stat_t *disk_ptr;
127 >        sg_fs_stats *disk_ptr;
128  
129   #ifdef SOLARIS
130          struct mnttab mp;
131          struct statvfs fs;
132   #endif
133 < #ifdef LINUX
133 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
134          struct mntent *mp;
135          struct statfs fs;
136   #endif
137 < #ifdef FREEBSD
137 > #ifdef ALLBSD
138          int nummnt;
139 + #ifdef HAVE_STATVFS
140 +        struct statvfs *mp;
141 + #else
142          struct statfs *mp;
143   #endif
144 + #endif
145  
146 <        if(watermark==-1){
131 <                disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
132 <                if(disk_stats==NULL){
133 <                        return NULL;
134 <                }
135 <                watermark=START_VAL;
136 <                init_disk_stat(0, watermark-1, disk_stats);
137 <        }
138 < #ifdef FREEBSD
146 > #ifdef ALLBSD
147          nummnt=getmntinfo(&mp , MNT_LOCAL);
148          if (nummnt<=0){
149 +                sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL);
150                  return NULL;
151          }
152          for(;nummnt--; mp++){
153 <                valid_type=0;
145 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
146 <                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
147 <                                valid_type=1;
148 <                                break;
149 <                        }
150 <                }
153 >                valid_type = is_valid_fs_type(mp->f_fstypename);
154   #endif
155  
156 < #ifdef LINUX
156 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
157 > #ifdef MNT_MNTTAB
158 >        if ((f=setmntent(MNT_MNTTAB, "r" ))==NULL){
159 > #else
160          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
161 + #endif
162 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
163                  return NULL;
164          }
165  
# Line 160 | Line 168 | disk_stat_t *get_disk_stats(int *entries){
168                          continue;
169                  }      
170  
171 <                valid_type=0;
164 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
165 <                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
166 <                                valid_type=1;
167 <                                break;
168 <                        }
169 <                }
171 >                valid_type = is_valid_fs_type(mp->mnt_type);
172   #endif
173  
174   #ifdef SOLARIS
175          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
176 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/etc/mnttab");
177                  return NULL;
178          }
179          while((getmntent(f, &mp)) == 0){
180                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
181                          continue;
182                  }
183 <                valid_type=0;
181 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
182 <                        if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
183 <                                valid_type=1;
184 <                                break;
185 <                        }
186 <                }
183 >                valid_type = is_valid_fs_type(mp.mnt_fstype);
184   #endif
185  
186                  if(valid_type){
187 <                        if(num_disks>watermark-1){
188 <                                disk_ptr=disk_stats;
192 <                                if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
193 <                                        disk_stats=disk_ptr;
194 <                                        return NULL;
195 <                                }
196 <
197 <                                watermark=watermark*2;
198 <                                init_disk_stat(num_disks, watermark-1, disk_stats);
187 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
188 >                                return NULL;
189                          }
200
190                          disk_ptr=disk_stats+num_disks;
191 < #ifdef FREEBSD
192 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
191 >
192 > #ifdef ALLBSD
193 >                        if (sg_update_string(&disk_ptr->device_name, mp->f_mntfromname) < 0) {
194                                  return NULL;
195                          }
196 <
207 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
196 >                        if (sg_update_string(&disk_ptr->fs_type, mp->f_fstypename) < 0) {
197                                  return NULL;
198                          }
199 <
211 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
199 >                        if (sg_update_string(&disk_ptr->mnt_point, mp->f_mntonname) < 0) {
200                                  return NULL;
201                          }
202  
# Line 221 | Line 209 | disk_stat_t *get_disk_stats(int *entries){
209                          /* Freebsd doesn't have a "available" inodes */
210                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
211   #endif
212 < #ifdef LINUX
213 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
212 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
213 >                        if (sg_update_string(&disk_ptr->device_name, mp->mnt_fsname) < 0) {
214                                  return NULL;
215                          }
216                                  
217 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
217 >                        if (sg_update_string(&disk_ptr->fs_type, mp->mnt_type) < 0) {  
218                                  return NULL;
219                          }
220  
221 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
221 >                        if (sg_update_string(&disk_ptr->mnt_point, mp->mnt_dir) < 0) {
222                                  return NULL;
223                          }
224                          disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
# Line 244 | Line 232 | disk_stat_t *get_disk_stats(int *entries){
232   #endif
233  
234   #ifdef SOLARIS
247                        /* Memory leak in event of realloc failing */
235                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
236                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
237                           * be upwards of a k each
238                           */
239 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
239 >                        if (sg_update_string(&disk_ptr->device_name, mp.mnt_special) < 0) {
240                                  return NULL;
241                          }
242  
243 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
244 <                                return NULL;
245 <                        }
243 >                        if (sg_update_string(&disk_ptr->fs_type, mp.mnt_fstype) < 0) {
244 >                                return NULL;
245 >                        }
246          
247 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
248 <                                return NULL;
249 <                        }
247 >                        if (sg_update_string(&disk_ptr->mnt_point, mp.mnt_mountp) < 0) {
248 >                                return NULL;
249 >                        }
250                          
251                          disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
252                          disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
# Line 275 | Line 262 | disk_stat_t *get_disk_stats(int *entries){
262  
263          *entries=num_disks;    
264  
265 <        /* If this fails, there is very little i can do about it, so i'll ignore it :) */
266 < #if defined(LINUX) || defined(SOLARIS)
265 >        /* If this fails, there is very little i can do about it, so
266 >           I'll ignore it :) */
267 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
268 >        endmntent(f);
269 > #endif
270 > #if defined(SOLARIS)
271          fclose(f);
272   #endif
273  
274          return disk_stats;
275  
276   }
286 void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
277  
278 <        for(diskio_stats+=start; start<end; start++){
279 <                diskio_stats->disk_name=NULL;
280 <                
281 <                diskio_stats++;
282 <        }
278 > int sg_fs_compare_device_name(const void *va, const void *vb) {
279 >        const sg_fs_stats *a = (const sg_fs_stats *)va;
280 >        const sg_fs_stats *b = (const sg_fs_stats *)vb;
281 >
282 >        return strcmp(a->device_name, b->device_name);
283   }
284  
285 < #ifdef SOLARIS
286 < char *drive_map(char *sunos_name){
287 <        file_line_t *file_lines = NULL;
298 <        file_line_t *f_ptr;
299 <        static disk_mapping_t *disk_mapping = NULL;
300 <        disk_mapping_t *d_ptr;
301 <        int x = 0;
302 <        int y = 256;
303 <        FILE *f;
304 <        DIR *dsk;
305 <        struct dirent *dsk_ent;
306 <        char linkpointer[PATH_MAX];
307 <        char link_path[PATH_MAX];
308 <        char *p, *r, *q;
285 > int sg_fs_compare_mnt_point(const void *va, const void *vb) {
286 >        const sg_fs_stats *a = (const sg_fs_stats *)va;
287 >        const sg_fs_stats *b = (const sg_fs_stats *)vb;
288  
289 <        int dev_num;
290 <        char dev_name[51];
289 >        return strcmp(a->mnt_point, b->mnt_point);
290 > }
291  
292 <        if(disk_mapping == NULL){
293 <                /* Read in the path_to_inst file into memory */
315 <                file_lines = malloc(sizeof(file_line_t) * y);
316 <                f_ptr = file_lines;
317 <                if(file_lines == NULL) return NULL;
318 <                f = fopen("/etc/path_to_inst", "r");
319 <                if (f == NULL){
320 <                        free(file_lines);
321 <                        return NULL;
322 <                }      
323 <                for(x=0;fgets(f_ptr->line, sizeof(f_ptr->line), f);x++){
324 <                        if ((x+1) >= y){
325 <                                y = y*2;
326 <                                file_lines = realloc(file_lines, sizeof(file_line_t) * y);
327 <                                if (file_lines == NULL) return NULL;
328 <                                f_ptr=file_lines+x;
329 <                        }
330 <                        f_ptr++;
331 <                }
332 <                f_ptr = NULL;
333 <                fclose(f);
334 <
335 <                dsk = opendir("/dev/dsk");
336 <                if (dsk == NULL){
337 <                        free(file_lines);
338 <                        return NULL;
339 <                }
340 <                
341 <                while((dsk_ent = readdir(dsk)) != NULL){
342 <                        snprintf(link_path, sizeof(link_path), "/dev/dsk/%s", dsk_ent->d_name);
343 <                        /* Ignore the non symlinks, e.g. ".." and "." */
344 <                        if((readlink(link_path, linkpointer, PATH_MAX)) == -1) continue;
345 <                        /* We are only intrested in the string past the ../../devices/ part */
346 <                        p = strstr(linkpointer, "devices");
347 <                        p+=8;
348 <                
349 <                        /* We are not intrested in the :a etc slices */
350 <                        q = strrchr(p, ':');
351 <                        *q = '\0';
352 <
353 <                        /* char *p should not contain the info we need to look up in the
354 <                         * path_to_inst file.
355 <                         */
356 <                        
357 <                        for(f_ptr = file_lines; f_ptr != NULL; f_ptr++){
358 <                                r = strstr(f_ptr->line, p);
359 <                                if (r != NULL) break;
360 <                        }
361 <
362 <                        if(r == NULL){
363 <                                free(file_lines);
364 <                                closedir(dsk);
365 <                                return NULL;
366 <                        }
367 <
368 <                        /* f_ptr should be pointing to the line of path_to_inst we are intrested in now */
369 <
370 <                        sscanf(r, "%*s %d \"%50s", &dev_num, dev_name);
371 <                        q = strrchr(dev_name, '"');
372 <                        if (q == NULL){
373 <                                free(file_lines);
374 <                                closedir(dsk);
375 <                                return NULL;
376 <                        }
377 <                        *q = '\0';
378 <                        
379 <                        /* Nasty... Add the number to the end of the string.. This means
380 <                         * dev_name now contains the sunos name.. E.g. ssd1
381 <                         */
382 <                        snprintf(dev_name, 50, "%s%d", dev_name, dev_num);
383 <                        if (disk_mapping == NULL){
384 <                                disk_mapping = malloc(sizeof(disk_mapping_t));
385 <                                d_ptr = disk_mapping;
386 <                        }else{
387 <                                d_ptr->next = malloc(sizeof(disk_mapping_t));
388 <                                d_ptr = d_ptr->next;
389 <                        }
390 <
391 <                        if (d_ptr == NULL){
392 <                                free(file_lines);
393 <                                closedir(dsk);
394 <                                return NULL;
395 <                        }
396 <
397 <                        if( ((d_ptr->sunos_name = strdup(dev_name)) == NULL) ||
398 <                           ((d_ptr->solaris_name = strdup(dsk_ent->d_name))==NULL) ){
399 <                                free(file_lines);
400 <                                closedir(dsk);
401 <                                return NULL;
402 <                        }
403 <                }
404 <                free(file_lines);
405 <                closedir(dsk);
406 <        }
407 <                
408 <        for(d_ptr = disk_mapping; d_ptr !=NULL; d_ptr=d_ptr->next){
409 <                if(!strcmp(sunos_name, d_ptr->sunos_name)){
410 <                        return (strdup(d_ptr->solaris_name));
411 <                }
412 <        }
413 <
414 <        /* If we really fail to find it.. Return the original name back */
415 <        return strdup(sunos_name);
292 > static void diskio_stat_init(sg_disk_io_stats *d) {
293 >        d->disk_name = NULL;
294   }
295  
296 < #endif
297 <
420 < diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){
421 <
422 <        if(diskio_stats==NULL){
423 <
424 <                if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){
425 <                        return NULL;
426 <                }
427 <                diskio_stat_init(0, needed_entries, diskio_stats);
428 <                *cur_entries=needed_entries;
429 <
430 <                return diskio_stats;
431 <        }
432 <
433 <
434 <        if(*cur_entries<needed_entries){
435 <                diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries));
436 <                if(diskio_stats==NULL){
437 <                        return NULL;
438 <                }
439 <                diskio_stat_init(*cur_entries, needed_entries, diskio_stats);
440 <                *cur_entries=needed_entries;
441 <        }
442 <
443 <        return diskio_stats;
296 > static void diskio_stat_destroy(sg_disk_io_stats *d) {
297 >        free(d->disk_name);
298   }
299  
300 < static diskio_stat_t *diskio_stats=NULL;        
301 < static int num_diskio=0;        
300 > VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
301 >                      diskio_stat_init, diskio_stat_destroy);
302  
303   #ifdef LINUX
304   typedef struct {
# Line 453 | Line 307 | typedef struct {
307   } partition;
308   #endif
309  
310 < diskio_stat_t *get_diskio_stats(int *entries){
310 > sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
311 >        int num_diskio;
312 > #ifndef LINUX
313 >        sg_disk_io_stats *diskio_stats_ptr;
314 > #endif
315  
316 <        static int sizeof_diskio_stats=0;
317 <        diskio_stat_t *diskio_stats_ptr;
318 <
316 > #ifdef HPUX
317 >        long long rbytes = 0, wbytes = 0;
318 >        struct dirent *dinfo = NULL;
319 >        struct stat lstatinfo;
320 >        struct pst_diskinfo pstat_diskinfo[DISK_BATCH];
321 >        char fullpathbuf[1024] = {0};
322 >        dev_t diskid;
323 >        DIR *dh = NULL;
324 >        int diskidx = 0;
325 >        int num, i;
326 > #endif
327   #ifdef SOLARIS
328 <        kstat_ctl_t *kc;
329 <        kstat_t *ksp;
328 >        kstat_ctl_t *kc;
329 >        kstat_t *ksp;
330          kstat_io_t kios;
331   #endif
332   #ifdef LINUX
333          FILE *f;
334          char *line_ptr;
335          int major, minor;
470        char dev_letter;
336          int has_pp_stats = 1;
337 <        static partition *parts = NULL;
473 <        static int alloc_parts = 0;
337 >        VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL);
338          int i, n;
339          time_t now;
340 +        const char *format;
341   #endif
342 < #ifdef FREEBSD
342 > #if defined(FREEBSD) || defined(DFBSD)
343          static struct statinfo stats;
344          static int stats_init = 0;
345          int counter;
# Line 483 | Line 348 | diskio_stat_t *get_diskio_stats(int *entries){
348          long sel_gen;
349          struct devstat *dev_ptr;
350   #endif
351 + #ifdef NETBSD
352 +        struct disk_sysctl *stats;
353 + #endif
354 + #ifdef OPENBSD
355 +        int diskcount;
356 +        char *disknames, *name, *bufpp;
357 +        char **dk_name;
358 +        struct diskstats *stats;
359 + #endif
360 + #ifdef NETBSD
361 + #define MIBSIZE 3
362 + #endif
363 + #ifdef OPENBSD
364 + #define MIBSIZE 2
365 + #endif
366 + #if defined(NETBSD) || defined(OPENBSD)
367 +        int num_disks, i;
368 +        int mib[MIBSIZE];
369 +        size_t size;
370 + #endif
371 +
372          num_diskio=0;
373  
374 < #ifdef FREEBSD
374 > #ifdef HPUX
375 >        while (1) {
376 >                num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
377 >                                    DISK_BATCH, diskidx);
378 >                if (num == -1) {
379 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
380 >                                                "pstat_getdisk");
381 >                        return NULL;
382 >                } else if (num == 0) {
383 >                        break;
384 >                }
385 >
386 >                for (i = 0; i < num; i++) {
387 >                        struct pst_diskinfo *di = &pstat_diskinfo[i];
388 >
389 >                        /* Skip "disabled" disks. */
390 >                        if (di->psd_status == 0) {
391 >                                continue;
392 >                        }
393 >        
394 >                        /* We can't seperate the reads from the writes, we'll
395 >                           just give the same to each. */
396 >                        rbytes = wbytes = di->psd_dkwds / 2;
397 >        
398 >                        /* Skip unused disks. */
399 >                        if (rbytes == 0 && wbytes == 0) {
400 >                                continue;
401 >                        }
402 >        
403 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
404 >                                return NULL;
405 >                        }
406 >        
407 >                        diskio_stats_ptr = diskio_stats + num_diskio;
408 >        
409 >                        diskio_stats_ptr->read_bytes = rbytes;
410 >                        diskio_stats_ptr->write_bytes = wbytes;
411 >        
412 >                        diskio_stats_ptr->systime = time(NULL);
413 >        
414 >                        num_diskio++;
415 >        
416 >                        if (diskio_stats_ptr->disk_name == NULL) {
417 >                                dh = opendir("/dev/dsk");
418 >                                if (dh == NULL) {
419 >                                        continue;
420 >                                }
421 >        
422 >                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
423 >                                while (1) {
424 >                                        dinfo = readdir(dh);
425 >                                        if (dinfo == NULL) {
426 >                                                break;
427 >                                        }
428 >                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
429 >                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
430 >                                                continue;
431 >                                        }
432 >        
433 >                                        if (lstatinfo.st_rdev == diskid) {
434 >                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
435 >                                                        return NULL;
436 >                                                }
437 >                                                break;
438 >                                        }
439 >                                }
440 >                                closedir(dh);
441 >        
442 >                                if (diskio_stats_ptr->disk_name == NULL) {
443 >                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
444 >                                                return NULL;
445 >                                        }
446 >                                }
447 >                        }
448 >                }
449 >                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
450 >        }
451 > #endif
452 > #ifdef OPENBSD
453 >        mib[0] = CTL_HW;
454 >        mib[1] = HW_DISKCOUNT;
455 >
456 >        size = sizeof(diskcount);
457 >        if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
458 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
459 >                return NULL;
460 >        }
461 >
462 >        mib[0] = CTL_HW;
463 >        mib[1] = HW_DISKNAMES;
464 >
465 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
466 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
467 >                return NULL;
468 >        }
469 >
470 >        disknames = sg_malloc(size);
471 >        if (disknames == NULL) {
472 >                return NULL;
473 >        }
474 >
475 >        if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
476 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
477 >                return NULL;
478 >        }
479 >
480 >        dk_name = sg_malloc(diskcount * sizeof(char *));
481 >        bufpp = disknames;
482 >        for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
483 >                dk_name[i] = name;
484 >        }
485 > #endif
486 >
487 > #if defined(NETBSD) || defined(OPENBSD)
488 >        mib[0] = CTL_HW;
489 >        mib[1] = HW_DISKSTATS;
490 > #ifdef NETBSD
491 >        mib[2] = sizeof(struct disk_sysctl);
492 > #endif
493 >
494 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
495 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
496 >                return NULL;
497 >        }
498 >
499 > #ifdef NETBSD
500 >        num_disks = size / sizeof(struct disk_sysctl);
501 > #else
502 >        num_disks = size / sizeof(struct diskstats);
503 > #endif
504 >
505 >        stats = sg_malloc(size);
506 >        if (stats == NULL) {
507 >                return NULL;
508 >        }
509 >
510 >        if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
511 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
512 >                return NULL;
513 >        }
514 >
515 >        for (i = 0; i < num_disks; i++) {
516 >                const char *name;
517 >                u_int64_t rbytes, wbytes;
518 >
519 > #ifdef NETBSD
520 > #ifdef HAVE_DK_RBYTES
521 >                rbytes = stats[i].dk_rbytes;
522 >                wbytes = stats[i].dk_wbytes;
523 > #else
524 >                /* Before 2.0, NetBSD merged reads and writes. */
525 >                rbytes = wbytes = stats[i].dk_bytes;
526 > #endif
527 > #else
528 > #ifdef HAVE_DS_RBYTES
529 >                rbytes = stats[i].ds_rbytes;
530 >                wbytes = stats[i].ds_wbytes;
531 > #else
532 >                /* Before 3.5, OpenBSD merged reads and writes */
533 >                rbytes = wbytes = stats[i].ds_bytes;
534 > #endif
535 > #endif
536 >
537 >                /* Don't keep stats for disks that have never been used. */
538 >                if (rbytes == 0 && wbytes == 0) {
539 >                        continue;
540 >                }
541 >
542 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
543 >                        return NULL;
544 >                }
545 >                diskio_stats_ptr = diskio_stats + num_diskio;
546 >                
547 >                diskio_stats_ptr->read_bytes = rbytes;
548 >                diskio_stats_ptr->write_bytes = wbytes;
549 > #ifdef NETBSD
550 >                name = stats[i].dk_name;
551 > #else
552 >                name = dk_name[i];
553 > #endif
554 >                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
555 >                        return NULL;
556 >                }
557 >                diskio_stats_ptr->systime = time(NULL);
558 >        
559 >                num_diskio++;  
560 >        }
561 >
562 >        free(stats);
563 > #ifdef OPENBSD
564 >        free(dk_name);
565 >        free(disknames);
566 > #endif
567 > #endif
568 >
569 > #if defined(FREEBSD) || defined(DFBSD)
570          if (!stats_init) {
571 <                stats.dinfo=malloc(sizeof(struct devinfo));
571 >                stats.dinfo=sg_malloc(sizeof(struct devinfo));
572                  if(stats.dinfo==NULL) return NULL;
573 +                bzero(stats.dinfo, sizeof(struct devinfo));
574                  stats_init = 1;
575          }
576 <        if ((getdevs(&stats)) < 0) return NULL;
576 > #ifdef FREEBSD5
577 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
578 >                /* FIXME devstat functions return a string error in
579 >                   devstat_errbuf */
580 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
581 >                return NULL;
582 >        }
583          /* Not aware of a get all devices, so i said 999. If we ever
584           * find a machine with more than 999 disks, then i'll change
585           * this number :)
586           */
587 <        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;
587 >        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) {
588 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
589 >                return NULL;
590 >        }
591 > #else
592 >        if ((getdevs(&stats)) < 0) {
593 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
594 >                return NULL;
595 >        }
596 >        /* Not aware of a get all devices, so i said 999. If we ever
597 >         * find a machine with more than 999 disks, then i'll change
598 >         * this number :)
599 >         */
600 >        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) {
601 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
602 >                return NULL;
603 >        }
604 > #endif
605  
606          for(counter=0;counter<stats.dinfo->numdevs;counter++){
607                  dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
# Line 505 | Line 610 | diskio_stat_t *get_diskio_stats(int *entries){
610                   * devices.. like mem, proc.. and also doesn't report floppy
611                   * drives etc unless they are doing stuff :)
612                   */
613 + #ifdef FREEBSD5
614 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
615 + #else
616                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
617 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
617 > #endif
618 >
619 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
620                          return NULL;
621                  }
622                  diskio_stats_ptr=diskio_stats+num_diskio;
623 <                
623 >
624 > #ifdef FREEBSD5        
625 >                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
626 >                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
627 > #else
628                  diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
629                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
630 + #endif
631                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
632 <                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
632 >                if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
633 >                        sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL);
634 >                        return NULL;
635 >                }
636                  diskio_stats_ptr->systime=time(NULL);
637  
638                  num_diskio++;
639          }
640          free(dev_sel);
523        free(stats.dinfo);
641  
642   #endif
643   #ifdef SOLARIS
644          if ((kc = kstat_open()) == NULL) {
645 <                return NULL;
646 <        }
645 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
646 >                return NULL;
647 >        }
648  
649          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
650 <                if (!strcmp(ksp->ks_class, "disk")) {
650 >                if (!strcmp(ksp->ks_class, "disk")) {
651  
652                          if(ksp->ks_type != KSTAT_TYPE_IO) continue;
653                          /* We dont want metadevices appearins as num_diskio */
654                          if(strcmp(ksp->ks_module, "md")==0) continue;
655 <                        if((kstat_read(kc, ksp, &kios))==-1){  
655 >                        if((kstat_read(kc, ksp, &kios))==-1){  
656                          }
657                          
658 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
658 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
659                                  kstat_close(kc);
660                                  return NULL;
661                          }
662                          diskio_stats_ptr=diskio_stats+num_diskio;
663                          
664                          diskio_stats_ptr->read_bytes=kios.nread;
547                        
665                          diskio_stats_ptr->write_bytes=kios.nwritten;
666 <
667 <                        if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
668 <
669 <                        diskio_stats_ptr->disk_name=drive_map(ksp->ks_name);
666 >                        if (sg_update_string(&diskio_stats_ptr->disk_name,
667 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
668 >                                return NULL;
669 >                        }
670                          diskio_stats_ptr->systime=time(NULL);
671 +
672                          num_diskio++;
673                  }
674          }
# Line 563 | Line 681 | diskio_stat_t *get_diskio_stats(int *entries){
681          n = 0;
682  
683          /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
684 <           have statistics in here too, so we can use those directly. */
684 >           have statistics in here too, so we can use those directly.
685 >           2.6 kernels have /proc/diskstats instead with almost (but not quite)
686 >           the same format. */
687  
688 <        f = fopen("/proc/partitions", "r");
688 >        f = fopen("/proc/diskstats", "r");
689 >        format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld";
690 >        if (f == NULL) {
691 >                f = fopen("/proc/partitions", "r");
692 >                format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld";
693 >        }
694          if (f == NULL) goto out;
695          now = time(NULL);
696  
697 <        while ((line_ptr = f_read_line(f, "")) != NULL) {
698 <                char name[20];
697 >        while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
698 >                char name[100];
699                  char *s;
700                  long long rsect, wsect;
701  
702 <                int nr = sscanf(line_ptr,
578 <                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
702 >                int nr = sscanf(line_ptr, format,
703                          &major, &minor, name, &rsect, &wsect);
704                  if (nr < 3) continue;
581                if (nr < 5) {
582                        has_pp_stats = 0;
583                        rsect = 0;
584                        wsect = 0;
585                }
705  
706                  /* Skip device names ending in numbers, since they're
707                     partitions. */
# Line 591 | Line 710 | diskio_stat_t *get_diskio_stats(int *entries){
710                  --s;
711                  if (*s >= '0' && *s <= '9') continue;
712  
713 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
714 <                        diskio_stats);
715 <                if (diskio_stats == NULL) goto out;
716 <                if (n >= alloc_parts) {
598 <                        alloc_parts += 16;
599 <                        parts = realloc(parts, alloc_parts * sizeof *parts);
600 <                        if (parts == NULL) {
601 <                                alloc_parts = 0;
602 <                                goto out;
603 <                        }
713 >                if (nr < 5) {
714 >                        has_pp_stats = 0;
715 >                        rsect = 0;
716 >                        wsect = 0;
717                  }
718  
719 <                if (diskio_stats[n].disk_name != NULL)
720 <                        free(diskio_stats[n].disk_name);
721 <                diskio_stats[n].disk_name = strdup(name);
719 >                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
720 >                        goto out;
721 >                }
722 >                if (VECTOR_RESIZE(parts, n + 1) < 0) {
723 >                        goto out;
724 >                }
725 >
726 >                if (sg_update_string(&diskio_stats[n].disk_name, name) < 0) {
727 >                        goto out;
728 >                }
729                  diskio_stats[n].read_bytes = rsect * 512;
730                  diskio_stats[n].write_bytes = wsect * 512;
731                  diskio_stats[n].systime = now;
# Line 615 | Line 735 | diskio_stat_t *get_diskio_stats(int *entries){
735                  n++;
736          }
737  
738 +        fclose(f);
739 +        f = NULL;
740 +
741          if (!has_pp_stats) {
742 <                /* This is an older kernel without stats in /proc/partitions.
743 <                   Read what we can from /proc/stat instead. */
742 >                /* This is an older kernel where /proc/partitions doesn't
743 >                   contain stats. Read what we can from /proc/stat instead, and
744 >                   fill in the appropriate bits of the list allocated above. */
745  
746                  f = fopen("/proc/stat", "r");
747                  if (f == NULL) goto out;
748                  now = time(NULL);
749          
750 <                line_ptr = f_read_line(f, "disk_io:");
750 >                line_ptr = sg_f_read_line(f, "disk_io:");
751                  if (line_ptr == NULL) goto out;
752          
753                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
# Line 684 | Line 808 | diskio_stat_t *get_diskio_stats(int *entries){
808          num_diskio = n;
809   out:
810          if (f != NULL) fclose(f);
811 + #endif
812  
813 + #ifdef CYGWIN
814 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
815 +        return NULL;
816   #endif
817 +
818          *entries=num_diskio;
819  
820          return diskio_stats;
821   }
822  
823 < diskio_stat_t *get_diskio_stats_diff(int *entries){
824 <        static diskio_stat_t *diskio_stats_diff=NULL;
825 <        static int sizeof_diskio_stats_diff=0;
826 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
827 <        int disks, x, y;
823 > sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
824 >        VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
825 >                              diskio_stat_init, diskio_stat_destroy);
826 >        sg_disk_io_stats *src = NULL, *dest;
827 >        int i, j, diff_count, new_count;
828  
829 <        if(diskio_stats==NULL){
830 <                diskio_stats_ptr=get_diskio_stats(&disks);
831 <                *entries=disks;
703 <                return diskio_stats_ptr;
829 >        if (diskio_stats == NULL) {
830 >                /* No previous stats, so we can't calculate a difference. */
831 >                return sg_get_disk_io_stats(entries);
832          }
833  
834 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
835 <        if(diskio_stats_diff==NULL){
834 >        /* Resize the results array to match the previous stats. */
835 >        diff_count = VECTOR_SIZE(diskio_stats);
836 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
837                  return NULL;
838          }
839  
840 <        diskio_stats_diff_ptr=diskio_stats_diff;
841 <        diskio_stats_ptr=diskio_stats;
840 >        /* Copy the previous stats into the result. */
841 >        for (i = 0; i < diff_count; i++) {
842 >                src = &diskio_stats[i];
843 >                dest = &diff[i];
844  
845 <        for(disks=0;disks<num_diskio;disks++){
846 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
716 <                        free(diskio_stats_diff_ptr->disk_name);
845 >                if (sg_update_string(&dest->disk_name, src->disk_name) < 0) {
846 >                        return NULL;
847                  }
848 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
849 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
850 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
851 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
848 >                dest->read_bytes = src->read_bytes;
849 >                dest->write_bytes = src->write_bytes;
850 >                dest->systime = src->systime;
851 >        }
852  
853 <                diskio_stats_diff_ptr++;
854 <                diskio_stats_ptr++;
853 >        /* Get a new set of stats. */
854 >        if (sg_get_disk_io_stats(&new_count) == NULL) {
855 >                return NULL;
856          }
857  
858 <        diskio_stats_ptr=get_diskio_stats(&disks);
859 <        diskio_stats_diff_ptr=diskio_stats_diff;
858 >        /* For each previous stat... */
859 >        for (i = 0; i < diff_count; i++) {
860 >                dest = &diff[i];
861  
862 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
863 <
864 <                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
865 <                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
866 <                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
867 <                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
868 <                }else{
737 <                        diskio_stats_ptr=diskio_stats;
738 <                        for(y=0;y<disks;y++){
739 <                                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
740 <                                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
741 <                                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
742 <                                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
743 <
744 <                                        break;
745 <                                }
746 <                                
747 <                                diskio_stats_ptr++;
862 >                /* ... find the corresponding new stat ... */
863 >                for (j = 0; j < new_count; j++) {
864 >                        /* Try the new stat in the same position first,
865 >                           since that's most likely to be it. */
866 >                        src = &diskio_stats[(i + j) % new_count];
867 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
868 >                                break;
869                          }
870                  }
871 +                if (j == new_count) {
872 +                        /* No match found. */
873 +                        continue;
874 +                }
875  
876 <                diskio_stats_ptr++;
877 <                diskio_stats_diff_ptr++;        
878 <
876 >                /* ... and subtract the previous stat from it to get the
877 >                   difference. */
878 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
879 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
880 >                dest->systime = src->systime - dest->systime;
881          }
882 <        
883 <        *entries=sizeof_diskio_stats_diff;
884 <        return diskio_stats_diff;
882 >
883 >        *entries = diff_count;
884 >        return diff;
885   }
886 +
887 + int sg_disk_io_compare_name(const void *va, const void *vb) {
888 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
889 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
890 +
891 +        return strcmp(a->disk_name, b->disk_name);
892 + }
893 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines