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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines