Add get_fs_size() support for BSDs
authorTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Wed, 10 Jun 2015 12:45:00 +0000 (21:45 +0900)
committerTomohiro Kusumi <kusumi.tomohiro@gmail.com>
Thu, 11 Jun 2015 11:08:36 +0000 (20:08 +0900)
Add get_fs_size() for FreeBSD/DragonFlyBSD. The same code may
work on NetBSD/OpenBSD as it uses POSIX statvfs(2).

The following results verify df(1) (which also typically uses
statfs(2) or statvfs(2)) shows the same number given that df(1)
output is correct for the given filesystems.

--
 # uname
 FreeBSD
 # cat ./test2.c
 #include <stdio.h>
 #include "os/os-freebsd.h"
 int main(int argc, char *argv[]) {
         printf("%s %llu\n", argv[1], get_fs_size(argv[1]));
         return 0;
 }
 # clang -Wall -g ./test2.c -o test2
 # ./test2 /
 / 102783143936
 # df -TH /
 Filesystem   Type    Size    Used   Avail Capacity  Mounted on
 /dev/ada0p2  ufs     112G    9.5G     94G     9%    /
                      ^^^^^^^^^^^^     ^^^
                      112G - 9.5G      bfree!=bavail
                      = 102.5G         like it differs on ext[234]
--
 # uname
 DragonFly
 # cat ./test2.c
 #include <stdio.h>
 #include "os/os-dragonfly.h"
 int main(int argc, char *argv[]) {
         printf("%s %llu\n", argv[1], get_fs_size(argv[1]));
         return 0;
 }
 # gcc -Wall -g ./test2.c -o test2
 # ./test2 /
 / 450287370240
 # df -TH /
 Filesystem  Type     Size   Used  Avail Capacity  Mounted on
 ROOT        hammer   480G    30G   450G     6%    /

os/os-dragonfly.h
os/os-freebsd.h

index 7f62233012df62540e7f0f959b643973727309d0..f101429fadc1064223502af3c827bc86c90a57ed 100644 (file)
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <sys/param.h>
 #include <sys/sysctl.h>
+#include <sys/statvfs.h>
 
 #include "../file.h"
 
@@ -14,6 +15,7 @@
 #define FIO_USE_GENERIC_BDEV_SIZE
 #define FIO_USE_GENERIC_RAND
 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
+#define FIO_HAVE_FS_STAT
 #define FIO_HAVE_GETTID
 
 #undef FIO_HAVE_CPU_AFFINITY   /* XXX notyet */
@@ -50,6 +52,19 @@ static inline int gettid(void)
        return (int) lwp_gettid();
 }
 
+static inline unsigned long long get_fs_size(const char *path)
+{
+       unsigned long long ret;
+       struct statvfs s;
+
+       if (statvfs(path, &s) < 0)
+               return -1ULL;
+
+       ret = s.f_frsize;
+       ret *= (unsigned long long) s.f_bfree;
+       return ret;
+}
+
 #ifdef MADV_FREE
 #define FIO_MADV_FREE  MADV_FREE
 #endif
index 22765ce60161075bb4aba10c32d4ddd4900ddf90..19ec54e813001df73c363db34be899b141d7296a 100644 (file)
@@ -10,6 +10,7 @@
 #include <sys/socket.h>
 #include <sys/param.h>
 #include <sys/cpuset.h>
+#include <sys/statvfs.h>
 
 #include "../file.h"
 
@@ -17,6 +18,7 @@
 #define FIO_USE_GENERIC_RAND
 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
 #define FIO_HAVE_CHARDEV_SIZE
+#define FIO_HAVE_FS_STAT
 #define FIO_HAVE_GETTID
 #define FIO_HAVE_CPU_AFFINITY
 
@@ -99,6 +101,19 @@ static inline int gettid(void)
        return (int) lwpid;
 }
 
+static inline unsigned long long get_fs_size(const char *path)
+{
+       unsigned long long ret;
+       struct statvfs s;
+
+       if (statvfs(path, &s) < 0)
+               return -1ULL;
+
+       ret = s.f_frsize;
+       ret *= (unsigned long long) s.f_bfree;
+       return ret;
+}
+
 #ifdef MADV_FREE
 #define FIO_MADV_FREE  MADV_FREE
 #endif