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