blob: f35ae17bc574b7af5928e4c73e6e76f8a744b230 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * splice engine
3 *
4 * IO engine that transfers data by doing splices to/from pipes and
5 * the files.
Jens Axboe2866c822006-10-09 15:57:48 +02006 *
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>
Jens Axboe81887d52007-09-07 20:36:08 +020014#include <sys/mman.h>
Jens Axboe5f350952006-11-07 15:20:59 +010015
16#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020017
18struct spliceio_data {
Jens Axboe2866c822006-10-09 15:57:48 +020019 int pipe[2];
Jens Axboef24254e2007-06-13 19:04:16 +020020 int vmsplice_to_user;
Jens Axboe8b850242007-10-16 09:42:40 +020021 int vmsplice_to_user_map;
Jens Axboe2866c822006-10-09 15:57:48 +020022};
23
Jens Axboe2866c822006-10-09 15:57:48 +020024/*
Jens Axboef24254e2007-06-13 19:04:16 +020025 * 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.
Jens Axboe2866c822006-10-09 15:57:48 +020028 */
Jens Axboef24254e2007-06-13 19:04:16 +020029static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020030{
31 struct spliceio_data *sd = td->io_ops->data;
Jens Axboe53cdc682006-10-18 11:50:58 +020032 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +020033 int ret, ret2, buflen;
34 off_t offset;
35 void *p;
36
37 offset = io_u->offset;
Jens Axboecec6b552007-02-06 20:15:38 +010038 buflen = io_u->xfer_buflen;
39 p = io_u->xfer_buf;
Jens Axboe2866c822006-10-09 15:57:48 +020040 while (buflen) {
41 int this_len = buflen;
42
43 if (this_len > SPLICE_DEF_SIZE)
44 this_len = SPLICE_DEF_SIZE;
45
Jens Axboe53cdc682006-10-18 11:50:58 +020046 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
Jens Axboe2866c822006-10-09 15:57:48 +020047 if (ret < 0) {
48 if (errno == ENODATA || errno == EAGAIN)
49 continue;
50
Jens Axboeb4ba5f32007-04-02 10:36:59 +020051 return -errno;
Jens Axboe2866c822006-10-09 15:57:48 +020052 }
53
54 buflen -= ret;
55
56 while (ret) {
57 ret2 = read(sd->pipe[0], p, ret);
58 if (ret2 < 0)
Jens Axboeb4ba5f32007-04-02 10:36:59 +020059 return -errno;
Jens Axboe2866c822006-10-09 15:57:48 +020060
61 ret -= ret2;
62 p += ret2;
63 }
64 }
65
Jens Axboecec6b552007-02-06 20:15:38 +010066 return io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020067}
68
69/*
Jens Axboef24254e2007-06-13 19:04:16 +020070 * We can now vmsplice into userspace, so do the transfer by splicing into
71 * a pipe and vmsplicing that into userspace.
72 */
73static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
74{
75 struct spliceio_data *sd = td->io_ops->data;
76 struct fio_file *f = io_u->file;
77 struct iovec iov;
Jens Axboe8b850242007-10-16 09:42:40 +020078 int ret , buflen, mmap_len;
Jens Axboef24254e2007-06-13 19:04:16 +020079 off_t offset;
Jens Axboe81887d52007-09-07 20:36:08 +020080 void *p, *map;
Jens Axboef24254e2007-06-13 19:04:16 +020081
Jens Axboe8b850242007-10-16 09:42:40 +020082 ret = 0;
Jens Axboef24254e2007-06-13 19:04:16 +020083 offset = io_u->offset;
Jens Axboe81887d52007-09-07 20:36:08 +020084 mmap_len = buflen = io_u->xfer_buflen;
85
Jens Axboe8b850242007-10-16 09:42:40 +020086 if (sd->vmsplice_to_user_map) {
87 map = mmap(io_u->xfer_buf, buflen, PROT_READ, MAP_PRIVATE|OS_MAP_ANON, 0, 0);
88 if (map == MAP_FAILED) {
89 td_verror(td, errno, "mmap io_u");
90 return -1;
91 }
92
93 p = map;
94 } else {
95 map = NULL;
96 p = io_u->xfer_buf;
Jens Axboe81887d52007-09-07 20:36:08 +020097 }
98
Jens Axboef24254e2007-06-13 19:04:16 +020099 while (buflen) {
100 int this_len = buflen;
Jens Axboe81887d52007-09-07 20:36:08 +0200101 int flags = 0;
Jens Axboef24254e2007-06-13 19:04:16 +0200102
Jens Axboe81887d52007-09-07 20:36:08 +0200103 if (this_len > SPLICE_DEF_SIZE) {
Jens Axboef24254e2007-06-13 19:04:16 +0200104 this_len = SPLICE_DEF_SIZE;
Jens Axboe81887d52007-09-07 20:36:08 +0200105 flags = SPLICE_F_MORE;
106 }
Jens Axboef24254e2007-06-13 19:04:16 +0200107
Jens Axboe81887d52007-09-07 20:36:08 +0200108 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len,flags);
Jens Axboef24254e2007-06-13 19:04:16 +0200109 if (ret < 0) {
110 if (errno == ENODATA || errno == EAGAIN)
111 continue;
112
Jens Axboe81887d52007-09-07 20:36:08 +0200113 td_verror(td, errno, "splice-from-fd");
114 break;
Jens Axboef24254e2007-06-13 19:04:16 +0200115 }
116
117 buflen -= ret;
118 iov.iov_base = p;
119 iov.iov_len = ret;
Jens Axboef24254e2007-06-13 19:04:16 +0200120
121 while (iov.iov_len) {
122 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
Jens Axboe81887d52007-09-07 20:36:08 +0200123 if (ret < 0) {
Jens Axboecd988012009-04-20 12:39:17 +0200124 if (errno == EFAULT &&
125 sd->vmsplice_to_user_map) {
Jens Axboe8b850242007-10-16 09:42:40 +0200126 sd->vmsplice_to_user_map = 0;
127 munmap(map, mmap_len);
Jens Axboecd988012009-04-20 12:39:17 +0200128 map = NULL;
129 p = io_u->xfer_buf;
130 iov.iov_base = p;
131 continue;
Jens Axboe8b850242007-10-16 09:42:40 +0200132 }
Jens Axboe3a6d2672007-10-16 09:44:47 +0200133 if (errno == EBADF) {
134 ret = -EBADF;
135 break;
136 }
Jens Axboe81887d52007-09-07 20:36:08 +0200137 td_verror(td, errno, "vmsplice");
138 break;
139 } else if (!ret) {
140 td_verror(td, ENODATA, "vmsplice");
141 ret = -1;
142 break;
143 }
Jens Axboef24254e2007-06-13 19:04:16 +0200144
145 iov.iov_len -= ret;
146 iov.iov_base += ret;
Jens Axboecd988012009-04-20 12:39:17 +0200147 p += ret;
Jens Axboef24254e2007-06-13 19:04:16 +0200148 }
Jens Axboe81887d52007-09-07 20:36:08 +0200149 if (ret < 0)
150 break;
Jens Axboef24254e2007-06-13 19:04:16 +0200151 }
152
Jens Axboe8b850242007-10-16 09:42:40 +0200153 if (sd->vmsplice_to_user_map && munmap(map, mmap_len) < 0) {
Jens Axboe81887d52007-09-07 20:36:08 +0200154 td_verror(td, errno, "munnap io_u");
155 return -1;
156 }
157 if (ret < 0)
158 return ret;
159
Jens Axboef24254e2007-06-13 19:04:16 +0200160 return io_u->xfer_buflen;
161}
162
Jens Axboef24254e2007-06-13 19:04:16 +0200163/*
Jens Axboe2866c822006-10-09 15:57:48 +0200164 * For splice writing, we can vmsplice our data buffer directly into a
165 * pipe and then splice that to a file.
166 */
167static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
168{
169 struct spliceio_data *sd = td->io_ops->data;
Jens Axboef24254e2007-06-13 19:04:16 +0200170 struct iovec iov = {
171 .iov_base = io_u->xfer_buf,
172 .iov_len = io_u->xfer_buflen,
Jens Axboe2866c822006-10-09 15:57:48 +0200173 };
174 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
Jens Axboe53cdc682006-10-18 11:50:58 +0200175 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +0200176 off_t off = io_u->offset;
177 int ret, ret2;
178
Jens Axboef24254e2007-06-13 19:04:16 +0200179 while (iov.iov_len) {
Jens Axboe2866c822006-10-09 15:57:48 +0200180 if (poll(&pfd, 1, -1) < 0)
181 return errno;
182
Jens Axboef24254e2007-06-13 19:04:16 +0200183 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
Jens Axboe2866c822006-10-09 15:57:48 +0200184 if (ret < 0)
Jens Axboeb4ba5f32007-04-02 10:36:59 +0200185 return -errno;
Jens Axboe2866c822006-10-09 15:57:48 +0200186
Jens Axboef24254e2007-06-13 19:04:16 +0200187 iov.iov_len -= ret;
188 iov.iov_base += ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200189
190 while (ret) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200191 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
Jens Axboe2866c822006-10-09 15:57:48 +0200192 if (ret2 < 0)
Jens Axboeb4ba5f32007-04-02 10:36:59 +0200193 return -errno;
Jens Axboe2866c822006-10-09 15:57:48 +0200194
195 ret -= ret2;
196 }
197 }
198
Jens Axboecec6b552007-02-06 20:15:38 +0100199 return io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +0200200}
201
202static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
203{
Jens Axboef24254e2007-06-13 19:04:16 +0200204 struct spliceio_data *sd = td->io_ops->data;
Jens Axboe24d23ca2012-11-13 08:31:24 -0700205 int ret = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200206
Jens Axboe7101d9c2007-09-12 13:12:39 +0200207 fio_ro_check(td, io_u);
208
Jens Axboef24254e2007-06-13 19:04:16 +0200209 if (io_u->ddir == DDIR_READ) {
Jens Axboe43688572007-08-14 13:43:05 +0200210 if (sd->vmsplice_to_user) {
Jens Axboef24254e2007-06-13 19:04:16 +0200211 ret = fio_splice_read(td, io_u);
Jens Axboe43688572007-08-14 13:43:05 +0200212 /*
213 * This kernel doesn't support vmsplice to user
214 * space. Reset the vmsplice_to_user flag, so that
215 * we retry below and don't hit this path again.
216 */
217 if (ret == -EBADF)
218 sd->vmsplice_to_user = 0;
219 }
220 if (!sd->vmsplice_to_user)
Jens Axboef24254e2007-06-13 19:04:16 +0200221 ret = fio_splice_read_old(td, io_u);
222 } else if (io_u->ddir == DDIR_WRITE)
Jens Axboe2866c822006-10-09 15:57:48 +0200223 ret = fio_splice_write(td, io_u);
Jens Axboea5f30272010-07-19 16:19:55 -0600224 else if (io_u->ddir == DDIR_TRIM)
225 ret = do_io_u_trim(td, io_u);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200226 else
Jens Axboef0115312010-03-09 21:47:15 +0100227 ret = do_io_u_sync(td, io_u);
Jens Axboe2866c822006-10-09 15:57:48 +0200228
Jens Axboecec6b552007-02-06 20:15:38 +0100229 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe22819ec2007-02-18 07:47:14 +0100230 if (ret >= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100231 io_u->resid = io_u->xfer_buflen - ret;
232 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100233 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200234 } else
235 io_u->error = errno;
236 }
237
Jens Axboe0aa417a2008-06-09 19:25:49 +0200238 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100239 td_verror(td, io_u->error, "xfer");
Jens Axboe0aa417a2008-06-09 19:25:49 +0200240 if (io_u->error == EINVAL)
241 log_err("fio: looks like splice doesn't work on this"
242 " file system\n");
243 }
Jens Axboe2866c822006-10-09 15:57:48 +0200244
Jens Axboe36167d82007-02-18 05:41:31 +0100245 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200246}
247
248static void fio_spliceio_cleanup(struct thread_data *td)
249{
250 struct spliceio_data *sd = td->io_ops->data;
251
252 if (sd) {
253 close(sd->pipe[0]);
254 close(sd->pipe[1]);
255 free(sd);
Jens Axboe2866c822006-10-09 15:57:48 +0200256 }
257}
258
259static int fio_spliceio_init(struct thread_data *td)
260{
261 struct spliceio_data *sd = malloc(sizeof(*sd));
262
Jens Axboe2866c822006-10-09 15:57:48 +0200263 if (pipe(sd->pipe) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100264 td_verror(td, errno, "pipe");
Jens Axboe2866c822006-10-09 15:57:48 +0200265 free(sd);
266 return 1;
267 }
268
Jens Axboef24254e2007-06-13 19:04:16 +0200269 /*
Jens Axboe43688572007-08-14 13:43:05 +0200270 * Assume this work, we'll reset this if it doesn't
Jens Axboef24254e2007-06-13 19:04:16 +0200271 */
Jens Axboe43688572007-08-14 13:43:05 +0200272 sd->vmsplice_to_user = 1;
Jens Axboef24254e2007-06-13 19:04:16 +0200273
Jens Axboe81887d52007-09-07 20:36:08 +0200274 /*
Jens Axboe8b850242007-10-16 09:42:40 +0200275 * Works with "real" vmsplice to user, eg mapping pages directly.
276 * Reset if we fail.
277 */
278 sd->vmsplice_to_user_map = 1;
279
280 /*
Jens Axboe81887d52007-09-07 20:36:08 +0200281 * And if vmsplice_to_user works, we definitely need aligned
282 * buffers. Just set ->odirect to force that.
283 */
284 if (td_read(td))
Jens Axboe76a31792011-05-13 21:19:25 +0200285 td->o.mem_align = 1;
Jens Axboe81887d52007-09-07 20:36:08 +0200286
Jens Axboe2866c822006-10-09 15:57:48 +0200287 td->io_ops->data = sd;
288 return 0;
289}
290
Jens Axboe5f350952006-11-07 15:20:59 +0100291static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200292 .name = "splice",
293 .version = FIO_IOOPS_VERSION,
294 .init = fio_spliceio_init,
295 .queue = fio_spliceio_queue,
Jens Axboe2866c822006-10-09 15:57:48 +0200296 .cleanup = fio_spliceio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100297 .open_file = generic_open_file,
298 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100299 .get_file_size = generic_get_file_size,
Jens Axboe9c0d2242009-07-01 12:26:28 +0200300 .flags = FIO_SYNCIO | FIO_PIPEIO,
Jens Axboe2866c822006-10-09 15:57:48 +0200301};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100302
Jens Axboe5f350952006-11-07 15:20:59 +0100303static void fio_init fio_spliceio_register(void)
304{
305 register_ioengine(&ioengine);
306}
307
308static void fio_exit fio_spliceio_unregister(void)
309{
310 unregister_ioengine(&ioengine);
311}