blob: d5c3f76e03fb40afe8c3807f0ded8620f50b1d1e [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001#ifndef FIO_OS_LINUX_H
2#define FIO_OS_LINUX_H
3
Jens Axboecca84642011-10-07 12:47:57 +02004#define FIO_OS os_linux
5
Jens Axboeebac4652005-12-08 15:25:21 +01006#include <sys/ioctl.h>
Jens Axboe8756e4d2006-05-27 20:24:53 +02007#include <sys/uio.h>
Jens Axboe3c39a372006-06-06 20:56:12 +02008#include <sys/syscall.h>
Jens Axboe2e3bd4c2010-05-17 12:29:57 +02009#include <sys/vfs.h>
Jens Axboe3c39a372006-06-06 20:56:12 +020010#include <unistd.h>
Jens Axboe97fbdfa2006-10-05 13:44:15 +020011#include <fcntl.h>
Jens Axboe3c2d93e2009-01-05 19:04:15 +010012#include <errno.h>
Jens Axboed09913c2009-12-16 19:23:10 +010013#include <sched.h>
Jens Axboeea421792006-07-11 09:11:07 +020014#include <linux/unistd.h>
Jens Axboe07e5b262007-04-02 14:46:07 +020015#include <linux/raw.h>
16#include <linux/major.h>
Jens Axboe610a7302011-10-04 13:53:35 +020017#include <endian.h>
Jens Axboeebac4652005-12-08 15:25:21 +010018
Jens Axboef3de88a2008-02-24 21:36:00 +010019#include "indirect.h"
Jens Axboe79a43182010-09-07 13:28:58 +020020#include "binject.h"
Jens Axboee2e58882011-01-04 08:36:06 +010021#include "../file.h"
Jens Axboef3de88a2008-02-24 21:36:00 +010022
Jens Axboeebac4652005-12-08 15:25:21 +010023#define FIO_HAVE_LIBAIO
24#define FIO_HAVE_POSIXAIO
25#define FIO_HAVE_FADVISE
26#define FIO_HAVE_CPU_AFFINITY
27#define FIO_HAVE_DISK_UTIL
28#define FIO_HAVE_SGIO
Jens Axboeba4f8922006-01-26 13:45:54 -080029#define FIO_HAVE_IOPRIO
Jens Axboe8756e4d2006-05-27 20:24:53 +020030#define FIO_HAVE_SPLICE
Jens Axboe22f78b32006-06-07 11:30:07 +020031#define FIO_HAVE_IOSCHED_SWITCH
Jens Axboe2c0ecd22006-06-08 13:25:41 +020032#define FIO_HAVE_ODIRECT
Jens Axboe74b025b2006-12-19 15:18:14 +010033#define FIO_HAVE_HUGETLB
Jens Axboe07e5b262007-04-02 14:46:07 +020034#define FIO_HAVE_RAWBIND
Jens Axboe5e62c222007-05-22 13:27:30 +020035#define FIO_HAVE_BLKTRACE
Jens Axboe5921e802008-05-30 15:02:38 +020036#define FIO_HAVE_STRSEP
Jens Axboe207cb0f2008-06-02 12:28:02 +020037#define FIO_HAVE_POSIXAIO_FSYNC
Jens Axboef356d012009-01-05 09:56:29 +010038#define FIO_HAVE_PSHARED_MUTEX
Jens Axboeeb7ccf32009-04-29 09:48:04 +020039#define FIO_HAVE_CL_SIZE
Jens Axboea696fa22009-12-04 10:05:02 +010040#define FIO_HAVE_CGROUPS
Jens Axboec36d16f2009-12-20 22:29:10 +010041#define FIO_HAVE_FDATASYNC
Jens Axboe2e3bd4c2010-05-17 12:29:57 +020042#define FIO_HAVE_FS_STAT
Jens Axboea5f30272010-07-19 16:19:55 -060043#define FIO_HAVE_TRIM
Jens Axboe79a43182010-09-07 13:28:58 +020044#define FIO_HAVE_BINJECT
Bruce Cranecc314b2011-01-04 10:59:30 +010045#define FIO_HAVE_CLOCK_MONOTONIC
Jens Axboe47f767c2011-07-12 21:17:49 +020046#define FIO_HAVE_GETTID
Bruce Cran93bcfd22012-02-20 20:18:19 +010047#define FIO_USE_GENERIC_INIT_RANDOM_STATE
Jens Axboeb6959b52007-02-14 21:40:27 +000048
Jens Axboe804bb9a2011-07-12 20:49:46 +020049/*
50 * Can only enable this for newer glibcs, or the header and defines are
51 * missing
52 */
Jens Axboe52ad4b92012-03-01 16:07:46 +010053#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 6
54#define FIO_HAVE_FALLOCATE
55#endif
Jens Axboe78b269e2012-02-20 09:21:45 +010056#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 8
Jens Axboe804bb9a2011-07-12 20:49:46 +020057#define FIO_HAVE_LINUX_FALLOCATE
58#endif
59
Jens Axboe093729f2010-07-09 13:47:32 +020060#ifdef SYNC_FILE_RANGE_WAIT_BEFORE
61#define FIO_HAVE_SYNC_FILE_RANGE
62#endif
63
Jens Axboedc873b62008-06-04 20:13:04 +020064#define OS_MAP_ANON MAP_ANONYMOUS
Jens Axboeebac4652005-12-08 15:25:21 +010065
Shawn Lewis4d8947d2007-07-25 07:51:58 +020066#ifndef CLOCK_MONOTONIC
67#define CLOCK_MONOTONIC 1
68#endif
69
Jens Axboeebac4652005-12-08 15:25:21 +010070typedef cpu_set_t os_cpu_mask_t;
Jens Axboe4e78e402008-12-12 20:40:27 +010071
Jens Axboe6dfd46b2006-06-07 13:57:06 +020072typedef struct drand48_data os_random_state_t;
Jens Axboeebac4652005-12-08 15:25:21 +010073
74/*
75 * we want fadvise64 really, but it's so tangled... later
76 */
Shawn Lewis4d8947d2007-07-25 07:51:58 +020077#ifdef FIO_HAVE_FADVISE
Jens Axboeebac4652005-12-08 15:25:21 +010078#define fadvise(fd, off, len, advice) \
79 posix_fadvise((fd), (off_t)(off), (len), (advice))
Shawn Lewis4d8947d2007-07-25 07:51:58 +020080#endif
Jens Axboeebac4652005-12-08 15:25:21 +010081
Jens Axboec8f025f2007-05-22 17:06:24 +020082/*
83 * If you are on an ancient glibc (2.3.2), then define GLIBC_2_3_2 if you want
84 * the affinity helpers to work.
85 */
86#ifndef GLIBC_2_3_2
Jens Axboee8462bd2009-07-06 12:59:04 +020087#define fio_setaffinity(pid, cpumask) \
88 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
Jens Axboeebac4652005-12-08 15:25:21 +010089#define fio_getaffinity(pid, ptr) \
90 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
Jens Axboec8f025f2007-05-22 17:06:24 +020091#else
Jens Axboee8462bd2009-07-06 12:59:04 +020092#define fio_setaffinity(pid, cpumask) \
93 sched_setaffinity((pid), &(cpumask))
Jens Axboec8f025f2007-05-22 17:06:24 +020094#define fio_getaffinity(pid, ptr) \
95 sched_getaffinity((pid), (ptr))
96#endif
Jens Axboebe4ecfd2008-12-08 14:10:52 +010097
Jens Axboe1f4c0a42011-10-07 13:26:27 +020098#define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
99#define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
Jens Axboed2ce18b2008-12-12 20:51:40 +0100100
101static inline int fio_cpuset_init(os_cpu_mask_t *mask)
102{
103 CPU_ZERO(mask);
104 return 0;
105}
106
107static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
108{
109 return 0;
110}
Jens Axboe6d459ee2008-12-12 20:02:58 +0100111
112#define FIO_MAX_CPUS CPU_SETSIZE
Shawn Lewis4d8947d2007-07-25 07:51:58 +0200113
Jens Axboeebac4652005-12-08 15:25:21 +0100114static inline int ioprio_set(int which, int who, int ioprio)
115{
116 return syscall(__NR_ioprio_set, which, who, ioprio);
117}
118
Jens Axboe47f767c2011-07-12 21:17:49 +0200119static inline int gettid(void)
120{
121 return syscall(__NR_gettid);
122}
123
Jens Axboe97fbdfa2006-10-05 13:44:15 +0200124/*
125 * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
126 * aren't either.
127 */
128#ifndef SPLICE_F_MOVE
Jens Axboe8756e4d2006-05-27 20:24:53 +0200129#define SPLICE_F_MOVE (0x01) /* move pages instead of copying */
130#define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
131 /* we may still block on the fd we splice */
132 /* from/to, of course */
133#define SPLICE_F_MORE (0x04) /* expect more data */
134#define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */
135
Jens Axboe97fbdfa2006-10-05 13:44:15 +0200136static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
Jens Axboe495ee9b2007-06-08 11:24:58 +0200137 size_t len, unsigned int flags)
Jens Axboe97fbdfa2006-10-05 13:44:15 +0200138{
139 return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
140}
141
142static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
143{
144 return syscall(__NR_sys_tee, fdin, fdout, len, flags);
145}
146
147static inline int vmsplice(int fd, const struct iovec *iov,
148 unsigned long nr_segs, unsigned int flags)
149{
150 return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
151}
152#endif
153
Jens Axboe3feedc62006-05-28 20:13:51 +0200154#define SPLICE_DEF_SIZE (64*1024)
155
Jens Axboeb6959b52007-02-14 21:40:27 +0000156#ifdef FIO_HAVE_SYSLET
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100157
158struct syslet_uatom;
159struct async_head_user;
160
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100161/*
162 * syslet stuff
163 */
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100164static inline struct syslet_uatom *
165async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100166{
Daniel Rall2b525112008-09-10 08:56:01 +0200167 return (struct syslet_uatom *) syscall(__NR_async_exec, atom, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100168}
169
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100170static inline long
171async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
172 struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100173{
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100174 return syscall(__NR_async_wait, min_wait_events,
175 user_ring_idx, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100176}
177
Jens Axboe7756b0d2007-02-26 10:22:31 +0100178static inline long async_thread(void *event, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100179{
Jens Axboe7756b0d2007-02-26 10:22:31 +0100180 return syscall(__NR_async_thread, event, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100181}
182
183static inline long umem_add(unsigned long *uptr, unsigned long inc)
184{
185 return syscall(__NR_umem_add, uptr, inc);
186}
Jens Axboeb6959b52007-02-14 21:40:27 +0000187#endif /* FIO_HAVE_SYSLET */
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100188
Jens Axboeebac4652005-12-08 15:25:21 +0100189enum {
Jens Axboe6cefbe32007-04-18 12:46:56 +0200190 IOPRIO_CLASS_NONE,
191 IOPRIO_CLASS_RT,
192 IOPRIO_CLASS_BE,
193 IOPRIO_CLASS_IDLE,
194};
195
196enum {
Jens Axboeebac4652005-12-08 15:25:21 +0100197 IOPRIO_WHO_PROCESS = 1,
198 IOPRIO_WHO_PGRP,
199 IOPRIO_WHO_USER,
200};
201
Jens Axboe6cefbe32007-04-18 12:46:56 +0200202#define IOPRIO_BITS 16
Jens Axboeebac4652005-12-08 15:25:21 +0100203#define IOPRIO_CLASS_SHIFT 13
204
205#ifndef BLKGETSIZE64
206#define BLKGETSIZE64 _IOR(0x12,114,size_t)
207#endif
208
Jens Axboee5b401d2006-10-18 16:03:40 +0200209#ifndef BLKFLSBUF
210#define BLKFLSBUF _IO(0x12,97)
211#endif
212
Jens Axboea5f30272010-07-19 16:19:55 -0600213#ifndef BLKDISCARD
214#define BLKDISCARD _IO(0x12,119)
215#endif
216
Jens Axboee2e58882011-01-04 08:36:06 +0100217static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboee5b401d2006-10-18 16:03:40 +0200218{
Bruce Cranecc314b2011-01-04 10:59:30 +0100219 return ioctl(f->fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200220}
221
Bruce Cranecc314b2011-01-04 10:59:30 +0100222static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100223{
Bruce Cranecc314b2011-01-04 10:59:30 +0100224 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
Jens Axboeebac4652005-12-08 15:25:21 +0100225 return 0;
226
227 return errno;
228}
229
Jens Axboe32cd46a2006-06-07 13:40:40 +0200230static inline unsigned long long os_phys_mem(void)
231{
232 long pagesize, pages;
233
234 pagesize = sysconf(_SC_PAGESIZE);
235 pages = sysconf(_SC_PHYS_PAGES);
236 if (pages == -1 || pagesize == -1)
237 return 0;
238
239 return (unsigned long long) pages * (unsigned long long) pagesize;
240}
241
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200242static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
243{
244 srand48_r(seed, rs);
245}
246
247static inline long os_random_long(os_random_state_t *rs)
248{
249 long val;
250
251 lrand48_r(rs, &val);
252 return val;
253}
254
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200255static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200256{
257 struct raw_config_request rq;
258 int fd;
259
260 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200261 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200262
263 /*
264 * we should be able to find /dev/rawctl or /dev/raw/rawctl
265 */
266 fd = open("/dev/rawctl", O_RDONLY);
267 if (fd < 0) {
268 fd = open("/dev/raw/rawctl", O_RDONLY);
269 if (fd < 0)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200270 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200271 }
272
273 rq.raw_minor = minor(dev);
274 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
275 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200276 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200277 }
278
279 close(fd);
280 *majdev = rq.block_major;
281 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200282 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200283}
284
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200285#ifdef O_NOATIME
Jens Axboe5921e802008-05-30 15:02:38 +0200286#define FIO_O_NOATIME O_NOATIME
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200287#else
288#define FIO_O_NOATIME 0
289#endif
Jens Axboe5921e802008-05-30 15:02:38 +0200290
Jens Axboea1c58072009-08-04 23:17:02 +0200291#ifdef MADV_REMOVE
292#define FIO_MADV_FREE MADV_REMOVE
293#endif
294
Jens Axboe610a7302011-10-04 13:53:35 +0200295#if __BYTE_ORDER == __LITTLE_ENDIAN
296#define FIO_LITTLE_ENDIAN
297#elif __BYTE_ORDER == __BIG_ENDIAN
298#define FIO_BIG_ENDIAN
299#else
300#error "Unknown endianness"
301#endif
302
Jens Axboeff245192011-10-04 14:30:33 +0200303#define fio_swap16(x) __bswap_16(x)
304#define fio_swap32(x) __bswap_32(x)
305#define fio_swap64(x) __bswap_64(x)
306
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200307#define CACHE_LINE_FILE \
308 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
309
310static inline int arch_cache_line_size(void)
311{
312 char size[32];
313 int fd, ret;
314
315 fd = open(CACHE_LINE_FILE, O_RDONLY);
316 if (fd < 0)
317 return -1;
318
319 ret = read(fd, size, sizeof(size));
Jens Axboeec76f152009-06-09 13:05:32 +0200320
321 close(fd);
322
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200323 if (ret <= 0)
324 return -1;
Jens Axboeec76f152009-06-09 13:05:32 +0200325 else
326 return atoi(size);
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200327}
328
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200329static inline unsigned long long get_fs_size(const char *path)
330{
331 unsigned long long ret;
332 struct statfs s;
333
334 if (statfs(path, &s) < 0)
335 return -1ULL;
336
337 ret = s.f_bsize;
338 ret *= (unsigned long long) s.f_bfree;
339 return ret;
340}
341
Jens Axboea5f30272010-07-19 16:19:55 -0600342static inline int os_trim(int fd, unsigned long long start,
343 unsigned long long len)
344{
345 uint64_t range[2];
346
347 range[0] = start;
348 range[1] = len;
349
350 if (!ioctl(fd, BLKDISCARD, range))
351 return 0;
352
353 return errno;
354}
355
Jens Axboeebac4652005-12-08 15:25:21 +0100356#endif