os/os-android.h: fix compilation for Android O
[fio.git] / os / os-android.h
CommitLineData
ec5c6b12
AC
1#ifndef FIO_OS_ANDROID_H
2#define FIO_OS_ANDROID_H
3
4#define FIO_OS os_android
5
6#include <sys/ioctl.h>
93eeb558 7#include <sys/mman.h>
ec5c6b12
AC
8#include <sys/uio.h>
9#include <sys/syscall.h>
10#include <sys/vfs.h>
11#include <unistd.h>
12#include <fcntl.h>
13#include <errno.h>
14#include <sched.h>
15#include <linux/unistd.h>
16#include <linux/major.h>
da52c582 17#include <asm/byteorder.h>
ec5c6b12 18
eec97b93 19#include "./os-linux-syscall.h"
ec5c6b12
AC
20#include "binject.h"
21#include "../file.h"
22
0fa5c986
OM
23#ifndef __has_builtin // Optional of course.
24 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
25#endif
26
ec5c6b12 27#define FIO_HAVE_DISK_UTIL
ec5c6b12 28#define FIO_HAVE_IOSCHED_SWITCH
177380ab 29#define FIO_HAVE_IOPRIO
32ef447a 30#define FIO_HAVE_IOPRIO_CLASS
ec5c6b12
AC
31#define FIO_HAVE_ODIRECT
32#define FIO_HAVE_HUGETLB
33#define FIO_HAVE_BLKTRACE
ec5c6b12 34#define FIO_HAVE_CL_SIZE
ec5c6b12
AC
35#define FIO_HAVE_FS_STAT
36#define FIO_HAVE_TRIM
ec5c6b12
AC
37#define FIO_HAVE_GETTID
38#define FIO_USE_GENERIC_INIT_RANDOM_STATE
39#define FIO_HAVE_E4_ENG
40#define FIO_HAVE_BYTEORDER_FUNCS
6d0e9f83 41#define FIO_HAVE_MMAP_HUGE
a5e0ee11 42#define FIO_NO_HAVE_SHM_H
ec5c6b12
AC
43
44#define OS_MAP_ANON MAP_ANONYMOUS
45
93eeb558 46#ifndef POSIX_MADV_DONTNEED
ec5c6b12
AC
47#define posix_madvise madvise
48#define POSIX_MADV_DONTNEED MADV_DONTNEED
49#define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
50#define POSIX_MADV_RANDOM MADV_RANDOM
93eeb558 51#endif
52
ec5c6b12
AC
53#ifdef MADV_REMOVE
54#define FIO_MADV_FREE MADV_REMOVE
55#endif
a5e0ee11
O
56#ifndef MAP_HUGETLB
57#define MAP_HUGETLB 0x40000 /* arch specific */
58#endif
ec5c6b12 59
6ca9b06d 60#ifndef CONFIG_NO_SHM
ec5c6b12 61/*
68bf62a0 62 * Bionic doesn't support SysV shared memeory, so implement it using ashmem
ec5c6b12 63 */
239a11de
AJ
64#include <stdio.h>
65#include <linux/ashmem.h>
68bf62a0
OM
66#include <linux/shm.h>
67#define shmid_ds shmid64_ds
68#define SHM_HUGETLB 04000
239a11de
AJ
69
70#define ASHMEM_DEVICE "/dev/ashmem"
71
68bf62a0 72static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
ec5c6b12 73{
239a11de
AJ
74 int ret=0;
75 if (__cmd == IPC_RMID)
76 {
77 int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
78 struct ashmem_pin pin = {0 , length};
79 ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
80 close(__shmid);
81 }
82 return ret;
ec5c6b12
AC
83}
84
68bf62a0 85static inline int shmget(key_t __key, size_t __size, int __shmflg)
ec5c6b12 86{
239a11de 87 int fd,ret;
91f64346
OM
88 char keybuf[11];
89
239a11de
AJ
90 fd = open(ASHMEM_DEVICE, O_RDWR);
91 if (fd < 0)
92 return fd;
93
91f64346
OM
94 sprintf(keybuf,"%d",__key);
95 ret = ioctl(fd, ASHMEM_SET_NAME, keybuf);
239a11de
AJ
96 if (ret < 0)
97 goto error;
98
99 ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
100 if (ret < 0)
101 goto error;
102
103 return fd;
91f64346 104
239a11de
AJ
105error:
106 close(fd);
107 return ret;
ec5c6b12
AC
108}
109
68bf62a0 110static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
ec5c6b12 111{
239a11de
AJ
112 size_t *ptr, size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
113 ptr = mmap(NULL, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
114 *ptr = size; //save size at beginning of buffer, for use with munmap
115 return &ptr[1];
ec5c6b12
AC
116}
117
118static inline int shmdt (const void *__shmaddr)
119{
239a11de
AJ
120 size_t *ptr, size;
121 ptr = (size_t *)__shmaddr;
122 ptr--;
123 size = *ptr; //find mmap size which we stored at the beginning of the buffer
124 return munmap((void *)ptr, size + sizeof(size_t));
ec5c6b12 125}
6ca9b06d 126#endif
ec5c6b12 127
ec5c6b12
AC
128#define SPLICE_DEF_SIZE (64*1024)
129
177380ab
AC
130enum {
131 IOPRIO_CLASS_NONE,
132 IOPRIO_CLASS_RT,
133 IOPRIO_CLASS_BE,
134 IOPRIO_CLASS_IDLE,
135};
136
137enum {
138 IOPRIO_WHO_PROCESS = 1,
139 IOPRIO_WHO_PGRP,
140 IOPRIO_WHO_USER,
141};
142
143#define IOPRIO_BITS 16
144#define IOPRIO_CLASS_SHIFT 13
145
1767bd34
TK
146#define IOPRIO_MIN_PRIO 0 /* highest priority */
147#define IOPRIO_MAX_PRIO 7 /* lowest priority */
148
149#define IOPRIO_MIN_PRIO_CLASS 0
150#define IOPRIO_MAX_PRIO_CLASS 3
151
a415b2cc
AC
152static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
153{
154 /*
155 * If no class is set, assume BE
156 */
157 if (!ioprio_class)
158 ioprio_class = IOPRIO_CLASS_BE;
159
160 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
161 return syscall(__NR_ioprio_set, which, who, ioprio);
162}
163
ec5c6b12
AC
164#ifndef BLKGETSIZE64
165#define BLKGETSIZE64 _IOR(0x12,114,size_t)
166#endif
167
168#ifndef BLKFLSBUF
169#define BLKFLSBUF _IO(0x12,97)
170#endif
171
172#ifndef BLKDISCARD
173#define BLKDISCARD _IO(0x12,119)
174#endif
175
176static inline int blockdev_invalidate_cache(struct fio_file *f)
177{
178 return ioctl(f->fd, BLKFLSBUF);
179}
180
181static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
182{
183 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
184 return 0;
185
186 return errno;
187}
188
189static inline unsigned long long os_phys_mem(void)
190{
191 long pagesize, pages;
192
193 pagesize = sysconf(_SC_PAGESIZE);
194 pages = sysconf(_SC_PHYS_PAGES);
195 if (pages == -1 || pagesize == -1)
196 return 0;
197
198 return (unsigned long long) pages * (unsigned long long) pagesize;
199}
200
201typedef struct { unsigned short r[3]; } os_random_state_t;
202
203static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
204{
205 rs->r[0] = seed & 0xffff;
206 seed >>= 16;
207 rs->r[1] = seed & 0xffff;
208 seed >>= 16;
209 rs->r[2] = seed & 0xffff;
210 seed48(rs->r);
211}
212
213static inline long os_random_long(os_random_state_t *rs)
214{
215 return nrand48(rs->r);
216}
217
218#ifdef O_NOATIME
219#define FIO_O_NOATIME O_NOATIME
220#else
221#define FIO_O_NOATIME 0
222#endif
223
0fa5c986
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 */
228#define fio_swap16(x) __builtin_bswap16(x)
229#define fio_swap32(x) __builtin_bswap32(x)
230#define fio_swap64(x) __builtin_bswap64(x)
231#else
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 */
ec5c6b12
AC
237
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));
251
252 close(fd);
253
254 if (ret <= 0)
255 return -1;
256 else
257 return atoi(size);
258}
259
c08ad04c 260static inline unsigned long long get_fs_free_size(const char *path)
ec5c6b12
AC
261{
262 unsigned long long ret;
263 struct statfs s;
264
265 if (statfs(path, &s) < 0)
266 return -1ULL;
267
268 ret = s.f_bsize;
269 ret *= (unsigned long long) s.f_bfree;
270 return ret;
271}
272
273static inline int os_trim(int fd, unsigned long long start,
274 unsigned long long len)
275{
276 uint64_t range[2];
277
278 range[0] = start;
279 range[1] = len;
280
281 if (!ioctl(fd, BLKDISCARD, range))
282 return 0;
283
284 return errno;
285}
286
93eeb558 287#ifdef CONFIG_SCHED_IDLE
288static inline int fio_set_sched_idle(void)
289{
290 struct sched_param p = { .sched_priority = 0, };
291 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
292}
293#endif
294
ec5c6b12 295#endif