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