12188156cf280afea5ca29935d74ee794058228b
[fio.git] / os / os.h
1 #ifndef FIO_OS_H
2 #define FIO_OS_H
3
4 #include <sys/types.h>
5 #include <pthread.h>
6 #include <unistd.h>
7 #include <stdlib.h>
8
9 enum {
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
22 #if defined(__linux__)
23 #include "os-linux.h"
24 #elif defined(__FreeBSD__)
25 #include "os-freebsd.h"
26 #elif defined(__NetBSD__)
27 #include "os-netbsd.h"
28 #elif defined(__sun__)
29 #include "os-solaris.h"
30 #elif defined(__APPLE__)
31 #include "os-mac.h"
32 #elif defined(_AIX)
33 #include "os-aix.h"
34 #elif defined(__hpux)
35 #include "os-hpux.h"
36 #elif defined(__CYGWIN__)
37 #include "os-windows.h"
38 #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>
48 #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
49 typedef struct aiocb os_aiocb_t;
50 #endif
51 #endif
52
53 #ifdef FIO_HAVE_SGIO
54 #include <linux/fs.h>
55 #include <scsi/sg.h>
56 #endif
57
58 #ifndef FIO_HAVE_STRSEP
59 #include "../lib/strsep.h"
60 #endif
61
62 #ifdef MSG_DONTWAIT
63 #define OS_MSG_DONTWAIT MSG_DONTWAIT
64 #endif
65
66 #ifndef FIO_HAVE_FADVISE
67 #define posix_fadvise(fd, off, len, advice)     (0)
68
69 #ifndef POSIX_FADV_DONTNEED
70 #define POSIX_FADV_DONTNEED     (0)
71 #define POSIX_FADV_SEQUENTIAL   (0)
72 #define POSIX_FADV_RANDOM       (0)
73 #endif
74 #endif /* FIO_HAVE_FADVISE */
75
76 #ifndef FIO_HAVE_CPU_AFFINITY
77 #define fio_setaffinity(pid, mask)      (0)
78 #define fio_getaffinity(pid, mask)      do { } while (0)
79 #define fio_cpu_clear(mask, cpu)        do { } while (0)
80 #define fio_cpuset_exit(mask)           (-1)
81 typedef unsigned long os_cpu_mask_t;
82 #endif
83
84 #ifndef FIO_HAVE_IOPRIO
85 #define ioprio_set(which, who, prio)    (0)
86 #endif
87
88 #ifndef FIO_HAVE_ODIRECT
89 #define OS_O_DIRECT                     0
90 #else
91 #define OS_O_DIRECT                     O_DIRECT
92 #endif
93
94 #ifndef FIO_HAVE_HUGETLB
95 #define SHM_HUGETLB                     0
96 #ifndef FIO_HUGE_PAGE
97 #define FIO_HUGE_PAGE                   0
98 #endif
99 #else
100 #ifndef FIO_HUGE_PAGE
101 #define FIO_HUGE_PAGE                   4194304
102 #endif
103 #endif
104
105 #ifndef FIO_O_NOATIME
106 #define FIO_O_NOATIME                   0
107 #endif
108
109 #ifndef OS_RAND_MAX
110 #define OS_RAND_MAX                     RAND_MAX
111 #endif
112
113 #ifdef FIO_HAVE_CLOCK_MONOTONIC
114 #define FIO_TIMER_CLOCK CLOCK_MONOTONIC
115 #else
116 #define FIO_TIMER_CLOCK CLOCK_REALTIME
117 #endif
118
119 #ifndef FIO_HAVE_RAWBIND
120 #define fio_lookup_raw(dev, majdev, mindev)     1
121 #endif
122
123 #ifndef FIO_PREFERRED_ENGINE
124 #define FIO_PREFERRED_ENGINE    "sync"
125 #endif
126
127 #ifndef FIO_MAX_JOBS
128 #define FIO_MAX_JOBS            2048
129 #endif
130
131 #ifndef FIO_OS_HAVE_SOCKLEN_T
132 typedef socklen_t fio_socklen_t;
133 #endif
134
135 #ifdef FIO_USE_GENERIC_SWAP
136 static inline uint16_t fio_swap16(uint16_t val)
137 {
138         return (val << 8) | (val >> 8);
139 }
140
141 static inline uint32_t fio_swap32(uint32_t val)
142 {
143         val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
144
145         return (val >> 16) | (val << 16);
146 }
147
148 static inline uint64_t fio_swap64(uint64_t val)
149 {
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
159 #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
200 #ifndef FIO_HAVE_BLKTRACE
201 static inline int is_blktrace(const char *fname)
202 {
203         return 0;
204 }
205 struct thread_data;
206 static inline int load_blktrace(struct thread_data *td, const char *fname)
207 {
208         return 1;
209 }
210 #endif
211
212 #define FIO_DEF_CL_SIZE         128
213
214 static 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
228 #ifdef FIO_USE_GENERIC_BDEV_SIZE
229 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
230 {
231         off_t end;
232
233         *bytes = 0;
234
235         end = lseek(f->fd, 0, SEEK_END);
236         if (end < 0)
237                 return errno;
238
239         *bytes = end;
240         return 0;
241 }
242 #endif
243
244 #ifdef FIO_USE_GENERIC_RAND
245 typedef unsigned int os_random_state_t;
246
247 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
248 {
249         srand(seed);
250 }
251
252 static 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
261 #ifndef FIO_HAVE_FS_STAT
262 static inline unsigned long long get_fs_size(const char *path)
263 {
264         return 0;
265 }
266 #endif
267
268 #ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
269 static inline unsigned int cpus_online(void)
270 {
271         return sysconf(_SC_NPROCESSORS_ONLN);
272 }
273 #endif
274
275 #ifndef FIO_HAVE_GETTID
276 static inline int gettid(void)
277 {
278         return getpid();
279 }
280 #endif
281
282 #endif