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