[PATCH] Sync and invalidate cache prior to running verify
[fio.git] / os-linux.h
... / ...
CommitLineData
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 <unistd.h>
8#include <fcntl.h>
9#include <linux/unistd.h>
10
11#define FIO_HAVE_LIBAIO
12#define FIO_HAVE_POSIXAIO
13#define FIO_HAVE_FADVISE
14#define FIO_HAVE_CPU_AFFINITY
15#define FIO_HAVE_DISK_UTIL
16#define FIO_HAVE_SGIO
17#define FIO_HAVE_IOPRIO
18#define FIO_HAVE_SPLICE
19#define FIO_HAVE_IOSCHED_SWITCH
20#define FIO_HAVE_ODIRECT
21
22#define OS_MAP_ANON (MAP_ANONYMOUS)
23
24typedef cpu_set_t os_cpu_mask_t;
25typedef struct drand48_data os_random_state_t;
26
27/*
28 * we want fadvise64 really, but it's so tangled... later
29 */
30#define fadvise(fd, off, len, advice) \
31 posix_fadvise((fd), (off_t)(off), (len), (advice))
32
33#define fio_setaffinity(td) \
34 sched_setaffinity((td)->pid, sizeof((td)->cpumask), &(td)->cpumask)
35#define fio_getaffinity(pid, ptr) \
36 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
37
38static inline int ioprio_set(int which, int who, int ioprio)
39{
40 return syscall(__NR_ioprio_set, which, who, ioprio);
41}
42
43/*
44 * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
45 * aren't either.
46 */
47#ifndef SPLICE_F_MOVE
48#define SPLICE_F_MOVE (0x01) /* move pages instead of copying */
49#define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
50 /* we may still block on the fd we splice */
51 /* from/to, of course */
52#define SPLICE_F_MORE (0x04) /* expect more data */
53#define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */
54
55static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
56 size_t len, unsigned long flags)
57{
58 return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
59}
60
61static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
62{
63 return syscall(__NR_sys_tee, fdin, fdout, len, flags);
64}
65
66static inline int vmsplice(int fd, const struct iovec *iov,
67 unsigned long nr_segs, unsigned int flags)
68{
69 return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
70}
71#endif
72
73#define SPLICE_DEF_SIZE (64*1024)
74
75enum {
76 IOPRIO_WHO_PROCESS = 1,
77 IOPRIO_WHO_PGRP,
78 IOPRIO_WHO_USER,
79};
80
81#define IOPRIO_CLASS_SHIFT 13
82
83#ifndef BLKGETSIZE64
84#define BLKGETSIZE64 _IOR(0x12,114,size_t)
85#endif
86
87#ifndef BLKFLSBUF
88#define BLKFLSBUF _IO(0x12,97)
89#endif
90
91static inline int blockdev_invalidate_cache(int fd)
92{
93 if (!ioctl(fd, BLKFLSBUF))
94 return 0;
95
96 return errno;
97}
98
99static inline int blockdev_size(int fd, unsigned long long *bytes)
100{
101 if (!ioctl(fd, BLKGETSIZE64, bytes))
102 return 0;
103
104 return errno;
105}
106
107static inline unsigned long long os_phys_mem(void)
108{
109 long pagesize, pages;
110
111 pagesize = sysconf(_SC_PAGESIZE);
112 pages = sysconf(_SC_PHYS_PAGES);
113 if (pages == -1 || pagesize == -1)
114 return 0;
115
116 return (unsigned long long) pages * (unsigned long long) pagesize;
117}
118
119static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
120{
121 srand48_r(seed, rs);
122}
123
124static inline long os_random_long(os_random_state_t *rs)
125{
126 long val;
127
128 lrand48_r(rs, &val);
129 return val;
130}
131
132static inline double os_random_double(os_random_state_t *rs)
133{
134 double val;
135
136 drand48_r(rs, &val);
137 return val;
138}
139
140#endif