Dump io_u on timeout
[fio.git] / 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 <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 #define FIO_HAVE_HUGETLB
22
23 /*
24  * Only for x86 currently
25  */
26 #if defined(__i386__)
27 #define FIO_HAVE_SYSLET
28 #endif
29
30 #define OS_MAP_ANON             (MAP_ANONYMOUS)
31
32 typedef cpu_set_t os_cpu_mask_t;
33 typedef struct drand48_data os_random_state_t;
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
46 static inline int ioprio_set(int which, int who, int ioprio)
47 {
48         return syscall(__NR_ioprio_set, which, who, ioprio);
49 }
50
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 */
62
63 static inline int splice(int fdin, loff_t *off_in, int fdout, loff_t *off_out,
64                          size_t len, unsigned long flags)
65 {
66         return syscall(__NR_sys_splice, fdin, off_in, fdout, off_out, len, flags);
67 }
68
69 static inline int tee(int fdin, int fdout, size_t len, unsigned int flags)
70 {
71         return syscall(__NR_sys_tee, fdin, fdout, len, flags);
72 }
73
74 static inline int vmsplice(int fd, const struct iovec *iov,
75                            unsigned long nr_segs, unsigned int flags)
76 {
77         return syscall(__NR_sys_vmsplice, fd, iov, nr_segs, flags);
78 }
79 #endif
80
81 #define SPLICE_DEF_SIZE (64*1024)
82
83 #ifdef FIO_HAVE_SYSLET
84
85 struct syslet_uatom;
86 struct async_head_user;
87
88 /*
89  * syslet stuff
90  */
91 static inline struct syslet_uatom *
92 async_exec(struct syslet_uatom *atom, struct async_head_user *ahu)
93 {
94         return (void *) syscall(__NR_async_exec, atom, ahu);
95 }
96
97 static inline long
98 async_wait(unsigned long min_wait_events, unsigned long user_ring_idx,
99            struct async_head_user *ahu)
100 {
101         return syscall(__NR_async_wait, min_wait_events,
102                         user_ring_idx, ahu);
103 }
104
105 static inline long async_thread(void)
106 {
107         return syscall(__NR_async_thread);
108 }
109
110 static inline long umem_add(unsigned long *uptr, unsigned long inc)
111 {
112         return syscall(__NR_umem_add, uptr, inc);
113 }
114 #endif /* FIO_HAVE_SYSLET */
115
116 enum {
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
128 #ifndef BLKFLSBUF
129 #define BLKFLSBUF       _IO(0x12,97)
130 #endif
131
132 static inline int blockdev_invalidate_cache(int fd)
133 {
134         if (!ioctl(fd, BLKFLSBUF))
135                 return 0;
136
137         return errno;
138 }
139
140 static inline int blockdev_size(int fd, unsigned long long *bytes)
141 {
142         if (!ioctl(fd, BLKGETSIZE64, bytes))
143                 return 0;
144
145         return errno;
146 }
147
148 static 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
160 static inline void os_random_seed(unsigned long seed, os_random_state_t *rs)
161 {
162         srand48_r(seed, rs);
163 }
164
165 static 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
173 static 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
181 #endif