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.85 by tdb, Mon Oct 9 14:09:38 2006 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"}
41   #endif
42  
43   #if defined(LINUX) || defined(CYGWIN)
# Line 87 | Line 87
87   #include <dirent.h>
88   #include <stdio.h>
89   #include <time.h>
90 < #define VALID_FS_TYPES {"vxfs", "hfs"}
90 > #define VALID_FS_TYPES {"vxfs", "hfs", "nfs"}
91   #define DISK_BATCH 30
92   #endif
93  
94 + #ifdef WIN32
95 + #include "win32.h"
96 + /*#define VALID_FS_TYPES {"NTFS", "FAT", "FAT32"} unused*/
97 + #define BUFSIZE 512
98 + #endif
99 +
100   #ifdef ALLBSD
101   #define SG_MP_FSTYPENAME(mp) (mp)->f_fstypename
102   #define SG_MP_DEVNAME(mp)    (mp)->f_mntfromname
# Line 143 | Line 149 | static void disk_stat_destroy(sg_fs_stats *d) {
149          free(d->mnt_point);
150   }
151  
152 + #ifndef WIN32 /* not used by WIN32, so stop compiler throwing warnings */
153   static int is_valid_fs_type(const char *type) {
154          const char *types[] = VALID_FS_TYPES;
155          int i;
# Line 154 | Line 161 | static int is_valid_fs_type(const char *type) {
161          }
162          return 0;
163   }
164 + #endif
165  
166   sg_fs_stats *sg_get_fs_stats(int *entries){
167          VECTOR_DECLARE_STATIC(disk_stats, sg_fs_stats, 10,
# Line 182 | Line 190 | sg_fs_stats *sg_get_fs_stats(int *entries){
190          struct statfs *mp, **fs;
191   #endif
192   #endif
193 + #ifdef WIN32
194 +        char lp_buf[MAX_PATH];
195 +        char volume_name_buf[BUFSIZE];
196 +        char filesys_name_buf[BUFSIZE];
197 +        char drive[4] = " :\\";
198 +        char *p;
199 +        lp_buf[0]='\0';
200 + #endif
201  
202   #ifdef ALLBSD
203          nummnt=getmntinfo(&mp, MNT_WAIT);
# Line 220 | Line 236 | sg_fs_stats *sg_get_fs_stats(int *entries){
236                  }
237   #endif
238  
239 + #ifdef WIN32
240 +        if (!(GetLogicalDriveStrings(BUFSIZE-1, lp_buf))) {
241 +                sg_set_error(SG_ERROR_GETMNTINFO, "GetLogicalDriveStrings");
242 +                return NULL;
243 +        }
244 +        p = lp_buf;
245 +        do {
246 +                // Copy drive letter to template string
247 +                *drive = *p;
248 +                // Only interested in harddrives.
249 +                int drive_type = GetDriveType(drive);
250 +
251 +                if(drive_type == DRIVE_FIXED) {
252 + #else
253                  if(is_valid_fs_type(SG_MP_FSTYPENAME(mp))){
254 + #endif
255                          if (VECTOR_RESIZE(disk_stats, num_disks + 1) < 0) {
256                                  return NULL;
257                          }
258                          disk_ptr=disk_stats+num_disks;
259  
260 + #ifndef WIN32
261                          /* Maybe make this char[bigenough] and do strncpy's and put a null in the end?
262                           * Downside is its a bit hungry for a lot of mounts, as MNT_MAX_SIZE would prob
263                           * be upwards of a k each
# Line 243 | Line 275 | sg_fs_stats *sg_get_fs_stats(int *entries){
275                          disk_ptr->size  = SG_FS_FRSIZE(fs) * SG_FS_BLOCKS(fs);
276                          disk_ptr->avail = SG_FS_FRSIZE(fs) * SG_FS_BAVAIL(fs);
277                          disk_ptr->used  = (disk_ptr->size) - (SG_FS_FRSIZE(fs) * SG_FS_BFREE(fs));
278 <
278 >                
279                          disk_ptr->total_inodes = SG_FS_FILES(fs);
280                          disk_ptr->free_inodes  = SG_FS_FFREE(fs);
281                          /* Linux, FreeBSD don't have a "available" inodes */
# Line 256 | Line 288 | sg_fs_stats *sg_get_fs_stats(int *entries){
288                          disk_ptr->free_blocks  = SG_FS_BFREE(fs);
289                          disk_ptr->avail_blocks = SG_FS_BAVAIL(fs);
290                          disk_ptr->used_blocks  = disk_ptr->total_blocks - disk_ptr->free_blocks;
291 + #else
292 +                        if(!GetVolumeInformation(drive, volume_name_buf, BUFSIZE,
293 +                                                NULL, NULL, NULL,
294 +                                                filesys_name_buf, BUFSIZE)) {
295 +                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
296 +                                        "GetVolumeInformation");
297 +                                return NULL;
298 +                        }
299  
300 +                        if (sg_update_string(&disk_ptr->device_name,
301 +                                                volume_name_buf) < 0) {
302 +                                return NULL;
303 +                        }
304 +                        if (sg_update_string(&disk_ptr->fs_type,
305 +                                                filesys_name_buf) < 0) {
306 +                                return NULL;
307 +                        }
308 +                        if (sg_update_string(&disk_ptr->mnt_point,
309 +                                                drive) < 0) {
310 +                                return NULL;
311 +                        }
312 +                        if (!GetDiskFreeSpaceEx(drive, NULL,
313 +                                        (PULARGE_INTEGER)&disk_ptr->size,
314 +                                        (PULARGE_INTEGER)&disk_ptr->avail)) {
315 +                                sg_set_error_with_errno(SG_ERROR_DISKINFO,
316 +                                        "GetDiskFreeSpaceEx");
317 +                                return NULL;
318 +                        }
319 +                        disk_ptr->used = disk_ptr->size - disk_ptr->avail;
320 +                        disk_ptr->total_inodes = 0;
321 +                        disk_ptr->free_inodes  = 0;
322 +                        disk_ptr->used_inodes  = 0;
323 +                        disk_ptr->avail_inodes = 0;
324 +
325 +                        /* I dunno what to do with these... so have nothing */
326 +                        disk_ptr->io_size = 0;
327 +                        disk_ptr->block_size = 0;
328 +                        disk_ptr->total_blocks = 0;
329 +                        disk_ptr->free_blocks = 0;
330 +                        disk_ptr->avail_blocks = 0;
331 +                        disk_ptr->used_blocks = 0;
332 + #endif
333                          num_disks++;
334                  }
335 + #ifdef WIN32
336 +                while(*p++);
337 +        } while(*p);
338 + #else
339          }
340 + #endif
341  
342          *entries=num_disks;    
343  
# Line 371 | Line 449 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
449          int mib[MIBSIZE];
450          size_t size;
451   #endif
452 + #ifdef WIN32
453 +        char *name;
454 +        long long rbytes;
455 +        long long wbytes;
456 + #endif
457  
458          num_diskio=0;
459  
# Line 673 | Line 756 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
756                          diskio_stats_ptr->write_bytes=kios.nwritten;
757                          if (sg_update_string(&diskio_stats_ptr->disk_name,
758                                               sg_get_svr_from_bsd(ksp->ks_name)) < 0) {
759 +                                kstat_close(kc);
760                                  return NULL;
761                          }
762                          diskio_stats_ptr->systime=time(NULL);
# Line 768 | Line 852 | sg_disk_io_stats *sg_get_disk_io_stats(int *entries){
852                  f = fopen("/proc/stat", "r");
853                  if (f == NULL) goto out;
854                  now = time(NULL);
855 <        
855 >
856                  line_ptr = sg_f_read_line(f, "disk_io:");
857                  if (line_ptr == NULL) goto out;
858 <        
858 >
859                  while((line_ptr=strchr(line_ptr, ' '))!=NULL){
860                          long long rsect, wsect;
861  
862                          if (*++line_ptr == '\0') break;
863 <        
863 >
864                          if((sscanf(line_ptr,
865                                  "(%d,%d):(%*d, %*d, %lld, %*d, %lld)",
866                                  &major, &minor, &rsect, &wsect)) != 4) {
# Line 837 | Line 921 | out:
921          return NULL;
922   #endif
923  
924 + #ifdef WIN32
925 +        sg_set_error(SG_ERROR_NONE, NULL);
926 +
927 +        while((name = get_diskio(num_diskio, &rbytes, &wbytes)) != NULL) {
928 +                if (VECTOR_RESIZE(diskio_stats, num_diskio+1)) {
929 +                        return NULL;
930 +                }
931 +
932 +                diskio_stats_ptr = diskio_stats + num_diskio;
933 +
934 +                if (sg_update_string(&diskio_stats_ptr->disk_name, name) < 0) {
935 +                        return NULL;
936 +                }
937 +                sg_update_string(&name, NULL);
938 +                diskio_stats_ptr->read_bytes = rbytes;
939 +                diskio_stats_ptr->write_bytes = wbytes;
940 +
941 +                diskio_stats_ptr->systime = 0;
942 +
943 +                num_diskio++;
944 +        }
945 + #endif
946 +
947          *entries=num_diskio;
948  
949          return diskio_stats;
950   }
951  
952   sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entries){
953 + #ifndef WIN32
954          VECTOR_DECLARE_STATIC(diff, sg_disk_io_stats, 1,
955                                diskio_stat_init, diskio_stat_destroy);
956          sg_disk_io_stats *src = NULL, *dest;
# Line 904 | Line 1012 | sg_disk_io_stats *sg_get_disk_io_stats_diff(int *entri
1012  
1013          *entries = diff_count;
1014          return diff;
1015 + #else /* WIN32 */
1016 +        return sg_get_disk_io_stats(entries);
1017 + #endif
1018   }
1019  
1020   int sg_disk_io_compare_name(const void *va, const void *vb) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines