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.14 by pajs, Tue Mar 11 12:57:07 2003 UTC vs.
Revision 1.35 by ats, Sun Oct 19 21:06:55 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 <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 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 76 | Line 98 | disk_stat_t *get_disk_stats(int *entries){
98          char *fs_types[] = VALID_FS_TYPES;
99          int x, valid_type;
100          int num_disks=0;
101 + #if defined(LINUX) || defined (SOLARIS)
102          FILE *f;
103 + #endif
104  
105          disk_stat_t *disk_ptr;
106  
# Line 88 | Line 112 | disk_stat_t *get_disk_stats(int *entries){
112          struct mntent *mp;
113          struct statfs fs;
114   #endif
115 + #ifdef ALLBSD
116 +        int nummnt;
117 +        struct statfs *mp;
118 + #endif
119  
120          if(watermark==-1){
121                  disk_stats=malloc(START_VAL * sizeof(disk_stat_t));
# Line 97 | 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 ALLBSD
129 +        nummnt=getmntinfo(&mp , MNT_LOCAL);
130 +        if (nummnt<=0){
131 +                return NULL;
132 +        }
133 +        for(;nummnt--; mp++){
134 +                valid_type=0;
135 +                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
136 +                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
137 +                                valid_type=1;
138 +                                break;
139 +                        }
140 +                }
141 + #endif
142 +
143   #ifdef LINUX
144          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
145                  return NULL;
# Line 146 | Line 189 | disk_stat_t *get_disk_stats(int *entries){
189                          }
190  
191                          disk_ptr=disk_stats+num_disks;
192 + #ifdef ALLBSD
193 +                        if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
194 +                                return NULL;
195 +                        }
196 +
197 +                        if((disk_ptr->fs_type=copy_string(disk_ptr->fs_type, mp->f_fstypename))==NULL){
198 +                                return NULL;
199 +                        }
200 +
201 +                        if((disk_ptr->mnt_point=copy_string(disk_ptr->mnt_point, mp->f_mntonname))==NULL){
202 +                                return NULL;
203 +                        }
204 +
205 +                        disk_ptr->size = (long long)mp->f_bsize * (long long) mp->f_blocks;
206 +                        disk_ptr->avail = (long long)mp->f_bsize * (long long) mp->f_bavail;
207 +                        disk_ptr->used = (disk_ptr->size) - ((long long)mp->f_bsize * (long long)mp->f_bfree);
208 +
209 +                        disk_ptr->total_inodes=(long long)mp->f_files;
210 +                        disk_ptr->free_inodes=(long long)mp->f_ffree;
211 +                        /* Freebsd doesn't have a "available" inodes */
212 +                        disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
213 + #endif
214   #ifdef LINUX
215                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
216                                  return NULL;
# Line 201 | Line 266 | disk_stat_t *get_disk_stats(int *entries){
266          *entries=num_disks;    
267  
268          /* If this fails, there is very little i can do about it, so i'll ignore it :) */
269 + #if defined(LINUX) || defined(SOLARIS)
270          fclose(f);
271 + #endif
272  
273          return disk_stats;
274  
# Line 244 | 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 259 | 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 +        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 +        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];
442 +
443 +                /* Throw away devices that have done nothing, ever.. Eg "odd"
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 +
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);
467 +
468 +                num_diskio++;
469 +        }
470 +        free(dev_sel);
471 +
472 + #endif
473   #ifdef SOLARIS
474          if ((kc = kstat_open()) == NULL) {
475                  return NULL;
# Line 298 | 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 <                fclose(f);
513 <                return NULL;
514 <        }
515 <        if((line_ptr=f_read_line(f, "disk_io:"))==NULL){
516 <                *entries=0;
517 <                fclose(f);
518 <                return NULL;
519 <        }
520 <        while((line_ptr=strchr(line_ptr, ' '))!=NULL){
521 <                line_ptr++;
522 <                if(*line_ptr=='\0'){
523 <                        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                  }
317                if((diskio_stats=diskio_stat_malloc(num_diskio+1, &sizeof_diskio_stats, diskio_stats))==NULL){
318                        fclose(f);
319                        *entries=0;
320                        return NULL;
321                }
322                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){
342 <                                        diskio_stats_ptr->disk_name=strdup("fd0");
343 <                                }
344 <                                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);
363 <                                diskio_stats_ptr->disk_name=malloc(4);
364 <                                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 :) */
368 <                                diskio_stats_ptr->disk_name=malloc(16);
369 <                                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 +        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