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