Merge branch 'alignment' of https://github.com/omor1/fio
[fio.git] / os / os-android.h
... / ...
CommitLineData
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 * Bionic doesn't support SysV shared memeory, so implement it using ashmem
63 */
64#include <stdio.h>
65#include <linux/ashmem.h>
66#include <linux/shm.h>
67#define shmid_ds shmid64_ds
68#define SHM_HUGETLB 04000
69
70#define ASHMEM_DEVICE "/dev/ashmem"
71
72static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
73{
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;
83}
84
85static inline int shmget(key_t __key, size_t __size, int __shmflg)
86{
87 int fd,ret;
88 char keybuf[11];
89
90 fd = open(ASHMEM_DEVICE, O_RDWR);
91 if (fd < 0)
92 return fd;
93
94 sprintf(keybuf,"%d",__key);
95 ret = ioctl(fd, ASHMEM_SET_NAME, keybuf);
96 if (ret < 0)
97 goto error;
98
99 /* Stores size in first 8 bytes, allocate extra space */
100 ret = ioctl(fd, ASHMEM_SET_SIZE, __size + sizeof(uint64_t));
101 if (ret < 0)
102 goto error;
103
104 return fd;
105
106error:
107 close(fd);
108 return ret;
109}
110
111static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
112{
113 size_t size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
114 /* Needs to be 8-byte aligned to prevent SIGBUS on 32-bit ARM */
115 uint64_t *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
116 /* Save size at beginning of buffer, for use with munmap */
117 *ptr = size;
118 return ptr + 1;
119}
120
121static inline int shmdt (const void *__shmaddr)
122{
123 /* Find mmap size which we stored at the beginning of the buffer */
124 uint64_t *ptr = (uint64_t *)__shmaddr - 1;
125 size_t size = *ptr;
126 return munmap(ptr, size);
127}
128#endif
129
130#define SPLICE_DEF_SIZE (64*1024)
131
132enum {
133 IOPRIO_CLASS_NONE,
134 IOPRIO_CLASS_RT,
135 IOPRIO_CLASS_BE,
136 IOPRIO_CLASS_IDLE,
137};
138
139enum {
140 IOPRIO_WHO_PROCESS = 1,
141 IOPRIO_WHO_PGRP,
142 IOPRIO_WHO_USER,
143};
144
145#define IOPRIO_BITS 16
146#define IOPRIO_CLASS_SHIFT 13
147
148#define IOPRIO_MIN_PRIO 0 /* highest priority */
149#define IOPRIO_MAX_PRIO 7 /* lowest priority */
150
151#define IOPRIO_MIN_PRIO_CLASS 0
152#define IOPRIO_MAX_PRIO_CLASS 3
153
154static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
155{
156 /*
157 * If no class is set, assume BE
158 */
159 if (!ioprio_class)
160 ioprio_class = IOPRIO_CLASS_BE;
161
162 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
163 return syscall(__NR_ioprio_set, which, who, ioprio);
164}
165
166#ifndef BLKGETSIZE64
167#define BLKGETSIZE64 _IOR(0x12,114,size_t)
168#endif
169
170#ifndef BLKFLSBUF
171#define BLKFLSBUF _IO(0x12,97)
172#endif
173
174#ifndef BLKDISCARD
175#define BLKDISCARD _IO(0x12,119)
176#endif
177
178static inline int blockdev_invalidate_cache(struct fio_file *f)
179{
180 return ioctl(f->fd, BLKFLSBUF);
181}
182
183static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
184{
185 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
186 return 0;
187
188 return errno;
189}
190
191static inline unsigned long long os_phys_mem(void)
192{
193 long pagesize, pages;
194
195 pagesize = sysconf(_SC_PAGESIZE);
196 pages = sysconf(_SC_PHYS_PAGES);
197 if (pages == -1 || pagesize == -1)
198 return 0;
199
200 return (unsigned long long) pages * (unsigned long long) pagesize;
201}
202
203typedef struct { unsigned short r[3]; } os_random_state_t;
204
205static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
206{
207 rs->r[0] = seed & 0xffff;
208 seed >>= 16;
209 rs->r[1] = seed & 0xffff;
210 seed >>= 16;
211 rs->r[2] = seed & 0xffff;
212 seed48(rs->r);
213}
214
215static inline long os_random_long(os_random_state_t *rs)
216{
217 return nrand48(rs->r);
218}
219
220#ifdef O_NOATIME
221#define FIO_O_NOATIME O_NOATIME
222#else
223#define FIO_O_NOATIME 0
224#endif
225
226/* Check for GCC or Clang byte swap intrinsics */
227#if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
228 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
229 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
230#define fio_swap16(x) __builtin_bswap16(x)
231#define fio_swap32(x) __builtin_bswap32(x)
232#define fio_swap64(x) __builtin_bswap64(x)
233#else
234#include <byteswap.h>
235#define fio_swap16(x) bswap_16(x)
236#define fio_swap32(x) bswap_32(x)
237#define fio_swap64(x) bswap_64(x)
238#endif /* fio_swapN */
239
240#define CACHE_LINE_FILE \
241 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
242
243static inline int arch_cache_line_size(void)
244{
245 char size[32];
246 int fd, ret;
247
248 fd = open(CACHE_LINE_FILE, O_RDONLY);
249 if (fd < 0)
250 return -1;
251
252 ret = read(fd, size, sizeof(size));
253
254 close(fd);
255
256 if (ret <= 0)
257 return -1;
258 else
259 return atoi(size);
260}
261
262static inline unsigned long long get_fs_free_size(const char *path)
263{
264 unsigned long long ret;
265 struct statfs s;
266
267 if (statfs(path, &s) < 0)
268 return -1ULL;
269
270 ret = s.f_bsize;
271 ret *= (unsigned long long) s.f_bfree;
272 return ret;
273}
274
275static inline int os_trim(int fd, unsigned long long start,
276 unsigned long long len)
277{
278 uint64_t range[2];
279
280 range[0] = start;
281 range[1] = len;
282
283 if (!ioctl(fd, BLKDISCARD, range))
284 return 0;
285
286 return errno;
287}
288
289#ifdef CONFIG_SCHED_IDLE
290static inline int fio_set_sched_idle(void)
291{
292 struct sched_param p = { .sched_priority = 0, };
293 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
294}
295#endif
296
297#endif