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.28 by pajs, Thu Oct 9 15:22:59 2003 UTC vs.
Revision 1.58 by ats, Sun Apr 4 23:26:23 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 i-scream
5   *
6 < * This program is free software; you can redistribute it and/or
7 < * modify it under the terms of the GNU General Public License
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 > * 02111-1307 USA
20 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 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  
35   #ifdef SOLARIS
36   #include <sys/mnttab.h>
# Line 34 | Line 39
39   #define VALID_FS_TYPES {"ufs", "tmpfs"}
40   #endif
41  
42 < #ifdef LINUX
38 < #include <sys/vfs.h>
42 > #if defined(LINUX) || defined(CYGWIN)
43   #include <mntent.h>
44 + #include <sys/vfs.h>
45   #include "tools.h"
41 #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
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 < char *copy_string(char *orig_ptr, const char *newtext){
79 > static char *copy_string(char **dest, const char *src) {
80 >        char *new;
81  
82 <        /* Maybe free if not NULL, and strdup rather than realloc and strcpy? */
83 <        orig_ptr=realloc(orig_ptr, (1+strlen(newtext)));
84 <        if(orig_ptr==NULL){
85 <                return NULL;
60 <        }
61 <        strcpy(orig_ptr, newtext);
82 >        new = realloc(*dest, strlen(src) + 1);
83 >        if (new == NULL) {
84 >                return NULL;
85 >        }
86  
87 <        return orig_ptr;
87 >        strcpy(new, src);
88 >        *dest = new;
89 >        return new;
90   }
91  
92 + static void disk_stat_init(disk_stat_t *d) {
93 +        d->device_name = NULL;
94 +        d->fs_type = NULL;
95 +        d->mnt_point = NULL;
96 + }
97  
98 < void init_disk_stat(int start, int end, disk_stat_t *disk_stats){
98 > static void disk_stat_destroy(disk_stat_t *d) {
99 >        free(d->device_name);
100 >        free(d->fs_type);
101 >        free(d->mnt_point);
102 > }
103  
104 <        for(disk_stats+=start; start<=end; start++){
105 <                disk_stats->device_name=NULL;
106 <                disk_stats->fs_type=NULL;
107 <                disk_stats->mnt_point=NULL;
108 <                
109 <                disk_stats++;
104 > int is_valid_fs_type(const char *type) {
105 >        const char *types[] = VALID_FS_TYPES;
106 >        int i;
107 >
108 >        for (i = 0; i < (sizeof types / sizeof *types); i++) {
109 >                if (strcmp(types[i], type) == 0) {
110 >                        return 1;
111 >                }
112          }
113 +        return 0;
114   }
115  
116   disk_stat_t *get_disk_stats(int *entries){
117 +        VECTOR_DECLARE_STATIC(disk_stats, disk_stat_t, 10,
118 +                              disk_stat_init, disk_stat_destroy);
119  
120 <        static disk_stat_t *disk_stats;
81 <        static int watermark=-1;
82 <
83 <        char *fs_types[] = VALID_FS_TYPES;
84 <        int x, valid_type;
120 >        int valid_type;
121          int num_disks=0;
122 < #if defined(LINUX) || defined (SOLARIS)
122 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
123          FILE *f;
124   #endif
125  
# Line 93 | Line 129 | disk_stat_t *get_disk_stats(int *entries){
129          struct mnttab mp;
130          struct statvfs fs;
131   #endif
132 < #ifdef LINUX
132 > #if defined(LINUX) || defined(CYGWIN)
133          struct mntent *mp;
134          struct statfs fs;
135   #endif
136 < #ifdef FREEBSD
136 > #ifdef ALLBSD
137          int nummnt;
138          struct statfs *mp;
139   #endif
140  
141 <        if(watermark==-1){
106 <                disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
107 <                if(disk_stats==NULL){
108 <                        return NULL;
109 <                }
110 <                watermark=START_VAL;
111 <                init_disk_stat(0, watermark-1, disk_stats);
112 <        }
113 < #ifdef FREEBSD
141 > #ifdef ALLBSD
142          nummnt=getmntinfo(&mp , MNT_LOCAL);
143          if (nummnt<=0){
144                  return NULL;
145          }
146          for(;nummnt--; mp++){
147 <                valid_type=0;
120 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
121 <                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
122 <                                valid_type=1;
123 <                                break;
124 <                        }
125 <                }
147 >                valid_type = is_valid_fs_type(mp->f_fstypename);
148   #endif
149  
150 < #ifdef LINUX
150 > #if defined(LINUX) || defined(CYGWIN)
151          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
152                  return NULL;
153          }
# Line 135 | Line 157 | disk_stat_t *get_disk_stats(int *entries){
157                          continue;
158                  }      
159  
160 <                valid_type=0;
139 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
140 <                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
141 <                                valid_type=1;
142 <                                break;
143 <                        }
144 <                }
160 >                valid_type = is_valid_fs_type(mp->mnt_type);
161   #endif
162  
163   #ifdef SOLARIS
# Line 152 | Line 168 | disk_stat_t *get_disk_stats(int *entries){
168                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
169                          continue;
170                  }
171 <                valid_type=0;
156 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
157 <                        if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
158 <                                valid_type=1;
159 <                                break;
160 <                        }
161 <                }
171 >                valid_type = is_valid_fs_type(mp.mnt_fstype);
172   #endif
173  
174                  if(valid_type){
175 <                        if(num_disks>watermark-1){
176 <                                disk_ptr=disk_stats;
167 <                                if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
168 <                                        disk_stats=disk_ptr;
169 <                                        return NULL;
170 <                                }
171 <
172 <                                watermark=watermark*2;
173 <                                init_disk_stat(num_disks, watermark-1, disk_stats);
175 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
176 >                                return NULL;
177                          }
175
178                          disk_ptr=disk_stats+num_disks;
179 < #ifdef FREEBSD
180 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
179 >
180 > #ifdef ALLBSD
181 >                        if (copy_string(&disk_ptr->device_name, mp->f_mntfromname) == NULL) {
182                                  return NULL;
183                          }
184 <
182 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
184 >                        if (copy_string(&disk_ptr->fs_type, mp->f_fstypename) == NULL) {
185                                  return NULL;
186                          }
187 <
186 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
187 >                        if (copy_string(&disk_ptr->mnt_point, mp->f_mntonname) == NULL) {
188                                  return NULL;
189                          }
190  
# Line 196 | Line 197 | disk_stat_t *get_disk_stats(int *entries){
197                          /* Freebsd doesn't have a "available" inodes */
198                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
199   #endif
200 < #ifdef LINUX
201 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
200 > #if defined(LINUX) || defined(CYGWIN)
201 >                        if (copy_string(&disk_ptr->device_name, mp->mnt_fsname) == NULL) {
202                                  return NULL;
203                          }
204                                  
205 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
205 >                        if (copy_string(&disk_ptr->fs_type, mp->mnt_type) == NULL) {    
206                                  return NULL;
207                          }
208  
209 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
209 >                        if (copy_string(&disk_ptr->mnt_point, mp->mnt_dir) == NULL) {
210                                  return NULL;
211                          }
212                          disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
# Line 219 | Line 220 | disk_stat_t *get_disk_stats(int *entries){
220   #endif
221  
222   #ifdef SOLARIS
222                        /* Memory leak in event of realloc failing */
223                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
224                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
225                           * be upwards of a k each
226                           */
227 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
227 >                        if (copy_string(&disk_ptr->device_name, mp.mnt_special) == NULL) {
228                                  return NULL;
229                          }
230  
231 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
231 >                        if (copy_string(&disk_ptr->fs_type, mp.mnt_fstype) == NULL) {
232                                  return NULL;
233                          }
234          
235 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
235 >                        if (copy_string(&disk_ptr->mnt_point, mp.mnt_mountp) == NULL) {
236                                  return NULL;
237                          }
238                          
# Line 250 | Line 250 | disk_stat_t *get_disk_stats(int *entries){
250  
251          *entries=num_disks;    
252  
253 <        /* If this fails, there is very little i can do about it, so i'll ignore it :) */
254 < #if defined(LINUX) || defined(SOLARIS)
253 >        /* If this fails, there is very little i can do about it, so
254 >           I'll ignore it :) */
255 > #if defined(LINUX) || defined(CYGWIN)
256 >        endmntent(f);
257 > #endif
258 > #if defined(SOLARIS)
259          fclose(f);
260   #endif
261  
262          return disk_stats;
263  
264   }
261 void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
265  
266 <        for(diskio_stats+=start; start<end; start++){
267 <                diskio_stats->disk_name=NULL;
265 <                
266 <                diskio_stats++;
267 <        }
266 > static void diskio_stat_init(diskio_stat_t *d) {
267 >        d->disk_name = NULL;
268   }
269  
270 < diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){
271 <
272 <        if(diskio_stats==NULL){
273 <
274 <                if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){
275 <                        return NULL;
276 <                }
277 <                diskio_stat_init(0, needed_entries, diskio_stats);
278 <                *cur_entries=needed_entries;
279 <
280 <                return diskio_stats;
281 <        }
282 <
283 <
284 <        if(*cur_entries<needed_entries){
285 <                diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries));
286 <                if(diskio_stats==NULL){
287 <                        return NULL;
288 <                }
289 <                diskio_stat_init(*cur_entries, needed_entries, diskio_stats);
290 <                *cur_entries=needed_entries;
291 <        }
292 <
293 <        return diskio_stats;
270 > static void diskio_stat_destroy(diskio_stat_t *d) {
271 >        free(d->disk_name);
272   }
273  
274 < static diskio_stat_t *diskio_stats=NULL;        
275 < static int num_diskio=0;        
274 > VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10,
275 >                      diskio_stat_init, diskio_stat_destroy);
276  
277   #ifdef LINUX
278   typedef struct {
# Line 304 | Line 282 | typedef struct {
282   #endif
283  
284   diskio_stat_t *get_diskio_stats(int *entries){
285 <
286 <        static int sizeof_diskio_stats=0;
285 >        int num_diskio;
286 > #ifndef LINUX
287          diskio_stat_t *diskio_stats_ptr;
288 + #endif
289  
290   #ifdef SOLARIS
291          kstat_ctl_t *kc;
# Line 317 | Line 296 | diskio_stat_t *get_diskio_stats(int *entries){
296          FILE *f;
297          char *line_ptr;
298          int major, minor;
320        char dev_letter;
299          int has_pp_stats = 1;
300 <        static partition *parts = NULL;
323 <        static int alloc_parts = 0;
300 >        VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL);
301          int i, n;
302          time_t now;
303 +        const char *format;
304   #endif
305 < #ifdef FREEBSD
305 > #if defined(FREEBSD) || defined(DFBSD)
306          static struct statinfo stats;
307          static int stats_init = 0;
308          int counter;
# Line 333 | Line 311 | diskio_stat_t *get_diskio_stats(int *entries){
311          long sel_gen;
312          struct devstat *dev_ptr;
313   #endif
314 + #ifdef NETBSD
315 +        struct disk_sysctl *stats;
316 + #endif
317 + #ifdef OPENBSD
318 +        int diskcount;
319 +        char *disknames, *name, *bufpp;
320 +        char **dk_name;
321 +        struct diskstats *stats;
322 + #endif
323 + #ifdef NETBSD
324 + #define MIBSIZE 3
325 + #endif
326 + #ifdef OPENBSD
327 + #define MIBSIZE 2
328 + #endif
329 + #if defined(NETBSD) || defined(OPENBSD)
330 +        int num_disks, i;
331 +        int mib[MIBSIZE];
332 +        size_t size;
333 + #endif
334 +
335          num_diskio=0;
336  
337 < #ifdef FREEBSD
337 > #ifdef OPENBSD
338 >        mib[0] = CTL_HW;
339 >        mib[1] = HW_DISKCOUNT;
340 >
341 >        size = sizeof(diskcount);
342 >        if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
343 >                return NULL;
344 >        }
345 >
346 >        mib[0] = CTL_HW;
347 >        mib[1] = HW_DISKNAMES;
348 >
349 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
350 >                return NULL;
351 >        }
352 >
353 >        disknames = malloc(size);
354 >        if (disknames == NULL) {
355 >                return NULL;
356 >        }
357 >
358 >        if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
359 >                return NULL;
360 >        }
361 >
362 >        dk_name = calloc(diskcount, sizeof(char *));
363 >        bufpp = disknames;
364 >        for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
365 >                dk_name[i] = name;
366 >        }
367 > #endif
368 >
369 > #if defined(NETBSD) || defined(OPENBSD)
370 >        mib[0] = CTL_HW;
371 >        mib[1] = HW_DISKSTATS;
372 > #ifdef NETBSD
373 >        mib[2] = sizeof(struct disk_sysctl);
374 > #endif
375 >
376 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
377 >                return NULL;
378 >        }
379 >
380 > #ifdef NETBSD
381 >        num_disks = size / sizeof(struct disk_sysctl);
382 > #else
383 >        num_disks = size / sizeof(struct diskstats);
384 > #endif
385 >
386 >        stats = malloc(size);
387 >        if (stats == NULL) {
388 >                return NULL;
389 >        }
390 >
391 >        if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
392 >                return NULL;
393 >        }
394 >
395 >        for (i = 0; i < num_disks; i++) {
396 >                u_int64_t rbytes, wbytes;
397 >
398 > #ifdef NETBSD
399 > #ifdef HAVE_DK_RBYTES
400 >                rbytes = stats[i].dk_rbytes;
401 >                wbytes = stats[i].dk_wbytes;
402 > #else
403 >                /* Before 1.7, NetBSD merged reads and writes. */
404 >                rbytes = wbytes = stats[i].dk_bytes;
405 > #endif
406 > #else
407 >                rbytes = wbytes = stats[i].ds_bytes;
408 > #endif
409 >
410 >                /* Don't keep stats for disks that have never been used. */
411 >                if (rbytes == 0 && wbytes == 0) {
412 >                        continue;
413 >                }
414 >
415 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
416 >                        return NULL;
417 >                }
418 >                diskio_stats_ptr = diskio_stats + num_diskio;
419 >                
420 >                diskio_stats_ptr->read_bytes = rbytes;
421 >                diskio_stats_ptr->write_bytes = wbytes;
422 >                if (diskio_stats_ptr->disk_name != NULL) {
423 >                        free(diskio_stats_ptr->disk_name);
424 >                }
425 > #ifdef NETBSD
426 >                diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
427 > #else
428 >                diskio_stats_ptr->disk_name = strdup(dk_name[i]);
429 > #endif
430 >                diskio_stats_ptr->systime = time(NULL);
431 >        
432 >                num_diskio++;  
433 >        }
434 >
435 >        free(stats);
436 > #ifdef OPENBSD
437 >        free(dk_name);
438 >        free(disknames);
439 > #endif
440 > #endif
441 >
442 > #if defined(FREEBSD) || defined(DFBSD)
443          if (!stats_init) {
444                  stats.dinfo=malloc(sizeof(struct devinfo));
341                bzero(stats.dinfo, sizeof(struct devinfo));
445                  if(stats.dinfo==NULL) return NULL;
446 +                bzero(stats.dinfo, sizeof(struct devinfo));
447                  stats_init = 1;
448          }
449   #ifdef FREEBSD5
450          if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
451 <        /* Not aware of a get all devices, so i said 999. If we ever                                                                           * find a machine with more than 999 disks, then i'll change                                                                           * this number :)                                                                                                                      */
451 >        /* Not aware of a get all devices, so i said 999. If we ever
452 >         * find a machine with more than 999 disks, then i'll change
453 >         * this number :)
454 >         */
455          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;
456   #else
457          if ((getdevs(&stats)) < 0) return NULL;
# Line 367 | Line 474 | diskio_stat_t *get_diskio_stats(int *entries){
474   #else
475                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
476   #endif
477 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
477 >
478 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
479                          return NULL;
480                  }
481                  diskio_stats_ptr=diskio_stats+num_diskio;
# Line 402 | Line 510 | diskio_stat_t *get_diskio_stats(int *entries){
510                          if((kstat_read(kc, ksp, &kios))==-1){  
511                          }
512                          
513 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
513 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
514                                  kstat_close(kc);
515                                  return NULL;
516                          }
# Line 414 | Line 522 | diskio_stat_t *get_diskio_stats(int *entries){
522  
523                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
524  
525 <                        diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
525 >                        diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name));
526                          diskio_stats_ptr->systime=time(NULL);
527                          num_diskio++;
528                  }
# Line 428 | Line 536 | diskio_stat_t *get_diskio_stats(int *entries){
536          n = 0;
537  
538          /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
539 <           have statistics in here too, so we can use those directly. */
539 >           have statistics in here too, so we can use those directly.
540 >           2.6 kernels have /proc/diskstats instead with almost (but not quite)
541 >           the same format. */
542  
543 <        f = fopen("/proc/partitions", "r");
543 >        f = fopen("/proc/diskstats", "r");
544 >        format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld";
545 >        if (f == NULL) {
546 >                f = fopen("/proc/partitions", "r");
547 >                format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld";
548 >        }
549          if (f == NULL) goto out;
550          now = time(NULL);
551  
552          while ((line_ptr = f_read_line(f, "")) != NULL) {
553 <                char name[20];
553 >                char name[100];
554                  char *s;
555                  long long rsect, wsect;
556  
557 <                int nr = sscanf(line_ptr,
443 <                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
557 >                int nr = sscanf(line_ptr, format,
558                          &major, &minor, name, &rsect, &wsect);
559                  if (nr < 3) continue;
446                if (nr < 5) {
447                        has_pp_stats = 0;
448                        rsect = 0;
449                        wsect = 0;
450                }
560  
561                  /* Skip device names ending in numbers, since they're
562                     partitions. */
# Line 456 | Line 565 | diskio_stat_t *get_diskio_stats(int *entries){
565                  --s;
566                  if (*s >= '0' && *s <= '9') continue;
567  
568 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
569 <                        diskio_stats);
570 <                if (diskio_stats == NULL) goto out;
571 <                if (n >= alloc_parts) {
463 <                        alloc_parts += 16;
464 <                        parts = realloc(parts, alloc_parts * sizeof *parts);
465 <                        if (parts == NULL) {
466 <                                alloc_parts = 0;
467 <                                goto out;
468 <                        }
568 >                if (nr < 5) {
569 >                        has_pp_stats = 0;
570 >                        rsect = 0;
571 >                        wsect = 0;
572                  }
573  
574 +                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
575 +                        goto out;
576 +                }
577 +                if (VECTOR_RESIZE(parts, n + 1) < 0) {
578 +                        goto out;
579 +                }
580 +
581                  if (diskio_stats[n].disk_name != NULL)
582                          free(diskio_stats[n].disk_name);
583                  diskio_stats[n].disk_name = strdup(name);
# Line 480 | Line 590 | diskio_stat_t *get_diskio_stats(int *entries){
590                  n++;
591          }
592  
593 +        fclose(f);
594 +        f = NULL;
595 +
596          if (!has_pp_stats) {
597 <                /* This is an older kernel without stats in /proc/partitions.
598 <                   Read what we can from /proc/stat instead. */
597 >                /* This is an older kernel where /proc/partitions doesn't
598 >                   contain stats. Read what we can from /proc/stat instead, and
599 >                   fill in the appropriate bits of the list allocated above. */
600  
601                  f = fopen("/proc/stat", "r");
602                  if (f == NULL) goto out;
# Line 549 | Line 663 | diskio_stat_t *get_diskio_stats(int *entries){
663          num_diskio = n;
664   out:
665          if (f != NULL) fclose(f);
666 + #endif
667  
668 + #ifdef CYGWIN
669 +        return NULL;
670   #endif
671 +
672          *entries=num_diskio;
673  
674          return diskio_stats;
675   }
676  
677   diskio_stat_t *get_diskio_stats_diff(int *entries){
678 <        static diskio_stat_t *diskio_stats_diff=NULL;
679 <        static int sizeof_diskio_stats_diff=0;
680 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
681 <        int disks, x, y;
678 >        VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1,
679 >                              diskio_stat_init, diskio_stat_destroy);
680 >        diskio_stat_t *src = NULL, *dest;
681 >        int i, j, diff_count, new_count;
682  
683 <        if(diskio_stats==NULL){
684 <                diskio_stats_ptr=get_diskio_stats(&disks);
685 <                *entries=disks;
568 <                return diskio_stats_ptr;
683 >        if (diskio_stats == NULL) {
684 >                /* No previous stats, so we can't calculate a difference. */
685 >                return get_diskio_stats(entries);
686          }
687  
688 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
689 <        if(diskio_stats_diff==NULL){
688 >        /* Resize the results array to match the previous stats. */
689 >        diff_count = VECTOR_SIZE(diskio_stats);
690 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
691                  return NULL;
692          }
693  
694 <        diskio_stats_diff_ptr=diskio_stats_diff;
695 <        diskio_stats_ptr=diskio_stats;
694 >        /* Copy the previous stats into the result. */
695 >        for (i = 0; i < diff_count; i++) {
696 >                src = &diskio_stats[i];
697 >                dest = &diff[i];
698  
699 <        for(disks=0;disks<num_diskio;disks++){
700 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
581 <                        free(diskio_stats_diff_ptr->disk_name);
699 >                if (dest->disk_name != NULL) {
700 >                        free(dest->disk_name);
701                  }
702 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
703 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
704 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
705 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
702 >                dest->disk_name = strdup(src->disk_name);
703 >                dest->read_bytes = src->read_bytes;
704 >                dest->write_bytes = src->write_bytes;
705 >                dest->systime = src->systime;
706 >        }
707  
708 <                diskio_stats_diff_ptr++;
709 <                diskio_stats_ptr++;
708 >        /* Get a new set of stats. */
709 >        if (get_diskio_stats(&new_count) == NULL) {
710 >                return NULL;
711          }
712  
713 <        diskio_stats_ptr=get_diskio_stats(&disks);
714 <        diskio_stats_diff_ptr=diskio_stats_diff;
713 >        /* For each previous stat... */
714 >        for (i = 0; i < diff_count; i++) {
715 >                dest = &diff[i];
716  
717 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
718 <
719 <                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
720 <                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
721 <                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
722 <                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
723 <                }else{
602 <                        diskio_stats_ptr=diskio_stats;
603 <                        for(y=0;y<disks;y++){
604 <                                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
605 <                                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
606 <                                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
607 <                                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
608 <
609 <                                        break;
610 <                                }
611 <                                
612 <                                diskio_stats_ptr++;
717 >                /* ... find the corresponding new stat ... */
718 >                for (j = 0; j < new_count; j++) {
719 >                        /* Try the new stat in the same position first,
720 >                           since that's most likely to be it. */
721 >                        src = &diskio_stats[(i + j) % new_count];
722 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
723 >                                break;
724                          }
725                  }
726 +                if (j == new_count) {
727 +                        /* No match found. */
728 +                        continue;
729 +                }
730  
731 <                diskio_stats_ptr++;
732 <                diskio_stats_diff_ptr++;        
733 <
731 >                /* ... and subtract the previous stat from it to get the
732 >                   difference. */
733 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
734 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
735 >                dest->systime = src->systime - dest->systime;
736          }
737 <        
738 <        *entries=sizeof_diskio_stats_diff;
739 <        return diskio_stats_diff;
737 >
738 >        *entries = diff_count;
739 >        return diff;
740   }
741 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines