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>
24 #define FIO_HAVE_CPU_AFFINITY
25 #define FIO_HAVE_DISK_UTIL
27 #define FIO_HAVE_IOPRIO
28 #define FIO_HAVE_IOSCHED_SWITCH
29 #define FIO_HAVE_ODIRECT
30 #define FIO_HAVE_HUGETLB
31 #define FIO_HAVE_RAWBIND
32 #define FIO_HAVE_BLKTRACE
33 #define FIO_HAVE_PSHARED_MUTEX
34 #define FIO_HAVE_CL_SIZE
35 #define FIO_HAVE_CGROUPS
36 #define FIO_HAVE_FS_STAT
38 #define FIO_HAVE_BINJECT
39 #define FIO_HAVE_GETTID
40 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
41 #define FIO_HAVE_PWRITEV2
44 #define FIO_HAVE_MMAP_HUGE
47 #define OS_MAP_ANON MAP_ANONYMOUS
49 typedef cpu_set_t os_cpu_mask_t;
51 typedef struct drand48_data os_random_state_t;
53 #ifdef CONFIG_3ARG_AFFINITY
54 #define fio_setaffinity(pid, cpumask) \
55 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
56 #define fio_getaffinity(pid, ptr) \
57 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
58 #elif defined(CONFIG_2ARG_AFFINITY)
59 #define fio_setaffinity(pid, cpumask) \
60 sched_setaffinity((pid), &(cpumask))
61 #define fio_getaffinity(pid, ptr) \
62 sched_getaffinity((pid), (ptr))
65 #define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
66 #define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
67 #define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask))
68 #define fio_cpu_count(mask) CPU_COUNT((mask))
70 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
76 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
81 #define FIO_MAX_CPUS CPU_SETSIZE
91 IOPRIO_WHO_PROCESS = 1,
96 #define IOPRIO_BITS 16
97 #define IOPRIO_CLASS_SHIFT 13
99 #define IOPRIO_MIN_PRIO 0 /* highest priority */
100 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
102 #define IOPRIO_MIN_PRIO_CLASS 0
103 #define IOPRIO_MAX_PRIO_CLASS 3
105 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
108 * If no class is set, assume BE
111 ioprio_class = IOPRIO_CLASS_BE;
113 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
114 return syscall(__NR_ioprio_set, which, who, ioprio);
117 static inline int gettid(void)
119 return syscall(__NR_gettid);
122 #define SPLICE_DEF_SIZE (64*1024)
125 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
129 #define BLKFLSBUF _IO(0x12,97)
133 #define BLKDISCARD _IO(0x12,119)
136 static inline int blockdev_invalidate_cache(struct fio_file *f)
138 return ioctl(f->fd, BLKFLSBUF);
141 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
143 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
149 static inline unsigned long long os_phys_mem(void)
151 long pagesize, pages;
153 pagesize = sysconf(_SC_PAGESIZE);
154 pages = sysconf(_SC_PHYS_PAGES);
155 if (pages == -1 || pagesize == -1)
158 return (unsigned long long) pages * (unsigned long long) pagesize;
161 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
166 static inline long os_random_long(os_random_state_t *rs)
174 static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
176 struct raw_config_request rq;
179 if (major(dev) != RAW_MAJOR)
183 * we should be able to find /dev/rawctl or /dev/raw/rawctl
185 fd = open("/dev/rawctl", O_RDONLY);
187 fd = open("/dev/raw/rawctl", O_RDONLY);
192 rq.raw_minor = minor(dev);
193 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
199 *majdev = rq.block_major;
200 *mindev = rq.block_minor;
205 #define FIO_O_NOATIME O_NOATIME
207 #define FIO_O_NOATIME 0
211 #define OS_O_ATOMIC O_ATOMIC
213 #define OS_O_ATOMIC 040000000
217 #define FIO_MADV_FREE MADV_REMOVE
220 #if defined(__builtin_bswap16)
221 #define fio_swap16(x) __builtin_bswap16(x)
223 #define fio_swap16(x) __bswap_16(x)
225 #if defined(__builtin_bswap32)
226 #define fio_swap32(x) __builtin_bswap32(x)
228 #define fio_swap32(x) __bswap_32(x)
230 #if defined(__builtin_bswap64)
231 #define fio_swap64(x) __builtin_bswap64(x)
233 #define fio_swap64(x) __bswap_64(x)
236 #define CACHE_LINE_FILE \
237 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
239 static inline int arch_cache_line_size(void)
244 fd = open(CACHE_LINE_FILE, O_RDONLY);
248 ret = read(fd, size, sizeof(size));
258 static inline unsigned long long get_fs_free_size(const char *path)
260 unsigned long long ret;
263 if (statfs(path, &s) < 0)
267 ret *= (unsigned long long) s.f_bfree;
271 static inline int os_trim(int fd, unsigned long long start,
272 unsigned long long len)
279 if (!ioctl(fd, BLKDISCARD, range))
285 #ifdef CONFIG_SCHED_IDLE
286 static inline int fio_set_sched_idle(void)
288 struct sched_param p = { .sched_priority = 0, };
289 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
293 #ifndef POSIX_FADV_STREAMID
294 #define POSIX_FADV_STREAMID 8
297 #define FIO_HAVE_STREAMID
300 #define RWF_HIPRI 0x00000001
303 #define RWF_DSYNC 0x00000002
306 #define RWF_SYNC 0x00000004
309 #ifndef CONFIG_PWRITEV2
311 static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
314 *pos_l = offset & 0xffffffff;
315 *pos_h = ((uint64_t) offset) >> 32;
318 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
319 off_t offset, unsigned int flags)
321 unsigned long pos_l, pos_h;
323 make_pos_h_l(&pos_h, &pos_l, offset);
324 return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
326 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
327 off_t offset, unsigned int flags)
329 unsigned long pos_l, pos_h;
331 make_pos_h_l(&pos_h, &pos_l, offset);
332 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
335 static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
336 off_t offset, unsigned int flags)
341 static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
342 off_t offset, unsigned int flags)
347 #endif /* __NR_preadv2 */
348 #endif /* CONFIG_PWRITEV2 */