1 #ifndef FIO_OS_ANDROID_H
2 #define FIO_OS_ANDROID_H
4 #define FIO_OS os_android
9 #include <sys/syscall.h>
15 #include <linux/unistd.h>
16 #include <linux/major.h>
17 #include <asm/byteorder.h>
19 #include "./os-linux-syscall.h"
23 #ifndef __has_builtin // Optional of course.
24 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
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
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
44 #define OS_MAP_ANON MAP_ANONYMOUS
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
54 #define FIO_MADV_FREE MADV_REMOVE
57 #define MAP_HUGETLB 0x40000 /* arch specific */
62 * Bionic doesn't support SysV shared memeory, so implement it using ashmem
65 #include <linux/ashmem.h>
66 #include <linux/shm.h>
67 #define shmid_ds shmid64_ds
68 #define SHM_HUGETLB 04000
70 #define ASHMEM_DEVICE "/dev/ashmem"
72 static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
75 if (__cmd == IPC_RMID)
77 int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
78 struct ashmem_pin pin = {0 , length};
79 ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
85 static inline int shmget(key_t __key, size_t __size, int __shmflg)
90 fd = open(ASHMEM_DEVICE, O_RDWR);
94 sprintf(keybuf,"%d",__key);
95 ret = ioctl(fd, ASHMEM_SET_NAME, keybuf);
99 ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
110 static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
112 size_t *ptr, size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
113 ptr = mmap(NULL, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
114 *ptr = size; //save size at beginning of buffer, for use with munmap
118 static inline int shmdt (const void *__shmaddr)
121 ptr = (size_t *)__shmaddr;
123 size = *ptr; //find mmap size which we stored at the beginning of the buffer
124 return munmap((void *)ptr, size + sizeof(size_t));
128 #define SPLICE_DEF_SIZE (64*1024)
138 IOPRIO_WHO_PROCESS = 1,
143 #define IOPRIO_BITS 16
144 #define IOPRIO_CLASS_SHIFT 13
146 #define IOPRIO_MIN_PRIO 0 /* highest priority */
147 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
149 #define IOPRIO_MIN_PRIO_CLASS 0
150 #define IOPRIO_MAX_PRIO_CLASS 3
152 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
155 * If no class is set, assume BE
158 ioprio_class = IOPRIO_CLASS_BE;
160 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
161 return syscall(__NR_ioprio_set, which, who, ioprio);
165 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
169 #define BLKFLSBUF _IO(0x12,97)
173 #define BLKDISCARD _IO(0x12,119)
176 static inline int blockdev_invalidate_cache(struct fio_file *f)
178 return ioctl(f->fd, BLKFLSBUF);
181 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
183 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
189 static inline unsigned long long os_phys_mem(void)
191 long pagesize, pages;
193 pagesize = sysconf(_SC_PAGESIZE);
194 pages = sysconf(_SC_PHYS_PAGES);
195 if (pages == -1 || pagesize == -1)
198 return (unsigned long long) pages * (unsigned long long) pagesize;
201 typedef struct { unsigned short r[3]; } os_random_state_t;
203 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
205 rs->r[0] = seed & 0xffff;
207 rs->r[1] = seed & 0xffff;
209 rs->r[2] = seed & 0xffff;
213 static inline long os_random_long(os_random_state_t *rs)
215 return nrand48(rs->r);
219 #define FIO_O_NOATIME O_NOATIME
221 #define FIO_O_NOATIME 0
224 /* Check for GCC or Clang byte swap intrinsics */
225 #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
226 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
227 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
228 #define fio_swap16(x) __builtin_bswap16(x)
229 #define fio_swap32(x) __builtin_bswap32(x)
230 #define fio_swap64(x) __builtin_bswap64(x)
232 #include <byteswap.h>
233 #define fio_swap16(x) bswap_16(x)
234 #define fio_swap32(x) bswap_32(x)
235 #define fio_swap64(x) bswap_64(x)
236 #endif /* fio_swapN */
238 #define CACHE_LINE_FILE \
239 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
241 static inline int arch_cache_line_size(void)
246 fd = open(CACHE_LINE_FILE, O_RDONLY);
250 ret = read(fd, size, sizeof(size));
260 static inline unsigned long long get_fs_free_size(const char *path)
262 unsigned long long ret;
265 if (statfs(path, &s) < 0)
269 ret *= (unsigned long long) s.f_bfree;
273 static inline int os_trim(int fd, unsigned long long start,
274 unsigned long long len)
281 if (!ioctl(fd, BLKDISCARD, range))
287 #ifdef CONFIG_SCHED_IDLE
288 static inline int fio_set_sched_idle(void)
290 struct sched_param p = { .sched_priority = 0, };
291 return sched_setscheduler(gettid(), SCHED_IDLE, &p);