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.44 by pajs, Fri Jan 9 10:06:01 2004 UTC vs.
Revision 1.51 by tdb, Thu Feb 12 23:04:52 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 64 | Line 67
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 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 +        struct diskstats *stats;
366 + #endif
367 + #ifdef NETBSD
368 + #define MIBSIZE 3
369 + #endif
370 + #ifdef OPENBSD
371 + #define MIBSIZE 2
372 + #endif
373 + #if defined(NETBSD) || defined(OPENBSD)
374          int num_disks, i;
375 <        int mib[3];
375 >        int mib[MIBSIZE];
376          size_t size;
377   #endif
378  
379          num_diskio=0;
380  
381 < #ifdef NETBSD
381 > #if defined(NETBSD) || defined(OPENBSD)
382          mib[0] = CTL_HW;
383          mib[1] = HW_DISKSTATS;
384 + #ifdef NETBSD
385          mib[2] = sizeof(struct disk_sysctl);
386 + #endif
387  
388 <        if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
388 >        if (sysctl(mib, MIBSIZE, NULL, &size, NULL, 0) < 0) {
389                  return NULL;
390          }
391 +
392 + #ifdef NETBSD
393          num_disks = size / sizeof(struct disk_sysctl);
394 + #else
395 +        num_disks = size / sizeof(struct diskstats);
396 + #endif
397  
398          stats = malloc(size);
399          if (stats == NULL) {
400                  return NULL;
401          }
402  
403 <        if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) {
403 >        if (sysctl(mib, MIBSIZE, stats, &size, NULL, 0) < 0) {
404                  return NULL;
405          }
406  
407          for (i = 0; i < num_disks; i++) {
408                  u_int64_t rbytes, wbytes;
409  
410 + #ifdef NETBSD
411   #ifdef HAVE_DK_RBYTES
412                  rbytes = stats[i].dk_rbytes;
413                  wbytes = stats[i].dk_wbytes;
# Line 393 | Line 415 | diskio_stat_t *get_diskio_stats(int *entries){
415                  /* Before 1.7, NetBSD merged reads and writes. */
416                  rbytes = wbytes = stats[i].dk_bytes;
417   #endif
418 + #else
419 +                rbytes = wbytes = stats[i].ds_bytes;
420 + #endif
421  
422                  /* Don't keep stats for disks that have never been used. */
423                  if (rbytes == 0 && wbytes == 0) {
# Line 412 | Line 437 | diskio_stat_t *get_diskio_stats(int *entries){
437                  if (diskio_stats_ptr->disk_name != NULL) {
438                          free(diskio_stats_ptr->disk_name);
439                  }
440 + #ifdef NETBSD
441                  diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
442 + #else
443 +                asprintf((&diskio_stats_ptr->disk_name), "%s%d", "disk", i);
444 + #endif
445                  diskio_stats_ptr->systime = time(NULL);
446          
447                  num_diskio++;  
# Line 576 | Line 605 | diskio_stat_t *get_diskio_stats(int *entries){
605                  n++;
606          }
607  
608 <        if(f != NULL) fclose(f);
608 >        fclose(f);
609 >        f = NULL;
610  
581        /* This code relies on /proc/partions existing, and it all being malloc'ed above.
582
611          if (!has_pp_stats) {
612 <                /* This is an older kernel without stats in /proc/partitions.
613 <                   Read what we can from /proc/stat instead. */
612 >                /* This is an older kernel where /proc/partitions doesn't
613 >                   contain stats. Read what we can from /proc/stat instead, and
614 >                   fill in the appropriate bits of the list allocated above. */
615  
616                  f = fopen("/proc/stat", "r");
617                  if (f == NULL) goto out;
# Line 661 | Line 690 | out:
690   }
691  
692   diskio_stat_t *get_diskio_stats_diff(int *entries){
693 <        static diskio_stat_t *diskio_stats_diff=NULL;
694 <        static int sizeof_diskio_stats_diff=0;
695 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
696 <        int disks, x, y;
693 >        static diskio_stat_t *diff = NULL;
694 >        static int diff_count = 0;
695 >        diskio_stat_t *src, *dest;
696 >        int i, j, new_count;
697  
698 <        if(diskio_stats==NULL){
699 <                diskio_stats_ptr=get_diskio_stats(&disks);
700 <                *entries=disks;
672 <                return diskio_stats_ptr;
698 >        if (diskio_stats == NULL) {
699 >                /* No previous stats, so we can't calculate a difference. */
700 >                return get_diskio_stats(entries);
701          }
702  
703 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
704 <        if(diskio_stats_diff==NULL){
703 >        /* Resize the results array to match the previous stats. */
704 >        diff = diskio_stat_malloc(num_diskio, &diff_count, diff);
705 >        if (diff == NULL) {
706                  return NULL;
707          }
708  
709 <        diskio_stats_diff_ptr=diskio_stats_diff;
710 <        diskio_stats_ptr=diskio_stats;
709 >        /* Copy the previous stats into the result. */
710 >        for (i = 0; i < diff_count; i++) {
711 >                src = &diskio_stats[i];
712 >                dest = &diff[i];
713  
714 <        for(disks=0;disks<num_diskio;disks++){
715 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
685 <                        free(diskio_stats_diff_ptr->disk_name);
714 >                if (dest->disk_name != NULL) {
715 >                        free(dest->disk_name);
716                  }
717 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
718 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
719 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
720 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
717 >                dest->disk_name = strdup(src->disk_name);
718 >                dest->read_bytes = src->read_bytes;
719 >                dest->write_bytes = src->write_bytes;
720 >                dest->systime = src->systime;
721 >        }
722  
723 <                diskio_stats_diff_ptr++;
724 <                diskio_stats_ptr++;
723 >        /* Get a new set of stats. */
724 >        if (get_diskio_stats(&new_count) == NULL) {
725 >                return NULL;
726          }
727  
728 <        diskio_stats_ptr=get_diskio_stats(&disks);
729 <        diskio_stats_diff_ptr=diskio_stats_diff;
728 >        /* For each previous stat... */
729 >        for (i = 0; i < diff_count; i++) {
730 >                dest = &diff[i];
731  
732 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
733 <
734 <                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
735 <                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
736 <                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
737 <                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
738 <                }else{
706 <                        diskio_stats_ptr=diskio_stats;
707 <                        for(y=0;y<disks;y++){
708 <                                if((strcmp(diskio_stats_diff_ptr->disk_name, diskio_stats_ptr->disk_name))==0){
709 <                                        diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes-diskio_stats_diff_ptr->read_bytes;
710 <                                        diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes-diskio_stats_diff_ptr->write_bytes;
711 <                                        diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime-diskio_stats_diff_ptr->systime;
712 <
713 <                                        break;
714 <                                }
715 <                                
716 <                                diskio_stats_ptr++;
732 >                /* ... find the corresponding new stat ... */
733 >                for (j = 0; j < new_count; j++) {
734 >                        /* Try the new stat in the same position first,
735 >                           since that's most likely to be it. */
736 >                        src = &diskio_stats[(i + j) % new_count];
737 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
738 >                                break;
739                          }
740                  }
741 +                if (j == new_count) {
742 +                        /* No match found. */
743 +                        continue;
744 +                }
745  
746 <                diskio_stats_ptr++;
747 <                diskio_stats_diff_ptr++;        
748 <
746 >                /* ... and subtract the previous stat from it to get the
747 >                   difference. */
748 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
749 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
750 >                dest->systime = src->systime - dest->systime;
751          }
752 <        
753 <        *entries=sizeof_diskio_stats_diff;
754 <        return diskio_stats_diff;
752 >
753 >        *entries = diff_count;
754 >        return diff;
755   }
756 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines