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