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.39 by tdb, Mon Nov 10 21:07:04 2003 UTC vs.
Revision 1.50 by ats, Wed Jan 21 23:46:54 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 25 | Line 28
28   #include <stdio.h>
29   #include <stdlib.h>
30   #include <string.h>
31 + #include <time.h>
32   #include "statgrab.h"
33  
34   #ifdef SOLARIS
# Line 34 | Line 38
38   #define VALID_FS_TYPES {"ufs", "tmpfs"}
39   #endif
40  
41 < #ifdef LINUX
38 < #include <time.h>
39 < #include <sys/vfs.h>
41 > #if defined(LINUX) || defined(CYGWIN)
42   #include <mntent.h>
43 + #include <sys/vfs.h>
44   #include "tools.h"
45 < #ifdef CYGWIN
46 < #define VALID_FS_TYPES {"user"}
47 < #else
45 > #endif
46 >
47 > #ifdef LINUX
48   #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
49                          "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
50                          "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
51                          "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
52                          "umsdos", "vfat", "xfs", "jfs"}
53   #endif
54 +
55 + #ifdef CYGWIN
56 + #define VALID_FS_TYPES {"user"}
57   #endif
58  
59   #ifdef ALLBSD
# Line 95 | Line 101 | void init_disk_stat(int start, int end, disk_stat_t *d
101          }
102   }
103  
104 + int is_valid_fs_type(const char *type) {
105 +        const char *types[] = VALID_FS_TYPES;
106 +        int i;
107 +
108 +        for (i = 0; i < (sizeof types / sizeof *types); i++) {
109 +                if (strcmp(types[i], type) == 0) {
110 +                        return 1;
111 +                }
112 +        }
113 +        return 0;
114 + }
115 +
116   disk_stat_t *get_disk_stats(int *entries){
117  
118          static disk_stat_t *disk_stats;
119          static int watermark=-1;
120  
121 <        char *fs_types[] = VALID_FS_TYPES;
104 <        int x, valid_type;
121 >        int valid_type;
122          int num_disks=0;
123 < #if defined(LINUX) || defined (SOLARIS)
123 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
124          FILE *f;
125   #endif
126  
# Line 113 | Line 130 | disk_stat_t *get_disk_stats(int *entries){
130          struct mnttab mp;
131          struct statvfs fs;
132   #endif
133 < #ifdef LINUX
133 > #if defined(LINUX) || defined(CYGWIN)
134          struct mntent *mp;
135          struct statfs fs;
136   #endif
# Line 136 | Line 153 | disk_stat_t *get_disk_stats(int *entries){
153                  return NULL;
154          }
155          for(;nummnt--; mp++){
156 <                valid_type=0;
140 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
141 <                        if(strcmp(mp->f_fstypename, fs_types[x]) ==0){
142 <                                valid_type=1;
143 <                                break;
144 <                        }
145 <                }
156 >                valid_type = is_valid_fs_type(mp->f_fstypename);
157   #endif
158  
159 < #ifdef LINUX
159 > #if defined(LINUX) || defined(CYGWIN)
160          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
161                  return NULL;
162          }
# Line 155 | Line 166 | disk_stat_t *get_disk_stats(int *entries){
166                          continue;
167                  }      
168  
169 <                valid_type=0;
159 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
160 <                        if(strcmp(mp->mnt_type, fs_types[x]) ==0){
161 <                                valid_type=1;
162 <                                break;
163 <                        }
164 <                }
169 >                valid_type = is_valid_fs_type(mp->mnt_type);
170   #endif
171  
172   #ifdef SOLARIS
# Line 172 | Line 177 | disk_stat_t *get_disk_stats(int *entries){
177                  if ((statvfs(mp.mnt_mountp, &fs)) !=0){
178                          continue;
179                  }
180 <                valid_type=0;
176 <                for(x=0;x<((sizeof(fs_types))/(sizeof(char*)));x++){
177 <                        if(strcmp(mp.mnt_fstype, fs_types[x]) ==0){
178 <                                valid_type=1;
179 <                                break;
180 <                        }
181 <                }
180 >                valid_type = is_valid_fs_type(mp.mnt_fstype);
181   #endif
182  
183                  if(valid_type){
# Line 216 | Line 215 | disk_stat_t *get_disk_stats(int *entries){
215                          /* Freebsd doesn't have a "available" inodes */
216                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
217   #endif
218 < #ifdef LINUX
218 > #if defined(LINUX) || defined(CYGWIN)
219                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
220                                  return NULL;
221                          }
# Line 272 | Line 271 | disk_stat_t *get_disk_stats(int *entries){
271  
272          /* If this fails, there is very little i can do about it, so
273             I'll ignore it :) */
274 < #if defined(LINUX)
274 > #if defined(LINUX) || defined(CYGWIN)
275          endmntent(f);
276   #endif
277   #if defined(SOLARIS)
# Line 507 | Line 506 | diskio_stat_t *get_diskio_stats(int *entries){
506  
507                          if(diskio_stats_ptr->disk_name!=NULL) free(diskio_stats_ptr->disk_name);
508  
509 <                        diskio_stats_ptr->disk_name=strdup(ksp->ks_name);
509 >                        diskio_stats_ptr->disk_name=strdup((char *) get_svr_from_bsd(ksp->ks_name));
510                          diskio_stats_ptr->systime=time(NULL);
511                          num_diskio++;
512                  }
# Line 580 | Line 579 | diskio_stat_t *get_diskio_stats(int *entries){
579                  n++;
580          }
581  
582 +        fclose(f);
583 +        f = NULL;
584 +
585          if (!has_pp_stats) {
586 <                /* This is an older kernel without stats in /proc/partitions.
587 <                   Read what we can from /proc/stat instead. */
586 >                /* This is an older kernel where /proc/partitions doesn't
587 >                   contain stats. Read what we can from /proc/stat instead, and
588 >                   fill in the appropriate bits of the list allocated above. */
589  
590                  f = fopen("/proc/stat", "r");
591                  if (f == NULL) goto out;
# Line 649 | Line 652 | diskio_stat_t *get_diskio_stats(int *entries){
652          num_diskio = n;
653   out:
654          if (f != NULL) fclose(f);
655 + #endif
656  
657 + #ifdef CYGWIN
658 +        return NULL;
659   #endif
660 +
661          *entries=num_diskio;
662  
663          return diskio_stats;
664   }
665  
666   diskio_stat_t *get_diskio_stats_diff(int *entries){
667 <        static diskio_stat_t *diskio_stats_diff=NULL;
668 <        static int sizeof_diskio_stats_diff=0;
669 <        diskio_stat_t *diskio_stats_diff_ptr, *diskio_stats_ptr;
670 <        int disks, x, y;
667 >        static diskio_stat_t *diff = NULL;
668 >        static int diff_count = 0;
669 >        diskio_stat_t *src, *dest;
670 >        int i, j, new_count;
671  
672 <        if(diskio_stats==NULL){
673 <                diskio_stats_ptr=get_diskio_stats(&disks);
674 <                *entries=disks;
668 <                return diskio_stats_ptr;
672 >        if (diskio_stats == NULL) {
673 >                /* No previous stats, so we can't calculate a difference. */
674 >                return get_diskio_stats(entries);
675          }
676  
677 <        diskio_stats_diff=diskio_stat_malloc(num_diskio, &sizeof_diskio_stats_diff, diskio_stats_diff);
678 <        if(diskio_stats_diff==NULL){
677 >        /* Resize the results array to match the previous stats. */
678 >        diff = diskio_stat_malloc(num_diskio, &diff_count, diff);
679 >        if (diff == NULL) {
680                  return NULL;
681          }
682  
683 <        diskio_stats_diff_ptr=diskio_stats_diff;
684 <        diskio_stats_ptr=diskio_stats;
683 >        /* Copy the previous stats into the result. */
684 >        for (i = 0; i < diff_count; i++) {
685 >                src = &diskio_stats[i];
686 >                dest = &diff[i];
687  
688 <        for(disks=0;disks<num_diskio;disks++){
689 <                if(diskio_stats_diff_ptr->disk_name!=NULL){
681 <                        free(diskio_stats_diff_ptr->disk_name);
688 >                if (dest->disk_name != NULL) {
689 >                        free(dest->disk_name);
690                  }
691 <                diskio_stats_diff_ptr->disk_name=strdup(diskio_stats_ptr->disk_name);
692 <                diskio_stats_diff_ptr->read_bytes=diskio_stats_ptr->read_bytes;
693 <                diskio_stats_diff_ptr->write_bytes=diskio_stats_ptr->write_bytes;
694 <                diskio_stats_diff_ptr->systime=diskio_stats_ptr->systime;
691 >                dest->disk_name = strdup(src->disk_name);
692 >                dest->read_bytes = src->read_bytes;
693 >                dest->write_bytes = src->write_bytes;
694 >                dest->systime = src->systime;
695 >        }
696  
697 <                diskio_stats_diff_ptr++;
698 <                diskio_stats_ptr++;
697 >        /* Get a new set of stats. */
698 >        if (get_diskio_stats(&new_count) == NULL) {
699 >                return NULL;
700          }
701  
702 <        diskio_stats_ptr=get_diskio_stats(&disks);
703 <        diskio_stats_diff_ptr=diskio_stats_diff;
702 >        /* For each previous stat... */
703 >        for (i = 0; i < diff_count; i++) {
704 >                dest = &diff[i];
705  
706 <        for(x=0;x<sizeof_diskio_stats_diff;x++){
707 <
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 <                }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++;
706 >                /* ... find the corresponding new stat ... */
707 >                for (j = 0; j < new_count; j++) {
708 >                        /* Try the new stat in the same position first,
709 >                           since that's most likely to be it. */
710 >                        src = &diskio_stats[(i + j) % new_count];
711 >                        if (strcmp(src->disk_name, dest->disk_name) == 0) {
712 >                                break;
713                          }
714                  }
715 +                if (j == new_count) {
716 +                        /* No match found. */
717 +                        continue;
718 +                }
719  
720 <                diskio_stats_ptr++;
721 <                diskio_stats_diff_ptr++;        
722 <
720 >                /* ... and subtract the previous stat from it to get the
721 >                   difference. */
722 >                dest->read_bytes = src->read_bytes - dest->read_bytes;
723 >                dest->write_bytes = src->write_bytes - dest->write_bytes;
724 >                dest->systime = src->systime - dest->systime;
725          }
726 <        
727 <        *entries=sizeof_diskio_stats_diff;
728 <        return diskio_stats_diff;
726 >
727 >        *entries = diff_count;
728 >        return diff;
729   }
730 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines