b8449828ae63534971cf11ed731948c5d5078d60
[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
17 #include "binject.h"
18 #include "../file.h"
19
20 #define FIO_HAVE_DISK_UTIL
21 #define FIO_HAVE_IOSCHED_SWITCH
22 #define FIO_HAVE_IOPRIO
23 #define FIO_HAVE_ODIRECT
24 #define FIO_HAVE_HUGETLB
25 #define FIO_HAVE_BLKTRACE
26 #define FIO_HAVE_PSHARED_MUTEX
27 #define FIO_HAVE_CL_SIZE
28 #define FIO_HAVE_FS_STAT
29 #define FIO_HAVE_TRIM
30 #define FIO_HAVE_GETTID
31 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
32 #define FIO_HAVE_E4_ENG
33 #define FIO_HAVE_BYTEORDER_FUNCS
34 #define FIO_HAVE_MMAP_HUGE
35 #define FIO_NO_HAVE_SHM_H
36
37 #define OS_MAP_ANON             MAP_ANONYMOUS
38
39 #define posix_madvise   madvise
40 #define POSIX_MADV_DONTNEED MADV_DONTNEED
41 #define POSIX_MADV_SEQUENTIAL   MADV_SEQUENTIAL
42 #define POSIX_MADV_RANDOM       MADV_RANDOM
43 #ifdef MADV_REMOVE
44 #define FIO_MADV_FREE   MADV_REMOVE
45 #endif
46 #ifndef MAP_HUGETLB
47 #define MAP_HUGETLB 0x40000 /* arch specific */
48 #endif
49
50
51 /*
52  * The Android NDK doesn't currently export <sys/shm.h>, so define the
53  * necessary stuff here.
54  */
55
56 #include <linux/shm.h>
57 #define SHM_HUGETLB    04000
58
59 static inline int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf)
60 {
61         return syscall(__NR_shmctl, __shmid, __cmd, __buf);
62 }
63
64 static inline int shmget (key_t __key, size_t __size, int __shmflg)
65 {
66         return syscall(__NR_shmget, __key, __size, __shmflg);
67 }
68
69 static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg)
70 {
71         return (void *)syscall(__NR_shmat, __shmid, __shmaddr, __shmflg);
72 }
73
74 static inline int shmdt (const void *__shmaddr)
75 {
76         return syscall(__NR_shmctl, __shmaddr);
77 }
78
79
80 #define SPLICE_DEF_SIZE (64*1024)
81
82 enum {
83         IOPRIO_CLASS_NONE,
84         IOPRIO_CLASS_RT,
85         IOPRIO_CLASS_BE,
86         IOPRIO_CLASS_IDLE,
87 };
88
89 enum {
90         IOPRIO_WHO_PROCESS = 1,
91         IOPRIO_WHO_PGRP,
92         IOPRIO_WHO_USER,
93 };
94
95 #define IOPRIO_BITS             16
96 #define IOPRIO_CLASS_SHIFT      13
97
98 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
99 {
100         /*
101          * If no class is set, assume BE
102          */
103         if (!ioprio_class)
104                 ioprio_class = IOPRIO_CLASS_BE;
105
106         ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
107         return syscall(__NR_ioprio_set, which, who, ioprio);
108 }
109
110 #ifndef BLKGETSIZE64
111 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
112 #endif
113
114 #ifndef BLKFLSBUF
115 #define BLKFLSBUF       _IO(0x12,97)
116 #endif
117
118 #ifndef BLKDISCARD
119 #define BLKDISCARD      _IO(0x12,119)
120 #endif
121
122 static inline int blockdev_invalidate_cache(struct fio_file *f)
123 {
124         return ioctl(f->fd, BLKFLSBUF);
125 }
126
127 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
128 {
129         if (!ioctl(f->fd, BLKGETSIZE64, bytes))
130                 return 0;
131
132         return errno;
133 }
134
135 static inline unsigned long long os_phys_mem(void)
136 {
137         long pagesize, pages;
138
139         pagesize = sysconf(_SC_PAGESIZE);
140         pages = sysconf(_SC_PHYS_PAGES);
141         if (pages == -1 || pagesize == -1)
142                 return 0;
143
144         return (unsigned long long) pages * (unsigned long long) pagesize;
145 }
146
147 typedef struct { unsigned short r[3]; } os_random_state_t;
148
149 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
150 {
151         rs->r[0] = seed & 0xffff;
152         seed >>= 16;
153         rs->r[1] = seed & 0xffff;
154         seed >>= 16;
155         rs->r[2] = seed & 0xffff;
156         seed48(rs->r);
157 }
158
159 static inline long os_random_long(os_random_state_t *rs)
160 {
161         return nrand48(rs->r);
162 }
163
164 #ifdef O_NOATIME
165 #define FIO_O_NOATIME   O_NOATIME
166 #else
167 #define FIO_O_NOATIME   0
168 #endif
169
170 #define fio_swap16(x)   __bswap_16(x)
171 #define fio_swap32(x)   __bswap_32(x)
172 #define fio_swap64(x)   __bswap_64(x)
173
174 #define CACHE_LINE_FILE \
175         "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
176
177 static inline int arch_cache_line_size(void)
178 {
179         char size[32];
180         int fd, ret;
181
182         fd = open(CACHE_LINE_FILE, O_RDONLY);
183         if (fd < 0)
184                 return -1;
185
186         ret = read(fd, size, sizeof(size));
187
188         close(fd);
189
190         if (ret <= 0)
191                 return -1;
192         else
193                 return atoi(size);
194 }
195
196 static inline unsigned long long get_fs_size(const char *path)
197 {
198         unsigned long long ret;
199         struct statfs s;
200
201         if (statfs(path, &s) < 0)
202                 return -1ULL;
203
204         ret = s.f_bsize;
205         ret *= (unsigned long long) s.f_bfree;
206         return ret;
207 }
208
209 static inline int os_trim(int fd, unsigned long long start,
210                           unsigned long long len)
211 {
212         uint64_t range[2];
213
214         range[0] = start;
215         range[1] = len;
216
217         if (!ioctl(fd, BLKDISCARD, range))
218                 return 0;
219
220         return errno;
221 }
222
223 #endif