blob: 12188156cf280afea5ca29935d74ee794058228b [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001#ifndef FIO_OS_H
2#define FIO_OS_H
3
Jens Axboea14ca442009-12-20 22:24:10 +01004#include <sys/types.h>
Bruce Cran03e20d62011-01-02 20:14:54 +01005#include <pthread.h>
Jens Axboea14ca442009-12-20 22:24:10 +01006#include <unistd.h>
Bruce Cranecc314b2011-01-04 10:59:30 +01007#include <stdlib.h>
Jens Axboea14ca442009-12-20 22:24:10 +01008
Jens Axboecca84642011-10-07 12:47:57 +02009enum {
10 os_linux = 1,
11 os_aix,
12 os_freebsd,
13 os_hpux,
14 os_mac,
15 os_netbsd,
16 os_solaris,
17 os_windows,
18
19 os_nr,
20};
21
Jens Axboeebac4652005-12-08 15:25:21 +010022#if defined(__linux__)
23#include "os-linux.h"
24#elif defined(__FreeBSD__)
25#include "os-freebsd.h"
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020026#elif defined(__NetBSD__)
27#include "os-netbsd.h"
Jens Axboe2c0ecd22006-06-08 13:25:41 +020028#elif defined(__sun__)
29#include "os-solaris.h"
Jens Axboe2afd8262009-12-14 23:08:42 +010030#elif defined(__APPLE__)
31#include "os-mac.h"
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040032#elif defined(_AIX)
33#include "os-aix.h"
Jens Axboec00a2282011-07-08 20:56:06 +020034#elif defined(__hpux)
35#include "os-hpux.h"
Bruce Cran03e20d62011-01-02 20:14:54 +010036#elif defined(__CYGWIN__)
37#include "os-windows.h"
Jens Axboeebac4652005-12-08 15:25:21 +010038#else
39#error "unsupported os"
40#endif
41
42#ifdef FIO_HAVE_LIBAIO
43#include <libaio.h>
44#endif
45
46#ifdef FIO_HAVE_POSIXAIO
47#include <aio.h>
Jens Axboee97c1442011-09-21 09:38:01 +020048#ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
49typedef struct aiocb os_aiocb_t;
50#endif
Jens Axboeebac4652005-12-08 15:25:21 +010051#endif
52
53#ifdef FIO_HAVE_SGIO
54#include <linux/fs.h>
55#include <scsi/sg.h>
56#endif
57
Jens Axboe5921e802008-05-30 15:02:38 +020058#ifndef FIO_HAVE_STRSEP
Jens Axboe00fb3c82008-05-30 22:17:45 +020059#include "../lib/strsep.h"
Jens Axboe5921e802008-05-30 15:02:38 +020060#endif
61
Jens Axboe8e239ca2010-08-11 10:29:12 -040062#ifdef MSG_DONTWAIT
63#define OS_MSG_DONTWAIT MSG_DONTWAIT
64#endif
65
Jens Axboeebac4652005-12-08 15:25:21 +010066#ifndef FIO_HAVE_FADVISE
Bruce Cranecc314b2011-01-04 10:59:30 +010067#define posix_fadvise(fd, off, len, advice) (0)
Jens Axboeebac4652005-12-08 15:25:21 +010068
Shawn Lewis4d8947d2007-07-25 07:51:58 +020069#ifndef POSIX_FADV_DONTNEED
Jens Axboeebac4652005-12-08 15:25:21 +010070#define POSIX_FADV_DONTNEED (0)
71#define POSIX_FADV_SEQUENTIAL (0)
72#define POSIX_FADV_RANDOM (0)
Shawn Lewis4d8947d2007-07-25 07:51:58 +020073#endif
Jens Axboeebac4652005-12-08 15:25:21 +010074#endif /* FIO_HAVE_FADVISE */
75
76#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboee8462bd2009-07-06 12:59:04 +020077#define fio_setaffinity(pid, mask) (0)
Jens Axboebe4ecfd2008-12-08 14:10:52 +010078#define fio_getaffinity(pid, mask) do { } while (0)
79#define fio_cpu_clear(mask, cpu) do { } while (0)
Jens Axboe85daf2c2009-01-05 09:40:13 +010080#define fio_cpuset_exit(mask) (-1)
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020081typedef unsigned long os_cpu_mask_t;
Jens Axboeebac4652005-12-08 15:25:21 +010082#endif
83
84#ifndef FIO_HAVE_IOPRIO
85#define ioprio_set(which, who, prio) (0)
86#endif
87
Jens Axboe2c0ecd22006-06-08 13:25:41 +020088#ifndef FIO_HAVE_ODIRECT
Jens Axboe7d424762006-06-08 13:26:31 +020089#define OS_O_DIRECT 0
90#else
91#define OS_O_DIRECT O_DIRECT
Jens Axboe2c0ecd22006-06-08 13:25:41 +020092#endif
93
Jens Axboe74b025b2006-12-19 15:18:14 +010094#ifndef FIO_HAVE_HUGETLB
95#define SHM_HUGETLB 0
Shawn Lewis4d8947d2007-07-25 07:51:58 +020096#ifndef FIO_HUGE_PAGE
Jens Axboe74b025b2006-12-19 15:18:14 +010097#define FIO_HUGE_PAGE 0
Shawn Lewis4d8947d2007-07-25 07:51:58 +020098#endif
Jens Axboe74b025b2006-12-19 15:18:14 +010099#else
Jens Axboecb25df62007-03-23 08:23:30 +0100100#ifndef FIO_HUGE_PAGE
Jens Axboeee0e0a72007-03-19 10:50:47 +0100101#define FIO_HUGE_PAGE 4194304
Jens Axboe74b025b2006-12-19 15:18:14 +0100102#endif
Jens Axboecb25df62007-03-23 08:23:30 +0100103#endif
Jens Axboe74b025b2006-12-19 15:18:14 +0100104
Jens Axboe5921e802008-05-30 15:02:38 +0200105#ifndef FIO_O_NOATIME
106#define FIO_O_NOATIME 0
107#endif
108
Jens Axboedc873b62008-06-04 20:13:04 +0200109#ifndef OS_RAND_MAX
110#define OS_RAND_MAX RAND_MAX
111#endif
112
Bruce Cranecc314b2011-01-04 10:59:30 +0100113#ifdef FIO_HAVE_CLOCK_MONOTONIC
114#define FIO_TIMER_CLOCK CLOCK_MONOTONIC
115#else
116#define FIO_TIMER_CLOCK CLOCK_REALTIME
117#endif
118
Jens Axboe07e5b262007-04-02 14:46:07 +0200119#ifndef FIO_HAVE_RAWBIND
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200120#define fio_lookup_raw(dev, majdev, mindev) 1
Jens Axboe07e5b262007-04-02 14:46:07 +0200121#endif
122
Jens Axboe58483fa2011-01-19 11:09:58 -0700123#ifndef FIO_PREFERRED_ENGINE
124#define FIO_PREFERRED_ENGINE "sync"
125#endif
126
Jens Axboefca70352011-07-06 20:12:54 +0200127#ifndef FIO_MAX_JOBS
128#define FIO_MAX_JOBS 2048
129#endif
130
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200131#ifndef FIO_OS_HAVE_SOCKLEN_T
132typedef socklen_t fio_socklen_t;
133#endif
134
Jens Axboe901ebe12011-10-04 23:31:00 +0200135#ifdef FIO_USE_GENERIC_SWAP
Jens Axboe9677bec2011-10-04 16:02:51 +0200136static inline uint16_t fio_swap16(uint16_t val)
Jens Axboe232f9b72011-10-04 14:45:20 +0200137{
138 return (val << 8) | (val >> 8);
139}
140
Jens Axboe901ebe12011-10-04 23:31:00 +0200141static inline uint32_t fio_swap32(uint32_t val)
Jens Axboe232f9b72011-10-04 14:45:20 +0200142{
143 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
144
145 return (val >> 16) | (val << 16);
146}
147
Jens Axboe901ebe12011-10-04 23:31:00 +0200148static inline uint64_t fio_swap64(uint64_t val)
Jens Axboe232f9b72011-10-04 14:45:20 +0200149{
150 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
151 ((val & 0x00ff00ff00ff00ffULL) << 8);
152 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
153 ((val & 0x0000ffff0000ffffULL) << 16);
154
155 return (val >> 32) | (val << 32);
156}
157#endif
158
Jens Axboe81110922011-10-05 12:40:49 +0200159#ifdef FIO_LITTLE_ENDIAN
160#define __le16_to_cpu(x) (x)
161#define __le32_to_cpu(x) (x)
162#define __le64_to_cpu(x) (x)
163#define __cpu_to_le16(x) (x)
164#define __cpu_to_le32(x) (x)
165#define __cpu_to_le64(x) (x)
166#else
167#define __le16_to_cpu(x) fio_swap16(x)
168#define __le32_to_cpu(x) fio_swap32(x)
169#define __le64_to_cpu(x) fio_swap64(x)
170#define __cpu_to_le16(x) fio_swap16(x)
171#define __cpu_to_le32(x) fio_swap32(x)
172#define __cpu_to_le64(x) fio_swap64(x)
173#endif
174
175#define le16_to_cpu(val) ({ \
176 uint16_t *__val = &(val); \
177 __le16_to_cpu(*__val); \
178})
179#define le32_to_cpu(val) ({ \
180 uint32_t *__val = &(val); \
181 __le32_to_cpu(*__val); \
182})
183#define le64_to_cpu(val) ({ \
184 uint64_t *__val = &(val); \
185 __le64_to_cpu(*__val); \
186})
187#define cpu_to_le16(val) ({ \
188 uint16_t *__val = &(val); \
189 __cpu_to_le16(*__val); \
190})
191#define cpu_to_le32(val) ({ \
192 uint32_t *__val = &(val); \
193 __cpu_to_le32(*__val); \
194})
195#define cpu_to_le64(val) ({ \
196 uint64_t *__val = &(val); \
197 __cpu_to_le64(*__val); \
198})
199
Jens Axboe5e62c222007-05-22 13:27:30 +0200200#ifndef FIO_HAVE_BLKTRACE
201static inline int is_blktrace(const char *fname)
202{
203 return 0;
204}
Jens Axboe5921e802008-05-30 15:02:38 +0200205struct thread_data;
Jens Axboe5e62c222007-05-22 13:27:30 +0200206static inline int load_blktrace(struct thread_data *td, const char *fname)
207{
208 return 1;
209}
210#endif
211
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200212#define FIO_DEF_CL_SIZE 128
213
214static inline int os_cache_line_size(void)
215{
216#ifdef FIO_HAVE_CL_SIZE
217 int ret = arch_cache_line_size();
218
219 if (ret <= 0)
220 return FIO_DEF_CL_SIZE;
221
222 return ret;
223#else
224 return FIO_DEF_CL_SIZE;
225#endif
226}
227
Jens Axboe792d5512009-09-22 17:12:29 +0200228#ifdef FIO_USE_GENERIC_BDEV_SIZE
Bruce Cranecc314b2011-01-04 10:59:30 +0100229static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboe792d5512009-09-22 17:12:29 +0200230{
Jens Axboe3b2e1462009-12-15 08:58:10 +0100231 off_t end;
Jens Axboe792d5512009-09-22 17:12:29 +0200232
Jens Axboe3b2e1462009-12-15 08:58:10 +0100233 *bytes = 0;
234
Bruce Cranecc314b2011-01-04 10:59:30 +0100235 end = lseek(f->fd, 0, SEEK_END);
Jens Axboe792d5512009-09-22 17:12:29 +0200236 if (end < 0)
237 return errno;
238
239 *bytes = end;
240 return 0;
241}
242#endif
243
Jens Axboe53531372009-12-15 10:28:37 +0100244#ifdef FIO_USE_GENERIC_RAND
245typedef unsigned int os_random_state_t;
246
247static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
248{
249 srand(seed);
250}
251
252static inline long os_random_long(os_random_state_t *rs)
253{
254 long val;
255
256 val = rand_r(rs);
257 return val;
258}
259#endif
260
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200261#ifndef FIO_HAVE_FS_STAT
262static inline unsigned long long get_fs_size(const char *path)
263{
264 return 0;
265}
266#endif
267
Jens Axboec00a2282011-07-08 20:56:06 +0200268#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
269static inline unsigned int cpus_online(void)
270{
271 return sysconf(_SC_NPROCESSORS_ONLN);
272}
273#endif
274
Jens Axboe47f767c2011-07-12 21:17:49 +0200275#ifndef FIO_HAVE_GETTID
276static inline int gettid(void)
277{
278 return getpid();
279}
280#endif
281
Jens Axboeebac4652005-12-08 15:25:21 +0100282#endif