5 #include <sys/socket.h>
11 #include "../arch/arch.h" /* IWYU pragma: export */
12 #include "../lib/types.h"
30 /* IWYU pragma: begin_exports */
31 #if defined(__ANDROID__)
32 #include "os-android.h"
33 #elif defined(__linux__)
35 #elif defined(__FreeBSD__)
36 #include "os-freebsd.h"
37 #elif defined(__OpenBSD__)
38 #include "os-openbsd.h"
39 #elif defined(__NetBSD__)
40 #include "os-netbsd.h"
41 #elif defined(__sun__)
42 #include "os-solaris.h"
43 #elif defined(__APPLE__)
50 #include "os-windows.h"
51 #elif defined (__DragonFly__)
52 #include "os-dragonfly.h"
54 #error "unsupported os"
57 #ifdef CONFIG_POSIXAIO
59 #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
60 typedef struct aiocb os_aiocb_t;
65 #include "../oslib/strsep.h"
68 #ifndef CONFIG_STRLCAT
69 #include "../oslib/strlcat.h"
71 /* IWYU pragma: end_exports */
74 #define OS_MSG_DONTWAIT MSG_DONTWAIT
77 #ifndef POSIX_FADV_DONTNEED
78 #define POSIX_FADV_DONTNEED (0)
79 #define POSIX_FADV_SEQUENTIAL (0)
80 #define POSIX_FADV_RANDOM (0)
81 #define POSIX_FADV_NORMAL (0)
84 #ifndef FIO_HAVE_CPU_AFFINITY
85 #define fio_cpu_clear(mask, cpu) do { } while (0)
86 typedef unsigned long os_cpu_mask_t;
88 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
93 static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
98 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
103 static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
108 extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
111 #ifndef FIO_HAVE_IOPRIO
112 #define ioprio_set(which, who, prioclass, prio) (0)
115 #ifndef FIO_HAVE_ODIRECT
116 #define OS_O_DIRECT 0
118 #define OS_O_DIRECT O_DIRECT
122 #define FIO_O_ATOMIC OS_O_ATOMIC
124 #define FIO_O_ATOMIC 0
127 #ifndef FIO_HAVE_HUGETLB
128 #define SHM_HUGETLB 0
129 #define MAP_HUGETLB 0
130 #ifndef FIO_HUGE_PAGE
131 #define FIO_HUGE_PAGE 0
134 #ifndef FIO_HUGE_PAGE
135 #define FIO_HUGE_PAGE 4194304
139 #ifndef FIO_HAVE_MMAP_HUGE
140 #define MAP_HUGETLB 0
143 #ifndef FIO_O_NOATIME
144 #define FIO_O_NOATIME 0
148 #define OS_RAND_MAX RAND_MAX
151 #ifndef FIO_HAVE_RAWBIND
152 #define fio_lookup_raw(dev, majdev, mindev) 1
155 #ifndef FIO_PREFERRED_ENGINE
156 #define FIO_PREFERRED_ENGINE "psync"
159 #ifndef FIO_OS_PATH_SEPARATOR
160 #define FIO_OS_PATH_SEPARATOR '/'
163 #ifndef FIO_PREFERRED_CLOCK_SOURCE
164 #ifdef CONFIG_CLOCK_GETTIME
165 #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
167 #define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD
172 #define FIO_MAX_JOBS 4096
175 #ifndef CONFIG_SOCKLEN_T
176 typedef unsigned int socklen_t;
179 #ifndef FIO_OS_HAS_CTIME_R
180 #define os_ctime_r(x, y, z) (void) ctime_r((x), (y))
183 #ifdef FIO_USE_GENERIC_SWAP
184 static inline uint16_t fio_swap16(uint16_t val)
186 return (val << 8) | (val >> 8);
189 static inline uint32_t fio_swap32(uint32_t val)
191 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
193 return (val >> 16) | (val << 16);
196 static inline uint64_t fio_swap64(uint64_t val)
198 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
199 ((val & 0x00ff00ff00ff00ffULL) << 8);
200 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
201 ((val & 0x0000ffff0000ffffULL) << 16);
203 return (val >> 32) | (val << 32);
207 #ifndef FIO_HAVE_BYTEORDER_FUNCS
208 #ifdef CONFIG_LITTLE_ENDIAN
209 #define __be64_to_cpu(x) fio_swap64(x)
210 #define __le16_to_cpu(x) (x)
211 #define __le32_to_cpu(x) (x)
212 #define __le64_to_cpu(x) (x)
213 #define __cpu_to_be64(x) fio_swap64(x)
214 #define __cpu_to_le16(x) (x)
215 #define __cpu_to_le32(x) (x)
216 #define __cpu_to_le64(x) (x)
218 #define __be64_to_cpu(x) (x)
219 #define __le16_to_cpu(x) fio_swap16(x)
220 #define __le32_to_cpu(x) fio_swap32(x)
221 #define __le64_to_cpu(x) fio_swap64(x)
222 #define __cpu_to_be64(x) (x)
223 #define __cpu_to_le16(x) fio_swap16(x)
224 #define __cpu_to_le32(x) fio_swap32(x)
225 #define __cpu_to_le64(x) fio_swap64(x)
227 #endif /* FIO_HAVE_BYTEORDER_FUNCS */
230 #define be64_to_cpu(val) ({ \
231 typecheck(uint64_t, val); \
232 __be64_to_cpu(val); \
234 #define le16_to_cpu(val) ({ \
235 typecheck(uint16_t, val); \
236 __le16_to_cpu(val); \
238 #define le32_to_cpu(val) ({ \
239 typecheck(uint32_t, val); \
240 __le32_to_cpu(val); \
242 #define le64_to_cpu(val) ({ \
243 typecheck(uint64_t, val); \
244 __le64_to_cpu(val); \
248 #define cpu_to_be64(val) ({ \
249 typecheck(uint64_t, val); \
250 __cpu_to_be64(val); \
252 #define cpu_to_le16(val) ({ \
253 typecheck(uint16_t, val); \
254 __cpu_to_le16(val); \
256 #define cpu_to_le32(val) ({ \
257 typecheck(uint32_t, val); \
258 __cpu_to_le32(val); \
260 #define cpu_to_le64(val) ({ \
261 typecheck(uint64_t, val); \
262 __cpu_to_le64(val); \
265 #define FIO_DEF_CL_SIZE 128
267 static inline int os_cache_line_size(void)
269 #ifdef FIO_HAVE_CL_SIZE
270 int ret = arch_cache_line_size();
273 return FIO_DEF_CL_SIZE;
277 return FIO_DEF_CL_SIZE;
281 #ifdef FIO_USE_GENERIC_BDEV_SIZE
282 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
288 end = lseek(f->fd, 0, SEEK_END);
297 #ifdef FIO_USE_GENERIC_RAND
298 typedef unsigned int os_random_state_t;
300 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
305 static inline long os_random_long(os_random_state_t *rs)
314 #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
315 static inline int init_random_seeds(unsigned long *rand_seeds, int size)
319 fd = open("/dev/urandom", O_RDONLY);
324 if (read(fd, rand_seeds, size) < size) {
334 #ifndef FIO_HAVE_FS_STAT
335 static inline unsigned long long get_fs_free_size(const char *path)
341 #ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
342 static inline unsigned int cpus_online(void)
344 return sysconf(_SC_NPROCESSORS_ONLN);
349 #ifdef FIO_HAVE_CPU_AFFINITY
350 static inline int CPU_COUNT(os_cpu_mask_t *mask)
352 int max_cpus = cpus_online();
355 for (i = 0, nr_cpus = 0; i < max_cpus; i++)
356 if (fio_cpu_isset(mask, i))
364 #ifndef FIO_HAVE_GETTID
365 static inline int gettid(void)
371 #ifndef FIO_HAVE_SHM_ATTACH_REMOVED
372 static inline int shm_attach_to_open_removed(void)
378 #ifndef FIO_HAVE_NATIVE_FALLOCATE
379 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len)
386 #if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE)
387 # define FIO_HAVE_ANY_FALLOCATE