blob: 7fa0c9566a6d4e24405b93563f3958b87dd639bb [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
13#define FIO_HAVE_LIBAIO
14#define FIO_HAVE_POSIXAIO
15#define FIO_HAVE_FADVISE
16#define FIO_HAVE_CPU_AFFINITY
17#define FIO_HAVE_DISK_UTIL
18#define FIO_HAVE_SGIO
Jens Axboeba4f8922006-01-26 13:45:54 -080019#define FIO_HAVE_IOPRIO
Jens Axboe8756e4d2006-05-27 20:24:53 +020020#define FIO_HAVE_SPLICE
Jens Axboe22f78b32006-06-07 11:30:07 +020021#define FIO_HAVE_IOSCHED_SWITCH
Jens Axboe2c0ecd22006-06-08 13:25:41 +020022#define FIO_HAVE_ODIRECT
Jens Axboe74b025b2006-12-19 15:18:14 +010023#define FIO_HAVE_HUGETLB
Jens Axboe07e5b262007-04-02 14:46:07 +020024#define FIO_HAVE_RAWBIND
Jens Axboe5e62c222007-05-22 13:27:30 +020025#define FIO_HAVE_BLKTRACE
Jens Axboeb6959b52007-02-14 21:40:27 +000026
Jens Axboeebac4652005-12-08 15:25:21 +010027#define OS_MAP_ANON (MAP_ANONYMOUS)
28
29typedef cpu_set_t os_cpu_mask_t;
Jens Axboe6dfd46b2006-06-07 13:57:06 +020030typedef struct drand48_data os_random_state_t;
Jens Axboeebac4652005-12-08 15:25:21 +010031
32/*
33 * we want fadvise64 really, but it's so tangled... later
34 */
35#define fadvise(fd, off, len, advice) \
36 posix_fadvise((fd), (off_t)(off), (len), (advice))
37
38#define fio_setaffinity(td) \
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010039 sched_setaffinity((td)->pid, sizeof((td)->o.cpumask), &(td)->o.cpumask)
Jens Axboeebac4652005-12-08 15:25:21 +010040#define fio_getaffinity(pid, ptr) \
41 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
42
43static inline int ioprio_set(int which, int who, int ioprio)
44{
45 return syscall(__NR_ioprio_set, which, who, ioprio);
46}
47
Jens Axboe97fbdfa2006-10-05 13:44:15 +020048/*
49 * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
50 * aren't either.
51 */
52#ifndef SPLICE_F_MOVE
Jens Axboe8756e4d2006-05-27 20:24:53 +020053#define SPLICE_F_MOVE (0x01) /* move pages instead of copying */
54#define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
55 /* we may still block on the fd we splice */
56 /* from/to, of course */
57#define SPLICE_F_MORE (0x04) /* expect more data */
58#define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */
59
Jens Axboe97fbdfa2006-10-05 13:44:15 +020060static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
61 size_t len, unsigned long flags)
62{
63 return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
64}
65
66static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
67{
68 return syscall(__NR_sys_tee, fdin, fdout, len, flags);
69}
70
71static inline int vmsplice(int fd, const struct iovec *iov,
72 unsigned long nr_segs, unsigned int flags)
73{
74 return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
75}
76#endif
77
Jens Axboe3feedc62006-05-28 20:13:51 +020078#define SPLICE_DEF_SIZE (64*1024)
79
Jens Axboeb6959b52007-02-14 21:40:27 +000080#ifdef FIO_HAVE_SYSLET
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010081
82struct syslet_uatom;
83struct async_head_user;
84
Jens Axboea4f4fdd2007-02-14 01:16:39 +010085/*
86 * syslet stuff
87 */
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010088static inline struct syslet_uatom *
89async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +010090{
Jens Axboe1df341f2007-02-22 14:08:13 +010091 return (void *) syscall(__NR_async_exec, atom, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +010092}
93
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010094static inline long
95async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
96 struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +010097{
Ingo Molnarbf0dc8f2007-02-21 23:25:44 +010098 return syscall(__NR_async_wait, min_wait_events,
99 user_ring_idx, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100100}
101
Jens Axboe7756b0d2007-02-26 10:22:31 +0100102static inline long async_thread(void *event, struct async_head_user *ahu)
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100103{
Jens Axboe7756b0d2007-02-26 10:22:31 +0100104 return syscall(__NR_async_thread, event, ahu);
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100105}
106
107static inline long umem_add(unsigned long *uptr, unsigned long inc)
108{
109 return syscall(__NR_umem_add, uptr, inc);
110}
Jens Axboeb6959b52007-02-14 21:40:27 +0000111#endif /* FIO_HAVE_SYSLET */
Jens Axboea4f4fdd2007-02-14 01:16:39 +0100112
Jens Axboeebac4652005-12-08 15:25:21 +0100113enum {
Jens Axboe6cefbe32007-04-18 12:46:56 +0200114 IOPRIO_CLASS_NONE,
115 IOPRIO_CLASS_RT,
116 IOPRIO_CLASS_BE,
117 IOPRIO_CLASS_IDLE,
118};
119
120enum {
Jens Axboeebac4652005-12-08 15:25:21 +0100121 IOPRIO_WHO_PROCESS = 1,
122 IOPRIO_WHO_PGRP,
123 IOPRIO_WHO_USER,
124};
125
Jens Axboe6cefbe32007-04-18 12:46:56 +0200126#define IOPRIO_BITS 16
Jens Axboeebac4652005-12-08 15:25:21 +0100127#define IOPRIO_CLASS_SHIFT 13
128
129#ifndef BLKGETSIZE64
130#define BLKGETSIZE64 _IOR(0x12,114,size_t)
131#endif
132
Jens Axboee5b401d2006-10-18 16:03:40 +0200133#ifndef BLKFLSBUF
134#define BLKFLSBUF _IO(0x12,97)
135#endif
136
137static inline int blockdev_invalidate_cache(int fd)
138{
Jens Axboea16211b2007-03-15 11:35:33 +0100139 return ioctl(fd, BLKFLSBUF);
Jens Axboee5b401d2006-10-18 16:03:40 +0200140}
141
Jens Axboe9104f872005-12-28 23:50:45 +0100142static inline int blockdev_size(int fd, unsigned long long *bytes)
Jens Axboeebac4652005-12-08 15:25:21 +0100143{
144 if (!ioctl(fd, BLKGETSIZE64, bytes))
145 return 0;
146
147 return errno;
148}
149
Jens Axboe32cd46a2006-06-07 13:40:40 +0200150static inline unsigned long long os_phys_mem(void)
151{
152 long pagesize, pages;
153
154 pagesize = sysconf(_SC_PAGESIZE);
155 pages = sysconf(_SC_PHYS_PAGES);
156 if (pages == -1 || pagesize == -1)
157 return 0;
158
159 return (unsigned long long) pages * (unsigned long long) pagesize;
160}
161
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200162static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
163{
164 srand48_r(seed, rs);
165}
166
167static inline long os_random_long(os_random_state_t *rs)
168{
169 long val;
170
171 lrand48_r(rs, &val);
172 return val;
173}
174
175static inline double os_random_double(os_random_state_t *rs)
176{
177 double val;
178
179 drand48_r(rs, &val);
180 return val;
181}
182
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200183static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
Jens Axboe07e5b262007-04-02 14:46:07 +0200184{
185 struct raw_config_request rq;
186 int fd;
187
188 if (major(dev) != RAW_MAJOR)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200189 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200190
191 /*
192 * we should be able to find /dev/rawctl or /dev/raw/rawctl
193 */
194 fd = open("/dev/rawctl", O_RDONLY);
195 if (fd < 0) {
196 fd = open("/dev/raw/rawctl", O_RDONLY);
197 if (fd < 0)
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200198 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200199 }
200
201 rq.raw_minor = minor(dev);
202 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
203 close(fd);
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200204 return 1;
Jens Axboe07e5b262007-04-02 14:46:07 +0200205 }
206
207 close(fd);
208 *majdev = rq.block_major;
209 *mindev = rq.block_minor;
Jens Axboe8cc7afa2007-04-17 09:06:43 +0200210 return 0;
Jens Axboe07e5b262007-04-02 14:46:07 +0200211}
212
Jens Axboeebac4652005-12-08 15:25:21 +0100213#endif