8d15833562785ba4db31f1d31bd0e45c30587550
[fio.git] / os / os-dragonfly.h
1 #ifndef FIO_OS_DRAGONFLY_H
2 #define FIO_OS_DRAGONFLY_H
3
4 #define FIO_OS  os_dragonfly
5
6 #include <errno.h>
7 #include <unistd.h>
8 #include <sys/endian.h>
9 #include <sys/param.h>
10 #include <sys/sysctl.h>
11 #include <sys/statvfs.h>
12 #include <sys/diskslice.h>
13 #include <sys/ioctl_compat.h>
14 #include <sys/usched.h>
15 #include <sys/resource.h>
16
17 #include "../file.h"
18
19 #define FIO_HAVE_ODIRECT
20 #define FIO_USE_GENERIC_RAND
21 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
22 #define FIO_HAVE_FS_STAT
23 #define FIO_HAVE_TRIM
24 #define FIO_HAVE_CHARDEV_SIZE
25 #define FIO_HAVE_GETTID
26 #define FIO_HAVE_CPU_AFFINITY
27 #define FIO_HAVE_IOPRIO
28 #define FIO_HAVE_SHM_ATTACH_REMOVED
29
30 #define OS_MAP_ANON             MAP_ANON
31
32 #ifndef PTHREAD_STACK_MIN
33 #define PTHREAD_STACK_MIN 4096
34 #endif
35
36 #define fio_swap16(x)   bswap16(x)
37 #define fio_swap32(x)   bswap32(x)
38 #define fio_swap64(x)   bswap64(x)
39
40 /* This is supposed to equal (sizeof(cpumask_t)*8) */
41 #define FIO_MAX_CPUS    SMP_MAXCPU
42
43 typedef off_t off64_t;
44 typedef cpumask_t os_cpu_mask_t;
45
46 /*
47  * These macros are copied from sys/cpu/x86_64/include/types.h.
48  * It's okay to copy from arch dependent header because x86_64 is the only
49  * supported arch, and no other arch is going to be supported any time soon.
50  *
51  * These are supposed to be able to be included from userspace by defining
52  * _KERNEL_STRUCTURES, however this scheme is badly broken that enabling it
53  * causes compile-time conflicts with other headers. Although the current
54  * upstream code no longer requires _KERNEL_STRUCTURES, they should be kept
55  * here for compatibility with older versions.
56  */
57 #ifndef CPUMASK_SIMPLE
58 #define CPUMASK_SIMPLE(cpu)             ((uint64_t)1 << (cpu))
59 #define CPUMASK_TESTBIT(val, i)         ((val).ary[((i) >> 6) & 3] & \
60                                          CPUMASK_SIMPLE((i) & 63))
61 #define CPUMASK_ORBIT(mask, i)          ((mask).ary[((i) >> 6) & 3] |= \
62                                          CPUMASK_SIMPLE((i) & 63))
63 #define CPUMASK_NANDBIT(mask, i)        ((mask).ary[((i) >> 6) & 3] &= \
64                                          ~CPUMASK_SIMPLE((i) & 63))
65 #define CPUMASK_ASSZERO(mask)           do {                            \
66                                         (mask).ary[0] = 0;              \
67                                         (mask).ary[1] = 0;              \
68                                         (mask).ary[2] = 0;              \
69                                         (mask).ary[3] = 0;              \
70                                         } while(0)
71 #endif
72
73 /*
74  * Define USCHED_GET_CPUMASK as the macro didn't exist until release 4.5.
75  * usched_set(2) returns EINVAL if the kernel doesn't support it.
76  *
77  * Also note usched_set(2) works only for the current thread regardless of
78  * the command type. It doesn't work against another thread regardless of
79  * a caller's privilege. A caller would generally specify 0 for pid for the
80  * current thread though that's the only choice. See BUGS in usched_set(2).
81  */
82 #ifndef USCHED_GET_CPUMASK
83 #define USCHED_GET_CPUMASK      5
84 #endif
85
86 /* No CPU_COUNT(), but use the default function defined in os/os.h */
87 #define fio_cpu_count(mask)             CPU_COUNT((mask))
88
89 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
90 {
91         CPUMASK_ASSZERO(*mask);
92         return 0;
93 }
94
95 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
96 {
97         return 0;
98 }
99
100 static inline void fio_cpu_clear(os_cpu_mask_t *mask, int cpu)
101 {
102         CPUMASK_NANDBIT(*mask, cpu);
103 }
104
105 static inline void fio_cpu_set(os_cpu_mask_t *mask, int cpu)
106 {
107         CPUMASK_ORBIT(*mask, cpu);
108 }
109
110 static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu)
111 {
112         if (CPUMASK_TESTBIT(*mask, cpu))
113                 return 1;
114
115         return 0;
116 }
117
118 static inline int fio_setaffinity(int pid, os_cpu_mask_t mask)
119 {
120         int i, firstcall = 1;
121
122         /* 0 for the current thread, see BUGS in usched_set(2) */
123         pid = 0;
124
125         for (i = 0; i < FIO_MAX_CPUS; i++) {
126                 if (!CPUMASK_TESTBIT(mask, i))
127                         continue;
128                 if (firstcall) {
129                         if (usched_set(pid, USCHED_SET_CPU, &i, sizeof(int)))
130                                 return -1;
131                         firstcall = 0;
132                 } else {
133                         if (usched_set(pid, USCHED_ADD_CPU, &i, sizeof(int)))
134                                 return -1;
135                 }
136         }
137
138         return 0;
139 }
140
141 static inline int fio_getaffinity(int pid, os_cpu_mask_t *mask)
142 {
143         /* 0 for the current thread, see BUGS in usched_set(2) */
144         pid = 0;
145
146         if (usched_set(pid, USCHED_GET_CPUMASK, mask, sizeof(*mask)))
147                 return -1;
148
149         return 0;
150 }
151
152 /* fio code is Linux based, so rename macros to Linux style */
153 #define IOPRIO_WHO_PROCESS      PRIO_PROCESS
154 #define IOPRIO_WHO_PGRP         PRIO_PGRP
155 #define IOPRIO_WHO_USER         PRIO_USER
156
157 #define IOPRIO_MIN_PRIO         1       /* lowest priority */
158 #define IOPRIO_MAX_PRIO         10      /* highest priority */
159
160 /*
161  * Prototypes declared in sys/sys/resource.h are preventing from defining
162  * ioprio_set() with 4 arguments, so define fio's ioprio_set() as a macro.
163  * Note that there is no idea of class within ioprio_set(2) unlike Linux.
164  */
165 #define ioprio_set(which, who, ioprio_class, ioprio)    \
166         ioprio_set(which, who, ioprio)
167
168 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
169 {
170         struct partinfo pi;
171
172         if (!ioctl(f->fd, DIOCGPART, &pi)) {
173                 *bytes = (unsigned long long) pi.media_size;
174                 return 0;
175         }
176
177         *bytes = 0;
178         return errno;
179 }
180
181 static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
182 {
183         return blockdev_size(f, bytes);
184 }
185
186 static inline int blockdev_invalidate_cache(struct fio_file *f)
187 {
188         return ENOTSUP;
189 }
190
191 static inline unsigned long long os_phys_mem(void)
192 {
193         int mib[2] = { CTL_HW, HW_PHYSMEM };
194         uint64_t mem;
195         size_t len = sizeof(mem);
196
197         sysctl(mib, 2, &mem, &len, NULL, 0);
198         return mem;
199 }
200
201 static inline int gettid(void)
202 {
203         return (int) lwp_gettid();
204 }
205
206 static inline unsigned long long get_fs_free_size(const char *path)
207 {
208         unsigned long long ret;
209         struct statvfs s;
210
211         if (statvfs(path, &s) < 0)
212                 return -1ULL;
213
214         ret = s.f_frsize;
215         ret *= (unsigned long long) s.f_bfree;
216         return ret;
217 }
218
219 static inline int os_trim(int fd, unsigned long long start,
220                           unsigned long long len)
221 {
222         off_t range[2];
223
224         range[0] = start;
225         range[1] = len;
226
227         if (!ioctl(fd, IOCTLTRIM, range))
228                 return 0;
229
230         return errno;
231 }
232
233 #ifdef MADV_FREE
234 #define FIO_MADV_FREE   MADV_FREE
235 #endif
236
237 static inline int shm_attach_to_open_removed(void)
238 {
239         int x;
240         size_t len = sizeof(x);
241
242         if (sysctlbyname("kern.ipc.shm_allow_removed", &x, &len, NULL, 0) < 0)
243                 return 0;
244
245         return x > 0 ? 1 : 0;
246 }
247
248 #endif