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