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