4 #define FIO_OS os_linux
8 #include <sys/syscall.h>
9 #include <sys/sysmacros.h>
16 #include <linux/unistd.h>
17 #include <linux/raw.h>
18 #include <linux/major.h>
22 #ifdef ARCH_HAVE_CRC_CRYPTO
25 #define HWCAP_CRC32 (1 << 7)
26 #endif /* HWCAP_CRC32 */
27 #endif /* ARCH_HAVE_CRC_CRYPTO */
29 #include "./os-linux-syscall.h"
32 #ifndef __has_builtin // Optional of course.
33 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
36 #define FIO_HAVE_CPU_AFFINITY
37 #define FIO_HAVE_DISK_UTIL
39 #define FIO_HAVE_IOPRIO
40 #define FIO_HAVE_IOPRIO_CLASS
41 #define FIO_HAVE_IOSCHED_SWITCH
42 #define FIO_HAVE_ODIRECT
43 #define FIO_HAVE_HUGETLB
44 #define FIO_HAVE_RAWBIND
45 #define FIO_HAVE_BLKTRACE
46 #define FIO_HAVE_CL_SIZE
47 #define FIO_HAVE_CGROUPS
48 #define FIO_HAVE_FS_STAT
50 #define FIO_HAVE_GETTID
51 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
52 #define FIO_HAVE_PWRITEV2
53 #define FIO_HAVE_SHM_ATTACH_REMOVED
56 #define FIO_HAVE_MMAP_HUGE
59 #define OS_MAP_ANON MAP_ANONYMOUS
61 #define FIO_EXT_ENG_DIR "/usr/local/lib/fio"
63 typedef cpu_set_t os_cpu_mask_t;
65 #ifdef CONFIG_3ARG_AFFINITY
66 #define fio_setaffinity(pid, cpumask) \
67 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
68 #define fio_getaffinity(pid, ptr) \
69 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
70 #elif defined(CONFIG_2ARG_AFFINITY)
71 #define fio_setaffinity(pid, cpumask) \
72 sched_setaffinity((pid), &(cpumask))
73 #define fio_getaffinity(pid, ptr) \
74 sched_getaffinity((pid), (ptr))
77 #define fio_get_thread_affinity(mask) \
78 pthread_getaffinity_np(pthread_self(), sizeof(mask), &(mask))
80 #define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
81 #define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
82 #define fio_cpu_isset(mask, cpu) (CPU_ISSET((cpu), (mask)) != 0)
83 #define fio_cpu_count(mask) CPU_COUNT((mask))
85 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
91 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
96 #define FIO_MAX_CPUS CPU_SETSIZE
106 IOPRIO_WHO_PROCESS = 1,
111 #define IOPRIO_BITS 16
112 #define IOPRIO_CLASS_SHIFT 13
114 #define IOPRIO_MIN_PRIO 0 /* highest priority */
115 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
117 #define IOPRIO_MIN_PRIO_CLASS 0
118 #define IOPRIO_MAX_PRIO_CLASS 3
120 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
123 * If no class is set, assume BE
126 ioprio_class = IOPRIO_CLASS_BE;
128 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
129 return syscall(__NR_ioprio_set, which, who, ioprio);
132 #ifndef CONFIG_HAVE_GETTID
133 static inline int gettid(void)
135 return syscall(__NR_gettid);
139 #define SPLICE_DEF_SIZE (64*1024)
142 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
146 #define BLKFLSBUF _IO(0x12,97)
150 #define BLKDISCARD _IO(0x12,119)
153 static inline int blockdev_invalidate_cache(struct fio_file *f)
155 return ioctl(f->fd, BLKFLSBUF);
158 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
160 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
166 static inline unsigned long long os_phys_mem(void)
168 long pagesize, pages;
170 pagesize = sysconf(_SC_PAGESIZE);
171 pages = sysconf(_SC_PHYS_PAGES);
172 if (pages == -1 || pagesize == -1)
175 return (unsigned long long) pages * (unsigned long long) pagesize;
178 static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
180 struct raw_config_request rq;
183 if (major(dev) != RAW_MAJOR)
187 * we should be able to find /dev/rawctl or /dev/raw/rawctl
189 fd = open("/dev/rawctl", O_RDONLY);
191 fd = open("/dev/raw/rawctl", O_RDONLY);
196 rq.raw_minor = minor(dev);
197 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
203 *majdev = rq.block_major;
204 *mindev = rq.block_minor;
209 #define FIO_O_NOATIME O_NOATIME
211 #define FIO_O_NOATIME 0
215 #define OS_O_ATOMIC O_ATOMIC
217 #define OS_O_ATOMIC 040000000
221 #define FIO_MADV_FREE MADV_REMOVE
224 /* Check for GCC or Clang byte swap intrinsics */
225 #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
226 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
227 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
228 #define fio_swap16(x) __builtin_bswap16(x)
229 #define fio_swap32(x) __builtin_bswap32(x)
230 #define fio_swap64(x) __builtin_bswap64(x)
232 #include <byteswap.h>
233 #define fio_swap16(x) bswap_16(x)
234 #define fio_swap32(x) bswap_32(x)
235 #define fio_swap64(x) bswap_64(x)
236 #endif /* fio_swapN */
238 #define CACHE_LINE_FILE \
239 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
241 static inline int arch_cache_line_size(void)
246 fd = open(CACHE_LINE_FILE, O_RDONLY);
250 ret = read(fd, size, sizeof(size));
261 #define FIO_HAVE_CPU_ONLINE_SYSCONF
262 static inline unsigned int cpus_online(void)
264 return sysconf(_SC_NPROCESSORS_CONF);
268 static inline unsigned long long get_fs_free_size(const char *path)
270 unsigned long long ret;
273 if (statfs(path, &s) < 0)
277 ret *= (unsigned long long) s.f_bfree;
281 static inline int os_trim(struct fio_file *f, unsigned long long start,
282 unsigned long long len)
289 if (!ioctl(f->fd, BLKDISCARD, range))
295 #ifdef CONFIG_SCHED_IDLE
296 static inline int fio_set_sched_idle(void)
298 struct sched_param p = { .sched_priority = 0, };
299 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
303 #ifndef F_GET_RW_HINT
304 #ifndef F_LINUX_SPECIFIC_BASE
305 #define F_LINUX_SPECIFIC_BASE 1024
307 #define F_GET_RW_HINT (F_LINUX_SPECIFIC_BASE + 11)
308 #define F_SET_RW_HINT (F_LINUX_SPECIFIC_BASE + 12)
309 #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)
310 #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14)
313 #ifndef RWH_WRITE_LIFE_NONE
314 #define RWH_WRITE_LIFE_NOT_SET 0
315 #define RWH_WRITE_LIFE_NONE 1
316 #define RWH_WRITE_LIFE_SHORT 2
317 #define RWH_WRITE_LIFE_MEDIUM 3
318 #define RWH_WRITE_LIFE_LONG 4
319 #define RWH_WRITE_LIFE_EXTREME 5
322 #define FIO_HAVE_WRITE_HINT
325 #define RWF_HIPRI 0x00000001
328 #define RWF_DSYNC 0x00000002
331 #define RWF_SYNC 0x00000004
334 #define RWF_NOWAIT 0x00000008
338 #define RWF_UNCACHED 0x00000040
341 #ifndef RWF_WRITE_LIFE_SHIFT
342 #define RWF_WRITE_LIFE_SHIFT 4
343 #define RWF_WRITE_LIFE_SHORT (1 << RWF_WRITE_LIFE_SHIFT)
344 #define RWF_WRITE_LIFE_MEDIUM (2 << RWF_WRITE_LIFE_SHIFT)
345 #define RWF_WRITE_LIFE_LONG (3 << RWF_WRITE_LIFE_SHIFT)
346 #define RWF_WRITE_LIFE_EXTREME (4 << RWF_WRITE_LIFE_SHIFT)
349 #ifndef CONFIG_PWRITEV2
351 static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
354 #if BITS_PER_LONG == 64
358 *pos_l = offset & 0xffffffff;
359 *pos_h = ((uint64_t) offset) >> 32;
362 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
363 off_t offset, unsigned int flags)
365 unsigned long pos_l, pos_h;
367 make_pos_h_l(&pos_h, &pos_l, offset);
368 return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
370 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
371 off_t offset, unsigned int flags)
373 unsigned long pos_l, pos_h;
375 make_pos_h_l(&pos_h, &pos_l, offset);
376 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
379 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
380 off_t offset, unsigned int flags)
385 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
386 off_t offset, unsigned int flags)
391 #endif /* __NR_preadv2 */
392 #endif /* CONFIG_PWRITEV2 */
394 static inline int shm_attach_to_open_removed(void)
399 #ifdef CONFIG_LINUX_FALLOCATE
400 #define FIO_HAVE_NATIVE_FALLOCATE
401 static inline bool fio_fallocate(struct fio_file *f, uint64_t offset,
405 ret = fallocate(f->fd, 0, offset, len);
409 /* Work around buggy old glibc versions... */
417 #define FIO_HAVE_CPU_HAS
418 static inline bool os_cpu_has(cpu_features feature)
421 unsigned long fio_unused hwcap;
424 #ifdef ARCH_HAVE_CRC_CRYPTO
425 case CPU_ARM64_CRC32C:
426 hwcap = getauxval(AT_HWCAP);
427 have_feature = (hwcap & HWCAP_CRC32) != 0;
431 have_feature = false;