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