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.34 by ats, Sun Oct 19 12:04:05 2003 UTC vs.
Revision 1.40 by ats, Mon Nov 10 23:25:45 2003 UTC

# Line 25 | Line 25
25   #include <stdio.h>
26   #include <stdlib.h>
27   #include <string.h>
28 + #include <time.h>
29   #include "statgrab.h"
30  
31   #ifdef SOLARIS
# Line 34 | Line 35
35   #define VALID_FS_TYPES {"ufs", "tmpfs"}
36   #endif
37  
38 < #ifdef LINUX
38 < #include <time.h>
39 < #include <sys/vfs.h>
38 > #if defined(LINUX) || defined(CYGWIN)
39   #include <mntent.h>
40 + #include <sys/vfs.h>
41   #include "tools.h"
42 + #endif
43 +
44 + #ifdef LINUX
45   #define VALID_FS_TYPES {"adfs", "affs", "befs", "bfs", "efs", "ext2", \
46                          "ext3", "vxfs", "hfs", "hfsplus", "hpfs", "jffs", \
47                          "jffs2", "minix", "msdos", "ntfs", "qnx4", "ramfs", \
# Line 46 | Line 49
49                          "umsdos", "vfat", "xfs", "jfs"}
50   #endif
51  
52 + #ifdef CYGWIN
53 + #define VALID_FS_TYPES {"user"}
54 + #endif
55 +
56   #ifdef ALLBSD
57   #include <sys/param.h>
58   #include <sys/ucred.h>
59   #include <sys/mount.h>
53 #define VALID_FS_TYPES {"ufs", "mfs", "ffs"}
60   #endif
61   #ifdef FREEBSD
62   #include <sys/dkstat.h>
63   #include <devstat.h>
64 + #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
65 +                        "ufs", "mfs"}
66   #endif
67   #ifdef NETBSD
68   #include <sys/param.h>
69   #include <sys/sysctl.h>
70   #include <sys/disk.h>
71 + #define VALID_FS_TYPES {"ffs", "mfs", "msdos", "lfs", "adosfs", "ext2fs", \
72 +                        "ntfs"}
73   #endif
74  
75   #define START_VAL 1
# Line 96 | Line 106 | disk_stat_t *get_disk_stats(int *entries){
106          char *fs_types[] = VALID_FS_TYPES;
107          int x, valid_type;
108          int num_disks=0;
109 < #if defined(LINUX) || defined (SOLARIS)
109 > #if defined(LINUX) || defined (SOLARIS) || defined(CYGWIN)
110          FILE *f;
111   #endif
112  
# Line 106 | Line 116 | disk_stat_t *get_disk_stats(int *entries){
116          struct mnttab mp;
117          struct statvfs fs;
118   #endif
119 < #ifdef LINUX
119 > #if defined(LINUX) || defined(CYGWIN)
120          struct mntent *mp;
121          struct statfs fs;
122   #endif
# Line 138 | Line 148 | disk_stat_t *get_disk_stats(int *entries){
148                  }
149   #endif
150  
151 < #ifdef LINUX
151 > #if defined(LINUX) || defined(CYGWIN)
152          if ((f=setmntent("/etc/mtab", "r" ))==NULL){
153                  return NULL;
154          }
# Line 209 | Line 219 | disk_stat_t *get_disk_stats(int *entries){
219                          /* Freebsd doesn't have a "available" inodes */
220                          disk_ptr->used_inodes=disk_ptr->total_inodes-disk_ptr->free_inodes;
221   #endif
222 < #ifdef LINUX
222 > #if defined(LINUX) || defined(CYGWIN)
223                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->mnt_fsname))==NULL){
224                                  return NULL;
225                          }
# Line 263 | Line 273 | disk_stat_t *get_disk_stats(int *entries){
273  
274          *entries=num_disks;    
275  
276 <        /* If this fails, there is very little i can do about it, so i'll ignore it :) */
277 < #if defined(LINUX) || defined(SOLARIS)
276 >        /* If this fails, there is very little i can do about it, so
277 >           I'll ignore it :) */
278 > #if defined(LINUX) || defined(CYGWIN)
279 >        endmntent(f);
280 > #endif
281 > #if defined(SOLARIS)
282          fclose(f);
283   #endif
284  
# Line 319 | Line 333 | typedef struct {
333   diskio_stat_t *get_diskio_stats(int *entries){
334  
335          static int sizeof_diskio_stats=0;
336 + #ifndef LINUX
337          diskio_stat_t *diskio_stats_ptr;
338 + #endif
339  
340   #ifdef SOLARIS
341          kstat_ctl_t *kc;
# Line 330 | Line 346 | diskio_stat_t *get_diskio_stats(int *entries){
346          FILE *f;
347          char *line_ptr;
348          int major, minor;
333        char dev_letter;
349          int has_pp_stats = 1;
350          static partition *parts = NULL;
351          static int alloc_parts = 0;
352          int i, n;
353          time_t now;
354 +        const char *format;
355   #endif
356   #ifdef FREEBSD
357          static struct statinfo stats;
# Line 381 | Line 397 | diskio_stat_t *get_diskio_stats(int *entries){
397                  rbytes = stats[i].dk_rbytes;
398                  wbytes = stats[i].dk_wbytes;
399   #else
400 <                /* Before 1.6.1, NetBSD merged reads and writes. */
400 >                /* Before 1.7, NetBSD merged reads and writes. */
401                  rbytes = wbytes = stats[i].dk_bytes;
402   #endif
403  
# Line 508 | Line 524 | diskio_stat_t *get_diskio_stats(int *entries){
524          n = 0;
525  
526          /* Read /proc/partitions to find what devices exist. Recent 2.4 kernels
527 <           have statistics in here too, so we can use those directly. */
527 >           have statistics in here too, so we can use those directly.
528 >           2.6 kernels have /proc/diskstats instead with almost (but not quite)
529 >           the same format. */
530  
531 <        f = fopen("/proc/partitions", "r");
531 >        f = fopen("/proc/diskstats", "r");
532 >        format = " %d %d %19s %*d %*d %lld %*d %*d %*d %lld";
533 >        if (f == NULL) {
534 >                f = fopen("/proc/partitions", "r");
535 >                format = " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld";
536 >        }
537          if (f == NULL) goto out;
538          now = time(NULL);
539  
# Line 519 | Line 542 | diskio_stat_t *get_diskio_stats(int *entries){
542                  char *s;
543                  long long rsect, wsect;
544  
545 <                int nr = sscanf(line_ptr,
523 <                        " %d %d %*d %19s %*d %*d %lld %*d %*d %*d %lld",
545 >                int nr = sscanf(line_ptr, format,
546                          &major, &minor, name, &rsect, &wsect);
547                  if (nr < 3) continue;
526                if (nr < 5) {
527                        has_pp_stats = 0;
528                        rsect = 0;
529                        wsect = 0;
530                }
548  
549                  /* Skip device names ending in numbers, since they're
550                     partitions. */
# Line 535 | Line 552 | diskio_stat_t *get_diskio_stats(int *entries){
552                  while (*s != '\0') s++;
553                  --s;
554                  if (*s >= '0' && *s <= '9') continue;
555 +
556 +                if (nr < 5) {
557 +                        has_pp_stats = 0;
558 +                        rsect = 0;
559 +                        wsect = 0;
560 +                }
561  
562                  diskio_stats = diskio_stat_malloc(n + 1, &sizeof_diskio_stats,
563                          diskio_stats);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines