Add support for Android
[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
cca84643
JA
11enum {
12 os_linux = 1,
13 os_aix,
14 os_freebsd,
15 os_hpux,
16 os_mac,
17 os_netbsd,
18 os_solaris,
19 os_windows,
ec5c6b12 20 os_android,
cca84643
JA
21
22 os_nr,
23};
24
ec5c6b12
AC
25#if defined(__ANDROID__)
26#include "os-android.h"
27#elif defined(__linux__)
ebac4655
JA
28#include "os-linux.h"
29#elif defined(__FreeBSD__)
30#include "os-freebsd.h"
7452440e
YT
31#elif defined(__NetBSD__)
32#include "os-netbsd.h"
2c0ecd28
JA
33#elif defined(__sun__)
34#include "os-solaris.h"
2afd826b
JA
35#elif defined(__APPLE__)
36#include "os-mac.h"
bf2e821a
CC
37#elif defined(_AIX)
38#include "os-aix.h"
c00a2289
JA
39#elif defined(__hpux)
40#include "os-hpux.h"
93bcfd20 41#elif defined(WIN32)
03e20d68 42#include "os-windows.h"
ebac4655
JA
43#else
44#error "unsupported os"
45#endif
46
47#ifdef FIO_HAVE_LIBAIO
48#include <libaio.h>
49#endif
50
51#ifdef FIO_HAVE_POSIXAIO
52#include <aio.h>
e97c1442
JA
53#ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
54typedef struct aiocb os_aiocb_t;
55#endif
ebac4655
JA
56#endif
57
58#ifdef FIO_HAVE_SGIO
59#include <linux/fs.h>
60#include <scsi/sg.h>
61#endif
62
5921e80c 63#ifndef FIO_HAVE_STRSEP
00fb3c8d 64#include "../lib/strsep.h"
5921e80c
JA
65#endif
66
8e239cae
JA
67#ifdef MSG_DONTWAIT
68#define OS_MSG_DONTWAIT MSG_DONTWAIT
69#endif
70
ebac4655 71#ifndef FIO_HAVE_FADVISE
ec5c6b12
AC
72static inline int posix_fadvise(int fd, int off, int len, int advice)
73{
74 (void)fd;
75 (void)off;
76 (void)len;
77 (void)advice;
78 return 0;
79}
ebac4655 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)
4d8947de 85#endif
ebac4655
JA
86#endif /* FIO_HAVE_FADVISE */
87
88#ifndef FIO_HAVE_CPU_AFFINITY
e8462bd8 89#define fio_setaffinity(pid, mask) (0)
be4ecfdf
JA
90#define fio_getaffinity(pid, mask) do { } while (0)
91#define fio_cpu_clear(mask, cpu) do { } while (0)
85daf2c1 92#define fio_cpuset_exit(mask) (-1)
7452440e 93typedef unsigned long os_cpu_mask_t;
ebac4655
JA
94#endif
95
96#ifndef FIO_HAVE_IOPRIO
97#define ioprio_set(which, who, prio) (0)
98#endif
99
2c0ecd28 100#ifndef FIO_HAVE_ODIRECT
7d424763
JA
101#define OS_O_DIRECT 0
102#else
103#define OS_O_DIRECT O_DIRECT
2c0ecd28
JA
104#endif
105
74b025b0
JA
106#ifndef FIO_HAVE_HUGETLB
107#define SHM_HUGETLB 0
d6dc02fb 108#define MAP_HUGETLB 0
4d8947de 109#ifndef FIO_HUGE_PAGE
74b025b0 110#define FIO_HUGE_PAGE 0
4d8947de 111#endif
74b025b0 112#else
cb25df61 113#ifndef FIO_HUGE_PAGE
ee0e0a71 114#define FIO_HUGE_PAGE 4194304
74b025b0 115#endif
cb25df61 116#endif
74b025b0 117
5921e80c
JA
118#ifndef FIO_O_NOATIME
119#define FIO_O_NOATIME 0
120#endif
121
dc873b6f
JA
122#ifndef OS_RAND_MAX
123#define OS_RAND_MAX RAND_MAX
124#endif
125
ecc314ba
BC
126#ifdef FIO_HAVE_CLOCK_MONOTONIC
127#define FIO_TIMER_CLOCK CLOCK_MONOTONIC
128#else
129#define FIO_TIMER_CLOCK CLOCK_REALTIME
130#endif
131
07e5b264 132#ifndef FIO_HAVE_RAWBIND
8cc7afa9 133#define fio_lookup_raw(dev, majdev, mindev) 1
07e5b264
JA
134#endif
135
58483fa4
JA
136#ifndef FIO_PREFERRED_ENGINE
137#define FIO_PREFERRED_ENGINE "sync"
138#endif
139
b9fd788f
BC
140#ifndef FIO_OS_PATH_SEPARATOR
141#define FIO_OS_PATH_SEPARATOR "/"
142#endif
143
16de1bf9
BC
144#ifndef FIO_PREFERRED_CLOCK_SOURCE
145#define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
146#endif
147
fca70358
JA
148#ifndef FIO_MAX_JOBS
149#define FIO_MAX_JOBS 2048
150#endif
151
5ba13ea6
JA
152#ifndef FIO_OS_HAVE_SOCKLEN_T
153typedef socklen_t fio_socklen_t;
154#endif
155
45054cbe
SD
156#ifndef FIO_OS_HAS_CTIME_R
157#define os_ctime_r(x, y, z) ctime_r((x), (y))
158#endif
159
901ebe18 160#ifdef FIO_USE_GENERIC_SWAP
9677bec3 161static inline uint16_t fio_swap16(uint16_t val)
232f9b73
JA
162{
163 return (val << 8) | (val >> 8);
164}
165
901ebe18 166static inline uint32_t fio_swap32(uint32_t val)
232f9b73
JA
167{
168 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
169
170 return (val >> 16) | (val << 16);
171}
172
901ebe18 173static inline uint64_t fio_swap64(uint64_t val)
232f9b73
JA
174{
175 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
176 ((val & 0x00ff00ff00ff00ffULL) << 8);
177 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
178 ((val & 0x0000ffff0000ffffULL) << 16);
179
180 return (val >> 32) | (val << 32);
181}
182#endif
183
ec5c6b12 184#ifndef FIO_HAVE_BYTEORDER_FUNCS
8111092f
JA
185#ifdef FIO_LITTLE_ENDIAN
186#define __le16_to_cpu(x) (x)
187#define __le32_to_cpu(x) (x)
188#define __le64_to_cpu(x) (x)
189#define __cpu_to_le16(x) (x)
190#define __cpu_to_le32(x) (x)
191#define __cpu_to_le64(x) (x)
192#else
193#define __le16_to_cpu(x) fio_swap16(x)
194#define __le32_to_cpu(x) fio_swap32(x)
195#define __le64_to_cpu(x) fio_swap64(x)
196#define __cpu_to_le16(x) fio_swap16(x)
197#define __cpu_to_le32(x) fio_swap32(x)
198#define __cpu_to_le64(x) fio_swap64(x)
199#endif
ec5c6b12 200#endif /* FIO_HAVE_BYTEORDER_FUNCS */
8111092f
JA
201
202#define le16_to_cpu(val) ({ \
203 uint16_t *__val = &(val); \
204 __le16_to_cpu(*__val); \
205})
206#define le32_to_cpu(val) ({ \
207 uint32_t *__val = &(val); \
208 __le32_to_cpu(*__val); \
209})
210#define le64_to_cpu(val) ({ \
211 uint64_t *__val = &(val); \
212 __le64_to_cpu(*__val); \
213})
214#define cpu_to_le16(val) ({ \
215 uint16_t *__val = &(val); \
216 __cpu_to_le16(*__val); \
217})
218#define cpu_to_le32(val) ({ \
219 uint32_t *__val = &(val); \
220 __cpu_to_le32(*__val); \
221})
222#define cpu_to_le64(val) ({ \
223 uint64_t *__val = &(val); \
224 __cpu_to_le64(*__val); \
225})
226
5e62c22a
JA
227#ifndef FIO_HAVE_BLKTRACE
228static inline int is_blktrace(const char *fname)
229{
230 return 0;
231}
5921e80c 232struct thread_data;
5e62c22a
JA
233static inline int load_blktrace(struct thread_data *td, const char *fname)
234{
235 return 1;
236}
237#endif
238
eb7ccf38
JA
239#define FIO_DEF_CL_SIZE 128
240
241static inline int os_cache_line_size(void)
242{
243#ifdef FIO_HAVE_CL_SIZE
244 int ret = arch_cache_line_size();
245
246 if (ret <= 0)
247 return FIO_DEF_CL_SIZE;
248
249 return ret;
250#else
251 return FIO_DEF_CL_SIZE;
252#endif
253}
254
792d5517 255#ifdef FIO_USE_GENERIC_BDEV_SIZE
ecc314ba 256static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
792d5517 257{
3b2e1464 258 off_t end;
792d5517 259
3b2e1464
JA
260 *bytes = 0;
261
ecc314ba 262 end = lseek(f->fd, 0, SEEK_END);
792d5517
JA
263 if (end < 0)
264 return errno;
265
266 *bytes = end;
267 return 0;
268}
269#endif
270
53531370
JA
271#ifdef FIO_USE_GENERIC_RAND
272typedef unsigned int os_random_state_t;
273
274static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
275{
276 srand(seed);
277}
278
279static inline long os_random_long(os_random_state_t *rs)
280{
281 long val;
282
283 val = rand_r(rs);
284 return val;
285}
286#endif
287
93bcfd20
BC
288#ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
289extern void td_fill_rand_seeds(struct thread_data *td);
290/*
291 * Initialize the various random states we need (random io, block size ranges,
292 * read/write mix, etc).
293 */
294static inline int init_random_state(struct thread_data *td, unsigned long *rand_seeds, int size)
295{
296 int fd;
297
298 fd = open("/dev/urandom", O_RDONLY);
299 if (fd == -1) {
300 return 1;
301 }
302
303 if (read(fd, rand_seeds, size) < size) {
304 close(fd);
305 return 1;
306 }
307
308 close(fd);
309 td_fill_rand_seeds(td);
310 return 0;
311}
312#endif
313
2e3bd4c2
JA
314#ifndef FIO_HAVE_FS_STAT
315static inline unsigned long long get_fs_size(const char *path)
316{
317 return 0;
318}
319#endif
320
c00a2289
JA
321#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
322static inline unsigned int cpus_online(void)
323{
324 return sysconf(_SC_NPROCESSORS_ONLN);
325}
326#endif
327
47f767c1
JA
328#ifndef FIO_HAVE_GETTID
329static inline int gettid(void)
330{
331 return getpid();
332}
333#endif
334
ebac4655 335#endif