Windows: Update the EULA year and add more examples to the installer
[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
836fcc0f 11#include "../arch/arch.h"
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
ec5c6b12
AC
30#if defined(__ANDROID__)
31#include "os-android.h"
32#elif defined(__linux__)
ebac4655
JA
33#include "os-linux.h"
34#elif defined(__FreeBSD__)
35#include "os-freebsd.h"
3cd4c66f
J
36#elif defined(__OpenBSD__)
37#include "os-openbsd.h"
7452440e
YT
38#elif defined(__NetBSD__)
39#include "os-netbsd.h"
2c0ecd28
JA
40#elif defined(__sun__)
41#include "os-solaris.h"
2afd826b
JA
42#elif defined(__APPLE__)
43#include "os-mac.h"
bf2e821a
CC
44#elif defined(_AIX)
45#include "os-aix.h"
c00a2289
JA
46#elif defined(__hpux)
47#include "os-hpux.h"
93bcfd20 48#elif defined(WIN32)
03e20d68 49#include "os-windows.h"
239b2882
JA
50#elif defined (__DragonFly__)
51#include "os-dragonfly.h"
ebac4655
JA
52#else
53#error "unsupported os"
54#endif
55
67bf9823 56#ifdef CONFIG_POSIXAIO
ebac4655 57#include <aio.h>
e97c1442
JA
58#ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
59typedef struct aiocb os_aiocb_t;
60#endif
ebac4655
JA
61#endif
62
63#ifdef FIO_HAVE_SGIO
64#include <linux/fs.h>
65#include <scsi/sg.h>
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
KD
74#endif
75
8e239cae
JA
76#ifdef MSG_DONTWAIT
77#define OS_MSG_DONTWAIT MSG_DONTWAIT
78#endif
79
4d8947de 80#ifndef POSIX_FADV_DONTNEED
ebac4655
JA
81#define POSIX_FADV_DONTNEED (0)
82#define POSIX_FADV_SEQUENTIAL (0)
83#define POSIX_FADV_RANDOM (0)
3ab9ebca 84#define POSIX_FADV_NORMAL (0)
4d8947de 85#endif
ebac4655
JA
86
87#ifndef FIO_HAVE_CPU_AFFINITY
be4ecfdf 88#define fio_cpu_clear(mask, cpu) do { } while (0)
7452440e 89typedef unsigned long os_cpu_mask_t;
3e55d40f
TK
90
91static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
92{
93 return 0;
94}
95
2c6772e2
TK
96static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
97{
98 return -1;
99}
100
3e55d40f
TK
101static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
102{
103 return -1;
104}
105
106static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
107{
108 return 0;
109}
c2acfbac
JA
110#else
111extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
ebac4655
JA
112#endif
113
114#ifndef FIO_HAVE_IOPRIO
28727df7 115#define ioprio_set(which, who, prioclass, prio) (0)
ebac4655
JA
116#endif
117
2c0ecd28 118#ifndef FIO_HAVE_ODIRECT
7d424763
JA
119#define OS_O_DIRECT 0
120#else
121#define OS_O_DIRECT O_DIRECT
2c0ecd28
JA
122#endif
123
d01612f3
CM
124#ifdef OS_O_ATOMIC
125#define FIO_O_ATOMIC OS_O_ATOMIC
126#else
127#define FIO_O_ATOMIC 0
128#endif
129
74b025b0
JA
130#ifndef FIO_HAVE_HUGETLB
131#define SHM_HUGETLB 0
d6dc02fb 132#define MAP_HUGETLB 0
4d8947de 133#ifndef FIO_HUGE_PAGE
74b025b0 134#define FIO_HUGE_PAGE 0
4d8947de 135#endif
74b025b0 136#else
cb25df61 137#ifndef FIO_HUGE_PAGE
ee0e0a71 138#define FIO_HUGE_PAGE 4194304
74b025b0 139#endif
cb25df61 140#endif
74b025b0 141
39b93568
JA
142#ifndef FIO_HAVE_MMAP_HUGE
143#define MAP_HUGETLB 0
144#endif
145
5921e80c
JA
146#ifndef FIO_O_NOATIME
147#define FIO_O_NOATIME 0
148#endif
149
dc873b6f
JA
150#ifndef OS_RAND_MAX
151#define OS_RAND_MAX RAND_MAX
152#endif
153
07e5b264 154#ifndef FIO_HAVE_RAWBIND
8cc7afa9 155#define fio_lookup_raw(dev, majdev, mindev) 1
07e5b264
JA
156#endif
157
58483fa4 158#ifndef FIO_PREFERRED_ENGINE
df96a39e 159#define FIO_PREFERRED_ENGINE "psync"
58483fa4
JA
160#endif
161
b9fd788f
BC
162#ifndef FIO_OS_PATH_SEPARATOR
163#define FIO_OS_PATH_SEPARATOR "/"
164#endif
165
16de1bf9 166#ifndef FIO_PREFERRED_CLOCK_SOURCE
86e53416 167#ifdef CONFIG_CLOCK_GETTIME
16de1bf9 168#define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
86e53416
BC
169#else
170#define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD
171#endif
16de1bf9
BC
172#endif
173
fca70358 174#ifndef FIO_MAX_JOBS
cab2472e 175#define FIO_MAX_JOBS 4096
fca70358
JA
176#endif
177
67bf9823
JA
178#ifndef CONFIG_SOCKLEN_T
179typedef unsigned int socklen_t;
5ba13ea6
JA
180#endif
181
45054cbe 182#ifndef FIO_OS_HAS_CTIME_R
1f81991e 183#define os_ctime_r(x, y, z) (void) ctime_r((x), (y))
45054cbe
SD
184#endif
185
901ebe18 186#ifdef FIO_USE_GENERIC_SWAP
9677bec3 187static inline uint16_t fio_swap16(uint16_t val)
232f9b73
JA
188{
189 return (val << 8) | (val >> 8);
190}
191
901ebe18 192static inline uint32_t fio_swap32(uint32_t val)
232f9b73
JA
193{
194 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
195
196 return (val >> 16) | (val << 16);
197}
198
901ebe18 199static inline uint64_t fio_swap64(uint64_t val)
232f9b73
JA
200{
201 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
202 ((val & 0x00ff00ff00ff00ffULL) << 8);
203 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
204 ((val & 0x0000ffff0000ffffULL) << 16);
205
206 return (val >> 32) | (val << 32);
207}
208#endif
209
ec5c6b12 210#ifndef FIO_HAVE_BYTEORDER_FUNCS
0dcebdf4 211#ifdef CONFIG_LITTLE_ENDIAN
8111092f
JA
212#define __le16_to_cpu(x) (x)
213#define __le32_to_cpu(x) (x)
214#define __le64_to_cpu(x) (x)
215#define __cpu_to_le16(x) (x)
216#define __cpu_to_le32(x) (x)
217#define __cpu_to_le64(x) (x)
218#else
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)
222#define __cpu_to_le16(x) fio_swap16(x)
223#define __cpu_to_le32(x) fio_swap32(x)
224#define __cpu_to_le64(x) fio_swap64(x)
225#endif
ec5c6b12 226#endif /* FIO_HAVE_BYTEORDER_FUNCS */
8111092f 227
0bbe46e7 228#ifdef FIO_INTERNAL
8111092f 229#define le16_to_cpu(val) ({ \
ff8039b7 230 typecheck(uint16_t, val); \
c46fda90 231 __le16_to_cpu(val); \
8111092f
JA
232})
233#define le32_to_cpu(val) ({ \
ff8039b7 234 typecheck(uint32_t, val); \
c46fda90 235 __le32_to_cpu(val); \
8111092f
JA
236})
237#define le64_to_cpu(val) ({ \
ff8039b7
JA
238 typecheck(uint64_t, val); \
239 __le64_to_cpu(val); \
8111092f 240})
0bbe46e7
DG
241#endif
242
8111092f 243#define cpu_to_le16(val) ({ \
ff8039b7 244 typecheck(uint16_t, val); \
c46fda90 245 __cpu_to_le16(val); \
8111092f
JA
246})
247#define cpu_to_le32(val) ({ \
ff8039b7 248 typecheck(uint32_t, val); \
c46fda90 249 __cpu_to_le32(val); \
8111092f
JA
250})
251#define cpu_to_le64(val) ({ \
ff8039b7 252 typecheck(uint64_t, val); \
c46fda90 253 __cpu_to_le64(val); \
8111092f
JA
254})
255
5e62c22a 256#ifndef FIO_HAVE_BLKTRACE
d95b34a6 257static inline int is_blktrace(const char *fname, int *need_swap)
5e62c22a
JA
258{
259 return 0;
260}
5921e80c 261struct thread_data;
d95b34a6
JA
262static inline int load_blktrace(struct thread_data *td, const char *fname,
263 int need_swap)
5e62c22a
JA
264{
265 return 1;
266}
267#endif
268
eb7ccf38
JA
269#define FIO_DEF_CL_SIZE 128
270
271static inline int os_cache_line_size(void)
272{
273#ifdef FIO_HAVE_CL_SIZE
274 int ret = arch_cache_line_size();
275
276 if (ret <= 0)
277 return FIO_DEF_CL_SIZE;
278
279 return ret;
280#else
281 return FIO_DEF_CL_SIZE;
282#endif
283}
284
792d5517 285#ifdef FIO_USE_GENERIC_BDEV_SIZE
ecc314ba 286static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
792d5517 287{
3b2e1464 288 off_t end;
792d5517 289
3b2e1464
JA
290 *bytes = 0;
291
ecc314ba 292 end = lseek(f->fd, 0, SEEK_END);
792d5517
JA
293 if (end < 0)
294 return errno;
295
296 *bytes = end;
297 return 0;
298}
299#endif
300
53531370
JA
301#ifdef FIO_USE_GENERIC_RAND
302typedef unsigned int os_random_state_t;
303
304static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
305{
306 srand(seed);
307}
308
309static inline long os_random_long(os_random_state_t *rs)
310{
311 long val;
312
313 val = rand_r(rs);
314 return val;
315}
316#endif
317
93bcfd20
BC
318#ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
319extern void td_fill_rand_seeds(struct thread_data *td);
320/*
321 * Initialize the various random states we need (random io, block size ranges,
322 * read/write mix, etc).
323 */
324static inline int init_random_state(struct thread_data *td, unsigned long *rand_seeds, int size)
325{
326 int fd;
327
328 fd = open("/dev/urandom", O_RDONLY);
329 if (fd == -1) {
330 return 1;
331 }
332
333 if (read(fd, rand_seeds, size) < size) {
334 close(fd);
335 return 1;
336 }
337
338 close(fd);
339 td_fill_rand_seeds(td);
340 return 0;
341}
342#endif
343
2e3bd4c2 344#ifndef FIO_HAVE_FS_STAT
c08ad04c 345static inline unsigned long long get_fs_free_size(const char *path)
2e3bd4c2
JA
346{
347 return 0;
348}
349#endif
350
c5dd5d97
DQZ
351#ifdef __powerpc64__
352#define FIO_HAVE_CPU_ONLINE_SYSCONF
353static inline unsigned int cpus_online(void)
354{
355 return sysconf(_SC_NPROCESSORS_CONF);
356}
357#endif
358
c00a2289
JA
359#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
360static inline unsigned int cpus_online(void)
361{
362 return sysconf(_SC_NPROCESSORS_ONLN);
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{
370 int max_cpus = cpus_online();
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
JA
382#ifndef FIO_HAVE_GETTID
383static inline int gettid(void)
384{
385 return getpid();
386}
387#endif
388
ebac4655 389#endif