Merge branch 'master' of https://github.com/DevriesL/fio
[fio.git] / os / os-openbsd.h
1 #ifndef FIO_OS_OPENBSD_H
2 #define FIO_OS_OPENBSD_H
3
4 #define FIO_OS  os_openbsd
5
6 #include <errno.h>
7 #include <sys/param.h>
8 #include <sys/statvfs.h>
9 #include <sys/ioctl.h>
10 #include <sys/dkio.h>
11 #include <sys/disklabel.h>
12 #include <sys/endian.h>
13 #include <sys/utsname.h>
14 #include <sys/sysctl.h>
15
16 /* XXX hack to avoid conflicts between rbtree.h and <sys/tree.h> */
17 #undef RB_BLACK
18 #undef RB_RED
19 #undef RB_ROOT
20
21 #include "../file.h"
22
23 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
24 #define FIO_HAVE_FS_STAT
25 #define FIO_HAVE_GETTID
26 #define FIO_HAVE_SHM_ATTACH_REMOVED
27
28 #define OS_MAP_ANON             MAP_ANON
29
30 #ifndef PTHREAD_STACK_MIN
31 #define PTHREAD_STACK_MIN 4096
32 #endif
33
34 #define fio_swap16(x)   swap16(x)
35 #define fio_swap32(x)   swap32(x)
36 #define fio_swap64(x)   swap64(x)
37
38 #ifdef CONFIG_PTHREAD_GETAFFINITY
39 #define FIO_HAVE_GET_THREAD_AFFINITY
40 #define fio_get_thread_affinity(mask)   \
41         pthread_getaffinity_np(pthread_self(), sizeof(mask), &(mask))
42 #endif
43
44 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
45 {
46         struct disklabel dl;
47
48         if (!ioctl(f->fd, DIOCGDINFO, &dl)) {
49                 *bytes = ((unsigned long long)dl.d_secperunit) * dl.d_secsize;
50                 return 0;
51         }
52
53         *bytes = 0;
54         return errno;
55 }
56
57 static inline int blockdev_invalidate_cache(struct fio_file *f)
58 {
59         return ENOTSUP;
60 }
61
62 static inline unsigned long long os_phys_mem(void)
63 {
64         int mib[2] = { CTL_HW, HW_PHYSMEM64 };
65         uint64_t mem;
66         size_t len = sizeof(mem);
67
68         sysctl(mib, 2, &mem, &len, NULL, 0);
69         return mem;
70 }
71
72 #ifndef CONFIG_HAVE_GETTID
73 static inline int gettid(void)
74 {
75         return (int)(intptr_t) pthread_self();
76 }
77 #endif
78
79 static inline unsigned long long get_fs_free_size(const char *path)
80 {
81         unsigned long long ret;
82         struct statvfs s;
83
84         if (statvfs(path, &s) < 0)
85                 return -1ULL;
86
87         ret = s.f_frsize;
88         ret *= (unsigned long long) s.f_bfree;
89         return ret;
90 }
91
92 #ifdef MADV_FREE
93 #define FIO_MADV_FREE   MADV_FREE
94 #endif
95
96 static inline int shm_attach_to_open_removed(void)
97 {
98         struct utsname uts;
99         int major, minor;
100
101         if (uname(&uts) == -1)
102                 return 0;
103
104         /*
105          * Return 1 if >= OpenBSD 5.1 according to 97900ebf,
106          * assuming both major/minor versions are < 10.
107          */
108         if (uts.release[0] > '9' || uts.release[0] < '0')
109                 return 0;
110         if (uts.release[1] != '.')
111                 return 0;
112         if (uts.release[2] > '9' || uts.release[2] < '0')
113                 return 0;
114
115         major = uts.release[0] - '0';
116         minor = uts.release[2] - '0';
117
118         if (major > 5)
119                 return 1;
120         if (major == 5 && minor >= 1)
121                 return 1;
122
123         return 0;
124 }
125
126 #endif