blob: 6f2372bcd7e48bc7074928a4afa0212202ebade3 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001#ifndef FIO_OS_LINUX_H
2#define FIO_OS_LINUX_H
3
4#include <sys/ioctl.h>
Jens Axboe8756e4d2006-05-27 20:24:53 +02005#include <sys/uio.h>
Jens Axboe3c39a372006-06-06 20:56:12 +02006#include <sys/syscall.h>
7#include <unistd.h>
Jens Axboe97fbdfa2006-10-05 13:44:15 +02008#include <fcntl.h>
Jens Axboeea421792006-07-11 09:11:07 +02009#include <linux/unistd.h>
Jens Axboe07e5b262007-04-02 14:46:07 +020010#include <linux/raw.h>
11#include <linux/major.h>
Jens Axboeebac4652005-12-08 15:25:21 +010012
Jens Axboef3de88a2008-02-24 21:36:00 +010013#include "indirect.h"
14
Jens Axboeebac4652005-12-08 15:25:21 +010015#define FIO_HAVE_LIBAIO
16#define FIO_HAVE_POSIXAIO
17#define FIO_HAVE_FADVISE
18#define FIO_HAVE_CPU_AFFINITY
19#define FIO_HAVE_DISK_UTIL
20#define FIO_HAVE_SGIO
Jens Axboeba4f8922006-01-26 13:45:54 -080021#define FIO_HAVE_IOPRIO
Jens Axboe8756e4d2006-05-27 20:24:53 +020022#define FIO_HAVE_SPLICE
Jens Axboe22f78b32006-06-07 11:30:07 +020023#define FIO_HAVE_IOSCHED_SWITCH
Jens Axboe2c0ecd22006-06-08 13:25:41 +020024#define FIO_HAVE_ODIRECT
Jens Axboe74b025b2006-12-19 15:18:14 +010025#define FIO_HAVE_HUGETLB
Jens Axboe07e5b262007-04-02 14:46:07 +020026#define FIO_HAVE_RAWBIND
Jens Axboe5e62c222007-05-22 13:27:30 +020027#define FIO_HAVE_BLKTRACE
Jens Axboe5921e802008-05-30 15:02:38 +020028#define FIO_HAVE_STRSEP
Jens Axboeb6959b52007-02-14 21:40:27 +000029
Jens Axboeebac4652005-12-08 15:25:21 +010030#define OS_MAP_ANON (MAP_ANONYMOUS)
31
Shawn Lewis4d8947d2007-07-25 07:51:58 +020032#ifndef CLOCK_MONOTONIC
33#define CLOCK_MONOTONIC 1
34#endif
35
36#ifdef FIO_HAVE_CPU_AFFINITY
Jens Axboeebac4652005-12-08 15:25:21 +010037typedef cpu_set_t os_cpu_mask_t;
Shawn Lewis4d8947d2007-07-25 07:51:58 +020038#else
39typedef int os_cpu_mask_t;
40#endif
Jens Axboe6dfd46b2006-06-07 13:57:06 +020041typedef struct drand48_data os_random_state_t;
Jens Axboeebac4652005-12-08 15:25:21 +010042
43/*
44 * we want fadvise64 really, but it's so tangled... later
45 */
Shawn Lewis4d8947d2007-07-25 07:51:58 +020046#ifdef FIO_HAVE_FADVISE
Jens Axboeebac4652005-12-08 15:25:21 +010047#define fadvise(fd, off, len, advice) \
48 posix_fadvise((fd), (off_t)(off), (len), (advice))
Shawn Lewis4d8947d2007-07-25 07:51:58 +020049#endif
Jens Axboeebac4652005-12-08 15:25:21 +010050
Jens Axboec8f025f2007-05-22 17:06:24 +020051/*
52 * If you are on an ancient glibc (2.3.2), then define GLIBC_2_3_2 if you want
53 * the affinity helpers to work.
54 */
Shawn Lewis4d8947d2007-07-25 07:51:58 +020055#ifdef FIO_HAVE_CPU_AFFINITY
Jens Axboec8f025f2007-05-22 17:06:24 +020056#ifndef GLIBC_2_3_2
Jens Axboeebac4652005-12-08 15:25:21 +010057#define fio_setaffinity(td) \
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010058 sched_setaffinity((td)->pid, sizeof((td)->o.cpumask), &(td)->o.cpumask)
Jens Axboeebac4652005-12-08 15:25:21 +010059#define fio_getaffinity(pid, ptr) \
60 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
Jens Axboec8f025f2007-05-22 17:06:24 +020061#else
62#define fio_setaffinity(td) \
63 sched_setaffinity((td)->pid, &(td)->o.cpumask)
64#define fio_getaffinity(pid, ptr) \
65 sched_getaffinity((pid), (ptr))
66#endif
Shawn Lewis4d8947d2007-07-25 07:51:58 +020067#endif
68
Jens Axboeebac4652005-12-08 15:25:21 +010069static inline int ioprio_set(int which, int who, int ioprio)
70{
71 return syscall(__NR_ioprio_set, which, who, ioprio);
72}
73
Jens Axboe97fbdfa2006-10-05 13:44:15 +020074/*
75 * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
76 * aren't either.
77 */
78#ifndef SPLICE_F_MOVE
Jens Axboe8756e4d2006-05-27 20:24:53 +020079#define SPLICE_F_MOVE (0x01) /* move pages instead of copying */
80#define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
81 /* we may still block on the fd we splice */
82 /* from/to, of course */
83#define SPLICE_F_MORE (0x04) /* expect more data */
84#define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */
85
Jens Axboe97fbdfa2006-10-05 13:44:15 +020086static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
Jens Axboe495ee9b2007-06-08 11:24:58 +020087 size_t len, unsigned int flags)
Jens Axboe97fbdfa2006-10-05 13:44:15 +020088{
89 return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
90}
91
92static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
93{
94 return syscall(__NR_sys_tee, fdin, fdout, len, flags);
95}
96
97static inline int vmsplice(int fd, const struct iovec *iov,
98 unsigned long nr_segs, unsigned int flags)
99{
100 return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
101}
102#endif
103
Jens Axboe3feedc62006-05-28 20:13:51 +0200104#define SPLICE_DEF_SIZE (64*1024)
105
Jens Axboeb6959b52007-02-14 21:40:27 +0000106#ifdef FIO_HAVE_SYSLET
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100107
108struct syslet_uatom;
109struct async_head_user;
110
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100111/*
112 * syslet stuff
113 */
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100114static inline struct syslet_uatom *
115async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100116{
Jens Axboe1df341f2007-02-22 14:08:13 +0100117 return (void *) syscall(__NR_async_exec, atom, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100118}
119
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100120static inline long
121async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
122 struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100123{
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +0100124 return syscall(__NR_async_wait, min_wait_events,
125 user_ring_idx, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100126}
127
Jens Axboe7756b0d2007-02-26 10:22:31 +0100128static inline long async_thread(void *event, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100129{
Jens Axboe7756b0d2007-02-26 10:22:31 +0100130 return syscall(__NR_async_thread, event, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100131}
132
133static inline long umem_add(unsigned long *uptr, unsigned long inc)
134{
135 return syscall(__NR_umem_add, uptr, inc);
136}
Jens Axboeb6959b52007-02-14 21:40:27 +0000137#endif /* FIO_HAVE_SYSLET */
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100138
Jens Axboeebac4652005-12-08 15:25:21 +0100139enum {
Jens Axboe6cefbe32007-04-18 12:46:56 +0200140 IOPRIO_CLASS_NONE,
141 IOPRIO_CLASS_RT,
142 IOPRIO_CLASS_BE,
143 IOPRIO_CLASS_IDLE,
144};
145
146enum {
Jens Axboeebac4652005-12-08 15:25:21 +0100147 IOPRIO_WHO_PROCESS = 1,
148 IOPRIO_WHO_PGRP,
149 IOPRIO_WHO_USER,
150};
151
Jens Axboe6cefbe32007-04-18 12:46:56 +0200152#define IOPRIO_BITS 16
Jens Axboeebac4652005-12-08 15:25:21 +0100153#define IOPRIO_CLASS_SHIFT 13
154
155#ifndef BLKGETSIZE64
156#define BLKGETSIZE64 _IOR(0x12,114,size_t)
157#endif
158
Jens Axboee5b401d2006-10-18 16:03:40 +0200159#ifndef BLKFLSBUF
160#define BLKFLSBUF _IO(0x12,97)
161#endif
162
163static inline int blockdev_invalidate_cache(int fd)
164{
Jens Axboea16211b2007-03-15 11:35:33 +0100165 return ioctl(fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200166}
167
Jens Axboe9104f872005-12-28 23:50:45 +0100168static inline int blockdev_size(int fd, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100169{
170 if (!ioctl(fd, BLKGETSIZE64, bytes))
171 return 0;
172
173 return errno;
174}
175
Jens Axboe32cd46a2006-06-07 13:40:40 +0200176static inline unsigned long long os_phys_mem(void)
177{
178 long pagesize, pages;
179
180 pagesize = sysconf(_SC_PAGESIZE);
181 pages = sysconf(_SC_PHYS_PAGES);
182 if (pages == -1 || pagesize == -1)
183 return 0;
184
185 return (unsigned long long) pages * (unsigned long long) pagesize;
186}
187
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200188static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
189{
190 srand48_r(seed, rs);
191}
192
193static inline long os_random_long(os_random_state_t *rs)
194{
195 long val;
196
197 lrand48_r(rs, &val);
198 return val;
199}
200
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200201static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200202{
203 struct raw_config_request rq;
204 int fd;
205
206 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200207 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200208
209 /*
210 * we should be able to find /dev/rawctl or /dev/raw/rawctl
211 */
212 fd = open("/dev/rawctl", O_RDONLY);
213 if (fd < 0) {
214 fd = open("/dev/raw/rawctl", O_RDONLY);
215 if (fd < 0)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200216 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200217 }
218
219 rq.raw_minor = minor(dev);
220 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
221 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200222 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200223 }
224
225 close(fd);
226 *majdev = rq.block_major;
227 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200228 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200229}
230
Jens Axboe5921e802008-05-30 15:02:38 +0200231#define FIO_O_NOATIME O_NOATIME
232
Jens Axboeebac4652005-12-08 15:25:21 +0100233#endif