blob: e1c285ce5a5152e6e7cb76af71687efd473dc845 [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 Axboeebac4652005-12-08 15:25:21 +01009#if defined(__linux__)
10#include "os-linux.h"
11#elif defined(__FreeBSD__)
12#include "os-freebsd.h"
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020013#elif defined(__NetBSD__)
14#include "os-netbsd.h"
Jens Axboe2c0ecd22006-06-08 13:25:41 +020015#elif defined(__sun__)
16#include "os-solaris.h"
Jens Axboe2afd8262009-12-14 23:08:42 +010017#elif defined(__APPLE__)
18#include "os-mac.h"
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040019#elif defined(_AIX)
20#include "os-aix.h"
Jens Axboec00a2282011-07-08 20:56:06 +020021#elif defined(__hpux)
22#include "os-hpux.h"
Bruce Cran03e20d62011-01-02 20:14:54 +010023#elif defined(__CYGWIN__)
24#include "os-windows.h"
Jens Axboeebac4652005-12-08 15:25:21 +010025#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>
Jens Axboee97c1442011-09-21 09:38:01 +020035#ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
36typedef struct aiocb os_aiocb_t;
37#endif
Jens Axboeebac4652005-12-08 15:25:21 +010038#endif
39
40#ifdef FIO_HAVE_SGIO
41#include <linux/fs.h>
42#include <scsi/sg.h>
43#endif
44
Jens Axboe5921e802008-05-30 15:02:38 +020045#ifndef FIO_HAVE_STRSEP
Jens Axboe00fb3c82008-05-30 22:17:45 +020046#include "../lib/strsep.h"
Jens Axboe5921e802008-05-30 15:02:38 +020047#endif
48
Jens Axboe8e239ca2010-08-11 10:29:12 -040049#ifdef MSG_DONTWAIT
50#define OS_MSG_DONTWAIT MSG_DONTWAIT
51#endif
52
Jens Axboeebac4652005-12-08 15:25:21 +010053#ifndef FIO_HAVE_FADVISE
Bruce Cranecc314b2011-01-04 10:59:30 +010054#define posix_fadvise(fd, off, len, advice) (0)
Jens Axboeebac4652005-12-08 15:25:21 +010055
Shawn Lewis4d8947d2007-07-25 07:51:58 +020056#ifndef POSIX_FADV_DONTNEED
Jens Axboeebac4652005-12-08 15:25:21 +010057#define POSIX_FADV_DONTNEED (0)
58#define POSIX_FADV_SEQUENTIAL (0)
59#define POSIX_FADV_RANDOM (0)
Shawn Lewis4d8947d2007-07-25 07:51:58 +020060#endif
Jens Axboeebac4652005-12-08 15:25:21 +010061#endif /* FIO_HAVE_FADVISE */
62
63#ifndef FIO_HAVE_CPU_AFFINITY
Jens Axboee8462bd2009-07-06 12:59:04 +020064#define fio_setaffinity(pid, mask) (0)
Jens Axboebe4ecfd2008-12-08 14:10:52 +010065#define fio_getaffinity(pid, mask) do { } while (0)
66#define fio_cpu_clear(mask, cpu) do { } while (0)
Jens Axboe85daf2c2009-01-05 09:40:13 +010067#define fio_cpuset_exit(mask) (-1)
YAMAMOTO Takashi74524402010-05-12 15:06:46 +020068typedef unsigned long os_cpu_mask_t;
Jens Axboeebac4652005-12-08 15:25:21 +010069#endif
70
71#ifndef FIO_HAVE_IOPRIO
72#define ioprio_set(which, who, prio) (0)
73#endif
74
Jens Axboe2c0ecd22006-06-08 13:25:41 +020075#ifndef FIO_HAVE_ODIRECT
Jens Axboe7d424762006-06-08 13:26:31 +020076#define OS_O_DIRECT 0
77#else
78#define OS_O_DIRECT O_DIRECT
Jens Axboe2c0ecd22006-06-08 13:25:41 +020079#endif
80
Jens Axboe74b025b2006-12-19 15:18:14 +010081#ifndef FIO_HAVE_HUGETLB
82#define SHM_HUGETLB 0
Shawn Lewis4d8947d2007-07-25 07:51:58 +020083#ifndef FIO_HUGE_PAGE
Jens Axboe74b025b2006-12-19 15:18:14 +010084#define FIO_HUGE_PAGE 0
Shawn Lewis4d8947d2007-07-25 07:51:58 +020085#endif
Jens Axboe74b025b2006-12-19 15:18:14 +010086#else
Jens Axboecb25df62007-03-23 08:23:30 +010087#ifndef FIO_HUGE_PAGE
Jens Axboeee0e0a72007-03-19 10:50:47 +010088#define FIO_HUGE_PAGE 4194304
Jens Axboe74b025b2006-12-19 15:18:14 +010089#endif
Jens Axboecb25df62007-03-23 08:23:30 +010090#endif
Jens Axboe74b025b2006-12-19 15:18:14 +010091
Jens Axboe5921e802008-05-30 15:02:38 +020092#ifndef FIO_O_NOATIME
93#define FIO_O_NOATIME 0
94#endif
95
Jens Axboedc873b62008-06-04 20:13:04 +020096#ifndef OS_RAND_MAX
97#define OS_RAND_MAX RAND_MAX
98#endif
99
Bruce Cranecc314b2011-01-04 10:59:30 +0100100#ifdef FIO_HAVE_CLOCK_MONOTONIC
101#define FIO_TIMER_CLOCK CLOCK_MONOTONIC
102#else
103#define FIO_TIMER_CLOCK CLOCK_REALTIME
104#endif
105
Jens Axboe07e5b262007-04-02 14:46:07 +0200106#ifndef FIO_HAVE_RAWBIND
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200107#define fio_lookup_raw(dev, majdev, mindev) 1
Jens Axboe07e5b262007-04-02 14:46:07 +0200108#endif
109
Jens Axboe58483fa2011-01-19 11:09:58 -0700110#ifndef FIO_PREFERRED_ENGINE
111#define FIO_PREFERRED_ENGINE "sync"
112#endif
113
Jens Axboefca70352011-07-06 20:12:54 +0200114#ifndef FIO_MAX_JOBS
115#define FIO_MAX_JOBS 2048
116#endif
117
Jens Axboe5ba13ea2011-10-04 23:50:28 +0200118#ifndef FIO_OS_HAVE_SOCKLEN_T
119typedef socklen_t fio_socklen_t;
120#endif
121
Jens Axboe901ebe12011-10-04 23:31:00 +0200122#ifdef FIO_USE_GENERIC_SWAP
Jens Axboe9677bec2011-10-04 16:02:51 +0200123static inline uint16_t fio_swap16(uint16_t val)
Jens Axboe232f9b72011-10-04 14:45:20 +0200124{
125 return (val << 8) | (val >> 8);
126}
127
Jens Axboe901ebe12011-10-04 23:31:00 +0200128static inline uint32_t fio_swap32(uint32_t val)
Jens Axboe232f9b72011-10-04 14:45:20 +0200129{
130 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
131
132 return (val >> 16) | (val << 16);
133}
134
Jens Axboe901ebe12011-10-04 23:31:00 +0200135static inline uint64_t fio_swap64(uint64_t val)
Jens Axboe232f9b72011-10-04 14:45:20 +0200136{
137 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
138 ((val & 0x00ff00ff00ff00ffULL) << 8);
139 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
140 ((val & 0x0000ffff0000ffffULL) << 16);
141
142 return (val >> 32) | (val << 32);
143}
144#endif
145
Jens Axboe81110922011-10-05 12:40:49 +0200146#ifdef FIO_LITTLE_ENDIAN
147#define __le16_to_cpu(x) (x)
148#define __le32_to_cpu(x) (x)
149#define __le64_to_cpu(x) (x)
150#define __cpu_to_le16(x) (x)
151#define __cpu_to_le32(x) (x)
152#define __cpu_to_le64(x) (x)
153#else
154#define __le16_to_cpu(x) fio_swap16(x)
155#define __le32_to_cpu(x) fio_swap32(x)
156#define __le64_to_cpu(x) fio_swap64(x)
157#define __cpu_to_le16(x) fio_swap16(x)
158#define __cpu_to_le32(x) fio_swap32(x)
159#define __cpu_to_le64(x) fio_swap64(x)
160#endif
161
162#define le16_to_cpu(val) ({ \
163 uint16_t *__val = &(val); \
164 __le16_to_cpu(*__val); \
165})
166#define le32_to_cpu(val) ({ \
167 uint32_t *__val = &(val); \
168 __le32_to_cpu(*__val); \
169})
170#define le64_to_cpu(val) ({ \
171 uint64_t *__val = &(val); \
172 __le64_to_cpu(*__val); \
173})
174#define cpu_to_le16(val) ({ \
175 uint16_t *__val = &(val); \
176 __cpu_to_le16(*__val); \
177})
178#define cpu_to_le32(val) ({ \
179 uint32_t *__val = &(val); \
180 __cpu_to_le32(*__val); \
181})
182#define cpu_to_le64(val) ({ \
183 uint64_t *__val = &(val); \
184 __cpu_to_le64(*__val); \
185})
186
Jens Axboe5e62c222007-05-22 13:27:30 +0200187#ifndef FIO_HAVE_BLKTRACE
188static inline int is_blktrace(const char *fname)
189{
190 return 0;
191}
Jens Axboe5921e802008-05-30 15:02:38 +0200192struct thread_data;
Jens Axboe5e62c222007-05-22 13:27:30 +0200193static inline int load_blktrace(struct thread_data *td, const char *fname)
194{
195 return 1;
196}
197#endif
198
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200199#define FIO_DEF_CL_SIZE 128
200
201static inline int os_cache_line_size(void)
202{
203#ifdef FIO_HAVE_CL_SIZE
204 int ret = arch_cache_line_size();
205
206 if (ret <= 0)
207 return FIO_DEF_CL_SIZE;
208
209 return ret;
210#else
211 return FIO_DEF_CL_SIZE;
212#endif
213}
214
Jens Axboe792d5512009-09-22 17:12:29 +0200215#ifdef FIO_USE_GENERIC_BDEV_SIZE
Bruce Cranecc314b2011-01-04 10:59:30 +0100216static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboe792d5512009-09-22 17:12:29 +0200217{
Jens Axboe3b2e1462009-12-15 08:58:10 +0100218 off_t end;
Jens Axboe792d5512009-09-22 17:12:29 +0200219
Jens Axboe3b2e1462009-12-15 08:58:10 +0100220 *bytes = 0;
221
Bruce Cranecc314b2011-01-04 10:59:30 +0100222 end = lseek(f->fd, 0, SEEK_END);
Jens Axboe792d5512009-09-22 17:12:29 +0200223 if (end < 0)
224 return errno;
225
226 *bytes = end;
227 return 0;
228}
229#endif
230
Jens Axboe53531372009-12-15 10:28:37 +0100231#ifdef FIO_USE_GENERIC_RAND
232typedef unsigned int os_random_state_t;
233
234static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
235{
236 srand(seed);
237}
238
239static inline long os_random_long(os_random_state_t *rs)
240{
241 long val;
242
243 val = rand_r(rs);
244 return val;
245}
246#endif
247
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200248#ifndef FIO_HAVE_FS_STAT
249static inline unsigned long long get_fs_size(const char *path)
250{
251 return 0;
252}
253#endif
254
Jens Axboec00a2282011-07-08 20:56:06 +0200255#ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
256static inline unsigned int cpus_online(void)
257{
258 return sysconf(_SC_NPROCESSORS_ONLN);
259}
260#endif
261
Jens Axboe47f767c2011-07-12 21:17:49 +0200262#ifndef FIO_HAVE_GETTID
263static inline int gettid(void)
264{
265 return getpid();
266}
267#endif
268
Jens Axboeebac4652005-12-08 15:25:21 +0100269#endif