engines/exec: Code cleanup to remove leaks
[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
e9d2a04d
TK
77#ifdef CONFIG_PTHREAD_GETAFFINITY
78#define FIO_HAVE_GET_THREAD_AFFINITY
874d55e5
JA
79#define fio_get_thread_affinity(mask) \
80 pthread_getaffinity_np(pthread_self(), sizeof(mask), &(mask))
e9d2a04d 81#endif
874d55e5 82
1f4c0a4f
JA
83#define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
84#define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
52fd65f4 85#define fio_cpu_isset(mask, cpu) (CPU_ISSET((cpu), (mask)) != 0)
d004a209 86#define fio_cpu_count(mask) CPU_COUNT((mask))
d2ce18b5
JA
87
88static inline int fio_cpuset_init(os_cpu_mask_t *mask)
89{
90 CPU_ZERO(mask);
91 return 0;
92}
93
94static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
95{
96 return 0;
97}
6d459ee7
JA
98
99#define FIO_MAX_CPUS CPU_SETSIZE
4d8947de 100
28727df7
JA
101enum {
102 IOPRIO_CLASS_NONE,
103 IOPRIO_CLASS_RT,
104 IOPRIO_CLASS_BE,
105 IOPRIO_CLASS_IDLE,
106};
107
108enum {
109 IOPRIO_WHO_PROCESS = 1,
110 IOPRIO_WHO_PGRP,
111 IOPRIO_WHO_USER,
112};
113
114#define IOPRIO_BITS 16
115#define IOPRIO_CLASS_SHIFT 13
116
1767bd34
TK
117#define IOPRIO_MIN_PRIO 0 /* highest priority */
118#define IOPRIO_MAX_PRIO 7 /* lowest priority */
119
120#define IOPRIO_MIN_PRIO_CLASS 0
121#define IOPRIO_MAX_PRIO_CLASS 3
122
28727df7 123static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
ebac4655 124{
28727df7
JA
125 /*
126 * If no class is set, assume BE
127 */
128 if (!ioprio_class)
129 ioprio_class = IOPRIO_CLASS_BE;
130
131 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
ebac4655
JA
132 return syscall(__NR_ioprio_set, which, who, ioprio);
133}
134
de5ed0e4 135#ifndef CONFIG_HAVE_GETTID
47f767c1
JA
136static inline int gettid(void)
137{
138 return syscall(__NR_gettid);
139}
de5ed0e4 140#endif
47f767c1 141
3feedc60
JA
142#define SPLICE_DEF_SIZE (64*1024)
143
ebac4655
JA
144#ifndef BLKGETSIZE64
145#define BLKGETSIZE64 _IOR(0x12,114,size_t)
146#endif
147
e5b401d4
JA
148#ifndef BLKFLSBUF
149#define BLKFLSBUF _IO(0x12,97)
150#endif
151
a5f3027c
JA
152#ifndef BLKDISCARD
153#define BLKDISCARD _IO(0x12,119)
154#endif
155
e2e58886 156static inline int blockdev_invalidate_cache(struct fio_file *f)
e5b401d4 157{
ecc314ba 158 return ioctl(f->fd, BLKFLSBUF);
e5b401d4
JA
159}
160
ecc314ba 161static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
ebac4655 162{
ecc314ba 163 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
ebac4655
JA
164 return 0;
165
166 return errno;
167}
168
32cd46a0
JA
169static inline unsigned long long os_phys_mem(void)
170{
171 long pagesize, pages;
172
173 pagesize = sysconf(_SC_PAGESIZE);
174 pages = sysconf(_SC_PHYS_PAGES);
175 if (pages == -1 || pagesize == -1)
176 return 0;
177
178 return (unsigned long long) pages * (unsigned long long) pagesize;
179}
180
8cc7afa9 181static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
07e5b264
JA
182{
183 struct raw_config_request rq;
184 int fd;
185
186 if (major(dev) != RAW_MAJOR)
8cc7afa9 187 return 1;
07e5b264
JA
188
189 /*
190 * we should be able to find /dev/rawctl or /dev/raw/rawctl
191 */
192 fd = open("/dev/rawctl", O_RDONLY);
193 if (fd < 0) {
194 fd = open("/dev/raw/rawctl", O_RDONLY);
195 if (fd < 0)
8cc7afa9 196 return 1;
07e5b264
JA
197 }
198
199 rq.raw_minor = minor(dev);
200 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
201 close(fd);
8cc7afa9 202 return 1;
07e5b264
JA
203 }
204
205 close(fd);
206 *majdev = rq.block_major;
207 *mindev = rq.block_minor;
8cc7afa9 208 return 0;
07e5b264
JA
209}
210
5b6f5c66 211#ifdef O_NOATIME
5921e80c 212#define FIO_O_NOATIME O_NOATIME
5b6f5c66
JA
213#else
214#define FIO_O_NOATIME 0
215#endif
5921e80c 216
d01612f3
CM
217#ifdef O_ATOMIC
218#define OS_O_ATOMIC O_ATOMIC
219#else
220#define OS_O_ATOMIC 040000000
221#endif
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
d1419c4f
TK
263#ifdef __powerpc64__
264#define FIO_HAVE_CPU_ONLINE_SYSCONF
265static inline unsigned int cpus_online(void)
266{
267 return sysconf(_SC_NPROCESSORS_CONF);
268}
269#endif
270
c08ad04c 271static inline unsigned long long get_fs_free_size(const char *path)
2e3bd4c2
JA
272{
273 unsigned long long ret;
274 struct statfs s;
275
276 if (statfs(path, &s) < 0)
277 return -1ULL;
278
279 ret = s.f_bsize;
280 ret *= (unsigned long long) s.f_bfree;
281 return ret;
282}
283
496b1f9e 284static inline int os_trim(struct fio_file *f, unsigned long long start,
a5f3027c
JA
285 unsigned long long len)
286{
287 uint64_t range[2];
288
289 range[0] = start;
290 range[1] = len;
291
496b1f9e 292 if (!ioctl(f->fd, BLKDISCARD, range))
a5f3027c
JA
293 return 0;
294
295 return errno;
296}
297
7e09a9f1 298#ifdef CONFIG_SCHED_IDLE
f2a2ce0e
HL
299static inline int fio_set_sched_idle(void)
300{
301 struct sched_param p = { .sched_priority = 0, };
302 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
303}
7e09a9f1 304#endif
f2a2ce0e 305
ae8e559e
JA
306#ifndef F_GET_RW_HINT
307#ifndef F_LINUX_SPECIFIC_BASE
308#define F_LINUX_SPECIFIC_BASE 1024
309#endif
310#define F_GET_RW_HINT (F_LINUX_SPECIFIC_BASE + 11)
311#define F_SET_RW_HINT (F_LINUX_SPECIFIC_BASE + 12)
bd553af6
JA
312#define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13)
313#define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14)
ae8e559e
JA
314#endif
315
316#ifndef RWH_WRITE_LIFE_NONE
bd553af6
JA
317#define RWH_WRITE_LIFE_NOT_SET 0
318#define RWH_WRITE_LIFE_NONE 1
319#define RWH_WRITE_LIFE_SHORT 2
320#define RWH_WRITE_LIFE_MEDIUM 3
321#define RWH_WRITE_LIFE_LONG 4
322#define RWH_WRITE_LIFE_EXTREME 5
37659335
JA
323#endif
324
ae8e559e 325#define FIO_HAVE_WRITE_HINT
37659335 326
6562685f
JA
327#ifndef RWF_HIPRI
328#define RWF_HIPRI 0x00000001
329#endif
330#ifndef RWF_DSYNC
331#define RWF_DSYNC 0x00000002
332#endif
333#ifndef RWF_SYNC
334#define RWF_SYNC 0x00000004
335#endif
7d42e66e
KK
336#ifndef RWF_NOWAIT
337#define RWF_NOWAIT 0x00000008
338#endif
6562685f 339
4a87b584 340#ifndef RWF_UNCACHED
8beee62d 341#define RWF_UNCACHED 0x00000040
4a87b584
JA
342#endif
343
72cd4146
JA
344#ifndef RWF_WRITE_LIFE_SHIFT
345#define RWF_WRITE_LIFE_SHIFT 4
346#define RWF_WRITE_LIFE_SHORT (1 << RWF_WRITE_LIFE_SHIFT)
347#define RWF_WRITE_LIFE_MEDIUM (2 << RWF_WRITE_LIFE_SHIFT)
348#define RWF_WRITE_LIFE_LONG (3 << RWF_WRITE_LIFE_SHIFT)
349#define RWF_WRITE_LIFE_EXTREME (4 << RWF_WRITE_LIFE_SHIFT)
350#endif
351
6562685f
JA
352#ifndef CONFIG_PWRITEV2
353#ifdef __NR_preadv2
354static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
355 off_t offset)
356{
7b5c648f 357#if BITS_PER_LONG == 64
7b5c648f
JA
358 *pos_l = offset;
359 *pos_h = 0;
360#else
6562685f
JA
361 *pos_l = offset & 0xffffffff;
362 *pos_h = ((uint64_t) offset) >> 32;
7b5c648f 363#endif
6562685f
JA
364}
365static inline ssize_t preadv2(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_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
372}
373static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
374 off_t offset, unsigned int flags)
375{
376 unsigned long pos_l, pos_h;
377
378 make_pos_h_l(&pos_h, &pos_l, offset);
379 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
380}
381#else
382static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
383 off_t offset, unsigned int flags)
384{
385 errno = ENOSYS;
386 return -1;
387}
388static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
389 off_t offset, unsigned int flags)
390{
391 errno = ENOSYS;
392 return -1;
393}
394#endif /* __NR_preadv2 */
395#endif /* CONFIG_PWRITEV2 */
396
0cfe2089
TK
397static inline int shm_attach_to_open_removed(void)
398{
399 return 1;
400}
401
2c3e17be
SW
402#ifdef CONFIG_LINUX_FALLOCATE
403#define FIO_HAVE_NATIVE_FALLOCATE
404static inline bool fio_fallocate(struct fio_file *f, uint64_t offset,
405 uint64_t len)
406{
407 int ret;
e69dfc2b 408 ret = fallocate(f->fd, 0, offset, len);
2c3e17be
SW
409 if (ret == 0)
410 return true;
411
412 /* Work around buggy old glibc versions... */
413 if (ret > 0)
414 errno = ret;
415
416 return false;
417}
418#endif
419
58828d07
SW
420#define FIO_HAVE_CPU_HAS
421static inline bool os_cpu_has(cpu_features feature)
422{
423 bool have_feature;
424 unsigned long fio_unused hwcap;
425
426 switch (feature) {
427#ifdef ARCH_HAVE_CRC_CRYPTO
428 case CPU_ARM64_CRC32C:
429 hwcap = getauxval(AT_HWCAP);
430 have_feature = (hwcap & HWCAP_CRC32) != 0;
431 break;
432#endif
433 default:
434 have_feature = false;
435 }
436
437 return have_feature;
438}
439
ebac4655 440#endif