Add gettid() for proper thread affinity on Linux
[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>
03e20d68 5#include <pthread.h>
a14ca44a 6#include <unistd.h>
ecc314ba 7#include <stdlib.h>
a14ca44a 8
ebac4655
JA
9#if defined(__linux__)
10#include "os-linux.h"
11#elif defined(__FreeBSD__)
12#include "os-freebsd.h"
7452440e
YT
13#elif defined(__NetBSD__)
14#include "os-netbsd.h"
2c0ecd28
JA
15#elif defined(__sun__)
16#include "os-solaris.h"
2afd826b
JA
17#elif defined(__APPLE__)
18#include "os-mac.h"
bf2e821a
CC
19#elif defined(_AIX)
20#include "os-aix.h"
c00a2289
JA
21#elif defined(__hpux)
22#include "os-hpux.h"
03e20d68
BC
23#elif defined(__CYGWIN__)
24#include "os-windows.h"
ebac4655
JA
25#else
26#error "unsupported os"
27#endif
28
29#ifdef FIO_HAVE_LIBAIO
30#include <libaio.h>
31#endif
32
33#ifdef FIO_HAVE_POSIXAIO
34#include <aio.h>
35#endif
36
37#ifdef FIO_HAVE_SGIO
38#include <linux/fs.h>
39#include <scsi/sg.h>
40#endif
41
5921e80c 42#ifndef FIO_HAVE_STRSEP
00fb3c8d 43#include "../lib/strsep.h"
5921e80c
JA
44#endif
45
8e239cae
JA
46#ifdef MSG_DONTWAIT
47#define OS_MSG_DONTWAIT MSG_DONTWAIT
48#endif
49
ebac4655 50#ifndef FIO_HAVE_FADVISE
ecc314ba 51#define posix_fadvise(fd, off, len, advice) (0)
ebac4655 52
4d8947de 53#ifndef POSIX_FADV_DONTNEED
ebac4655
JA
54#define POSIX_FADV_DONTNEED (0)
55#define POSIX_FADV_SEQUENTIAL (0)
56#define POSIX_FADV_RANDOM (0)
4d8947de 57#endif
ebac4655
JA
58#endif /* FIO_HAVE_FADVISE */
59
60#ifndef FIO_HAVE_CPU_AFFINITY
e8462bd8 61#define fio_setaffinity(pid, mask) (0)
be4ecfdf
JA
62#define fio_getaffinity(pid, mask) do { } while (0)
63#define fio_cpu_clear(mask, cpu) do { } while (0)
85daf2c1 64#define fio_cpuset_exit(mask) (-1)
7452440e 65typedef unsigned long os_cpu_mask_t;
ebac4655
JA
66#endif
67
68#ifndef FIO_HAVE_IOPRIO
69#define ioprio_set(which, who, prio) (0)
70#endif
71
2c0ecd28 72#ifndef FIO_HAVE_ODIRECT
7d424763
JA
73#define OS_O_DIRECT 0
74#else
75#define OS_O_DIRECT O_DIRECT
2c0ecd28
JA
76#endif
77
74b025b0
JA
78#ifndef FIO_HAVE_HUGETLB
79#define SHM_HUGETLB 0
4d8947de 80#ifndef FIO_HUGE_PAGE
74b025b0 81#define FIO_HUGE_PAGE 0
4d8947de 82#endif
74b025b0 83#else
cb25df61 84#ifndef FIO_HUGE_PAGE
ee0e0a71 85#define FIO_HUGE_PAGE 4194304
74b025b0 86#endif
cb25df61 87#endif
74b025b0 88
5921e80c
JA
89#ifndef FIO_O_NOATIME
90#define FIO_O_NOATIME 0
91#endif
92
dc873b6f
JA
93#ifndef OS_RAND_MAX
94#define OS_RAND_MAX RAND_MAX
95#endif
96
ecc314ba
BC
97#ifdef FIO_HAVE_CLOCK_MONOTONIC
98#define FIO_TIMER_CLOCK CLOCK_MONOTONIC
99#else
100#define FIO_TIMER_CLOCK CLOCK_REALTIME
101#endif
102
07e5b264 103#ifndef FIO_HAVE_RAWBIND
8cc7afa9 104#define fio_lookup_raw(dev, majdev, mindev) 1
07e5b264
JA
105#endif
106
58483fa4
JA
107#ifndef FIO_PREFERRED_ENGINE
108#define FIO_PREFERRED_ENGINE "sync"
109#endif
110
fca70358
JA
111#ifndef FIO_MAX_JOBS
112#define FIO_MAX_JOBS 2048
113#endif
114
5e62c22a
JA
115#ifndef FIO_HAVE_BLKTRACE
116static inline int is_blktrace(const char *fname)
117{
118 return 0;
119}
5921e80c 120struct thread_data;
5e62c22a
JA
121static inline int load_blktrace(struct thread_data *td, const char *fname)
122{
123 return 1;
124}
125#endif
126
eb7ccf38
JA
127#define FIO_DEF_CL_SIZE 128
128
129static inline int os_cache_line_size(void)
130{
131#ifdef FIO_HAVE_CL_SIZE
132 int ret = arch_cache_line_size();
133
134 if (ret <= 0)
135 return FIO_DEF_CL_SIZE;
136
137 return ret;
138#else
139 return FIO_DEF_CL_SIZE;
140#endif
141}
142
792d5517 143#ifdef FIO_USE_GENERIC_BDEV_SIZE
ecc314ba 144static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
792d5517 145{
3b2e1464 146 off_t end;
792d5517 147
3b2e1464
JA
148 *bytes = 0;
149
ecc314ba 150 end = lseek(f->fd, 0, SEEK_END);
792d5517
JA
151 if (end < 0)
152 return errno;
153
154 *bytes = end;
155 return 0;
156}
157#endif
158
53531370
JA
159#ifdef FIO_USE_GENERIC_RAND
160typedef unsigned int os_random_state_t;
161
162static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
163{
164 srand(seed);
165}
166
167static inline long os_random_long(os_random_state_t *rs)
168{
169 long val;
170
171 val = rand_r(rs);
172 return val;
173}
174#endif
175
2e3bd4c2
JA
176#ifndef FIO_HAVE_FS_STAT
177static inline unsigned long long get_fs_size(const char *path)
178{
179 return 0;
180}
181#endif
182
c00a2289
JA
183#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
184static inline unsigned int cpus_online(void)
185{
186 return sysconf(_SC_NPROCESSORS_ONLN);
187}
188#endif
189
47f767c1
JA
190#ifndef FIO_HAVE_GETTID
191static inline int gettid(void)
192{
193 return getpid();
194}
195#endif
196
ebac4655 197#endif