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