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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines