blob: ef80ce2bd274182ced24cfbdfe76369bde33f4aa [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 Axboeebac4652005-12-08 15:25:21 +010018
Jens Axboe79a43182010-09-07 13:28:58 +020019#include "binject.h"
Jens Axboee2e58882011-01-04 08:36:06 +010020#include "../file.h"
Jens Axboef3de88a2008-02-24 21:36:00 +010021
Jens Axboeebac4652005-12-08 15:25:21 +010022#define FIO_HAVE_CPU_AFFINITY
23#define FIO_HAVE_DISK_UTIL
24#define FIO_HAVE_SGIO
Jens Axboeba4f8922006-01-26 13:45:54 -080025#define FIO_HAVE_IOPRIO
Jens Axboe22f78b32006-06-07 11:30:07 +020026#define FIO_HAVE_IOSCHED_SWITCH
Jens Axboe2c0ecd22006-06-08 13:25:41 +020027#define FIO_HAVE_ODIRECT
Jens Axboe74b025b2006-12-19 15:18:14 +010028#define FIO_HAVE_HUGETLB
Jens Axboe07e5b262007-04-02 14:46:07 +020029#define FIO_HAVE_RAWBIND
Jens Axboe5e62c222007-05-22 13:27:30 +020030#define FIO_HAVE_BLKTRACE
Jens Axboef356d012009-01-05 09:56:29 +010031#define FIO_HAVE_PSHARED_MUTEX
Jens Axboeeb7ccf32009-04-29 09:48:04 +020032#define FIO_HAVE_CL_SIZE
Jens Axboea696fa22009-12-04 10:05:02 +010033#define FIO_HAVE_CGROUPS
Jens Axboe2e3bd4c2010-05-17 12:29:57 +020034#define FIO_HAVE_FS_STAT
Jens Axboea5f30272010-07-19 16:19:55 -060035#define FIO_HAVE_TRIM
Jens Axboe79a43182010-09-07 13:28:58 +020036#define FIO_HAVE_BINJECT
Jens Axboe47f767c2011-07-12 21:17:49 +020037#define FIO_HAVE_GETTID
Bruce Cran93bcfd22012-02-20 20:18:19 +010038#define FIO_USE_GENERIC_INIT_RANDOM_STATE
Jens Axboeb6959b52007-02-14 21:40:27 +000039
Jens Axboe39b93562012-11-27 08:27:50 +010040#ifdef MAP_HUGETLB
41#define FIO_HAVE_MMAP_HUGE
42#endif
43
Jens Axboedc873b62008-06-04 20:13:04 +020044#define OS_MAP_ANON MAP_ANONYMOUS
Jens Axboeebac4652005-12-08 15:25:21 +010045
46typedef cpu_set_t os_cpu_mask_t;
Jens Axboe4e78e402008-12-12 20:40:27 +010047
Jens Axboe6dfd46b2006-06-07 13:57:06 +020048typedef struct drand48_data os_random_state_t;
Jens Axboeebac4652005-12-08 15:25:21 +010049
Jens Axboe67bf9822013-01-10 11:23:19 +010050#ifdef CONFIG_3ARG_AFFINITY
Jens Axboee8462bd2009-07-06 12:59:04 +020051#define fio_setaffinity(pid, cpumask) \
52 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
Jens Axboeebac4652005-12-08 15:25:21 +010053#define fio_getaffinity(pid, ptr) \
54 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
Jens Axboe67bf9822013-01-10 11:23:19 +010055#elif defined(CONFIG_2ARG_AFFINITY)
Jens Axboee8462bd2009-07-06 12:59:04 +020056#define fio_setaffinity(pid, cpumask) \
57 sched_setaffinity((pid), &(cpumask))
Jens Axboec8f025f2007-05-22 17:06:24 +020058#define fio_getaffinity(pid, ptr) \
59 sched_getaffinity((pid), (ptr))
60#endif
Jens Axboebe4ecfd2008-12-08 14:10:52 +010061
Jens Axboe1f4c0a42011-10-07 13:26:27 +020062#define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
63#define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
Jens Axboe50b58602014-02-28 15:08:25 -080064#define fio_cpu_isset(mask, cpu) CPU_ISSET((cpu), (mask))
Jens Axboec2acfba2014-02-27 15:52:02 -080065#define fio_cpu_count(maks) CPU_COUNT((mask))
Jens Axboed2ce18b2008-12-12 20:51:40 +010066
67static inline int fio_cpuset_init(os_cpu_mask_t *mask)
68{
69 CPU_ZERO(mask);
70 return 0;
71}
72
73static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
74{
75 return 0;
76}
Jens Axboe6d459ee2008-12-12 20:02:58 +010077
78#define FIO_MAX_CPUS CPU_SETSIZE
Shawn Lewis4d8947d2007-07-25 07:51:58 +020079
Jens Axboe28727df2012-03-29 08:33:15 +020080enum {
81 IOPRIO_CLASS_NONE,
82 IOPRIO_CLASS_RT,
83 IOPRIO_CLASS_BE,
84 IOPRIO_CLASS_IDLE,
85};
86
87enum {
88 IOPRIO_WHO_PROCESS = 1,
89 IOPRIO_WHO_PGRP,
90 IOPRIO_WHO_USER,
91};
92
93#define IOPRIO_BITS 16
94#define IOPRIO_CLASS_SHIFT 13
95
96static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
Jens Axboeebac4652005-12-08 15:25:21 +010097{
Jens Axboe28727df2012-03-29 08:33:15 +020098 /*
99 * If no class is set, assume BE
100 */
101 if (!ioprio_class)
102 ioprio_class = IOPRIO_CLASS_BE;
103
104 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100105 return syscall(__NR_ioprio_set, which, who, ioprio);
106}
107
Jens Axboe47f767c2011-07-12 21:17:49 +0200108static inline int gettid(void)
109{
110 return syscall(__NR_gettid);
111}
112
Jens Axboe3feedc62006-05-28 20:13:51 +0200113#define SPLICE_DEF_SIZE (64*1024)
114
Jens Axboeebac4652005-12-08 15:25:21 +0100115#ifndef BLKGETSIZE64
116#define BLKGETSIZE64 _IOR(0x12,114,size_t)
117#endif
118
Jens Axboee5b401d2006-10-18 16:03:40 +0200119#ifndef BLKFLSBUF
120#define BLKFLSBUF _IO(0x12,97)
121#endif
122
Jens Axboea5f30272010-07-19 16:19:55 -0600123#ifndef BLKDISCARD
124#define BLKDISCARD _IO(0x12,119)
125#endif
126
Jens Axboee2e58882011-01-04 08:36:06 +0100127static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboee5b401d2006-10-18 16:03:40 +0200128{
Bruce Cranecc314b2011-01-04 10:59:30 +0100129 return ioctl(f->fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200130}
131
Bruce Cranecc314b2011-01-04 10:59:30 +0100132static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100133{
Bruce Cranecc314b2011-01-04 10:59:30 +0100134 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
Jens Axboeebac4652005-12-08 15:25:21 +0100135 return 0;
136
137 return errno;
138}
139
Jens Axboe32cd46a2006-06-07 13:40:40 +0200140static inline unsigned long long os_phys_mem(void)
141{
142 long pagesize, pages;
143
144 pagesize = sysconf(_SC_PAGESIZE);
145 pages = sysconf(_SC_PHYS_PAGES);
146 if (pages == -1 || pagesize == -1)
147 return 0;
148
149 return (unsigned long long) pages * (unsigned long long) pagesize;
150}
151
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200152static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
153{
154 srand48_r(seed, rs);
155}
156
157static inline long os_random_long(os_random_state_t *rs)
158{
159 long val;
160
161 lrand48_r(rs, &val);
162 return val;
163}
164
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200165static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200166{
167 struct raw_config_request rq;
168 int fd;
169
170 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200171 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200172
173 /*
174 * we should be able to find /dev/rawctl or /dev/raw/rawctl
175 */
176 fd = open("/dev/rawctl", O_RDONLY);
177 if (fd < 0) {
178 fd = open("/dev/raw/rawctl", O_RDONLY);
179 if (fd < 0)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200180 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200181 }
182
183 rq.raw_minor = minor(dev);
184 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
185 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200186 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200187 }
188
189 close(fd);
190 *majdev = rq.block_major;
191 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200192 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200193}
194
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200195#ifdef O_NOATIME
Jens Axboe5921e802008-05-30 15:02:38 +0200196#define FIO_O_NOATIME O_NOATIME
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200197#else
198#define FIO_O_NOATIME 0
199#endif
Jens Axboe5921e802008-05-30 15:02:38 +0200200
Chris Masond01612f2013-11-15 15:52:58 -0700201#ifdef O_ATOMIC
202#define OS_O_ATOMIC O_ATOMIC
203#else
204#define OS_O_ATOMIC 040000000
205#endif
206
Jens Axboea1c58072009-08-04 23:17:02 +0200207#ifdef MADV_REMOVE
208#define FIO_MADV_FREE MADV_REMOVE
209#endif
210
Jens Axboeff245192011-10-04 14:30:33 +0200211#define fio_swap16(x) __bswap_16(x)
212#define fio_swap32(x) __bswap_32(x)
213#define fio_swap64(x) __bswap_64(x)
214
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200215#define CACHE_LINE_FILE \
216 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
217
218static inline int arch_cache_line_size(void)
219{
220 char size[32];
221 int fd, ret;
222
223 fd = open(CACHE_LINE_FILE, O_RDONLY);
224 if (fd < 0)
225 return -1;
226
227 ret = read(fd, size, sizeof(size));
Jens Axboeec76f152009-06-09 13:05:32 +0200228
229 close(fd);
230
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200231 if (ret <= 0)
232 return -1;
Jens Axboeec76f152009-06-09 13:05:32 +0200233 else
234 return atoi(size);
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200235}
236
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200237static inline unsigned long long get_fs_size(const char *path)
238{
239 unsigned long long ret;
240 struct statfs s;
241
242 if (statfs(path, &s) < 0)
243 return -1ULL;
244
245 ret = s.f_bsize;
246 ret *= (unsigned long long) s.f_bfree;
247 return ret;
248}
249
Jens Axboea5f30272010-07-19 16:19:55 -0600250static inline int os_trim(int fd, unsigned long long start,
251 unsigned long long len)
252{
253 uint64_t range[2];
254
255 range[0] = start;
256 range[1] = len;
257
258 if (!ioctl(fd, BLKDISCARD, range))
259 return 0;
260
261 return errno;
262}
263
Jens Axboe7e09a9f2013-01-30 13:58:58 +0100264#ifdef CONFIG_SCHED_IDLE
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100265static inline int fio_set_sched_idle(void)
266{
267 struct sched_param p = { .sched_priority = 0, };
268 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
269}
Jens Axboe7e09a9f2013-01-30 13:58:58 +0100270#endif
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100271
Jens Axboeebac4652005-12-08 15:25:21 +0100272#endif