Add runtime handlers for 97900ebf for FreeBSD/DragonFlyBSD
authorTomohiro Kusumi <tkusumi@tuxera.com>
Tue, 7 Mar 2017 20:12:52 +0000 (22:12 +0200)
committerJens Axboe <axboe@fb.com>
Fri, 10 Mar 2017 21:43:37 +0000 (14:43 -0700)
As 97900ebf itself explains, this is a runtime tunable on FreeBSD
and DragonFlyBSD, so they need to check the sysctl on runtime.

Not sure if OpenBSD has any tunable for this as the original commit
only says about >=5.1, but afaik recent versions of OpenBSD can't
even compile fio anyway. Also not sure how it works on NetBSD, and
the original commit said nothing about it too. Also see below url.

http://www.spinics.net/lists/fio/msg05586.html
http://www.spinics.net/lists/fio/msg05588.html

--
 # uname -r
 11.0-RELEASE-p1
 # cat ./test1.c
 #include <stdio.h>
 #include "./os/os-freebsd.h"
 int main(void) {
         printf("%d\n", shm_attach_to_open_removed());
         return 0;
 }
 # clang -Wall -g ./test1.c
 # sysctl kern.ipc.shm_allow_removed
 kern.ipc.shm_allow_removed: 1
 # ./a.out
 1
 # sysctl kern.ipc.shm_allow_removed=123
 kern.ipc.shm_allow_removed: 1 -> 123
 # ./a.out
 1
 # sysctl kern.ipc.shm_allow_removed=0
 kern.ipc.shm_allow_removed: 123 -> 0
 # ./a.out
 0

Signed-off-by: Tomohiro Kusumi <tkusumi@tuxera.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
init.c
os/os-dragonfly.h
os/os-freebsd.h
os/os-linux.h
os/os-openbsd.h
os/os.h

diff --git a/init.c b/init.c
index fabc887b06c75c737e8eb49632f8bebe11e28c47..acdc659879b42656f593ab6dbfc3ad83f36bc041 100644 (file)
--- a/init.c
+++ b/init.c
@@ -356,9 +356,8 @@ static int setup_thread_area(void)
                perror("shmat");
                return 1;
        }
-#ifdef FIO_HAVE_SHM_ATTACH_REMOVED
-       shmctl(shm_id, IPC_RMID, NULL);
-#endif
+       if (shm_attach_to_open_removed())
+               shmctl(shm_id, IPC_RMID, NULL);
 #endif
 
        memset(threads, 0, max_jobs * sizeof(struct thread_data));
index 5e94855cac5faef440a72492093944c0ffc0c3fa..97452cad6b22b0c87bb6fa851250a6bd1c5b15df 100644 (file)
@@ -24,8 +24,7 @@
 #define FIO_HAVE_GETTID
 #define FIO_HAVE_CPU_AFFINITY
 #define FIO_HAVE_IOPRIO
-/* Only have attach-to-open-removed when kern.ipc.shm_allow_removed is 1 */
-#undef  FIO_HAVE_SHM_ATTACH_REMOVED
+#define FIO_HAVE_SHM_ATTACH_REMOVED
 
 #define OS_MAP_ANON            MAP_ANON
 
@@ -234,4 +233,15 @@ static inline int os_trim(int fd, unsigned long long start,
 #define FIO_MADV_FREE  MADV_FREE
 #endif
 
+static inline int shm_attach_to_open_removed(void)
+{
+       int x;
+       size_t len = sizeof(x);
+
+       if (sysctlbyname("kern.ipc.shm_allow_removed", &x, &len, NULL, 0) < 0)
+               return 0;
+
+       return x > 0 ? 1 : 0;
+}
+
 #endif
index aa90954dc6497b54ef841cce82635847b5c354d9..9d1af3b4c49f5a15ab0cb2250130b5ea3ffd091a 100644 (file)
@@ -22,9 +22,7 @@
 #define FIO_HAVE_TRIM
 #define FIO_HAVE_GETTID
 #define FIO_HAVE_CPU_AFFINITY
-/* Only have attach-to-open-removed when kern.ipc.shm_allow_removed is 1 */
-#undef  FIO_HAVE_SHM_ATTACH_REMOVED
-
+#define FIO_HAVE_SHM_ATTACH_REMOVED
 
 #define OS_MAP_ANON            MAP_ANON
 
@@ -136,4 +134,15 @@ static inline int os_trim(int fd, unsigned long long start,
 #define FIO_MADV_FREE  MADV_FREE
 #endif
 
+static inline int shm_attach_to_open_removed(void)
+{
+       int x;
+       size_t len = sizeof(x);
+
+       if (sysctlbyname("kern.ipc.shm_allow_removed", &x, &len, NULL, 0) < 0)
+               return 0;
+
+       return x > 0 ? 1 : 0;
+}
+
 #endif
index 18298297756ed86d687fa9117b9fda2f3a6f6ab5..7be833bf067a5f81df3505fb2a74df4af4cdc265 100644 (file)
@@ -350,4 +350,9 @@ static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
 #endif /* __NR_preadv2 */
 #endif /* CONFIG_PWRITEV2 */
 
+static inline int shm_attach_to_open_removed(void)
+{
+       return 1;
+}
+
 #endif
index 47005721abba67b2f5bb12c760cf9ec144fa7b25..3b1948321bcbb6e39fa881f0c74893c6ec431c88 100644 (file)
 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
 #define FIO_HAVE_FS_STAT
 #define FIO_HAVE_GETTID
+#define FIO_HAVE_SHM_ATTACH_REMOVED
 
 #undef FIO_HAVE_CPU_AFFINITY   /* XXX notyet */
 
-/* Only OpenBSD 5.1 and above have attach-to-open-removed semantics */
-#undef  FIO_HAVE_SHM_ATTACH_REMOVED
-
 #define OS_MAP_ANON            MAP_ANON
 
 #ifndef PTHREAD_STACK_MIN
@@ -90,4 +88,12 @@ static inline unsigned long long get_fs_free_size(const char *path)
 #define FIO_MADV_FREE  MADV_FREE
 #endif
 
+static inline int shm_attach_to_open_removed(void)
+{
+       /*
+        * XXX: Return 1 if >= OpenBSD 5.1 according to 97900ebf.
+        */
+       return 0;
+}
+
 #endif
diff --git a/os/os.h b/os/os.h
index 4178e6f916f42831ce05bb0d0626b75f763e3c36..5e3c813d8fb5b5f726d650c172aedde94f84f21d 100644 (file)
--- a/os/os.h
+++ b/os/os.h
@@ -386,4 +386,11 @@ static inline int gettid(void)
 }
 #endif
 
+#ifndef FIO_HAVE_SHM_ATTACH_REMOVED
+static inline int shm_attach_to_open_removed(void)
+{
+       return 0;
+}
+#endif
+
 #endif