Merge branch 'atomic-writes'
[fio.git] / os / os-linux.h
CommitLineData
ebac4655
JA
1#ifndef FIO_OS_LINUX_H
2#define FIO_OS_LINUX_H
3
910f378a
BVA
4#ifdef __ANDROID__
5#define FIO_OS os_android
6#else
cca84643 7#define FIO_OS os_linux
910f378a 8#endif
cca84643 9
ebac4655 10#include <sys/ioctl.h>
8756e4d4 11#include <sys/uio.h>
3c39a379 12#include <sys/syscall.h>
250e878a 13#include <sys/sysmacros.h>
2e3bd4c2 14#include <sys/vfs.h>
39b93568 15#include <sys/mman.h>
3c39a379 16#include <unistd.h>
97fbdfa1 17#include <fcntl.h>
3c2d93ed 18#include <errno.h>
d09913c2 19#include <sched.h>
ea421790 20#include <linux/unistd.h>
07e5b264 21#include <linux/major.h>
1894d171
TK
22#include <linux/fs.h>
23#include <scsi/sg.h>
e69d8385 24#include <asm/byteorder.h>
910f378a
BVA
25#ifdef __ANDROID__
26#include "os-ashmem.h"
27#define FIO_NO_HAVE_SHM_H
28#endif
ebac4655 29
58828d07
SW
30#ifdef ARCH_HAVE_CRC_CRYPTO
31#include <sys/auxv.h>
3721c7fe
SW
32#ifndef HWCAP_PMULL
33#define HWCAP_PMULL (1 << 4)
34#endif /* HWCAP_PMULL */
58828d07
SW
35#ifndef HWCAP_CRC32
36#define HWCAP_CRC32 (1 << 7)
37#endif /* HWCAP_CRC32 */
38#endif /* ARCH_HAVE_CRC_CRYPTO */
39
1c764dbe 40#include "./os-linux-syscall.h"
e2e58886 41#include "../file.h"
f3de88a7 42
7922a7b7
OM
43#ifndef __has_builtin // Optional of course.
44 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
45#endif
46
ebac4655
JA
47#define FIO_HAVE_CPU_AFFINITY
48#define FIO_HAVE_DISK_UTIL
49#define FIO_HAVE_SGIO
ba4f8923 50#define FIO_HAVE_IOPRIO
32ef447a 51#define FIO_HAVE_IOPRIO_CLASS
22f78b32 52#define FIO_HAVE_IOSCHED_SWITCH
2c0ecd28 53#define FIO_HAVE_ODIRECT
74b025b0 54#define FIO_HAVE_HUGETLB
5e62c22a 55#define FIO_HAVE_BLKTRACE
eb7ccf38 56#define FIO_HAVE_CL_SIZE
a696fa2a 57#define FIO_HAVE_CGROUPS
2e3bd4c2 58#define FIO_HAVE_FS_STAT
a5f3027c 59#define FIO_HAVE_TRIM
47f767c1 60#define FIO_HAVE_GETTID
93bcfd20 61#define FIO_USE_GENERIC_INIT_RANDOM_STATE
e69d8385 62#define FIO_HAVE_BYTEORDER_FUNCS
6562685f 63#define FIO_HAVE_PWRITEV2
97900ebf 64#define FIO_HAVE_SHM_ATTACH_REMOVED
40f1fc11 65#define FIO_HAVE_RWF_ATOMIC
b6959b55 66
39b93568
JA
67#ifdef MAP_HUGETLB
68#define FIO_HAVE_MMAP_HUGE
69#endif
70
dc873b6f 71#define OS_MAP_ANON MAP_ANONYMOUS
ebac4655 72
4db9ac71 73#define FIO_EXT_ENG_DIR "/usr/local/lib/fio"
5a8a6a03 74
ebac4655 75typedef cpu_set_t os_cpu_mask_t;
4e78e405 76
67bf9823 77#ifdef CONFIG_3ARG_AFFINITY
e8462bd8
JA
78#define fio_setaffinity(pid, cpumask) \
79 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
ebac4655
JA
80#define fio_getaffinity(pid, ptr) \
81 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
67bf9823 82#elif defined(CONFIG_2ARG_AFFINITY)
e8462bd8
JA
83#define fio_setaffinity(pid, cpumask) \
84 sched_setaffinity((pid), &(cpumask))
c8f025f8
JA
85#define fio_getaffinity(pid, ptr) \
86 sched_getaffinity((pid), (ptr))
87#endif
be4ecfdf 88
e9d2a04d
TK
89#ifdef CONFIG_PTHREAD_GETAFFINITY
90#define FIO_HAVE_GET_THREAD_AFFINITY
874d55e5
JA
91#define fio_get_thread_affinity(mask) \
92 pthread_getaffinity_np(pthread_self(), sizeof(mask), &(mask))
e9d2a04d 93#endif
874d55e5 94
6e0cefdc
BVA
95#define fio_cpu_clear(mask, cpu) CPU_CLR((cpu), (mask))
96#define fio_cpu_set(mask, cpu) CPU_SET((cpu), (mask))
52fd65f4 97#define fio_cpu_isset(mask, cpu) (CPU_ISSET((cpu), (mask)) != 0)
d004a209 98#define fio_cpu_count(mask) CPU_COUNT((mask))
d2ce18b5
JA
99
100static inline int fio_cpuset_init(os_cpu_mask_t *mask)
101{
102 CPU_ZERO(mask);
103 return 0;
104}
105
106static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
107{
108 return 0;
109}
6d459ee7
JA
110
111#define FIO_MAX_CPUS CPU_SETSIZE
4d8947de 112
28727df7
JA
113enum {
114 IOPRIO_CLASS_NONE,
115 IOPRIO_CLASS_RT,
116 IOPRIO_CLASS_BE,
117 IOPRIO_CLASS_IDLE,
118};
119
120enum {
121 IOPRIO_WHO_PROCESS = 1,
122 IOPRIO_WHO_PGRP,
123 IOPRIO_WHO_USER,
124};
125
126#define IOPRIO_BITS 16
127#define IOPRIO_CLASS_SHIFT 13
128
b3048c1b
DLM
129#define IOPRIO_HINT_BITS 10
130#define IOPRIO_HINT_SHIFT 3
131
1767bd34
TK
132#define IOPRIO_MIN_PRIO 0 /* highest priority */
133#define IOPRIO_MAX_PRIO 7 /* lowest priority */
134
135#define IOPRIO_MIN_PRIO_CLASS 0
136#define IOPRIO_MAX_PRIO_CLASS 3
137
b3048c1b
DLM
138#define IOPRIO_MIN_PRIO_HINT 0
139#define IOPRIO_MAX_PRIO_HINT ((1 << IOPRIO_HINT_BITS) - 1)
140
d4d503a5
DLM
141#define ioprio_class(ioprio) ((ioprio) >> IOPRIO_CLASS_SHIFT)
142#define ioprio(ioprio) ((ioprio) & IOPRIO_MAX_PRIO)
b3048c1b
DLM
143#define ioprio_hint(ioprio) \
144 (((ioprio) >> IOPRIO_HINT_SHIFT) & IOPRIO_MAX_PRIO_HINT)
d4d503a5 145
b3048c1b 146static inline int ioprio_value(int ioprio_class, int ioprio, int ioprio_hint)
ebac4655 147{
28727df7
JA
148 /*
149 * If no class is set, assume BE
150 */
8d6e8d99
DLM
151 if (!ioprio_class)
152 ioprio_class = IOPRIO_CLASS_BE;
153
b3048c1b
DLM
154 return (ioprio_class << IOPRIO_CLASS_SHIFT) |
155 (ioprio_hint << IOPRIO_HINT_SHIFT) |
156 ioprio;
8d6e8d99 157}
28727df7 158
03ec570f
DLM
159static inline bool ioprio_value_is_class_rt(unsigned int priority)
160{
d4d503a5 161 return ioprio_class(priority) == IOPRIO_CLASS_RT;
03ec570f
DLM
162}
163
b3048c1b
DLM
164static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio,
165 int ioprio_hint)
8d6e8d99
DLM
166{
167 return syscall(__NR_ioprio_set, which, who,
b3048c1b 168 ioprio_value(ioprio_class, ioprio, ioprio_hint));
ebac4655
JA
169}
170
de5ed0e4 171#ifndef CONFIG_HAVE_GETTID
47f767c1
JA
172static inline int gettid(void)
173{
174 return syscall(__NR_gettid);
175}
de5ed0e4 176#endif
47f767c1 177
3feedc60
JA
178#define SPLICE_DEF_SIZE (64*1024)
179
ebac4655
JA
180#ifndef BLKGETSIZE64
181#define BLKGETSIZE64 _IOR(0x12,114,size_t)
182#endif
183
e5b401d4
JA
184#ifndef BLKFLSBUF
185#define BLKFLSBUF _IO(0x12,97)
186#endif
187
a5f3027c
JA
188#ifndef BLKDISCARD
189#define BLKDISCARD _IO(0x12,119)
190#endif
191
e2e58886 192static inline int blockdev_invalidate_cache(struct fio_file *f)
e5b401d4 193{
ecc314ba 194 return ioctl(f->fd, BLKFLSBUF);
e5b401d4
JA
195}
196
ecc314ba 197static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
ebac4655 198{
ecc314ba 199 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
ebac4655
JA
200 return 0;
201
202 return errno;
203}
204
32cd46a0
JA
205static inline unsigned long long os_phys_mem(void)
206{
207 long pagesize, pages;
208
209 pagesize = sysconf(_SC_PAGESIZE);
210 pages = sysconf(_SC_PHYS_PAGES);
211 if (pages == -1 || pagesize == -1)
212 return 0;
213
214 return (unsigned long long) pages * (unsigned long long) pagesize;
215}
216
5b6f5c66 217#ifdef O_NOATIME
5921e80c 218#define FIO_O_NOATIME O_NOATIME
5b6f5c66
JA
219#else
220#define FIO_O_NOATIME 0
221#endif
5921e80c 222
a1c58075
JA
223#ifdef MADV_REMOVE
224#define FIO_MADV_FREE MADV_REMOVE
225#endif
226
7922a7b7
OM
227/* Check for GCC or Clang byte swap intrinsics */
228#if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
229 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
230 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
df73aab1 231#define fio_swap16(x) __builtin_bswap16(x)
df73aab1 232#define fio_swap32(x) __builtin_bswap32(x)
df73aab1
JA
233#define fio_swap64(x) __builtin_bswap64(x)
234#else
7922a7b7
OM
235#include <byteswap.h>
236#define fio_swap16(x) bswap_16(x)
237#define fio_swap32(x) bswap_32(x)
238#define fio_swap64(x) bswap_64(x)
239#endif /* fio_swapN */
ff245192 240
eb7ccf38
JA
241#define CACHE_LINE_FILE \
242 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
243
244static inline int arch_cache_line_size(void)
245{
246 char size[32];
247 int fd, ret;
248
249 fd = open(CACHE_LINE_FILE, O_RDONLY);
250 if (fd < 0)
251 return -1;
252
253 ret = read(fd, size, sizeof(size));
ec76f15a
JA
254
255 close(fd);
256
eb7ccf38
JA
257 if (ret <= 0)
258 return -1;
ec76f15a
JA
259 else
260 return atoi(size);
eb7ccf38
JA
261}
262
c08ad04c 263static inline unsigned long long get_fs_free_size(const char *path)
2e3bd4c2
JA
264{
265 unsigned long long ret;
266 struct statfs s;
267
268 if (statfs(path, &s) < 0)
269 return -1ULL;
270
271 ret = s.f_bsize;
272 ret *= (unsigned long long) s.f_bfree;
273 return ret;
274}
275
496b1f9e 276static inline int os_trim(struct fio_file *f, unsigned long long start,
a5f3027c
JA
277 unsigned long long len)
278{
279 uint64_t range[2];
280
281 range[0] = start;
282 range[1] = len;
283
496b1f9e 284 if (!ioctl(f->fd, BLKDISCARD, range))
a5f3027c
JA
285 return 0;
286
287 return errno;
288}
289
7e09a9f1 290#ifdef CONFIG_SCHED_IDLE
f2a2ce0e
HL
291static inline int fio_set_sched_idle(void)
292{
293 struct sched_param p = { .sched_priority = 0, };
294 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
295}
7e09a9f1 296#endif
f2a2ce0e 297
ae8e559e
JA
298#ifndef F_GET_RW_HINT
299#ifndef F_LINUX_SPECIFIC_BASE
300#define F_LINUX_SPECIFIC_BASE 1024
301#endif
302#define F_GET_RW_HINT (F_LINUX_SPECIFIC_BASE + 11)
303#define F_SET_RW_HINT (F_LINUX_SPECIFIC_BASE + 12)
bd553af6
JA
304#define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)
305#define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14)
ae8e559e
JA
306#endif
307
308#ifndef RWH_WRITE_LIFE_NONE
bd553af6
JA
309#define RWH_WRITE_LIFE_NOT_SET 0
310#define RWH_WRITE_LIFE_NONE 1
311#define RWH_WRITE_LIFE_SHORT 2
312#define RWH_WRITE_LIFE_MEDIUM 3
313#define RWH_WRITE_LIFE_LONG 4
314#define RWH_WRITE_LIFE_EXTREME 5
37659335
JA
315#endif
316
ae8e559e 317#define FIO_HAVE_WRITE_HINT
37659335 318
6562685f
JA
319#ifndef RWF_HIPRI
320#define RWF_HIPRI 0x00000001
321#endif
322#ifndef RWF_DSYNC
323#define RWF_DSYNC 0x00000002
324#endif
325#ifndef RWF_SYNC
326#define RWF_SYNC 0x00000004
327#endif
7d42e66e
KK
328#ifndef RWF_NOWAIT
329#define RWF_NOWAIT 0x00000008
330#endif
6562685f 331
5ba34480
JG
332#ifndef RWF_ATOMIC
333#define RWF_ATOMIC 0x00000040
334#endif
335
72cd4146
JA
336#ifndef RWF_WRITE_LIFE_SHIFT
337#define RWF_WRITE_LIFE_SHIFT 4
338#define RWF_WRITE_LIFE_SHORT (1 << RWF_WRITE_LIFE_SHIFT)
339#define RWF_WRITE_LIFE_MEDIUM (2 << RWF_WRITE_LIFE_SHIFT)
340#define RWF_WRITE_LIFE_LONG (3 << RWF_WRITE_LIFE_SHIFT)
341#define RWF_WRITE_LIFE_EXTREME (4 << RWF_WRITE_LIFE_SHIFT)
342#endif
343
6562685f
JA
344#ifndef CONFIG_PWRITEV2
345#ifdef __NR_preadv2
346static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
347 off_t offset)
348{
7b5c648f 349#if BITS_PER_LONG == 64
7b5c648f
JA
350 *pos_l = offset;
351 *pos_h = 0;
352#else
6562685f
JA
353 *pos_l = offset & 0xffffffff;
354 *pos_h = ((uint64_t) offset) >> 32;
7b5c648f 355#endif
6562685f
JA
356}
357static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
358 off_t offset, unsigned int flags)
359{
360 unsigned long pos_l, pos_h;
361
362 make_pos_h_l(&pos_h, &pos_l, offset);
363 return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
364}
365static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
366 off_t offset, unsigned int flags)
367{
368 unsigned long pos_l, pos_h;
369
370 make_pos_h_l(&pos_h, &pos_l, offset);
371 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
372}
373#else
374static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
375 off_t offset, unsigned int flags)
376{
377 errno = ENOSYS;
378 return -1;
379}
380static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
381 off_t offset, unsigned int flags)
382{
383 errno = ENOSYS;
384 return -1;
385}
386#endif /* __NR_preadv2 */
387#endif /* CONFIG_PWRITEV2 */
388
0cfe2089
TK
389static inline int shm_attach_to_open_removed(void)
390{
391 return 1;
392}
393
2c3e17be
SW
394#ifdef CONFIG_LINUX_FALLOCATE
395#define FIO_HAVE_NATIVE_FALLOCATE
396static inline bool fio_fallocate(struct fio_file *f, uint64_t offset,
397 uint64_t len)
398{
399 int ret;
e69dfc2b 400 ret = fallocate(f->fd, 0, offset, len);
2c3e17be
SW
401 if (ret == 0)
402 return true;
403
404 /* Work around buggy old glibc versions... */
405 if (ret > 0)
406 errno = ret;
407
408 return false;
409}
410#endif
411
58828d07
SW
412#define FIO_HAVE_CPU_HAS
413static inline bool os_cpu_has(cpu_features feature)
414{
415 bool have_feature;
416 unsigned long fio_unused hwcap;
417
418 switch (feature) {
419#ifdef ARCH_HAVE_CRC_CRYPTO
420 case CPU_ARM64_CRC32C:
421 hwcap = getauxval(AT_HWCAP);
3721c7fe
SW
422 have_feature = (hwcap & (HWCAP_PMULL | HWCAP_CRC32)) ==
423 (HWCAP_PMULL | HWCAP_CRC32);
58828d07
SW
424 break;
425#endif
426 default:
427 have_feature = false;
428 }
429
430 return have_feature;
431}
432
ebac4655 433#endif