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