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