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