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.16 by pajs, Fri Apr 4 14:25:26 2003 UTC vs.
Revision 1.26 by tdb, Wed Oct 8 09:20:56 2003 UTC

# Line 1 | Line 1
1   /*
2   * i-scream central monitoring system
3 < * http://www.i-scream.org.uk
4 < * Copyright (C) 2000-2002 i-scream
3 > * http://www.i-scream.org
4 > * Copyright (C) 2000-2003 i-scream
5   *
6   * This program is free software; you can redistribute it and/or
7   * modify it under the terms of the GNU General Public License
# Line 22 | Line 22
22   #include "config.h"
23   #endif
24  
25 + #include <stdio.h>
26   #include <stdlib.h>
27   #include <string.h>
28   #include "statgrab.h"
29  
30   #ifdef SOLARIS
30 #include <stdio.h>
31   #include <sys/mnttab.h>
32   #include <sys/statvfs.h>
33   #include <kstat.h>
# Line 35 | Line 35
35   #endif
36  
37   #ifdef LINUX
38 #include <stdio.h>
38   #include <sys/vfs.h>
39   #include <mntent.h>
40   #include "tools.h"
# Line 46 | Line 45
45   #include <sys/param.h>
46   #include <sys/ucred.h>
47   #include <sys/mount.h>
48 + #include <sys/dkstat.h>
49 + #include <devstat.h>
50   #define VALID_FS_TYPES {"ufs", "mfs"}
51   #endif
52   #define START_VAL 1
# Line 295 | Line 296 | diskio_stat_t *diskio_stat_malloc(int needed_entries,
296   static diskio_stat_t *diskio_stats=NULL;        
297   static int num_diskio=0;        
298  
299 + #ifdef LINUX
300 + typedef struct {
301 +        int major;
302 +        int minor;
303 + } partition;
304 + #endif
305 +
306   diskio_stat_t *get_diskio_stats(int *entries){
307  
308          static int sizeof_diskio_stats=0;
# Line 310 | Line 318 | diskio_stat_t *get_diskio_stats(int *entries){
318          char *line_ptr;
319          int major, minor;
320          char dev_letter;
321 +        int has_pp_stats = 1;
322 +        static partition *parts = NULL;
323 +        static int alloc_parts = 0;
324 +        int i, n;
325 +        time_t now;
326   #endif
327 + #ifdef FREEBSD
328 +        static struct statinfo stats;
329 +        static int stats_init = 0;
330 +        int counter;
331 +        struct device_selection *dev_sel = NULL;
332 +        int n_selected, n_selections;
333 +        long sel_gen;
334 +        struct devstat *dev_ptr;
335 + #endif
336          num_diskio=0;
337  
338 + #ifdef FREEBSD
339 +        if (!stats_init) {
340 +                stats.dinfo=malloc(sizeof(struct devinfo));
341 +                bzero(stats.dinfo, sizeof(struct devinfo));
342 +                if(stats.dinfo==NULL) return NULL;
343 +                stats_init = 1;
344 +        }
345 +        if ((getdevs(&stats)) < 0) return NULL;
346 +        /* Not aware of a get all devices, so i said 999. If we ever
347 +         * find a machine with more than 999 disks, then i'll change
348 +         * this number :)
349 +         */
350 +        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;
351 +
352 +        for(counter=0;counter<stats.dinfo->numdevs;counter++){
353 +                dev_ptr=&stats.dinfo->devices[dev_sel[counter].position];
354 +
355 +                /* Throw away devices that have done nothing, ever.. Eg "odd"
356 +                 * devices.. like mem, proc.. and also doesn't report floppy
357 +                 * drives etc unless they are doing stuff :)
358 +                 */
359 +                if((dev_ptr->bytes_read==0) && (dev_ptr->bytes_written==0)) continue;
360 +                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
361 +                        return NULL;
362 +                }
363 +                diskio_stats_ptr=diskio_stats+num_diskio;
364 +                
365 +                diskio_stats_ptr->read_bytes=dev_ptr->bytes_read;
366 +                diskio_stats_ptr->write_bytes=dev_ptr->bytes_written;
367 +                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
368 +                asprintf((&diskio_stats_ptr->disk_name), "%s%d", dev_ptr->device_name, dev_ptr->unit_number);
369 +                diskio_stats_ptr->systime=time(NULL);
370 +
371 +                num_diskio++;
372 +        }
373 +        free(dev_sel);
374 +
375 + #endif
376   #ifdef SOLARIS
377          if ((kc = kstat_open()) == NULL) {
378                  return NULL;
# Line 349 | Line 409 | diskio_stat_t *get_diskio_stats(int *entries){
409   #endif
410  
411   #ifdef LINUX
412 <        f=fopen("/proc/stat", "r");
413 <        if(f==NULL){
414 <                *entries=0;
415 <                fclose(f);
416 <                return NULL;
417 <        }
418 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
419 <                *entries=0;
420 <                fclose(f);
421 <                return NULL;
422 <        }
423 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
424 <                line_ptr++;
425 <                if(*line_ptr=='\0'){
426 <                        break;
412 >        num_diskio = 0;
413 >        n = 0;
414 >
415 >        /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
416 >           have statistics in here too, so we can use those directly. */
417 >
418 >        f = fopen("/proc/partitions", "r");
419 >        if (f == NULL) goto out;
420 >        now = time(NULL);
421 >
422 >        while ((line_ptr = f_read_line(f, "")) != NULL) {
423 >                char name[20];
424 >                char *s;
425 >                long long rsect, wsect;
426 >
427 >                int nr = sscanf(line_ptr,
428 >                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
429 >                        &major, &minor, name, &rsect, &wsect);
430 >                if (nr < 3) continue;
431 >                if (nr < 5) {
432 >                        has_pp_stats = 0;
433 >                        rsect = 0;
434 >                        wsect = 0;
435                  }
368                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
369                        fclose(f);
370                        *entries=0;
371                        return NULL;
372                }
373                diskio_stats_ptr=diskio_stats+num_diskio;
436  
437 +                /* Skip device names ending in numbers, since they're
438 +                   partitions. */
439 +                s = name;
440 +                while (*s != '\0') s++;
441 +                --s;
442 +                if (*s >= '0' && *s <= '9') continue;
443  
444 <                if((sscanf(line_ptr, "(%d,%d):(%*d, %*d, %lld, %*d, %lld)", \
445 <                        &major, \
446 <                        &minor, \
447 <                        &diskio_stats_ptr->read_bytes, \
448 <                        &diskio_stats_ptr->write_bytes))!=4) {
449 <                                continue;
444 >                diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
445 >                        diskio_stats);
446 >                if (diskio_stats == NULL) goto out;
447 >                if (n >= alloc_parts) {
448 >                        alloc_parts += 16;
449 >                        parts = realloc(parts, alloc_parts * sizeof *parts);
450 >                        if (parts == NULL) {
451 >                                alloc_parts = 0;
452 >                                goto out;
453 >                        }
454                  }
455  
456 <                /* We read the number of blocks. Blocks are stored in 512 bytes */
457 <                diskio_stats_ptr->read_bytes=diskio_stats_ptr->read_bytes*512;
458 <                diskio_stats_ptr->write_bytes=diskio_stats_ptr->write_bytes*512;
456 >                if (diskio_stats[n].disk_name != NULL)
457 >                        free(diskio_stats[n].disk_name);
458 >                diskio_stats[n].disk_name = strdup(name);
459 >                diskio_stats[n].read_bytes = rsect * 512;
460 >                diskio_stats[n].write_bytes = wsect * 512;
461 >                diskio_stats[n].systime = now;
462 >                parts[n].major = major;
463 >                parts[n].minor = minor;
464  
465 <                if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
465 >                n++;
466 >        }
467  
468 <                switch(major){
469 <                        case 2:
470 <                                if(minor==0){
393 <                                        diskio_stats_ptr->disk_name=strdup("fd0");
394 <                                }
395 <                                break;
468 >        if (!has_pp_stats) {
469 >                /* This is an older kernel without stats in /proc/partitions.
470 >                   Read what we can from /proc/stat instead. */
471  
472 <                        case 3:
473 <                                if(minor==0){
474 <                                        diskio_stats_ptr->disk_name=strdup("hda");
475 <                                }else{
476 <                                        diskio_stats_ptr->disk_name=strdup("hdb");
477 <                                }
478 <                                break;
472 >                f = fopen("/proc/stat", "r");
473 >                if (f == NULL) goto out;
474 >                now = time(NULL);
475 >        
476 >                line_ptr = f_read_line(f, "disk_io:");
477 >                if (line_ptr == NULL) goto out;
478 >        
479 >                while((line_ptr=strchr(line_ptr, ' '))!=NULL){
480 >                        long long rsect, wsect;
481  
482 +                        if (*++line_ptr == '\0') break;
483 +        
484 +                        if((sscanf(line_ptr,
485 +                                "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
486 +                                &major, &minor, &rsect, &wsect)) != 4) {
487 +                                        continue;
488 +                        }
489 +
490 +                        /* Find the corresponding device from earlier.
491 +                           Just to add to the fun, "minor" is actually the disk
492 +                           number, not the device minor, so we need to figure
493 +                           out the real minor number based on the major!
494 +                           This list is not exhaustive; if you're running
495 +                           an older kernel you probably don't have fancy
496 +                           I2O hardware anyway... */
497 +                        switch (major) {
498 +                        case 3:
499 +                        case 21:
500                          case 22:
501 <                                if(minor==0){
502 <                                        diskio_stats_ptr->disk_name=strdup("hdc");
503 <                                }else{
504 <                                        diskio_stats_ptr->disk_name=strdup("hdd");
505 <                                }
501 >                        case 33:
502 >                        case 34:
503 >                        case 36:
504 >                        case 56:
505 >                        case 57:
506 >                        case 88:
507 >                        case 89:
508 >                        case 90:
509 >                        case 91:
510 >                                minor *= 64;
511                                  break;
512 <                        case 8:
513 <                                dev_letter='a'+(minor/16);
414 <                                diskio_stats_ptr->disk_name=malloc(4);
415 <                                snprintf(diskio_stats_ptr->disk_name, 4, "sd%c", dev_letter);
512 >                        case 9:
513 >                        case 43:
514                                  break;
515                          default:
516 <                                /* I have no idea what it is then :) */
419 <                                diskio_stats_ptr->disk_name=malloc(16);
420 <                                snprintf(diskio_stats_ptr->disk_name, 16, "%d %d", major, minor);
516 >                                minor *= 16;
517                                  break;
518 <                }
518 >                        }
519 >                        for (i = 0; i < n; i++) {
520 >                                if (major == parts[i].major
521 >                                        && minor == parts[i].minor)
522 >                                        break;
523 >                        }
524 >                        if (i == n) continue;
525  
526 <                diskio_stats_ptr->systime=time(NULL);
527 <                num_diskio++;
526 >                        /* We read the number of blocks. Blocks are stored in
527 >                           512 bytes */
528 >                        diskio_stats[i].read_bytes = rsect * 512;
529 >                        diskio_stats[i].write_bytes = wsect * 512;
530 >                        diskio_stats[i].systime = now;
531 >                }
532          }
533  
534 <        fclose(f);
534 >        num_diskio = n;
535 > out:
536 >        if (f != NULL) fclose(f);
537  
538   #endif
539          *entries=num_diskio;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines