Split os-android.h
[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/mman.h>
8 #include <sys/uio.h>
9 #include <sys/syscall.h>
10 #include <sys/sysmacros.h>
11 #include <sys/vfs.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sched.h>
16 #include <linux/unistd.h>
17 #include <linux/major.h>
18 #include <asm/byteorder.h>
19
20 #include "./os-linux-syscall.h"
21 #include "../file.h"
22
23 #ifndef __has_builtin         // Optional of course.
24   #define __has_builtin(x) 0  // Compatibility with non-clang compilers.
25 #endif
26
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
38 #define FIO_HAVE_TRIM
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
45
46 #define OS_MAP_ANON             MAP_ANONYMOUS
47
48 typedef cpu_set_t os_cpu_mask_t;
49
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))
54
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
60 #endif
61
62 #ifdef MADV_REMOVE
63 #define FIO_MADV_FREE   MADV_REMOVE
64 #endif
65 #ifndef MAP_HUGETLB
66 #define MAP_HUGETLB 0x40000 /* arch specific */
67 #endif
68
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))
73 #endif
74
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))
79
80 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
81 {
82         CPU_ZERO(mask);
83         return 0;
84 }
85
86 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
87 {
88         return 0;
89 }
90
91 #define FIO_MAX_CPUS                    CPU_SETSIZE
92
93 #include "os-ashmem.h"
94
95 #define SPLICE_DEF_SIZE (64*1024)
96
97 enum {
98         IOPRIO_CLASS_NONE,
99         IOPRIO_CLASS_RT,
100         IOPRIO_CLASS_BE,
101         IOPRIO_CLASS_IDLE,
102 };
103
104 enum {
105         IOPRIO_WHO_PROCESS = 1,
106         IOPRIO_WHO_PGRP,
107         IOPRIO_WHO_USER,
108 };
109
110 #define IOPRIO_BITS             16
111 #define IOPRIO_CLASS_SHIFT      13
112
113 #define IOPRIO_MIN_PRIO         0       /* highest priority */
114 #define IOPRIO_MAX_PRIO         7       /* lowest priority */
115
116 #define IOPRIO_MIN_PRIO_CLASS   0
117 #define IOPRIO_MAX_PRIO_CLASS   3
118
119 static inline int ioprio_value(int ioprio_class, int ioprio)
120 {
121         /*
122          * If no class is set, assume BE
123          */
124         if (!ioprio_class)
125                 ioprio_class = IOPRIO_CLASS_BE;
126
127         return (ioprio_class << IOPRIO_CLASS_SHIFT) | ioprio;
128 }
129
130 static inline bool ioprio_value_is_class_rt(unsigned int priority)
131 {
132         return (priority >> IOPRIO_CLASS_SHIFT) == IOPRIO_CLASS_RT;
133 }
134
135 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
136 {
137         return syscall(__NR_ioprio_set, which, who,
138                        ioprio_value(ioprio_class, ioprio));
139 }
140
141 #ifndef BLKGETSIZE64
142 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
143 #endif
144
145 #ifndef BLKFLSBUF
146 #define BLKFLSBUF       _IO(0x12,97)
147 #endif
148
149 #ifndef BLKDISCARD
150 #define BLKDISCARD      _IO(0x12,119)
151 #endif
152
153 static inline int blockdev_invalidate_cache(struct fio_file *f)
154 {
155         return ioctl(f->fd, BLKFLSBUF);
156 }
157
158 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
159 {
160         if (!ioctl(f->fd, BLKGETSIZE64, bytes))
161                 return 0;
162
163         return errno;
164 }
165
166 static inline unsigned long long os_phys_mem(void)
167 {
168         long pagesize, pages;
169
170         pagesize = sysconf(_SC_PAGESIZE);
171         pages = sysconf(_SC_PHYS_PAGES);
172         if (pages == -1 || pagesize == -1)
173                 return 0;
174
175         return (unsigned long long) pages * (unsigned long long) pagesize;
176 }
177
178 #ifdef O_NOATIME
179 #define FIO_O_NOATIME   O_NOATIME
180 #else
181 #define FIO_O_NOATIME   0
182 #endif
183
184 /* Check for GCC or Clang byte swap intrinsics */
185 #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
186      && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
187      || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
188 #define fio_swap16(x)   __builtin_bswap16(x)
189 #define fio_swap32(x)   __builtin_bswap32(x)
190 #define fio_swap64(x)   __builtin_bswap64(x)
191 #else
192 #include <byteswap.h>
193 #define fio_swap16(x)   bswap_16(x)
194 #define fio_swap32(x)   bswap_32(x)
195 #define fio_swap64(x)   bswap_64(x)
196 #endif /* fio_swapN */
197
198 #define CACHE_LINE_FILE \
199         "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
200
201 static inline int arch_cache_line_size(void)
202 {
203         char size[32];
204         int fd, ret;
205
206         fd = open(CACHE_LINE_FILE, O_RDONLY);
207         if (fd < 0)
208                 return -1;
209
210         ret = read(fd, size, sizeof(size));
211
212         close(fd);
213
214         if (ret <= 0)
215                 return -1;
216         else
217                 return atoi(size);
218 }
219
220 static inline unsigned long long get_fs_free_size(const char *path)
221 {
222         unsigned long long ret;
223         struct statfs s;
224
225         if (statfs(path, &s) < 0)
226                 return -1ULL;
227
228         ret = s.f_bsize;
229         ret *= (unsigned long long) s.f_bfree;
230         return ret;
231 }
232
233 static inline int os_trim(struct fio_file *f, unsigned long long start,
234                           unsigned long long len)
235 {
236         uint64_t range[2];
237
238         range[0] = start;
239         range[1] = len;
240
241         if (!ioctl(f->fd, BLKDISCARD, range))
242                 return 0;
243
244         return errno;
245 }
246
247 #ifdef CONFIG_SCHED_IDLE
248 static inline int fio_set_sched_idle(void)
249 {
250         struct sched_param p = { .sched_priority = 0, };
251         return sched_setscheduler(gettid(), SCHED_IDLE, &p);
252 }
253 #endif
254
255 #ifndef RWF_UNCACHED
256 #define RWF_UNCACHED    0x00000040
257 #endif
258
259 #endif