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.30 by ats, Sun Oct 19 00:10:30 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"
# Line 296 | Line 297 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
297   static diskio_stat_t *diskio_stats=NULL;        
298   static int num_diskio=0;        
299  
300 + #ifdef LINUX
301 + typedef struct {
302 +        int major;
303 +        int minor;
304 + } partition;
305 + #endif
306 +
307   diskio_stat_t *get_diskio_stats(int *entries){
308  
309          static int sizeof_diskio_stats=0;
# Line 311 | Line 319 | diskio_stat_t *get_diskio_stats(int *entries){
319          char *line_ptr;
320          int major, minor;
321          char dev_letter;
322 +        int has_pp_stats = 1;
323 +        static partition *parts = NULL;
324 +        static int alloc_parts = 0;
325 +        int i, n;
326 +        time_t now;
327   #endif
328   #ifdef FREEBSD
329 <        struct statinfo stats;
329 >        static struct statinfo stats;
330 >        static int stats_init = 0;
331          int counter;
332          struct device_selection *dev_sel = NULL;
333          int n_selected, n_selections;
# Line 323 | Line 337 | diskio_stat_t *get_diskio_stats(int *entries){
337          num_diskio=0;
338  
339   #ifdef FREEBSD
340 <        stats.dinfo=malloc(sizeof(struct devinfo));
341 <        if(stats.dinfo==NULL) return NULL;
340 >        if (!stats_init) {
341 >                stats.dinfo=malloc(sizeof(struct devinfo));
342 >                bzero(stats.dinfo, sizeof(struct devinfo));
343 >                if(stats.dinfo==NULL) return NULL;
344 >                stats_init = 1;
345 >        }
346 > #ifdef FREEBSD5
347 >        if ((devstat_getdevs(NULL, &stats)) < 0) return NULL;
348 >        /* Not aware of a get all devices, so i said 999. If we ever
349 >         * find a machine with more than 999 disks, then i'll change
350 >         * this number :)
351 >         */
352 >        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;
353 > #else
354          if ((getdevs(&stats)) < 0) return NULL;
355          /* Not aware of a get all devices, so i said 999. If we ever
356           * find a machine with more than 999 disks, then i'll change
357           * this number :)
358           */
359          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;
360 + #endif
361  
362          for(counter=0;counter<stats.dinfo->numdevs;counter++){
363                  dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
# Line 339 | Line 366 | diskio_stat_t *get_diskio_stats(int *entries){
366                   * devices.. like mem, proc.. and also doesn't report floppy
367                   * drives etc unless they are doing stuff :)
368                   */
369 + #ifdef FREEBSD5
370 +                if((dev_ptr->bytes[DEVSTAT_READ]==0) && (dev_ptr->bytes[DEVSTAT_WRITE]==0)) continue;
371 + #else
372                  if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
373 + #endif
374                  if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
375                          return NULL;
376                  }
377                  diskio_stats_ptr=diskio_stats+num_diskio;
378 <                
378 >
379 > #ifdef FREEBSD5        
380 >                diskio_stats_ptr->read_bytes=dev_ptr->bytes[DEVSTAT_READ];
381 >                diskio_stats_ptr->write_bytes=dev_ptr->bytes[DEVSTAT_WRITE];
382 > #else
383                  diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
384                  diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
385 + #endif
386                  if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
387                  asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
388                  diskio_stats_ptr->systime=time(NULL);
# Line 354 | Line 390 | diskio_stat_t *get_diskio_stats(int *entries){
390                  num_diskio++;
391          }
392          free(dev_sel);
357        free(stats.dinfo);
393  
394   #endif
395   #ifdef SOLARIS
# Line 393 | Line 428 | diskio_stat_t *get_diskio_stats(int *entries){
428   #endif
429  
430   #ifdef LINUX
431 <        f=fopen("/proc/stat", "r");
432 <        if(f==NULL){
433 <                *entries=0;
434 <                return NULL;
435 <        }
436 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
437 <                *entries=0;
438 <                fclose(f);
439 <                return NULL;
440 <        }
441 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
442 <                line_ptr++;
443 <                if(*line_ptr=='\0'){
444 <                        break;
431 >        num_diskio = 0;
432 >        n = 0;
433 >
434 >        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
435 >           have statistics in here too, so we can use those directly. */
436 >
437 >        f = fopen("/proc/partitions", "r");
438 >        if (f == NULL) goto out;
439 >        now = time(NULL);
440 >
441 >        while ((line_ptr = f_read_line(f, "")) != NULL) {
442 >                char name[20];
443 >                char *s;
444 >                long long rsect, wsect;
445 >
446 >                int nr = sscanf(line_ptr,
447 >                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
448 >                        &major, &minor, name, &rsect, &wsect);
449 >                if (nr < 3) continue;
450 >                if (nr < 5) {
451 >                        has_pp_stats = 0;
452 >                        rsect = 0;
453 >                        wsect = 0;
454                  }
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;
455  
456 +                /* Skip device names ending in numbers, since they're
457 +                   partitions. */
458 +                s = name;
459 +                while (*s != '\0') s++;
460 +                --s;
461 +                if (*s >= '0' && *s <= '9') continue;
462  
463 <                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
464 <                        &major, \
465 <                        &minor, \
466 <                        &diskio_stats_ptr->read_bytes, \
467 <                        &diskio_stats_ptr->write_bytes))!=4) {
468 <                                continue;
463 >                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
464 >                        diskio_stats);
465 >                if (diskio_stats == NULL) goto out;
466 >                if (n >= alloc_parts) {
467 >                        alloc_parts += 16;
468 >                        parts = realloc(parts, alloc_parts * sizeof *parts);
469 >                        if (parts == NULL) {
470 >                                alloc_parts = 0;
471 >                                goto out;
472 >                        }
473                  }
474  
475 <                /* We read the number of blocks. Blocks are stored in 512 bytes */
476 <                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
477 <                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
475 >                if (diskio_stats[n].disk_name != NULL)
476 >                        free(diskio_stats[n].disk_name);
477 >                diskio_stats[n].disk_name = strdup(name);
478 >                diskio_stats[n].read_bytes = rsect * 512;
479 >                diskio_stats[n].write_bytes = wsect * 512;
480 >                diskio_stats[n].systime = now;
481 >                parts[n].major = major;
482 >                parts[n].minor = minor;
483  
484 <                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
484 >                n++;
485 >        }
486  
487 <                switch(major){
488 <                        case 2:
489 <                                if(minor==0){
436 <                                        diskio_stats_ptr->disk_name=strdup("fd0");
437 <                                }
438 <                                break;
487 >        if (!has_pp_stats) {
488 >                /* This is an older kernel without stats in /proc/partitions.
489 >                   Read what we can from /proc/stat instead. */
490  
491 <                        case 3:
492 <                                if(minor==0){
493 <                                        diskio_stats_ptr->disk_name=strdup("hda");
494 <                                }else{
495 <                                        diskio_stats_ptr->disk_name=strdup("hdb");
496 <                                }
497 <                                break;
491 >                f = fopen("/proc/stat", "r");
492 >                if (f == NULL) goto out;
493 >                now = time(NULL);
494 >        
495 >                line_ptr = f_read_line(f, "disk_io:");
496 >                if (line_ptr == NULL) goto out;
497 >        
498 >                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
499 >                        long long rsect, wsect;
500  
501 +                        if (*++line_ptr == '\0') break;
502 +        
503 +                        if((sscanf(line_ptr,
504 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
505 +                                &major, &minor, &rsect, &wsect)) != 4) {
506 +                                        continue;
507 +                        }
508 +
509 +                        /* Find the corresponding device from earlier.
510 +                           Just to add to the fun, "minor" is actually the disk
511 +                           number, not the device minor, so we need to figure
512 +                           out the real minor number based on the major!
513 +                           This list is not exhaustive; if you're running
514 +                           an older kernel you probably don't have fancy
515 +                           I2O hardware anyway... */
516 +                        switch (major) {
517 +                        case 3:
518 +                        case 21:
519                          case 22:
520 <                                if(minor==0){
521 <                                        diskio_stats_ptr->disk_name=strdup("hdc");
522 <                                }else{
523 <                                        diskio_stats_ptr->disk_name=strdup("hdd");
524 <                                }
520 >                        case 33:
521 >                        case 34:
522 >                        case 36:
523 >                        case 56:
524 >                        case 57:
525 >                        case 88:
526 >                        case 89:
527 >                        case 90:
528 >                        case 91:
529 >                                minor *= 64;
530                                  break;
531 <                        case 8:
532 <                                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);
531 >                        case 9:
532 >                        case 43:
533                                  break;
534                          default:
535 <                                /* 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);
535 >                                minor *= 16;
536                                  break;
537 <                }
537 >                        }
538 >                        for (i = 0; i < n; i++) {
539 >                                if (major == parts[i].major
540 >                                        && minor == parts[i].minor)
541 >                                        break;
542 >                        }
543 >                        if (i == n) continue;
544  
545 <                diskio_stats_ptr->systime=time(NULL);
546 <                num_diskio++;
545 >                        /* We read the number of blocks. Blocks are stored in
546 >                           512 bytes */
547 >                        diskio_stats[i].read_bytes = rsect * 512;
548 >                        diskio_stats[i].write_bytes = wsect * 512;
549 >                        diskio_stats[i].systime = now;
550 >                }
551          }
552  
553 <        fclose(f);
553 >        num_diskio = n;
554 > out:
555 >        if (f != NULL) fclose(f);
556  
557   #endif
558          *entries=num_diskio;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines