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 /* Stores size in first 8 bytes, allocate extra space */
100 ret = ioctl(fd, ASHMEM_SET_SIZE, __size + sizeof(uint64_t));
111 static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
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 */
121 static inline int shmdt (const void *__shmaddr)
123 /* Find mmap size which we stored at the beginning of the buffer */
124 uint64_t *ptr = (uint64_t *)__shmaddr - 1;
126 return munmap(ptr, size);
130 #define SPLICE_DEF_SIZE (64*1024)
140 IOPRIO_WHO_PROCESS = 1,
145 #define IOPRIO_BITS 16
146 #define IOPRIO_CLASS_SHIFT 13
148 #define IOPRIO_MIN_PRIO 0 /* highest priority */
149 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
151 #define IOPRIO_MIN_PRIO_CLASS 0
152 #define IOPRIO_MAX_PRIO_CLASS 3
154 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
157 * If no class is set, assume BE
160 ioprio_class = IOPRIO_CLASS_BE;
162 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
163 return syscall(__NR_ioprio_set, which, who, ioprio);
167 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
171 #define BLKFLSBUF _IO(0x12,97)
175 #define BLKDISCARD _IO(0x12,119)
178 static inline int blockdev_invalidate_cache(struct fio_file *f)
180 return ioctl(f->fd, BLKFLSBUF);
183 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
185 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
191 static inline unsigned long long os_phys_mem(void)
193 long pagesize, pages;
195 pagesize = sysconf(_SC_PAGESIZE);
196 pages = sysconf(_SC_PHYS_PAGES);
197 if (pages == -1 || pagesize == -1)
200 return (unsigned long long) pages * (unsigned long long) pagesize;
203 typedef struct { unsigned short r[3]; } os_random_state_t;
205 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
207 rs->r[0] = seed & 0xffff;
209 rs->r[1] = seed & 0xffff;
211 rs->r[2] = seed & 0xffff;
215 static inline long os_random_long(os_random_state_t *rs)
217 return nrand48(rs->r);
221 #define FIO_O_NOATIME O_NOATIME
223 #define FIO_O_NOATIME 0
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)
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 */
240 #define CACHE_LINE_FILE \
241 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
243 static inline int arch_cache_line_size(void)
248 fd = open(CACHE_LINE_FILE, O_RDONLY);
252 ret = read(fd, size, sizeof(size));
262 static inline unsigned long long get_fs_free_size(const char *path)
264 unsigned long long ret;
267 if (statfs(path, &s) < 0)
271 ret *= (unsigned long long) s.f_bfree;
275 static inline int os_trim(int fd, unsigned long long start,
276 unsigned long long len)
283 if (!ioctl(fd, BLKDISCARD, range))
289 #ifdef CONFIG_SCHED_IDLE
290 static inline int fio_set_sched_idle(void)
292 struct sched_param p = { .sched_priority = 0, };
293 return sched_setscheduler(gettid(), SCHED_IDLE, &p);