Merge branch 'master' of https://github.com/davidzengxhsh/fio
[fio.git] / os / os-linux.h
1 #ifndef FIO_OS_LINUX_H
2 #define FIO_OS_LINUX_H
3
4 #define FIO_OS  os_linux
5
6 #include <sys/ioctl.h>
7 #include <sys/uio.h>
8 #include <sys/syscall.h>
9 #include <sys/sysmacros.h>
10 #include <sys/vfs.h>
11 #include <sys/mman.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14 #include <errno.h>
15 #include <sched.h>
16 #include <linux/unistd.h>
17 #include <linux/raw.h>
18 #include <linux/major.h>
19 #include <byteswap.h>
20
21 #include "binject.h"
22 #include "../file.h"
23
24 #define FIO_HAVE_CPU_AFFINITY
25 #define FIO_HAVE_DISK_UTIL
26 #define FIO_HAVE_SGIO
27 #define FIO_HAVE_IOPRIO
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_PSHARED_MUTEX
34 #define FIO_HAVE_CL_SIZE
35 #define FIO_HAVE_CGROUPS
36 #define FIO_HAVE_FS_STAT
37 #define FIO_HAVE_TRIM
38 #define FIO_HAVE_BINJECT
39 #define FIO_HAVE_GETTID
40 #define FIO_USE_GENERIC_INIT_RANDOM_STATE
41
42 #ifdef MAP_HUGETLB
43 #define FIO_HAVE_MMAP_HUGE
44 #endif
45
46 #define OS_MAP_ANON             MAP_ANONYMOUS
47
48 typedef cpu_set_t os_cpu_mask_t;
49
50 typedef struct drand48_data os_random_state_t;
51
52 #ifdef CONFIG_3ARG_AFFINITY
53 #define fio_setaffinity(pid, cpumask)           \
54         sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
55 #define fio_getaffinity(pid, ptr)       \
56         sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
57 #elif defined(CONFIG_2ARG_AFFINITY)
58 #define fio_setaffinity(pid, cpumask)   \
59         sched_setaffinity((pid), &(cpumask))
60 #define fio_getaffinity(pid, ptr)       \
61         sched_getaffinity((pid), (ptr))
62 #endif
63
64 #define fio_cpu_clear(mask, cpu)        (void) CPU_CLR((cpu), (mask))
65 #define fio_cpu_set(mask, cpu)          (void) CPU_SET((cpu), (mask))
66 #define fio_cpu_isset(mask, cpu)        CPU_ISSET((cpu), (mask))
67 #define fio_cpu_count(mask)             CPU_COUNT((mask))
68
69 static inline int fio_cpuset_init(os_cpu_mask_t *mask)
70 {
71         CPU_ZERO(mask);
72         return 0;
73 }
74
75 static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
76 {
77         return 0;
78 }
79
80 #define FIO_MAX_CPUS                    CPU_SETSIZE
81
82 enum {
83         IOPRIO_CLASS_NONE,
84         IOPRIO_CLASS_RT,
85         IOPRIO_CLASS_BE,
86         IOPRIO_CLASS_IDLE,
87 };
88
89 enum {
90         IOPRIO_WHO_PROCESS = 1,
91         IOPRIO_WHO_PGRP,
92         IOPRIO_WHO_USER,
93 };
94
95 #define IOPRIO_BITS             16
96 #define IOPRIO_CLASS_SHIFT      13
97
98 static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
99 {
100         /*
101          * If no class is set, assume BE
102          */
103         if (!ioprio_class)
104                 ioprio_class = IOPRIO_CLASS_BE;
105
106         ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
107         return syscall(__NR_ioprio_set, which, who, ioprio);
108 }
109
110 static inline int gettid(void)
111 {
112         return syscall(__NR_gettid);
113 }
114
115 #define SPLICE_DEF_SIZE (64*1024)
116
117 #ifndef BLKGETSIZE64
118 #define BLKGETSIZE64    _IOR(0x12,114,size_t)
119 #endif
120
121 #ifndef BLKFLSBUF
122 #define BLKFLSBUF       _IO(0x12,97)
123 #endif
124
125 #ifndef BLKDISCARD
126 #define BLKDISCARD      _IO(0x12,119)
127 #endif
128
129 static inline int blockdev_invalidate_cache(struct fio_file *f)
130 {
131         return ioctl(f->fd, BLKFLSBUF);
132 }
133
134 static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
135 {
136         if (!ioctl(f->fd, BLKGETSIZE64, bytes))
137                 return 0;
138
139         return errno;
140 }
141
142 static inline unsigned long long os_phys_mem(void)
143 {
144         long pagesize, pages;
145
146         pagesize = sysconf(_SC_PAGESIZE);
147         pages = sysconf(_SC_PHYS_PAGES);
148         if (pages == -1 || pagesize == -1)
149                 return 0;
150
151         return (unsigned long long) pages * (unsigned long long) pagesize;
152 }
153
154 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
155 {
156         srand48_r(seed, rs);
157 }
158
159 static inline long os_random_long(os_random_state_t *rs)
160 {
161         long val;
162
163         lrand48_r(rs, &val);
164         return val;
165 }
166
167 static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
168 {
169         struct raw_config_request rq;
170         int fd;
171
172         if (major(dev) != RAW_MAJOR)
173                 return 1;
174
175         /*
176          * we should be able to find /dev/rawctl or /dev/raw/rawctl
177          */
178         fd = open("/dev/rawctl", O_RDONLY);
179         if (fd < 0) {
180                 fd = open("/dev/raw/rawctl", O_RDONLY);
181                 if (fd < 0)
182                         return 1;
183         }
184
185         rq.raw_minor = minor(dev);
186         if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
187                 close(fd);
188                 return 1;
189         }
190
191         close(fd);
192         *majdev = rq.block_major;
193         *mindev = rq.block_minor;
194         return 0;
195 }
196
197 #ifdef O_NOATIME
198 #define FIO_O_NOATIME   O_NOATIME
199 #else
200 #define FIO_O_NOATIME   0
201 #endif
202
203 #ifdef O_ATOMIC
204 #define OS_O_ATOMIC     O_ATOMIC
205 #else
206 #define OS_O_ATOMIC     040000000
207 #endif
208
209 #ifdef MADV_REMOVE
210 #define FIO_MADV_FREE   MADV_REMOVE
211 #endif
212
213 #if defined(__builtin_bswap16)
214 #define fio_swap16(x)   __builtin_bswap16(x)
215 #else
216 #define fio_swap16(x)   __bswap_16(x)
217 #endif
218 #if defined(__builtin_bswap32)
219 #define fio_swap32(x)   __builtin_bswap32(x)
220 #else
221 #define fio_swap32(x)   __bswap_32(x)
222 #endif
223 #if defined(__builtin_bswap64)
224 #define fio_swap64(x)   __builtin_bswap64(x)
225 #else
226 #define fio_swap64(x)   __bswap_64(x)
227 #endif
228
229 #define CACHE_LINE_FILE \
230         "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
231
232 static inline int arch_cache_line_size(void)
233 {
234         char size[32];
235         int fd, ret;
236
237         fd = open(CACHE_LINE_FILE, O_RDONLY);
238         if (fd < 0)
239                 return -1;
240
241         ret = read(fd, size, sizeof(size));
242
243         close(fd);
244
245         if (ret <= 0)
246                 return -1;
247         else
248                 return atoi(size);
249 }
250
251 static inline unsigned long long get_fs_free_size(const char *path)
252 {
253         unsigned long long ret;
254         struct statfs s;
255
256         if (statfs(path, &s) < 0)
257                 return -1ULL;
258
259         ret = s.f_bsize;
260         ret *= (unsigned long long) s.f_bfree;
261         return ret;
262 }
263
264 static inline int os_trim(int fd, unsigned long long start,
265                           unsigned long long len)
266 {
267         uint64_t range[2];
268
269         range[0] = start;
270         range[1] = len;
271
272         if (!ioctl(fd, BLKDISCARD, range))
273                 return 0;
274
275         return errno;
276 }
277
278 #ifdef CONFIG_SCHED_IDLE
279 static inline int fio_set_sched_idle(void)
280 {
281         struct sched_param p = { .sched_priority = 0, };
282         return sched_setscheduler(gettid(), SCHED_IDLE, &p);
283 }
284 #endif
285
286 #ifndef POSIX_FADV_STREAMID
287 #define POSIX_FADV_STREAMID     8
288 #endif
289
290 #define FIO_HAVE_STREAMID
291
292 #endif