5 #define FIO_OS os_android
7 #define FIO_OS os_linux
10 #include <sys/ioctl.h>
12 #include <sys/syscall.h>
13 #include <sys/sysmacros.h>
20 #include <linux/unistd.h>
21 #include <linux/major.h>
24 #include <asm/byteorder.h>
26 #include "os-ashmem.h"
27 #define FIO_NO_HAVE_SHM_H
30 #ifdef ARCH_HAVE_CRC_CRYPTO
33 #define HWCAP_PMULL (1 << 4)
34 #endif /* HWCAP_PMULL */
36 #define HWCAP_CRC32 (1 << 7)
37 #endif /* HWCAP_CRC32 */
38 #endif /* ARCH_HAVE_CRC_CRYPTO */
40 #include "./os-linux-syscall.h"
43 #ifndef __has_builtin // Optional of course.
44 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
47 #define FIO_HAVE_CPU_AFFINITY
48 #define FIO_HAVE_DISK_UTIL
50 #define FIO_HAVE_IOPRIO
51 #define FIO_HAVE_IOPRIO_CLASS
52 #define FIO_HAVE_IOSCHED_SWITCH
53 #define FIO_HAVE_ODIRECT
54 #define FIO_HAVE_HUGETLB
55 #define FIO_HAVE_BLKTRACE
56 #define FIO_HAVE_CL_SIZE
57 #define FIO_HAVE_CGROUPS
58 #define FIO_HAVE_FS_STAT
60 #define FIO_HAVE_GETTID
61 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
62 #define FIO_HAVE_BYTEORDER_FUNCS
63 #define FIO_HAVE_PWRITEV2
64 #define FIO_HAVE_SHM_ATTACH_REMOVED
65 #define FIO_HAVE_RWF_ATOMIC
68 #define FIO_HAVE_MMAP_HUGE
71 #define OS_MAP_ANON MAP_ANONYMOUS
73 #define FIO_EXT_ENG_DIR "/usr/local/lib/fio"
75 typedef cpu_set_t os_cpu_mask_t;
77 #ifdef CONFIG_3ARG_AFFINITY
78 #define fio_setaffinity(pid, cpumask) \
79 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
80 #define fio_getaffinity(pid, ptr) \
81 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
82 #elif defined(CONFIG_2ARG_AFFINITY)
83 #define fio_setaffinity(pid, cpumask) \
84 sched_setaffinity((pid), &(cpumask))
85 #define fio_getaffinity(pid, ptr) \
86 sched_getaffinity((pid), (ptr))
89 #ifdef CONFIG_PTHREAD_GETAFFINITY
90 #define FIO_HAVE_GET_THREAD_AFFINITY
91 #define fio_get_thread_affinity(mask) \
92 pthread_getaffinity_np(pthread_self(), sizeof(mask), &(mask))
95 #define fio_cpu_clear(mask, cpu) CPU_CLR((cpu), (mask))
96 #define fio_cpu_set(mask, cpu) CPU_SET((cpu), (mask))
97 #define fio_cpu_isset(mask, cpu) (CPU_ISSET((cpu), (mask)) != 0)
98 #define fio_cpu_count(mask) CPU_COUNT((mask))
100 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
106 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
111 #define FIO_MAX_CPUS CPU_SETSIZE
121 IOPRIO_WHO_PROCESS = 1,
126 #define IOPRIO_BITS 16
127 #define IOPRIO_CLASS_SHIFT 13
129 #define IOPRIO_HINT_BITS 10
130 #define IOPRIO_HINT_SHIFT 3
132 #define IOPRIO_MIN_PRIO 0 /* highest priority */
133 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
135 #define IOPRIO_MIN_PRIO_CLASS 0
136 #define IOPRIO_MAX_PRIO_CLASS 3
138 #define IOPRIO_MIN_PRIO_HINT 0
139 #define IOPRIO_MAX_PRIO_HINT ((1 << IOPRIO_HINT_BITS) - 1)
141 #define ioprio_class(ioprio) ((ioprio) >> IOPRIO_CLASS_SHIFT)
142 #define ioprio(ioprio) ((ioprio) & IOPRIO_MAX_PRIO)
143 #define ioprio_hint(ioprio) \
144 (((ioprio) >> IOPRIO_HINT_SHIFT) & IOPRIO_MAX_PRIO_HINT)
146 static inline int ioprio_value(int ioprio_class, int ioprio, int ioprio_hint)
149 * If no class is set, assume BE
152 ioprio_class = IOPRIO_CLASS_BE;
154 return (ioprio_class << IOPRIO_CLASS_SHIFT) |
155 (ioprio_hint << IOPRIO_HINT_SHIFT) |
159 static inline bool ioprio_value_is_class_rt(unsigned int priority)
161 return ioprio_class(priority) == IOPRIO_CLASS_RT;
164 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio,
167 return syscall(__NR_ioprio_set, which, who,
168 ioprio_value(ioprio_class, ioprio, ioprio_hint));
171 #ifndef CONFIG_HAVE_GETTID
172 static inline int gettid(void)
174 return syscall(__NR_gettid);
178 #define SPLICE_DEF_SIZE (64*1024)
181 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
185 #define BLKFLSBUF _IO(0x12,97)
189 #define BLKDISCARD _IO(0x12,119)
192 static inline int blockdev_invalidate_cache(struct fio_file *f)
194 return ioctl(f->fd, BLKFLSBUF);
197 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
199 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
205 static inline unsigned long long os_phys_mem(void)
207 long pagesize, pages;
209 pagesize = sysconf(_SC_PAGESIZE);
210 pages = sysconf(_SC_PHYS_PAGES);
211 if (pages == -1 || pagesize == -1)
214 return (unsigned long long) pages * (unsigned long long) pagesize;
218 #define FIO_O_NOATIME O_NOATIME
220 #define FIO_O_NOATIME 0
224 #define FIO_MADV_FREE MADV_REMOVE
227 /* Check for GCC or Clang byte swap intrinsics */
228 #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
229 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
230 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
231 #define fio_swap16(x) __builtin_bswap16(x)
232 #define fio_swap32(x) __builtin_bswap32(x)
233 #define fio_swap64(x) __builtin_bswap64(x)
235 #include <byteswap.h>
236 #define fio_swap16(x) bswap_16(x)
237 #define fio_swap32(x) bswap_32(x)
238 #define fio_swap64(x) bswap_64(x)
239 #endif /* fio_swapN */
241 #define CACHE_LINE_FILE \
242 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
244 static inline int arch_cache_line_size(void)
249 fd = open(CACHE_LINE_FILE, O_RDONLY);
253 ret = read(fd, size, sizeof(size));
263 static inline unsigned long long get_fs_free_size(const char *path)
265 unsigned long long ret;
268 if (statfs(path, &s) < 0)
272 ret *= (unsigned long long) s.f_bfree;
276 static inline int os_trim(struct fio_file *f, unsigned long long start,
277 unsigned long long len)
284 if (!ioctl(f->fd, BLKDISCARD, range))
290 #ifdef CONFIG_SCHED_IDLE
291 static inline int fio_set_sched_idle(void)
293 struct sched_param p = { .sched_priority = 0, };
294 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
298 #ifndef F_GET_RW_HINT
299 #ifndef F_LINUX_SPECIFIC_BASE
300 #define F_LINUX_SPECIFIC_BASE 1024
302 #define F_GET_RW_HINT (F_LINUX_SPECIFIC_BASE + 11)
303 #define F_SET_RW_HINT (F_LINUX_SPECIFIC_BASE + 12)
304 #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)
305 #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14)
308 #ifndef RWH_WRITE_LIFE_NONE
309 #define RWH_WRITE_LIFE_NOT_SET 0
310 #define RWH_WRITE_LIFE_NONE 1
311 #define RWH_WRITE_LIFE_SHORT 2
312 #define RWH_WRITE_LIFE_MEDIUM 3
313 #define RWH_WRITE_LIFE_LONG 4
314 #define RWH_WRITE_LIFE_EXTREME 5
317 #define FIO_HAVE_WRITE_HINT
320 #define RWF_HIPRI 0x00000001
323 #define RWF_DSYNC 0x00000002
326 #define RWF_SYNC 0x00000004
329 #define RWF_NOWAIT 0x00000008
333 #define RWF_ATOMIC 0x00000040
336 #ifndef RWF_WRITE_LIFE_SHIFT
337 #define RWF_WRITE_LIFE_SHIFT 4
338 #define RWF_WRITE_LIFE_SHORT (1 << RWF_WRITE_LIFE_SHIFT)
339 #define RWF_WRITE_LIFE_MEDIUM (2 << RWF_WRITE_LIFE_SHIFT)
340 #define RWF_WRITE_LIFE_LONG (3 << RWF_WRITE_LIFE_SHIFT)
341 #define RWF_WRITE_LIFE_EXTREME (4 << RWF_WRITE_LIFE_SHIFT)
344 #ifndef CONFIG_PWRITEV2
346 static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
349 #if BITS_PER_LONG == 64
353 *pos_l = offset & 0xffffffff;
354 *pos_h = ((uint64_t) offset) >> 32;
357 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
358 off_t offset, unsigned int flags)
360 unsigned long pos_l, pos_h;
362 make_pos_h_l(&pos_h, &pos_l, offset);
363 return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
365 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
366 off_t offset, unsigned int flags)
368 unsigned long pos_l, pos_h;
370 make_pos_h_l(&pos_h, &pos_l, offset);
371 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
374 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
375 off_t offset, unsigned int flags)
380 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
381 off_t offset, unsigned int flags)
386 #endif /* __NR_preadv2 */
387 #endif /* CONFIG_PWRITEV2 */
389 static inline int shm_attach_to_open_removed(void)
394 #ifdef CONFIG_LINUX_FALLOCATE
395 #define FIO_HAVE_NATIVE_FALLOCATE
396 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset,
400 ret = fallocate(f->fd, 0, offset, len);
404 /* Work around buggy old glibc versions... */
412 #define FIO_HAVE_CPU_HAS
413 static inline bool os_cpu_has(cpu_features feature)
416 unsigned long fio_unused hwcap;
419 #ifdef ARCH_HAVE_CRC_CRYPTO
420 case CPU_ARM64_CRC32C:
421 hwcap = getauxval(AT_HWCAP);
422 have_feature = (hwcap & (HWCAP_PMULL | HWCAP_CRC32)) ==
423 (HWCAP_PMULL | HWCAP_CRC32);
427 have_feature = false;