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