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 | ||
28727df7 | 121 | static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio) |
ebac4655 | 122 | { |
28727df7 JA |
123 | /* |
124 | * If no class is set, assume BE | |
125 | */ | |
126 | if (!ioprio_class) | |
127 | ioprio_class = IOPRIO_CLASS_BE; | |
128 | ||
129 | ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT; | |
ebac4655 JA |
130 | return syscall(__NR_ioprio_set, which, who, ioprio); |
131 | } | |
132 | ||
de5ed0e4 | 133 | #ifndef CONFIG_HAVE_GETTID |
47f767c1 JA |
134 | static inline int gettid(void) |
135 | { | |
136 | return syscall(__NR_gettid); | |
137 | } | |
de5ed0e4 | 138 | #endif |
47f767c1 | 139 | |
3feedc60 JA |
140 | #define SPLICE_DEF_SIZE (64*1024) |
141 | ||
ebac4655 JA |
142 | #ifndef BLKGETSIZE64 |
143 | #define BLKGETSIZE64 _IOR(0x12,114,size_t) | |
144 | #endif | |
145 | ||
e5b401d4 JA |
146 | #ifndef BLKFLSBUF |
147 | #define BLKFLSBUF _IO(0x12,97) | |
148 | #endif | |
149 | ||
a5f3027c JA |
150 | #ifndef BLKDISCARD |
151 | #define BLKDISCARD _IO(0x12,119) | |
152 | #endif | |
153 | ||
e2e58886 | 154 | static inline int blockdev_invalidate_cache(struct fio_file *f) |
e5b401d4 | 155 | { |
ecc314ba | 156 | return ioctl(f->fd, BLKFLSBUF); |
e5b401d4 JA |
157 | } |
158 | ||
ecc314ba | 159 | static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes) |
ebac4655 | 160 | { |
ecc314ba | 161 | if (!ioctl(f->fd, BLKGETSIZE64, bytes)) |
ebac4655 JA |
162 | return 0; |
163 | ||
164 | return errno; | |
165 | } | |
166 | ||
32cd46a0 JA |
167 | static inline unsigned long long os_phys_mem(void) |
168 | { | |
169 | long pagesize, pages; | |
170 | ||
171 | pagesize = sysconf(_SC_PAGESIZE); | |
172 | pages = sysconf(_SC_PHYS_PAGES); | |
173 | if (pages == -1 || pagesize == -1) | |
174 | return 0; | |
175 | ||
176 | return (unsigned long long) pages * (unsigned long long) pagesize; | |
177 | } | |
178 | ||
5b6f5c66 | 179 | #ifdef O_NOATIME |
5921e80c | 180 | #define FIO_O_NOATIME O_NOATIME |
5b6f5c66 JA |
181 | #else |
182 | #define FIO_O_NOATIME 0 | |
183 | #endif | |
5921e80c | 184 | |
d01612f3 CM |
185 | #ifdef O_ATOMIC |
186 | #define OS_O_ATOMIC O_ATOMIC | |
187 | #else | |
188 | #define OS_O_ATOMIC 040000000 | |
189 | #endif | |
190 | ||
a1c58075 JA |
191 | #ifdef MADV_REMOVE |
192 | #define FIO_MADV_FREE MADV_REMOVE | |
193 | #endif | |
194 | ||
7922a7b7 OM |
195 | /* Check for GCC or Clang byte swap intrinsics */ |
196 | #if (__has_builtin(__builtin_bswap16) && __has_builtin(__builtin_bswap32) \ | |
197 | && __has_builtin(__builtin_bswap64)) || (__GNUC__ > 4 \ | |
198 | || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) /* fio_swapN */ | |
df73aab1 | 199 | #define fio_swap16(x) __builtin_bswap16(x) |
df73aab1 | 200 | #define fio_swap32(x) __builtin_bswap32(x) |
df73aab1 JA |
201 | #define fio_swap64(x) __builtin_bswap64(x) |
202 | #else | |
7922a7b7 OM |
203 | #include <byteswap.h> |
204 | #define fio_swap16(x) bswap_16(x) | |
205 | #define fio_swap32(x) bswap_32(x) | |
206 | #define fio_swap64(x) bswap_64(x) | |
207 | #endif /* fio_swapN */ | |
ff245192 | 208 | |
eb7ccf38 JA |
209 | #define CACHE_LINE_FILE \ |
210 | "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size" | |
211 | ||
212 | static inline int arch_cache_line_size(void) | |
213 | { | |
214 | char size[32]; | |
215 | int fd, ret; | |
216 | ||
217 | fd = open(CACHE_LINE_FILE, O_RDONLY); | |
218 | if (fd < 0) | |
219 | return -1; | |
220 | ||
221 | ret = read(fd, size, sizeof(size)); | |
ec76f15a JA |
222 | |
223 | close(fd); | |
224 | ||
eb7ccf38 JA |
225 | if (ret <= 0) |
226 | return -1; | |
ec76f15a JA |
227 | else |
228 | return atoi(size); | |
eb7ccf38 JA |
229 | } |
230 | ||
d1419c4f TK |
231 | #ifdef __powerpc64__ |
232 | #define FIO_HAVE_CPU_ONLINE_SYSCONF | |
233 | static inline unsigned int cpus_online(void) | |
234 | { | |
235 | return sysconf(_SC_NPROCESSORS_CONF); | |
236 | } | |
237 | #endif | |
238 | ||
c08ad04c | 239 | static inline unsigned long long get_fs_free_size(const char *path) |
2e3bd4c2 JA |
240 | { |
241 | unsigned long long ret; | |
242 | struct statfs s; | |
243 | ||
244 | if (statfs(path, &s) < 0) | |
245 | return -1ULL; | |
246 | ||
247 | ret = s.f_bsize; | |
248 | ret *= (unsigned long long) s.f_bfree; | |
249 | return ret; | |
250 | } | |
251 | ||
496b1f9e | 252 | static inline int os_trim(struct fio_file *f, unsigned long long start, |
a5f3027c JA |
253 | unsigned long long len) |
254 | { | |
255 | uint64_t range[2]; | |
256 | ||
257 | range[0] = start; | |
258 | range[1] = len; | |
259 | ||
496b1f9e | 260 | if (!ioctl(f->fd, BLKDISCARD, range)) |
a5f3027c JA |
261 | return 0; |
262 | ||
263 | return errno; | |
264 | } | |
265 | ||
7e09a9f1 | 266 | #ifdef CONFIG_SCHED_IDLE |
f2a2ce0e HL |
267 | static inline int fio_set_sched_idle(void) |
268 | { | |
269 | struct sched_param p = { .sched_priority = 0, }; | |
270 | return sched_setscheduler(gettid(), SCHED_IDLE, &p); | |
271 | } | |
7e09a9f1 | 272 | #endif |
f2a2ce0e | 273 | |
ae8e559e JA |
274 | #ifndef F_GET_RW_HINT |
275 | #ifndef F_LINUX_SPECIFIC_BASE | |
276 | #define F_LINUX_SPECIFIC_BASE 1024 | |
277 | #endif | |
278 | #define F_GET_RW_HINT (F_LINUX_SPECIFIC_BASE + 11) | |
279 | #define F_SET_RW_HINT (F_LINUX_SPECIFIC_BASE + 12) | |
bd553af6 JA |
280 | #define F_GET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 13) |
281 | #define F_SET_FILE_RW_HINT (F_LINUX_SPECIFIC_BASE + 14) | |
ae8e559e JA |
282 | #endif |
283 | ||
284 | #ifndef RWH_WRITE_LIFE_NONE | |
bd553af6 JA |
285 | #define RWH_WRITE_LIFE_NOT_SET 0 |
286 | #define RWH_WRITE_LIFE_NONE 1 | |
287 | #define RWH_WRITE_LIFE_SHORT 2 | |
288 | #define RWH_WRITE_LIFE_MEDIUM 3 | |
289 | #define RWH_WRITE_LIFE_LONG 4 | |
290 | #define RWH_WRITE_LIFE_EXTREME 5 | |
37659335 JA |
291 | #endif |
292 | ||
ae8e559e | 293 | #define FIO_HAVE_WRITE_HINT |
37659335 | 294 | |
6562685f JA |
295 | #ifndef RWF_HIPRI |
296 | #define RWF_HIPRI 0x00000001 | |
297 | #endif | |
298 | #ifndef RWF_DSYNC | |
299 | #define RWF_DSYNC 0x00000002 | |
300 | #endif | |
301 | #ifndef RWF_SYNC | |
302 | #define RWF_SYNC 0x00000004 | |
303 | #endif | |
7d42e66e KK |
304 | #ifndef RWF_NOWAIT |
305 | #define RWF_NOWAIT 0x00000008 | |
306 | #endif | |
6562685f | 307 | |
4a87b584 | 308 | #ifndef RWF_UNCACHED |
8beee62d | 309 | #define RWF_UNCACHED 0x00000040 |
4a87b584 JA |
310 | #endif |
311 | ||
72cd4146 JA |
312 | #ifndef RWF_WRITE_LIFE_SHIFT |
313 | #define RWF_WRITE_LIFE_SHIFT 4 | |
314 | #define RWF_WRITE_LIFE_SHORT (1 << RWF_WRITE_LIFE_SHIFT) | |
315 | #define RWF_WRITE_LIFE_MEDIUM (2 << RWF_WRITE_LIFE_SHIFT) | |
316 | #define RWF_WRITE_LIFE_LONG (3 << RWF_WRITE_LIFE_SHIFT) | |
317 | #define RWF_WRITE_LIFE_EXTREME (4 << RWF_WRITE_LIFE_SHIFT) | |
318 | #endif | |
319 | ||
6562685f JA |
320 | #ifndef CONFIG_PWRITEV2 |
321 | #ifdef __NR_preadv2 | |
322 | static inline void make_pos_h_l(unsigned long *pos_h, unsigned long *pos_l, | |
323 | off_t offset) | |
324 | { | |
7b5c648f | 325 | #if BITS_PER_LONG == 64 |
7b5c648f JA |
326 | *pos_l = offset; |
327 | *pos_h = 0; | |
328 | #else | |
6562685f JA |
329 | *pos_l = offset & 0xffffffff; |
330 | *pos_h = ((uint64_t) offset) >> 32; | |
7b5c648f | 331 | #endif |
6562685f JA |
332 | } |
333 | static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, | |
334 | off_t offset, unsigned int flags) | |
335 | { | |
336 | unsigned long pos_l, pos_h; | |
337 | ||
338 | make_pos_h_l(&pos_h, &pos_l, offset); | |
339 | return syscall(__NR_preadv2, fd, iov, iovcnt, pos_l, pos_h, flags); | |
340 | } | |
341 | static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, | |
342 | off_t offset, unsigned int flags) | |
343 | { | |
344 | unsigned long pos_l, pos_h; | |
345 | ||
346 | make_pos_h_l(&pos_h, &pos_l, offset); | |
347 | return syscall(__NR_pwritev2, fd, iov, iovcnt, pos_l, pos_h, flags); | |
348 | } | |
349 | #else | |
350 | static inline ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, | |
351 | off_t offset, unsigned int flags) | |
352 | { | |
353 | errno = ENOSYS; | |
354 | return -1; | |
355 | } | |
356 | static inline ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, | |
357 | off_t offset, unsigned int flags) | |
358 | { | |
359 | errno = ENOSYS; | |
360 | return -1; | |
361 | } | |
362 | #endif /* __NR_preadv2 */ | |
363 | #endif /* CONFIG_PWRITEV2 */ | |
364 | ||
0cfe2089 TK |
365 | static inline int shm_attach_to_open_removed(void) |
366 | { | |
367 | return 1; | |
368 | } | |
369 | ||
2c3e17be SW |
370 | #ifdef CONFIG_LINUX_FALLOCATE |
371 | #define FIO_HAVE_NATIVE_FALLOCATE | |
372 | static inline bool fio_fallocate(struct fio_file *f, uint64_t offset, | |
373 | uint64_t len) | |
374 | { | |
375 | int ret; | |
e69dfc2b | 376 | ret = fallocate(f->fd, 0, offset, len); |
2c3e17be SW |
377 | if (ret == 0) |
378 | return true; | |
379 | ||
380 | /* Work around buggy old glibc versions... */ | |
381 | if (ret > 0) | |
382 | errno = ret; | |
383 | ||
384 | return false; | |
385 | } | |
386 | #endif | |
387 | ||
58828d07 SW |
388 | #define FIO_HAVE_CPU_HAS |
389 | static inline bool os_cpu_has(cpu_features feature) | |
390 | { | |
391 | bool have_feature; | |
392 | unsigned long fio_unused hwcap; | |
393 | ||
394 | switch (feature) { | |
395 | #ifdef ARCH_HAVE_CRC_CRYPTO | |
396 | case CPU_ARM64_CRC32C: | |
397 | hwcap = getauxval(AT_HWCAP); | |
398 | have_feature = (hwcap & HWCAP_CRC32) != 0; | |
399 | break; | |
400 | #endif | |
401 | default: | |
402 | have_feature = false; | |
403 | } | |
404 | ||
405 | return have_feature; | |
406 | } | |
407 | ||
ebac4655 | 408 | #endif |