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.30 by ats, Sun Oct 19 00:10:30 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 #define VALID_FS_TYPES {"ext2", "ext3", "xfs", "reiserfs", "vfat", "tmpfs"}
42   #endif
43  
44 < #ifdef FREEBSD
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", \
48 >                        "rootfs", "reiserfs", "sysv", "v7", "udf", "ufs", \
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>
60 + #endif
61 + #ifdef FREEBSD
62   #include <sys/dkstat.h>
63   #include <devstat.h>
64 < #define VALID_FS_TYPES {"ufs", "mfs"}
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
76  
77   char *copy_string(char *orig_ptr, const char *newtext){
# Line 84 | 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 94 | 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
123 < #ifdef FREEBSD
123 > #ifdef ALLBSD
124          int nummnt;
125          struct statfs *mp;
126   #endif
# Line 111 | Line 133 | disk_stat_t *get_disk_stats(int *entries){
133                  watermark=START_VAL;
134                  init_disk_stat(0, watermark-1, disk_stats);
135          }
136 < #ifdef FREEBSD
136 > #ifdef ALLBSD
137          nummnt=getmntinfo(&mp , MNT_LOCAL);
138          if (nummnt<=0){
139                  return NULL;
# Line 126 | 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 175 | Line 197 | disk_stat_t *get_disk_stats(int *entries){
197                          }
198  
199                          disk_ptr=disk_stats+num_disks;
200 < #ifdef FREEBSD
200 > #ifdef ALLBSD
201                          if((disk_ptr->device_name=copy_string(disk_ptr->device_name, mp->f_mntfromname))==NULL){
202                                  return NULL;
203                          }
# Line 197 | 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 251 | 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 307 | 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 318 | Line 346 | diskio_stat_t *get_diskio_stats(int *entries){
346          FILE *f;
347          char *line_ptr;
348          int major, minor;
321        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 334 | Line 362 | diskio_stat_t *get_diskio_stats(int *entries){
362          long sel_gen;
363          struct devstat *dev_ptr;
364   #endif
365 + #ifdef NETBSD
366 +        struct disk_sysctl *stats;
367 +        int num_disks, i;
368 +        int mib[3];
369 +        size_t size;
370 + #endif
371 +
372          num_diskio=0;
373  
374 + #ifdef NETBSD
375 +        mib[0] = CTL_HW;
376 +        mib[1] = HW_DISKSTATS;
377 +        mib[2] = sizeof(struct disk_sysctl);
378 +
379 +        if (sysctl(mib, 3, NULL, &size, NULL, 0) < 0) {
380 +                return NULL;
381 +        }
382 +        num_disks = size / sizeof(struct disk_sysctl);
383 +
384 +        stats = malloc(size);
385 +        if (stats == NULL) {
386 +                return NULL;
387 +        }
388 +
389 +        if (sysctl(mib, 3, stats, &size, NULL, 0) < 0) {
390 +                return NULL;
391 +        }
392 +
393 +        for (i = 0; i < num_disks; i++) {
394 +                u_int64_t rbytes, wbytes;
395 +
396 + #ifdef HAVE_DK_RBYTES
397 +                rbytes = stats[i].dk_rbytes;
398 +                wbytes = stats[i].dk_wbytes;
399 + #else
400 +                /* Before 1.7, NetBSD merged reads and writes. */
401 +                rbytes = wbytes = stats[i].dk_bytes;
402 + #endif
403 +
404 +                /* Don't keep stats for disks that have never been used. */
405 +                if (rbytes == 0 && wbytes == 0) {
406 +                        continue;
407 +                }
408 +
409 +                diskio_stats = diskio_stat_malloc(num_diskio + 1,
410 +                                                  &sizeof_diskio_stats,
411 +                                                  diskio_stats);
412 +                if (diskio_stats == NULL) {
413 +                        return NULL;
414 +                }
415 +                diskio_stats_ptr = diskio_stats + num_diskio;
416 +                
417 +                diskio_stats_ptr->read_bytes = rbytes;
418 +                diskio_stats_ptr->write_bytes = wbytes;
419 +                if (diskio_stats_ptr->disk_name != NULL) {
420 +                        free(diskio_stats_ptr->disk_name);
421 +                }
422 +                diskio_stats_ptr->disk_name = strdup(stats[i].dk_name);
423 +                diskio_stats_ptr->systime = time(NULL);
424 +        
425 +                num_diskio++;  
426 +        }
427 +
428 +        free(stats);
429 + #endif
430 +
431   #ifdef FREEBSD
432          if (!stats_init) {
433                  stats.dinfo=malloc(sizeof(struct devinfo));
342                bzero(stats.dinfo, sizeof(struct devinfo));
434                  if(stats.dinfo==NULL) return NULL;
435 +                bzero(stats.dinfo, sizeof(struct devinfo));
436                  stats_init = 1;
437          }
438   #ifdef FREEBSD5
# Line 432 | 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 443 | 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,
447 <                        " %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;
450                if (nr < 5) {
451                        has_pp_stats = 0;
452                        rsect = 0;
453                        wsect = 0;
454                }
548  
549                  /* Skip device names ending in numbers, since they're
550                     partitions. */
# Line 459 | 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