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