Thread waitpid() error check
[fio.git] / os-linux.h
CommitLineData
ebac4655
JA
1#ifndef FIO_OS_LINUX_H
2#define FIO_OS_LINUX_H
3
4#include <sys/ioctl.h>
8756e4d4 5#include <sys/uio.h>
3c39a379
JA
6#include <sys/syscall.h>
7#include <unistd.h>
97fbdfa1 8#include <fcntl.h>
ea421790 9#include <linux/unistd.h>
ebac4655
JA
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
ba4f8923 17#define FIO_HAVE_IOPRIO
8756e4d4 18#define FIO_HAVE_SPLICE
22f78b32 19#define FIO_HAVE_IOSCHED_SWITCH
2c0ecd28 20#define FIO_HAVE_ODIRECT
74b025b0 21#define FIO_HAVE_HUGETLB
b6959b55
JA
22
23/*
24 * Only for x86 currently
25 */
26#if defined(__i386__)
a4f4fdd7 27#define FIO_HAVE_SYSLET
b6959b55 28#endif
ebac4655
JA
29
30#define OS_MAP_ANON (MAP_ANONYMOUS)
31
32typedef cpu_set_t os_cpu_mask_t;
6dfd46b9 33typedef struct drand48_data os_random_state_t;
ebac4655
JA
34
35/*
36 * we want fadvise64 really, but it's so tangled... later
37 */
38#define fadvise(fd, off, len, advice) \
39 posix_fadvise((fd), (off_t)(off), (len), (advice))
40
41#define fio_setaffinity(td) \
42 sched_setaffinity((td)->pid, sizeof((td)->cpumask), &(td)->cpumask)
43#define fio_getaffinity(pid, ptr) \
44 sched_getaffinity((pid), sizeof(cpu_set_t), (ptr))
45
46static inline int ioprio_set(int which, int who, int ioprio)
47{
48 return syscall(__NR_ioprio_set, which, who, ioprio);
49}
50
97fbdfa1
JA
51/*
52 * Just check for SPLICE_F_MOVE, if that isn't there, assume the others
53 * aren't either.
54 */
55#ifndef SPLICE_F_MOVE
56#define SPLICE_F_MOVE (0x01) /* move pages instead of copying */
57#define SPLICE_F_NONBLOCK (0x02) /* don't block on the pipe splicing (but */
58 /* we may still block on the fd we splice */
59 /* from/to, of course */
60#define SPLICE_F_MORE (0x04) /* expect more data */
61#define SPLICE_F_GIFT (0x08) /* pages passed in are a gift */
8756e4d4
JA
62
63static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
64 size_t len, unsigned long flags)
65{
97fbdfa1 66 return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
8756e4d4
JA
67}
68
69static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
70{
97fbdfa1 71 return syscall(__NR_sys_tee, fdin, fdout, len, flags);
8756e4d4
JA
72}
73
74static inline int vmsplice(int fd, const struct iovec *iov,
75 unsigned long nr_segs, unsigned int flags)
76{
97fbdfa1 77 return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
8756e4d4 78}
97fbdfa1 79#endif
8756e4d4 80
3feedc60
JA
81#define SPLICE_DEF_SIZE (64*1024)
82
b6959b55 83#ifdef FIO_HAVE_SYSLET
bf0dc8fa
IM
84
85struct syslet_uatom;
86struct async_head_user;
87
a4f4fdd7
JA
88/*
89 * syslet stuff
90 */
bf0dc8fa
IM
91static inline struct syslet_uatom *
92async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
a4f4fdd7 93{
145dad6d 94 return syscall(__NR_async_exec, atom, ahu);
a4f4fdd7
JA
95}
96
bf0dc8fa
IM
97static inline long
98async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
99 struct async_head_user *ahu)
a4f4fdd7 100{
bf0dc8fa
IM
101 return syscall(__NR_async_wait, min_wait_events,
102 user_ring_idx, ahu);
a4f4fdd7
JA
103}
104
bf0dc8fa 105static inline long async_thread(void)
a4f4fdd7 106{
bf0dc8fa 107 return syscall(__NR_async_thread);
a4f4fdd7
JA
108}
109
110static inline long umem_add(unsigned long *uptr, unsigned long inc)
111{
112 return syscall(__NR_umem_add, uptr, inc);
113}
b6959b55 114#endif /* FIO_HAVE_SYSLET */
a4f4fdd7 115
ebac4655
JA
116enum {
117 IOPRIO_WHO_PROCESS = 1,
118 IOPRIO_WHO_PGRP,
119 IOPRIO_WHO_USER,
120};
121
122#define IOPRIO_CLASS_SHIFT 13
123
124#ifndef BLKGETSIZE64
125#define BLKGETSIZE64 _IOR(0x12,114,size_t)
126#endif
127
e5b401d4
JA
128#ifndef BLKFLSBUF
129#define BLKFLSBUF _IO(0x12,97)
130#endif
131
132static inline int blockdev_invalidate_cache(int fd)
133{
134 if (!ioctl(fd, BLKFLSBUF))
135 return 0;
136
137 return errno;
138}
139
9104f874 140static inline int blockdev_size(int fd, unsigned long long *bytes)
ebac4655
JA
141{
142 if (!ioctl(fd, BLKGETSIZE64, bytes))
143 return 0;
144
145 return errno;
146}
147
32cd46a0
JA
148static inline unsigned long long os_phys_mem(void)
149{
150 long pagesize, pages;
151
152 pagesize = sysconf(_SC_PAGESIZE);
153 pages = sysconf(_SC_PHYS_PAGES);
154 if (pages == -1 || pagesize == -1)
155 return 0;
156
157 return (unsigned long long) pages * (unsigned long long) pagesize;
158}
159
6dfd46b9
JA
160static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
161{
162 srand48_r(seed, rs);
163}
164
165static inline long os_random_long(os_random_state_t *rs)
166{
167 long val;
168
169 lrand48_r(rs, &val);
170 return val;
171}
172
173static inline double os_random_double(os_random_state_t *rs)
174{
175 double val;
176
177 drand48_r(rs, &val);
178 return val;
179}
180
ebac4655 181#endif