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