blob: a36552ba68ba93caf6ca58e5fc9761fc53c0b88d [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001#ifndef FIO_OS_LINUX_H
2#define FIO_OS_LINUX_H
3
4#include <sys/ioctl.h>
Jens Axboe8756e4d2006-05-27 20:24:53 +02005#include <sys/uio.h>
Jens Axboe3c39a372006-06-06 20:56:12 +02006#include <sys/syscall.h>
Jens Axboe2e3bd4c2010-05-17 12:29:57 +02007#include <sys/vfs.h>
Jens Axboe3c39a372006-06-06 20:56:12 +02008#include <unistd.h>
Jens Axboe97fbdfa2006-10-05 13:44:15 +02009#include <fcntl.h>
Jens Axboe3c2d93e2009-01-05 19:04:15 +010010#include <errno.h>
Jens Axboed09913c2009-12-16 19:23:10 +010011#include <sched.h>
Jens Axboeea421792006-07-11 09:11:07 +020012#include <linux/unistd.h>
Jens Axboe07e5b262007-04-02 14:46:07 +020013#include <linux/raw.h>
14#include <linux/major.h>
Jens Axboeebac4652005-12-08 15:25:21 +010015
Jens Axboef3de88a2008-02-24 21:36:00 +010016#include "indirect.h"
Jens Axboe79a43182010-09-07 13:28:58 +020017#include "binject.h"
Jens Axboee2e58882011-01-04 08:36:06 +010018#include "../file.h"
Jens Axboef3de88a2008-02-24 21:36:00 +010019
Jens Axboeebac4652005-12-08 15:25:21 +010020#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
Jens Axboeba4f8922006-01-26 13:45:54 -080026#define FIO_HAVE_IOPRIO
Jens Axboe8756e4d2006-05-27 20:24:53 +020027#define FIO_HAVE_SPLICE
Jens Axboe22f78b32006-06-07 11:30:07 +020028#define FIO_HAVE_IOSCHED_SWITCH
Jens Axboe2c0ecd22006-06-08 13:25:41 +020029#define FIO_HAVE_ODIRECT
Jens Axboe74b025b2006-12-19 15:18:14 +010030#define FIO_HAVE_HUGETLB
Jens Axboe07e5b262007-04-02 14:46:07 +020031#define FIO_HAVE_RAWBIND
Jens Axboe5e62c222007-05-22 13:27:30 +020032#define FIO_HAVE_BLKTRACE
Jens Axboe5921e802008-05-30 15:02:38 +020033#define FIO_HAVE_STRSEP
Jens Axboefffca022008-06-02 12:23:40 +020034#define FIO_HAVE_FALLOCATE
Jens Axboe207cb0f2008-06-02 12:28:02 +020035#define FIO_HAVE_POSIXAIO_FSYNC
Jens Axboef356d012009-01-05 09:56:29 +010036#define FIO_HAVE_PSHARED_MUTEX
Jens Axboeeb7ccf32009-04-29 09:48:04 +020037#define FIO_HAVE_CL_SIZE
Jens Axboea696fa22009-12-04 10:05:02 +010038#define FIO_HAVE_CGROUPS
Jens Axboec36d16f2009-12-20 22:29:10 +010039#define FIO_HAVE_FDATASYNC
Jens Axboe2e3bd4c2010-05-17 12:29:57 +020040#define FIO_HAVE_FS_STAT
Jens Axboea5f30272010-07-19 16:19:55 -060041#define FIO_HAVE_TRIM
Jens Axboe79a43182010-09-07 13:28:58 +020042#define FIO_HAVE_BINJECT
Bruce Cranecc314b2011-01-04 10:59:30 +010043#define FIO_HAVE_CLOCK_MONOTONIC
Jens Axboe47f767c2011-07-12 21:17:49 +020044#define FIO_HAVE_GETTID
Jens Axboeb6959b52007-02-14 21:40:27 +000045
Jens Axboe804bb9a2011-07-12 20:49:46 +020046/*
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
Jens Axboe093729f2010-07-09 13:47:32 +020054#ifdef SYNC_FILE_RANGE_WAIT_BEFORE
55#define FIO_HAVE_SYNC_FILE_RANGE
56#endif
57
Jens Axboedc873b62008-06-04 20:13:04 +020058#define OS_MAP_ANON MAP_ANONYMOUS
Jens Axboeebac4652005-12-08 15:25:21 +010059
Shawn Lewis4d8947d2007-07-25 07:51:58 +020060#ifndef CLOCK_MONOTONIC
61#define CLOCK_MONOTONIC 1
62#endif
63
Jens Axboeebac4652005-12-08 15:25:21 +010064typedef cpu_set_t os_cpu_mask_t;
Jens Axboe4e78e402008-12-12 20:40:27 +010065
Jens Axboe6dfd46b2006-06-07 13:57:06 +020066typedef struct drand48_data os_random_state_t;
Jens Axboeebac4652005-12-08 15:25:21 +010067
68/*
69 * we want fadvise64 really, but it's so tangled... later
70 */
Shawn Lewis4d8947d2007-07-25 07:51:58 +020071#ifdef FIO_HAVE_FADVISE
Jens Axboeebac4652005-12-08 15:25:21 +010072#define fadvise(fd, off, len, advice) \
73 posix_fadvise((fd), (off_t)(off), (len), (advice))
Shawn Lewis4d8947d2007-07-25 07:51:58 +020074#endif
Jens Axboeebac4652005-12-08 15:25:21 +010075
Jens Axboec8f025f2007-05-22 17:06:24 +020076/*
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
Jens Axboee8462bd2009-07-06 12:59:04 +020081#define fio_setaffinity(pid, cpumask) \
82 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
Jens Axboeebac4652005-12-08 15:25:21 +010083#define fio_getaffinity(pid, ptr) \
84 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
Jens Axboec8f025f2007-05-22 17:06:24 +020085#else
Jens Axboee8462bd2009-07-06 12:59:04 +020086#define fio_setaffinity(pid, cpumask) \
87 sched_setaffinity((pid), &(cpumask))
Jens Axboec8f025f2007-05-22 17:06:24 +020088#define fio_getaffinity(pid, ptr) \
89 sched_getaffinity((pid), (ptr))
90#endif
Jens Axboebe4ecfd2008-12-08 14:10:52 +010091
92#define fio_cpu_clear(mask, cpu) CPU_CLR((cpu), (mask))
Jens Axboe6d459ee2008-12-12 20:02:58 +010093#define fio_cpu_set(mask, cpu) CPU_SET((cpu), (mask))
Jens Axboed2ce18b2008-12-12 20:51:40 +010094
95static inline int fio_cpuset_init(os_cpu_mask_t *mask)
96{
97 CPU_ZERO(mask);
98 return 0;
99}
100
101static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
102{
103 return 0;
104}
Jens Axboe6d459ee2008-12-12 20:02:58 +0100105
106#define FIO_MAX_CPUS CPU_SETSIZE
Shawn Lewis4d8947d2007-07-25 07:51:58 +0200107
Jens Axboeebac4652005-12-08 15:25:21 +0100108static inline int ioprio_set(int which, int who, int ioprio)
109{
110 return syscall(__NR_ioprio_set, which, who, ioprio);
111}
112
Jens Axboe47f767c2011-07-12 21:17:49 +0200113static inline int gettid(void)
114{
115 return syscall(__NR_gettid);
116}
117
Jens Axboe97fbdfa2006-10-05 13:44:15 +0200118/*
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
Jens Axboe8756e4d2006-05-27 20:24:53 +0200123#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
Jens Axboe97fbdfa2006-10-05 13:44:15 +0200130static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
Jens Axboe495ee9b2007-06-08 11:24:58 +0200131 size_t len, unsigned int flags)
Jens Axboe97fbdfa2006-10-05 13:44:15 +0200132{
133 return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
134}
135
136static 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
141static 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
Jens Axboe3feedc62006-05-28 20:13:51 +0200148#define SPLICE_DEF_SIZE (64*1024)
149
Jens Axboeb6959b52007-02-14 21:40:27 +0000150#ifdef FIO_HAVE_SYSLET
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100151
152struct syslet_uatom;
153struct async_head_user;
154
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100155/*
156 * syslet stuff
157 */
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100158static inline struct syslet_uatom *
159async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100160{
Daniel Rall2b525112008-09-10 08:56:01 +0200161 return (struct syslet_uatom *) syscall(__NR_async_exec, atom, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100162}
163
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100164static inline long
165async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
166 struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100167{
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100168 return syscall(__NR_async_wait, min_wait_events,
169 user_ring_idx, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100170}
171
Jens Axboe7756b0d2007-02-26 10:22:31 +0100172static inline long async_thread(void *event, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100173{
Jens Axboe7756b0d2007-02-26 10:22:31 +0100174 return syscall(__NR_async_thread, event, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100175}
176
177static inline long umem_add(unsigned long *uptr, unsigned long inc)
178{
179 return syscall(__NR_umem_add, uptr, inc);
180}
Jens Axboeb6959b52007-02-14 21:40:27 +0000181#endif /* FIO_HAVE_SYSLET */
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100182
Jens Axboeebac4652005-12-08 15:25:21 +0100183enum {
Jens Axboe6cefbe32007-04-18 12:46:56 +0200184 IOPRIO_CLASS_NONE,
185 IOPRIO_CLASS_RT,
186 IOPRIO_CLASS_BE,
187 IOPRIO_CLASS_IDLE,
188};
189
190enum {
Jens Axboeebac4652005-12-08 15:25:21 +0100191 IOPRIO_WHO_PROCESS = 1,
192 IOPRIO_WHO_PGRP,
193 IOPRIO_WHO_USER,
194};
195
Jens Axboe6cefbe32007-04-18 12:46:56 +0200196#define IOPRIO_BITS 16
Jens Axboeebac4652005-12-08 15:25:21 +0100197#define IOPRIO_CLASS_SHIFT 13
198
199#ifndef BLKGETSIZE64
200#define BLKGETSIZE64 _IOR(0x12,114,size_t)
201#endif
202
Jens Axboee5b401d2006-10-18 16:03:40 +0200203#ifndef BLKFLSBUF
204#define BLKFLSBUF _IO(0x12,97)
205#endif
206
Jens Axboea5f30272010-07-19 16:19:55 -0600207#ifndef BLKDISCARD
208#define BLKDISCARD _IO(0x12,119)
209#endif
210
Jens Axboee2e58882011-01-04 08:36:06 +0100211static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboee5b401d2006-10-18 16:03:40 +0200212{
Bruce Cranecc314b2011-01-04 10:59:30 +0100213 return ioctl(f->fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200214}
215
Bruce Cranecc314b2011-01-04 10:59:30 +0100216static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100217{
Bruce Cranecc314b2011-01-04 10:59:30 +0100218 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
Jens Axboeebac4652005-12-08 15:25:21 +0100219 return 0;
220
221 return errno;
222}
223
Jens Axboe32cd46a2006-06-07 13:40:40 +0200224static 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
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200236static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
237{
238 srand48_r(seed, rs);
239}
240
241static 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
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200249static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200250{
251 struct raw_config_request rq;
252 int fd;
253
254 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200255 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200256
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)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200264 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200265 }
266
267 rq.raw_minor = minor(dev);
268 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
269 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200270 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200271 }
272
273 close(fd);
274 *majdev = rq.block_major;
275 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200276 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200277}
278
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200279#ifdef O_NOATIME
Jens Axboe5921e802008-05-30 15:02:38 +0200280#define FIO_O_NOATIME O_NOATIME
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200281#else
282#define FIO_O_NOATIME 0
283#endif
Jens Axboe5921e802008-05-30 15:02:38 +0200284
Jens Axboea1c58072009-08-04 23:17:02 +0200285#ifdef MADV_REMOVE
286#define FIO_MADV_FREE MADV_REMOVE
287#endif
288
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200289#define CACHE_LINE_FILE \
290 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
291
292static 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));
Jens Axboeec76f152009-06-09 13:05:32 +0200302
303 close(fd);
304
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200305 if (ret <= 0)
306 return -1;
Jens Axboeec76f152009-06-09 13:05:32 +0200307 else
308 return atoi(size);
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200309}
310
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200311static 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
Jens Axboea5f30272010-07-19 16:19:55 -0600324static 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
Jens Axboeebac4652005-12-08 15:25:21 +0100338#endif