5 #include <sys/socket.h>
12 #include "../arch/arch.h" /* IWYU pragma: export */
13 #include "../lib/types.h"
36 /* IWYU pragma: begin_exports */
37 #if defined(__linux__)
39 #elif defined(__FreeBSD__)
40 #include "os-freebsd.h"
41 #elif defined(__OpenBSD__)
42 #include "os-openbsd.h"
43 #elif defined(__QNX__)
45 #elif defined(__NetBSD__)
46 #include "os-netbsd.h"
47 #elif defined(__sun__)
48 #include "os-solaris.h"
49 #elif defined(__APPLE__)
56 #include "os-windows.h"
57 #elif defined (__DragonFly__)
58 #include "os-dragonfly.h"
60 #error "unsupported os"
67 #ifdef CONFIG_POSIXAIO
69 #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
70 typedef struct aiocb os_aiocb_t;
75 #include "../oslib/strsep.h"
78 #ifndef CONFIG_STRLCAT
79 #include "../oslib/strlcat.h"
81 /* IWYU pragma: end_exports */
84 #define OS_MSG_DONTWAIT MSG_DONTWAIT
87 #ifndef POSIX_FADV_DONTNEED
88 #define POSIX_FADV_DONTNEED (0)
89 #define POSIX_FADV_SEQUENTIAL (0)
90 #define POSIX_FADV_RANDOM (0)
91 #define POSIX_FADV_NORMAL (0)
94 #ifndef FIO_HAVE_CPU_AFFINITY
95 #define fio_cpu_clear(mask, cpu) do { } while (0)
96 typedef unsigned long os_cpu_mask_t;
98 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
103 static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
108 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
113 static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
118 extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
121 #ifndef FIO_HAVE_IOPRIO_CLASS
122 #define ioprio_class(prio) 0
123 #define ioprio_value_is_class_rt(prio) (false)
124 #define IOPRIO_MIN_PRIO_CLASS 0
125 #define IOPRIO_MAX_PRIO_CLASS 0
126 #define ioprio_hint(prio) 0
127 #define IOPRIO_MIN_PRIO_HINT 0
128 #define IOPRIO_MAX_PRIO_HINT 0
130 #ifndef FIO_HAVE_IOPRIO
131 #define ioprio_value(prioclass, prio, priohint) (0)
132 #define ioprio(ioprio) 0
133 #define ioprio_set(which, who, prioclass, prio, priohint) (0)
134 #define IOPRIO_MIN_PRIO 0
135 #define IOPRIO_MAX_PRIO 0
138 #ifndef FIO_HAVE_ODIRECT
139 #define OS_O_DIRECT 0
141 #define OS_O_DIRECT O_DIRECT
144 #ifndef FIO_HAVE_HUGETLB
145 #define SHM_HUGETLB 0
146 #define MAP_HUGETLB 0
147 #ifndef FIO_HUGE_PAGE
148 #define FIO_HUGE_PAGE 0
151 #ifndef FIO_HUGE_PAGE
152 #define FIO_HUGE_PAGE 4194304
156 #ifndef FIO_HAVE_MMAP_HUGE
157 #define MAP_HUGETLB 0
160 #ifndef FIO_O_NOATIME
161 #define FIO_O_NOATIME 0
165 #define OS_RAND_MAX RAND_MAX
168 #ifndef FIO_PREFERRED_ENGINE
169 #define FIO_PREFERRED_ENGINE "psync"
172 #ifndef FIO_OS_PATH_SEPARATOR
173 #define FIO_OS_PATH_SEPARATOR '/'
176 #ifndef FIO_PREFERRED_CLOCK_SOURCE
177 #ifdef CONFIG_CLOCK_GETTIME
178 #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
180 #define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD
184 #ifndef CONFIG_SOCKLEN_T
185 typedef unsigned int socklen_t;
188 #ifndef FIO_OS_HAS_CTIME_R
189 #define os_ctime_r(x, y, z) (void) ctime_r((x), (y))
192 #ifdef FIO_USE_GENERIC_SWAP
193 static inline uint16_t fio_swap16(uint16_t val)
195 return (val << 8) | (val >> 8);
198 static inline uint32_t fio_swap32(uint32_t val)
200 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
202 return (val >> 16) | (val << 16);
205 static inline uint64_t fio_swap64(uint64_t val)
207 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
208 ((val & 0x00ff00ff00ff00ffULL) << 8);
209 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
210 ((val & 0x0000ffff0000ffffULL) << 16);
212 return (val >> 32) | (val << 32);
216 #ifndef FIO_HAVE_BYTEORDER_FUNCS
217 #ifdef CONFIG_LITTLE_ENDIAN
218 #define __be16_to_cpu(x) fio_swap16(x)
219 #define __be32_to_cpu(x) fio_swap32(x)
220 #define __be64_to_cpu(x) fio_swap64(x)
221 #define __le16_to_cpu(x) (x)
222 #define __le32_to_cpu(x) (x)
223 #define __le64_to_cpu(x) (x)
224 #define __cpu_to_be16(x) fio_swap16(x)
225 #define __cpu_to_be32(x) fio_swap32(x)
226 #define __cpu_to_be64(x) fio_swap64(x)
227 #define __cpu_to_le16(x) (x)
228 #define __cpu_to_le32(x) (x)
229 #define __cpu_to_le64(x) (x)
231 #define __be16_to_cpu(x) (x)
232 #define __be32_to_cpu(x) (x)
233 #define __be64_to_cpu(x) (x)
234 #define __le16_to_cpu(x) fio_swap16(x)
235 #define __le32_to_cpu(x) fio_swap32(x)
236 #define __le64_to_cpu(x) fio_swap64(x)
237 #define __cpu_to_be16(x) (x)
238 #define __cpu_to_be32(x) (x)
239 #define __cpu_to_be64(x) (x)
240 #define __cpu_to_le16(x) fio_swap16(x)
241 #define __cpu_to_le32(x) fio_swap32(x)
242 #define __cpu_to_le64(x) fio_swap64(x)
244 #endif /* FIO_HAVE_BYTEORDER_FUNCS */
247 #define be16_to_cpu(val) ({ \
248 typecheck(uint16_t, val); \
249 __be16_to_cpu(val); \
251 #define be32_to_cpu(val) ({ \
252 typecheck(uint32_t, val); \
253 __be32_to_cpu(val); \
255 #define be64_to_cpu(val) ({ \
256 typecheck(uint64_t, val); \
257 __be64_to_cpu(val); \
259 #define le16_to_cpu(val) ({ \
260 typecheck(uint16_t, val); \
261 __le16_to_cpu(val); \
263 #define le32_to_cpu(val) ({ \
264 typecheck(uint32_t, val); \
265 __le32_to_cpu(val); \
267 #define le64_to_cpu(val) ({ \
268 typecheck(uint64_t, val); \
269 __le64_to_cpu(val); \
273 #define cpu_to_be16(val) ({ \
274 typecheck(uint16_t, val); \
275 __cpu_to_be16(val); \
277 #define cpu_to_be32(val) ({ \
278 typecheck(uint32_t, val); \
279 __cpu_to_be32(val); \
281 #define cpu_to_be64(val) ({ \
282 typecheck(uint64_t, val); \
283 __cpu_to_be64(val); \
285 #define cpu_to_le16(val) ({ \
286 typecheck(uint16_t, val); \
287 __cpu_to_le16(val); \
289 #define cpu_to_le32(val) ({ \
290 typecheck(uint32_t, val); \
291 __cpu_to_le32(val); \
293 #define cpu_to_le64(val) ({ \
294 typecheck(uint64_t, val); \
295 __cpu_to_le64(val); \
298 #define FIO_DEF_CL_SIZE 128
300 static inline int os_cache_line_size(void)
302 #ifdef FIO_HAVE_CL_SIZE
303 int ret = arch_cache_line_size();
306 return FIO_DEF_CL_SIZE;
310 return FIO_DEF_CL_SIZE;
314 #ifdef FIO_USE_GENERIC_BDEV_SIZE
315 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
321 end = lseek(f->fd, 0, SEEK_END);
330 #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
331 static inline int init_random_seeds(uint64_t *rand_seeds, int size)
335 fd = open("/dev/urandom", O_RDONLY);
340 if (read(fd, rand_seeds, size) < size) {
350 #ifndef FIO_HAVE_FS_STAT
351 static inline unsigned long long get_fs_free_size(const char *path)
357 #ifndef FIO_HAVE_CPU_CONF_SYSCONF
358 static inline unsigned int cpus_configured(void)
360 int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
362 return nr_cpus >= 1 ? nr_cpus : 1;
367 #ifdef FIO_HAVE_CPU_AFFINITY
368 static inline int CPU_COUNT(os_cpu_mask_t *mask)
370 int max_cpus = cpus_configured();
373 for (i = 0, nr_cpus = 0; i < max_cpus; i++)
374 if (fio_cpu_isset(mask, i))
382 #ifndef FIO_HAVE_GETTID
383 #ifndef CONFIG_HAVE_GETTID
384 static inline int gettid(void)
391 #ifndef FIO_HAVE_SHM_ATTACH_REMOVED
392 static inline int shm_attach_to_open_removed(void)
398 #ifndef FIO_HAVE_NATIVE_FALLOCATE
399 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len)
406 #if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE)
407 # define FIO_HAVE_DEFAULT_FALLOCATE
410 #ifndef FIO_HAVE_CPU_HAS
411 static inline bool os_cpu_has(cpu_features feature)
417 #ifndef FIO_EMULATED_MKDIR_TWO
418 # define fio_mkdir(path, mode) mkdir(path, mode)
422 static inline void os_clk_tck(long *clk_tck)
424 *clk_tck = sysconf(_SC_CLK_TCK);
427 extern void os_clk_tck(long *clk_tck);
430 #endif /* FIO_OS_H */