blob: d7eec0f2fd7d1bece339f9b61e723523755545ff [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 Axboe610a7302011-10-04 13:53:35 +020018#include <endian.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 Axboed2ce18b2008-12-12 20:51:40 +010065
66static inline int fio_cpuset_init(os_cpu_mask_t *mask)
67{
68 CPU_ZERO(mask);
69 return 0;
70}
71
72static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
73{
74 return 0;
75}
Jens Axboe6d459ee2008-12-12 20:02:58 +010076
77#define FIO_MAX_CPUS CPU_SETSIZE
Shawn Lewis4d8947d2007-07-25 07:51:58 +020078
Jens Axboeebac4652005-12-08 15:25:21 +010079static inline int ioprio_set(int which, int who, int ioprio)
80{
81 return syscall(__NR_ioprio_set, which, who, ioprio);
82}
83
Jens Axboe47f767c2011-07-12 21:17:49 +020084static inline int gettid(void)
85{
86 return syscall(__NR_gettid);
87}
88
Jens Axboe3feedc62006-05-28 20:13:51 +020089#define SPLICE_DEF_SIZE (64*1024)
90
Jens Axboeebac4652005-12-08 15:25:21 +010091enum {
Jens Axboe6cefbe32007-04-18 12:46:56 +020092 IOPRIO_CLASS_NONE,
93 IOPRIO_CLASS_RT,
94 IOPRIO_CLASS_BE,
95 IOPRIO_CLASS_IDLE,
96};
97
98enum {
Jens Axboeebac4652005-12-08 15:25:21 +010099 IOPRIO_WHO_PROCESS = 1,
100 IOPRIO_WHO_PGRP,
101 IOPRIO_WHO_USER,
102};
103
Jens Axboe6cefbe32007-04-18 12:46:56 +0200104#define IOPRIO_BITS 16
Jens Axboeebac4652005-12-08 15:25:21 +0100105#define IOPRIO_CLASS_SHIFT 13
106
107#ifndef BLKGETSIZE64
108#define BLKGETSIZE64 _IOR(0x12,114,size_t)
109#endif
110
Jens Axboee5b401d2006-10-18 16:03:40 +0200111#ifndef BLKFLSBUF
112#define BLKFLSBUF _IO(0x12,97)
113#endif
114
Jens Axboea5f30272010-07-19 16:19:55 -0600115#ifndef BLKDISCARD
116#define BLKDISCARD _IO(0x12,119)
117#endif
118
Jens Axboee2e58882011-01-04 08:36:06 +0100119static inline int blockdev_invalidate_cache(struct fio_file *f)
Jens Axboee5b401d2006-10-18 16:03:40 +0200120{
Bruce Cranecc314b2011-01-04 10:59:30 +0100121 return ioctl(f->fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200122}
123
Bruce Cranecc314b2011-01-04 10:59:30 +0100124static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100125{
Bruce Cranecc314b2011-01-04 10:59:30 +0100126 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
Jens Axboeebac4652005-12-08 15:25:21 +0100127 return 0;
128
129 return errno;
130}
131
Jens Axboe32cd46a2006-06-07 13:40:40 +0200132static inline unsigned long long os_phys_mem(void)
133{
134 long pagesize, pages;
135
136 pagesize = sysconf(_SC_PAGESIZE);
137 pages = sysconf(_SC_PHYS_PAGES);
138 if (pages == -1 || pagesize == -1)
139 return 0;
140
141 return (unsigned long long) pages * (unsigned long long) pagesize;
142}
143
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200144static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
145{
146 srand48_r(seed, rs);
147}
148
149static inline long os_random_long(os_random_state_t *rs)
150{
151 long val;
152
153 lrand48_r(rs, &val);
154 return val;
155}
156
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200157static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200158{
159 struct raw_config_request rq;
160 int fd;
161
162 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200163 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200164
165 /*
166 * we should be able to find /dev/rawctl or /dev/raw/rawctl
167 */
168 fd = open("/dev/rawctl", O_RDONLY);
169 if (fd < 0) {
170 fd = open("/dev/raw/rawctl", O_RDONLY);
171 if (fd < 0)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200172 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200173 }
174
175 rq.raw_minor = minor(dev);
176 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
177 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200178 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200179 }
180
181 close(fd);
182 *majdev = rq.block_major;
183 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200184 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200185}
186
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200187#ifdef O_NOATIME
Jens Axboe5921e802008-05-30 15:02:38 +0200188#define FIO_O_NOATIME O_NOATIME
Jens Axboe5b6f5c62008-06-11 10:21:43 +0200189#else
190#define FIO_O_NOATIME 0
191#endif
Jens Axboe5921e802008-05-30 15:02:38 +0200192
Jens Axboea1c58072009-08-04 23:17:02 +0200193#ifdef MADV_REMOVE
194#define FIO_MADV_FREE MADV_REMOVE
195#endif
196
Jens Axboe610a7302011-10-04 13:53:35 +0200197#if __BYTE_ORDER == __LITTLE_ENDIAN
198#define FIO_LITTLE_ENDIAN
199#elif __BYTE_ORDER == __BIG_ENDIAN
200#define FIO_BIG_ENDIAN
201#else
202#error "Unknown endianness"
203#endif
204
Jens Axboeff245192011-10-04 14:30:33 +0200205#define fio_swap16(x) __bswap_16(x)
206#define fio_swap32(x) __bswap_32(x)
207#define fio_swap64(x) __bswap_64(x)
208
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200209#define CACHE_LINE_FILE \
210 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
211
212static inline int arch_cache_line_size(void)
213{
214 char size[32];
215 int fd, ret;
216
217 fd = open(CACHE_LINE_FILE, O_RDONLY);
218 if (fd < 0)
219 return -1;
220
221 ret = read(fd, size, sizeof(size));
Jens Axboeec76f152009-06-09 13:05:32 +0200222
223 close(fd);
224
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200225 if (ret <= 0)
226 return -1;
Jens Axboeec76f152009-06-09 13:05:32 +0200227 else
228 return atoi(size);
Jens Axboeeb7ccf32009-04-29 09:48:04 +0200229}
230
Jens Axboe2e3bd4c2010-05-17 12:29:57 +0200231static inline unsigned long long get_fs_size(const char *path)
232{
233 unsigned long long ret;
234 struct statfs s;
235
236 if (statfs(path, &s) < 0)
237 return -1ULL;
238
239 ret = s.f_bsize;
240 ret *= (unsigned long long) s.f_bfree;
241 return ret;
242}
243
Jens Axboea5f30272010-07-19 16:19:55 -0600244static inline int os_trim(int fd, unsigned long long start,
245 unsigned long long len)
246{
247 uint64_t range[2];
248
249 range[0] = start;
250 range[1] = len;
251
252 if (!ioctl(fd, BLKDISCARD, range))
253 return 0;
254
255 return errno;
256}
257
Jens Axboeebac4652005-12-08 15:25:21 +0100258#endif