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.82 by tdb, Wed Jul 13 13:01:24 2005 UTC vs.
Revision 1.88 by tdb, Fri Dec 4 22:09:30 2009 UTC

# Line 37 | Line 37
37   #include <sys/mnttab.h>
38   #include <sys/statvfs.h>
39   #include <kstat.h>
40 < #define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs"}
40 > #define VALID_FS_TYPES {"ufs", "tmpfs", "vxfs", "nfs", "zfs"}
41   #endif
42  
43   #if defined(LINUX) || defined(CYGWIN)
# Line 66 | Line 66
66   #if defined(FREEBSD) || defined(DFBSD)
67   #include <sys/dkstat.h>
68   #include <devstat.h>
69 < #define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
70 <                        "ufs", "mfs", "nfs"}
69 > #include <sys/param.h>
70 > #include <sys/mount.h>
71 > #include <sys/sysctl.h>
72 > /*#define VALID_FS_TYPES {"hpfs", "msdosfs", "ntfs", "udf", "ext2fs", \
73 >                        "ufs", "mfs", "nfs", "zfs", "tmpfs", "reiserfs", \
74 >                        "xfs"}*/
75   #endif
76   #if defined(NETBSD) || defined(OPENBSD)
77   #include <sys/param.h>
# Line 87 | Line 91
91   #include <dirent.h>
92   #include <stdio.h>
93   #include <time.h>
94 < #define VALID_FS_TYPES {"vxfs", "hfs"}
94 > #define VALID_FS_TYPES {"vxfs", "hfs", "nfs"}
95   #define DISK_BATCH 30
96   #endif
97  
98 + #ifdef WIN32
99 + #include "win32.h"
100 + /*#define VALID_FS_TYPES {"NTFS", "FAT", "FAT32"} unused*/
101 + #define BUFSIZE 512
102 + #endif
103 +
104   #ifdef ALLBSD
105   #define SG_MP_FSTYPENAME(mp) (mp)->f_fstypename
106   #define SG_MP_DEVNAME(mp)    (mp)->f_mntfromname
# Line 143 | Line 153 | static void disk_stat_destroy(sg_fs_stats *d) {
153          free(d->mnt_point);
154   }
155  
156 + #ifndef WIN32 /* not used by WIN32, so stop compiler throwing warnings */
157   static int is_valid_fs_type(const char *type) {
147        const char *types[] = VALID_FS_TYPES;
158          int i;
159  
160 + #if defined(FREEBSD) || defined(DFBSD)
161 +        struct xvfsconf *xvfsp;
162 +        size_t buflen;
163 +        int cnt, nbvfs = 0;
164 +
165 +        if (sysctlbyname("vfs.conflist", NULL, &buflen, NULL, 0) < 0) {
166 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vfs.conflist");
167 +                return 0;
168 +        }
169 +        xvfsp = alloca(buflen);
170 +        if (sysctlbyname("vfs.conflist", xvfsp, &buflen, NULL, 0) < 0) {
171 +                sg_set_error_with_errno(SG_ERROR_SYSCTLBYNAME, "vfs.conflist");
172 +                return 0;
173 +        }
174 +        cnt = buflen / sizeof(struct xvfsconf);
175 +        for (i = 0; i < cnt; i++) {
176 +                if (strcmp(xvfsp[i].vfc_name, type) == 0) {
177 +                        return 1;
178 +                }
179 +        }
180 + #else
181 +        const char *types[] = VALID_FS_TYPES;
182 +
183          for (i = 0; i < (int) (sizeof types / sizeof *types); i++) {
184                  if (strcmp(types[i], type) == 0) {
185                          return 1;
186                  }
187          }
188 + #endif
189          return 0;
190   }
191 + #endif
192  
193   sg_fs_stats *sg_get_fs_stats(int *entries){
194          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
# Line 182 | Line 217 | sg_fs_stats *sg_get_fs_stats(int *entries){
217          struct statfs *mp, **fs;
218   #endif
219   #endif
220 + #ifdef WIN32
221 +        char lp_buf[MAX_PATH];
222 +        char volume_name_buf[BUFSIZE];
223 +        char filesys_name_buf[BUFSIZE];
224 +        char drive[4] = " :\\";
225 +        char *p;
226 +        lp_buf[0]='\0';
227 + #endif
228  
229   #ifdef ALLBSD
230          nummnt=getmntinfo(&mp, MNT_WAIT);
# Line 220 | Line 263 | sg_fs_stats *sg_get_fs_stats(int *entries){
263                  }
264   #endif
265  
266 + #ifdef WIN32
267 +        if (!(GetLogicalDriveStrings(BUFSIZE-1, lp_buf))) {
268 +                sg_set_error(SG_ERROR_GETMNTINFO, "GetLogicalDriveStrings");
269 +                return NULL;
270 +        }
271 +        p = lp_buf;
272 +        do {
273 +                // Copy drive letter to template string
274 +                *drive = *p;
275 +                // Only interested in harddrives.
276 +                int drive_type = GetDriveType(drive);
277 +
278 +                if(drive_type == DRIVE_FIXED) {
279 + #else
280                  if(is_valid_fs_type(SG_MP_FSTYPENAME(mp))){
281 + #endif
282                          if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
283                                  return NULL;
284                          }
285                          disk_ptr=disk_stats+num_disks;
286  
287 + #ifndef WIN32
288                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
289                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
290                           * be upwards of a k each
# Line 243 | Line 302 | sg_fs_stats *sg_get_fs_stats(int *entries){
302                          disk_ptr->size  = SG_FS_FRSIZE(fs) * SG_FS_BLOCKS(fs);
303                          disk_ptr->avail = SG_FS_FRSIZE(fs) * SG_FS_BAVAIL(fs);
304                          disk_ptr->used  = (disk_ptr->size) - (SG_FS_FRSIZE(fs) * SG_FS_BFREE(fs));
305 <
305 >                
306                          disk_ptr->total_inodes = SG_FS_FILES(fs);
307                          disk_ptr->free_inodes  = SG_FS_FFREE(fs);
308                          /* Linux, FreeBSD don't have a "available" inodes */
# Line 256 | Line 315 | sg_fs_stats *sg_get_fs_stats(int *entries){
315                          disk_ptr->free_blocks  = SG_FS_BFREE(fs);
316                          disk_ptr->avail_blocks = SG_FS_BAVAIL(fs);
317                          disk_ptr->used_blocks  = disk_ptr->total_blocks - disk_ptr->free_blocks;
318 + #else
319 +                        if(!GetVolumeInformation(drive, volume_name_buf, BUFSIZE,
320 +                                                NULL, NULL, NULL,
321 +                                                filesys_name_buf, BUFSIZE)) {
322 +                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
323 +                                        "GetVolumeInformation");
324 +                                return NULL;
325 +                        }
326  
327 +                        if (sg_update_string(&disk_ptr->device_name,
328 +                                                volume_name_buf) < 0) {
329 +                                return NULL;
330 +                        }
331 +                        if (sg_update_string(&disk_ptr->fs_type,
332 +                                                filesys_name_buf) < 0) {
333 +                                return NULL;
334 +                        }
335 +                        if (sg_update_string(&disk_ptr->mnt_point,
336 +                                                drive) < 0) {
337 +                                return NULL;
338 +                        }
339 +                        if (!GetDiskFreeSpaceEx(drive, NULL,
340 +                                        (PULARGE_INTEGER)&disk_ptr->size,
341 +                                        (PULARGE_INTEGER)&disk_ptr->avail)) {
342 +                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
343 +                                        "GetDiskFreeSpaceEx");
344 +                                return NULL;
345 +                        }
346 +                        disk_ptr->used = disk_ptr->size - disk_ptr->avail;
347 +                        disk_ptr->total_inodes = 0;
348 +                        disk_ptr->free_inodes  = 0;
349 +                        disk_ptr->used_inodes  = 0;
350 +                        disk_ptr->avail_inodes = 0;
351 +
352 +                        /* I dunno what to do with these... so have nothing */
353 +                        disk_ptr->io_size = 0;
354 +                        disk_ptr->block_size = 0;
355 +                        disk_ptr->total_blocks = 0;
356 +                        disk_ptr->free_blocks = 0;
357 +                        disk_ptr->avail_blocks = 0;
358 +                        disk_ptr->used_blocks = 0;
359 + #endif
360                          num_disks++;
361                  }
362 + #ifdef WIN32
363 +                while(*p++);
364 +        } while(*p);
365 + #else
366          }
367 + #endif
368  
369          *entries=num_disks;    
370  
# Line 371 | Line 476 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
476          int mib[MIBSIZE];
477          size_t size;
478   #endif
479 + #ifdef WIN32
480 +        char *name;
481 +        long long rbytes;
482 +        long long wbytes;
483 + #endif
484  
485          num_diskio=0;
486  
# Line 673 | Line 783 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
783                          diskio_stats_ptr->write_bytes=kios.nwritten;
784                          if (sg_update_string(&diskio_stats_ptr->disk_name,
785                                               sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
786 +                                kstat_close(kc);
787                                  return NULL;
788                          }
789                          diskio_stats_ptr->systime=time(NULL);
# Line 768 | Line 879 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
879                  f = fopen("/proc/stat", "r");
880                  if (f == NULL) goto out;
881                  now = time(NULL);
882 <        
882 >
883                  line_ptr = sg_f_read_line(f, "disk_io:");
884                  if (line_ptr == NULL) goto out;
885 <        
885 >
886                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
887                          long long rsect, wsect;
888  
889                          if (*++line_ptr == '\0') break;
890 <        
890 >
891                          if((sscanf(line_ptr,
892                                  "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
893                                  &major, &minor, &rsect, &wsect)) != 4) {
# Line 837 | Line 948 | out:
948          return NULL;
949   #endif
950  
951 + #ifdef WIN32
952 +        sg_set_error(SG_ERROR_NONE, NULL);
953 +
954 +        while((name = get_diskio(num_diskio, &rbytes, &wbytes)) != NULL) {
955 +                if (VECTOR_RESIZE(diskio_stats, num_diskio+1)) {
956 +                        return NULL;
957 +                }
958 +
959 +                diskio_stats_ptr = diskio_stats + num_diskio;
960 +
961 +                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
962 +                        return NULL;
963 +                }
964 +                sg_update_string(&name, NULL);
965 +                diskio_stats_ptr->read_bytes = rbytes;
966 +                diskio_stats_ptr->write_bytes = wbytes;
967 +
968 +                diskio_stats_ptr->systime = 0;
969 +
970 +                num_diskio++;
971 +        }
972 + #endif
973 +
974          *entries=num_diskio;
975  
976          return diskio_stats;
977   }
978  
979   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
980 + #ifndef WIN32
981          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
982                                diskio_stat_init, diskio_stat_destroy);
983          sg_disk_io_stats *src = NULL, *dest;
# Line 904 | Line 1039 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
1039  
1040          *entries = diff_count;
1041          return diff;
1042 + #else /* WIN32 */
1043 +        return sg_get_disk_io_stats(entries);
1044 + #endif
1045   }
1046  
1047   int sg_disk_io_compare_name(const void *va, const void *vb) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines