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.43 by tdb, Thu Nov 20 12:13:12 2003 UTC vs.
Revision 1.53 by tdb, Mon Feb 16 14:55:32 2004 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * i-scream central monitoring system
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
4 > * Copyright (C) 2000-2004 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
8 < * as published by the Free Software Foundation; either version 2
9 < * of the License, or (at your option) any later version.
6 > * This library is free software; you can redistribute it and/or
7 > * modify it under the terms of the GNU Lesser General Public
8 > * License as published by the Free Software Foundation; either
9 > * version 2.1 of the License, or (at your option) any later version.
10   *
11 < * This program is distributed in the hope that it will be useful,
11 > * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 < * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 < * GNU General Public License for more details.
13 > * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 > * Lesser General Public License for more details.
15   *
16 < * You should have received a copy of the GNU General Public License
17 < * along with this program; if not, write to the Free Software
18 < * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16 > * You should have received a copy of the GNU Lesser General Public
17 > * License along with this library; if not, write to the Free Software
18 > * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 > * 02111-1307 USA
20 > *
21 > * $Id$
22   */
23  
24   #ifdef HAVE_CONFIG_H
# Line 58 | Line 61
61   #include <sys/ucred.h>
62   #include <sys/mount.h>
63   #endif
64 < #ifdef FREEBSD
64 > #if defined(FREEBSD) || defined(DFBSD)
65   #include <sys/dkstat.h>
66   #include <devstat.h>
67   #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
68                          "ufs", "mfs"}
69   #endif
70 < #ifdef NETBSD
70 > #if defined(NETBSD) || defined(OPENBSD)
71   #include <sys/param.h>
72   #include <sys/sysctl.h>
73   #include <sys/disk.h>
# Line 346 | Line 349 | diskio_stat_t *get_diskio_stats(int *entries){
349          time_t now;
350          const char *format;
351   #endif
352 < #ifdef FREEBSD
352 > #if defined(FREEBSD) || defined(DFBSD)
353          static struct statinfo stats;
354          static int stats_init = 0;
355          int counter;
# Line 357 | Line 360 | diskio_stat_t *get_diskio_stats(int *entries){
360   #endif
361   #ifdef NETBSD
362          struct disk_sysctl *stats;
363 + #endif
364 + #ifdef OPENBSD
365 +        int diskcount;
366 +        char *disknames, *name, *bufpp;
367 +        char **dk_name;
368 +        struct diskstats *stats;
369 + #endif
370 + #ifdef NETBSD
371 + #define MIBSIZE 3
372 + #endif
373 + #ifdef OPENBSD
374 + #define MIBSIZE 2
375 + #endif
376 + #if defined(NETBSD) || defined(OPENBSD)
377          int num_disks, i;
378 <        int mib[3];
378 >        int mib[MIBSIZE];
379          size_t size;
380   #endif
381  
382          num_diskio=0;
383  
384 < #ifdef NETBSD
384 > #ifdef OPENBSD
385          mib[0] = CTL_HW;
386 +        mib[1] = HW_DISKCOUNT;
387 +
388 +        size = sizeof(diskcount);
389 +        if (sysctl(mib, MIBSIZE, &diskcount, &size, NULL, 0) < 0) {
390 +                return NULL;
391 +        }
392 +
393 +        mib[0] = CTL_HW;
394 +        mib[1] = HW_DISKNAMES;
395 +
396 +        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
397 +                return NULL;
398 +        }
399 +
400 +        disknames = malloc(size);
401 +        if (disknames == NULL) {
402 +                return NULL;
403 +        }
404 +
405 +        if (sysctl(mib, MIBSIZE, disknames, &size, NULL, 0) < 0) {
406 +                return NULL;
407 +        }
408 +
409 +        dk_name = calloc(diskcount, sizeof(char *));
410 +        bufpp = disknames;
411 +        for (i = 0; i < diskcount && (name = strsep(&bufpp, ",")) != NULL; i++) {
412 +                dk_name[i] = name;
413 +        }
414 + #endif
415 +
416 + #if defined(NETBSD) || defined(OPENBSD)
417 +        mib[0] = CTL_HW;
418          mib[1] = HW_DISKSTATS;
419 + #ifdef NETBSD
420          mib[2] = sizeof(struct disk_sysctl);
421 + #endif
422  
423 <        if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
423 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
424                  return NULL;
425          }
426 +
427 + #ifdef NETBSD
428          num_disks = size / sizeof(struct disk_sysctl);
429 + #else
430 +        num_disks = size / sizeof(struct diskstats);
431 + #endif
432  
433          stats = malloc(size);
434          if (stats == NULL) {
435                  return NULL;
436          }
437  
438 <        if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) {
438 >        if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
439                  return NULL;
440          }
441  
442          for (i = 0; i < num_disks; i++) {
443                  u_int64_t rbytes, wbytes;
444  
445 + #ifdef NETBSD
446   #ifdef HAVE_DK_RBYTES
447                  rbytes = stats[i].dk_rbytes;
448                  wbytes = stats[i].dk_wbytes;
# Line 393 | Line 450 | diskio_stat_t *get_diskio_stats(int *entries){
450                  /* Before 1.7, NetBSD merged reads and writes. */
451                  rbytes = wbytes = stats[i].dk_bytes;
452   #endif
453 + #else
454 +                rbytes = wbytes = stats[i].ds_bytes;
455 + #endif
456  
457                  /* Don't keep stats for disks that have never been used. */
458                  if (rbytes == 0 && wbytes == 0) {
# Line 412 | Line 472 | diskio_stat_t *get_diskio_stats(int *entries){
472                  if (diskio_stats_ptr->disk_name != NULL) {
473                          free(diskio_stats_ptr->disk_name);
474                  }
475 + #ifdef NETBSD
476                  diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
477 + #else
478 +                diskio_stats_ptr->disk_name = strdup(dk_name[i]);
479 + #endif
480                  diskio_stats_ptr->systime = time(NULL);
481          
482                  num_diskio++;  
483          }
484  
485          free(stats);
486 + #ifdef OPENBSD
487 +        free(dk_name);
488 +        free(disknames);
489   #endif
490 + #endif
491  
492 < #ifdef FREEBSD
492 > #if defined(FREEBSD) || defined(DFBSD)
493          if (!stats_init) {
494                  stats.dinfo=malloc(sizeof(struct devinfo));
495                  if(stats.dinfo==NULL) return NULL;
# Line 576 | Line 644 | diskio_stat_t *get_diskio_stats(int *entries){
644                  n++;
645          }
646  
647 +        fclose(f);
648 +        f = NULL;
649 +
650          if (!has_pp_stats) {
651 <                /* This is an older kernel without stats in /proc/partitions.
652 <                   Read what we can from /proc/stat instead. */
651 >                /* This is an older kernel where /proc/partitions doesn't
652 >                   contain stats. Read what we can from /proc/stat instead, and
653 >                   fill in the appropriate bits of the list allocated above. */
654  
655                  f = fopen("/proc/stat", "r");
656                  if (f == NULL) goto out;
# Line 657 | Line 729 | out:
729   }
730  
731   diskio_stat_t *get_diskio_stats_diff(int *entries){
732 <        static diskio_stat_t *diskio_stats_diff=NULL;
733 <        static int sizeof_diskio_stats_diff=0;
734 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
735 <        int disks, x, y;
732 >        static diskio_stat_t *diff = NULL;
733 >        static int diff_count = 0;
734 >        diskio_stat_t *src, *dest;
735 >        int i, j, new_count;
736  
737 <        if(diskio_stats==NULL){
738 <                diskio_stats_ptr=get_diskio_stats(&disks);
739 <                *entries=disks;
668 <                return diskio_stats_ptr;
737 >        if (diskio_stats == NULL) {
738 >                /* No previous stats, so we can't calculate a difference. */
739 >                return get_diskio_stats(entries);
740          }
741  
742 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
743 <        if(diskio_stats_diff==NULL){
742 >        /* Resize the results array to match the previous stats. */
743 >        diff = diskio_stat_malloc(num_diskio, &diff_count, diff);
744 >        if (diff == NULL) {
745                  return NULL;
746          }
747  
748 <        diskio_stats_diff_ptr=diskio_stats_diff;
749 <        diskio_stats_ptr=diskio_stats;
748 >        /* Copy the previous stats into the result. */
749 >        for (i = 0; i < diff_count; i++) {
750 >                src = &diskio_stats[i];
751 >                dest = &diff[i];
752  
753 <        for(disks=0;disks<num_diskio;disks++){
754 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
681 <                        free(diskio_stats_diff_ptr->disk_name);
753 >                if (dest->disk_name != NULL) {
754 >                        free(dest->disk_name);
755                  }
756 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
757 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
758 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
759 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
756 >                dest->disk_name = strdup(src->disk_name);
757 >                dest->read_bytes = src->read_bytes;
758 >                dest->write_bytes = src->write_bytes;
759 >                dest->systime = src->systime;
760 >        }
761  
762 <                diskio_stats_diff_ptr++;
763 <                diskio_stats_ptr++;
762 >        /* Get a new set of stats. */
763 >        if (get_diskio_stats(&new_count) == NULL) {
764 >                return NULL;
765          }
766  
767 <        diskio_stats_ptr=get_diskio_stats(&disks);
768 <        diskio_stats_diff_ptr=diskio_stats_diff;
767 >        /* For each previous stat... */
768 >        for (i = 0; i < diff_count; i++) {
769 >                dest = &diff[i];
770  
771 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
772 <
773 <                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
774 <                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
775 <                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
776 <                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
777 <                }else{
702 <                        diskio_stats_ptr=diskio_stats;
703 <                        for(y=0;y<disks;y++){
704 <                                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
705 <                                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
706 <                                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
707 <                                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
708 <
709 <                                        break;
710 <                                }
711 <                                
712 <                                diskio_stats_ptr++;
771 >                /* ... find the corresponding new stat ... */
772 >                for (j = 0; j < new_count; j++) {
773 >                        /* Try the new stat in the same position first,
774 >                           since that's most likely to be it. */
775 >                        src = &diskio_stats[(i + j) % new_count];
776 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
777 >                                break;
778                          }
779                  }
780 +                if (j == new_count) {
781 +                        /* No match found. */
782 +                        continue;
783 +                }
784  
785 <                diskio_stats_ptr++;
786 <                diskio_stats_diff_ptr++;        
787 <
785 >                /* ... and subtract the previous stat from it to get the
786 >                   difference. */
787 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
788 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
789 >                dest->systime = src->systime - dest->systime;
790          }
791 <        
792 <        *entries=sizeof_diskio_stats_diff;
793 <        return diskio_stats_diff;
791 >
792 >        *entries = diff_count;
793 >        return diff;
794   }
795 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines