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> |
| 7 | #include <sys/uio.h> |
| 8 | #include <sys/syscall.h> |
| 9 | #include <sys/vfs.h> |
| 10 | #include <unistd.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <errno.h> |
| 13 | #include <sched.h> |
| 14 | #include <linux/unistd.h> |
| 15 | #include <linux/major.h> |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 16 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 17 | #include "binject.h" |
| 18 | #include "../file.h" |
| 19 | |
| 20 | #define FIO_HAVE_DISK_UTIL |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 21 | #define FIO_HAVE_IOSCHED_SWITCH |
| 22 | #define FIO_HAVE_ODIRECT |
| 23 | #define FIO_HAVE_HUGETLB |
| 24 | #define FIO_HAVE_BLKTRACE |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 25 | #define FIO_HAVE_PSHARED_MUTEX |
| 26 | #define FIO_HAVE_CL_SIZE |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 27 | #define FIO_HAVE_FS_STAT |
| 28 | #define FIO_HAVE_TRIM |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 29 | #define FIO_HAVE_GETTID |
| 30 | #define FIO_USE_GENERIC_INIT_RANDOM_STATE |
| 31 | #define FIO_HAVE_E4_ENG |
| 32 | #define FIO_HAVE_BYTEORDER_FUNCS |
Aaron Carroll | 6d0e9f8 | 2013-02-12 09:58:14 +0100 | [diff] [blame^] | 33 | #define FIO_HAVE_MMAP_HUGE |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 34 | |
| 35 | #define OS_MAP_ANON MAP_ANONYMOUS |
| 36 | |
| 37 | #define posix_madvise madvise |
| 38 | #define POSIX_MADV_DONTNEED MADV_DONTNEED |
| 39 | #define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL |
| 40 | #define POSIX_MADV_RANDOM MADV_RANDOM |
| 41 | #ifdef MADV_REMOVE |
| 42 | #define FIO_MADV_FREE MADV_REMOVE |
| 43 | #endif |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | * The Android NDK doesn't currently export <sys/shm.h>, so define the |
| 48 | * necessary stuff here. |
| 49 | */ |
| 50 | |
| 51 | #include <linux/shm.h> |
| 52 | #define SHM_HUGETLB 04000 |
| 53 | |
| 54 | static inline int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf) |
| 55 | { |
| 56 | return syscall(__NR_shmctl, __shmid, __cmd, __buf); |
| 57 | } |
| 58 | |
| 59 | static inline int shmget (key_t __key, size_t __size, int __shmflg) |
| 60 | { |
| 61 | return syscall(__NR_shmget, __key, __size, __shmflg); |
| 62 | } |
| 63 | |
| 64 | static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg) |
| 65 | { |
| 66 | return (void *)syscall(__NR_shmat, __shmid, __shmaddr, __shmflg); |
| 67 | } |
| 68 | |
| 69 | static inline int shmdt (const void *__shmaddr) |
| 70 | { |
| 71 | return syscall(__NR_shmctl, __shmaddr); |
| 72 | } |
| 73 | |
| 74 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 75 | #define SPLICE_DEF_SIZE (64*1024) |
| 76 | |
| 77 | #ifndef BLKGETSIZE64 |
| 78 | #define BLKGETSIZE64 _IOR(0x12,114,size_t) |
| 79 | #endif |
| 80 | |
| 81 | #ifndef BLKFLSBUF |
| 82 | #define BLKFLSBUF _IO(0x12,97) |
| 83 | #endif |
| 84 | |
| 85 | #ifndef BLKDISCARD |
| 86 | #define BLKDISCARD _IO(0x12,119) |
| 87 | #endif |
| 88 | |
| 89 | static inline int blockdev_invalidate_cache(struct fio_file *f) |
| 90 | { |
| 91 | return ioctl(f->fd, BLKFLSBUF); |
| 92 | } |
| 93 | |
| 94 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
| 95 | { |
| 96 | if (!ioctl(f->fd, BLKGETSIZE64, bytes)) |
| 97 | return 0; |
| 98 | |
| 99 | return errno; |
| 100 | } |
| 101 | |
| 102 | static inline unsigned long long os_phys_mem(void) |
| 103 | { |
| 104 | long pagesize, pages; |
| 105 | |
| 106 | pagesize = sysconf(_SC_PAGESIZE); |
| 107 | pages = sysconf(_SC_PHYS_PAGES); |
| 108 | if (pages == -1 || pagesize == -1) |
| 109 | return 0; |
| 110 | |
| 111 | return (unsigned long long) pages * (unsigned long long) pagesize; |
| 112 | } |
| 113 | |
| 114 | typedef struct { unsigned short r[3]; } os_random_state_t; |
| 115 | |
| 116 | static inline void os_random_seed(unsigned long seed, os_random_state_t *rs) |
| 117 | { |
| 118 | rs->r[0] = seed & 0xffff; |
| 119 | seed >>= 16; |
| 120 | rs->r[1] = seed & 0xffff; |
| 121 | seed >>= 16; |
| 122 | rs->r[2] = seed & 0xffff; |
| 123 | seed48(rs->r); |
| 124 | } |
| 125 | |
| 126 | static inline long os_random_long(os_random_state_t *rs) |
| 127 | { |
| 128 | return nrand48(rs->r); |
| 129 | } |
| 130 | |
| 131 | #ifdef O_NOATIME |
| 132 | #define FIO_O_NOATIME O_NOATIME |
| 133 | #else |
| 134 | #define FIO_O_NOATIME 0 |
| 135 | #endif |
| 136 | |
Aaron Carroll | ec5c6b1 | 2012-11-21 10:39:00 +0100 | [diff] [blame] | 137 | #define fio_swap16(x) __bswap_16(x) |
| 138 | #define fio_swap32(x) __bswap_32(x) |
| 139 | #define fio_swap64(x) __bswap_64(x) |
| 140 | |
| 141 | #define CACHE_LINE_FILE \ |
| 142 | "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size" |
| 143 | |
| 144 | static inline int arch_cache_line_size(void) |
| 145 | { |
| 146 | char size[32]; |
| 147 | int fd, ret; |
| 148 | |
| 149 | fd = open(CACHE_LINE_FILE, O_RDONLY); |
| 150 | if (fd < 0) |
| 151 | return -1; |
| 152 | |
| 153 | ret = read(fd, size, sizeof(size)); |
| 154 | |
| 155 | close(fd); |
| 156 | |
| 157 | if (ret <= 0) |
| 158 | return -1; |
| 159 | else |
| 160 | return atoi(size); |
| 161 | } |
| 162 | |
| 163 | static inline unsigned long long get_fs_size(const char *path) |
| 164 | { |
| 165 | unsigned long long ret; |
| 166 | struct statfs s; |
| 167 | |
| 168 | if (statfs(path, &s) < 0) |
| 169 | return -1ULL; |
| 170 | |
| 171 | ret = s.f_bsize; |
| 172 | ret *= (unsigned long long) s.f_bfree; |
| 173 | return ret; |
| 174 | } |
| 175 | |
| 176 | static inline int os_trim(int fd, unsigned long long start, |
| 177 | unsigned long long len) |
| 178 | { |
| 179 | uint64_t range[2]; |
| 180 | |
| 181 | range[0] = start; |
| 182 | range[1] = len; |
| 183 | |
| 184 | if (!ioctl(fd, BLKDISCARD, range)) |
| 185 | return 0; |
| 186 | |
| 187 | return errno; |
| 188 | } |
| 189 | |
| 190 | #endif |