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.19 by ats, Thu Aug 28 21:33:42 2003 UTC vs.
Revision 1.31 by ats, Sun Oct 19 02:03:02 2003 UTC

# Line 35 | Line 35
35   #endif
36  
37   #ifdef LINUX
38 + #include <time.h>
39   #include <sys/vfs.h>
40   #include <mntent.h>
41   #include "tools.h"
42   #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
43   #endif
44  
45 < #ifdef FREEBSD
45 > #ifdef ALLBSD
46   #include <sys/param.h>
47   #include <sys/ucred.h>
48   #include <sys/mount.h>
49 + #define VALID_FS_TYPES {"ufs", "mfs", "ffs"}
50 + #endif
51 + #ifdef FREEBSD
52   #include <sys/dkstat.h>
53   #include <devstat.h>
50 #define VALID_FS_TYPES {"ufs", "mfs"}
54   #endif
55 +
56   #define START_VAL 1
57  
58   char *copy_string(char *orig_ptr, const char *newtext){
# Line 97 | Line 101 | disk_stat_t *get_disk_stats(int *entries){
101          struct mntent *mp;
102          struct statfs fs;
103   #endif
104 < #ifdef FREEBSD
104 > #ifdef ALLBSD
105          int nummnt;
106          struct statfs *mp;
107   #endif
# Line 110 | Line 114 | disk_stat_t *get_disk_stats(int *entries){
114                  watermark=START_VAL;
115                  init_disk_stat(0, watermark-1, disk_stats);
116          }
117 < #ifdef FREEBSD
117 > #ifdef ALLBSD
118          nummnt=getmntinfo(&mp , MNT_LOCAL);
119          if (nummnt<=0){
120                  return NULL;
# Line 174 | Line 178 | disk_stat_t *get_disk_stats(int *entries){
178                          }
179  
180                          disk_ptr=disk_stats+num_disks;
181 < #ifdef FREEBSD
181 > #ifdef ALLBSD
182                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
183                                  return NULL;
184                          }
# Line 296 | Line 300 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
300   static diskio_stat_t *diskio_stats=NULL;        
301   static int num_diskio=0;        
302  
303 + #ifdef LINUX
304 + typedef struct {
305 +        int major;
306 +        int minor;
307 + } partition;
308 + #endif
309 +
310   diskio_stat_t *get_diskio_stats(int *entries){
311  
312          static int sizeof_diskio_stats=0;
# Line 311 | Line 322 | diskio_stat_t *get_diskio_stats(int *entries){
322          char *line_ptr;
323          int major, minor;
324          char dev_letter;
325 +        int has_pp_stats = 1;
326 +        static partition *parts = NULL;
327 +        static int alloc_parts = 0;
328 +        int i, n;
329 +        time_t now;
330   #endif
331   #ifdef FREEBSD
332 <        struct statinfo stats;
332 >        static struct statinfo stats;
333 >        static int stats_init = 0;
334          int counter;
335          struct device_selection *dev_sel = NULL;
336          int n_selected, n_selections;
337          long sel_gen;
338          struct devstat *dev_ptr;
339   #endif
340 + #ifdef NETBSD
341 +        /* FIXME get_diskio_stats NYI on NetBSD.
342 +         * See vmstat/dkstats.c in NetBSD source for examples.
343 +         */
344 + #endif
345          num_diskio=0;
346  
347   #ifdef FREEBSD
348 <        stats.dinfo=malloc(sizeof(struct devinfo));
349 <        if(stats.dinfo==NULL) return NULL;
348 >        if (!stats_init) {
349 >                stats.dinfo=malloc(sizeof(struct devinfo));
350 >                bzero(stats.dinfo, sizeof(struct devinfo));
351 >                if(stats.dinfo==NULL) return NULL;
352 >                stats_init = 1;
353 >        }
354 > #ifdef FREEBSD5
355 >        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
356 >        /* Not aware of a get all devices, so i said 999. If we ever
357 >         * find a machine with more than 999 disks, then i'll change
358 >         * this number :)
359 >         */
360 >        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;
361 > #else
362          if ((getdevs(&stats)) < 0) return NULL;
363          /* Not aware of a get all devices, so i said 999. If we ever
364           * find a machine with more than 999 disks, then i'll change
365           * this number :)
366           */
367          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;
368 + #endif
369  
370          for(counter=0;counter<stats.dinfo->numdevs;counter++){
371                  dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
# Line 339 | Line 374 | diskio_stat_t *get_diskio_stats(int *entries){
374                   * devices.. like mem, proc.. and also doesn't report floppy
375                   * drives etc unless they are doing stuff :)
376                   */
377 + #ifdef FREEBSD5
378 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
379 + #else
380                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
381 + #endif
382                  if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
383                          return NULL;
384                  }
385                  diskio_stats_ptr=diskio_stats+num_diskio;
386 <                
386 >
387 > #ifdef FREEBSD5        
388 >                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
389 >                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
390 > #else
391                  diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
392                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
393 + #endif
394                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
395                  asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
396                  diskio_stats_ptr->systime=time(NULL);
# Line 354 | Line 398 | diskio_stat_t *get_diskio_stats(int *entries){
398                  num_diskio++;
399          }
400          free(dev_sel);
357        free(stats.dinfo);
401  
402   #endif
403   #ifdef SOLARIS
# Line 393 | Line 436 | diskio_stat_t *get_diskio_stats(int *entries){
436   #endif
437  
438   #ifdef LINUX
439 <        f=fopen("/proc/stat", "r");
440 <        if(f==NULL){
441 <                *entries=0;
442 <                return NULL;
443 <        }
444 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
445 <                *entries=0;
446 <                fclose(f);
447 <                return NULL;
448 <        }
449 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
450 <                line_ptr++;
451 <                if(*line_ptr=='\0'){
452 <                        break;
439 >        num_diskio = 0;
440 >        n = 0;
441 >
442 >        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
443 >           have statistics in here too, so we can use those directly. */
444 >
445 >        f = fopen("/proc/partitions", "r");
446 >        if (f == NULL) goto out;
447 >        now = time(NULL);
448 >
449 >        while ((line_ptr = f_read_line(f, "")) != NULL) {
450 >                char name[20];
451 >                char *s;
452 >                long long rsect, wsect;
453 >
454 >                int nr = sscanf(line_ptr,
455 >                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
456 >                        &major, &minor, name, &rsect, &wsect);
457 >                if (nr < 3) continue;
458 >                if (nr < 5) {
459 >                        has_pp_stats = 0;
460 >                        rsect = 0;
461 >                        wsect = 0;
462                  }
411                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
412                        fclose(f);
413                        *entries=0;
414                        return NULL;
415                }
416                diskio_stats_ptr=diskio_stats+num_diskio;
463  
464 +                /* Skip device names ending in numbers, since they're
465 +                   partitions. */
466 +                s = name;
467 +                while (*s != '\0') s++;
468 +                --s;
469 +                if (*s >= '0' && *s <= '9') continue;
470  
471 <                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
472 <                        &major, \
473 <                        &minor, \
474 <                        &diskio_stats_ptr->read_bytes, \
475 <                        &diskio_stats_ptr->write_bytes))!=4) {
476 <                                continue;
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 >                        }
481                  }
482  
483 <                /* We read the number of blocks. Blocks are stored in 512 bytes */
484 <                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
485 <                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
483 >                if (diskio_stats[n].disk_name != NULL)
484 >                        free(diskio_stats[n].disk_name);
485 >                diskio_stats[n].disk_name = strdup(name);
486 >                diskio_stats[n].read_bytes = rsect * 512;
487 >                diskio_stats[n].write_bytes = wsect * 512;
488 >                diskio_stats[n].systime = now;
489 >                parts[n].major = major;
490 >                parts[n].minor = minor;
491  
492 <                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
492 >                n++;
493 >        }
494  
495 <                switch(major){
496 <                        case 2:
497 <                                if(minor==0){
436 <                                        diskio_stats_ptr->disk_name=strdup("fd0");
437 <                                }
438 <                                break;
495 >        if (!has_pp_stats) {
496 >                /* This is an older kernel without stats in /proc/partitions.
497 >                   Read what we can from /proc/stat instead. */
498  
499 <                        case 3:
500 <                                if(minor==0){
501 <                                        diskio_stats_ptr->disk_name=strdup("hda");
502 <                                }else{
503 <                                        diskio_stats_ptr->disk_name=strdup("hdb");
504 <                                }
505 <                                break;
499 >                f = fopen("/proc/stat", "r");
500 >                if (f == NULL) goto out;
501 >                now = time(NULL);
502 >        
503 >                line_ptr = f_read_line(f, "disk_io:");
504 >                if (line_ptr == NULL) goto out;
505 >        
506 >                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
507 >                        long long rsect, wsect;
508  
509 +                        if (*++line_ptr == '\0') break;
510 +        
511 +                        if((sscanf(line_ptr,
512 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
513 +                                &major, &minor, &rsect, &wsect)) != 4) {
514 +                                        continue;
515 +                        }
516 +
517 +                        /* Find the corresponding device from earlier.
518 +                           Just to add to the fun, "minor" is actually the disk
519 +                           number, not the device minor, so we need to figure
520 +                           out the real minor number based on the major!
521 +                           This list is not exhaustive; if you're running
522 +                           an older kernel you probably don't have fancy
523 +                           I2O hardware anyway... */
524 +                        switch (major) {
525 +                        case 3:
526 +                        case 21:
527                          case 22:
528 <                                if(minor==0){
529 <                                        diskio_stats_ptr->disk_name=strdup("hdc");
530 <                                }else{
531 <                                        diskio_stats_ptr->disk_name=strdup("hdd");
532 <                                }
528 >                        case 33:
529 >                        case 34:
530 >                        case 36:
531 >                        case 56:
532 >                        case 57:
533 >                        case 88:
534 >                        case 89:
535 >                        case 90:
536 >                        case 91:
537 >                                minor *= 64;
538                                  break;
539 <                        case 8:
540 <                                dev_letter='a'+(minor/16);
457 <                                diskio_stats_ptr->disk_name=malloc(4);
458 <                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
539 >                        case 9:
540 >                        case 43:
541                                  break;
542                          default:
543 <                                /* I have no idea what it is then :) */
462 <                                diskio_stats_ptr->disk_name=malloc(16);
463 <                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
543 >                                minor *= 16;
544                                  break;
545 <                }
545 >                        }
546 >                        for (i = 0; i < n; i++) {
547 >                                if (major == parts[i].major
548 >                                        && minor == parts[i].minor)
549 >                                        break;
550 >                        }
551 >                        if (i == n) continue;
552  
553 <                diskio_stats_ptr->systime=time(NULL);
554 <                num_diskio++;
553 >                        /* We read the number of blocks. Blocks are stored in
554 >                           512 bytes */
555 >                        diskio_stats[i].read_bytes = rsect * 512;
556 >                        diskio_stats[i].write_bytes = wsect * 512;
557 >                        diskio_stats[i].systime = now;
558 >                }
559          }
560  
561 <        fclose(f);
561 >        num_diskio = n;
562 > out:
563 >        if (f != NULL) fclose(f);
564  
565   #endif
566          *entries=num_diskio;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines