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>
21 #include "./os-linux-syscall.h"
25 #define FIO_HAVE_CPU_AFFINITY
26 #define FIO_HAVE_DISK_UTIL
28 #define FIO_HAVE_IOPRIO
29 #define FIO_HAVE_IOPRIO_CLASS
30 #define FIO_HAVE_IOSCHED_SWITCH
31 #define FIO_HAVE_ODIRECT
32 #define FIO_HAVE_HUGETLB
33 #define FIO_HAVE_RAWBIND
34 #define FIO_HAVE_BLKTRACE
35 #define FIO_HAVE_PSHARED_MUTEX
36 #define FIO_HAVE_CL_SIZE
37 #define FIO_HAVE_CGROUPS
38 #define FIO_HAVE_FS_STAT
40 #define FIO_HAVE_BINJECT
41 #define FIO_HAVE_GETTID
42 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
43 #define FIO_HAVE_PWRITEV2
46 #define FIO_HAVE_MMAP_HUGE
49 #define OS_MAP_ANON MAP_ANONYMOUS
51 typedef cpu_set_t os_cpu_mask_t;
53 typedef struct drand48_data os_random_state_t;
55 #ifdef CONFIG_3ARG_AFFINITY
56 #define fio_setaffinity(pid, cpumask) \
57 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
58 #define fio_getaffinity(pid, ptr) \
59 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
60 #elif defined(CONFIG_2ARG_AFFINITY)
61 #define fio_setaffinity(pid, cpumask) \
62 sched_setaffinity((pid), &(cpumask))
63 #define fio_getaffinity(pid, ptr) \
64 sched_getaffinity((pid), (ptr))
67 #define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
68 #define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
69 #define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask))
70 #define fio_cpu_count(mask) CPU_COUNT((mask))
72 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
78 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
83 #define FIO_MAX_CPUS CPU_SETSIZE
93 IOPRIO_WHO_PROCESS = 1,
98 #define IOPRIO_BITS 16
99 #define IOPRIO_CLASS_SHIFT 13
101 #define IOPRIO_MIN_PRIO 0 /* highest priority */
102 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
104 #define IOPRIO_MIN_PRIO_CLASS 0
105 #define IOPRIO_MAX_PRIO_CLASS 3
107 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
110 * If no class is set, assume BE
113 ioprio_class = IOPRIO_CLASS_BE;
115 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
116 return syscall(__NR_ioprio_set, which, who, ioprio);
119 static inline int gettid(void)
121 return syscall(__NR_gettid);
124 #define SPLICE_DEF_SIZE (64*1024)
127 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
131 #define BLKFLSBUF _IO(0x12,97)
135 #define BLKDISCARD _IO(0x12,119)
138 static inline int blockdev_invalidate_cache(struct fio_file *f)
140 return ioctl(f->fd, BLKFLSBUF);
143 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
145 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
151 static inline unsigned long long os_phys_mem(void)
153 long pagesize, pages;
155 pagesize = sysconf(_SC_PAGESIZE);
156 pages = sysconf(_SC_PHYS_PAGES);
157 if (pages == -1 || pagesize == -1)
160 return (unsigned long long) pages * (unsigned long long) pagesize;
163 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
168 static inline long os_random_long(os_random_state_t *rs)
176 static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
178 struct raw_config_request rq;
181 if (major(dev) != RAW_MAJOR)
185 * we should be able to find /dev/rawctl or /dev/raw/rawctl
187 fd = open("/dev/rawctl", O_RDONLY);
189 fd = open("/dev/raw/rawctl", O_RDONLY);
194 rq.raw_minor = minor(dev);
195 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
201 *majdev = rq.block_major;
202 *mindev = rq.block_minor;
207 #define FIO_O_NOATIME O_NOATIME
209 #define FIO_O_NOATIME 0
213 #define OS_O_ATOMIC O_ATOMIC
215 #define OS_O_ATOMIC 040000000
219 #define FIO_MADV_FREE MADV_REMOVE
222 #if defined(__builtin_bswap16)
223 #define fio_swap16(x) __builtin_bswap16(x)
225 #define fio_swap16(x) __bswap_16(x)
227 #if defined(__builtin_bswap32)
228 #define fio_swap32(x) __builtin_bswap32(x)
230 #define fio_swap32(x) __bswap_32(x)
232 #if defined(__builtin_bswap64)
233 #define fio_swap64(x) __builtin_bswap64(x)
235 #define fio_swap64(x) __bswap_64(x)
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));
260 static inline unsigned long long get_fs_free_size(const char *path)
262 unsigned long long ret;
265 if (statfs(path, &s) < 0)
269 ret *= (unsigned long long) s.f_bfree;
273 static inline int os_trim(int fd, unsigned long long start,
274 unsigned long long len)
281 if (!ioctl(fd, BLKDISCARD, range))
287 #ifdef CONFIG_SCHED_IDLE
288 static inline int fio_set_sched_idle(void)
290 struct sched_param p = { .sched_priority = 0, };
291 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
295 #ifndef POSIX_FADV_STREAMID
296 #define POSIX_FADV_STREAMID 8
299 #define FIO_HAVE_STREAMID
302 #define RWF_HIPRI 0x00000001
305 #define RWF_DSYNC 0x00000002
308 #define RWF_SYNC 0x00000004
311 #ifndef CONFIG_PWRITEV2
313 static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
316 *pos_l = offset & 0xffffffff;
317 *pos_h = ((uint64_t) offset) >> 32;
320 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
321 off_t offset, unsigned int flags)
323 unsigned long pos_l, pos_h;
325 make_pos_h_l(&pos_h, &pos_l, offset);
326 return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
328 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
329 off_t offset, unsigned int flags)
331 unsigned long pos_l, pos_h;
333 make_pos_h_l(&pos_h, &pos_l, offset);
334 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
337 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
338 off_t offset, unsigned int flags)
343 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
344 off_t offset, unsigned int flags)
349 #endif /* __NR_preadv2 */
350 #endif /* CONFIG_PWRITEV2 */