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_CPU_AFFINITY
28 #define FIO_HAVE_DISK_UTIL
29 #define FIO_HAVE_IOSCHED_SWITCH
30 #define FIO_HAVE_IOPRIO
31 #define FIO_HAVE_IOPRIO_CLASS
32 #define FIO_HAVE_ODIRECT
33 #define FIO_HAVE_HUGETLB
34 #define FIO_HAVE_BLKTRACE
35 #define FIO_HAVE_CL_SIZE
36 #define FIO_HAVE_CGROUPS
37 #define FIO_HAVE_FS_STAT
39 #define FIO_HAVE_GETTID
40 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
41 #define FIO_HAVE_E4_ENG
42 #define FIO_HAVE_BYTEORDER_FUNCS
43 #define FIO_HAVE_MMAP_HUGE
44 #define FIO_NO_HAVE_SHM_H
46 #define OS_MAP_ANON MAP_ANONYMOUS
48 typedef cpu_set_t os_cpu_mask_t;
50 #define fio_setaffinity(pid, cpumask) \
51 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
52 #define fio_getaffinity(pid, ptr) \
53 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
55 #ifndef POSIX_MADV_DONTNEED
56 #define posix_madvise madvise
57 #define POSIX_MADV_DONTNEED MADV_DONTNEED
58 #define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
59 #define POSIX_MADV_RANDOM MADV_RANDOM
63 #define FIO_MADV_FREE MADV_REMOVE
66 #define MAP_HUGETLB 0x40000 /* arch specific */
69 #ifdef CONFIG_PTHREAD_GETAFFINITY
70 #define FIO_HAVE_GET_THREAD_AFFINITY
71 #define fio_get_thread_affinity(mask) \
72 pthread_getaffinity_np(pthread_self(), sizeof(mask), &(mask))
75 #define fio_cpu_clear(mask, cpu) CPU_CLR((cpu), (mask))
76 #define fio_cpu_set(mask, cpu) CPU_SET((cpu), (mask))
77 #define fio_cpu_isset(mask, cpu) (CPU_ISSET((cpu), (mask)) != 0)
78 #define fio_cpu_count(mask) CPU_COUNT((mask))
80 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
86 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
91 #define FIO_MAX_CPUS CPU_SETSIZE
95 * Bionic doesn't support SysV shared memory, so implement it using ashmem
98 #include <linux/ashmem.h>
99 #include <linux/shm.h>
100 #include <android/api-level.h>
101 #if __ANDROID_API__ >= __ANDROID_API_O__
102 #include <android/sharedmem.h>
104 #define ASHMEM_DEVICE "/dev/ashmem"
106 #define shmid_ds shmid64_ds
107 #define SHM_HUGETLB 04000
109 static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
112 if (__cmd == IPC_RMID)
114 int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
115 struct ashmem_pin pin = {0 , length};
116 ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
122 #if __ANDROID_API__ >= __ANDROID_API_O__
123 static inline int shmget(key_t __key, size_t __size, int __shmflg)
127 sprintf(keybuf, "%d", __key);
129 return ASharedMemory_create(keybuf, __size + sizeof(uint64_t));
132 static inline int shmget(key_t __key, size_t __size, int __shmflg)
137 fd = open(ASHMEM_DEVICE, O_RDWR);
141 sprintf(keybuf,"%d",__key);
142 ret = ioctl(fd, ASHMEM_SET_NAME, keybuf);
146 /* Stores size in first 8 bytes, allocate extra space */
147 ret = ioctl(fd, ASHMEM_SET_SIZE, __size + sizeof(uint64_t));
159 static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
161 size_t size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
162 /* Needs to be 8-byte aligned to prevent SIGBUS on 32-bit ARM */
163 uint64_t *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
164 /* Save size at beginning of buffer, for use with munmap */
169 static inline int shmdt (const void *__shmaddr)
171 /* Find mmap size which we stored at the beginning of the buffer */
172 uint64_t *ptr = (uint64_t *)__shmaddr - 1;
174 return munmap(ptr, size);
178 #define SPLICE_DEF_SIZE (64*1024)
188 IOPRIO_WHO_PROCESS = 1,
193 #define IOPRIO_BITS 16
194 #define IOPRIO_CLASS_SHIFT 13
196 #define IOPRIO_MIN_PRIO 0 /* highest priority */
197 #define IOPRIO_MAX_PRIO 7 /* lowest priority */
199 #define IOPRIO_MIN_PRIO_CLASS 0
200 #define IOPRIO_MAX_PRIO_CLASS 3
202 static inline int ioprio_value(int ioprio_class, int ioprio)
205 * If no class is set, assume BE
208 ioprio_class = IOPRIO_CLASS_BE;
210 return (ioprio_class << IOPRIO_CLASS_SHIFT) | ioprio;
213 static inline bool ioprio_value_is_class_rt(unsigned int priority)
215 return (priority >> IOPRIO_CLASS_SHIFT) == IOPRIO_CLASS_RT;
218 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
220 return syscall(__NR_ioprio_set, which, who,
221 ioprio_value(ioprio_class, ioprio));
225 #define BLKGETSIZE64 _IOR(0x12,114,size_t)
229 #define BLKFLSBUF _IO(0x12,97)
233 #define BLKDISCARD _IO(0x12,119)
236 static inline int blockdev_invalidate_cache(struct fio_file *f)
238 return ioctl(f->fd, BLKFLSBUF);
241 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
243 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
249 static inline unsigned long long os_phys_mem(void)
251 long pagesize, pages;
253 pagesize = sysconf(_SC_PAGESIZE);
254 pages = sysconf(_SC_PHYS_PAGES);
255 if (pages == -1 || pagesize == -1)
258 return (unsigned long long) pages * (unsigned long long) pagesize;
262 #define FIO_O_NOATIME O_NOATIME
264 #define FIO_O_NOATIME 0
267 /* Check for GCC or Clang byte swap intrinsics */
268 #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
269 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
270 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
271 #define fio_swap16(x) __builtin_bswap16(x)
272 #define fio_swap32(x) __builtin_bswap32(x)
273 #define fio_swap64(x) __builtin_bswap64(x)
275 #include <byteswap.h>
276 #define fio_swap16(x) bswap_16(x)
277 #define fio_swap32(x) bswap_32(x)
278 #define fio_swap64(x) bswap_64(x)
279 #endif /* fio_swapN */
281 #define CACHE_LINE_FILE \
282 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
284 static inline int arch_cache_line_size(void)
289 fd = open(CACHE_LINE_FILE, O_RDONLY);
293 ret = read(fd, size, sizeof(size));
303 static inline unsigned long long get_fs_free_size(const char *path)
305 unsigned long long ret;
308 if (statfs(path, &s) < 0)
312 ret *= (unsigned long long) s.f_bfree;
316 static inline int os_trim(struct fio_file *f, unsigned long long start,
317 unsigned long long len)
324 if (!ioctl(f->fd, BLKDISCARD, range))
330 #ifdef CONFIG_SCHED_IDLE
331 static inline int fio_set_sched_idle(void)
333 struct sched_param p = { .sched_priority = 0, };
334 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
339 #define RWF_UNCACHED 0x00000040