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