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