Add support for Android
[fio.git] / os / os-android.h
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>
16 #include <endian.h>
17
18 #include "indirect.h"
19 #include "binject.h"
20 #include "../file.h"
21
22 #define FIO_HAVE_DISK_UTIL
23 #define FIO_HAVE_SPLICE
24 #define FIO_HAVE_IOSCHED_SWITCH
25 #define FIO_HAVE_ODIRECT
26 #define FIO_HAVE_HUGETLB
27 #define FIO_HAVE_BLKTRACE
28 #define FIO_HAVE_STRSEP
29 #define FIO_HAVE_POSIXAIO_FSYNC
30 #define FIO_HAVE_PSHARED_MUTEX
31 #define FIO_HAVE_CL_SIZE
32 #define FIO_HAVE_FDATASYNC
33 #define FIO_HAVE_FS_STAT
34 #define FIO_HAVE_TRIM
35 #define FIO_HAVE_CLOCK_MONOTONIC
36 #define FIO_HAVE_GETTID
37 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
38 #define FIO_HAVE_E4_ENG
39 #define FIO_HAVE_BYTEORDER_FUNCS
40
41 #define OS_MAP_ANON             MAP_ANONYMOUS
42
43 #define posix_madvise   madvise
44 #define POSIX_MADV_DONTNEED MADV_DONTNEED
45 #define POSIX_MADV_SEQUENTIAL   MADV_SEQUENTIAL
46 #define POSIX_MADV_RANDOM       MADV_RANDOM
47 #ifdef MADV_REMOVE
48 #define FIO_MADV_FREE   MADV_REMOVE
49 #endif
50
51
52 /*
53  * The Android NDK doesn't currently export <sys/shm.h>, so define the
54  * necessary stuff here.
55  */
56
57 #include <linux/shm.h>
58 #define SHM_HUGETLB    04000
59
60 static inline int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf)
61 {
62         return syscall(__NR_shmctl, __shmid, __cmd, __buf);
63 }
64
65 static inline int shmget (key_t __key, size_t __size, int __shmflg)
66 {
67         return syscall(__NR_shmget, __key, __size, __shmflg);
68 }
69
70 static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg)
71 {
72         return (void *)syscall(__NR_shmat, __shmid, __shmaddr, __shmflg);
73 }
74
75 static inline int shmdt (const void *__shmaddr)
76 {
77         return syscall(__NR_shmctl, __shmaddr);
78 }
79
80
81 /*
82  * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
83  * aren't either.
84  */
85 #ifndef SPLICE_F_MOVE
86 #define SPLICE_F_MOVE   (0x01)  /* move pages instead of copying */
87 #define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
88                                  /* we may still block on the fd we splice */
89                                  /* from/to, of course */
90 #define SPLICE_F_MORE   (0x04)  /* expect more data */
91 #define SPLICE_F_GIFT   (0x08)  /* pages passed in are a gift */
92
93 static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
94                          size_t len, unsigned int flags)
95 {
96         return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
97 }
98
99 static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
100 {
101         return syscall(__NR_sys_tee, fdin, fdout, len, flags);
102 }
103
104 static inline int vmsplice(int fd, const struct iovec *iov,
105                            unsigned long nr_segs, unsigned int flags)
106 {
107         return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
108 }
109 #endif
110
111 #define SPLICE_DEF_SIZE (64*1024)
112
113 #ifndef BLKGETSIZE64
114 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
115 #endif
116
117 #ifndef BLKFLSBUF
118 #define BLKFLSBUF       _IO(0x12,97)
119 #endif
120
121 #ifndef BLKDISCARD
122 #define BLKDISCARD      _IO(0x12,119)
123 #endif
124
125 static inline int blockdev_invalidate_cache(struct fio_file *f)
126 {
127         return ioctl(f->fd, BLKFLSBUF);
128 }
129
130 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
131 {
132         if (!ioctl(f->fd, BLKGETSIZE64, bytes))
133                 return 0;
134
135         return errno;
136 }
137
138 static inline unsigned long long os_phys_mem(void)
139 {
140         long pagesize, pages;
141
142         pagesize = sysconf(_SC_PAGESIZE);
143         pages = sysconf(_SC_PHYS_PAGES);
144         if (pages == -1 || pagesize == -1)
145                 return 0;
146
147         return (unsigned long long) pages * (unsigned long long) pagesize;
148 }
149
150 typedef struct { unsigned short r[3]; } os_random_state_t;
151
152 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
153 {
154         rs->r[0] = seed & 0xffff;
155         seed >>= 16;
156         rs->r[1] = seed & 0xffff;
157         seed >>= 16;
158         rs->r[2] = seed & 0xffff;
159         seed48(rs->r);
160 }
161
162 static inline long os_random_long(os_random_state_t *rs)
163 {
164         return nrand48(rs->r);
165 }
166
167 #ifdef O_NOATIME
168 #define FIO_O_NOATIME   O_NOATIME
169 #else
170 #define FIO_O_NOATIME   0
171 #endif
172
173 #if __BYTE_ORDER == __LITTLE_ENDIAN
174 #define FIO_LITTLE_ENDIAN
175 #elif __BYTE_ORDER == __BIG_ENDIAN
176 #define FIO_BIG_ENDIAN
177 #else
178 #error "Unknown endianness"
179 #endif
180
181 #define fio_swap16(x)   __bswap_16(x)
182 #define fio_swap32(x)   __bswap_32(x)
183 #define fio_swap64(x)   __bswap_64(x)
184
185 #define CACHE_LINE_FILE \
186         "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
187
188 static inline int arch_cache_line_size(void)
189 {
190         char size[32];
191         int fd, ret;
192
193         fd = open(CACHE_LINE_FILE, O_RDONLY);
194         if (fd < 0)
195                 return -1;
196
197         ret = read(fd, size, sizeof(size));
198
199         close(fd);
200
201         if (ret <= 0)
202                 return -1;
203         else
204                 return atoi(size);
205 }
206
207 static inline unsigned long long get_fs_size(const char *path)
208 {
209         unsigned long long ret;
210         struct statfs s;
211
212         if (statfs(path, &s) < 0)
213                 return -1ULL;
214
215         ret = s.f_bsize;
216         ret *= (unsigned long long) s.f_bfree;
217         return ret;
218 }
219
220 static inline int os_trim(int fd, unsigned long long start,
221                           unsigned long long len)
222 {
223         uint64_t range[2];
224
225         range[0] = start;
226         range[1] = len;
227
228         if (!ioctl(fd, BLKDISCARD, range))
229                 return 0;
230
231         return errno;
232 }
233
234 #endif