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.57 by ats, Sun Apr 4 22:29:54 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>
37   #include <sys/statvfs.h>
38   #include <kstat.h>
34 #include <sys/types.h>
35 #include <dirent.h>
36 #include <limits.h>
37 #include <unistd.h>
39   #define VALID_FS_TYPES {"ufs", "tmpfs"}
40   #endif
41  
42 < #ifdef LINUX
42 < #include <sys/vfs.h>
42 > #if defined(LINUX) || defined(CYGWIN)
43   #include <mntent.h>
44 + #include <sys/vfs.h>
45   #include "tools.h"
45 #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
72 <
73 < /* Solaris kernel stores the disk names in the old BSD format
74 < * and we would prefer it in th modern format.
75 < */
76 < #ifdef SOLARIS
62 < /* Lines from the path_to_inst file */
63 < typedef struct {
64 <        /* unlikely to get a line that long */
65 <        char line[256];
66 < }file_line_t;
67 <
68 < /* Linked list of the mappings from the older bsd-based sunos names
69 < * 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;
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;
85 <        }
86 <        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;
106 <        static int watermark=-1;
107 <
108 <        char *fs_types[] = VALID_FS_TYPES;
109 <        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 118 | 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){
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
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;
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 <                }
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 160 | Line 157 | disk_stat_t *get_disk_stats(int *entries){
157                          continue;
158                  }      
159  
160 <                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 <                }
160 >                valid_type = is_valid_fs_type(mp->mnt_type);
161   #endif
162  
163   #ifdef SOLARIS
# Line 177 | 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;
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 <                }
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;
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);
175 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
176 >                                return NULL;
177                          }
200
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 <
207 <                        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 <
211 <                        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 221 | 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 244 | Line 220 | disk_stat_t *get_disk_stats(int *entries){
220   #endif
221  
222   #ifdef SOLARIS
247                        /* 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 275 | 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   }
286 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;
290 <                
291 <                diskio_stats++;
292 <        }
266 > static void diskio_stat_init(diskio_stat_t *d) {
267 >        d->disk_name = NULL;
268   }
269  
270 < #ifdef SOLARIS
271 < char *drive_map(char *sunos_name){
297 <        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;
309 <
310 <        int dev_num;
311 <        char dev_name[51];
312 <
313 <        if(disk_mapping == NULL){
314 <                /* 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);
270 > static void diskio_stat_destroy(diskio_stat_t *d) {
271 >        free(d->disk_name);
272   }
273  
274 < #endif
274 > VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10,
275 >                      diskio_stat_init, diskio_stat_destroy);
276  
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;
444 }
445
446 static diskio_stat_t *diskio_stats=NULL;        
447 static int num_diskio=0;        
448
277   #ifdef LINUX
278   typedef struct {
279          int major;
# Line 454 | 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 467 | Line 296 | diskio_stat_t *get_diskio_stats(int *entries){
296          FILE *f;
297          char *line_ptr;
298          int major, minor;
470        char dev_letter;
299          int has_pp_stats = 1;
300          static partition *parts = NULL;
301          static int alloc_parts = 0;
302          int i, n;
303          time_t now;
304 +        const char *format;
305   #endif
306 < #ifdef FREEBSD
306 > #if defined(FREEBSD) || defined(DFBSD)
307          static struct statinfo stats;
308          static int stats_init = 0;
309          int counter;
# Line 483 | Line 312 | diskio_stat_t *get_diskio_stats(int *entries){
312          long sel_gen;
313          struct devstat *dev_ptr;
314   #endif
315 + #ifdef NETBSD
316 +        struct disk_sysctl *stats;
317 + #endif
318 + #ifdef OPENBSD
319 +        int diskcount;
320 +        char *disknames, *name, *bufpp;
321 +        char **dk_name;
322 +        struct diskstats *stats;
323 + #endif
324 + #ifdef NETBSD
325 + #define MIBSIZE 3
326 + #endif
327 + #ifdef OPENBSD
328 + #define MIBSIZE 2
329 + #endif
330 + #if defined(NETBSD) || defined(OPENBSD)
331 +        int num_disks, i;
332 +        int mib[MIBSIZE];
333 +        size_t size;
334 + #endif
335 +
336          num_diskio=0;
337  
338 < #ifdef FREEBSD
338 > #ifdef OPENBSD
339 >        mib[0] = CTL_HW;
340 >        mib[1] = HW_DISKCOUNT;
341 >
342 >        size = sizeof(diskcount);
343 >        if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
344 >                return NULL;
345 >        }
346 >
347 >        mib[0] = CTL_HW;
348 >        mib[1] = HW_DISKNAMES;
349 >
350 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
351 >                return NULL;
352 >        }
353 >
354 >        disknames = malloc(size);
355 >        if (disknames == NULL) {
356 >                return NULL;
357 >        }
358 >
359 >        if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
360 >                return NULL;
361 >        }
362 >
363 >        dk_name = calloc(diskcount, sizeof(char *));
364 >        bufpp = disknames;
365 >        for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
366 >                dk_name[i] = name;
367 >        }
368 > #endif
369 >
370 > #if defined(NETBSD) || defined(OPENBSD)
371 >        mib[0] = CTL_HW;
372 >        mib[1] = HW_DISKSTATS;
373 > #ifdef NETBSD
374 >        mib[2] = sizeof(struct disk_sysctl);
375 > #endif
376 >
377 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
378 >                return NULL;
379 >        }
380 >
381 > #ifdef NETBSD
382 >        num_disks = size / sizeof(struct disk_sysctl);
383 > #else
384 >        num_disks = size / sizeof(struct diskstats);
385 > #endif
386 >
387 >        stats = malloc(size);
388 >        if (stats == NULL) {
389 >                return NULL;
390 >        }
391 >
392 >        if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
393 >                return NULL;
394 >        }
395 >
396 >        for (i = 0; i < num_disks; i++) {
397 >                u_int64_t rbytes, wbytes;
398 >
399 > #ifdef NETBSD
400 > #ifdef HAVE_DK_RBYTES
401 >                rbytes = stats[i].dk_rbytes;
402 >                wbytes = stats[i].dk_wbytes;
403 > #else
404 >                /* Before 1.7, NetBSD merged reads and writes. */
405 >                rbytes = wbytes = stats[i].dk_bytes;
406 > #endif
407 > #else
408 >                rbytes = wbytes = stats[i].ds_bytes;
409 > #endif
410 >
411 >                /* Don't keep stats for disks that have never been used. */
412 >                if (rbytes == 0 && wbytes == 0) {
413 >                        continue;
414 >                }
415 >
416 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
417 >                        return NULL;
418 >                }
419 >                diskio_stats_ptr = diskio_stats + num_diskio;
420 >                
421 >                diskio_stats_ptr->read_bytes = rbytes;
422 >                diskio_stats_ptr->write_bytes = wbytes;
423 >                if (diskio_stats_ptr->disk_name != NULL) {
424 >                        free(diskio_stats_ptr->disk_name);
425 >                }
426 > #ifdef NETBSD
427 >                diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
428 > #else
429 >                diskio_stats_ptr->disk_name = strdup(dk_name[i]);
430 > #endif
431 >                diskio_stats_ptr->systime = time(NULL);
432 >        
433 >                num_diskio++;  
434 >        }
435 >
436 >        free(stats);
437 > #ifdef OPENBSD
438 >        free(dk_name);
439 >        free(disknames);
440 > #endif
441 > #endif
442 >
443 > #if defined(FREEBSD) || defined(DFBSD)
444          if (!stats_init) {
445                  stats.dinfo=malloc(sizeof(struct devinfo));
446                  if(stats.dinfo==NULL) return NULL;
447 +                bzero(stats.dinfo, sizeof(struct devinfo));
448                  stats_init = 1;
449          }
450 + #ifdef FREEBSD5
451 +        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
452 +        /* Not aware of a get all devices, so i said 999. If we ever
453 +         * find a machine with more than 999 disks, then i'll change
454 +         * this number :)
455 +         */
456 +        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;
457 + #else
458          if ((getdevs(&stats)) < 0) return NULL;
459          /* Not aware of a get all devices, so i said 999. If we ever
460           * find a machine with more than 999 disks, then i'll change
461           * this number :)
462           */
463          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;
464 + #endif
465  
466          for(counter=0;counter<stats.dinfo->numdevs;counter++){
467                  dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
# Line 505 | Line 470 | diskio_stat_t *get_diskio_stats(int *entries){
470                   * devices.. like mem, proc.. and also doesn't report floppy
471                   * drives etc unless they are doing stuff :)
472                   */
473 + #ifdef FREEBSD5
474 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
475 + #else
476                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
477 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
477 > #endif
478 >
479 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
480                          return NULL;
481                  }
482                  diskio_stats_ptr=diskio_stats+num_diskio;
483 <                
483 >
484 > #ifdef FREEBSD5        
485 >                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
486 >                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
487 > #else
488                  diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
489                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
490 + #endif
491                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
492                  asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
493                  diskio_stats_ptr->systime=time(NULL);
# Line 520 | Line 495 | diskio_stat_t *get_diskio_stats(int *entries){
495                  num_diskio++;
496          }
497          free(dev_sel);
523        free(stats.dinfo);
498  
499   #endif
500   #ifdef SOLARIS
# Line 537 | Line 511 | diskio_stat_t *get_diskio_stats(int *entries){
511                          if((kstat_read(kc, ksp, &kios))==-1){  
512                          }
513                          
514 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
514 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
515                                  kstat_close(kc);
516                                  return NULL;
517                          }
# Line 549 | Line 523 | diskio_stat_t *get_diskio_stats(int *entries){
523  
524                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
525  
526 <                        diskio_stats_ptr->disk_name=drive_map(ksp->ks_name);
526 >                        diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name));
527                          diskio_stats_ptr->systime=time(NULL);
528                          num_diskio++;
529                  }
# Line 563 | Line 537 | diskio_stat_t *get_diskio_stats(int *entries){
537          n = 0;
538  
539          /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
540 <           have statistics in here too, so we can use those directly. */
540 >           have statistics in here too, so we can use those directly.
541 >           2.6 kernels have /proc/diskstats instead with almost (but not quite)
542 >           the same format. */
543  
544 <        f = fopen("/proc/partitions", "r");
544 >        f = fopen("/proc/diskstats", "r");
545 >        format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld";
546 >        if (f == NULL) {
547 >                f = fopen("/proc/partitions", "r");
548 >                format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld";
549 >        }
550          if (f == NULL) goto out;
551          now = time(NULL);
552  
553          while ((line_ptr = f_read_line(f, "")) != NULL) {
554 <                char name[20];
554 >                char name[100];
555                  char *s;
556                  long long rsect, wsect;
557  
558 <                int nr = sscanf(line_ptr,
578 <                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
558 >                int nr = sscanf(line_ptr, format,
559                          &major, &minor, name, &rsect, &wsect);
560                  if (nr < 3) continue;
581                if (nr < 5) {
582                        has_pp_stats = 0;
583                        rsect = 0;
584                        wsect = 0;
585                }
561  
562                  /* Skip device names ending in numbers, since they're
563                     partitions. */
# Line 591 | Line 566 | diskio_stat_t *get_diskio_stats(int *entries){
566                  --s;
567                  if (*s >= '0' && *s <= '9') continue;
568  
569 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
570 <                        diskio_stats);
571 <                if (diskio_stats == NULL) goto out;
569 >                if (nr < 5) {
570 >                        has_pp_stats = 0;
571 >                        rsect = 0;
572 >                        wsect = 0;
573 >                }
574 >
575 >                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
576 >                        goto out;
577 >                }
578                  if (n >= alloc_parts) {
579                          alloc_parts += 16;
580                          parts = realloc(parts, alloc_parts * sizeof *parts);
# Line 615 | Line 596 | diskio_stat_t *get_diskio_stats(int *entries){
596                  n++;
597          }
598  
599 +        fclose(f);
600 +        f = NULL;
601 +
602          if (!has_pp_stats) {
603 <                /* This is an older kernel without stats in /proc/partitions.
604 <                   Read what we can from /proc/stat instead. */
603 >                /* This is an older kernel where /proc/partitions doesn't
604 >                   contain stats. Read what we can from /proc/stat instead, and
605 >                   fill in the appropriate bits of the list allocated above. */
606  
607                  f = fopen("/proc/stat", "r");
608                  if (f == NULL) goto out;
# Line 684 | Line 669 | diskio_stat_t *get_diskio_stats(int *entries){
669          num_diskio = n;
670   out:
671          if (f != NULL) fclose(f);
672 + #endif
673  
674 + #ifdef CYGWIN
675 +        return NULL;
676   #endif
677 +
678          *entries=num_diskio;
679  
680          return diskio_stats;
681   }
682  
683   diskio_stat_t *get_diskio_stats_diff(int *entries){
684 <        static diskio_stat_t *diskio_stats_diff=NULL;
685 <        static int sizeof_diskio_stats_diff=0;
686 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
687 <        int disks, x, y;
684 >        VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1,
685 >                              diskio_stat_init, diskio_stat_destroy);
686 >        diskio_stat_t *src = NULL, *dest;
687 >        int i, j, diff_count, new_count;
688  
689 <        if(diskio_stats==NULL){
690 <                diskio_stats_ptr=get_diskio_stats(&disks);
691 <                *entries=disks;
703 <                return diskio_stats_ptr;
689 >        if (diskio_stats == NULL) {
690 >                /* No previous stats, so we can't calculate a difference. */
691 >                return get_diskio_stats(entries);
692          }
693  
694 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
695 <        if(diskio_stats_diff==NULL){
694 >        /* Resize the results array to match the previous stats. */
695 >        diff_count = VECTOR_SIZE(diskio_stats);
696 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
697                  return NULL;
698          }
699  
700 <        diskio_stats_diff_ptr=diskio_stats_diff;
701 <        diskio_stats_ptr=diskio_stats;
700 >        /* Copy the previous stats into the result. */
701 >        for (i = 0; i < diff_count; i++) {
702 >                src = &diskio_stats[i];
703 >                dest = &diff[i];
704  
705 <        for(disks=0;disks<num_diskio;disks++){
706 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
716 <                        free(diskio_stats_diff_ptr->disk_name);
705 >                if (dest->disk_name != NULL) {
706 >                        free(dest->disk_name);
707                  }
708 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
709 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
710 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
711 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
708 >                dest->disk_name = strdup(src->disk_name);
709 >                dest->read_bytes = src->read_bytes;
710 >                dest->write_bytes = src->write_bytes;
711 >                dest->systime = src->systime;
712 >        }
713  
714 <                diskio_stats_diff_ptr++;
715 <                diskio_stats_ptr++;
714 >        /* Get a new set of stats. */
715 >        if (get_diskio_stats(&new_count) == NULL) {
716 >                return NULL;
717          }
718  
719 <        diskio_stats_ptr=get_diskio_stats(&disks);
720 <        diskio_stats_diff_ptr=diskio_stats_diff;
719 >        /* For each previous stat... */
720 >        for (i = 0; i < diff_count; i++) {
721 >                dest = &diff[i];
722  
723 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
724 <
725 <                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
726 <                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
727 <                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
728 <                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
729 <                }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++;
723 >                /* ... find the corresponding new stat ... */
724 >                for (j = 0; j < new_count; j++) {
725 >                        /* Try the new stat in the same position first,
726 >                           since that's most likely to be it. */
727 >                        src = &diskio_stats[(i + j) % new_count];
728 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
729 >                                break;
730                          }
731                  }
732 +                if (j == new_count) {
733 +                        /* No match found. */
734 +                        continue;
735 +                }
736  
737 <                diskio_stats_ptr++;
738 <                diskio_stats_diff_ptr++;        
739 <
737 >                /* ... and subtract the previous stat from it to get the
738 >                   difference. */
739 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
740 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
741 >                dest->systime = src->systime - dest->systime;
742          }
743 <        
744 <        *entries=sizeof_diskio_stats_diff;
745 <        return diskio_stats_diff;
743 >
744 >        *entries = diff_count;
745 >        return diff;
746   }
747 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines