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