Make return value type of fio_getaffinity() consistent
[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)
4d8947de 84#endif
ebac4655
JA
85
86#ifndef FIO_HAVE_CPU_AFFINITY
be4ecfdf 87#define fio_cpu_clear(mask, cpu) do { } while (0)
7452440e 88typedef unsigned long os_cpu_mask_t;
3e55d40f
TK
89
90static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
91{
92 return 0;
93}
94
2c6772e2
TK
95static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
96{
97 return -1;
98}
99
3e55d40f
TK
100static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
101{
102 return -1;
103}
104
105static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
106{
107 return 0;
108}
c2acfbac
JA
109#else
110extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
ebac4655
JA
111#endif
112
113#ifndef FIO_HAVE_IOPRIO
28727df7 114#define ioprio_set(which, who, prioclass, prio) (0)
ebac4655
JA
115#endif
116
2c0ecd28 117#ifndef FIO_HAVE_ODIRECT
7d424763
JA
118#define OS_O_DIRECT 0
119#else
120#define OS_O_DIRECT O_DIRECT
2c0ecd28
JA
121#endif
122
d01612f3
CM
123#ifdef OS_O_ATOMIC
124#define FIO_O_ATOMIC OS_O_ATOMIC
125#else
126#define FIO_O_ATOMIC 0
127#endif
128
74b025b0
JA
129#ifndef FIO_HAVE_HUGETLB
130#define SHM_HUGETLB 0
d6dc02fb 131#define MAP_HUGETLB 0
4d8947de 132#ifndef FIO_HUGE_PAGE
74b025b0 133#define FIO_HUGE_PAGE 0
4d8947de 134#endif
74b025b0 135#else
cb25df61 136#ifndef FIO_HUGE_PAGE
ee0e0a71 137#define FIO_HUGE_PAGE 4194304
74b025b0 138#endif
cb25df61 139#endif
74b025b0 140
39b93568
JA
141#ifndef FIO_HAVE_MMAP_HUGE
142#define MAP_HUGETLB 0
143#endif
144
5921e80c
JA
145#ifndef FIO_O_NOATIME
146#define FIO_O_NOATIME 0
147#endif
148
dc873b6f
JA
149#ifndef OS_RAND_MAX
150#define OS_RAND_MAX RAND_MAX
151#endif
152
07e5b264 153#ifndef FIO_HAVE_RAWBIND
8cc7afa9 154#define fio_lookup_raw(dev, majdev, mindev) 1
07e5b264
JA
155#endif
156
58483fa4 157#ifndef FIO_PREFERRED_ENGINE
df96a39e 158#define FIO_PREFERRED_ENGINE "psync"
58483fa4
JA
159#endif
160
b9fd788f
BC
161#ifndef FIO_OS_PATH_SEPARATOR
162#define FIO_OS_PATH_SEPARATOR "/"
163#endif
164
16de1bf9 165#ifndef FIO_PREFERRED_CLOCK_SOURCE
86e53416 166#ifdef CONFIG_CLOCK_GETTIME
16de1bf9 167#define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
86e53416
BC
168#else
169#define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD
170#endif
16de1bf9
BC
171#endif
172
fca70358
JA
173#ifndef FIO_MAX_JOBS
174#define FIO_MAX_JOBS 2048
175#endif
176
67bf9823
JA
177#ifndef CONFIG_SOCKLEN_T
178typedef unsigned int socklen_t;
5ba13ea6
JA
179#endif
180
45054cbe 181#ifndef FIO_OS_HAS_CTIME_R
1f81991e 182#define os_ctime_r(x, y, z) (void) ctime_r((x), (y))
45054cbe
SD
183#endif
184
901ebe18 185#ifdef FIO_USE_GENERIC_SWAP
9677bec3 186static inline uint16_t fio_swap16(uint16_t val)
232f9b73
JA
187{
188 return (val << 8) | (val >> 8);
189}
190
901ebe18 191static inline uint32_t fio_swap32(uint32_t val)
232f9b73
JA
192{
193 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
194
195 return (val >> 16) | (val << 16);
196}
197
901ebe18 198static inline uint64_t fio_swap64(uint64_t val)
232f9b73
JA
199{
200 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
201 ((val & 0x00ff00ff00ff00ffULL) << 8);
202 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
203 ((val & 0x0000ffff0000ffffULL) << 16);
204
205 return (val >> 32) | (val << 32);
206}
207#endif
208
ec5c6b12 209#ifndef FIO_HAVE_BYTEORDER_FUNCS
0dcebdf4 210#ifdef CONFIG_LITTLE_ENDIAN
8111092f
JA
211#define __le16_to_cpu(x) (x)
212#define __le32_to_cpu(x) (x)
213#define __le64_to_cpu(x) (x)
214#define __cpu_to_le16(x) (x)
215#define __cpu_to_le32(x) (x)
216#define __cpu_to_le64(x) (x)
217#else
218#define __le16_to_cpu(x) fio_swap16(x)
219#define __le32_to_cpu(x) fio_swap32(x)
220#define __le64_to_cpu(x) fio_swap64(x)
221#define __cpu_to_le16(x) fio_swap16(x)
222#define __cpu_to_le32(x) fio_swap32(x)
223#define __cpu_to_le64(x) fio_swap64(x)
224#endif
ec5c6b12 225#endif /* FIO_HAVE_BYTEORDER_FUNCS */
8111092f 226
0bbe46e7 227#ifdef FIO_INTERNAL
8111092f 228#define le16_to_cpu(val) ({ \
ff8039b7 229 typecheck(uint16_t, val); \
c46fda90 230 __le16_to_cpu(val); \
8111092f
JA
231})
232#define le32_to_cpu(val) ({ \
ff8039b7 233 typecheck(uint32_t, val); \
c46fda90 234 __le32_to_cpu(val); \
8111092f
JA
235})
236#define le64_to_cpu(val) ({ \
ff8039b7
JA
237 typecheck(uint64_t, val); \
238 __le64_to_cpu(val); \
8111092f 239})
0bbe46e7
DG
240#endif
241
8111092f 242#define cpu_to_le16(val) ({ \
ff8039b7 243 typecheck(uint16_t, val); \
c46fda90 244 __cpu_to_le16(val); \
8111092f
JA
245})
246#define cpu_to_le32(val) ({ \
ff8039b7 247 typecheck(uint32_t, val); \
c46fda90 248 __cpu_to_le32(val); \
8111092f
JA
249})
250#define cpu_to_le64(val) ({ \
ff8039b7 251 typecheck(uint64_t, val); \
c46fda90 252 __cpu_to_le64(val); \
8111092f
JA
253})
254
5e62c22a 255#ifndef FIO_HAVE_BLKTRACE
d95b34a6 256static inline int is_blktrace(const char *fname, int *need_swap)
5e62c22a
JA
257{
258 return 0;
259}
5921e80c 260struct thread_data;
d95b34a6
JA
261static inline int load_blktrace(struct thread_data *td, const char *fname,
262 int need_swap)
5e62c22a
JA
263{
264 return 1;
265}
266#endif
267
eb7ccf38
JA
268#define FIO_DEF_CL_SIZE 128
269
270static inline int os_cache_line_size(void)
271{
272#ifdef FIO_HAVE_CL_SIZE
273 int ret = arch_cache_line_size();
274
275 if (ret <= 0)
276 return FIO_DEF_CL_SIZE;
277
278 return ret;
279#else
280 return FIO_DEF_CL_SIZE;
281#endif
282}
283
792d5517 284#ifdef FIO_USE_GENERIC_BDEV_SIZE
ecc314ba 285static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
792d5517 286{
3b2e1464 287 off_t end;
792d5517 288
3b2e1464
JA
289 *bytes = 0;
290
ecc314ba 291 end = lseek(f->fd, 0, SEEK_END);
792d5517
JA
292 if (end < 0)
293 return errno;
294
295 *bytes = end;
296 return 0;
297}
298#endif
299
53531370
JA
300#ifdef FIO_USE_GENERIC_RAND
301typedef unsigned int os_random_state_t;
302
303static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
304{
305 srand(seed);
306}
307
308static inline long os_random_long(os_random_state_t *rs)
309{
310 long val;
311
312 val = rand_r(rs);
313 return val;
314}
315#endif
316
93bcfd20
BC
317#ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
318extern void td_fill_rand_seeds(struct thread_data *td);
319/*
320 * Initialize the various random states we need (random io, block size ranges,
321 * read/write mix, etc).
322 */
323static inline int init_random_state(struct thread_data *td, unsigned long *rand_seeds, int size)
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);
338 td_fill_rand_seeds(td);
339 return 0;
340}
341#endif
342
2e3bd4c2 343#ifndef FIO_HAVE_FS_STAT
c08ad04c 344static inline unsigned long long get_fs_free_size(const char *path)
2e3bd4c2
JA
345{
346 return 0;
347}
348#endif
349
c5dd5d97
DQZ
350#ifdef __powerpc64__
351#define FIO_HAVE_CPU_ONLINE_SYSCONF
352static inline unsigned int cpus_online(void)
353{
354 return sysconf(_SC_NPROCESSORS_CONF);
355}
356#endif
357
c00a2289
JA
358#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
359static inline unsigned int cpus_online(void)
360{
361 return sysconf(_SC_NPROCESSORS_ONLN);
362}
363#endif
364
7221da37 365#ifndef CPU_COUNT
6cf71c24
JA
366#ifdef FIO_HAVE_CPU_AFFINITY
367static inline int CPU_COUNT(os_cpu_mask_t *mask)
368{
369 int max_cpus = cpus_online();
370 int nr_cpus, i;
371
372 for (i = 0, nr_cpus = 0; i < max_cpus; i++)
373 if (fio_cpu_isset(mask, i))
374 nr_cpus++;
375
376 return nr_cpus;
377}
378#endif
379#endif
380
47f767c1
JA
381#ifndef FIO_HAVE_GETTID
382static inline int gettid(void)
383{
384 return getpid();
385}
386#endif
387
ebac4655 388#endif