1 #ifndef FIO_OS_ANDROID_H
2 #define FIO_OS_ANDROID_H
4 #define FIO_OS os_android
9 #include <sys/syscall.h>
10 #include <sys/sysmacros.h>
16 #include <linux/unistd.h>
17 #include <linux/major.h>
18 #include <asm/byteorder.h>
20 #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_CGROUPS
36 #define FIO_HAVE_FS_STAT
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
42 #define FIO_HAVE_MMAP_HUGE
43 #define FIO_NO_HAVE_SHM_H
45 #define OS_MAP_ANON MAP_ANONYMOUS
47 #ifndef POSIX_MADV_DONTNEED
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
55 #define FIO_MADV_FREE MADV_REMOVE
58 #define MAP_HUGETLB 0x40000 /* arch specific */
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))
69 * Bionic doesn't support SysV shared memeory, so implement it using ashmem
72 #include <linux/ashmem.h>
73 #include <linux/shm.h>
74 #define shmid_ds shmid64_ds
75 #define SHM_HUGETLB 04000
77 #define ASHMEM_DEVICE "/dev/ashmem"
79 static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
82 if (__cmd == IPC_RMID)
84 int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
85 struct ashmem_pin pin = {0 , length};
86 ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
92 static inline int shmget(key_t __key, size_t __size, int __shmflg)
97 fd = open(ASHMEM_DEVICE, O_RDWR);
101 sprintf(keybuf,"%d",__key);
102 ret = ioctl(fd, ASHMEM_SET_NAME, keybuf);
106 /* Stores size in first 8 bytes, allocate extra space */
107 ret = ioctl(fd, ASHMEM_SET_SIZE, __size + sizeof(uint64_t));
118 static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
120 size_t size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
121 /* Needs to be 8-byte aligned to prevent SIGBUS on 32-bit ARM */
122 uint64_t *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
123 /* Save size at beginning of buffer, for use with munmap */
128 static inline int shmdt (const void *__shmaddr)
130 /* Find mmap size which we stored at the beginning of the buffer */
131 uint64_t *ptr = (uint64_t *)__shmaddr - 1;
133 return munmap(ptr, size);
137 #define SPLICE_DEF_SIZE (64*1024)
147 IOPRIO_WHO_PROCESS = 1,
152 #define IOPRIO_BITS 16
153 #define IOPRIO_CLASS_SHIFT 13
155 #define IOPRIO_MIN_PRIO 0 /* highest priority */
156 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
158 #define IOPRIO_MIN_PRIO_CLASS 0
159 #define IOPRIO_MAX_PRIO_CLASS 3
161 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
164 * If no class is set, assume BE
167 ioprio_class = IOPRIO_CLASS_BE;
169 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
170 return syscall(__NR_ioprio_set, which, who, ioprio);
174 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
178 #define BLKFLSBUF _IO(0x12,97)
182 #define BLKDISCARD _IO(0x12,119)
185 static inline int blockdev_invalidate_cache(struct fio_file *f)
187 return ioctl(f->fd, BLKFLSBUF);
190 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
192 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
198 static inline unsigned long long os_phys_mem(void)
200 long pagesize, pages;
202 pagesize = sysconf(_SC_PAGESIZE);
203 pages = sysconf(_SC_PHYS_PAGES);
204 if (pages == -1 || pagesize == -1)
207 return (unsigned long long) pages * (unsigned long long) pagesize;
211 #define FIO_O_NOATIME O_NOATIME
213 #define FIO_O_NOATIME 0
216 /* Check for GCC or Clang byte swap intrinsics */
217 #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
218 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
219 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
220 #define fio_swap16(x) __builtin_bswap16(x)
221 #define fio_swap32(x) __builtin_bswap32(x)
222 #define fio_swap64(x) __builtin_bswap64(x)
224 #include <byteswap.h>
225 #define fio_swap16(x) bswap_16(x)
226 #define fio_swap32(x) bswap_32(x)
227 #define fio_swap64(x) bswap_64(x)
228 #endif /* fio_swapN */
230 #define CACHE_LINE_FILE \
231 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
233 static inline int arch_cache_line_size(void)
238 fd = open(CACHE_LINE_FILE, O_RDONLY);
242 ret = read(fd, size, sizeof(size));
252 static inline unsigned long long get_fs_free_size(const char *path)
254 unsigned long long ret;
257 if (statfs(path, &s) < 0)
261 ret *= (unsigned long long) s.f_bfree;
265 static inline int os_trim(struct fio_file *f, unsigned long long start,
266 unsigned long long len)
273 if (!ioctl(f->fd, BLKDISCARD, range))
279 #ifdef CONFIG_SCHED_IDLE
280 static inline int fio_set_sched_idle(void)
282 struct sched_param p = { .sched_priority = 0, };
283 return sched_setscheduler(gettid(), SCHED_IDLE, &p);