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