glusterfs: update for new API
[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
JA
60
61typedef cpu_set_t os_cpu_mask_t;
4e78e405 62
67bf9823 63#ifdef CONFIG_3ARG_AFFINITY
e8462bd8
JA
64#define fio_setaffinity(pid, cpumask) \
65 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
ebac4655
JA
66#define fio_getaffinity(pid, ptr) \
67 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
67bf9823 68#elif defined(CONFIG_2ARG_AFFINITY)
e8462bd8
JA
69#define fio_setaffinity(pid, cpumask) \
70 sched_setaffinity((pid), &(cpumask))
c8f025f8
JA
71#define fio_getaffinity(pid, ptr) \
72 sched_getaffinity((pid), (ptr))
73#endif
be4ecfdf 74
1f4c0a4f
JA
75#define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
76#define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
52fd65f4 77#define fio_cpu_isset(mask, cpu) (CPU_ISSET((cpu), (mask)) != 0)
d004a209 78#define fio_cpu_count(mask) CPU_COUNT((mask))
d2ce18b5
JA
79
80static inline int fio_cpuset_init(os_cpu_mask_t *mask)
81{
82 CPU_ZERO(mask);
83 return 0;
84}
85
86static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
87{
88 return 0;
89}
6d459ee7
JA
90
91#define FIO_MAX_CPUS CPU_SETSIZE
4d8947de 92
28727df7
JA
93enum {
94 IOPRIO_CLASS_NONE,
95 IOPRIO_CLASS_RT,
96 IOPRIO_CLASS_BE,
97 IOPRIO_CLASS_IDLE,
98};
99
100enum {
101 IOPRIO_WHO_PROCESS = 1,
102 IOPRIO_WHO_PGRP,
103 IOPRIO_WHO_USER,
104};
105
106#define IOPRIO_BITS 16
107#define IOPRIO_CLASS_SHIFT 13
108
1767bd34
TK
109#define IOPRIO_MIN_PRIO 0 /* highest priority */
110#define IOPRIO_MAX_PRIO 7 /* lowest priority */
111
112#define IOPRIO_MIN_PRIO_CLASS 0
113#define IOPRIO_MAX_PRIO_CLASS 3
114
28727df7 115static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
ebac4655 116{
28727df7
JA
117 /*
118 * If no class is set, assume BE
119 */
120 if (!ioprio_class)
121 ioprio_class = IOPRIO_CLASS_BE;
122
123 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
ebac4655
JA
124 return syscall(__NR_ioprio_set, which, who, ioprio);
125}
126
de5ed0e4 127#ifndef CONFIG_HAVE_GETTID
47f767c1
JA
128static inline int gettid(void)
129{
130 return syscall(__NR_gettid);
131}
de5ed0e4 132#endif
47f767c1 133
3feedc60
JA
134#define SPLICE_DEF_SIZE (64*1024)
135
ebac4655
JA
136#ifndef BLKGETSIZE64
137#define BLKGETSIZE64 _IOR(0x12,114,size_t)
138#endif
139
e5b401d4
JA
140#ifndef BLKFLSBUF
141#define BLKFLSBUF _IO(0x12,97)
142#endif
143
a5f3027c
JA
144#ifndef BLKDISCARD
145#define BLKDISCARD _IO(0x12,119)
146#endif
147
e2e58886 148static inline int blockdev_invalidate_cache(struct fio_file *f)
e5b401d4 149{
ecc314ba 150 return ioctl(f->fd, BLKFLSBUF);
e5b401d4
JA
151}
152
ecc314ba 153static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
ebac4655 154{
ecc314ba 155 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
ebac4655
JA
156 return 0;
157
158 return errno;
159}
160
32cd46a0
JA
161static inline unsigned long long os_phys_mem(void)
162{
163 long pagesize, pages;
164
165 pagesize = sysconf(_SC_PAGESIZE);
166 pages = sysconf(_SC_PHYS_PAGES);
167 if (pages == -1 || pagesize == -1)
168 return 0;
169
170 return (unsigned long long) pages * (unsigned long long) pagesize;
171}
172
8cc7afa9 173static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
07e5b264
JA
174{
175 struct raw_config_request rq;
176 int fd;
177
178 if (major(dev) != RAW_MAJOR)
8cc7afa9 179 return 1;
07e5b264
JA
180
181 /*
182 * we should be able to find /dev/rawctl or /dev/raw/rawctl
183 */
184 fd = open("/dev/rawctl", O_RDONLY);
185 if (fd < 0) {
186 fd = open("/dev/raw/rawctl", O_RDONLY);
187 if (fd < 0)
8cc7afa9 188 return 1;
07e5b264
JA
189 }
190
191 rq.raw_minor = minor(dev);
192 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
193 close(fd);
8cc7afa9 194 return 1;
07e5b264
JA
195 }
196
197 close(fd);
198 *majdev = rq.block_major;
199 *mindev = rq.block_minor;
8cc7afa9 200 return 0;
07e5b264
JA
201}
202
5b6f5c66 203#ifdef O_NOATIME
5921e80c 204#define FIO_O_NOATIME O_NOATIME
5b6f5c66
JA
205#else
206#define FIO_O_NOATIME 0
207#endif
5921e80c 208
d01612f3
CM
209#ifdef O_ATOMIC
210#define OS_O_ATOMIC O_ATOMIC
211#else
212#define OS_O_ATOMIC 040000000
213#endif
214
a1c58075
JA
215#ifdef MADV_REMOVE
216#define FIO_MADV_FREE MADV_REMOVE
217#endif
218
7922a7b7
OM
219/* Check for GCC or Clang byte swap intrinsics */
220#if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
221 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
222 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
df73aab1 223#define fio_swap16(x) __builtin_bswap16(x)
df73aab1 224#define fio_swap32(x) __builtin_bswap32(x)
df73aab1
JA
225#define fio_swap64(x) __builtin_bswap64(x)
226#else
7922a7b7
OM
227#include <byteswap.h>
228#define fio_swap16(x) bswap_16(x)
229#define fio_swap32(x) bswap_32(x)
230#define fio_swap64(x) bswap_64(x)
231#endif /* fio_swapN */
ff245192 232
eb7ccf38
JA
233#define CACHE_LINE_FILE \
234 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
235
236static inline int arch_cache_line_size(void)
237{
238 char size[32];
239 int fd, ret;
240
241 fd = open(CACHE_LINE_FILE, O_RDONLY);
242 if (fd < 0)
243 return -1;
244
245 ret = read(fd, size, sizeof(size));
ec76f15a
JA
246
247 close(fd);
248
eb7ccf38
JA
249 if (ret <= 0)
250 return -1;
ec76f15a
JA
251 else
252 return atoi(size);
eb7ccf38
JA
253}
254
d1419c4f
TK
255#ifdef __powerpc64__
256#define FIO_HAVE_CPU_ONLINE_SYSCONF
257static inline unsigned int cpus_online(void)
258{
259 return sysconf(_SC_NPROCESSORS_CONF);
260}
261#endif
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
328
72cd4146
JA
329#ifndef RWF_WRITE_LIFE_SHIFT
330#define RWF_WRITE_LIFE_SHIFT 4
331#define RWF_WRITE_LIFE_SHORT (1 << RWF_WRITE_LIFE_SHIFT)
332#define RWF_WRITE_LIFE_MEDIUM (2 << RWF_WRITE_LIFE_SHIFT)
333#define RWF_WRITE_LIFE_LONG (3 << RWF_WRITE_LIFE_SHIFT)
334#define RWF_WRITE_LIFE_EXTREME (4 << RWF_WRITE_LIFE_SHIFT)
335#endif
336
6562685f
JA
337#ifndef CONFIG_PWRITEV2
338#ifdef __NR_preadv2
339static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l,
340 off_t offset)
341{
7b5c648f 342#if BITS_PER_LONG == 64
7b5c648f
JA
343 *pos_l = offset;
344 *pos_h = 0;
345#else
6562685f
JA
346 *pos_l = offset & 0xffffffff;
347 *pos_h = ((uint64_t) offset) >> 32;
7b5c648f 348#endif
6562685f
JA
349}
350static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
351 off_t offset, unsigned int flags)
352{
353 unsigned long pos_l, pos_h;
354
355 make_pos_h_l(&pos_h, &pos_l, offset);
356 return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags);
357}
358static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
359 off_t offset, unsigned int flags)
360{
361 unsigned long pos_l, pos_h;
362
363 make_pos_h_l(&pos_h, &pos_l, offset);
364 return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags);
365}
366#else
367static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt,
368 off_t offset, unsigned int flags)
369{
370 errno = ENOSYS;
371 return -1;
372}
373static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt,
374 off_t offset, unsigned int flags)
375{
376 errno = ENOSYS;
377 return -1;
378}
379#endif /* __NR_preadv2 */
380#endif /* CONFIG_PWRITEV2 */
381
0cfe2089
TK
382static inline int shm_attach_to_open_removed(void)
383{
384 return 1;
385}
386
2c3e17be
SW
387#ifdef CONFIG_LINUX_FALLOCATE
388#define FIO_HAVE_NATIVE_FALLOCATE
389static inline bool fio_fallocate(struct fio_file *f, uint64_t offset,
390 uint64_t len)
391{
392 int ret;
e69dfc2b 393 ret = fallocate(f->fd, 0, offset, len);
2c3e17be
SW
394 if (ret == 0)
395 return true;
396
397 /* Work around buggy old glibc versions... */
398 if (ret > 0)
399 errno = ret;
400
401 return false;
402}
403#endif
404
58828d07
SW
405#define FIO_HAVE_CPU_HAS
406static inline bool os_cpu_has(cpu_features feature)
407{
408 bool have_feature;
409 unsigned long fio_unused hwcap;
410
411 switch (feature) {
412#ifdef ARCH_HAVE_CRC_CRYPTO
413 case CPU_ARM64_CRC32C:
414 hwcap = getauxval(AT_HWCAP);
415 have_feature = (hwcap & HWCAP_CRC32) != 0;
416 break;
417#endif
418 default:
419 have_feature = false;
420 }
421
422 return have_feature;
423}
424
ebac4655 425#endif