Add display of major and minor faults
[fio.git] / engines / splice.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * splice engine
3 *
4 * IO engine that transfers data by doing splices to/from pipes and
5 * the files.
2866c82d
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
13#include <sys/poll.h>
5f350952
JA
14
15#include "../fio.h"
2866c82d 16
34cfcdaf
JA
17#ifdef FIO_HAVE_SPLICE
18
2866c82d 19struct spliceio_data {
2866c82d 20 int pipe[2];
f24254e1 21 int vmsplice_to_user;
2866c82d
JA
22};
23
2866c82d 24/*
f24254e1
JA
25 * vmsplice didn't use to support splicing to user space, this is the old
26 * variant of getting that job done. Doesn't make a lot of sense, but it
27 * uses splices to move data from the source into a pipe.
2866c82d 28 */
f24254e1 29static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
2866c82d
JA
30{
31 struct spliceio_data *sd = td->io_ops->data;
53cdc686 32 struct fio_file *f = io_u->file;
2866c82d
JA
33 int ret, ret2, buflen;
34 off_t offset;
35 void *p;
36
37 offset = io_u->offset;
cec6b55d
JA
38 buflen = io_u->xfer_buflen;
39 p = io_u->xfer_buf;
2866c82d
JA
40 while (buflen) {
41 int this_len = buflen;
42
43 if (this_len > SPLICE_DEF_SIZE)
44 this_len = SPLICE_DEF_SIZE;
45
53cdc686 46 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
2866c82d
JA
47 if (ret < 0) {
48 if (errno == ENODATA || errno == EAGAIN)
49 continue;
50
b4ba5f30 51 return -errno;
2866c82d
JA
52 }
53
54 buflen -= ret;
55
56 while (ret) {
57 ret2 = read(sd->pipe[0], p, ret);
58 if (ret2 < 0)
b4ba5f30 59 return -errno;
2866c82d
JA
60
61 ret -= ret2;
62 p += ret2;
63 }
64 }
65
cec6b55d 66 return io_u->xfer_buflen;
2866c82d
JA
67}
68
a26fc7c5
JA
69static void splice_unmap_io_u(struct thread_data *td, struct io_u *io_u)
70{
71 struct spliceio_data *sd = td->io_ops->data;
72 struct iovec iov = {
73 .iov_base = io_u->xfer_buf,
74 .iov_len = io_u->xfer_buflen,
75 };
76
77 vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_UNMAP);
78}
79
f24254e1
JA
80/*
81 * We can now vmsplice into userspace, so do the transfer by splicing into
82 * a pipe and vmsplicing that into userspace.
83 */
84static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
85{
86 struct spliceio_data *sd = td->io_ops->data;
87 struct fio_file *f = io_u->file;
88 struct iovec iov;
89 int ret, buflen;
90 off_t offset;
91 void *p;
92
93 offset = io_u->offset;
94 buflen = io_u->xfer_buflen;
95 p = io_u->xfer_buf;
bb446c11 96 io_u->xfer_buf = NULL;
f24254e1
JA
97 while (buflen) {
98 int this_len = buflen;
99
100 if (this_len > SPLICE_DEF_SIZE)
101 this_len = SPLICE_DEF_SIZE;
102
103 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
104 if (ret < 0) {
105 if (errno == ENODATA || errno == EAGAIN)
106 continue;
107
108 return -errno;
109 }
110
111 buflen -= ret;
112 iov.iov_base = p;
113 iov.iov_len = ret;
114 p += ret;
115
116 while (iov.iov_len) {
117 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
118 if (ret < 0)
119 return -errno;
120 else if (!ret)
121 return -ENODATA;
122
bb446c11
JA
123 if (!io_u->xfer_buf)
124 io_u->xfer_buf = iov.iov_base;
f24254e1
JA
125 iov.iov_len -= ret;
126 iov.iov_base += ret;
127 }
128 }
129
bb446c11 130 io_u->unmap = splice_unmap_io_u;
f24254e1
JA
131 return io_u->xfer_buflen;
132}
133
2866c82d
JA
134/*
135 * For splice writing, we can vmsplice our data buffer directly into a
136 * pipe and then splice that to a file.
137 */
138static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
139{
140 struct spliceio_data *sd = td->io_ops->data;
f24254e1
JA
141 struct iovec iov = {
142 .iov_base = io_u->xfer_buf,
143 .iov_len = io_u->xfer_buflen,
2866c82d
JA
144 };
145 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
53cdc686 146 struct fio_file *f = io_u->file;
2866c82d
JA
147 off_t off = io_u->offset;
148 int ret, ret2;
149
f24254e1 150 while (iov.iov_len) {
2866c82d
JA
151 if (poll(&pfd, 1, -1) < 0)
152 return errno;
153
f24254e1 154 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
2866c82d 155 if (ret < 0)
b4ba5f30 156 return -errno;
2866c82d 157
f24254e1
JA
158 iov.iov_len -= ret;
159 iov.iov_base += ret;
2866c82d
JA
160
161 while (ret) {
53cdc686 162 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
2866c82d 163 if (ret2 < 0)
b4ba5f30 164 return -errno;
2866c82d
JA
165
166 ret -= ret2;
167 }
168 }
169
cec6b55d 170 return io_u->xfer_buflen;
2866c82d
JA
171}
172
173static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
174{
f24254e1 175 struct spliceio_data *sd = td->io_ops->data;
cec6b55d 176 int ret;
2866c82d 177
f24254e1 178 if (io_u->ddir == DDIR_READ) {
43688575 179 if (sd->vmsplice_to_user) {
f24254e1 180 ret = fio_splice_read(td, io_u);
43688575
JA
181 /*
182 * This kernel doesn't support vmsplice to user
183 * space. Reset the vmsplice_to_user flag, so that
184 * we retry below and don't hit this path again.
185 */
186 if (ret == -EBADF)
187 sd->vmsplice_to_user = 0;
188 }
189 if (!sd->vmsplice_to_user)
f24254e1
JA
190 ret = fio_splice_read_old(td, io_u);
191 } else if (io_u->ddir == DDIR_WRITE)
2866c82d 192 ret = fio_splice_write(td, io_u);
87dc1ab1
JA
193 else
194 ret = fsync(io_u->file->fd);
2866c82d 195
cec6b55d 196 if (ret != (int) io_u->xfer_buflen) {
22819ec2 197 if (ret >= 0) {
cec6b55d
JA
198 io_u->resid = io_u->xfer_buflen - ret;
199 io_u->error = 0;
36167d82 200 return FIO_Q_COMPLETED;
2866c82d
JA
201 } else
202 io_u->error = errno;
203 }
204
36167d82 205 if (io_u->error)
e1161c32 206 td_verror(td, io_u->error, "xfer");
2866c82d 207
36167d82 208 return FIO_Q_COMPLETED;
2866c82d
JA
209}
210
211static void fio_spliceio_cleanup(struct thread_data *td)
212{
213 struct spliceio_data *sd = td->io_ops->data;
214
215 if (sd) {
216 close(sd->pipe[0]);
217 close(sd->pipe[1]);
218 free(sd);
219 td->io_ops->data = NULL;
220 }
221}
222
223static int fio_spliceio_init(struct thread_data *td)
224{
225 struct spliceio_data *sd = malloc(sizeof(*sd));
226
2866c82d 227 if (pipe(sd->pipe) < 0) {
e1161c32 228 td_verror(td, errno, "pipe");
2866c82d
JA
229 free(sd);
230 return 1;
231 }
232
f24254e1 233 /*
43688575 234 * Assume this work, we'll reset this if it doesn't
f24254e1 235 */
43688575 236 sd->vmsplice_to_user = 1;
f24254e1 237
2866c82d
JA
238 td->io_ops->data = sd;
239 return 0;
240}
241
5f350952 242static struct ioengine_ops ioengine = {
2866c82d
JA
243 .name = "splice",
244 .version = FIO_IOOPS_VERSION,
245 .init = fio_spliceio_init,
246 .queue = fio_spliceio_queue,
2866c82d 247 .cleanup = fio_spliceio_cleanup,
b5af8293
JA
248 .open_file = generic_open_file,
249 .close_file = generic_close_file,
2866c82d
JA
250 .flags = FIO_SYNCIO,
251};
34cfcdaf
JA
252
253#else /* FIO_HAVE_SPLICE */
254
255/*
256 * When we have a proper configure system in place, we simply wont build
257 * and install this io engine. For now install a crippled version that
258 * just complains and fails to load.
259 */
260static int fio_spliceio_init(struct thread_data fio_unused *td)
261{
262 fprintf(stderr, "fio: splice not available\n");
263 return 1;
264}
265
5f350952 266static struct ioengine_ops ioengine = {
34cfcdaf
JA
267 .name = "splice",
268 .version = FIO_IOOPS_VERSION,
269 .init = fio_spliceio_init,
270};
271
272#endif
5f350952
JA
273
274static void fio_init fio_spliceio_register(void)
275{
276 register_ioengine(&ioengine);
277}
278
279static void fio_exit fio_spliceio_unregister(void)
280{
281 unregister_ioengine(&ioengine);
282}