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