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.31 by ats, Sun Oct 19 02:03:02 2003 UTC vs.
Revision 1.82 by tdb, Wed Jul 13 13:01:24 2005 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 < #define VALID_FS_TYPES {"ufs", "tmpfs"}
40 > #define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs"}
41   #endif
42  
43 < #ifdef LINUX
38 < #include <time.h>
39 < #include <sys/vfs.h>
43 > #if defined(LINUX) || defined(CYGWIN)
44   #include <mntent.h>
45 < #include "tools.h"
46 < #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
45 > #include <sys/vfs.h>
46 > #include <sys/statvfs.h>
47   #endif
48  
49 + #ifdef LINUX
50 + #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
51 +                        "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
52 +                        "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
53 +                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
54 +                        "umsdos", "vfat", "xfs", "jfs", "nfs"}
55 + #endif
56 +
57 + #ifdef CYGWIN
58 + #define VALID_FS_TYPES {"user"}
59 + #endif
60 +
61   #ifdef ALLBSD
62   #include <sys/param.h>
63   #include <sys/ucred.h>
64   #include <sys/mount.h>
49 #define VALID_FS_TYPES {"ufs", "mfs", "ffs"}
65   #endif
66 < #ifdef FREEBSD
66 > #if defined(FREEBSD) || defined(DFBSD)
67   #include <sys/dkstat.h>
68   #include <devstat.h>
69 + #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
70 +                        "ufs", "mfs", "nfs"}
71   #endif
72 + #if defined(NETBSD) || defined(OPENBSD)
73 + #include <sys/param.h>
74 + #include <sys/sysctl.h>
75 + #include <sys/disk.h>
76 + #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
77 +                        "ntfs", "nfs"}
78 + #endif
79  
80 < #define START_VAL 1
80 > #ifdef HPUX
81 > #include <sys/param.h>
82 > #include <sys/pstat.h>
83 > #include <sys/types.h>
84 > #include <sys/stat.h>
85 > #include <sys/vfs.h>
86 > #include <mntent.h>
87 > #include <dirent.h>
88 > #include <stdio.h>
89 > #include <time.h>
90 > #define VALID_FS_TYPES {"vxfs", "hfs"}
91 > #define DISK_BATCH 30
92 > #endif
93  
94 < char *copy_string(char *orig_ptr, const char *newtext){
94 > #ifdef ALLBSD
95 > #define SG_MP_FSTYPENAME(mp) (mp)->f_fstypename
96 > #define SG_MP_DEVNAME(mp)    (mp)->f_mntfromname
97 > #define SG_MP_MOUNTP(mp)     (mp)->f_mntonname
98 > #define SG_FS_FRSIZE(fs)     (long long) (*(fs))->f_bsize
99 > #define SG_FS_BSIZE(fs)      (long long) (*(fs))->f_iosize
100 > #define SG_FS_BLOCKS(fs)     (long long) (*(fs))->f_blocks
101 > #define SG_FS_BFREE(fs)      (long long) (*(fs))->f_bfree
102 > #define SG_FS_BAVAIL(fs)     (long long) (*(fs))->f_bavail
103 > #define SG_FS_FILES(fs)      (long long) (*(fs))->f_files
104 > #define SG_FS_FFREE(fs)      (long long) (*(fs))->f_ffree
105 > #define SG_FS_FAVAIL(fs)     -1LL
106 > #endif
107 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
108 > #define SG_MP_FSTYPENAME(mp) (mp)->mnt_type
109 > #define SG_MP_DEVNAME(mp)    (mp)->mnt_fsname
110 > #define SG_MP_MOUNTP(mp)     (mp)->mnt_dir
111 > #define SG_FS_FRSIZE(fs)     (long long) (fs).f_frsize
112 > #define SG_FS_BSIZE(fs)      (long long) (fs).f_bsize
113 > #define SG_FS_BLOCKS(fs)     (long long) (fs).f_blocks
114 > #define SG_FS_BFREE(fs)      (long long) (fs).f_bfree
115 > #define SG_FS_BAVAIL(fs)     (long long) (fs).f_bavail
116 > #define SG_FS_FILES(fs)      (long long) (fs).f_files
117 > #define SG_FS_FFREE(fs)      (long long) (fs).f_ffree
118 > #define SG_FS_FAVAIL(fs)     (long long) (fs).f_favail
119 > #endif
120 > #ifdef SOLARIS
121 > #define SG_MP_FSTYPENAME(mp) (mp).mnt_fstype
122 > #define SG_MP_DEVNAME(mp)    (mp).mnt_special
123 > #define SG_MP_MOUNTP(mp)     (mp).mnt_mountp
124 > #define SG_FS_FRSIZE(fs)     (long long) (fs).f_frsize
125 > #define SG_FS_BSIZE(fs)      (long long) (fs).f_bsize
126 > #define SG_FS_BLOCKS(fs)     (long long) (fs).f_blocks
127 > #define SG_FS_BFREE(fs)      (long long) (fs).f_bfree
128 > #define SG_FS_BAVAIL(fs)     (long long) (fs).f_bavail
129 > #define SG_FS_FILES(fs)      (long long) (fs).f_files
130 > #define SG_FS_FFREE(fs)      (long long) (fs).f_ffree
131 > #define SG_FS_FAVAIL(fs)     (long long) (fs).f_favail
132 > #endif
133  
134 <        /* Maybe free if not NULL, and strdup rather than realloc and strcpy? */
135 <        orig_ptr=realloc(orig_ptr, (1+strlen(newtext)));
136 <        if(orig_ptr==NULL){
137 <                return NULL;
138 <        }
65 <        strcpy(orig_ptr, newtext);
134 > static void disk_stat_init(sg_fs_stats *d) {
135 >        d->device_name = NULL;
136 >        d->fs_type = NULL;
137 >        d->mnt_point = NULL;
138 > }
139  
140 <        return orig_ptr;
140 > static void disk_stat_destroy(sg_fs_stats *d) {
141 >        free(d->device_name);
142 >        free(d->fs_type);
143 >        free(d->mnt_point);
144   }
145  
146 + static int is_valid_fs_type(const char *type) {
147 +        const char *types[] = VALID_FS_TYPES;
148 +        int i;
149  
150 < void init_disk_stat(int start, int end, disk_stat_t *disk_stats){
151 <
152 <        for(disk_stats+=start; start<=end; start++){
153 <                disk_stats->device_name=NULL;
75 <                disk_stats->fs_type=NULL;
76 <                disk_stats->mnt_point=NULL;
77 <                
78 <                disk_stats++;
150 >        for (i = 0; i < (int) (sizeof types / sizeof *types); i++) {
151 >                if (strcmp(types[i], type) == 0) {
152 >                        return 1;
153 >                }
154          }
155 +        return 0;
156   }
157  
158 < disk_stat_t *get_disk_stats(int *entries){
158 > sg_fs_stats *sg_get_fs_stats(int *entries){
159 >        VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
160 >                              disk_stat_init, disk_stat_destroy);
161  
84        static disk_stat_t *disk_stats;
85        static int watermark=-1;
86
87        char *fs_types[] = VALID_FS_TYPES;
88        int x, valid_type;
162          int num_disks=0;
163 < #if defined(LINUX) || defined (SOLARIS)
163 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN) || defined(HPUX)
164          FILE *f;
165   #endif
166  
167 <        disk_stat_t *disk_ptr;
167 >        sg_fs_stats *disk_ptr;
168  
169   #ifdef SOLARIS
170          struct mnttab mp;
171          struct statvfs fs;
172   #endif
173 < #ifdef LINUX
173 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
174          struct mntent *mp;
175 <        struct statfs fs;
175 >        struct statvfs fs;
176   #endif
177   #ifdef ALLBSD
178          int nummnt;
179 <        struct statfs *mp;
179 > #ifdef HAVE_STATVFS
180 >        struct statvfs *mp, **fs;
181 > #else
182 >        struct statfs *mp, **fs;
183   #endif
184 + #endif
185  
109        if(watermark==-1){
110                disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
111                if(disk_stats==NULL){
112                        return NULL;
113                }
114                watermark=START_VAL;
115                init_disk_stat(0, watermark-1, disk_stats);
116        }
186   #ifdef ALLBSD
187 <        nummnt=getmntinfo(&mp , MNT_LOCAL);
187 >        nummnt=getmntinfo(&mp, MNT_WAIT);
188          if (nummnt<=0){
189 +                sg_set_error_with_errno(SG_ERROR_GETMNTINFO, NULL);
190                  return NULL;
191          }
192 <        for(;nummnt--; mp++){
123 <                valid_type=0;
124 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
125 <                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
126 <                                valid_type=1;
127 <                                break;
128 <                        }
129 <                }
192 >        for(fs = &mp; nummnt--; (*fs)++){
193   #endif
194  
195 < #ifdef LINUX
195 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
196 > #ifdef MNT_MNTTAB
197 >        if ((f=setmntent(MNT_MNTTAB, "r" ))==NULL){
198 > #else
199          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
200 + #endif
201 +                sg_set_error(SG_ERROR_SETMNTENT, NULL);
202                  return NULL;
203          }
204  
205          while((mp=getmntent(f))){
206 <                if((statfs(mp->mnt_dir, &fs)) !=0){
206 >                if((statvfs(mp->mnt_dir, &fs)) !=0){
207                          continue;
208                  }      
209  
142                valid_type=0;
143                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
144                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
145                                valid_type=1;
146                                break;
147                        }
148                }
210   #endif
211  
212   #ifdef SOLARIS
213          if ((f=fopen("/etc/mnttab", "r" ))==NULL){
214 +                sg_set_error_with_errno(SG_ERROR_OPEN, "/etc/mnttab");
215                  return NULL;
216          }
217          while((getmntent(f, &mp)) == 0){
218                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
219                          continue;
220                  }
159                valid_type=0;
160                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
161                        if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
162                                valid_type=1;
163                                break;
164                        }
165                }
221   #endif
222  
223 <                if(valid_type){
224 <                        if(num_disks>watermark-1){
170 <                                disk_ptr=disk_stats;
171 <                                if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
172 <                                        disk_stats=disk_ptr;
173 <                                        return NULL;
174 <                                }
175 <
176 <                                watermark=watermark*2;
177 <                                init_disk_stat(num_disks, watermark-1, disk_stats);
178 <                        }
179 <
180 <                        disk_ptr=disk_stats+num_disks;
181 < #ifdef ALLBSD
182 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
223 >                if(is_valid_fs_type(SG_MP_FSTYPENAME(mp))){
224 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
225                                  return NULL;
226                          }
227 +                        disk_ptr=disk_stats+num_disks;
228  
229 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
229 >                        /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
230 >                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
231 >                         * be upwards of a k each
232 >                         */
233 >                        if (sg_update_string(&disk_ptr->device_name, SG_MP_DEVNAME(mp)) < 0) {
234                                  return NULL;
235                          }
236 <
190 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
236 >                        if (sg_update_string(&disk_ptr->fs_type, SG_MP_FSTYPENAME(mp)) < 0) {
237                                  return NULL;
238                          }
239 <
194 <                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
195 <                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
196 <                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
197 <
198 <                        disk_ptr->total_inodes=(long long)mp->f_files;
199 <                        disk_ptr->free_inodes=(long long)mp->f_ffree;
200 <                        /* Freebsd doesn't have a "available" inodes */
201 <                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
202 < #endif
203 < #ifdef LINUX
204 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
239 >                        if (sg_update_string(&disk_ptr->mnt_point, SG_MP_MOUNTP(mp)) < 0) {
240                                  return NULL;
241                          }
207                                
208                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
209                                return NULL;
210                        }
242  
243 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
244 <                                return NULL;
245 <                        }
215 <                        disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
216 <                        disk_ptr->avail = (long long)fs.f_bsize * (long long)fs.f_bavail;
217 <                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_bsize * (long long)fs.f_bfree);
243 >                        disk_ptr->size  = SG_FS_FRSIZE(fs) * SG_FS_BLOCKS(fs);
244 >                        disk_ptr->avail = SG_FS_FRSIZE(fs) * SG_FS_BAVAIL(fs);
245 >                        disk_ptr->used  = (disk_ptr->size) - (SG_FS_FRSIZE(fs) * SG_FS_BFREE(fs));
246  
247 <                        disk_ptr->total_inodes=(long long)fs.f_files;
248 <                        disk_ptr->free_inodes=(long long)fs.f_ffree;
249 <                        /* Linux doesn't have a "available" inodes */
250 <                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
251 < #endif
247 >                        disk_ptr->total_inodes = SG_FS_FILES(fs);
248 >                        disk_ptr->free_inodes  = SG_FS_FFREE(fs);
249 >                        /* Linux, FreeBSD don't have a "available" inodes */
250 >                        disk_ptr->used_inodes  = disk_ptr->total_inodes - disk_ptr->free_inodes;
251 >                        disk_ptr->avail_inodes = SG_FS_FAVAIL(fs);
252  
253 < #ifdef SOLARIS
254 <                        /* Memory leak in event of realloc failing */
255 <                        /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
256 <                         * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
257 <                         * be upwards of a k each
258 <                         */
231 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
232 <                                return NULL;
233 <                        }
253 >                        disk_ptr->io_size      = SG_FS_BSIZE(fs);
254 >                        disk_ptr->block_size   = SG_FS_FRSIZE(fs);
255 >                        disk_ptr->total_blocks = SG_FS_BLOCKS(fs);
256 >                        disk_ptr->free_blocks  = SG_FS_BFREE(fs);
257 >                        disk_ptr->avail_blocks = SG_FS_BAVAIL(fs);
258 >                        disk_ptr->used_blocks  = disk_ptr->total_blocks - disk_ptr->free_blocks;
259  
235                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
236                                return NULL;
237                        }
238        
239                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
240                                return NULL;
241                        }
242                        
243                        disk_ptr->size = (long long)fs.f_frsize * (long long)fs.f_blocks;
244                        disk_ptr->avail = (long long)fs.f_frsize * (long long)fs.f_bavail;
245                        disk_ptr->used = (disk_ptr->size) - ((long long)fs.f_frsize * (long long)fs.f_bfree);
246                
247                        disk_ptr->total_inodes=(long long)fs.f_files;
248                        disk_ptr->used_inodes=disk_ptr->total_inodes - (long long)fs.f_ffree;
249                        disk_ptr->free_inodes=(long long)fs.f_favail;
250 #endif
260                          num_disks++;
261                  }
262          }
263  
264          *entries=num_disks;    
265  
266 <        /* If this fails, there is very little i can do about it, so i'll ignore it :) */
267 < #if defined(LINUX) || defined(SOLARIS)
266 >        /* If this fails, there is very little i can do about it, so
267 >           I'll ignore it :) */
268 > #if defined(LINUX) || defined(CYGWIN) || defined(HPUX)
269 >        endmntent(f);
270 > #endif
271 > #if defined(SOLARIS)
272          fclose(f);
273   #endif
274  
275          return disk_stats;
276  
277   }
265 void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
278  
279 <        for(diskio_stats+=start; start<end; start++){
280 <                diskio_stats->disk_name=NULL;
281 <                
282 <                diskio_stats++;
283 <        }
279 > int sg_fs_compare_device_name(const void *va, const void *vb) {
280 >        const sg_fs_stats *a = (const sg_fs_stats *)va;
281 >        const sg_fs_stats *b = (const sg_fs_stats *)vb;
282 >
283 >        return strcmp(a->device_name, b->device_name);
284   }
285  
286 < diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){
286 > int sg_fs_compare_mnt_point(const void *va, const void *vb) {
287 >        const sg_fs_stats *a = (const sg_fs_stats *)va;
288 >        const sg_fs_stats *b = (const sg_fs_stats *)vb;
289  
290 <        if(diskio_stats==NULL){
290 >        return strcmp(a->mnt_point, b->mnt_point);
291 > }
292  
293 <                if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){
294 <                        return NULL;
295 <                }
281 <                diskio_stat_init(0, needed_entries, diskio_stats);
282 <                *cur_entries=needed_entries;
293 > static void diskio_stat_init(sg_disk_io_stats *d) {
294 >        d->disk_name = NULL;
295 > }
296  
297 <                return diskio_stats;
298 <        }
286 <
287 <
288 <        if(*cur_entries<needed_entries){
289 <                diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries));
290 <                if(diskio_stats==NULL){
291 <                        return NULL;
292 <                }
293 <                diskio_stat_init(*cur_entries, needed_entries, diskio_stats);
294 <                *cur_entries=needed_entries;
295 <        }
296 <
297 <        return diskio_stats;
297 > static void diskio_stat_destroy(sg_disk_io_stats *d) {
298 >        free(d->disk_name);
299   }
300  
301 < static diskio_stat_t *diskio_stats=NULL;        
302 < static int num_diskio=0;        
301 > VECTOR_DECLARE_STATIC(diskio_stats, sg_disk_io_stats, 10,
302 >                      diskio_stat_init, diskio_stat_destroy);
303  
304   #ifdef LINUX
305   typedef struct {
# Line 307 | Line 308 | typedef struct {
308   } partition;
309   #endif
310  
311 < diskio_stat_t *get_diskio_stats(int *entries){
311 > sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
312 >        int num_diskio;
313 > #ifndef LINUX
314 >        sg_disk_io_stats *diskio_stats_ptr;
315 > #endif
316  
317 <        static int sizeof_diskio_stats=0;
318 <        diskio_stat_t *diskio_stats_ptr;
319 <
317 > #ifdef HPUX
318 >        long long rbytes = 0, wbytes = 0;
319 >        struct dirent *dinfo = NULL;
320 >        struct stat lstatinfo;
321 >        struct pst_diskinfo pstat_diskinfo[DISK_BATCH];
322 >        char fullpathbuf[1024] = {0};
323 >        dev_t diskid;
324 >        DIR *dh = NULL;
325 >        int diskidx = 0;
326 >        int num, i;
327 > #endif
328   #ifdef SOLARIS
329 <        kstat_ctl_t *kc;
330 <        kstat_t *ksp;
329 >        kstat_ctl_t *kc;
330 >        kstat_t *ksp;
331          kstat_io_t kios;
332   #endif
333   #ifdef LINUX
334          FILE *f;
335          char *line_ptr;
336          int major, minor;
324        char dev_letter;
337          int has_pp_stats = 1;
338 <        static partition *parts = NULL;
327 <        static int alloc_parts = 0;
338 >        VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL);
339          int i, n;
340          time_t now;
341 +        const char *format;
342 +        static regex_t not_part_re, part_re;
343 +        static int re_compiled = 0;
344   #endif
345 < #ifdef FREEBSD
345 > #if defined(FREEBSD) || defined(DFBSD)
346          static struct statinfo stats;
347          static int stats_init = 0;
348          int counter;
# Line 338 | Line 352 | diskio_stat_t *get_diskio_stats(int *entries){
352          struct devstat *dev_ptr;
353   #endif
354   #ifdef NETBSD
355 <        /* FIXME get_diskio_stats NYI on NetBSD.
342 <         * See vmstat/dkstats.c in NetBSD source for examples.
343 <         */
355 >        struct disk_sysctl *stats;
356   #endif
357 + #ifdef OPENBSD
358 +        int diskcount;
359 +        char *disknames, *name, *bufpp;
360 +        char **dk_name;
361 +        struct diskstats *stats;
362 + #endif
363 + #ifdef NETBSD
364 + #define MIBSIZE 3
365 + #endif
366 + #ifdef OPENBSD
367 + #define MIBSIZE 2
368 + #endif
369 + #if defined(NETBSD) || defined(OPENBSD)
370 +        int num_disks, i;
371 +        int mib[MIBSIZE];
372 +        size_t size;
373 + #endif
374 +
375          num_diskio=0;
376  
377 < #ifdef FREEBSD
377 > #ifdef HPUX
378 >        while (1) {
379 >                num = pstat_getdisk(pstat_diskinfo, sizeof pstat_diskinfo[0],
380 >                                    DISK_BATCH, diskidx);
381 >                if (num == -1) {
382 >                        sg_set_error_with_errno(SG_ERROR_PSTAT,
383 >                                                "pstat_getdisk");
384 >                        return NULL;
385 >                } else if (num == 0) {
386 >                        break;
387 >                }
388 >
389 >                for (i = 0; i < num; i++) {
390 >                        struct pst_diskinfo *di = &pstat_diskinfo[i];
391 >
392 >                        /* Skip "disabled" disks. */
393 >                        if (di->psd_status == 0) {
394 >                                continue;
395 >                        }
396 >        
397 >                        /* We can't seperate the reads from the writes, we'll
398 >                         * just give the same to each. (This value is in
399 >                         * 64-byte chunks according to the pstat header file,
400 >                         * and can wrap to be negative.)
401 >                         */
402 >                        rbytes = wbytes = ((unsigned long) di->psd_dkwds) * 64LL;
403 >        
404 >                        /* Skip unused disks. */
405 >                        if (rbytes == 0 && wbytes == 0) {
406 >                                continue;
407 >                        }
408 >        
409 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
410 >                                return NULL;
411 >                        }
412 >        
413 >                        diskio_stats_ptr = diskio_stats + num_diskio;
414 >        
415 >                        diskio_stats_ptr->read_bytes = rbytes;
416 >                        diskio_stats_ptr->write_bytes = wbytes;
417 >        
418 >                        diskio_stats_ptr->systime = time(NULL);
419 >        
420 >                        num_diskio++;
421 >        
422 >                        /* FIXME This should use a static cache, like the Linux
423 >                         * code below. */
424 >                        if (diskio_stats_ptr->disk_name == NULL) {
425 >                                dh = opendir("/dev/dsk");
426 >                                if (dh == NULL) {
427 >                                        continue;
428 >                                }
429 >        
430 >                                diskid = (di->psd_dev.psd_major << 24) | di->psd_dev.psd_minor;
431 >                                while (1) {
432 >                                        dinfo = readdir(dh);
433 >                                        if (dinfo == NULL) {
434 >                                                break;
435 >                                        }
436 >                                        snprintf(fullpathbuf, sizeof(fullpathbuf), "/dev/dsk/%s", dinfo->d_name);
437 >                                        if (lstat(fullpathbuf, &lstatinfo) < 0) {
438 >                                                continue;
439 >                                        }
440 >        
441 >                                        if (lstatinfo.st_rdev == diskid) {
442 >                                                if (sg_update_string(&diskio_stats_ptr->disk_name, dinfo->d_name) < 0) {
443 >                                                        return NULL;
444 >                                                }
445 >                                                break;
446 >                                        }
447 >                                }
448 >                                closedir(dh);
449 >        
450 >                                if (diskio_stats_ptr->disk_name == NULL) {
451 >                                        if (sg_update_string(&diskio_stats_ptr->disk_name, di->psd_hw_path.psh_name) < 0) {
452 >                                                return NULL;
453 >                                        }
454 >                                }
455 >                        }
456 >                }
457 >                diskidx = pstat_diskinfo[num - 1].psd_idx + 1;
458 >        }
459 > #endif
460 > #ifdef OPENBSD
461 >        mib[0] = CTL_HW;
462 >        mib[1] = HW_DISKCOUNT;
463 >
464 >        size = sizeof(diskcount);
465 >        if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
466 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKCOUNT");
467 >                return NULL;
468 >        }
469 >
470 >        mib[0] = CTL_HW;
471 >        mib[1] = HW_DISKNAMES;
472 >
473 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
474 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
475 >                return NULL;
476 >        }
477 >
478 >        disknames = sg_malloc(size);
479 >        if (disknames == NULL) {
480 >                return NULL;
481 >        }
482 >
483 >        if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
484 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKNAMES");
485 >                return NULL;
486 >        }
487 >
488 >        dk_name = sg_malloc(diskcount * sizeof(char *));
489 >        bufpp = disknames;
490 >        for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
491 >                dk_name[i] = name;
492 >        }
493 > #endif
494 >
495 > #if defined(NETBSD) || defined(OPENBSD)
496 >        mib[0] = CTL_HW;
497 >        mib[1] = HW_DISKSTATS;
498 > #ifdef NETBSD
499 >        mib[2] = sizeof(struct disk_sysctl);
500 > #endif
501 >
502 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
503 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
504 >                return NULL;
505 >        }
506 >
507 > #ifdef NETBSD
508 >        num_disks = size / sizeof(struct disk_sysctl);
509 > #else
510 >        num_disks = size / sizeof(struct diskstats);
511 > #endif
512 >
513 >        stats = sg_malloc(size);
514 >        if (stats == NULL) {
515 >                return NULL;
516 >        }
517 >
518 >        if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
519 >                sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_HW.HW_DISKSTATS");
520 >                return NULL;
521 >        }
522 >
523 >        for (i = 0; i < num_disks; i++) {
524 >                const char *name;
525 >                u_int64_t rbytes, wbytes;
526 >
527 > #ifdef NETBSD
528 > #ifdef HAVE_DK_RBYTES
529 >                rbytes = stats[i].dk_rbytes;
530 >                wbytes = stats[i].dk_wbytes;
531 > #else
532 >                /* Before 2.0, NetBSD merged reads and writes. */
533 >                rbytes = wbytes = stats[i].dk_bytes;
534 > #endif
535 > #else
536 > #ifdef HAVE_DS_RBYTES
537 >                rbytes = stats[i].ds_rbytes;
538 >                wbytes = stats[i].ds_wbytes;
539 > #else
540 >                /* Before 3.5, OpenBSD merged reads and writes */
541 >                rbytes = wbytes = stats[i].ds_bytes;
542 > #endif
543 > #endif
544 >
545 >                /* Don't keep stats for disks that have never been used. */
546 >                if (rbytes == 0 && wbytes == 0) {
547 >                        continue;
548 >                }
549 >
550 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
551 >                        return NULL;
552 >                }
553 >                diskio_stats_ptr = diskio_stats + num_diskio;
554 >                
555 >                diskio_stats_ptr->read_bytes = rbytes;
556 >                diskio_stats_ptr->write_bytes = wbytes;
557 > #ifdef NETBSD
558 >                name = stats[i].dk_name;
559 > #else
560 >                name = dk_name[i];
561 > #endif
562 >                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
563 >                        return NULL;
564 >                }
565 >                diskio_stats_ptr->systime = time(NULL);
566 >        
567 >                num_diskio++;  
568 >        }
569 >
570 >        free(stats);
571 > #ifdef OPENBSD
572 >        free(dk_name);
573 >        free(disknames);
574 > #endif
575 > #endif
576 >
577 > #if defined(FREEBSD) || defined(DFBSD)
578          if (!stats_init) {
579 <                stats.dinfo=malloc(sizeof(struct devinfo));
350 <                bzero(stats.dinfo, sizeof(struct devinfo));
579 >                stats.dinfo=sg_malloc(sizeof(struct devinfo));
580                  if(stats.dinfo==NULL) return NULL;
581 +                bzero(stats.dinfo, sizeof(struct devinfo));
582                  stats_init = 1;
583          }
584   #ifdef FREEBSD5
585 <        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
585 >        if ((devstat_getdevs(NULL, &stats)) < 0) {
586 >                /* FIXME devstat functions return a string error in
587 >                   devstat_errbuf */
588 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
589 >                return NULL;
590 >        }
591          /* Not aware of a get all devices, so i said 999. If we ever
592           * find a machine with more than 999 disks, then i'll change
593           * this number :)
594           */
595 <        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;
595 >        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) {
596 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
597 >                return NULL;
598 >        }
599   #else
600 <        if ((getdevs(&stats)) < 0) return NULL;
600 >        if ((getdevs(&stats)) < 0) {
601 >                sg_set_error(SG_ERROR_DEVSTAT_GETDEVS, NULL);
602 >                return NULL;
603 >        }
604          /* Not aware of a get all devices, so i said 999. If we ever
605           * find a machine with more than 999 disks, then i'll change
606           * this number :)
607           */
608 <        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;
608 >        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) {
609 >                sg_set_error(SG_ERROR_DEVSTAT_SELECTDEVS, NULL);
610 >                return NULL;
611 >        }
612   #endif
613  
614          for(counter=0;counter<stats.dinfo->numdevs;counter++){
# Line 379 | Line 623 | diskio_stat_t *get_diskio_stats(int *entries){
623   #else
624                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
625   #endif
626 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
626 >
627 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
628                          return NULL;
629                  }
630                  diskio_stats_ptr=diskio_stats+num_diskio;
# Line 392 | Line 637 | diskio_stat_t *get_diskio_stats(int *entries){
637                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
638   #endif
639                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
640 <                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
640 >                if (asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number) == -1) {
641 >                        sg_set_error_with_errno(SG_ERROR_ASPRINTF, NULL);
642 >                        return NULL;
643 >                }
644                  diskio_stats_ptr->systime=time(NULL);
645  
646                  num_diskio++;
# Line 402 | Line 650 | diskio_stat_t *get_diskio_stats(int *entries){
650   #endif
651   #ifdef SOLARIS
652          if ((kc = kstat_open()) == NULL) {
653 <                return NULL;
654 <        }
653 >                sg_set_error(SG_ERROR_KSTAT_OPEN, NULL);
654 >                return NULL;
655 >        }
656  
657          for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
658 <                if (!strcmp(ksp->ks_class, "disk")) {
658 >                if (!strcmp(ksp->ks_class, "disk")) {
659  
660                          if(ksp->ks_type != KSTAT_TYPE_IO) continue;
661                          /* We dont want metadevices appearins as num_diskio */
662                          if(strcmp(ksp->ks_module, "md")==0) continue;
663 <                        if((kstat_read(kc, ksp, &kios))==-1){  
663 >                        if((kstat_read(kc, ksp, &kios))==-1){  
664                          }
665                          
666 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
666 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
667                                  kstat_close(kc);
668                                  return NULL;
669                          }
670                          diskio_stats_ptr=diskio_stats+num_diskio;
671                          
672                          diskio_stats_ptr->read_bytes=kios.nread;
424                        
673                          diskio_stats_ptr->write_bytes=kios.nwritten;
674 <
675 <                        if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
676 <
677 <                        diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
674 >                        if (sg_update_string(&diskio_stats_ptr->disk_name,
675 >                                             sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
676 >                                return NULL;
677 >                        }
678                          diskio_stats_ptr->systime=time(NULL);
679 +
680                          num_diskio++;
681                  }
682          }
# Line 440 | Line 689 | diskio_stat_t *get_diskio_stats(int *entries){
689          n = 0;
690  
691          /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
692 <           have statistics in here too, so we can use those directly. */
692 >           have statistics in here too, so we can use those directly.
693 >           2.6 kernels have /proc/diskstats instead with almost (but not quite)
694 >           the same format. */
695  
696 <        f = fopen("/proc/partitions", "r");
696 >        f = fopen("/proc/diskstats", "r");
697 >        format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld";
698 >        if (f == NULL) {
699 >                f = fopen("/proc/partitions", "r");
700 >                format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld";
701 >        }
702          if (f == NULL) goto out;
703          now = time(NULL);
704  
705 <        while ((line_ptr = f_read_line(f, "")) != NULL) {
706 <                char name[20];
707 <                char *s;
705 >        if (!re_compiled) {
706 >                if (regcomp(&part_re, "^(.*/)?[^/]*[0-9]$", REG_EXTENDED | REG_NOSUB) != 0) {
707 >                        sg_set_error(SG_ERROR_PARSE, NULL);
708 >                        goto out;
709 >                }
710 >                if (regcomp(&not_part_re, "^(.*/)?[^/0-9]+[0-9]+d[0-9]+$", REG_EXTENDED | REG_NOSUB) != 0) {
711 >                        sg_set_error(SG_ERROR_PARSE, NULL);
712 >                        goto out;
713 >                }
714 >                re_compiled = 1;
715 >        }
716 >
717 >        while ((line_ptr = sg_f_read_line(f, "")) != NULL) {
718 >                char name[100];
719                  long long rsect, wsect;
720  
721 <                int nr = sscanf(line_ptr,
455 <                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
721 >                int nr = sscanf(line_ptr, format,
722                          &major, &minor, name, &rsect, &wsect);
723                  if (nr < 3) continue;
724 +
725 +                /* Skip device names ending in numbers, since they're
726 +                   partitions, unless they match the c0d0 pattern that some
727 +                   RAID devices use. */
728 +                /* FIXME: For 2.6+, we should probably be using sysfs to detect
729 +                   this... */
730 +                if ((regexec(&part_re, name, 0, NULL, 0) == 0)
731 +                    && (regexec(&not_part_re, name, 0, NULL, 0) != 0)) {
732 +                        continue;
733 +                }
734 +
735                  if (nr < 5) {
736                          has_pp_stats = 0;
737                          rsect = 0;
738                          wsect = 0;
739                  }
740  
741 <                /* Skip device names ending in numbers, since they're
742 <                   partitions. */
466 <                s = name;
467 <                while (*s != '\0') s++;
468 <                --s;
469 <                if (*s >= '0' && *s <= '9') continue;
470 <
471 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
472 <                        diskio_stats);
473 <                if (diskio_stats == NULL) goto out;
474 <                if (n >= alloc_parts) {
475 <                        alloc_parts += 16;
476 <                        parts = realloc(parts, alloc_parts * sizeof *parts);
477 <                        if (parts == NULL) {
478 <                                alloc_parts = 0;
479 <                                goto out;
480 <                        }
741 >                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
742 >                        goto out;
743                  }
744 +                if (VECTOR_RESIZE(parts, n + 1) < 0) {
745 +                        goto out;
746 +                }
747  
748 <                if (diskio_stats[n].disk_name != NULL)
749 <                        free(diskio_stats[n].disk_name);
750 <                diskio_stats[n].disk_name = strdup(name);
748 >                if (sg_update_string(&diskio_stats[n].disk_name, name) < 0) {
749 >                        goto out;
750 >                }
751                  diskio_stats[n].read_bytes = rsect * 512;
752                  diskio_stats[n].write_bytes = wsect * 512;
753                  diskio_stats[n].systime = now;
# Line 492 | Line 757 | diskio_stat_t *get_diskio_stats(int *entries){
757                  n++;
758          }
759  
760 +        fclose(f);
761 +        f = NULL;
762 +
763          if (!has_pp_stats) {
764 <                /* This is an older kernel without stats in /proc/partitions.
765 <                   Read what we can from /proc/stat instead. */
764 >                /* This is an older kernel where /proc/partitions doesn't
765 >                   contain stats. Read what we can from /proc/stat instead, and
766 >                   fill in the appropriate bits of the list allocated above. */
767  
768                  f = fopen("/proc/stat", "r");
769                  if (f == NULL) goto out;
770                  now = time(NULL);
771          
772 <                line_ptr = f_read_line(f, "disk_io:");
772 >                line_ptr = sg_f_read_line(f, "disk_io:");
773                  if (line_ptr == NULL) goto out;
774          
775                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
# Line 561 | Line 830 | diskio_stat_t *get_diskio_stats(int *entries){
830          num_diskio = n;
831   out:
832          if (f != NULL) fclose(f);
833 + #endif
834  
835 + #ifdef CYGWIN
836 +        sg_set_error(SG_ERROR_UNSUPPORTED, "Cygwin");
837 +        return NULL;
838   #endif
839 +
840          *entries=num_diskio;
841  
842          return diskio_stats;
843   }
844  
845 < diskio_stat_t *get_diskio_stats_diff(int *entries){
846 <        static diskio_stat_t *diskio_stats_diff=NULL;
847 <        static int sizeof_diskio_stats_diff=0;
848 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
849 <        int disks, x, y;
845 > sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
846 >        VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
847 >                              diskio_stat_init, diskio_stat_destroy);
848 >        sg_disk_io_stats *src = NULL, *dest;
849 >        int i, j, diff_count, new_count;
850  
851 <        if(diskio_stats==NULL){
852 <                diskio_stats_ptr=get_diskio_stats(&disks);
853 <                *entries=disks;
580 <                return diskio_stats_ptr;
851 >        if (diskio_stats == NULL) {
852 >                /* No previous stats, so we can't calculate a difference. */
853 >                return sg_get_disk_io_stats(entries);
854          }
855  
856 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
857 <        if(diskio_stats_diff==NULL){
856 >        /* Resize the results array to match the previous stats. */
857 >        diff_count = VECTOR_SIZE(diskio_stats);
858 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
859                  return NULL;
860          }
861  
862 <        diskio_stats_diff_ptr=diskio_stats_diff;
863 <        diskio_stats_ptr=diskio_stats;
862 >        /* Copy the previous stats into the result. */
863 >        for (i = 0; i < diff_count; i++) {
864 >                src = &diskio_stats[i];
865 >                dest = &diff[i];
866  
867 <        for(disks=0;disks<num_diskio;disks++){
868 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
593 <                        free(diskio_stats_diff_ptr->disk_name);
867 >                if (sg_update_string(&dest->disk_name, src->disk_name) < 0) {
868 >                        return NULL;
869                  }
870 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
871 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
872 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
873 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
870 >                dest->read_bytes = src->read_bytes;
871 >                dest->write_bytes = src->write_bytes;
872 >                dest->systime = src->systime;
873 >        }
874  
875 <                diskio_stats_diff_ptr++;
876 <                diskio_stats_ptr++;
875 >        /* Get a new set of stats. */
876 >        if (sg_get_disk_io_stats(&new_count) == NULL) {
877 >                return NULL;
878          }
879  
880 <        diskio_stats_ptr=get_diskio_stats(&disks);
881 <        diskio_stats_diff_ptr=diskio_stats_diff;
880 >        /* For each previous stat... */
881 >        for (i = 0; i < diff_count; i++) {
882 >                dest = &diff[i];
883  
884 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
885 <
886 <                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
887 <                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
888 <                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
889 <                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
890 <                }else{
614 <                        diskio_stats_ptr=diskio_stats;
615 <                        for(y=0;y<disks;y++){
616 <                                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
617 <                                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
618 <                                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
619 <                                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
620 <
621 <                                        break;
622 <                                }
623 <                                
624 <                                diskio_stats_ptr++;
884 >                /* ... find the corresponding new stat ... */
885 >                for (j = 0; j < new_count; j++) {
886 >                        /* Try the new stat in the same position first,
887 >                           since that's most likely to be it. */
888 >                        src = &diskio_stats[(i + j) % new_count];
889 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
890 >                                break;
891                          }
892                  }
893 +                if (j == new_count) {
894 +                        /* No match found. */
895 +                        continue;
896 +                }
897  
898 <                diskio_stats_ptr++;
899 <                diskio_stats_diff_ptr++;        
900 <
898 >                /* ... and subtract the previous stat from it to get the
899 >                   difference. */
900 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
901 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
902 >                dest->systime = src->systime - dest->systime;
903          }
904 <        
905 <        *entries=sizeof_diskio_stats_diff;
906 <        return diskio_stats_diff;
904 >
905 >        *entries = diff_count;
906 >        return diff;
907   }
908 +
909 + int sg_disk_io_compare_name(const void *va, const void *vb) {
910 +        const sg_disk_io_stats *a = (const sg_disk_io_stats *)va;
911 +        const sg_disk_io_stats *b = (const sg_disk_io_stats *)vb;
912 +
913 +        return strcmp(a->disk_name, b->disk_name);
914 + }
915 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines