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.18 by tdb, Sun Aug 24 20:24:09 2003 UTC vs.
Revision 1.34 by ats, Sun Oct 19 12:04:05 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 + #ifdef NETBSD
60 + #include <sys/param.h>
61 + #include <sys/sysctl.h>
62 + #include <sys/disk.h>
63 + #endif
64 +
65   #define START_VAL 1
66  
67   char *copy_string(char *orig_ptr, const char *newtext){
# Line 97 | Line 110 | disk_stat_t *get_disk_stats(int *entries){
110          struct mntent *mp;
111          struct statfs fs;
112   #endif
113 < #ifdef FREEBSD
113 > #ifdef ALLBSD
114          int nummnt;
115          struct statfs *mp;
116   #endif
# Line 110 | Line 123 | disk_stat_t *get_disk_stats(int *entries){
123                  watermark=START_VAL;
124                  init_disk_stat(0, watermark-1, disk_stats);
125          }
126 < #ifdef FREEBSD
126 > #ifdef ALLBSD
127          nummnt=getmntinfo(&mp , MNT_LOCAL);
128          if (nummnt<=0){
129                  return NULL;
# Line 174 | Line 187 | disk_stat_t *get_disk_stats(int *entries){
187                          }
188  
189                          disk_ptr=disk_stats+num_disks;
190 < #ifdef FREEBSD
190 > #ifdef ALLBSD
191                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
192                                  return NULL;
193                          }
# Line 296 | Line 309 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
309   static diskio_stat_t *diskio_stats=NULL;        
310   static int num_diskio=0;        
311  
312 + #ifdef LINUX
313 + typedef struct {
314 +        int major;
315 +        int minor;
316 + } partition;
317 + #endif
318 +
319   diskio_stat_t *get_diskio_stats(int *entries){
320  
321          static int sizeof_diskio_stats=0;
# Line 311 | Line 331 | diskio_stat_t *get_diskio_stats(int *entries){
331          char *line_ptr;
332          int major, minor;
333          char dev_letter;
334 +        int has_pp_stats = 1;
335 +        static partition *parts = NULL;
336 +        static int alloc_parts = 0;
337 +        int i, n;
338 +        time_t now;
339   #endif
340   #ifdef FREEBSD
341 <        struct statinfo stats;
341 >        static struct statinfo stats;
342 >        static int stats_init = 0;
343          int counter;
344          struct device_selection *dev_sel = NULL;
345          int n_selected, n_selections;
346          long sel_gen;
347          struct devstat *dev_ptr;
348   #endif
349 + #ifdef NETBSD
350 +        struct disk_sysctl *stats;
351 +        int num_disks, i;
352 +        int mib[3];
353 +        size_t size;
354 + #endif
355 +
356          num_diskio=0;
357  
358 + #ifdef NETBSD
359 +        mib[0] = CTL_HW;
360 +        mib[1] = HW_DISKSTATS;
361 +        mib[2] = sizeof(struct disk_sysctl);
362 +
363 +        if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
364 +                return NULL;
365 +        }
366 +        num_disks = size / sizeof(struct disk_sysctl);
367 +
368 +        stats = malloc(size);
369 +        if (stats == NULL) {
370 +                return NULL;
371 +        }
372 +
373 +        if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) {
374 +                return NULL;
375 +        }
376 +
377 +        for (i = 0; i < num_disks; i++) {
378 +                u_int64_t rbytes, wbytes;
379 +
380 + #ifdef HAVE_DK_RBYTES
381 +                rbytes = stats[i].dk_rbytes;
382 +                wbytes = stats[i].dk_wbytes;
383 + #else
384 +                /* Before 1.6.1, NetBSD merged reads and writes. */
385 +                rbytes = wbytes = stats[i].dk_bytes;
386 + #endif
387 +
388 +                /* Don't keep stats for disks that have never been used. */
389 +                if (rbytes == 0 && wbytes == 0) {
390 +                        continue;
391 +                }
392 +
393 +                diskio_stats = diskio_stat_malloc(num_diskio + 1,
394 +                                                  &sizeof_diskio_stats,
395 +                                                  diskio_stats);
396 +                if (diskio_stats == NULL) {
397 +                        return NULL;
398 +                }
399 +                diskio_stats_ptr = diskio_stats + num_diskio;
400 +                
401 +                diskio_stats_ptr->read_bytes = rbytes;
402 +                diskio_stats_ptr->write_bytes = wbytes;
403 +                if (diskio_stats_ptr->disk_name != NULL) {
404 +                        free(diskio_stats_ptr->disk_name);
405 +                }
406 +                diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
407 +                diskio_stats_ptr->systime = time(NULL);
408 +        
409 +                num_diskio++;  
410 +        }
411 +
412 +        free(stats);
413 + #endif
414 +
415   #ifdef FREEBSD
416 <        stats.dinfo=malloc(sizeof(struct devinfo));
417 <        if(stats.dinfo==NULL) return NULL;
416 >        if (!stats_init) {
417 >                stats.dinfo=malloc(sizeof(struct devinfo));
418 >                if(stats.dinfo==NULL) return NULL;
419 >                bzero(stats.dinfo, sizeof(struct devinfo));
420 >                stats_init = 1;
421 >        }
422 > #ifdef FREEBSD5
423 >        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
424 >        /* Not aware of a get all devices, so i said 999. If we ever
425 >         * find a machine with more than 999 disks, then i'll change
426 >         * this number :)
427 >         */
428 >        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;
429 > #else
430          if ((getdevs(&stats)) < 0) return NULL;
431          /* Not aware of a get all devices, so i said 999. If we ever
432           * find a machine with more than 999 disks, then i'll change
433           * this number :)
434           */
435          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;
436 + #endif
437  
438          for(counter=0;counter<stats.dinfo->numdevs;counter++){
439                  dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
# Line 339 | Line 442 | diskio_stat_t *get_diskio_stats(int *entries){
442                   * devices.. like mem, proc.. and also doesn't report floppy
443                   * drives etc unless they are doing stuff :)
444                   */
445 + #ifdef FREEBSD5
446 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
447 + #else
448                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
449 + #endif
450                  if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
451                          return NULL;
452                  }
453                  diskio_stats_ptr=diskio_stats+num_diskio;
454 <                
454 >
455 > #ifdef FREEBSD5        
456 >                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
457 >                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
458 > #else
459                  diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
460                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
461 + #endif
462                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
463                  asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
464                  diskio_stats_ptr->systime=time(NULL);
# Line 354 | Line 466 | diskio_stat_t *get_diskio_stats(int *entries){
466                  num_diskio++;
467          }
468          free(dev_sel);
357        free(stats.dinfo);
469  
470   #endif
471   #ifdef SOLARIS
# Line 393 | Line 504 | diskio_stat_t *get_diskio_stats(int *entries){
504   #endif
505  
506   #ifdef LINUX
507 <        f=fopen("/proc/stat", "r");
508 <        if(f==NULL){
509 <                *entries=0;
510 <                fclose(f);
511 <                return NULL;
512 <        }
513 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
514 <                *entries=0;
515 <                fclose(f);
516 <                return NULL;
517 <        }
518 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
519 <                line_ptr++;
520 <                if(*line_ptr=='\0'){
521 <                        break;
507 >        num_diskio = 0;
508 >        n = 0;
509 >
510 >        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
511 >           have statistics in here too, so we can use those directly. */
512 >
513 >        f = fopen("/proc/partitions", "r");
514 >        if (f == NULL) goto out;
515 >        now = time(NULL);
516 >
517 >        while ((line_ptr = f_read_line(f, "")) != NULL) {
518 >                char name[20];
519 >                char *s;
520 >                long long rsect, wsect;
521 >
522 >                int nr = sscanf(line_ptr,
523 >                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
524 >                        &major, &minor, name, &rsect, &wsect);
525 >                if (nr < 3) continue;
526 >                if (nr < 5) {
527 >                        has_pp_stats = 0;
528 >                        rsect = 0;
529 >                        wsect = 0;
530                  }
412                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
413                        fclose(f);
414                        *entries=0;
415                        return NULL;
416                }
417                diskio_stats_ptr=diskio_stats+num_diskio;
531  
532 +                /* Skip device names ending in numbers, since they're
533 +                   partitions. */
534 +                s = name;
535 +                while (*s != '\0') s++;
536 +                --s;
537 +                if (*s >= '0' && *s <= '9') continue;
538  
539 <                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
540 <                        &major, \
541 <                        &minor, \
542 <                        &diskio_stats_ptr->read_bytes, \
543 <                        &diskio_stats_ptr->write_bytes))!=4) {
544 <                                continue;
539 >                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
540 >                        diskio_stats);
541 >                if (diskio_stats == NULL) goto out;
542 >                if (n >= alloc_parts) {
543 >                        alloc_parts += 16;
544 >                        parts = realloc(parts, alloc_parts * sizeof *parts);
545 >                        if (parts == NULL) {
546 >                                alloc_parts = 0;
547 >                                goto out;
548 >                        }
549                  }
550  
551 <                /* We read the number of blocks. Blocks are stored in 512 bytes */
552 <                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
553 <                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
551 >                if (diskio_stats[n].disk_name != NULL)
552 >                        free(diskio_stats[n].disk_name);
553 >                diskio_stats[n].disk_name = strdup(name);
554 >                diskio_stats[n].read_bytes = rsect * 512;
555 >                diskio_stats[n].write_bytes = wsect * 512;
556 >                diskio_stats[n].systime = now;
557 >                parts[n].major = major;
558 >                parts[n].minor = minor;
559  
560 <                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
560 >                n++;
561 >        }
562  
563 <                switch(major){
564 <                        case 2:
565 <                                if(minor==0){
437 <                                        diskio_stats_ptr->disk_name=strdup("fd0");
438 <                                }
439 <                                break;
563 >        if (!has_pp_stats) {
564 >                /* This is an older kernel without stats in /proc/partitions.
565 >                   Read what we can from /proc/stat instead. */
566  
567 <                        case 3:
568 <                                if(minor==0){
569 <                                        diskio_stats_ptr->disk_name=strdup("hda");
570 <                                }else{
571 <                                        diskio_stats_ptr->disk_name=strdup("hdb");
572 <                                }
573 <                                break;
567 >                f = fopen("/proc/stat", "r");
568 >                if (f == NULL) goto out;
569 >                now = time(NULL);
570 >        
571 >                line_ptr = f_read_line(f, "disk_io:");
572 >                if (line_ptr == NULL) goto out;
573 >        
574 >                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
575 >                        long long rsect, wsect;
576  
577 +                        if (*++line_ptr == '\0') break;
578 +        
579 +                        if((sscanf(line_ptr,
580 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
581 +                                &major, &minor, &rsect, &wsect)) != 4) {
582 +                                        continue;
583 +                        }
584 +
585 +                        /* Find the corresponding device from earlier.
586 +                           Just to add to the fun, "minor" is actually the disk
587 +                           number, not the device minor, so we need to figure
588 +                           out the real minor number based on the major!
589 +                           This list is not exhaustive; if you're running
590 +                           an older kernel you probably don't have fancy
591 +                           I2O hardware anyway... */
592 +                        switch (major) {
593 +                        case 3:
594 +                        case 21:
595                          case 22:
596 <                                if(minor==0){
597 <                                        diskio_stats_ptr->disk_name=strdup("hdc");
598 <                                }else{
599 <                                        diskio_stats_ptr->disk_name=strdup("hdd");
600 <                                }
596 >                        case 33:
597 >                        case 34:
598 >                        case 36:
599 >                        case 56:
600 >                        case 57:
601 >                        case 88:
602 >                        case 89:
603 >                        case 90:
604 >                        case 91:
605 >                                minor *= 64;
606                                  break;
607 <                        case 8:
608 <                                dev_letter='a'+(minor/16);
458 <                                diskio_stats_ptr->disk_name=malloc(4);
459 <                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
607 >                        case 9:
608 >                        case 43:
609                                  break;
610                          default:
611 <                                /* I have no idea what it is then :) */
463 <                                diskio_stats_ptr->disk_name=malloc(16);
464 <                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
611 >                                minor *= 16;
612                                  break;
613 <                }
613 >                        }
614 >                        for (i = 0; i < n; i++) {
615 >                                if (major == parts[i].major
616 >                                        && minor == parts[i].minor)
617 >                                        break;
618 >                        }
619 >                        if (i == n) continue;
620  
621 <                diskio_stats_ptr->systime=time(NULL);
622 <                num_diskio++;
621 >                        /* We read the number of blocks. Blocks are stored in
622 >                           512 bytes */
623 >                        diskio_stats[i].read_bytes = rsect * 512;
624 >                        diskio_stats[i].write_bytes = wsect * 512;
625 >                        diskio_stats[i].systime = now;
626 >                }
627          }
628  
629 <        fclose(f);
629 >        num_diskio = n;
630 > out:
631 >        if (f != NULL) fclose(f);
632  
633   #endif
634          *entries=num_diskio;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines