Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | #ifndef FIO_OS_LINUX_H |
2 | #define FIO_OS_LINUX_H | |
3 | ||
cca84643 JA |
4 | #define FIO_OS os_linux |
5 | ||
ebac4655 | 6 | #include <sys/ioctl.h> |
8756e4d4 | 7 | #include <sys/uio.h> |
3c39a379 | 8 | #include <sys/syscall.h> |
250e878a | 9 | #include <sys/sysmacros.h> |
2e3bd4c2 | 10 | #include <sys/vfs.h> |
39b93568 | 11 | #include <sys/mman.h> |
3c39a379 | 12 | #include <unistd.h> |
97fbdfa1 | 13 | #include <fcntl.h> |
3c2d93ed | 14 | #include <errno.h> |
d09913c2 | 15 | #include <sched.h> |
ea421790 | 16 | #include <linux/unistd.h> |
07e5b264 JA |
17 | #include <linux/raw.h> |
18 | #include <linux/major.h> | |
df73aab1 | 19 | #include <byteswap.h> |
ebac4655 | 20 | |
1c764dbe | 21 | #include "./os-linux-syscall.h" |
79a43187 | 22 | #include "binject.h" |
e2e58886 | 23 | #include "../file.h" |
f3de88a7 | 24 | |
ebac4655 JA |
25 | #define FIO_HAVE_CPU_AFFINITY |
26 | #define FIO_HAVE_DISK_UTIL | |
27 | #define FIO_HAVE_SGIO | |
ba4f8923 | 28 | #define FIO_HAVE_IOPRIO |
32ef447a | 29 | #define FIO_HAVE_IOPRIO_CLASS |
22f78b32 | 30 | #define FIO_HAVE_IOSCHED_SWITCH |
2c0ecd28 | 31 | #define FIO_HAVE_ODIRECT |
74b025b0 | 32 | #define FIO_HAVE_HUGETLB |
07e5b264 | 33 | #define FIO_HAVE_RAWBIND |
5e62c22a | 34 | #define FIO_HAVE_BLKTRACE |
f356d01d | 35 | #define FIO_HAVE_PSHARED_MUTEX |
eb7ccf38 | 36 | #define FIO_HAVE_CL_SIZE |
a696fa2a | 37 | #define FIO_HAVE_CGROUPS |
2e3bd4c2 | 38 | #define FIO_HAVE_FS_STAT |
a5f3027c | 39 | #define FIO_HAVE_TRIM |
79a43187 | 40 | #define FIO_HAVE_BINJECT |
47f767c1 | 41 | #define FIO_HAVE_GETTID |
93bcfd20 | 42 | #define FIO_USE_GENERIC_INIT_RANDOM_STATE |
6562685f | 43 | #define FIO_HAVE_PWRITEV2 |
b6959b55 | 44 | |
39b93568 JA |
45 | #ifdef MAP_HUGETLB |
46 | #define FIO_HAVE_MMAP_HUGE | |
47 | #endif | |
48 | ||
dc873b6f | 49 | #define OS_MAP_ANON MAP_ANONYMOUS |
ebac4655 JA |
50 | |
51 | typedef cpu_set_t os_cpu_mask_t; | |
4e78e405 | 52 | |
6dfd46b9 | 53 | typedef struct drand48_data os_random_state_t; |
ebac4655 | 54 | |
67bf9823 | 55 | #ifdef CONFIG_3ARG_AFFINITY |
e8462bd8 JA |
56 | #define fio_setaffinity(pid, cpumask) \ |
57 | sched_setaffinity((pid), sizeof(cpumask), &(cpumask)) | |
ebac4655 JA |
58 | #define fio_getaffinity(pid, ptr) \ |
59 | sched_getaffinity((pid), sizeof(cpu_set_t), (ptr)) | |
67bf9823 | 60 | #elif defined(CONFIG_2ARG_AFFINITY) |
e8462bd8 JA |
61 | #define fio_setaffinity(pid, cpumask) \ |
62 | sched_setaffinity((pid), &(cpumask)) | |
c8f025f8 JA |
63 | #define fio_getaffinity(pid, ptr) \ |
64 | sched_getaffinity((pid), (ptr)) | |
65 | #endif | |
be4ecfdf | 66 | |
1f4c0a4f JA |
67 | #define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask)) |
68 | #define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask)) | |
50b5860b | 69 | #define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask)) |
d004a209 | 70 | #define fio_cpu_count(mask) CPU_COUNT((mask)) |
d2ce18b5 JA |
71 | |
72 | static inline int fio_cpuset_init(os_cpu_mask_t *mask) | |
73 | { | |
74 | CPU_ZERO(mask); | |
75 | return 0; | |
76 | } | |
77 | ||
78 | static inline int fio_cpuset_exit(os_cpu_mask_t *mask) | |
79 | { | |
80 | return 0; | |
81 | } | |
6d459ee7 JA |
82 | |
83 | #define FIO_MAX_CPUS CPU_SETSIZE | |
4d8947de | 84 | |
28727df7 JA |
85 | enum { |
86 | IOPRIO_CLASS_NONE, | |
87 | IOPRIO_CLASS_RT, | |
88 | IOPRIO_CLASS_BE, | |
89 | IOPRIO_CLASS_IDLE, | |
90 | }; | |
91 | ||
92 | enum { | |
93 | IOPRIO_WHO_PROCESS = 1, | |
94 | IOPRIO_WHO_PGRP, | |
95 | IOPRIO_WHO_USER, | |
96 | }; | |
97 | ||
98 | #define IOPRIO_BITS 16 | |
99 | #define IOPRIO_CLASS_SHIFT 13 | |
100 | ||
1767bd34 TK |
101 | #define IOPRIO_MIN_PRIO 0 /* highest priority */ |
102 | #define IOPRIO_MAX_PRIO 7 /* lowest priority */ | |
103 | ||
104 | #define IOPRIO_MIN_PRIO_CLASS 0 | |
105 | #define IOPRIO_MAX_PRIO_CLASS 3 | |
106 | ||
28727df7 | 107 | static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio) |
ebac4655 | 108 | { |
28727df7 JA |
109 | /* |
110 | * If no class is set, assume BE | |
111 | */ | |
112 | if (!ioprio_class) | |
113 | ioprio_class = IOPRIO_CLASS_BE; | |
114 | ||
115 | ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT; | |
ebac4655 JA |
116 | return syscall(__NR_ioprio_set, which, who, ioprio); |
117 | } | |
118 | ||
47f767c1 JA |
119 | static inline int gettid(void) |
120 | { | |
121 | return syscall(__NR_gettid); | |
122 | } | |
123 | ||
3feedc60 JA |
124 | #define SPLICE_DEF_SIZE (64*1024) |
125 | ||
ebac4655 JA |
126 | #ifndef BLKGETSIZE64 |
127 | #define BLKGETSIZE64 _IOR(0x12,114,size_t) | |
128 | #endif | |
129 | ||
e5b401d4 JA |
130 | #ifndef BLKFLSBUF |
131 | #define BLKFLSBUF _IO(0x12,97) | |
132 | #endif | |
133 | ||
a5f3027c JA |
134 | #ifndef BLKDISCARD |
135 | #define BLKDISCARD _IO(0x12,119) | |
136 | #endif | |
137 | ||
e2e58886 | 138 | static inline int blockdev_invalidate_cache(struct fio_file *f) |
e5b401d4 | 139 | { |
ecc314ba | 140 | return ioctl(f->fd, BLKFLSBUF); |
e5b401d4 JA |
141 | } |
142 | ||
ecc314ba | 143 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
ebac4655 | 144 | { |
ecc314ba | 145 | if (!ioctl(f->fd, BLKGETSIZE64, bytes)) |
ebac4655 JA |
146 | return 0; |
147 | ||
148 | return errno; | |
149 | } | |
150 | ||
32cd46a0 JA |
151 | static inline unsigned long long os_phys_mem(void) |
152 | { | |
153 | long pagesize, pages; | |
154 | ||
155 | pagesize = sysconf(_SC_PAGESIZE); | |
156 | pages = sysconf(_SC_PHYS_PAGES); | |
157 | if (pages == -1 || pagesize == -1) | |
158 | return 0; | |
159 | ||
160 | return (unsigned long long) pages * (unsigned long long) pagesize; | |
161 | } | |
162 | ||
6dfd46b9 JA |
163 | static inline void os_random_seed(unsigned long seed, os_random_state_t *rs) |
164 | { | |
165 | srand48_r(seed, rs); | |
166 | } | |
167 | ||
168 | static inline long os_random_long(os_random_state_t *rs) | |
169 | { | |
170 | long val; | |
171 | ||
172 | lrand48_r(rs, &val); | |
173 | return val; | |
174 | } | |
175 | ||
8cc7afa9 | 176 | static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev) |
07e5b264 JA |
177 | { |
178 | struct raw_config_request rq; | |
179 | int fd; | |
180 | ||
181 | if (major(dev) != RAW_MAJOR) | |
8cc7afa9 | 182 | return 1; |
07e5b264 JA |
183 | |
184 | /* | |
185 | * we should be able to find /dev/rawctl or /dev/raw/rawctl | |
186 | */ | |
187 | fd = open("/dev/rawctl", O_RDONLY); | |
188 | if (fd < 0) { | |
189 | fd = open("/dev/raw/rawctl", O_RDONLY); | |
190 | if (fd < 0) | |
8cc7afa9 | 191 | return 1; |
07e5b264 JA |
192 | } |
193 | ||
194 | rq.raw_minor = minor(dev); | |
195 | if (ioctl(fd, RAW_GETBIND, &rq) < 0) { | |
196 | close(fd); | |
8cc7afa9 | 197 | return 1; |
07e5b264 JA |
198 | } |
199 | ||
200 | close(fd); | |
201 | *majdev = rq.block_major; | |
202 | *mindev = rq.block_minor; | |
8cc7afa9 | 203 | return 0; |
07e5b264 JA |
204 | } |
205 | ||
5b6f5c66 | 206 | #ifdef O_NOATIME |
5921e80c | 207 | #define FIO_O_NOATIME O_NOATIME |
5b6f5c66 JA |
208 | #else |
209 | #define FIO_O_NOATIME 0 | |
210 | #endif | |
5921e80c | 211 | |
d01612f3 CM |
212 | #ifdef O_ATOMIC |
213 | #define OS_O_ATOMIC O_ATOMIC | |
214 | #else | |
215 | #define OS_O_ATOMIC 040000000 | |
216 | #endif | |
217 | ||
a1c58075 JA |
218 | #ifdef MADV_REMOVE |
219 | #define FIO_MADV_FREE MADV_REMOVE | |
220 | #endif | |
221 | ||
df73aab1 JA |
222 | #if defined(__builtin_bswap16) |
223 | #define fio_swap16(x) __builtin_bswap16(x) | |
224 | #else | |
ff245192 | 225 | #define fio_swap16(x) __bswap_16(x) |
df73aab1 JA |
226 | #endif |
227 | #if defined(__builtin_bswap32) | |
228 | #define fio_swap32(x) __builtin_bswap32(x) | |
229 | #else | |
ff245192 | 230 | #define fio_swap32(x) __bswap_32(x) |
df73aab1 JA |
231 | #endif |
232 | #if defined(__builtin_bswap64) | |
233 | #define fio_swap64(x) __builtin_bswap64(x) | |
234 | #else | |
ff245192 | 235 | #define fio_swap64(x) __bswap_64(x) |
df73aab1 | 236 | #endif |
ff245192 | 237 | |
eb7ccf38 JA |
238 | #define CACHE_LINE_FILE \ |
239 | "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size" | |
240 | ||
241 | static inline int arch_cache_line_size(void) | |
242 | { | |
243 | char size[32]; | |
244 | int fd, ret; | |
245 | ||
246 | fd = open(CACHE_LINE_FILE, O_RDONLY); | |
247 | if (fd < 0) | |
248 | return -1; | |
249 | ||
250 | ret = read(fd, size, sizeof(size)); | |
ec76f15a JA |
251 | |
252 | close(fd); | |
253 | ||
eb7ccf38 JA |
254 | if (ret <= 0) |
255 | return -1; | |
ec76f15a JA |
256 | else |
257 | return atoi(size); | |
eb7ccf38 JA |
258 | } |
259 | ||
c08ad04c | 260 | static inline unsigned long long get_fs_free_size(const char *path) |
2e3bd4c2 JA |
261 | { |
262 | unsigned long long ret; | |
263 | struct statfs s; | |
264 | ||
265 | if (statfs(path, &s) < 0) | |
266 | return -1ULL; | |
267 | ||
268 | ret = s.f_bsize; | |
269 | ret *= (unsigned long long) s.f_bfree; | |
270 | return ret; | |
271 | } | |
272 | ||
a5f3027c JA |
273 | static inline int os_trim(int fd, unsigned long long start, |
274 | unsigned long long len) | |
275 | { | |
276 | uint64_t range[2]; | |
277 | ||
278 | range[0] = start; | |
279 | range[1] = len; | |
280 | ||
281 | if (!ioctl(fd, BLKDISCARD, range)) | |
282 | return 0; | |
283 | ||
284 | return errno; | |
285 | } | |
286 | ||
7e09a9f1 | 287 | #ifdef CONFIG_SCHED_IDLE |
f2a2ce0e HL |
288 | static inline int fio_set_sched_idle(void) |
289 | { | |
290 | struct sched_param p = { .sched_priority = 0, }; | |
291 | return sched_setscheduler(gettid(), SCHED_IDLE, &p); | |
292 | } | |
7e09a9f1 | 293 | #endif |
f2a2ce0e | 294 | |
37659335 JA |
295 | #ifndef POSIX_FADV_STREAMID |
296 | #define POSIX_FADV_STREAMID 8 | |
297 | #endif | |
298 | ||
299 | #define FIO_HAVE_STREAMID | |
300 | ||
6562685f JA |
301 | #ifndef RWF_HIPRI |
302 | #define RWF_HIPRI 0x00000001 | |
303 | #endif | |
304 | #ifndef RWF_DSYNC | |
305 | #define RWF_DSYNC 0x00000002 | |
306 | #endif | |
307 | #ifndef RWF_SYNC | |
308 | #define RWF_SYNC 0x00000004 | |
309 | #endif | |
310 | ||
311 | #ifndef CONFIG_PWRITEV2 | |
312 | #ifdef __NR_preadv2 | |
313 | static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l, | |
314 | off_t offset) | |
315 | { | |
316 | *pos_l = offset & 0xffffffff; | |
317 | *pos_h = ((uint64_t) offset) >> 32; | |
318 | ||
319 | } | |
320 | static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, | |
321 | off_t offset, unsigned int flags) | |
322 | { | |
323 | unsigned long pos_l, pos_h; | |
324 | ||
325 | make_pos_h_l(&pos_h, &pos_l, offset); | |
326 | return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags); | |
327 | } | |
328 | static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, | |
329 | off_t offset, unsigned int flags) | |
330 | { | |
331 | unsigned long pos_l, pos_h; | |
332 | ||
333 | make_pos_h_l(&pos_h, &pos_l, offset); | |
334 | return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags); | |
335 | } | |
336 | #else | |
337 | static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, | |
338 | off_t offset, unsigned int flags) | |
339 | { | |
340 | errno = ENOSYS; | |
341 | return -1; | |
342 | } | |
343 | static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, | |
344 | off_t offset, unsigned int flags) | |
345 | { | |
346 | errno = ENOSYS; | |
347 | return -1; | |
348 | } | |
349 | #endif /* __NR_preadv2 */ | |
350 | #endif /* CONFIG_PWRITEV2 */ | |
351 | ||
ebac4655 | 352 | #endif |