blob: 3bb44b9529290ebea26515521be2f1e88ce63ad8 [file] [log] [blame]
Aaron Carrollec5c6b12012-11-21 10:39:00 +01001#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>
Aaron Carrollec5c6b12012-11-21 10:39:00 +010016
Aaron Carrollec5c6b12012-11-21 10:39:00 +010017#include "binject.h"
18#include "../file.h"
19
20#define FIO_HAVE_DISK_UTIL
Aaron Carrollec5c6b12012-11-21 10:39:00 +010021#define FIO_HAVE_IOSCHED_SWITCH
22#define FIO_HAVE_ODIRECT
23#define FIO_HAVE_HUGETLB
24#define FIO_HAVE_BLKTRACE
Aaron Carrollec5c6b12012-11-21 10:39:00 +010025#define FIO_HAVE_PSHARED_MUTEX
26#define FIO_HAVE_CL_SIZE
Aaron Carrollec5c6b12012-11-21 10:39:00 +010027#define FIO_HAVE_FS_STAT
28#define FIO_HAVE_TRIM
Aaron Carrollec5c6b12012-11-21 10:39:00 +010029#define FIO_HAVE_GETTID
30#define FIO_USE_GENERIC_INIT_RANDOM_STATE
31#define FIO_HAVE_E4_ENG
32#define FIO_HAVE_BYTEORDER_FUNCS
Aaron Carroll6d0e9f82013-02-12 09:58:14 +010033#define FIO_HAVE_MMAP_HUGE
Olega5e0ee12013-03-12 00:06:53 -070034#define FIO_NO_HAVE_SHM_H
Aaron Carrollec5c6b12012-11-21 10:39:00 +010035
36#define OS_MAP_ANON MAP_ANONYMOUS
37
38#define posix_madvise madvise
39#define POSIX_MADV_DONTNEED MADV_DONTNEED
40#define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL
41#define POSIX_MADV_RANDOM MADV_RANDOM
42#ifdef MADV_REMOVE
43#define FIO_MADV_FREE MADV_REMOVE
44#endif
Olega5e0ee12013-03-12 00:06:53 -070045#ifndef MAP_HUGETLB
46#define MAP_HUGETLB 0x40000 /* arch specific */
47#endif
Aaron Carrollec5c6b12012-11-21 10:39:00 +010048
49
50/*
51 * The Android NDK doesn't currently export <sys/shm.h>, so define the
52 * necessary stuff here.
53 */
54
55#include <linux/shm.h>
56#define SHM_HUGETLB 04000
57
58static inline int shmctl (int __shmid, int __cmd, struct shmid_ds *__buf)
59{
60 return syscall(__NR_shmctl, __shmid, __cmd, __buf);
61}
62
63static inline int shmget (key_t __key, size_t __size, int __shmflg)
64{
65 return syscall(__NR_shmget, __key, __size, __shmflg);
66}
67
68static inline void *shmat (int __shmid, const void *__shmaddr, int __shmflg)
69{
70 return (void *)syscall(__NR_shmat, __shmid, __shmaddr, __shmflg);
71}
72
73static inline int shmdt (const void *__shmaddr)
74{
75 return syscall(__NR_shmctl, __shmaddr);
76}
77
78
Aaron Carrollec5c6b12012-11-21 10:39:00 +010079#define SPLICE_DEF_SIZE (64*1024)
80
81#ifndef BLKGETSIZE64
82#define BLKGETSIZE64 _IOR(0x12,114,size_t)
83#endif
84
85#ifndef BLKFLSBUF
86#define BLKFLSBUF _IO(0x12,97)
87#endif
88
89#ifndef BLKDISCARD
90#define BLKDISCARD _IO(0x12,119)
91#endif
92
93static inline int blockdev_invalidate_cache(struct fio_file *f)
94{
95 return ioctl(f->fd, BLKFLSBUF);
96}
97
98static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
99{
100 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
101 return 0;
102
103 return errno;
104}
105
106static inline unsigned long long os_phys_mem(void)
107{
108 long pagesize, pages;
109
110 pagesize = sysconf(_SC_PAGESIZE);
111 pages = sysconf(_SC_PHYS_PAGES);
112 if (pages == -1 || pagesize == -1)
113 return 0;
114
115 return (unsigned long long) pages * (unsigned long long) pagesize;
116}
117
118typedef struct { unsigned short r[3]; } os_random_state_t;
119
120static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
121{
122 rs->r[0] = seed & 0xffff;
123 seed >>= 16;
124 rs->r[1] = seed & 0xffff;
125 seed >>= 16;
126 rs->r[2] = seed & 0xffff;
127 seed48(rs->r);
128}
129
130static inline long os_random_long(os_random_state_t *rs)
131{
132 return nrand48(rs->r);
133}
134
135#ifdef O_NOATIME
136#define FIO_O_NOATIME O_NOATIME
137#else
138#define FIO_O_NOATIME 0
139#endif
140
Aaron Carrollec5c6b12012-11-21 10:39:00 +0100141#define fio_swap16(x) __bswap_16(x)
142#define fio_swap32(x) __bswap_32(x)
143#define fio_swap64(x) __bswap_64(x)
144
145#define CACHE_LINE_FILE \
146 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
147
148static inline int arch_cache_line_size(void)
149{
150 char size[32];
151 int fd, ret;
152
153 fd = open(CACHE_LINE_FILE, O_RDONLY);
154 if (fd < 0)
155 return -1;
156
157 ret = read(fd, size, sizeof(size));
158
159 close(fd);
160
161 if (ret <= 0)
162 return -1;
163 else
164 return atoi(size);
165}
166
167static inline unsigned long long get_fs_size(const char *path)
168{
169 unsigned long long ret;
170 struct statfs s;
171
172 if (statfs(path, &s) < 0)
173 return -1ULL;
174
175 ret = s.f_bsize;
176 ret *= (unsigned long long) s.f_bfree;
177 return ret;
178}
179
180static inline int os_trim(int fd, unsigned long long start,
181 unsigned long long len)
182{
183 uint64_t range[2];
184
185 range[0] = start;
186 range[1] = len;
187
188 if (!ioctl(fd, BLKDISCARD, range))
189 return 0;
190
191 return errno;
192}
193
194#endif