Initial suppor for sync_file_range()
[fio.git] / os / os-linux.h
1 #ifndef FIO_OS_LINUX_H
2 #define FIO_OS_LINUX_H
3
4 #include <sys/ioctl.h>
5 #include <sys/uio.h>
6 #include <sys/syscall.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <sched.h>
11 #include <linux/unistd.h>
12 #include <linux/raw.h>
13 #include <linux/major.h>
14
15 #include "indirect.h"
16
17 #define FIO_HAVE_LIBAIO
18 #define FIO_HAVE_POSIXAIO
19 #define FIO_HAVE_FADVISE
20 #define FIO_HAVE_CPU_AFFINITY
21 #define FIO_HAVE_DISK_UTIL
22 #define FIO_HAVE_SGIO
23 #define FIO_HAVE_IOPRIO
24 #define FIO_HAVE_SPLICE
25 #define FIO_HAVE_IOSCHED_SWITCH
26 #define FIO_HAVE_ODIRECT
27 #define FIO_HAVE_HUGETLB
28 #define FIO_HAVE_RAWBIND
29 #define FIO_HAVE_BLKTRACE
30 #define FIO_HAVE_STRSEP
31 #define FIO_HAVE_FALLOCATE
32 #define FIO_HAVE_POSIXAIO_FSYNC
33 #define FIO_HAVE_PSHARED_MUTEX
34 #define FIO_HAVE_CL_SIZE
35 #define FIO_HAVE_CGROUPS
36 #define FIO_HAVE_FDATASYNC
37 #define FIO_HAVE_SYNC_FILE_RANGE
38
39 #define OS_MAP_ANON             MAP_ANONYMOUS
40
41 #ifndef CLOCK_MONOTONIC
42 #define CLOCK_MONOTONIC 1
43 #endif
44
45 typedef cpu_set_t os_cpu_mask_t;
46
47 typedef struct drand48_data os_random_state_t;
48
49 /*
50  * we want fadvise64 really, but it's so tangled... later
51  */
52 #ifdef FIO_HAVE_FADVISE
53 #define fadvise(fd, off, len, advice)   \
54         posix_fadvise((fd), (off_t)(off), (len), (advice))
55 #endif
56
57 /*
58  * If you are on an ancient glibc (2.3.2), then define GLIBC_2_3_2 if you want
59  * the affinity helpers to work.
60  */
61 #ifndef GLIBC_2_3_2
62 #define fio_setaffinity(pid, cpumask)           \
63         sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
64 #define fio_getaffinity(pid, ptr)       \
65         sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
66 #else
67 #define fio_setaffinity(pid, cpumask)   \
68         sched_setaffinity((pid), &(cpumask))
69 #define fio_getaffinity(pid, ptr)       \
70         sched_getaffinity((pid), (ptr))
71 #endif
72
73 #define fio_cpu_clear(mask, cpu)        CPU_CLR((cpu), (mask))
74 #define fio_cpu_set(mask, cpu)          CPU_SET((cpu), (mask))
75
76 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
77 {
78         CPU_ZERO(mask);
79         return 0;
80 }
81
82 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
83 {
84         return 0;
85 }
86
87 #define FIO_MAX_CPUS                    CPU_SETSIZE
88
89 static inline int ioprio_set(int which, int who, int ioprio)
90 {
91         return syscall(__NR_ioprio_set, which, who, ioprio);
92 }
93
94 /*
95  * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
96  * aren't either.
97  */
98 #ifndef SPLICE_F_MOVE
99 #define SPLICE_F_MOVE   (0x01)  /* move pages instead of copying */
100 #define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
101                                  /* we may still block on the fd we splice */
102                                  /* from/to, of course */
103 #define SPLICE_F_MORE   (0x04)  /* expect more data */
104 #define SPLICE_F_GIFT   (0x08)  /* pages passed in are a gift */
105
106 static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
107                          size_t len, unsigned int flags)
108 {
109         return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
110 }
111
112 static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
113 {
114         return syscall(__NR_sys_tee, fdin, fdout, len, flags);
115 }
116
117 static inline int vmsplice(int fd, const struct iovec *iov,
118                            unsigned long nr_segs, unsigned int flags)
119 {
120         return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
121 }
122 #endif
123
124 #define SPLICE_DEF_SIZE (64*1024)
125
126 #ifdef FIO_HAVE_SYSLET
127
128 struct syslet_uatom;
129 struct async_head_user;
130
131 /*
132  * syslet stuff
133  */
134 static inline struct syslet_uatom *
135 async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
136 {
137         return (struct syslet_uatom *) syscall(__NR_async_exec, atom, ahu);
138 }
139
140 static inline long
141 async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
142            struct async_head_user *ahu)
143 {
144         return syscall(__NR_async_wait, min_wait_events,
145                         user_ring_idx, ahu);
146 }
147
148 static inline long async_thread(void *event, struct async_head_user *ahu)
149 {
150         return syscall(__NR_async_thread, event, ahu);
151 }
152
153 static inline long umem_add(unsigned long *uptr, unsigned long inc)
154 {
155         return syscall(__NR_umem_add, uptr, inc);
156 }
157 #endif /* FIO_HAVE_SYSLET */
158
159 enum {
160         IOPRIO_CLASS_NONE,
161         IOPRIO_CLASS_RT,
162         IOPRIO_CLASS_BE,
163         IOPRIO_CLASS_IDLE,
164 };
165
166 enum {
167         IOPRIO_WHO_PROCESS = 1,
168         IOPRIO_WHO_PGRP,
169         IOPRIO_WHO_USER,
170 };
171
172 #define IOPRIO_BITS             16
173 #define IOPRIO_CLASS_SHIFT      13
174
175 #ifndef BLKGETSIZE64
176 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
177 #endif
178
179 #ifndef BLKFLSBUF
180 #define BLKFLSBUF       _IO(0x12,97)
181 #endif
182
183 static inline int blockdev_invalidate_cache(int fd)
184 {
185         return ioctl(fd, BLKFLSBUF);
186 }
187
188 static inline int blockdev_size(int fd, unsigned long long *bytes)
189 {
190         if (!ioctl(fd, BLKGETSIZE64, bytes))
191                 return 0;
192
193         return errno;
194 }
195
196 static inline unsigned long long os_phys_mem(void)
197 {
198         long pagesize, pages;
199
200         pagesize = sysconf(_SC_PAGESIZE);
201         pages = sysconf(_SC_PHYS_PAGES);
202         if (pages == -1 || pagesize == -1)
203                 return 0;
204
205         return (unsigned long long) pages * (unsigned long long) pagesize;
206 }
207
208 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
209 {
210         srand48_r(seed, rs);
211 }
212
213 static inline long os_random_long(os_random_state_t *rs)
214 {
215         long val;
216
217         lrand48_r(rs, &val);
218         return val;
219 }
220
221 static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
222 {
223         struct raw_config_request rq;
224         int fd;
225
226         if (major(dev) != RAW_MAJOR)
227                 return 1;
228
229         /*
230          * we should be able to find /dev/rawctl or /dev/raw/rawctl
231          */
232         fd = open("/dev/rawctl", O_RDONLY);
233         if (fd < 0) {
234                 fd = open("/dev/raw/rawctl", O_RDONLY);
235                 if (fd < 0)
236                         return 1;
237         }
238
239         rq.raw_minor = minor(dev);
240         if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
241                 close(fd);
242                 return 1;
243         }
244
245         close(fd);
246         *majdev = rq.block_major;
247         *mindev = rq.block_minor;
248         return 0;
249 }
250
251 #ifdef O_NOATIME
252 #define FIO_O_NOATIME   O_NOATIME
253 #else
254 #define FIO_O_NOATIME   0
255 #endif
256
257 #ifdef MADV_REMOVE
258 #define FIO_MADV_FREE   MADV_REMOVE
259 #endif
260
261 #define CACHE_LINE_FILE \
262         "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
263
264 static inline int arch_cache_line_size(void)
265 {
266         char size[32];
267         int fd, ret;
268
269         fd = open(CACHE_LINE_FILE, O_RDONLY);
270         if (fd < 0)
271                 return -1;
272
273         ret = read(fd, size, sizeof(size));
274
275         close(fd);
276
277         if (ret <= 0)
278                 return -1;
279         else
280                 return atoi(size);
281 }
282
283 #endif