Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | #ifndef FIO_OS_H |
2 | #define FIO_OS_H | |
3 | ||
a14ca44a | 4 | #include <sys/types.h> |
203913ea | 5 | #include <sys/socket.h> |
93bcfd20 | 6 | #include <fcntl.h> |
03e20d68 | 7 | #include <pthread.h> |
a14ca44a | 8 | #include <unistd.h> |
ecc314ba | 9 | #include <stdlib.h> |
a14ca44a | 10 | |
c473a9bd | 11 | #include "../arch/arch.h" /* IWYU pragma: export */ |
e39c0676 | 12 | #include "../lib/types.h" |
836fcc0f | 13 | |
cca84643 JA |
14 | enum { |
15 | os_linux = 1, | |
16 | os_aix, | |
17 | os_freebsd, | |
18 | os_hpux, | |
19 | os_mac, | |
20 | os_netbsd, | |
3cd4c66f | 21 | os_openbsd, |
cca84643 JA |
22 | os_solaris, |
23 | os_windows, | |
ec5c6b12 | 24 | os_android, |
239b2882 | 25 | os_dragonfly, |
cca84643 JA |
26 | |
27 | os_nr, | |
28 | }; | |
29 | ||
c473a9bd | 30 | /* IWYU pragma: begin_exports */ |
ec5c6b12 AC |
31 | #if defined(__ANDROID__) |
32 | #include "os-android.h" | |
33 | #elif defined(__linux__) | |
ebac4655 JA |
34 | #include "os-linux.h" |
35 | #elif defined(__FreeBSD__) | |
36 | #include "os-freebsd.h" | |
3cd4c66f J |
37 | #elif defined(__OpenBSD__) |
38 | #include "os-openbsd.h" | |
7452440e YT |
39 | #elif defined(__NetBSD__) |
40 | #include "os-netbsd.h" | |
2c0ecd28 JA |
41 | #elif defined(__sun__) |
42 | #include "os-solaris.h" | |
2afd826b JA |
43 | #elif defined(__APPLE__) |
44 | #include "os-mac.h" | |
bf2e821a CC |
45 | #elif defined(_AIX) |
46 | #include "os-aix.h" | |
c00a2289 JA |
47 | #elif defined(__hpux) |
48 | #include "os-hpux.h" | |
93bcfd20 | 49 | #elif defined(WIN32) |
03e20d68 | 50 | #include "os-windows.h" |
239b2882 JA |
51 | #elif defined (__DragonFly__) |
52 | #include "os-dragonfly.h" | |
ebac4655 JA |
53 | #else |
54 | #error "unsupported os" | |
55 | #endif | |
56 | ||
67bf9823 | 57 | #ifdef CONFIG_POSIXAIO |
ebac4655 | 58 | #include <aio.h> |
e97c1442 JA |
59 | #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF |
60 | typedef struct aiocb os_aiocb_t; | |
61 | #endif | |
ebac4655 JA |
62 | #endif |
63 | ||
4feafb1e | 64 | #ifndef CONFIG_STRSEP |
984f30c9 | 65 | #include "../oslib/strsep.h" |
5921e80c JA |
66 | #endif |
67 | ||
5ad7be56 | 68 | #ifndef CONFIG_STRLCAT |
984f30c9 | 69 | #include "../oslib/strlcat.h" |
5ad7be56 | 70 | #endif |
c473a9bd | 71 | /* IWYU pragma: end_exports */ |
5ad7be56 | 72 | |
8e239cae JA |
73 | #ifdef MSG_DONTWAIT |
74 | #define OS_MSG_DONTWAIT MSG_DONTWAIT | |
75 | #endif | |
76 | ||
4d8947de | 77 | #ifndef POSIX_FADV_DONTNEED |
ebac4655 JA |
78 | #define POSIX_FADV_DONTNEED (0) |
79 | #define POSIX_FADV_SEQUENTIAL (0) | |
80 | #define POSIX_FADV_RANDOM (0) | |
3ab9ebca | 81 | #define POSIX_FADV_NORMAL (0) |
4d8947de | 82 | #endif |
ebac4655 JA |
83 | |
84 | #ifndef FIO_HAVE_CPU_AFFINITY | |
be4ecfdf | 85 | #define fio_cpu_clear(mask, cpu) do { } while (0) |
7452440e | 86 | typedef unsigned long os_cpu_mask_t; |
3e55d40f TK |
87 | |
88 | static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask) | |
89 | { | |
90 | return 0; | |
91 | } | |
92 | ||
2c6772e2 TK |
93 | static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask) |
94 | { | |
95 | return -1; | |
96 | } | |
97 | ||
3e55d40f TK |
98 | static inline int fio_cpuset_exit(os_cpu_mask_t *mask) |
99 | { | |
100 | return -1; | |
101 | } | |
102 | ||
103 | static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index) | |
104 | { | |
105 | return 0; | |
106 | } | |
c2acfbac JA |
107 | #else |
108 | extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu); | |
ebac4655 JA |
109 | #endif |
110 | ||
111 | #ifndef FIO_HAVE_IOPRIO | |
28727df7 | 112 | #define ioprio_set(which, who, prioclass, prio) (0) |
ebac4655 JA |
113 | #endif |
114 | ||
2c0ecd28 | 115 | #ifndef FIO_HAVE_ODIRECT |
7d424763 JA |
116 | #define OS_O_DIRECT 0 |
117 | #else | |
118 | #define OS_O_DIRECT O_DIRECT | |
2c0ecd28 JA |
119 | #endif |
120 | ||
d01612f3 CM |
121 | #ifdef OS_O_ATOMIC |
122 | #define FIO_O_ATOMIC OS_O_ATOMIC | |
123 | #else | |
124 | #define FIO_O_ATOMIC 0 | |
125 | #endif | |
126 | ||
74b025b0 JA |
127 | #ifndef FIO_HAVE_HUGETLB |
128 | #define SHM_HUGETLB 0 | |
d6dc02fb | 129 | #define MAP_HUGETLB 0 |
4d8947de | 130 | #ifndef FIO_HUGE_PAGE |
74b025b0 | 131 | #define FIO_HUGE_PAGE 0 |
4d8947de | 132 | #endif |
74b025b0 | 133 | #else |
cb25df61 | 134 | #ifndef FIO_HUGE_PAGE |
ee0e0a71 | 135 | #define FIO_HUGE_PAGE 4194304 |
74b025b0 | 136 | #endif |
cb25df61 | 137 | #endif |
74b025b0 | 138 | |
39b93568 JA |
139 | #ifndef FIO_HAVE_MMAP_HUGE |
140 | #define MAP_HUGETLB 0 | |
141 | #endif | |
142 | ||
5921e80c JA |
143 | #ifndef FIO_O_NOATIME |
144 | #define FIO_O_NOATIME 0 | |
145 | #endif | |
146 | ||
dc873b6f JA |
147 | #ifndef OS_RAND_MAX |
148 | #define OS_RAND_MAX RAND_MAX | |
149 | #endif | |
150 | ||
07e5b264 | 151 | #ifndef FIO_HAVE_RAWBIND |
8cc7afa9 | 152 | #define fio_lookup_raw(dev, majdev, mindev) 1 |
07e5b264 JA |
153 | #endif |
154 | ||
58483fa4 | 155 | #ifndef FIO_PREFERRED_ENGINE |
df96a39e | 156 | #define FIO_PREFERRED_ENGINE "psync" |
58483fa4 JA |
157 | #endif |
158 | ||
b9fd788f | 159 | #ifndef FIO_OS_PATH_SEPARATOR |
53a7af85 | 160 | #define FIO_OS_PATH_SEPARATOR '/' |
b9fd788f BC |
161 | #endif |
162 | ||
16de1bf9 | 163 | #ifndef FIO_PREFERRED_CLOCK_SOURCE |
86e53416 | 164 | #ifdef CONFIG_CLOCK_GETTIME |
16de1bf9 | 165 | #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME |
86e53416 BC |
166 | #else |
167 | #define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD | |
168 | #endif | |
16de1bf9 BC |
169 | #endif |
170 | ||
fca70358 | 171 | #ifndef FIO_MAX_JOBS |
cab2472e | 172 | #define FIO_MAX_JOBS 4096 |
fca70358 JA |
173 | #endif |
174 | ||
67bf9823 JA |
175 | #ifndef CONFIG_SOCKLEN_T |
176 | typedef unsigned int socklen_t; | |
5ba13ea6 JA |
177 | #endif |
178 | ||
45054cbe | 179 | #ifndef FIO_OS_HAS_CTIME_R |
1f81991e | 180 | #define os_ctime_r(x, y, z) (void) ctime_r((x), (y)) |
45054cbe SD |
181 | #endif |
182 | ||
901ebe18 | 183 | #ifdef FIO_USE_GENERIC_SWAP |
9677bec3 | 184 | static inline uint16_t fio_swap16(uint16_t val) |
232f9b73 JA |
185 | { |
186 | return (val << 8) | (val >> 8); | |
187 | } | |
188 | ||
901ebe18 | 189 | static inline uint32_t fio_swap32(uint32_t val) |
232f9b73 JA |
190 | { |
191 | val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8); | |
192 | ||
193 | return (val >> 16) | (val << 16); | |
194 | } | |
195 | ||
901ebe18 | 196 | static inline uint64_t fio_swap64(uint64_t val) |
232f9b73 JA |
197 | { |
198 | val = ((val & 0xff00ff00ff00ff00ULL) >> 8) | | |
199 | ((val & 0x00ff00ff00ff00ffULL) << 8); | |
200 | val = ((val & 0xffff0000ffff0000ULL) >> 16) | | |
201 | ((val & 0x0000ffff0000ffffULL) << 16); | |
202 | ||
203 | return (val >> 32) | (val << 32); | |
204 | } | |
205 | #endif | |
206 | ||
ec5c6b12 | 207 | #ifndef FIO_HAVE_BYTEORDER_FUNCS |
0dcebdf4 | 208 | #ifdef CONFIG_LITTLE_ENDIAN |
b747af64 | 209 | #define __be64_to_cpu(x) fio_swap64(x) |
8111092f JA |
210 | #define __le16_to_cpu(x) (x) |
211 | #define __le32_to_cpu(x) (x) | |
212 | #define __le64_to_cpu(x) (x) | |
b747af64 | 213 | #define __cpu_to_be64(x) fio_swap64(x) |
8111092f JA |
214 | #define __cpu_to_le16(x) (x) |
215 | #define __cpu_to_le32(x) (x) | |
216 | #define __cpu_to_le64(x) (x) | |
217 | #else | |
b747af64 | 218 | #define __be64_to_cpu(x) (x) |
8111092f JA |
219 | #define __le16_to_cpu(x) fio_swap16(x) |
220 | #define __le32_to_cpu(x) fio_swap32(x) | |
221 | #define __le64_to_cpu(x) fio_swap64(x) | |
b747af64 | 222 | #define __cpu_to_be64(x) (x) |
8111092f JA |
223 | #define __cpu_to_le16(x) fio_swap16(x) |
224 | #define __cpu_to_le32(x) fio_swap32(x) | |
225 | #define __cpu_to_le64(x) fio_swap64(x) | |
226 | #endif | |
ec5c6b12 | 227 | #endif /* FIO_HAVE_BYTEORDER_FUNCS */ |
8111092f | 228 | |
0bbe46e7 | 229 | #ifdef FIO_INTERNAL |
b747af64 SW |
230 | #define be64_to_cpu(val) ({ \ |
231 | typecheck(uint64_t, val); \ | |
232 | __be64_to_cpu(val); \ | |
233 | }) | |
8111092f | 234 | #define le16_to_cpu(val) ({ \ |
ff8039b7 | 235 | typecheck(uint16_t, val); \ |
c46fda90 | 236 | __le16_to_cpu(val); \ |
8111092f JA |
237 | }) |
238 | #define le32_to_cpu(val) ({ \ | |
ff8039b7 | 239 | typecheck(uint32_t, val); \ |
c46fda90 | 240 | __le32_to_cpu(val); \ |
8111092f JA |
241 | }) |
242 | #define le64_to_cpu(val) ({ \ | |
ff8039b7 JA |
243 | typecheck(uint64_t, val); \ |
244 | __le64_to_cpu(val); \ | |
8111092f | 245 | }) |
0bbe46e7 DG |
246 | #endif |
247 | ||
b747af64 SW |
248 | #define cpu_to_be64(val) ({ \ |
249 | typecheck(uint64_t, val); \ | |
250 | __cpu_to_be64(val); \ | |
251 | }) | |
8111092f | 252 | #define cpu_to_le16(val) ({ \ |
ff8039b7 | 253 | typecheck(uint16_t, val); \ |
c46fda90 | 254 | __cpu_to_le16(val); \ |
8111092f JA |
255 | }) |
256 | #define cpu_to_le32(val) ({ \ | |
ff8039b7 | 257 | typecheck(uint32_t, val); \ |
c46fda90 | 258 | __cpu_to_le32(val); \ |
8111092f JA |
259 | }) |
260 | #define cpu_to_le64(val) ({ \ | |
ff8039b7 | 261 | typecheck(uint64_t, val); \ |
c46fda90 | 262 | __cpu_to_le64(val); \ |
8111092f JA |
263 | }) |
264 | ||
eb7ccf38 JA |
265 | #define FIO_DEF_CL_SIZE 128 |
266 | ||
267 | static inline int os_cache_line_size(void) | |
268 | { | |
269 | #ifdef FIO_HAVE_CL_SIZE | |
270 | int ret = arch_cache_line_size(); | |
271 | ||
272 | if (ret <= 0) | |
273 | return FIO_DEF_CL_SIZE; | |
274 | ||
275 | return ret; | |
276 | #else | |
277 | return FIO_DEF_CL_SIZE; | |
278 | #endif | |
279 | } | |
280 | ||
792d5517 | 281 | #ifdef FIO_USE_GENERIC_BDEV_SIZE |
ecc314ba | 282 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
792d5517 | 283 | { |
3b2e1464 | 284 | off_t end; |
792d5517 | 285 | |
3b2e1464 JA |
286 | *bytes = 0; |
287 | ||
ecc314ba | 288 | end = lseek(f->fd, 0, SEEK_END); |
792d5517 JA |
289 | if (end < 0) |
290 | return errno; | |
291 | ||
292 | *bytes = end; | |
293 | return 0; | |
294 | } | |
295 | #endif | |
296 | ||
53531370 JA |
297 | #ifdef FIO_USE_GENERIC_RAND |
298 | typedef unsigned int os_random_state_t; | |
299 | ||
300 | static inline void os_random_seed(unsigned long seed, os_random_state_t *rs) | |
301 | { | |
302 | srand(seed); | |
303 | } | |
304 | ||
305 | static inline long os_random_long(os_random_state_t *rs) | |
306 | { | |
307 | long val; | |
308 | ||
309 | val = rand_r(rs); | |
310 | return val; | |
311 | } | |
312 | #endif | |
313 | ||
93bcfd20 | 314 | #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE |
1ed84d86 | 315 | static inline int init_random_seeds(unsigned long *rand_seeds, int size) |
93bcfd20 BC |
316 | { |
317 | int fd; | |
318 | ||
319 | fd = open("/dev/urandom", O_RDONLY); | |
320 | if (fd == -1) { | |
321 | return 1; | |
322 | } | |
323 | ||
324 | if (read(fd, rand_seeds, size) < size) { | |
325 | close(fd); | |
326 | return 1; | |
327 | } | |
328 | ||
329 | close(fd); | |
93bcfd20 BC |
330 | return 0; |
331 | } | |
332 | #endif | |
333 | ||
2e3bd4c2 | 334 | #ifndef FIO_HAVE_FS_STAT |
c08ad04c | 335 | static inline unsigned long long get_fs_free_size(const char *path) |
2e3bd4c2 JA |
336 | { |
337 | return 0; | |
338 | } | |
339 | #endif | |
340 | ||
c00a2289 JA |
341 | #ifndef FIO_HAVE_CPU_ONLINE_SYSCONF |
342 | static inline unsigned int cpus_online(void) | |
343 | { | |
344 | return sysconf(_SC_NPROCESSORS_ONLN); | |
345 | } | |
346 | #endif | |
347 | ||
7221da37 | 348 | #ifndef CPU_COUNT |
6cf71c24 JA |
349 | #ifdef FIO_HAVE_CPU_AFFINITY |
350 | static inline int CPU_COUNT(os_cpu_mask_t *mask) | |
351 | { | |
352 | int max_cpus = cpus_online(); | |
353 | int nr_cpus, i; | |
354 | ||
355 | for (i = 0, nr_cpus = 0; i < max_cpus; i++) | |
356 | if (fio_cpu_isset(mask, i)) | |
357 | nr_cpus++; | |
358 | ||
359 | return nr_cpus; | |
360 | } | |
361 | #endif | |
362 | #endif | |
363 | ||
47f767c1 JA |
364 | #ifndef FIO_HAVE_GETTID |
365 | static inline int gettid(void) | |
366 | { | |
367 | return getpid(); | |
368 | } | |
369 | #endif | |
370 | ||
0cfe2089 TK |
371 | #ifndef FIO_HAVE_SHM_ATTACH_REMOVED |
372 | static inline int shm_attach_to_open_removed(void) | |
373 | { | |
374 | return 0; | |
375 | } | |
376 | #endif | |
377 | ||
2c3e17be SW |
378 | #ifndef FIO_HAVE_NATIVE_FALLOCATE |
379 | static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len) | |
380 | { | |
381 | errno = ENOSYS; | |
382 | return false; | |
383 | } | |
384 | #endif | |
385 | ||
cbbdf1c8 SW |
386 | #if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE) |
387 | # define FIO_HAVE_ANY_FALLOCATE | |
388 | #endif | |
389 | ||
ebac4655 | 390 | #endif |