5 #include <sys/socket.h>
11 #include "../arch/arch.h" /* IWYU pragma: export */
12 #include "../lib/types.h"
34 /* IWYU pragma: begin_exports */
35 #if defined(__ANDROID__)
36 #include "os-android.h"
37 #elif defined(__linux__)
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__)
54 #include "os-windows.h"
55 #elif defined (__DragonFly__)
56 #include "os-dragonfly.h"
58 #error "unsupported os"
61 #ifdef CONFIG_POSIXAIO
63 #ifndef FIO_OS_HAVE_AIOCB_TYPEDEF
64 typedef struct aiocb os_aiocb_t;
69 #include "../oslib/strsep.h"
72 #ifndef CONFIG_STRLCAT
73 #include "../oslib/strlcat.h"
75 /* IWYU pragma: end_exports */
78 #define OS_MSG_DONTWAIT MSG_DONTWAIT
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)
88 #ifndef FIO_HAVE_CPU_AFFINITY
89 #define fio_cpu_clear(mask, cpu) do { } while (0)
90 typedef unsigned long os_cpu_mask_t;
92 static inline int fio_setaffinity(int pid, os_cpu_mask_t cpumask)
97 static inline int fio_getaffinity(int pid, os_cpu_mask_t *cpumask)
102 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
107 static inline int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu_index)
112 extern int fio_cpus_split(os_cpu_mask_t *mask, unsigned int cpu);
115 #ifndef FIO_HAVE_IOPRIO
116 #define ioprio_set(which, who, prioclass, prio) (0)
119 #ifndef FIO_HAVE_ODIRECT
120 #define OS_O_DIRECT 0
122 #define OS_O_DIRECT O_DIRECT
126 #define FIO_O_ATOMIC OS_O_ATOMIC
128 #define FIO_O_ATOMIC 0
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
138 #ifndef FIO_HUGE_PAGE
139 #define FIO_HUGE_PAGE 4194304
143 #ifndef FIO_HAVE_MMAP_HUGE
144 #define MAP_HUGETLB 0
147 #ifndef FIO_O_NOATIME
148 #define FIO_O_NOATIME 0
152 #define OS_RAND_MAX RAND_MAX
155 #ifndef FIO_HAVE_RAWBIND
156 #define fio_lookup_raw(dev, majdev, mindev) 1
159 #ifndef FIO_PREFERRED_ENGINE
160 #define FIO_PREFERRED_ENGINE "psync"
163 #ifndef FIO_OS_PATH_SEPARATOR
164 #define FIO_OS_PATH_SEPARATOR '/'
167 #ifndef FIO_PREFERRED_CLOCK_SOURCE
168 #ifdef CONFIG_CLOCK_GETTIME
169 #define FIO_PREFERRED_CLOCK_SOURCE CS_CGETTIME
171 #define FIO_PREFERRED_CLOCK_SOURCE CS_GTOD
176 #define FIO_MAX_JOBS 4096
179 #ifndef CONFIG_SOCKLEN_T
180 typedef unsigned int socklen_t;
183 #ifndef FIO_OS_HAS_CTIME_R
184 #define os_ctime_r(x, y, z) (void) ctime_r((x), (y))
187 #ifdef FIO_USE_GENERIC_SWAP
188 static inline uint16_t fio_swap16(uint16_t val)
190 return (val << 8) | (val >> 8);
193 static inline uint32_t fio_swap32(uint32_t val)
195 val = ((val & 0xff00ff00UL) >> 8) | ((val & 0x00ff00ffUL) << 8);
197 return (val >> 16) | (val << 16);
200 static inline uint64_t fio_swap64(uint64_t val)
202 val = ((val & 0xff00ff00ff00ff00ULL) >> 8) |
203 ((val & 0x00ff00ff00ff00ffULL) << 8);
204 val = ((val & 0xffff0000ffff0000ULL) >> 16) |
205 ((val & 0x0000ffff0000ffffULL) << 16);
207 return (val >> 32) | (val << 32);
211 #ifndef FIO_HAVE_BYTEORDER_FUNCS
212 #ifdef CONFIG_LITTLE_ENDIAN
213 #define __be64_to_cpu(x) fio_swap64(x)
214 #define __le16_to_cpu(x) (x)
215 #define __le32_to_cpu(x) (x)
216 #define __le64_to_cpu(x) (x)
217 #define __cpu_to_be64(x) fio_swap64(x)
218 #define __cpu_to_le16(x) (x)
219 #define __cpu_to_le32(x) (x)
220 #define __cpu_to_le64(x) (x)
222 #define __be64_to_cpu(x) (x)
223 #define __le16_to_cpu(x) fio_swap16(x)
224 #define __le32_to_cpu(x) fio_swap32(x)
225 #define __le64_to_cpu(x) fio_swap64(x)
226 #define __cpu_to_be64(x) (x)
227 #define __cpu_to_le16(x) fio_swap16(x)
228 #define __cpu_to_le32(x) fio_swap32(x)
229 #define __cpu_to_le64(x) fio_swap64(x)
231 #endif /* FIO_HAVE_BYTEORDER_FUNCS */
234 #define be64_to_cpu(val) ({ \
235 typecheck(uint64_t, val); \
236 __be64_to_cpu(val); \
238 #define le16_to_cpu(val) ({ \
239 typecheck(uint16_t, val); \
240 __le16_to_cpu(val); \
242 #define le32_to_cpu(val) ({ \
243 typecheck(uint32_t, val); \
244 __le32_to_cpu(val); \
246 #define le64_to_cpu(val) ({ \
247 typecheck(uint64_t, val); \
248 __le64_to_cpu(val); \
252 #define cpu_to_be64(val) ({ \
253 typecheck(uint64_t, val); \
254 __cpu_to_be64(val); \
256 #define cpu_to_le16(val) ({ \
257 typecheck(uint16_t, val); \
258 __cpu_to_le16(val); \
260 #define cpu_to_le32(val) ({ \
261 typecheck(uint32_t, val); \
262 __cpu_to_le32(val); \
264 #define cpu_to_le64(val) ({ \
265 typecheck(uint64_t, val); \
266 __cpu_to_le64(val); \
269 #define FIO_DEF_CL_SIZE 128
271 static inline int os_cache_line_size(void)
273 #ifdef FIO_HAVE_CL_SIZE
274 int ret = arch_cache_line_size();
277 return FIO_DEF_CL_SIZE;
281 return FIO_DEF_CL_SIZE;
285 #ifdef FIO_USE_GENERIC_BDEV_SIZE
286 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
292 end = lseek(f->fd, 0, SEEK_END);
301 #ifdef FIO_USE_GENERIC_RAND
302 typedef unsigned int os_random_state_t;
304 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
309 static inline long os_random_long(os_random_state_t *rs)
318 #ifdef FIO_USE_GENERIC_INIT_RANDOM_STATE
319 static inline int init_random_seeds(unsigned long *rand_seeds, int size)
323 fd = open("/dev/urandom", O_RDONLY);
328 if (read(fd, rand_seeds, size) < size) {
338 #ifndef FIO_HAVE_FS_STAT
339 static inline unsigned long long get_fs_free_size(const char *path)
345 #ifndef FIO_HAVE_CPU_ONLINE_SYSCONF
346 static inline unsigned int cpus_online(void)
348 return sysconf(_SC_NPROCESSORS_ONLN);
353 #ifdef FIO_HAVE_CPU_AFFINITY
354 static inline int CPU_COUNT(os_cpu_mask_t *mask)
356 int max_cpus = cpus_online();
359 for (i = 0, nr_cpus = 0; i < max_cpus; i++)
360 if (fio_cpu_isset(mask, i))
368 #ifndef FIO_HAVE_GETTID
369 static inline int gettid(void)
375 #ifndef FIO_HAVE_SHM_ATTACH_REMOVED
376 static inline int shm_attach_to_open_removed(void)
382 #ifndef FIO_HAVE_NATIVE_FALLOCATE
383 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, uint64_t len)
390 #if defined(CONFIG_POSIX_FALLOCATE) || defined(FIO_HAVE_NATIVE_FALLOCATE)
391 # define FIO_HAVE_ANY_FALLOCATE
394 #ifndef FIO_HAVE_CPU_HAS
395 static inline bool os_cpu_has(cpu_features feature)