Android: add support for cgroups
[fio.git] / os / os-android.h
CommitLineData
ec5c6b12
AC
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>
93eeb558 7#include <sys/mman.h>
ec5c6b12
AC
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>
15#include <linux/unistd.h>
16#include <linux/major.h>
da52c582 17#include <asm/byteorder.h>
ec5c6b12 18
eec97b93 19#include "./os-linux-syscall.h"
ec5c6b12
AC
20#include "binject.h"
21#include "../file.h"
22
0fa5c986
OM
23#ifndef __has_builtin // Optional of course.
24 #define __has_builtin(x) 0 // Compatibility with non-clang compilers.
25#endif
26
ec5c6b12 27#define FIO_HAVE_DISK_UTIL
ec5c6b12 28#define FIO_HAVE_IOSCHED_SWITCH
177380ab 29#define FIO_HAVE_IOPRIO
32ef447a 30#define FIO_HAVE_IOPRIO_CLASS
ec5c6b12
AC
31#define FIO_HAVE_ODIRECT
32#define FIO_HAVE_HUGETLB
33#define FIO_HAVE_BLKTRACE
ec5c6b12 34#define FIO_HAVE_CL_SIZE
3714a963 35#define FIO_HAVE_CGROUPS
ec5c6b12
AC
36#define FIO_HAVE_FS_STAT
37#define FIO_HAVE_TRIM
ec5c6b12
AC
38#define FIO_HAVE_GETTID
39#define FIO_USE_GENERIC_INIT_RANDOM_STATE
40#define FIO_HAVE_E4_ENG
41#define FIO_HAVE_BYTEORDER_FUNCS
6d0e9f83 42#define FIO_HAVE_MMAP_HUGE
a5e0ee11 43#define FIO_NO_HAVE_SHM_H
ec5c6b12
AC
44
45#define OS_MAP_ANON MAP_ANONYMOUS
46
93eeb558 47#ifndef POSIX_MADV_DONTNEED
ec5c6b12
AC
48#define posix_madvise madvise
49#define POSIX_MADV_DONTNEED MADV_DONTNEED
50#define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
51#define POSIX_MADV_RANDOM MADV_RANDOM
93eeb558 52#endif
53
ec5c6b12
AC
54#ifdef MADV_REMOVE
55#define FIO_MADV_FREE MADV_REMOVE
56#endif
a5e0ee11
O
57#ifndef MAP_HUGETLB
58#define MAP_HUGETLB 0x40000 /* arch specific */
59#endif
ec5c6b12 60
6ca9b06d 61#ifndef CONFIG_NO_SHM
ec5c6b12
AC
62/*
63 * The Android NDK doesn't currently export <sys/shm.h>, so define the
64 * necessary stuff here.
65 */
66
6ca9b06d 67#include <sys/shm.h>
ec5c6b12
AC
68#define SHM_HUGETLB 04000
69
239a11de
AJ
70#include <stdio.h>
71#include <linux/ashmem.h>
239a11de
AJ
72
73#define ASHMEM_DEVICE "/dev/ashmem"
74
ec5c6b12
AC
75static inline int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf)
76{
239a11de
AJ
77 int ret=0;
78 if (__cmd == IPC_RMID)
79 {
80 int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
81 struct ashmem_pin pin = {0 , length};
82 ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
83 close(__shmid);
84 }
85 return ret;
ec5c6b12
AC
86}
87
88static inline int shmget (key_t __key, size_t __size, int __shmflg)
89{
239a11de 90 int fd,ret;
91f64346
OM
91 char keybuf[11];
92
239a11de
AJ
93 fd = open(ASHMEM_DEVICE, O_RDWR);
94 if (fd < 0)
95 return fd;
96
91f64346
OM
97 sprintf(keybuf,"%d",__key);
98 ret = ioctl(fd, ASHMEM_SET_NAME, keybuf);
239a11de
AJ
99 if (ret < 0)
100 goto error;
101
102 ret = ioctl(fd, ASHMEM_SET_SIZE, __size);
103 if (ret < 0)
104 goto error;
105
106 return fd;
91f64346 107
239a11de
AJ
108error:
109 close(fd);
110 return ret;
ec5c6b12
AC
111}
112
113static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg)
114{
239a11de
AJ
115 size_t *ptr, size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
116 ptr = mmap(NULL, size + sizeof(size_t), PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
117 *ptr = size; //save size at beginning of buffer, for use with munmap
118 return &ptr[1];
ec5c6b12
AC
119}
120
121static inline int shmdt (const void *__shmaddr)
122{
239a11de
AJ
123 size_t *ptr, size;
124 ptr = (size_t *)__shmaddr;
125 ptr--;
126 size = *ptr; //find mmap size which we stored at the beginning of the buffer
127 return munmap((void *)ptr, size + sizeof(size_t));
ec5c6b12 128}
6ca9b06d 129#endif
ec5c6b12 130
ec5c6b12
AC
131#define SPLICE_DEF_SIZE (64*1024)
132
177380ab
AC
133enum {
134 IOPRIO_CLASS_NONE,
135 IOPRIO_CLASS_RT,
136 IOPRIO_CLASS_BE,
137 IOPRIO_CLASS_IDLE,
138};
139
140enum {
141 IOPRIO_WHO_PROCESS = 1,
142 IOPRIO_WHO_PGRP,
143 IOPRIO_WHO_USER,
144};
145
146#define IOPRIO_BITS 16
147#define IOPRIO_CLASS_SHIFT 13
148
1767bd34
TK
149#define IOPRIO_MIN_PRIO 0 /* highest priority */
150#define IOPRIO_MAX_PRIO 7 /* lowest priority */
151
152#define IOPRIO_MIN_PRIO_CLASS 0
153#define IOPRIO_MAX_PRIO_CLASS 3
154
a415b2cc
AC
155static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
156{
157 /*
158 * If no class is set, assume BE
159 */
160 if (!ioprio_class)
161 ioprio_class = IOPRIO_CLASS_BE;
162
163 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
164 return syscall(__NR_ioprio_set, which, who, ioprio);
165}
166
ec5c6b12
AC
167#ifndef BLKGETSIZE64
168#define BLKGETSIZE64 _IOR(0x12,114,size_t)
169#endif
170
171#ifndef BLKFLSBUF
172#define BLKFLSBUF _IO(0x12,97)
173#endif
174
175#ifndef BLKDISCARD
176#define BLKDISCARD _IO(0x12,119)
177#endif
178
179static inline int blockdev_invalidate_cache(struct fio_file *f)
180{
181 return ioctl(f->fd, BLKFLSBUF);
182}
183
184static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
185{
186 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
187 return 0;
188
189 return errno;
190}
191
192static inline unsigned long long os_phys_mem(void)
193{
194 long pagesize, pages;
195
196 pagesize = sysconf(_SC_PAGESIZE);
197 pages = sysconf(_SC_PHYS_PAGES);
198 if (pages == -1 || pagesize == -1)
199 return 0;
200
201 return (unsigned long long) pages * (unsigned long long) pagesize;
202}
203
204typedef struct { unsigned short r[3]; } os_random_state_t;
205
206static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
207{
208 rs->r[0] = seed & 0xffff;
209 seed >>= 16;
210 rs->r[1] = seed & 0xffff;
211 seed >>= 16;
212 rs->r[2] = seed & 0xffff;
213 seed48(rs->r);
214}
215
216static inline long os_random_long(os_random_state_t *rs)
217{
218 return nrand48(rs->r);
219}
220
221#ifdef O_NOATIME
222#define FIO_O_NOATIME O_NOATIME
223#else
224#define FIO_O_NOATIME 0
225#endif
226
0fa5c986
OM
227/* Check for GCC or Clang byte swap intrinsics */
228#if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
229 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
230 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
231#define fio_swap16(x) __builtin_bswap16(x)
232#define fio_swap32(x) __builtin_bswap32(x)
233#define fio_swap64(x) __builtin_bswap64(x)
234#else
235#include <byteswap.h>
236#define fio_swap16(x) bswap_16(x)
237#define fio_swap32(x) bswap_32(x)
238#define fio_swap64(x) bswap_64(x)
239#endif /* fio_swapN */
ec5c6b12
AC
240
241#define CACHE_LINE_FILE \
242 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
243
244static inline int arch_cache_line_size(void)
245{
246 char size[32];
247 int fd, ret;
248
249 fd = open(CACHE_LINE_FILE, O_RDONLY);
250 if (fd < 0)
251 return -1;
252
253 ret = read(fd, size, sizeof(size));
254
255 close(fd);
256
257 if (ret <= 0)
258 return -1;
259 else
260 return atoi(size);
261}
262
c08ad04c 263static inline unsigned long long get_fs_free_size(const char *path)
ec5c6b12
AC
264{
265 unsigned long long ret;
266 struct statfs s;
267
268 if (statfs(path, &s) < 0)
269 return -1ULL;
270
271 ret = s.f_bsize;
272 ret *= (unsigned long long) s.f_bfree;
273 return ret;
274}
275
276static inline int os_trim(int fd, unsigned long long start,
277 unsigned long long len)
278{
279 uint64_t range[2];
280
281 range[0] = start;
282 range[1] = len;
283
284 if (!ioctl(fd, BLKDISCARD, range))
285 return 0;
286
287 return errno;
288}
289
93eeb558 290#ifdef CONFIG_SCHED_IDLE
291static inline int fio_set_sched_idle(void)
292{
293 struct sched_param p = { .sched_priority = 0, };
294 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
295}
296#endif
297
ec5c6b12 298#endif