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