1d11cccde42bdd8b1db35bf016ccb65d8f38ac62
[fio.git] / os / os-android.h
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
61 /*
62  * The Android NDK doesn't currently export <sys/shm.h>, so define the
63  * necessary stuff here.
64  */
65
66 #include <linux/shm.h>
67 #define SHM_HUGETLB    04000
68
69 #include <stdio.h>
70 #include <linux/ashmem.h>
71 #include <sys/mman.h>
72
73 #define ASHMEM_DEVICE   "/dev/ashmem"
74
75 static inline int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf)
76 {
77         int ret=0;
78         if (__cmd == IPC_RMID)
79         {
80                 int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
81                 struct ashmem_pin pin = {0 , length};
82                 ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
83                 close(__shmid);
84         }
85         return ret;
86 }
87
88 static inline int shmget (key_t __key, size_t __size, int __shmflg)
89 {
90         int fd,ret;
91         char key[11];
92         
93         fd = open(ASHMEM_DEVICE, O_RDWR);
94         if (fd < 0)
95                 return fd;
96
97         sprintf(key,"%d",__key);
98         ret = ioctl(fd, ASHMEM_SET_NAME, key);
99         if (ret < 0)
100                 goto error;
101
102         ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
103         if (ret < 0)
104                 goto error;
105
106         return fd;
107         
108 error:
109         close(fd);
110         return ret;
111 }
112
113 static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg)
114 {
115         size_t *ptr, size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
116         ptr = mmap(NULL, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
117         *ptr = size;    //save size at beginning of buffer, for use with munmap
118         return &ptr[1];
119 }
120
121 static inline int shmdt (const void *__shmaddr)
122 {
123         size_t *ptr, size;
124         ptr = (size_t *)__shmaddr;
125         ptr--;
126         size = *ptr;    //find mmap size which we stored at the beginning of the buffer
127         return munmap((void *)ptr, size + sizeof(size_t));
128 }
129
130 #define SPLICE_DEF_SIZE (64*1024)
131
132 enum {
133         IOPRIO_CLASS_NONE,
134         IOPRIO_CLASS_RT,
135         IOPRIO_CLASS_BE,
136         IOPRIO_CLASS_IDLE,
137 };
138
139 enum {
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
154 static 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
178 static inline int blockdev_invalidate_cache(struct fio_file *f)
179 {
180         return ioctl(f->fd, BLKFLSBUF);
181 }
182
183 static 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
191 static 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
203 typedef struct { unsigned short r[3]; } os_random_state_t;
204
205 static 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
215 static 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
243 static 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
262 static 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
275 static 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
290 static 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