HP-UX disk query size
[fio.git] / os / os-linux.h
1 #ifndef FIO_OS_LINUX_H
2 #define FIO_OS_LINUX_H
3
4 #include <sys/ioctl.h>
5 #include <sys/uio.h>
6 #include <sys/syscall.h>
7 #include <sys/vfs.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <sched.h>
12 #include <linux/unistd.h>
13 #include <linux/raw.h>
14 #include <linux/major.h>
15
16 #include "indirect.h"
17 #include "binject.h"
18 #include "../file.h"
19
20 #define FIO_HAVE_LIBAIO
21 #define FIO_HAVE_POSIXAIO
22 #define FIO_HAVE_FADVISE
23 #define FIO_HAVE_CPU_AFFINITY
24 #define FIO_HAVE_DISK_UTIL
25 #define FIO_HAVE_SGIO
26 #define FIO_HAVE_IOPRIO
27 #define FIO_HAVE_SPLICE
28 #define FIO_HAVE_IOSCHED_SWITCH
29 #define FIO_HAVE_ODIRECT
30 #define FIO_HAVE_HUGETLB
31 #define FIO_HAVE_RAWBIND
32 #define FIO_HAVE_BLKTRACE
33 #define FIO_HAVE_STRSEP
34 #define FIO_HAVE_FALLOCATE
35 #define FIO_HAVE_LINUX_FALLOCATE
36 #define FIO_HAVE_POSIXAIO_FSYNC
37 #define FIO_HAVE_PSHARED_MUTEX
38 #define FIO_HAVE_CL_SIZE
39 #define FIO_HAVE_CGROUPS
40 #define FIO_HAVE_FDATASYNC
41 #define FIO_HAVE_FS_STAT
42 #define FIO_HAVE_TRIM
43 #define FIO_HAVE_BINJECT
44 #define FIO_HAVE_CLOCK_MONOTONIC
45
46 #ifdef SYNC_FILE_RANGE_WAIT_BEFORE
47 #define FIO_HAVE_SYNC_FILE_RANGE
48 #endif
49
50 #define OS_MAP_ANON             MAP_ANONYMOUS
51
52 #ifndef CLOCK_MONOTONIC
53 #define CLOCK_MONOTONIC 1
54 #endif
55
56 typedef cpu_set_t os_cpu_mask_t;
57
58 typedef struct drand48_data os_random_state_t;
59
60 /*
61  * we want fadvise64 really, but it's so tangled... later
62  */
63 #ifdef FIO_HAVE_FADVISE
64 #define fadvise(fd, off, len, advice)   \
65         posix_fadvise((fd), (off_t)(off), (len), (advice))
66 #endif
67
68 /*
69  * If you are on an ancient glibc (2.3.2), then define GLIBC_2_3_2 if you want
70  * the affinity helpers to work.
71  */
72 #ifndef GLIBC_2_3_2
73 #define fio_setaffinity(pid, cpumask)           \
74         sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
75 #define fio_getaffinity(pid, ptr)       \
76         sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
77 #else
78 #define fio_setaffinity(pid, cpumask)   \
79         sched_setaffinity((pid), &(cpumask))
80 #define fio_getaffinity(pid, ptr)       \
81         sched_getaffinity((pid), (ptr))
82 #endif
83
84 #define fio_cpu_clear(mask, cpu)        CPU_CLR((cpu), (mask))
85 #define fio_cpu_set(mask, cpu)          CPU_SET((cpu), (mask))
86
87 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
88 {
89         CPU_ZERO(mask);
90         return 0;
91 }
92
93 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
94 {
95         return 0;
96 }
97
98 #define FIO_MAX_CPUS                    CPU_SETSIZE
99
100 static inline int ioprio_set(int which, int who, int ioprio)
101 {
102         return syscall(__NR_ioprio_set, which, who, ioprio);
103 }
104
105 /*
106  * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
107  * aren't either.
108  */
109 #ifndef SPLICE_F_MOVE
110 #define SPLICE_F_MOVE   (0x01)  /* move pages instead of copying */
111 #define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
112                                  /* we may still block on the fd we splice */
113                                  /* from/to, of course */
114 #define SPLICE_F_MORE   (0x04)  /* expect more data */
115 #define SPLICE_F_GIFT   (0x08)  /* pages passed in are a gift */
116
117 static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
118                          size_t len, unsigned int flags)
119 {
120         return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
121 }
122
123 static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
124 {
125         return syscall(__NR_sys_tee, fdin, fdout, len, flags);
126 }
127
128 static inline int vmsplice(int fd, const struct iovec *iov,
129                            unsigned long nr_segs, unsigned int flags)
130 {
131         return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
132 }
133 #endif
134
135 #define SPLICE_DEF_SIZE (64*1024)
136
137 #ifdef FIO_HAVE_SYSLET
138
139 struct syslet_uatom;
140 struct async_head_user;
141
142 /*
143  * syslet stuff
144  */
145 static inline struct syslet_uatom *
146 async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
147 {
148         return (struct syslet_uatom *) syscall(__NR_async_exec, atom, ahu);
149 }
150
151 static inline long
152 async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
153            struct async_head_user *ahu)
154 {
155         return syscall(__NR_async_wait, min_wait_events,
156                         user_ring_idx, ahu);
157 }
158
159 static inline long async_thread(void *event, struct async_head_user *ahu)
160 {
161         return syscall(__NR_async_thread, event, ahu);
162 }
163
164 static inline long umem_add(unsigned long *uptr, unsigned long inc)
165 {
166         return syscall(__NR_umem_add, uptr, inc);
167 }
168 #endif /* FIO_HAVE_SYSLET */
169
170 enum {
171         IOPRIO_CLASS_NONE,
172         IOPRIO_CLASS_RT,
173         IOPRIO_CLASS_BE,
174         IOPRIO_CLASS_IDLE,
175 };
176
177 enum {
178         IOPRIO_WHO_PROCESS = 1,
179         IOPRIO_WHO_PGRP,
180         IOPRIO_WHO_USER,
181 };
182
183 #define IOPRIO_BITS             16
184 #define IOPRIO_CLASS_SHIFT      13
185
186 #ifndef BLKGETSIZE64
187 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
188 #endif
189
190 #ifndef BLKFLSBUF
191 #define BLKFLSBUF       _IO(0x12,97)
192 #endif
193
194 #ifndef BLKDISCARD
195 #define BLKDISCARD      _IO(0x12,119)
196 #endif
197
198 static inline int blockdev_invalidate_cache(struct fio_file *f)
199 {
200         return ioctl(f->fd, BLKFLSBUF);
201 }
202
203 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
204 {
205         if (!ioctl(f->fd, BLKGETSIZE64, bytes))
206                 return 0;
207
208         return errno;
209 }
210
211 static inline unsigned long long os_phys_mem(void)
212 {
213         long pagesize, pages;
214
215         pagesize = sysconf(_SC_PAGESIZE);
216         pages = sysconf(_SC_PHYS_PAGES);
217         if (pages == -1 || pagesize == -1)
218                 return 0;
219
220         return (unsigned long long) pages * (unsigned long long) pagesize;
221 }
222
223 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
224 {
225         srand48_r(seed, rs);
226 }
227
228 static inline long os_random_long(os_random_state_t *rs)
229 {
230         long val;
231
232         lrand48_r(rs, &val);
233         return val;
234 }
235
236 static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
237 {
238         struct raw_config_request rq;
239         int fd;
240
241         if (major(dev) != RAW_MAJOR)
242                 return 1;
243
244         /*
245          * we should be able to find /dev/rawctl or /dev/raw/rawctl
246          */
247         fd = open("/dev/rawctl", O_RDONLY);
248         if (fd < 0) {
249                 fd = open("/dev/raw/rawctl", O_RDONLY);
250                 if (fd < 0)
251                         return 1;
252         }
253
254         rq.raw_minor = minor(dev);
255         if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
256                 close(fd);
257                 return 1;
258         }
259
260         close(fd);
261         *majdev = rq.block_major;
262         *mindev = rq.block_minor;
263         return 0;
264 }
265
266 #ifdef O_NOATIME
267 #define FIO_O_NOATIME   O_NOATIME
268 #else
269 #define FIO_O_NOATIME   0
270 #endif
271
272 #ifdef MADV_REMOVE
273 #define FIO_MADV_FREE   MADV_REMOVE
274 #endif
275
276 #define CACHE_LINE_FILE \
277         "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
278
279 static inline int arch_cache_line_size(void)
280 {
281         char size[32];
282         int fd, ret;
283
284         fd = open(CACHE_LINE_FILE, O_RDONLY);
285         if (fd < 0)
286                 return -1;
287
288         ret = read(fd, size, sizeof(size));
289
290         close(fd);
291
292         if (ret <= 0)
293                 return -1;
294         else
295                 return atoi(size);
296 }
297
298 static inline unsigned long long get_fs_size(const char *path)
299 {
300         unsigned long long ret;
301         struct statfs s;
302
303         if (statfs(path, &s) < 0)
304                 return -1ULL;
305
306         ret = s.f_bsize;
307         ret *= (unsigned long long) s.f_bfree;
308         return ret;
309 }
310
311 static inline int os_trim(int fd, unsigned long long start,
312                           unsigned long long len)
313 {
314         uint64_t range[2];
315
316         range[0] = start;
317         range[1] = len;
318
319         if (!ioctl(fd, BLKDISCARD, range))
320                 return 0;
321
322         return errno;
323 }
324
325 #endif