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