sg: Clean up handling of big endian data fields
[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" /* IWYU pragma: export */
12 #include "../lib/types.h"
13
14 enum {
15         os_linux = 1,
16         os_aix,
17         os_freebsd,
18         os_hpux,
19         os_mac,
20         os_netbsd,
21         os_openbsd,
22         os_solaris,
23         os_windows,
24         os_android,
25         os_dragonfly,
26
27         os_nr,
28 };
29
30 typedef enum {
31         CPU_ARM64_CRC32C,
32 } cpu_features;
33
34 /* IWYU pragma: begin_exports */
35 #if defined(__ANDROID__)
36 #include "os-android.h"
37 #elif defined(__linux__)
38 #include "os-linux.h"
39 #elif defined(__FreeBSD__)
40 #include "os-freebsd.h"
41 #elif defined(__OpenBSD__)
42 #include "os-openbsd.h"
43 #elif defined(__NetBSD__)
44 #include "os-netbsd.h"
45 #elif defined(__sun__)
46 #include "os-solaris.h"
47 #elif defined(__APPLE__)
48 #include "os-mac.h"
49 #elif defined(_AIX)
50 #include "os-aix.h"
51 #elif defined(__hpux)
52 #include "os-hpux.h"
53 #elif defined(WIN32)
54 #include "os-windows.h"
55 #elif defined (__DragonFly__)
56 #include "os-dragonfly.h"
57 #else
58 #error "unsupported os"
59 #endif
60
61 #ifdef CONFIG_POSIXAIO
62 #include <aio.h>
63 #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
64 typedef struct aiocb os_aiocb_t;
65 #endif
66 #endif
67
68 #ifndef CONFIG_STRSEP
69 #include "../oslib/strsep.h"
70 #endif
71
72 #ifndef CONFIG_STRLCAT
73 #include "../oslib/strlcat.h"
74 #endif
75 /* IWYU pragma: end_exports */
76
77 #ifdef MSG_DONTWAIT
78 #define OS_MSG_DONTWAIT MSG_DONTWAIT
79 #endif
80
81 #ifndef POSIX_FADV_DONTNEED
82 #define POSIX_FADV_DONTNEED     (0)
83 #define POSIX_FADV_SEQUENTIAL   (0)
84 #define POSIX_FADV_RANDOM       (0)
85 #define POSIX_FADV_NORMAL       (0)
86 #endif
87
88 #ifndef FIO_HAVE_CPU_AFFINITY
89 #define fio_cpu_clear(mask, cpu)        do { } while (0)
90 typedef unsigned long os_cpu_mask_t;
91
92 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
93 {
94         return 0;
95 }
96
97 static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
98 {
99         return -1;
100 }
101
102 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
103 {
104         return -1;
105 }
106
107 static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
108 {
109         return 0;
110 }
111 #else
112 extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
113 #endif
114
115 #ifndef FIO_HAVE_IOPRIO
116 #define ioprio_set(which, who, prioclass, prio) (0)
117 #endif
118
119 #ifndef FIO_HAVE_ODIRECT
120 #define OS_O_DIRECT                     0
121 #else
122 #define OS_O_DIRECT                     O_DIRECT
123 #endif
124
125 #ifdef OS_O_ATOMIC
126 #define FIO_O_ATOMIC                    OS_O_ATOMIC
127 #else
128 #define FIO_O_ATOMIC                    0
129 #endif
130
131 #ifndef FIO_HAVE_HUGETLB
132 #define SHM_HUGETLB                     0
133 #define MAP_HUGETLB                     0
134 #ifndef FIO_HUGE_PAGE
135 #define FIO_HUGE_PAGE                   0
136 #endif
137 #else
138 #ifndef FIO_HUGE_PAGE
139 #define FIO_HUGE_PAGE                   4194304
140 #endif
141 #endif
142
143 #ifndef FIO_HAVE_MMAP_HUGE
144 #define MAP_HUGETLB                     0
145 #endif
146
147 #ifndef FIO_O_NOATIME
148 #define FIO_O_NOATIME                   0
149 #endif
150
151 #ifndef OS_RAND_MAX
152 #define OS_RAND_MAX                     RAND_MAX
153 #endif
154
155 #ifndef FIO_HAVE_RAWBIND
156 #define fio_lookup_raw(dev, majdev, mindev)     1
157 #endif
158
159 #ifndef FIO_PREFERRED_ENGINE
160 #define FIO_PREFERRED_ENGINE    "psync"
161 #endif
162
163 #ifndef FIO_OS_PATH_SEPARATOR
164 #define FIO_OS_PATH_SEPARATOR   '/'
165 #endif
166
167 #ifndef FIO_PREFERRED_CLOCK_SOURCE
168 #ifdef CONFIG_CLOCK_GETTIME
169 #define FIO_PREFERRED_CLOCK_SOURCE      CS_CGETTIME
170 #else
171 #define FIO_PREFERRED_CLOCK_SOURCE      CS_GTOD
172 #endif
173 #endif
174
175 #ifndef FIO_MAX_JOBS
176 #define FIO_MAX_JOBS            4096
177 #endif
178
179 #ifndef CONFIG_SOCKLEN_T
180 typedef unsigned int socklen_t;
181 #endif
182
183 #ifndef FIO_OS_HAS_CTIME_R
184 #define os_ctime_r(x, y, z)     (void) ctime_r((x), (y))
185 #endif
186
187 #ifdef FIO_USE_GENERIC_SWAP
188 static inline uint16_t fio_swap16(uint16_t val)
189 {
190         return (val << 8) | (val >> 8);
191 }
192
193 static inline uint32_t fio_swap32(uint32_t val)
194 {
195         val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
196
197         return (val >> 16) | (val << 16);
198 }
199
200 static inline uint64_t fio_swap64(uint64_t val)
201 {
202         val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
203               ((val & 0x00ff00ff00ff00ffULL) << 8);
204         val = ((val & 0xffff0000ffff0000ULL) >> 16) |
205               ((val & 0x0000ffff0000ffffULL) << 16);
206
207         return (val >> 32) | (val << 32);
208 }
209 #endif
210
211 #ifndef FIO_HAVE_BYTEORDER_FUNCS
212 #ifdef CONFIG_LITTLE_ENDIAN
213 #define __be16_to_cpu(x)                fio_swap16(x)
214 #define __be32_to_cpu(x)                fio_swap32(x)
215 #define __be64_to_cpu(x)                fio_swap64(x)
216 #define __le16_to_cpu(x)                (x)
217 #define __le32_to_cpu(x)                (x)
218 #define __le64_to_cpu(x)                (x)
219 #define __cpu_to_be16(x)                fio_swap16(x)
220 #define __cpu_to_be32(x)                fio_swap32(x)
221 #define __cpu_to_be64(x)                fio_swap64(x)
222 #define __cpu_to_le16(x)                (x)
223 #define __cpu_to_le32(x)                (x)
224 #define __cpu_to_le64(x)                (x)
225 #else
226 #define __be16_to_cpu(x)                (x)
227 #define __be32_to_cpu(x)                (x)
228 #define __be64_to_cpu(x)                (x)
229 #define __le16_to_cpu(x)                fio_swap16(x)
230 #define __le32_to_cpu(x)                fio_swap32(x)
231 #define __le64_to_cpu(x)                fio_swap64(x)
232 #define __cpu_to_be16(x)                (x)
233 #define __cpu_to_be32(x)                (x)
234 #define __cpu_to_be64(x)                (x)
235 #define __cpu_to_le16(x)                fio_swap16(x)
236 #define __cpu_to_le32(x)                fio_swap32(x)
237 #define __cpu_to_le64(x)                fio_swap64(x)
238 #endif
239 #endif /* FIO_HAVE_BYTEORDER_FUNCS */
240
241 #ifdef FIO_INTERNAL
242 #define be16_to_cpu(val) ({                     \
243         typecheck(uint16_t, val);               \
244         __be16_to_cpu(val);                     \
245 })
246 #define be32_to_cpu(val) ({                     \
247         typecheck(uint32_t, val);               \
248         __be32_to_cpu(val);                     \
249 })
250 #define be64_to_cpu(val) ({                     \
251         typecheck(uint64_t, val);               \
252         __be64_to_cpu(val);                     \
253 })
254 #define le16_to_cpu(val) ({                     \
255         typecheck(uint16_t, val);               \
256         __le16_to_cpu(val);                     \
257 })
258 #define le32_to_cpu(val) ({                     \
259         typecheck(uint32_t, val);               \
260         __le32_to_cpu(val);                     \
261 })
262 #define le64_to_cpu(val) ({                     \
263         typecheck(uint64_t, val);               \
264         __le64_to_cpu(val);                     \
265 })
266 #endif
267
268 #define cpu_to_be16(val) ({                     \
269         typecheck(uint16_t, val);               \
270         __cpu_to_be16(val);                     \
271 })
272 #define cpu_to_be32(val) ({                     \
273         typecheck(uint32_t, val);               \
274         __cpu_to_be32(val);                     \
275 })
276 #define cpu_to_be64(val) ({                     \
277         typecheck(uint64_t, val);               \
278         __cpu_to_be64(val);                     \
279 })
280 #define cpu_to_le16(val) ({                     \
281         typecheck(uint16_t, val);               \
282         __cpu_to_le16(val);                     \
283 })
284 #define cpu_to_le32(val) ({                     \
285         typecheck(uint32_t, val);               \
286         __cpu_to_le32(val);                     \
287 })
288 #define cpu_to_le64(val) ({                     \
289         typecheck(uint64_t, val);               \
290         __cpu_to_le64(val);                     \
291 })
292
293 #define FIO_DEF_CL_SIZE         128
294
295 static inline int os_cache_line_size(void)
296 {
297 #ifdef FIO_HAVE_CL_SIZE
298         int ret = arch_cache_line_size();
299
300         if (ret <= 0)
301                 return FIO_DEF_CL_SIZE;
302
303         return ret;
304 #else
305         return FIO_DEF_CL_SIZE;
306 #endif
307 }
308
309 #ifdef FIO_USE_GENERIC_BDEV_SIZE
310 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
311 {
312         off_t end;
313
314         *bytes = 0;
315
316         end = lseek(f->fd, 0, SEEK_END);
317         if (end < 0)
318                 return errno;
319
320         *bytes = end;
321         return 0;
322 }
323 #endif
324
325 #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
326 static inline int init_random_seeds(unsigned long *rand_seeds, int size)
327 {
328         int fd;
329
330         fd = open("/dev/urandom", O_RDONLY);
331         if (fd == -1) {
332                 return 1;
333         }
334
335         if (read(fd, rand_seeds, size) < size) {
336                 close(fd);
337                 return 1;
338         }
339
340         close(fd);
341         return 0;
342 }
343 #endif
344
345 #ifndef FIO_HAVE_FS_STAT
346 static inline unsigned long long get_fs_free_size(const char *path)
347 {
348         return 0;
349 }
350 #endif
351
352 #ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
353 static inline unsigned int cpus_online(void)
354 {
355         return sysconf(_SC_NPROCESSORS_ONLN);
356 }
357 #endif
358
359 #ifndef CPU_COUNT
360 #ifdef FIO_HAVE_CPU_AFFINITY
361 static inline int CPU_COUNT(os_cpu_mask_t *mask)
362 {
363         int max_cpus = cpus_online();
364         int nr_cpus, i;
365
366         for (i = 0, nr_cpus = 0; i < max_cpus; i++)
367                 if (fio_cpu_isset(mask, i))
368                         nr_cpus++;
369
370         return nr_cpus;
371 }
372 #endif
373 #endif
374
375 #ifndef FIO_HAVE_GETTID
376 static inline int gettid(void)
377 {
378         return getpid();
379 }
380 #endif
381
382 #ifndef FIO_HAVE_SHM_ATTACH_REMOVED
383 static inline int shm_attach_to_open_removed(void)
384 {
385         return 0;
386 }
387 #endif
388
389 #ifndef FIO_HAVE_NATIVE_FALLOCATE
390 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len)
391 {
392         errno = ENOSYS;
393         return false;
394 }
395 #endif
396
397 #if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE)
398 # define FIO_HAVE_ANY_FALLOCATE
399 #endif
400
401 #ifndef FIO_HAVE_CPU_HAS
402 static inline bool os_cpu_has(cpu_features feature)
403 {
404         return false;
405 }
406 #endif
407
408 #endif