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