Commit | Line | Data |
---|---|---|
ebac4655 JA |
1 | #ifndef FIO_OS_LINUX_H |
2 | #define FIO_OS_LINUX_H | |
3 | ||
910f378a BVA |
4 | #ifdef __ANDROID__ |
5 | #define FIO_OS os_android | |
6 | #else | |
cca84643 | 7 | #define FIO_OS os_linux |
910f378a | 8 | #endif |
cca84643 | 9 | |
ebac4655 | 10 | #include <sys/ioctl.h> |
8756e4d4 | 11 | #include <sys/uio.h> |
3c39a379 | 12 | #include <sys/syscall.h> |
250e878a | 13 | #include <sys/sysmacros.h> |
2e3bd4c2 | 14 | #include <sys/vfs.h> |
39b93568 | 15 | #include <sys/mman.h> |
3c39a379 | 16 | #include <unistd.h> |
97fbdfa1 | 17 | #include <fcntl.h> |
3c2d93ed | 18 | #include <errno.h> |
d09913c2 | 19 | #include <sched.h> |
ea421790 | 20 | #include <linux/unistd.h> |
07e5b264 | 21 | #include <linux/major.h> |
1894d171 TK |
22 | #include <linux/fs.h> |
23 | #include <scsi/sg.h> | |
e69d8385 | 24 | #include <asm/byteorder.h> |
910f378a BVA |
25 | #ifdef __ANDROID__ |
26 | #include "os-ashmem.h" | |
27 | #define FIO_NO_HAVE_SHM_H | |
28 | #endif | |
ebac4655 | 29 | |
58828d07 SW |
30 | #ifdef ARCH_HAVE_CRC_CRYPTO |
31 | #include <sys/auxv.h> | |
3721c7fe SW |
32 | #ifndef HWCAP_PMULL |
33 | #define HWCAP_PMULL (1 << 4) | |
34 | #endif /* HWCAP_PMULL */ | |
58828d07 SW |
35 | #ifndef HWCAP_CRC32 |
36 | #define HWCAP_CRC32 (1 << 7) | |
37 | #endif /* HWCAP_CRC32 */ | |
38 | #endif /* ARCH_HAVE_CRC_CRYPTO */ | |
39 | ||
1c764dbe | 40 | #include "./os-linux-syscall.h" |
e2e58886 | 41 | #include "../file.h" |
f3de88a7 | 42 | |
7922a7b7 OM |
43 | #ifndef __has_builtin // Optional of course. |
44 | #define __has_builtin(x) 0 // Compatibility with non-clang compilers. | |
45 | #endif | |
46 | ||
ebac4655 JA |
47 | #define FIO_HAVE_CPU_AFFINITY |
48 | #define FIO_HAVE_DISK_UTIL | |
49 | #define FIO_HAVE_SGIO | |
ba4f8923 | 50 | #define FIO_HAVE_IOPRIO |
32ef447a | 51 | #define FIO_HAVE_IOPRIO_CLASS |
22f78b32 | 52 | #define FIO_HAVE_IOSCHED_SWITCH |
2c0ecd28 | 53 | #define FIO_HAVE_ODIRECT |
74b025b0 | 54 | #define FIO_HAVE_HUGETLB |
5e62c22a | 55 | #define FIO_HAVE_BLKTRACE |
eb7ccf38 | 56 | #define FIO_HAVE_CL_SIZE |
a696fa2a | 57 | #define FIO_HAVE_CGROUPS |
2e3bd4c2 | 58 | #define FIO_HAVE_FS_STAT |
a5f3027c | 59 | #define FIO_HAVE_TRIM |
47f767c1 | 60 | #define FIO_HAVE_GETTID |
93bcfd20 | 61 | #define FIO_USE_GENERIC_INIT_RANDOM_STATE |
e69d8385 | 62 | #define FIO_HAVE_BYTEORDER_FUNCS |
6562685f | 63 | #define FIO_HAVE_PWRITEV2 |
97900ebf | 64 | #define FIO_HAVE_SHM_ATTACH_REMOVED |
b6959b55 | 65 | |
39b93568 JA |
66 | #ifdef MAP_HUGETLB |
67 | #define FIO_HAVE_MMAP_HUGE | |
68 | #endif | |
69 | ||
dc873b6f | 70 | #define OS_MAP_ANON MAP_ANONYMOUS |
ebac4655 | 71 | |
4db9ac71 | 72 | #define FIO_EXT_ENG_DIR "/usr/local/lib/fio" |
5a8a6a03 | 73 | |
ebac4655 | 74 | typedef cpu_set_t os_cpu_mask_t; |
4e78e405 | 75 | |
67bf9823 | 76 | #ifdef CONFIG_3ARG_AFFINITY |
e8462bd8 JA |
77 | #define fio_setaffinity(pid, cpumask) \ |
78 | sched_setaffinity((pid), sizeof(cpumask), &(cpumask)) | |
ebac4655 JA |
79 | #define fio_getaffinity(pid, ptr) \ |
80 | sched_getaffinity((pid), sizeof(cpu_set_t), (ptr)) | |
67bf9823 | 81 | #elif defined(CONFIG_2ARG_AFFINITY) |
e8462bd8 JA |
82 | #define fio_setaffinity(pid, cpumask) \ |
83 | sched_setaffinity((pid), &(cpumask)) | |
c8f025f8 JA |
84 | #define fio_getaffinity(pid, ptr) \ |
85 | sched_getaffinity((pid), (ptr)) | |
86 | #endif | |
be4ecfdf | 87 | |
e9d2a04d TK |
88 | #ifdef CONFIG_PTHREAD_GETAFFINITY |
89 | #define FIO_HAVE_GET_THREAD_AFFINITY | |
874d55e5 JA |
90 | #define fio_get_thread_affinity(mask) \ |
91 | pthread_getaffinity_np(pthread_self(), sizeof(mask), &(mask)) | |
e9d2a04d | 92 | #endif |
874d55e5 | 93 | |
6e0cefdc BVA |
94 | #define fio_cpu_clear(mask, cpu) CPU_CLR((cpu), (mask)) |
95 | #define fio_cpu_set(mask, cpu) CPU_SET((cpu), (mask)) | |
52fd65f4 | 96 | #define fio_cpu_isset(mask, cpu) (CPU_ISSET((cpu), (mask)) != 0) |
d004a209 | 97 | #define fio_cpu_count(mask) CPU_COUNT((mask)) |
d2ce18b5 JA |
98 | |
99 | static inline int fio_cpuset_init(os_cpu_mask_t *mask) | |
100 | { | |
101 | CPU_ZERO(mask); | |
102 | return 0; | |
103 | } | |
104 | ||
105 | static inline int fio_cpuset_exit(os_cpu_mask_t *mask) | |
106 | { | |
107 | return 0; | |
108 | } | |
6d459ee7 JA |
109 | |
110 | #define FIO_MAX_CPUS CPU_SETSIZE | |
4d8947de | 111 | |
28727df7 JA |
112 | enum { |
113 | IOPRIO_CLASS_NONE, | |
114 | IOPRIO_CLASS_RT, | |
115 | IOPRIO_CLASS_BE, | |
116 | IOPRIO_CLASS_IDLE, | |
117 | }; | |
118 | ||
119 | enum { | |
120 | IOPRIO_WHO_PROCESS = 1, | |
121 | IOPRIO_WHO_PGRP, | |
122 | IOPRIO_WHO_USER, | |
123 | }; | |
124 | ||
125 | #define IOPRIO_BITS 16 | |
126 | #define IOPRIO_CLASS_SHIFT 13 | |
127 | ||
b3048c1b DLM |
128 | #define IOPRIO_HINT_BITS 10 |
129 | #define IOPRIO_HINT_SHIFT 3 | |
130 | ||
1767bd34 TK |
131 | #define IOPRIO_MIN_PRIO 0 /* highest priority */ |
132 | #define IOPRIO_MAX_PRIO 7 /* lowest priority */ | |
133 | ||
134 | #define IOPRIO_MIN_PRIO_CLASS 0 | |
135 | #define IOPRIO_MAX_PRIO_CLASS 3 | |
136 | ||
b3048c1b DLM |
137 | #define IOPRIO_MIN_PRIO_HINT 0 |
138 | #define IOPRIO_MAX_PRIO_HINT ((1 << IOPRIO_HINT_BITS) - 1) | |
139 | ||
d4d503a5 DLM |
140 | #define ioprio_class(ioprio) ((ioprio) >> IOPRIO_CLASS_SHIFT) |
141 | #define ioprio(ioprio) ((ioprio) & IOPRIO_MAX_PRIO) | |
b3048c1b DLM |
142 | #define ioprio_hint(ioprio) \ |
143 | (((ioprio) >> IOPRIO_HINT_SHIFT) & IOPRIO_MAX_PRIO_HINT) | |
d4d503a5 | 144 | |
b3048c1b | 145 | static inline int ioprio_value(int ioprio_class, int ioprio, int ioprio_hint) |
ebac4655 | 146 | { |
28727df7 JA |
147 | /* |
148 | * If no class is set, assume BE | |
149 | */ | |
8d6e8d99 DLM |
150 | if (!ioprio_class) |
151 | ioprio_class = IOPRIO_CLASS_BE; | |
152 | ||
b3048c1b DLM |
153 | return (ioprio_class << IOPRIO_CLASS_SHIFT) | |
154 | (ioprio_hint << IOPRIO_HINT_SHIFT) | | |
155 | ioprio; | |
8d6e8d99 | 156 | } |
28727df7 | 157 | |
03ec570f DLM |
158 | static inline bool ioprio_value_is_class_rt(unsigned int priority) |
159 | { | |
d4d503a5 | 160 | return ioprio_class(priority) == IOPRIO_CLASS_RT; |
03ec570f DLM |
161 | } |
162 | ||
b3048c1b DLM |
163 | static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio, |
164 | int ioprio_hint) | |
8d6e8d99 DLM |
165 | { |
166 | return syscall(__NR_ioprio_set, which, who, | |
b3048c1b | 167 | ioprio_value(ioprio_class, ioprio, ioprio_hint)); |
ebac4655 JA |
168 | } |
169 | ||
de5ed0e4 | 170 | #ifndef CONFIG_HAVE_GETTID |
47f767c1 JA |
171 | static inline int gettid(void) |
172 | { | |
173 | return syscall(__NR_gettid); | |
174 | } | |
de5ed0e4 | 175 | #endif |
47f767c1 | 176 | |
3feedc60 JA |
177 | #define SPLICE_DEF_SIZE (64*1024) |
178 | ||
ebac4655 JA |
179 | #ifndef BLKGETSIZE64 |
180 | #define BLKGETSIZE64 _IOR(0x12,114,size_t) | |
181 | #endif | |
182 | ||
e5b401d4 JA |
183 | #ifndef BLKFLSBUF |
184 | #define BLKFLSBUF _IO(0x12,97) | |
185 | #endif | |
186 | ||
a5f3027c JA |
187 | #ifndef BLKDISCARD |
188 | #define BLKDISCARD _IO(0x12,119) | |
189 | #endif | |
190 | ||
e2e58886 | 191 | static inline int blockdev_invalidate_cache(struct fio_file *f) |
e5b401d4 | 192 | { |
ecc314ba | 193 | return ioctl(f->fd, BLKFLSBUF); |
e5b401d4 JA |
194 | } |
195 | ||
ecc314ba | 196 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
ebac4655 | 197 | { |
ecc314ba | 198 | if (!ioctl(f->fd, BLKGETSIZE64, bytes)) |
ebac4655 JA |
199 | return 0; |
200 | ||
201 | return errno; | |
202 | } | |
203 | ||
32cd46a0 JA |
204 | static inline unsigned long long os_phys_mem(void) |
205 | { | |
206 | long pagesize, pages; | |
207 | ||
208 | pagesize = sysconf(_SC_PAGESIZE); | |
209 | pages = sysconf(_SC_PHYS_PAGES); | |
210 | if (pages == -1 || pagesize == -1) | |
211 | return 0; | |
212 | ||
213 | return (unsigned long long) pages * (unsigned long long) pagesize; | |
214 | } | |
215 | ||
5b6f5c66 | 216 | #ifdef O_NOATIME |
5921e80c | 217 | #define FIO_O_NOATIME O_NOATIME |
5b6f5c66 JA |
218 | #else |
219 | #define FIO_O_NOATIME 0 | |
220 | #endif | |
5921e80c | 221 | |
a1c58075 JA |
222 | #ifdef MADV_REMOVE |
223 | #define FIO_MADV_FREE MADV_REMOVE | |
224 | #endif | |
225 | ||
7922a7b7 OM |
226 | /* Check for GCC or Clang byte swap intrinsics */ |
227 | #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \ | |
228 | && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \ | |
229 | || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */ | |
df73aab1 | 230 | #define fio_swap16(x) __builtin_bswap16(x) |
df73aab1 | 231 | #define fio_swap32(x) __builtin_bswap32(x) |
df73aab1 JA |
232 | #define fio_swap64(x) __builtin_bswap64(x) |
233 | #else | |
7922a7b7 OM |
234 | #include <byteswap.h> |
235 | #define fio_swap16(x) bswap_16(x) | |
236 | #define fio_swap32(x) bswap_32(x) | |
237 | #define fio_swap64(x) bswap_64(x) | |
238 | #endif /* fio_swapN */ | |
ff245192 | 239 | |
eb7ccf38 JA |
240 | #define CACHE_LINE_FILE \ |
241 | "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size" | |
242 | ||
243 | static inline int arch_cache_line_size(void) | |
244 | { | |
245 | char size[32]; | |
246 | int fd, ret; | |
247 | ||
248 | fd = open(CACHE_LINE_FILE, O_RDONLY); | |
249 | if (fd < 0) | |
250 | return -1; | |
251 | ||
252 | ret = read(fd, size, sizeof(size)); | |
ec76f15a JA |
253 | |
254 | close(fd); | |
255 | ||
eb7ccf38 JA |
256 | if (ret <= 0) |
257 | return -1; | |
ec76f15a JA |
258 | else |
259 | return atoi(size); | |
eb7ccf38 JA |
260 | } |
261 | ||
c08ad04c | 262 | static inline unsigned long long get_fs_free_size(const char *path) |
2e3bd4c2 JA |
263 | { |
264 | unsigned long long ret; | |
265 | struct statfs s; | |
266 | ||
267 | if (statfs(path, &s) < 0) | |
268 | return -1ULL; | |
269 | ||
270 | ret = s.f_bsize; | |
271 | ret *= (unsigned long long) s.f_bfree; | |
272 | return ret; | |
273 | } | |
274 | ||
496b1f9e | 275 | static inline int os_trim(struct fio_file *f, unsigned long long start, |
a5f3027c JA |
276 | unsigned long long len) |
277 | { | |
278 | uint64_t range[2]; | |
279 | ||
280 | range[0] = start; | |
281 | range[1] = len; | |
282 | ||
496b1f9e | 283 | if (!ioctl(f->fd, BLKDISCARD, range)) |
a5f3027c JA |
284 | return 0; |
285 | ||
286 | return errno; | |
287 | } | |
288 | ||
7e09a9f1 | 289 | #ifdef CONFIG_SCHED_IDLE |
f2a2ce0e HL |
290 | static inline int fio_set_sched_idle(void) |
291 | { | |
292 | struct sched_param p = { .sched_priority = 0, }; | |
293 | return sched_setscheduler(gettid(), SCHED_IDLE, &p); | |
294 | } | |
7e09a9f1 | 295 | #endif |
f2a2ce0e | 296 | |
ae8e559e JA |
297 | #ifndef F_GET_RW_HINT |
298 | #ifndef F_LINUX_SPECIFIC_BASE | |
299 | #define F_LINUX_SPECIFIC_BASE 1024 | |
300 | #endif | |
301 | #define F_GET_RW_HINT (F_LINUX_SPECIFIC_BASE + 11) | |
302 | #define F_SET_RW_HINT (F_LINUX_SPECIFIC_BASE + 12) | |
bd553af6 JA |
303 | #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13) |
304 | #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) | |
ae8e559e JA |
305 | #endif |
306 | ||
307 | #ifndef RWH_WRITE_LIFE_NONE | |
bd553af6 JA |
308 | #define RWH_WRITE_LIFE_NOT_SET 0 |
309 | #define RWH_WRITE_LIFE_NONE 1 | |
310 | #define RWH_WRITE_LIFE_SHORT 2 | |
311 | #define RWH_WRITE_LIFE_MEDIUM 3 | |
312 | #define RWH_WRITE_LIFE_LONG 4 | |
313 | #define RWH_WRITE_LIFE_EXTREME 5 | |
37659335 JA |
314 | #endif |
315 | ||
ae8e559e | 316 | #define FIO_HAVE_WRITE_HINT |
37659335 | 317 | |
6562685f JA |
318 | #ifndef RWF_HIPRI |
319 | #define RWF_HIPRI 0x00000001 | |
320 | #endif | |
321 | #ifndef RWF_DSYNC | |
322 | #define RWF_DSYNC 0x00000002 | |
323 | #endif | |
324 | #ifndef RWF_SYNC | |
325 | #define RWF_SYNC 0x00000004 | |
326 | #endif | |
7d42e66e KK |
327 | #ifndef RWF_NOWAIT |
328 | #define RWF_NOWAIT 0x00000008 | |
329 | #endif | |
6562685f | 330 | |
4a87b584 | 331 | #ifndef RWF_UNCACHED |
8beee62d | 332 | #define RWF_UNCACHED 0x00000040 |
4a87b584 JA |
333 | #endif |
334 | ||
72cd4146 JA |
335 | #ifndef RWF_WRITE_LIFE_SHIFT |
336 | #define RWF_WRITE_LIFE_SHIFT 4 | |
337 | #define RWF_WRITE_LIFE_SHORT (1 << RWF_WRITE_LIFE_SHIFT) | |
338 | #define RWF_WRITE_LIFE_MEDIUM (2 << RWF_WRITE_LIFE_SHIFT) | |
339 | #define RWF_WRITE_LIFE_LONG (3 << RWF_WRITE_LIFE_SHIFT) | |
340 | #define RWF_WRITE_LIFE_EXTREME (4 << RWF_WRITE_LIFE_SHIFT) | |
341 | #endif | |
342 | ||
6562685f JA |
343 | #ifndef CONFIG_PWRITEV2 |
344 | #ifdef __NR_preadv2 | |
345 | static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l, | |
346 | off_t offset) | |
347 | { | |
7b5c648f | 348 | #if BITS_PER_LONG == 64 |
7b5c648f JA |
349 | *pos_l = offset; |
350 | *pos_h = 0; | |
351 | #else | |
6562685f JA |
352 | *pos_l = offset & 0xffffffff; |
353 | *pos_h = ((uint64_t) offset) >> 32; | |
7b5c648f | 354 | #endif |
6562685f JA |
355 | } |
356 | static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, | |
357 | off_t offset, unsigned int flags) | |
358 | { | |
359 | unsigned long pos_l, pos_h; | |
360 | ||
361 | make_pos_h_l(&pos_h, &pos_l, offset); | |
362 | return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags); | |
363 | } | |
364 | static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, | |
365 | off_t offset, unsigned int flags) | |
366 | { | |
367 | unsigned long pos_l, pos_h; | |
368 | ||
369 | make_pos_h_l(&pos_h, &pos_l, offset); | |
370 | return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags); | |
371 | } | |
372 | #else | |
373 | static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, | |
374 | off_t offset, unsigned int flags) | |
375 | { | |
376 | errno = ENOSYS; | |
377 | return -1; | |
378 | } | |
379 | static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, | |
380 | off_t offset, unsigned int flags) | |
381 | { | |
382 | errno = ENOSYS; | |
383 | return -1; | |
384 | } | |
385 | #endif /* __NR_preadv2 */ | |
386 | #endif /* CONFIG_PWRITEV2 */ | |
387 | ||
0cfe2089 TK |
388 | static inline int shm_attach_to_open_removed(void) |
389 | { | |
390 | return 1; | |
391 | } | |
392 | ||
2c3e17be SW |
393 | #ifdef CONFIG_LINUX_FALLOCATE |
394 | #define FIO_HAVE_NATIVE_FALLOCATE | |
395 | static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, | |
396 | uint64_t len) | |
397 | { | |
398 | int ret; | |
e69dfc2b | 399 | ret = fallocate(f->fd, 0, offset, len); |
2c3e17be SW |
400 | if (ret == 0) |
401 | return true; | |
402 | ||
403 | /* Work around buggy old glibc versions... */ | |
404 | if (ret > 0) | |
405 | errno = ret; | |
406 | ||
407 | return false; | |
408 | } | |
409 | #endif | |
410 | ||
58828d07 SW |
411 | #define FIO_HAVE_CPU_HAS |
412 | static inline bool os_cpu_has(cpu_features feature) | |
413 | { | |
414 | bool have_feature; | |
415 | unsigned long fio_unused hwcap; | |
416 | ||
417 | switch (feature) { | |
418 | #ifdef ARCH_HAVE_CRC_CRYPTO | |
419 | case CPU_ARM64_CRC32C: | |
420 | hwcap = getauxval(AT_HWCAP); | |
3721c7fe SW |
421 | have_feature = (hwcap & (HWCAP_PMULL | HWCAP_CRC32)) == |
422 | (HWCAP_PMULL | HWCAP_CRC32); | |
58828d07 SW |
423 | break; |
424 | #endif | |
425 | default: | |
426 | have_feature = false; | |
427 | } | |
428 | ||
429 | return have_feature; | |
430 | } | |
431 | ||
ebac4655 | 432 | #endif |