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