blob: 869a25d8ee5d86c684762d91c4f7a4496f2b5d7f [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 Axboed2ce18b2008-12-12 20:51:40 +010064
65static inline int fio_cpuset_init(os_cpu_mask_t *mask)
66{
67 CPU_ZERO(mask);
68 return 0;
69}
70
71static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
72{
73 return 0;
74}
Jens Axboe6d459ee2008-12-12 20:02:58 +010075
76#define FIO_MAX_CPUS CPU_SETSIZE
Shawn Lewis4d8947d2007-07-25 07:51:58 +020077
Jens Axboe28727df2012-03-29 08:33:15 +020078enum {
79 IOPRIO_CLASS_NONE,
80 IOPRIO_CLASS_RT,
81 IOPRIO_CLASS_BE,
82 IOPRIO_CLASS_IDLE,
83};
84
85enum {
86 IOPRIO_WHO_PROCESS = 1,
87 IOPRIO_WHO_PGRP,
88 IOPRIO_WHO_USER,
89};
90
91#define IOPRIO_BITS 16
92#define IOPRIO_CLASS_SHIFT 13
93
94static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
Jens Axboeebac4652005-12-08 15:25:21 +010095{
Jens Axboe28727df2012-03-29 08:33:15 +020096 /*
97 * If no class is set, assume BE
98 */
99 if (!ioprio_class)
100 ioprio_class = IOPRIO_CLASS_BE;
101
102 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
Jens Axboeebac4652005-12-08 15:25:21 +0100103 return syscall(__NR_ioprio_set, which, who, ioprio);
104}
105
Jens Axboe47f767c2011-07-12 21:17:49 +0200106static inline int gettid(void)
107{
108 return syscall(__NR_gettid);
109}
110
Jens Axboe3feedc62006-05-28 20:13:51 +0200111#define SPLICE_DEF_SIZE (64*1024)
112
Jens Axboeebac4652005-12-08 15:25:21 +0100113#ifndef BLKGETSIZE64
114#define BLKGETSIZE64 _IOR(0x12,114,size_t)
115#endif
116
Jens Axboee5b401d2006-10-18 16:03:40 +0200117#ifndef BLKFLSBUF
118#define BLKFLSBUF _IO(0x12,97)
119#endif
120
Jens Axboea5f30272010-07-19 16:19:55 -0600121#ifndef BLKDISCARD
122#define BLKDISCARD _IO(0x12,119)
123#endif
124
Jens Axboee2e58882011-01-04 08:36:06 +0100125static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboee5b401d2006-10-18 16:03:40 +0200126{
Bruce Cranecc314b2011-01-04 10:59:30 +0100127 return ioctl(f->fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200128}
129
Bruce Cranecc314b2011-01-04 10:59:30 +0100130static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100131{
Bruce Cranecc314b2011-01-04 10:59:30 +0100132 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
Jens Axboeebac4652005-12-08 15:25:21 +0100133 return 0;
134
135 return errno;
136}
137
Jens Axboe32cd46a2006-06-07 13:40:40 +0200138static inline unsigned long long os_phys_mem(void)
139{
140 long pagesize, pages;
141
142 pagesize = sysconf(_SC_PAGESIZE);
143 pages = sysconf(_SC_PHYS_PAGES);
144 if (pages == -1 || pagesize == -1)
145 return 0;
146
147 return (unsigned long long) pages * (unsigned long long) pagesize;
148}
149
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200150static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
151{
152 srand48_r(seed, rs);
153}
154
155static inline long os_random_long(os_random_state_t *rs)
156{
157 long val;
158
159 lrand48_r(rs, &val);
160 return val;
161}
162
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200163static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200164{
165 struct raw_config_request rq;
166 int fd;
167
168 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200169 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200170
171 /*
172 * we should be able to find /dev/rawctl or /dev/raw/rawctl
173 */
174 fd = open("/dev/rawctl", O_RDONLY);
175 if (fd < 0) {
176 fd = open("/dev/raw/rawctl", O_RDONLY);
177 if (fd < 0)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200178 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200179 }
180
181 rq.raw_minor = minor(dev);
182 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
183 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200184 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200185 }
186
187 close(fd);
188 *majdev = rq.block_major;
189 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200190 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200191}
192
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200193#ifdef O_NOATIME
Jens Axboe5921e802008-05-30 15:02:38 +0200194#define FIO_O_NOATIME O_NOATIME
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200195#else
196#define FIO_O_NOATIME 0
197#endif
Jens Axboe5921e802008-05-30 15:02:38 +0200198
Jens Axboea1c58072009-08-04 23:17:02 +0200199#ifdef MADV_REMOVE
200#define FIO_MADV_FREE MADV_REMOVE
201#endif
202
Jens Axboeff245192011-10-04 14:30:33 +0200203#define fio_swap16(x) __bswap_16(x)
204#define fio_swap32(x) __bswap_32(x)
205#define fio_swap64(x) __bswap_64(x)
206
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200207#define CACHE_LINE_FILE \
208 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
209
210static inline int arch_cache_line_size(void)
211{
212 char size[32];
213 int fd, ret;
214
215 fd = open(CACHE_LINE_FILE, O_RDONLY);
216 if (fd < 0)
217 return -1;
218
219 ret = read(fd, size, sizeof(size));
Jens Axboeec76f152009-06-09 13:05:32 +0200220
221 close(fd);
222
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200223 if (ret <= 0)
224 return -1;
Jens Axboeec76f152009-06-09 13:05:32 +0200225 else
226 return atoi(size);
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200227}
228
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200229static inline unsigned long long get_fs_size(const char *path)
230{
231 unsigned long long ret;
232 struct statfs s;
233
234 if (statfs(path, &s) < 0)
235 return -1ULL;
236
237 ret = s.f_bsize;
238 ret *= (unsigned long long) s.f_bfree;
239 return ret;
240}
241
Jens Axboea5f30272010-07-19 16:19:55 -0600242static inline int os_trim(int fd, unsigned long long start,
243 unsigned long long len)
244{
245 uint64_t range[2];
246
247 range[0] = start;
248 range[1] = len;
249
250 if (!ioctl(fd, BLKDISCARD, range))
251 return 0;
252
253 return errno;
254}
255
Jens Axboe7e09a9f2013-01-30 13:58:58 +0100256#ifdef CONFIG_SCHED_IDLE
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100257static inline int fio_set_sched_idle(void)
258{
259 struct sched_param p = { .sched_priority = 0, };
260 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
261}
Jens Axboe7e09a9f2013-01-30 13:58:58 +0100262#endif
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100263
Jens Axboeebac4652005-12-08 15:25:21 +0100264#endif