glusterfs: update for new API
[fio.git] / os / os-android.h
... / ...
CommitLineData
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_DISK_UTIL
28#define FIO_HAVE_IOSCHED_SWITCH
29#define FIO_HAVE_IOPRIO
30#define FIO_HAVE_IOPRIO_CLASS
31#define FIO_HAVE_ODIRECT
32#define FIO_HAVE_HUGETLB
33#define FIO_HAVE_BLKTRACE
34#define FIO_HAVE_CL_SIZE
35#define FIO_HAVE_CGROUPS
36#define FIO_HAVE_FS_STAT
37#define FIO_HAVE_TRIM
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
42#define FIO_HAVE_MMAP_HUGE
43#define FIO_NO_HAVE_SHM_H
44
45#define OS_MAP_ANON MAP_ANONYMOUS
46
47#ifndef POSIX_MADV_DONTNEED
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
52#endif
53
54#ifdef MADV_REMOVE
55#define FIO_MADV_FREE MADV_REMOVE
56#endif
57#ifndef MAP_HUGETLB
58#define MAP_HUGETLB 0x40000 /* arch specific */
59#endif
60
61#ifndef CONFIG_NO_SHM
62/*
63 * Bionic doesn't support SysV shared memeory, so implement it using ashmem
64 */
65#include <stdio.h>
66#include <linux/ashmem.h>
67#include <linux/shm.h>
68#define shmid_ds shmid64_ds
69#define SHM_HUGETLB 04000
70
71#define ASHMEM_DEVICE "/dev/ashmem"
72
73static inline int shmctl(int __shmid, int __cmd, struct shmid_ds *__buf)
74{
75 int ret=0;
76 if (__cmd == IPC_RMID)
77 {
78 int length = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
79 struct ashmem_pin pin = {0 , length};
80 ret = ioctl(__shmid, ASHMEM_UNPIN, &pin);
81 close(__shmid);
82 }
83 return ret;
84}
85
86static inline int shmget(key_t __key, size_t __size, int __shmflg)
87{
88 int fd,ret;
89 char keybuf[11];
90
91 fd = open(ASHMEM_DEVICE, O_RDWR);
92 if (fd < 0)
93 return fd;
94
95 sprintf(keybuf,"%d",__key);
96 ret = ioctl(fd, ASHMEM_SET_NAME, keybuf);
97 if (ret < 0)
98 goto error;
99
100 /* Stores size in first 8 bytes, allocate extra space */
101 ret = ioctl(fd, ASHMEM_SET_SIZE, __size + sizeof(uint64_t));
102 if (ret < 0)
103 goto error;
104
105 return fd;
106
107error:
108 close(fd);
109 return ret;
110}
111
112static inline void *shmat(int __shmid, const void *__shmaddr, int __shmflg)
113{
114 size_t size = ioctl(__shmid, ASHMEM_GET_SIZE, NULL);
115 /* Needs to be 8-byte aligned to prevent SIGBUS on 32-bit ARM */
116 uint64_t *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, __shmid, 0);
117 /* Save size at beginning of buffer, for use with munmap */
118 *ptr = size;
119 return ptr + 1;
120}
121
122static inline int shmdt (const void *__shmaddr)
123{
124 /* Find mmap size which we stored at the beginning of the buffer */
125 uint64_t *ptr = (uint64_t *)__shmaddr - 1;
126 size_t size = *ptr;
127 return munmap(ptr, size);
128}
129#endif
130
131#define SPLICE_DEF_SIZE (64*1024)
132
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
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
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
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
204#ifdef O_NOATIME
205#define FIO_O_NOATIME O_NOATIME
206#else
207#define FIO_O_NOATIME 0
208#endif
209
210/* Check for GCC or Clang byte swap intrinsics */
211#if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \
212 && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \
213 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */
214#define fio_swap16(x) __builtin_bswap16(x)
215#define fio_swap32(x) __builtin_bswap32(x)
216#define fio_swap64(x) __builtin_bswap64(x)
217#else
218#include <byteswap.h>
219#define fio_swap16(x) bswap_16(x)
220#define fio_swap32(x) bswap_32(x)
221#define fio_swap64(x) bswap_64(x)
222#endif /* fio_swapN */
223
224#define CACHE_LINE_FILE \
225 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
226
227static inline int arch_cache_line_size(void)
228{
229 char size[32];
230 int fd, ret;
231
232 fd = open(CACHE_LINE_FILE, O_RDONLY);
233 if (fd < 0)
234 return -1;
235
236 ret = read(fd, size, sizeof(size));
237
238 close(fd);
239
240 if (ret <= 0)
241 return -1;
242 else
243 return atoi(size);
244}
245
246static inline unsigned long long get_fs_free_size(const char *path)
247{
248 unsigned long long ret;
249 struct statfs s;
250
251 if (statfs(path, &s) < 0)
252 return -1ULL;
253
254 ret = s.f_bsize;
255 ret *= (unsigned long long) s.f_bfree;
256 return ret;
257}
258
259static inline int os_trim(struct fio_file *f, unsigned long long start,
260 unsigned long long len)
261{
262 uint64_t range[2];
263
264 range[0] = start;
265 range[1] = len;
266
267 if (!ioctl(f->fd, BLKDISCARD, range))
268 return 0;
269
270 return errno;
271}
272
273#ifdef CONFIG_SCHED_IDLE
274static inline int fio_set_sched_idle(void)
275{
276 struct sched_param p = { .sched_priority = 0, };
277 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
278}
279#endif
280
281#endif