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