Add CPU affinity support for DragonFlyBSD
[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>
239b2882 8#include <sys/param.h>
239b2882 9#include <sys/sysctl.h>
60f840a8 10#include <sys/statvfs.h>
1116dc04 11#include <sys/diskslice.h>
c9a4e0c8 12#include <sys/ioctl_compat.h>
a306497a 13#include <sys/usched.h>
239b2882
JA
14
15#include "../file.h"
16
17#define FIO_HAVE_ODIRECT
239b2882
JA
18#define FIO_USE_GENERIC_RAND
19#define FIO_USE_GENERIC_INIT_RANDOM_STATE
60f840a8 20#define FIO_HAVE_FS_STAT
c9a4e0c8 21#define FIO_HAVE_TRIM
1116dc04 22#define FIO_HAVE_CHARDEV_SIZE
239b2882 23#define FIO_HAVE_GETTID
a306497a 24#define FIO_HAVE_CPU_AFFINITY
239b2882
JA
25
26#define OS_MAP_ANON MAP_ANON
27
28#ifndef PTHREAD_STACK_MIN
29#define PTHREAD_STACK_MIN 4096
30#endif
31
32#define fio_swap16(x) bswap16(x)
33#define fio_swap32(x) bswap32(x)
34#define fio_swap64(x) bswap64(x)
35
a306497a
TK
36/* This is supposed to equal (sizeof(cpumask_t)*8) */
37#define FIO_MAX_CPUS SMP_MAXCPU
38
239b2882 39typedef off_t off64_t;
a306497a
TK
40typedef cpumask_t os_cpu_mask_t;
41
42/*
43 * These macros are copied from sys/cpu/x86_64/include/types.h.
44 * It's okay to copy from arch dependent header because x86_64 is the only
45 * supported arch, and no other arch is going to be supported any time soon.
46 *
47 * These are supposed to be able to be included from userspace by defining
48 * _KERNEL_STRUCTURES, however this scheme is badly broken that enabling it
49 * causes compile-time conflicts with other headers. Although the current
50 * upstream code no longer requires _KERNEL_STRUCTURES, they should be kept
51 * here for compatibility with older versions.
52 */
53#ifndef CPUMASK_SIMPLE
54#define CPUMASK_SIMPLE(cpu) ((uint64_t)1 << (cpu))
55#define CPUMASK_TESTBIT(val, i) ((val).ary[((i) >> 6) & 3] & \
56 CPUMASK_SIMPLE((i) & 63))
57#define CPUMASK_ORBIT(mask, i) ((mask).ary[((i) >> 6) & 3] |= \
58 CPUMASK_SIMPLE((i) & 63))
59#define CPUMASK_NANDBIT(mask, i) ((mask).ary[((i) >> 6) & 3] &= \
60 ~CPUMASK_SIMPLE((i) & 63))
61#define CPUMASK_ASSZERO(mask) do { \
62 (mask).ary[0] = 0; \
63 (mask).ary[1] = 0; \
64 (mask).ary[2] = 0; \
65 (mask).ary[3] = 0; \
66 } while(0)
67#endif
68
69/*
70 * Define USCHED_GET_CPUMASK as the macro didn't exist until release 4.5.
71 * usched_set(2) returns EINVAL if the kernel doesn't support it, though
72 * fio_getaffinity() returns void.
73 *
74 * Also note usched_set(2) works only for the current thread regardless of
75 * the command type. It doesn't work against another thread regardless of
76 * a caller's privilege. A caller would generally specify 0 for pid for the
77 * current thread though that's the only choice. See BUGS in usched_set(2).
78 */
79#ifndef USCHED_GET_CPUMASK
80#define USCHED_GET_CPUMASK 5
81#endif
82
83static inline int fio_cpuset_init(os_cpu_mask_t *mask)
84{
85 CPUMASK_ASSZERO(*mask);
86 return 0;
87}
88
89static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
90{
91 return 0;
92}
93
94static inline void fio_cpu_clear(os_cpu_mask_t *mask, int cpu)
95{
96 CPUMASK_NANDBIT(*mask, cpu);
97}
98
99static inline void fio_cpu_set(os_cpu_mask_t *mask, int cpu)
100{
101 CPUMASK_ORBIT(*mask, cpu);
102}
103
104static inline int fio_cpu_isset(os_cpu_mask_t *mask, int cpu)
105{
106 if (CPUMASK_TESTBIT(*mask, cpu))
107 return 1;
108
109 return 0;
110}
111
112static inline int fio_cpu_count(os_cpu_mask_t *mask)
113{
114 int i, n = 0;
115
116 for (i = 0; i < FIO_MAX_CPUS; i++)
117 if (CPUMASK_TESTBIT(*mask, i))
118 n++;
119
120 return n;
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 void 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 usched_set(pid, USCHED_GET_CPUMASK, mask, sizeof(*mask));
152}
239b2882 153
1116dc04
TK
154static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
155{
156 struct partinfo pi;
157
158 if (!ioctl(f->fd, DIOCGPART, &pi)) {
159 *bytes = (unsigned long long) pi.media_size;
160 return 0;
161 }
162
163 *bytes = 0;
164 return errno;
165}
166
167static inline int chardev_size(struct fio_file *f, unsigned long long *bytes)
168{
169 return blockdev_size(f, bytes);
170}
171
239b2882
JA
172static inline int blockdev_invalidate_cache(struct fio_file *f)
173{
174 return EINVAL;
175}
176
177static inline unsigned long long os_phys_mem(void)
178{
179 int mib[2] = { CTL_HW, HW_PHYSMEM };
180 uint64_t mem;
181 size_t len = sizeof(mem);
182
183 sysctl(mib, 2, &mem, &len, NULL, 0);
184 return mem;
185}
186
187static inline int gettid(void)
188{
189 return (int) lwp_gettid();
190}
191
c08ad04c 192static inline unsigned long long get_fs_free_size(const char *path)
60f840a8
TK
193{
194 unsigned long long ret;
195 struct statvfs s;
196
197 if (statvfs(path, &s) < 0)
198 return -1ULL;
199
200 ret = s.f_frsize;
201 ret *= (unsigned long long) s.f_bfree;
202 return ret;
203}
204
c9a4e0c8
TK
205static inline int os_trim(int fd, unsigned long long start,
206 unsigned long long len)
207{
208 off_t range[2];
209
210 range[0] = start;
211 range[1] = len;
212
213 if (!ioctl(fd, IOCTLTRIM, range))
214 return 0;
215
216 return errno;
217}
218
239b2882
JA
219#ifdef MADV_FREE
220#define FIO_MADV_FREE MADV_FREE
221#endif
222
223#endif