Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 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> |
Elliott Hughes | abe5ebc | 2015-02-20 10:34:05 -0800 | [diff] [blame] | 7 | #include <sys/mman.h> |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 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> |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 15 | #include <linux/major.h> |
Aaron Carroll | da52c58 | 2013-04-12 09:15:23 +0200 | [diff] [blame] | 16 | #include <asm/byteorder.h> |
Mohamad Ayyash | 3c77c13 | 2014-05-14 12:13:31 -0700 | [diff] [blame] | 17 | #include <byteswap.h> |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 18 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 19 | #include "binject.h" |
| 20 | #include "../file.h" |
| 21 | |
| 22 | #define FIO_HAVE_DISK_UTIL |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 23 | #define FIO_HAVE_IOSCHED_SWITCH |
Aaron Carroll | 177380a | 2013-03-12 15:46:10 +1100 | [diff] [blame] | 24 | #define FIO_HAVE_IOPRIO |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 25 | #define FIO_HAVE_ODIRECT |
| 26 | #define FIO_HAVE_HUGETLB |
| 27 | #define FIO_HAVE_BLKTRACE |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 28 | #define FIO_HAVE_PSHARED_MUTEX |
| 29 | #define FIO_HAVE_CL_SIZE |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 30 | #define FIO_HAVE_FS_STAT |
| 31 | #define FIO_HAVE_TRIM |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 32 | #define FIO_HAVE_GETTID |
| 33 | #define FIO_USE_GENERIC_INIT_RANDOM_STATE |
| 34 | #define FIO_HAVE_E4_ENG |
| 35 | #define FIO_HAVE_BYTEORDER_FUNCS |
Aaron Carroll | 6d0e9f8 | 2013-02-12 09:58:14 +0100 | [diff] [blame] | 36 | #define FIO_HAVE_MMAP_HUGE |
Oleg | a5e0ee1 | 2013-03-12 00:06:53 -0700 | [diff] [blame] | 37 | #define FIO_NO_HAVE_SHM_H |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 38 | |
| 39 | #define OS_MAP_ANON MAP_ANONYMOUS |
| 40 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 41 | #ifdef MADV_REMOVE |
| 42 | #define FIO_MADV_FREE MADV_REMOVE |
| 43 | #endif |
Oleg | a5e0ee1 | 2013-03-12 00:06:53 -0700 | [diff] [blame] | 44 | #ifndef MAP_HUGETLB |
| 45 | #define MAP_HUGETLB 0x40000 /* arch specific */ |
| 46 | #endif |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 47 | |
| 48 | |
| 49 | /* |
| 50 | * The Android NDK doesn't currently export <sys/shm.h>, so define the |
| 51 | * necessary stuff here. |
| 52 | */ |
| 53 | |
| 54 | #include <linux/shm.h> |
| 55 | #define SHM_HUGETLB 04000 |
| 56 | |
Akers, Jason B | 239a11d | 2013-05-01 21:28:49 +0200 | [diff] [blame] | 57 | #include <stdio.h> |
| 58 | #include <linux/ashmem.h> |
| 59 | #include <sys/mman.h> |
| 60 | |
| 61 | #define ASHMEM_DEVICE "/dev/ashmem" |
| 62 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 63 | static inline int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf) |
| 64 | { |
Akers, Jason B | 239a11d | 2013-05-01 21:28:49 +0200 | [diff] [blame] | 65 | int ret=0; |
| 66 | if (__cmd == IPC_RMID) |
| 67 | { |
| 68 | int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL); |
| 69 | struct ashmem_pin pin = {0 , length}; |
| 70 | ret = ioctl(__shmid, ASHMEM_UNPIN, &pin); |
| 71 | close(__shmid); |
| 72 | } |
| 73 | return ret; |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static inline int shmget (key_t __key, size_t __size, int __shmflg) |
| 77 | { |
Akers, Jason B | 239a11d | 2013-05-01 21:28:49 +0200 | [diff] [blame] | 78 | int fd,ret; |
| 79 | char key[11]; |
| 80 | |
| 81 | fd = open(ASHMEM_DEVICE, O_RDWR); |
| 82 | if (fd < 0) |
| 83 | return fd; |
| 84 | |
| 85 | sprintf(key,"%d",__key); |
| 86 | ret = ioctl(fd, ASHMEM_SET_NAME, key); |
| 87 | if (ret < 0) |
| 88 | goto error; |
| 89 | |
| 90 | ret = ioctl(fd, ASHMEM_SET_SIZE, __size); |
| 91 | if (ret < 0) |
| 92 | goto error; |
| 93 | |
| 94 | return fd; |
| 95 | |
| 96 | error: |
| 97 | close(fd); |
| 98 | return ret; |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg) |
| 102 | { |
Akers, Jason B | 239a11d | 2013-05-01 21:28:49 +0200 | [diff] [blame] | 103 | size_t *ptr, size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL); |
| 104 | ptr = mmap(NULL, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0); |
| 105 | *ptr = size; //save size at beginning of buffer, for use with munmap |
| 106 | return &ptr[1]; |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static inline int shmdt (const void *__shmaddr) |
| 110 | { |
Akers, Jason B | 239a11d | 2013-05-01 21:28:49 +0200 | [diff] [blame] | 111 | size_t *ptr, size; |
| 112 | ptr = (size_t *)__shmaddr; |
| 113 | ptr--; |
| 114 | size = *ptr; //find mmap size which we stored at the beginning of the buffer |
| 115 | return munmap((void *)ptr, size + sizeof(size_t)); |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 118 | #define SPLICE_DEF_SIZE (64*1024) |
| 119 | |
Aaron Carroll | 177380a | 2013-03-12 15:46:10 +1100 | [diff] [blame] | 120 | enum { |
| 121 | IOPRIO_CLASS_NONE, |
| 122 | IOPRIO_CLASS_RT, |
| 123 | IOPRIO_CLASS_BE, |
| 124 | IOPRIO_CLASS_IDLE, |
| 125 | }; |
| 126 | |
| 127 | enum { |
| 128 | IOPRIO_WHO_PROCESS = 1, |
| 129 | IOPRIO_WHO_PGRP, |
| 130 | IOPRIO_WHO_USER, |
| 131 | }; |
| 132 | |
| 133 | #define IOPRIO_BITS 16 |
| 134 | #define IOPRIO_CLASS_SHIFT 13 |
| 135 | |
Aaron Carroll | a415b2c | 2013-04-12 07:59:01 +0200 | [diff] [blame] | 136 | static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio) |
| 137 | { |
| 138 | /* |
| 139 | * If no class is set, assume BE |
| 140 | */ |
| 141 | if (!ioprio_class) |
| 142 | ioprio_class = IOPRIO_CLASS_BE; |
| 143 | |
| 144 | ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT; |
| 145 | return syscall(__NR_ioprio_set, which, who, ioprio); |
| 146 | } |
| 147 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 148 | #ifndef BLKGETSIZE64 |
| 149 | #define BLKGETSIZE64 _IOR(0x12,114,size_t) |
| 150 | #endif |
| 151 | |
| 152 | #ifndef BLKFLSBUF |
| 153 | #define BLKFLSBUF _IO(0x12,97) |
| 154 | #endif |
| 155 | |
| 156 | #ifndef BLKDISCARD |
| 157 | #define BLKDISCARD _IO(0x12,119) |
| 158 | #endif |
| 159 | |
| 160 | static inline int blockdev_invalidate_cache(struct fio_file *f) |
| 161 | { |
| 162 | return ioctl(f->fd, BLKFLSBUF); |
| 163 | } |
| 164 | |
| 165 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
| 166 | { |
| 167 | if (!ioctl(f->fd, BLKGETSIZE64, bytes)) |
| 168 | return 0; |
| 169 | |
| 170 | return errno; |
| 171 | } |
| 172 | |
| 173 | static inline unsigned long long os_phys_mem(void) |
| 174 | { |
| 175 | long pagesize, pages; |
| 176 | |
| 177 | pagesize = sysconf(_SC_PAGESIZE); |
| 178 | pages = sysconf(_SC_PHYS_PAGES); |
| 179 | if (pages == -1 || pagesize == -1) |
| 180 | return 0; |
| 181 | |
| 182 | return (unsigned long long) pages * (unsigned long long) pagesize; |
| 183 | } |
| 184 | |
| 185 | typedef struct { unsigned short r[3]; } os_random_state_t; |
| 186 | |
| 187 | static inline void os_random_seed(unsigned long seed, os_random_state_t *rs) |
| 188 | { |
| 189 | rs->r[0] = seed & 0xffff; |
| 190 | seed >>= 16; |
| 191 | rs->r[1] = seed & 0xffff; |
| 192 | seed >>= 16; |
| 193 | rs->r[2] = seed & 0xffff; |
| 194 | seed48(rs->r); |
| 195 | } |
| 196 | |
| 197 | static inline long os_random_long(os_random_state_t *rs) |
| 198 | { |
| 199 | return nrand48(rs->r); |
| 200 | } |
| 201 | |
| 202 | #ifdef O_NOATIME |
| 203 | #define FIO_O_NOATIME O_NOATIME |
| 204 | #else |
| 205 | #define FIO_O_NOATIME 0 |
| 206 | #endif |
| 207 | |
Mohamad Ayyash | 3c77c13 | 2014-05-14 12:13:31 -0700 | [diff] [blame] | 208 | #define fio_swap16(x) bswap_16(x) |
| 209 | #define fio_swap32(x) bswap_32(x) |
| 210 | #define fio_swap64(x) bswap_64(x) |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 211 | |
| 212 | #define CACHE_LINE_FILE \ |
| 213 | "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size" |
| 214 | |
| 215 | static inline int arch_cache_line_size(void) |
| 216 | { |
| 217 | char size[32]; |
| 218 | int fd, ret; |
| 219 | |
| 220 | fd = open(CACHE_LINE_FILE, O_RDONLY); |
| 221 | if (fd < 0) |
| 222 | return -1; |
| 223 | |
| 224 | ret = read(fd, size, sizeof(size)); |
| 225 | |
| 226 | close(fd); |
| 227 | |
| 228 | if (ret <= 0) |
| 229 | return -1; |
| 230 | else |
| 231 | return atoi(size); |
| 232 | } |
| 233 | |
| 234 | static inline unsigned long long get_fs_size(const char *path) |
| 235 | { |
| 236 | unsigned long long ret; |
| 237 | struct statfs s; |
| 238 | |
| 239 | if (statfs(path, &s) < 0) |
| 240 | return -1ULL; |
| 241 | |
| 242 | ret = s.f_bsize; |
| 243 | ret *= (unsigned long long) s.f_bfree; |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static inline int os_trim(int fd, unsigned long long start, |
| 248 | unsigned long long len) |
| 249 | { |
| 250 | uint64_t range[2]; |
| 251 | |
| 252 | range[0] = start; |
| 253 | range[1] = len; |
| 254 | |
| 255 | if (!ioctl(fd, BLKDISCARD, range)) |
| 256 | return 0; |
| 257 | |
| 258 | return errno; |
| 259 | } |
| 260 | |
Elliott Hughes | abe5ebc | 2015-02-20 10:34:05 -0800 | [diff] [blame] | 261 | #ifdef CONFIG_SCHED_IDLE |
| 262 | static inline int fio_set_sched_idle(void) |
| 263 | { |
| 264 | struct sched_param p = { .sched_priority = 0, }; |
| 265 | return sched_setscheduler(gettid(), SCHED_IDLE, &p); |
| 266 | } |
| 267 | #endif |
| 268 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 269 | #endif |