Fixup bad logging types
[fio.git] / os / os-linux.h
CommitLineData
ebac4655
JA
1#ifndef FIO_OS_LINUX_H
2#define FIO_OS_LINUX_H
3
cca84643
JA
4#define FIO_OS os_linux
5
ebac4655 6#include <sys/ioctl.h>
8756e4d4 7#include <sys/uio.h>
3c39a379 8#include <sys/syscall.h>
2e3bd4c2 9#include <sys/vfs.h>
39b93568 10#include <sys/mman.h>
3c39a379 11#include <unistd.h>
97fbdfa1 12#include <fcntl.h>
3c2d93ed 13#include <errno.h>
d09913c2 14#include <sched.h>
ea421790 15#include <linux/unistd.h>
07e5b264
JA
16#include <linux/raw.h>
17#include <linux/major.h>
ebac4655 18
79a43187 19#include "binject.h"
e2e58886 20#include "../file.h"
f3de88a7 21
ebac4655
JA
22#define FIO_HAVE_CPU_AFFINITY
23#define FIO_HAVE_DISK_UTIL
24#define FIO_HAVE_SGIO
ba4f8923 25#define FIO_HAVE_IOPRIO
22f78b32 26#define FIO_HAVE_IOSCHED_SWITCH
2c0ecd28 27#define FIO_HAVE_ODIRECT
74b025b0 28#define FIO_HAVE_HUGETLB
07e5b264 29#define FIO_HAVE_RAWBIND
5e62c22a 30#define FIO_HAVE_BLKTRACE
f356d01d 31#define FIO_HAVE_PSHARED_MUTEX
eb7ccf38 32#define FIO_HAVE_CL_SIZE
a696fa2a 33#define FIO_HAVE_CGROUPS
2e3bd4c2 34#define FIO_HAVE_FS_STAT
a5f3027c 35#define FIO_HAVE_TRIM
79a43187 36#define FIO_HAVE_BINJECT
47f767c1 37#define FIO_HAVE_GETTID
93bcfd20 38#define FIO_USE_GENERIC_INIT_RANDOM_STATE
b6959b55 39
39b93568
JA
40#ifdef MAP_HUGETLB
41#define FIO_HAVE_MMAP_HUGE
42#endif
43
dc873b6f 44#define OS_MAP_ANON MAP_ANONYMOUS
ebac4655
JA
45
46typedef cpu_set_t os_cpu_mask_t;
4e78e405 47
6dfd46b9 48typedef struct drand48_data os_random_state_t;
ebac4655 49
67bf9823 50#ifdef CONFIG_3ARG_AFFINITY
e8462bd8
JA
51#define fio_setaffinity(pid, cpumask) \
52 sched_setaffinity((pid), sizeof(cpumask), &(cpumask))
ebac4655
JA
53#define fio_getaffinity(pid, ptr) \
54 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
67bf9823 55#elif defined(CONFIG_2ARG_AFFINITY)
e8462bd8
JA
56#define fio_setaffinity(pid, cpumask) \
57 sched_setaffinity((pid), &(cpumask))
c8f025f8
JA
58#define fio_getaffinity(pid, ptr) \
59 sched_getaffinity((pid), (ptr))
60#endif
be4ecfdf 61
1f4c0a4f
JA
62#define fio_cpu_clear(mask, cpu) (void) CPU_CLR((cpu), (mask))
63#define fio_cpu_set(mask, cpu) (void) CPU_SET((cpu), (mask))
d2ce18b5
JA
64
65static inline int fio_cpuset_init(os_cpu_mask_t *mask)
66{
67 CPU_ZERO(mask);
68 return 0;
69}
70
71static inline int fio_cpuset_exit(os_cpu_mask_t *mask)
72{
73 return 0;
74}
6d459ee7
JA
75
76#define FIO_MAX_CPUS CPU_SETSIZE
4d8947de 77
28727df7
JA
78enum {
79 IOPRIO_CLASS_NONE,
80 IOPRIO_CLASS_RT,
81 IOPRIO_CLASS_BE,
82 IOPRIO_CLASS_IDLE,
83};
84
85enum {
86 IOPRIO_WHO_PROCESS = 1,
87 IOPRIO_WHO_PGRP,
88 IOPRIO_WHO_USER,
89};
90
91#define IOPRIO_BITS 16
92#define IOPRIO_CLASS_SHIFT 13
93
94static inline int ioprio_set(int which, int who, int ioprio_class, int ioprio)
ebac4655 95{
28727df7
JA
96 /*
97 * If no class is set, assume BE
98 */
99 if (!ioprio_class)
100 ioprio_class = IOPRIO_CLASS_BE;
101
102 ioprio |= ioprio_class << IOPRIO_CLASS_SHIFT;
ebac4655
JA
103 return syscall(__NR_ioprio_set, which, who, ioprio);
104}
105
47f767c1
JA
106static inline int gettid(void)
107{
108 return syscall(__NR_gettid);
109}
110
3feedc60
JA
111#define SPLICE_DEF_SIZE (64*1024)
112
ebac4655
JA
113#ifndef BLKGETSIZE64
114#define BLKGETSIZE64 _IOR(0x12,114,size_t)
115#endif
116
e5b401d4
JA
117#ifndef BLKFLSBUF
118#define BLKFLSBUF _IO(0x12,97)
119#endif
120
a5f3027c
JA
121#ifndef BLKDISCARD
122#define BLKDISCARD _IO(0x12,119)
123#endif
124
e2e58886 125static inline int blockdev_invalidate_cache(struct fio_file *f)
e5b401d4 126{
ecc314ba 127 return ioctl(f->fd, BLKFLSBUF);
e5b401d4
JA
128}
129
ecc314ba 130static inline int blockdev_size(struct fio_file *f, unsigned long long *bytes)
ebac4655 131{
ecc314ba 132 if (!ioctl(f->fd, BLKGETSIZE64, bytes))
ebac4655
JA
133 return 0;
134
135 return errno;
136}
137
32cd46a0
JA
138static inline unsigned long long os_phys_mem(void)
139{
140 long pagesize, pages;
141
142 pagesize = sysconf(_SC_PAGESIZE);
143 pages = sysconf(_SC_PHYS_PAGES);
144 if (pages == -1 || pagesize == -1)
145 return 0;
146
147 return (unsigned long long) pages * (unsigned long long) pagesize;
148}
149
6dfd46b9
JA
150static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
151{
152 srand48_r(seed, rs);
153}
154
155static inline long os_random_long(os_random_state_t *rs)
156{
157 long val;
158
159 lrand48_r(rs, &val);
160 return val;
161}
162
8cc7afa9 163static inline int fio_lookup_raw(dev_t dev, int *majdev, int *mindev)
07e5b264
JA
164{
165 struct raw_config_request rq;
166 int fd;
167
168 if (major(dev) != RAW_MAJOR)
8cc7afa9 169 return 1;
07e5b264
JA
170
171 /*
172 * we should be able to find /dev/rawctl or /dev/raw/rawctl
173 */
174 fd = open("/dev/rawctl", O_RDONLY);
175 if (fd < 0) {
176 fd = open("/dev/raw/rawctl", O_RDONLY);
177 if (fd < 0)
8cc7afa9 178 return 1;
07e5b264
JA
179 }
180
181 rq.raw_minor = minor(dev);
182 if (ioctl(fd, RAW_GETBIND, &rq) < 0) {
183 close(fd);
8cc7afa9 184 return 1;
07e5b264
JA
185 }
186
187 close(fd);
188 *majdev = rq.block_major;
189 *mindev = rq.block_minor;
8cc7afa9 190 return 0;
07e5b264
JA
191}
192
5b6f5c66 193#ifdef O_NOATIME
5921e80c 194#define FIO_O_NOATIME O_NOATIME
5b6f5c66
JA
195#else
196#define FIO_O_NOATIME 0
197#endif
5921e80c 198
a1c58075
JA
199#ifdef MADV_REMOVE
200#define FIO_MADV_FREE MADV_REMOVE
201#endif
202
ff245192
JA
203#define fio_swap16(x) __bswap_16(x)
204#define fio_swap32(x) __bswap_32(x)
205#define fio_swap64(x) __bswap_64(x)
206
eb7ccf38
JA
207#define CACHE_LINE_FILE \
208 "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"
209
210static inline int arch_cache_line_size(void)
211{
212 char size[32];
213 int fd, ret;
214
215 fd = open(CACHE_LINE_FILE, O_RDONLY);
216 if (fd < 0)
217 return -1;
218
219 ret = read(fd, size, sizeof(size));
ec76f15a
JA
220
221 close(fd);
222
eb7ccf38
JA
223 if (ret <= 0)
224 return -1;
ec76f15a
JA
225 else
226 return atoi(size);
eb7ccf38
JA
227}
228
2e3bd4c2
JA
229static inline unsigned long long get_fs_size(const char *path)
230{
231 unsigned long long ret;
232 struct statfs s;
233
234 if (statfs(path, &s) < 0)
235 return -1ULL;
236
237 ret = s.f_bsize;
238 ret *= (unsigned long long) s.f_bfree;
239 return ret;
240}
241
a5f3027c
JA
242static inline int os_trim(int fd, unsigned long long start,
243 unsigned long long len)
244{
245 uint64_t range[2];
246
247 range[0] = start;
248 range[1] = len;
249
250 if (!ioctl(fd, BLKDISCARD, range))
251 return 0;
252
253 return errno;
254}
255
7e09a9f1 256#ifdef CONFIG_SCHED_IDLE
f2a2ce0e
HL
257static inline int fio_set_sched_idle(void)
258{
259 struct sched_param p = { .sched_priority = 0, };
260 return sched_setscheduler(gettid(), SCHED_IDLE, &p);
261}
7e09a9f1 262#endif
f2a2ce0e 263
ebac4655 264#endif