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.53 by tdb, Mon Feb 16 14:55:32 2004 UTC vs.
Revision 1.59 by ats, Sun Apr 4 23:55:48 2004 UTC

# Line 30 | Line 30
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 75 | Line 76
76                          "ntfs"}
77   #endif
78  
79 < #define START_VAL 1
80 <
81 < char *copy_string(char *orig_ptr, const char *newtext){
82 <
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;
86 <        }
87 <        strcpy(orig_ptr, newtext);
88 <
89 <        return orig_ptr;
79 > static void disk_stat_init(disk_stat_t *d) {
80 >        d->device_name = NULL;
81 >        d->fs_type = NULL;
82 >        d->mnt_point = NULL;
83   }
84  
85 <
86 < void init_disk_stat(int start, int end, disk_stat_t *disk_stats){
87 <
88 <        for(disk_stats+=start; start<=end; start++){
96 <                disk_stats->device_name=NULL;
97 <                disk_stats->fs_type=NULL;
98 <                disk_stats->mnt_point=NULL;
99 <                
100 <                disk_stats++;
101 <        }
85 > static void disk_stat_destroy(disk_stat_t *d) {
86 >        free(d->device_name);
87 >        free(d->fs_type);
88 >        free(d->mnt_point);
89   }
90  
91   int is_valid_fs_type(const char *type) {
# Line 114 | Line 101 | int is_valid_fs_type(const char *type) {
101   }
102  
103   disk_stat_t *get_disk_stats(int *entries){
104 +        VECTOR_DECLARE_STATIC(disk_stats, disk_stat_t, 10,
105 +                              disk_stat_init, disk_stat_destroy);
106  
118        static disk_stat_t *disk_stats;
119        static int watermark=-1;
120
107          int valid_type;
108          int num_disks=0;
109   #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
# Line 139 | Line 125 | disk_stat_t *get_disk_stats(int *entries){
125          struct statfs *mp;
126   #endif
127  
142        if(watermark==-1){
143                disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
144                if(disk_stats==NULL){
145                        return NULL;
146                }
147                watermark=START_VAL;
148                init_disk_stat(0, watermark-1, disk_stats);
149        }
128   #ifdef ALLBSD
129          nummnt=getmntinfo(&mp , MNT_LOCAL);
130          if (nummnt<=0){
# Line 181 | Line 159 | disk_stat_t *get_disk_stats(int *entries){
159   #endif
160  
161                  if(valid_type){
162 <                        if(num_disks>watermark-1){
163 <                                disk_ptr=disk_stats;
186 <                                if((disk_stats=realloc(disk_stats, (watermark*2 * sizeof(disk_stat_t))))==NULL){
187 <                                        disk_stats=disk_ptr;
188 <                                        return NULL;
189 <                                }
190 <
191 <                                watermark=watermark*2;
192 <                                init_disk_stat(num_disks, watermark-1, disk_stats);
162 >                        if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
163 >                                return NULL;
164                          }
194
165                          disk_ptr=disk_stats+num_disks;
166 +
167   #ifdef ALLBSD
168 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
168 >                        if (update_string(&disk_ptr->device_name, mp->f_mntfromname) == NULL) {
169                                  return NULL;
170                          }
171 <
201 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
171 >                        if (update_string(&disk_ptr->fs_type, mp->f_fstypename) == NULL) {
172                                  return NULL;
173                          }
174 <
205 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
174 >                        if (update_string(&disk_ptr->mnt_point, mp->f_mntonname) == NULL) {
175                                  return NULL;
176                          }
177  
# Line 216 | Line 185 | disk_stat_t *get_disk_stats(int *entries){
185                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
186   #endif
187   #if defined(LINUX) || defined(CYGWIN)
188 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
188 >                        if (update_string(&disk_ptr->device_name, mp->mnt_fsname) == NULL) {
189                                  return NULL;
190                          }
191                                  
192 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->mnt_type))==NULL){    
192 >                        if (update_string(&disk_ptr->fs_type, mp->mnt_type) == NULL) {  
193                                  return NULL;
194                          }
195  
196 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->mnt_dir))==NULL){
196 >                        if (update_string(&disk_ptr->mnt_point, mp->mnt_dir) == NULL) {
197                                  return NULL;
198                          }
199                          disk_ptr->size = (long long)fs.f_bsize * (long long)fs.f_blocks;
# Line 238 | Line 207 | disk_stat_t *get_disk_stats(int *entries){
207   #endif
208  
209   #ifdef SOLARIS
241                        /* Memory leak in event of realloc failing */
210                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
211                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
212                           * be upwards of a k each
213                           */
214 <                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp.mnt_special))==NULL){
214 >                        if (update_string(&disk_ptr->device_name, mp.mnt_special) == NULL) {
215                                  return NULL;
216                          }
217  
218 <                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp.mnt_fstype))==NULL){
218 >                        if (update_string(&disk_ptr->fs_type, mp.mnt_fstype) == NULL) {
219                                  return NULL;
220                          }
221          
222 <                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp.mnt_mountp))==NULL){
222 >                        if (update_string(&disk_ptr->mnt_point, mp.mnt_mountp) == NULL) {
223                                  return NULL;
224                          }
225                          
# Line 281 | Line 249 | disk_stat_t *get_disk_stats(int *entries){
249          return disk_stats;
250  
251   }
284 void diskio_stat_init(int start, int end, diskio_stat_t *diskio_stats){
252  
253 <        for(diskio_stats+=start; start<end; start++){
254 <                diskio_stats->disk_name=NULL;
288 <                
289 <                diskio_stats++;
290 <        }
253 > static void diskio_stat_init(diskio_stat_t *d) {
254 >        d->disk_name = NULL;
255   }
256  
257 < diskio_stat_t *diskio_stat_malloc(int needed_entries, int *cur_entries, diskio_stat_t *diskio_stats){
258 <
295 <        if(diskio_stats==NULL){
296 <
297 <                if((diskio_stats=malloc(needed_entries * sizeof(diskio_stat_t)))==NULL){
298 <                        return NULL;
299 <                }
300 <                diskio_stat_init(0, needed_entries, diskio_stats);
301 <                *cur_entries=needed_entries;
302 <
303 <                return diskio_stats;
304 <        }
305 <
306 <
307 <        if(*cur_entries<needed_entries){
308 <                diskio_stats=realloc(diskio_stats, (sizeof(diskio_stat_t)*needed_entries));
309 <                if(diskio_stats==NULL){
310 <                        return NULL;
311 <                }
312 <                diskio_stat_init(*cur_entries, needed_entries, diskio_stats);
313 <                *cur_entries=needed_entries;
314 <        }
315 <
316 <        return diskio_stats;
257 > static void diskio_stat_destroy(diskio_stat_t *d) {
258 >        free(d->disk_name);
259   }
260  
261 < static diskio_stat_t *diskio_stats=NULL;        
262 < static int num_diskio=0;        
261 > VECTOR_DECLARE_STATIC(diskio_stats, diskio_stat_t, 10,
262 >                      diskio_stat_init, diskio_stat_destroy);
263  
264   #ifdef LINUX
265   typedef struct {
# Line 327 | Line 269 | typedef struct {
269   #endif
270  
271   diskio_stat_t *get_diskio_stats(int *entries){
272 <
331 <        static int sizeof_diskio_stats=0;
272 >        int num_diskio;
273   #ifndef LINUX
274          diskio_stat_t *diskio_stats_ptr;
275   #endif
# Line 343 | Line 284 | diskio_stat_t *get_diskio_stats(int *entries){
284          char *line_ptr;
285          int major, minor;
286          int has_pp_stats = 1;
287 <        static partition *parts = NULL;
347 <        static int alloc_parts = 0;
287 >        VECTOR_DECLARE_STATIC(parts, partition, 16, NULL, NULL);
288          int i, n;
289          time_t now;
290          const char *format;
# Line 459 | Line 399 | diskio_stat_t *get_diskio_stats(int *entries){
399                          continue;
400                  }
401  
402 <                diskio_stats = diskio_stat_malloc(num_diskio + 1,
463 <                                                  &sizeof_diskio_stats,
464 <                                                  diskio_stats);
465 <                if (diskio_stats == NULL) {
402 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
403                          return NULL;
404                  }
405                  diskio_stats_ptr = diskio_stats + num_diskio;
# Line 524 | Line 461 | diskio_stat_t *get_diskio_stats(int *entries){
461   #else
462                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
463   #endif
464 <                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
464 >
465 >                if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
466                          return NULL;
467                  }
468                  diskio_stats_ptr=diskio_stats+num_diskio;
# Line 559 | Line 497 | diskio_stat_t *get_diskio_stats(int *entries){
497                          if((kstat_read(kc, ksp, &kios))==-1){  
498                          }
499                          
500 <                        if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
500 >                        if (VECTOR_RESIZE(diskio_stats, num_diskio + 1) < 0) {
501                                  kstat_close(kc);
502                                  return NULL;
503                          }
# Line 590 | Line 528 | diskio_stat_t *get_diskio_stats(int *entries){
528             the same format. */
529  
530          f = fopen("/proc/diskstats", "r");
531 <        format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld";
531 >        format = " %d %d %99s %*d %*d %lld %*d %*d %*d %lld";
532          if (f == NULL) {
533                  f = fopen("/proc/partitions", "r");
534 <                format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld";
534 >                format = " %d %d %*d %99s %*d %*d %lld %*d %*d %*d %lld";
535          }
536          if (f == NULL) goto out;
537          now = time(NULL);
538  
539          while ((line_ptr = f_read_line(f, "")) != NULL) {
540 <                char name[20];
540 >                char name[100];
541                  char *s;
542                  long long rsect, wsect;
543  
# Line 620 | Line 558 | diskio_stat_t *get_diskio_stats(int *entries){
558                          wsect = 0;
559                  }
560  
561 <                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
562 <                        diskio_stats);
625 <                if (diskio_stats == NULL) goto out;
626 <                if (n >= alloc_parts) {
627 <                        alloc_parts += 16;
628 <                        parts = realloc(parts, alloc_parts * sizeof *parts);
629 <                        if (parts == NULL) {
630 <                                alloc_parts = 0;
631 <                                goto out;
632 <                        }
561 >                if (VECTOR_RESIZE(diskio_stats, n + 1) < 0) {
562 >                        goto out;
563                  }
564 +                if (VECTOR_RESIZE(parts, n + 1) < 0) {
565 +                        goto out;
566 +                }
567  
568                  if (diskio_stats[n].disk_name != NULL)
569                          free(diskio_stats[n].disk_name);
# Line 729 | Line 662 | out:
662   }
663  
664   diskio_stat_t *get_diskio_stats_diff(int *entries){
665 <        static diskio_stat_t *diff = NULL;
666 <        static int diff_count = 0;
667 <        diskio_stat_t *src, *dest;
668 <        int i, j, new_count;
665 >        VECTOR_DECLARE_STATIC(diff, diskio_stat_t, 1,
666 >                              diskio_stat_init, diskio_stat_destroy);
667 >        diskio_stat_t *src = NULL, *dest;
668 >        int i, j, diff_count, new_count;
669  
670          if (diskio_stats == NULL) {
671                  /* No previous stats, so we can't calculate a difference. */
# Line 740 | Line 673 | diskio_stat_t *get_diskio_stats_diff(int *entries){
673          }
674  
675          /* Resize the results array to match the previous stats. */
676 <        diff = diskio_stat_malloc(num_diskio, &diff_count, diff);
677 <        if (diff == NULL) {
676 >        diff_count = VECTOR_SIZE(diskio_stats);
677 >        if (VECTOR_RESIZE(diff, diff_count) < 0) {
678                  return NULL;
679          }
680  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines