Make I/O priority option generic for non-Linux environment [1/2]
[fio.git] / os / os-linux.h
CommitLineData
ebac4655
JA
1#ifndef FIO_OS_LINUX_H
2#define FIO_OS_LINUX_H
3
cca84643
JA
4#define FIO_OS os_linux
5
ebac4655 6#include <sys/ioctl.h>
8756e4d4 7#include <sys/uio.h>
3c39a379 8#include <sys/syscall.h>
250e878a 9#include <sys/sysmacros.h>
2e3bd4c2 10#include <sys/vfs.h>
39b93568 11#include <sys/mman.h>
3c39a379 12#include <unistd.h>
97fbdfa1 13#include <fcntl.h>
3c2d93ed 14#include <errno.h>
d09913c2 15#include <sched.h>
ea421790 16#include <linux/unistd.h>
07e5b264
JA
17#include <linux/raw.h>
18#include <linux/major.h>
df73aab1 19#include <byteswap.h>
ebac4655 20
79a43187 21#include "binject.h"
e2e58886 22#include "../file.h"
f3de88a7 23
ebac4655
JA
24#define FIO_HAVE_CPU_AFFINITY
25#define FIO_HAVE_DISK_UTIL
26#define FIO_HAVE_SGIO
ba4f8923 27#define FIO_HAVE_IOPRIO
22f78b32 28#define FIO_HAVE_IOSCHED_SWITCH
2c0ecd28 29#define FIO_HAVE_ODIRECT
74b025b0 30#define FIO_HAVE_HUGETLB
07e5b264 31#define FIO_HAVE_RAWBIND
5e62c22a 32#define FIO_HAVE_BLKTRACE
f356d01d 33#define FIO_HAVE_PSHARED_MUTEX
eb7ccf38 34#define FIO_HAVE_CL_SIZE
a696fa2a 35#define FIO_HAVE_CGROUPS
2e3bd4c2 36#define FIO_HAVE_FS_STAT
a5f3027c 37#define FIO_HAVE_TRIM
79a43187 38#define FIO_HAVE_BINJECT
47f767c1 39#define FIO_HAVE_GETTID
93bcfd20 40#define FIO_USE_GENERIC_INIT_RANDOM_STATE
6562685f 41#define FIO_HAVE_PWRITEV2
b6959b55 42
39b93568
JA
43#ifdef MAP_HUGETLB
44#define FIO_HAVE_MMAP_HUGE
45#endif
46
dc873b6f 47#define OS_MAP_ANON MAP_ANONYMOUS
ebac4655
JA
48
49typedef cpu_set_t os_cpu_mask_t;
4e78e405 50
6dfd46b9 51typedef struct drand48_data os_random_state_t;
ebac4655 52
67bf9823 53#ifdef CONFIG_3ARG_AFFINITY
e8462bd8
JA
54#define fio_setaffinity(pid, cpumask) \
55 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
ebac4655
JA
56#define fio_getaffinity(pid, ptr) \
57 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
67bf9823 58#elif defined(CONFIG_2ARG_AFFINITY)
e8462bd8
JA
59#define fio_setaffinity(pid, cpumask) \
60 sched_setaffinity((pid), &(cpumask))
c8f025f8
JA
61#define fio_getaffinity(pid, ptr) \
62 sched_getaffinity((pid), (ptr))
63#endif
be4ecfdf 64
1f4c0a4f
JA
65#define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
66#define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
50b5860b 67#define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask))
d004a209 68#define fio_cpu_count(mask) CPU_COUNT((mask))
d2ce18b5
JA
69
70static inline int fio_cpuset_init(os_cpu_mask_t *mask)
71{
72 CPU_ZERO(mask);
73 return 0;
74}
75
76static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
77{
78 return 0;
79}
6d459ee7
JA
80
81#define FIO_MAX_CPUS CPU_SETSIZE
4d8947de 82
28727df7
JA
83enum {
84 IOPRIO_CLASS_NONE,
85 IOPRIO_CLASS_RT,
86 IOPRIO_CLASS_BE,
87 IOPRIO_CLASS_IDLE,
88};
89
90enum {
91 IOPRIO_WHO_PROCESS = 1,
92 IOPRIO_WHO_PGRP,
93 IOPRIO_WHO_USER,
94};
95
96#define IOPRIO_BITS 16
97#define IOPRIO_CLASS_SHIFT 13
98
1767bd34
TK
99#define IOPRIO_MIN_PRIO 0 /* highest priority */
100#define IOPRIO_MAX_PRIO 7 /* lowest priority */
101
102#define IOPRIO_MIN_PRIO_CLASS 0
103#define IOPRIO_MAX_PRIO_CLASS 3
104
28727df7 105static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
ebac4655 106{
28727df7
JA
107 /*
108 * If no class is set, assume BE
109 */
110 if (!ioprio_class)
111 ioprio_class = IOPRIO_CLASS_BE;
112
113 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
ebac4655
JA
114 return syscall(__NR_ioprio_set, which, who, ioprio);
115}
116
47f767c1
JA
117static inline int gettid(void)
118{
119 return syscall(__NR_gettid);
120}
121
3feedc60
JA
122#define SPLICE_DEF_SIZE (64*1024)
123
ebac4655
JA
124#ifndef BLKGETSIZE64
125#define BLKGETSIZE64 _IOR(0x12,114,size_t)
126#endif
127
e5b401d4
JA
128#ifndef BLKFLSBUF
129#define BLKFLSBUF _IO(0x12,97)
130#endif
131
a5f3027c
JA
132#ifndef BLKDISCARD
133#define BLKDISCARD _IO(0x12,119)
134#endif
135
e2e58886 136static inline int blockdev_invalidate_cache(struct fio_file *f)
e5b401d4 137{
ecc314ba 138 return ioctl(f->fd, BLKFLSBUF);
e5b401d4
JA
139}
140
ecc314ba 141static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
ebac4655 142{
ecc314ba 143 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
ebac4655
JA
144 return 0;
145
146 return errno;
147}
148
32cd46a0
JA
149static inline unsigned long long os_phys_mem(void)
150{
151 long pagesize, pages;
152
153 pagesize = sysconf(_SC_PAGESIZE);
154 pages = sysconf(_SC_PHYS_PAGES);
155 if (pages == -1 || pagesize == -1)
156 return 0;
157
158 return (unsigned long long) pages * (unsigned long long) pagesize;
159}
160
6dfd46b9
JA
161static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
162{
163 srand48_r(seed, rs);
164}
165
166static inline long os_random_long(os_random_state_t *rs)
167{
168 long val;
169
170 lrand48_r(rs, &val);
171 return val;
172}
173
8cc7afa9 174static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
07e5b264
JA
175{
176 struct raw_config_request rq;
177 int fd;
178
179 if (major(dev) != RAW_MAJOR)
8cc7afa9 180 return 1;
07e5b264
JA
181
182 /*
183 * we should be able to find /dev/rawctl or /dev/raw/rawctl
184 */
185 fd = open("/dev/rawctl", O_RDONLY);
186 if (fd < 0) {
187 fd = open("/dev/raw/rawctl", O_RDONLY);
188 if (fd < 0)
8cc7afa9 189 return 1;
07e5b264
JA
190 }
191
192 rq.raw_minor = minor(dev);
193 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
194 close(fd);
8cc7afa9 195 return 1;
07e5b264
JA
196 }
197
198 close(fd);
199 *majdev = rq.block_major;
200 *mindev = rq.block_minor;
8cc7afa9 201 return 0;
07e5b264
JA
202}
203
5b6f5c66 204#ifdef O_NOATIME
5921e80c 205#define FIO_O_NOATIME O_NOATIME
5b6f5c66
JA
206#else
207#define FIO_O_NOATIME 0
208#endif
5921e80c 209
d01612f3
CM
210#ifdef O_ATOMIC
211#define OS_O_ATOMIC O_ATOMIC
212#else
213#define OS_O_ATOMIC 040000000
214#endif
215
a1c58075
JA
216#ifdef MADV_REMOVE
217#define FIO_MADV_FREE MADV_REMOVE
218#endif
219
df73aab1
JA
220#if defined(__builtin_bswap16)
221#define fio_swap16(x) __builtin_bswap16(x)
222#else
ff245192 223#define fio_swap16(x) __bswap_16(x)
df73aab1
JA
224#endif
225#if defined(__builtin_bswap32)
226#define fio_swap32(x) __builtin_bswap32(x)
227#else
ff245192 228#define fio_swap32(x) __bswap_32(x)
df73aab1
JA
229#endif
230#if defined(__builtin_bswap64)
231#define fio_swap64(x) __builtin_bswap64(x)
232#else
ff245192 233#define fio_swap64(x) __bswap_64(x)
df73aab1 234#endif
ff245192 235
eb7ccf38
JA
236#define CACHE_LINE_FILE \
237 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
238
239static inline int arch_cache_line_size(void)
240{
241 char size[32];
242 int fd, ret;
243
244 fd = open(CACHE_LINE_FILE, O_RDONLY);
245 if (fd < 0)
246 return -1;
247
248 ret = read(fd, size, sizeof(size));
ec76f15a
JA
249
250 close(fd);
251
eb7ccf38
JA
252 if (ret <= 0)
253 return -1;
ec76f15a
JA
254 else
255 return atoi(size);
eb7ccf38
JA
256}
257
c08ad04c 258static inline unsigned long long get_fs_free_size(const char *path)
2e3bd4c2
JA
259{
260 unsigned long long ret;
261 struct statfs s;
262
263 if (statfs(path, &s) < 0)
264 return -1ULL;
265
266 ret = s.f_bsize;
267 ret *= (unsigned long long) s.f_bfree;
268 return ret;
269}
270
a5f3027c
JA
271static inline int os_trim(int fd, unsigned long long start,
272 unsigned long long len)
273{
274 uint64_t range[2];
275
276 range[0] = start;
277 range[1] = len;
278
279 if (!ioctl(fd, BLKDISCARD, range))
280 return 0;
281
282 return errno;
283}
284
7e09a9f1 285#ifdef CONFIG_SCHED_IDLE
f2a2ce0e
HL
286static inline int fio_set_sched_idle(void)
287{
288 struct sched_param p = { .sched_priority = 0, };
289 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
290}
7e09a9f1 291#endif
f2a2ce0e 292
37659335
JA
293#ifndef POSIX_FADV_STREAMID
294#define POSIX_FADV_STREAMID 8
295#endif
296
297#define FIO_HAVE_STREAMID
298
6562685f
JA
299#ifndef RWF_HIPRI
300#define RWF_HIPRI 0x00000001
301#endif
302#ifndef RWF_DSYNC
303#define RWF_DSYNC 0x00000002
304#endif
305#ifndef RWF_SYNC
306#define RWF_SYNC 0x00000004
307#endif
308
309#ifndef CONFIG_PWRITEV2
310#ifdef __NR_preadv2
311static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
312 off_t offset)
313{
314 *pos_l = offset & 0xffffffff;
315 *pos_h = ((uint64_t) offset) >> 32;
316
317}
318static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
319 off_t offset, unsigned int flags)
320{
321 unsigned long pos_l, pos_h;
322
323 make_pos_h_l(&pos_h, &pos_l, offset);
324 return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
325}
326static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
327 off_t offset, unsigned int flags)
328{
329 unsigned long pos_l, pos_h;
330
331 make_pos_h_l(&pos_h, &pos_l, offset);
332 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
333}
334#else
335static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
336 off_t offset, unsigned int flags)
337{
338 errno = ENOSYS;
339 return -1;
340}
341static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
342 off_t offset, unsigned int flags)
343{
344 errno = ENOSYS;
345 return -1;
346}
347#endif /* __NR_preadv2 */
348#endif /* CONFIG_PWRITEV2 */
349
ebac4655 350#endif