5 #include <sys/socket.h>
12 #include "../arch/arch.h" /* IWYU pragma: export */
13 #include "../lib/types.h"
35 /* IWYU pragma: begin_exports */
36 #if defined(__linux__)
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__)
53 #include "os-windows.h"
54 #elif defined (__DragonFly__)
55 #include "os-dragonfly.h"
57 #error "unsupported os"
64 #ifdef CONFIG_POSIXAIO
66 #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
67 typedef struct aiocb os_aiocb_t;
72 #include "../oslib/strsep.h"
75 #ifndef CONFIG_STRLCAT
76 #include "../oslib/strlcat.h"
78 /* IWYU pragma: end_exports */
81 #define OS_MSG_DONTWAIT MSG_DONTWAIT
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)
91 #ifndef FIO_HAVE_CPU_AFFINITY
92 #define fio_cpu_clear(mask, cpu) do { } while (0)
93 typedef unsigned long os_cpu_mask_t;
95 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
100 static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
105 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
110 static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
115 extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
118 #ifndef FIO_HAVE_IOPRIO_CLASS
119 #define ioprio_value_is_class_rt(prio) (false)
120 #define IOPRIO_MIN_PRIO_CLASS 0
121 #define IOPRIO_MAX_PRIO_CLASS 0
123 #ifndef FIO_HAVE_IOPRIO
124 #define ioprio_value(prioclass, prio) (0)
125 #define ioprio_set(which, who, prioclass, prio) (0)
126 #define IOPRIO_MIN_PRIO 0
127 #define IOPRIO_MAX_PRIO 0
130 #ifndef FIO_HAVE_ODIRECT
131 #define OS_O_DIRECT 0
133 #define OS_O_DIRECT O_DIRECT
136 #ifndef FIO_HAVE_HUGETLB
137 #define SHM_HUGETLB 0
138 #define MAP_HUGETLB 0
139 #ifndef FIO_HUGE_PAGE
140 #define FIO_HUGE_PAGE 0
143 #ifndef FIO_HUGE_PAGE
144 #define FIO_HUGE_PAGE 4194304
148 #ifndef FIO_HAVE_MMAP_HUGE
149 #define MAP_HUGETLB 0
152 #ifndef FIO_O_NOATIME
153 #define FIO_O_NOATIME 0
157 #define OS_RAND_MAX RAND_MAX
160 #ifndef FIO_PREFERRED_ENGINE
161 #define FIO_PREFERRED_ENGINE "psync"
164 #ifndef FIO_OS_PATH_SEPARATOR
165 #define FIO_OS_PATH_SEPARATOR '/'
168 #ifndef FIO_PREFERRED_CLOCK_SOURCE
169 #ifdef CONFIG_CLOCK_GETTIME
170 #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
172 #define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD
176 #ifndef CONFIG_SOCKLEN_T
177 typedef unsigned int socklen_t;
180 #ifndef FIO_OS_HAS_CTIME_R
181 #define os_ctime_r(x, y, z) (void) ctime_r((x), (y))
184 #ifdef FIO_USE_GENERIC_SWAP
185 static inline uint16_t fio_swap16(uint16_t val)
187 return (val << 8) | (val >> 8);
190 static inline uint32_t fio_swap32(uint32_t val)
192 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
194 return (val >> 16) | (val << 16);
197 static inline uint64_t fio_swap64(uint64_t val)
199 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
200 ((val & 0x00ff00ff00ff00ffULL) << 8);
201 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
202 ((val & 0x0000ffff0000ffffULL) << 16);
204 return (val >> 32) | (val << 32);
208 #ifndef FIO_HAVE_BYTEORDER_FUNCS
209 #ifdef CONFIG_LITTLE_ENDIAN
210 #define __be16_to_cpu(x) fio_swap16(x)
211 #define __be32_to_cpu(x) fio_swap32(x)
212 #define __be64_to_cpu(x) fio_swap64(x)
213 #define __le16_to_cpu(x) (x)
214 #define __le32_to_cpu(x) (x)
215 #define __le64_to_cpu(x) (x)
216 #define __cpu_to_be16(x) fio_swap16(x)
217 #define __cpu_to_be32(x) fio_swap32(x)
218 #define __cpu_to_be64(x) fio_swap64(x)
219 #define __cpu_to_le16(x) (x)
220 #define __cpu_to_le32(x) (x)
221 #define __cpu_to_le64(x) (x)
223 #define __be16_to_cpu(x) (x)
224 #define __be32_to_cpu(x) (x)
225 #define __be64_to_cpu(x) (x)
226 #define __le16_to_cpu(x) fio_swap16(x)
227 #define __le32_to_cpu(x) fio_swap32(x)
228 #define __le64_to_cpu(x) fio_swap64(x)
229 #define __cpu_to_be16(x) (x)
230 #define __cpu_to_be32(x) (x)
231 #define __cpu_to_be64(x) (x)
232 #define __cpu_to_le16(x) fio_swap16(x)
233 #define __cpu_to_le32(x) fio_swap32(x)
234 #define __cpu_to_le64(x) fio_swap64(x)
236 #endif /* FIO_HAVE_BYTEORDER_FUNCS */
239 #define be16_to_cpu(val) ({ \
240 typecheck(uint16_t, val); \
241 __be16_to_cpu(val); \
243 #define be32_to_cpu(val) ({ \
244 typecheck(uint32_t, val); \
245 __be32_to_cpu(val); \
247 #define be64_to_cpu(val) ({ \
248 typecheck(uint64_t, val); \
249 __be64_to_cpu(val); \
251 #define le16_to_cpu(val) ({ \
252 typecheck(uint16_t, val); \
253 __le16_to_cpu(val); \
255 #define le32_to_cpu(val) ({ \
256 typecheck(uint32_t, val); \
257 __le32_to_cpu(val); \
259 #define le64_to_cpu(val) ({ \
260 typecheck(uint64_t, val); \
261 __le64_to_cpu(val); \
265 #define cpu_to_be16(val) ({ \
266 typecheck(uint16_t, val); \
267 __cpu_to_be16(val); \
269 #define cpu_to_be32(val) ({ \
270 typecheck(uint32_t, val); \
271 __cpu_to_be32(val); \
273 #define cpu_to_be64(val) ({ \
274 typecheck(uint64_t, val); \
275 __cpu_to_be64(val); \
277 #define cpu_to_le16(val) ({ \
278 typecheck(uint16_t, val); \
279 __cpu_to_le16(val); \
281 #define cpu_to_le32(val) ({ \
282 typecheck(uint32_t, val); \
283 __cpu_to_le32(val); \
285 #define cpu_to_le64(val) ({ \
286 typecheck(uint64_t, val); \
287 __cpu_to_le64(val); \
290 #define FIO_DEF_CL_SIZE 128
292 static inline int os_cache_line_size(void)
294 #ifdef FIO_HAVE_CL_SIZE
295 int ret = arch_cache_line_size();
298 return FIO_DEF_CL_SIZE;
302 return FIO_DEF_CL_SIZE;
306 #ifdef FIO_USE_GENERIC_BDEV_SIZE
307 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
313 end = lseek(f->fd, 0, SEEK_END);
322 #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
323 static inline int init_random_seeds(uint64_t *rand_seeds, int size)
327 fd = open("/dev/urandom", O_RDONLY);
332 if (read(fd, rand_seeds, size) < size) {
342 #ifndef FIO_HAVE_FS_STAT
343 static inline unsigned long long get_fs_free_size(const char *path)
349 #ifndef FIO_HAVE_CPU_CONF_SYSCONF
350 static inline unsigned int cpus_configured(void)
352 int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
354 return nr_cpus >= 1 ? nr_cpus : 1;
359 #ifdef FIO_HAVE_CPU_AFFINITY
360 static inline int CPU_COUNT(os_cpu_mask_t *mask)
362 int max_cpus = cpus_configured();
365 for (i = 0, nr_cpus = 0; i < max_cpus; i++)
366 if (fio_cpu_isset(mask, i))
374 #ifndef FIO_HAVE_GETTID
375 #ifndef CONFIG_HAVE_GETTID
376 static inline int gettid(void)
383 #ifndef FIO_HAVE_SHM_ATTACH_REMOVED
384 static inline int shm_attach_to_open_removed(void)
390 #ifndef FIO_HAVE_NATIVE_FALLOCATE
391 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len)
398 #if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE)
399 # define FIO_HAVE_DEFAULT_FALLOCATE
402 #ifndef FIO_HAVE_CPU_HAS
403 static inline bool os_cpu_has(cpu_features feature)
409 #ifndef FIO_EMULATED_MKDIR_TWO
410 # define fio_mkdir(path, mode) mkdir(path, mode)
414 static inline void os_clk_tck(long *clk_tck)
416 *clk_tck = sysconf(_SC_CLK_TCK);
419 extern void os_clk_tck(long *clk_tck);
422 #endif /* FIO_OS_H */