blob: e1936342bd54fceac5b61d765a86adfacfdcf970 [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 Axboe39b93562012-11-27 08:27:50 +010010#include <sys/mman.h>
Jens Axboe3c39a372006-06-06 20:56:12 +020011#include <unistd.h>
Jens Axboe97fbdfa2006-10-05 13:44:15 +020012#include <fcntl.h>
Jens Axboe3c2d93e2009-01-05 19:04:15 +010013#include <errno.h>
Jens Axboed09913c2009-12-16 19:23:10 +010014#include <sched.h>
Jens Axboeea421792006-07-11 09:11:07 +020015#include <linux/unistd.h>
Jens Axboe07e5b262007-04-02 14:46:07 +020016#include <linux/raw.h>
17#include <linux/major.h>
Jens Axboe10613762014-09-29 16:07:54 -060018#include <byteswap.h>
Jens Axboeebac4652005-12-08 15:25:21 +010019
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_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 Axboe22f78b32006-06-07 11:30:07 +020027#define FIO_HAVE_IOSCHED_SWITCH
Jens Axboe2c0ecd22006-06-08 13:25:41 +020028#define FIO_HAVE_ODIRECT
Jens Axboe74b025b2006-12-19 15:18:14 +010029#define FIO_HAVE_HUGETLB
Jens Axboe07e5b262007-04-02 14:46:07 +020030#define FIO_HAVE_RAWBIND
Jens Axboe5e62c222007-05-22 13:27:30 +020031#define FIO_HAVE_BLKTRACE
Jens Axboef356d012009-01-05 09:56:29 +010032#define FIO_HAVE_PSHARED_MUTEX
Jens Axboeeb7ccf32009-04-29 09:48:04 +020033#define FIO_HAVE_CL_SIZE
Jens Axboea696fa22009-12-04 10:05:02 +010034#define FIO_HAVE_CGROUPS
Jens Axboe2e3bd4c2010-05-17 12:29:57 +020035#define FIO_HAVE_FS_STAT
Jens Axboea5f30272010-07-19 16:19:55 -060036#define FIO_HAVE_TRIM
Jens Axboe79a43182010-09-07 13:28:58 +020037#define FIO_HAVE_BINJECT
Jens Axboe47f767c2011-07-12 21:17:49 +020038#define FIO_HAVE_GETTID
Bruce Cran93bcfd22012-02-20 20:18:19 +010039#define FIO_USE_GENERIC_INIT_RANDOM_STATE
Jens Axboeb6959b52007-02-14 21:40:27 +000040
Jens Axboe39b93562012-11-27 08:27:50 +010041#ifdef MAP_HUGETLB
42#define FIO_HAVE_MMAP_HUGE
43#endif
44
Jens Axboedc873b62008-06-04 20:13:04 +020045#define OS_MAP_ANON MAP_ANONYMOUS
Jens Axboeebac4652005-12-08 15:25:21 +010046
47typedef cpu_set_t os_cpu_mask_t;
Jens Axboe4e78e402008-12-12 20:40:27 +010048
Jens Axboe6dfd46b2006-06-07 13:57:06 +020049typedef struct drand48_data os_random_state_t;
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboe67bf9822013-01-10 11:23:19 +010051#ifdef CONFIG_3ARG_AFFINITY
Jens Axboee8462bd2009-07-06 12:59:04 +020052#define fio_setaffinity(pid, cpumask) \
53 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
Jens Axboeebac4652005-12-08 15:25:21 +010054#define fio_getaffinity(pid, ptr) \
55 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
Jens Axboe67bf9822013-01-10 11:23:19 +010056#elif defined(CONFIG_2ARG_AFFINITY)
Jens Axboee8462bd2009-07-06 12:59:04 +020057#define fio_setaffinity(pid, cpumask) \
58 sched_setaffinity((pid), &(cpumask))
Jens Axboec8f025f2007-05-22 17:06:24 +020059#define fio_getaffinity(pid, ptr) \
60 sched_getaffinity((pid), (ptr))
61#endif
Jens Axboebe4ecfd2008-12-08 14:10:52 +010062
Jens Axboe1f4c0a42011-10-07 13:26:27 +020063#define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
64#define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
Jens Axboe50b58602014-02-28 15:08:25 -080065#define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask))
Jens Axboed004a202014-03-04 09:02:10 -070066#define fio_cpu_count(mask) CPU_COUNT((mask))
Jens Axboed2ce18b2008-12-12 20:51:40 +010067
68static inline int fio_cpuset_init(os_cpu_mask_t *mask)
69{
70 CPU_ZERO(mask);
71 return 0;
72}
73
74static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
75{
76 return 0;
77}
Jens Axboe6d459ee2008-12-12 20:02:58 +010078
79#define FIO_MAX_CPUS CPU_SETSIZE
Shawn Lewis4d8947d2007-07-25 07:51:58 +020080
Jens Axboe28727df2012-03-29 08:33:15 +020081enum {
82 IOPRIO_CLASS_NONE,
83 IOPRIO_CLASS_RT,
84 IOPRIO_CLASS_BE,
85 IOPRIO_CLASS_IDLE,
86};
87
88enum {
89 IOPRIO_WHO_PROCESS = 1,
90 IOPRIO_WHO_PGRP,
91 IOPRIO_WHO_USER,
92};
93
94#define IOPRIO_BITS 16
95#define IOPRIO_CLASS_SHIFT 13
96
97static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
Jens Axboeebac4652005-12-08 15:25:21 +010098{
Jens Axboe28727df2012-03-29 08:33:15 +020099 /*
100 * If no class is set, assume BE
101 */
102 if (!ioprio_class)
103 ioprio_class = IOPRIO_CLASS_BE;
104
105 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100106 return syscall(__NR_ioprio_set, which, who, ioprio);
107}
108
Jens Axboe47f767c2011-07-12 21:17:49 +0200109static inline int gettid(void)
110{
111 return syscall(__NR_gettid);
112}
113
Jens Axboe3feedc62006-05-28 20:13:51 +0200114#define SPLICE_DEF_SIZE (64*1024)
115
Jens Axboeebac4652005-12-08 15:25:21 +0100116#ifndef BLKGETSIZE64
117#define BLKGETSIZE64 _IOR(0x12,114,size_t)
118#endif
119
Jens Axboee5b401d2006-10-18 16:03:40 +0200120#ifndef BLKFLSBUF
121#define BLKFLSBUF _IO(0x12,97)
122#endif
123
Jens Axboea5f30272010-07-19 16:19:55 -0600124#ifndef BLKDISCARD
125#define BLKDISCARD _IO(0x12,119)
126#endif
127
Jens Axboee2e58882011-01-04 08:36:06 +0100128static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboee5b401d2006-10-18 16:03:40 +0200129{
Bruce Cranecc314b2011-01-04 10:59:30 +0100130 return ioctl(f->fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200131}
132
Bruce Cranecc314b2011-01-04 10:59:30 +0100133static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100134{
Bruce Cranecc314b2011-01-04 10:59:30 +0100135 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
Jens Axboeebac4652005-12-08 15:25:21 +0100136 return 0;
137
138 return errno;
139}
140
Jens Axboe32cd46a2006-06-07 13:40:40 +0200141static inline unsigned long long os_phys_mem(void)
142{
143 long pagesize, pages;
144
145 pagesize = sysconf(_SC_PAGESIZE);
146 pages = sysconf(_SC_PHYS_PAGES);
147 if (pages == -1 || pagesize == -1)
148 return 0;
149
150 return (unsigned long long) pages * (unsigned long long) pagesize;
151}
152
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200153static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
154{
155 srand48_r(seed, rs);
156}
157
158static inline long os_random_long(os_random_state_t *rs)
159{
160 long val;
161
162 lrand48_r(rs, &val);
163 return val;
164}
165
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200166static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200167{
168 struct raw_config_request rq;
169 int fd;
170
171 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200172 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200173
174 /*
175 * we should be able to find /dev/rawctl or /dev/raw/rawctl
176 */
177 fd = open("/dev/rawctl", O_RDONLY);
178 if (fd < 0) {
179 fd = open("/dev/raw/rawctl", O_RDONLY);
180 if (fd < 0)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200181 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200182 }
183
184 rq.raw_minor = minor(dev);
185 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
186 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200187 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200188 }
189
190 close(fd);
191 *majdev = rq.block_major;
192 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200193 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200194}
195
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200196#ifdef O_NOATIME
Jens Axboe5921e802008-05-30 15:02:38 +0200197#define FIO_O_NOATIME O_NOATIME
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200198#else
199#define FIO_O_NOATIME 0
200#endif
Jens Axboe5921e802008-05-30 15:02:38 +0200201
Chris Masond01612f2013-11-15 15:52:58 -0700202#ifdef O_ATOMIC
203#define OS_O_ATOMIC O_ATOMIC
204#else
205#define OS_O_ATOMIC 040000000
206#endif
207
Jens Axboea1c58072009-08-04 23:17:02 +0200208#ifdef MADV_REMOVE
209#define FIO_MADV_FREE MADV_REMOVE
210#endif
211
Jens Axboe10613762014-09-29 16:07:54 -0600212#if defined(__builtin_bswap16)
213#define fio_swap16(x) __builtin_bswap16(x)
214#else
Jens Axboeff245192011-10-04 14:30:33 +0200215#define fio_swap16(x) __bswap_16(x)
Jens Axboe10613762014-09-29 16:07:54 -0600216#endif
217#if defined(__builtin_bswap32)
218#define fio_swap32(x) __builtin_bswap32(x)
219#else
Jens Axboeff245192011-10-04 14:30:33 +0200220#define fio_swap32(x) __bswap_32(x)
Jens Axboe10613762014-09-29 16:07:54 -0600221#endif
222#if defined(__builtin_bswap64)
223#define fio_swap64(x) __builtin_bswap64(x)
224#else
Jens Axboeff245192011-10-04 14:30:33 +0200225#define fio_swap64(x) __bswap_64(x)
Jens Axboe10613762014-09-29 16:07:54 -0600226#endif
Jens Axboeff245192011-10-04 14:30:33 +0200227
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200228#define CACHE_LINE_FILE \
229 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
230
231static inline int arch_cache_line_size(void)
232{
233 char size[32];
234 int fd, ret;
235
236 fd = open(CACHE_LINE_FILE, O_RDONLY);
237 if (fd < 0)
238 return -1;
239
240 ret = read(fd, size, sizeof(size));
Jens Axboeec76f152009-06-09 13:05:32 +0200241
242 close(fd);
243
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200244 if (ret <= 0)
245 return -1;
Jens Axboeec76f152009-06-09 13:05:32 +0200246 else
247 return atoi(size);
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200248}
249
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200250static inline unsigned long long get_fs_size(const char *path)
251{
252 unsigned long long ret;
253 struct statfs s;
254
255 if (statfs(path, &s) < 0)
256 return -1ULL;
257
258 ret = s.f_bsize;
259 ret *= (unsigned long long) s.f_bfree;
260 return ret;
261}
262
Jens Axboea5f30272010-07-19 16:19:55 -0600263static inline int os_trim(int fd, unsigned long long start,
264 unsigned long long len)
265{
266 uint64_t range[2];
267
268 range[0] = start;
269 range[1] = len;
270
271 if (!ioctl(fd, BLKDISCARD, range))
272 return 0;
273
274 return errno;
275}
276
Jens Axboe7e09a9f2013-01-30 13:58:58 +0100277#ifdef CONFIG_SCHED_IDLE
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100278static inline int fio_set_sched_idle(void)
279{
280 struct sched_param p = { .sched_priority = 0, };
281 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
282}
Jens Axboe7e09a9f2013-01-30 13:58:58 +0100283#endif
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100284
Jens Axboeebac4652005-12-08 15:25:21 +0100285#endif