4 * IO engine that transfers data by doing splices to/from pipes and
17 #ifdef FIO_HAVE_SPLICE
19 struct spliceio_data {
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.
29 static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
31 struct spliceio_data *sd = td->io_ops->data;
32 struct fio_file *f = io_u->file;
33 int ret, ret2, buflen;
37 offset = io_u->offset;
38 buflen = io_u->xfer_buflen;
41 int this_len = buflen;
43 if (this_len > SPLICE_DEF_SIZE)
44 this_len = SPLICE_DEF_SIZE;
46 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
48 if (errno == ENODATA || errno == EAGAIN)
57 ret2 = read(sd->pipe[0], p, ret);
66 return io_u->xfer_buflen;
69 static void splice_unmap_io_u(struct thread_data *td, struct io_u *io_u)
71 struct spliceio_data *sd = td->io_ops->data;
73 .iov_base = io_u->xfer_buf,
74 .iov_len = io_u->xfer_buflen,
77 vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_UNMAP);
81 * We can now vmsplice into userspace, so do the transfer by splicing into
82 * a pipe and vmsplicing that into userspace.
84 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
86 struct spliceio_data *sd = td->io_ops->data;
87 struct fio_file *f = io_u->file;
93 offset = io_u->offset;
94 buflen = io_u->xfer_buflen;
96 io_u->xfer_buf = NULL;
98 int this_len = buflen;
100 if (this_len > SPLICE_DEF_SIZE)
101 this_len = SPLICE_DEF_SIZE;
103 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
105 if (errno == ENODATA || errno == EAGAIN)
116 while (iov.iov_len) {
117 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
124 io_u->xfer_buf = iov.iov_base;
130 io_u->unmap = splice_unmap_io_u;
131 return io_u->xfer_buflen;
135 * For splice writing, we can vmsplice our data buffer directly into a
136 * pipe and then splice that to a file.
138 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
140 struct spliceio_data *sd = td->io_ops->data;
142 .iov_base = io_u->xfer_buf,
143 .iov_len = io_u->xfer_buflen,
145 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
146 struct fio_file *f = io_u->file;
147 off_t off = io_u->offset;
150 while (iov.iov_len) {
151 if (poll(&pfd, 1, -1) < 0)
154 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
162 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
170 return io_u->xfer_buflen;
173 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
175 struct spliceio_data *sd = td->io_ops->data;
178 if (io_u->ddir == DDIR_READ) {
179 if (sd->vmsplice_to_user) {
180 ret = fio_splice_read(td, io_u);
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.
187 sd->vmsplice_to_user = 0;
189 if (!sd->vmsplice_to_user)
190 ret = fio_splice_read_old(td, io_u);
191 } else if (io_u->ddir == DDIR_WRITE)
192 ret = fio_splice_write(td, io_u);
194 ret = fsync(io_u->file->fd);
196 if (ret != (int) io_u->xfer_buflen) {
198 io_u->resid = io_u->xfer_buflen - ret;
200 return FIO_Q_COMPLETED;
206 td_verror(td, io_u->error, "xfer");
208 return FIO_Q_COMPLETED;
211 static void fio_spliceio_cleanup(struct thread_data *td)
213 struct spliceio_data *sd = td->io_ops->data;
219 td->io_ops->data = NULL;
223 static int fio_spliceio_init(struct thread_data *td)
225 struct spliceio_data *sd = malloc(sizeof(*sd));
227 if (pipe(sd->pipe) < 0) {
228 td_verror(td, errno, "pipe");
234 * Assume this work, we'll reset this if it doesn't
236 sd->vmsplice_to_user = 1;
238 td->io_ops->data = sd;
242 static struct ioengine_ops ioengine = {
244 .version = FIO_IOOPS_VERSION,
245 .init = fio_spliceio_init,
246 .queue = fio_spliceio_queue,
247 .cleanup = fio_spliceio_cleanup,
248 .open_file = generic_open_file,
249 .close_file = generic_close_file,
253 #else /* FIO_HAVE_SPLICE */
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.
260 static int fio_spliceio_init(struct thread_data fio_unused *td)
262 fprintf(stderr, "fio: splice not available\n");
266 static struct ioengine_ops ioengine = {
268 .version = FIO_IOOPS_VERSION,
269 .init = fio_spliceio_init,
274 static void fio_init fio_spliceio_register(void)
276 register_ioengine(&ioengine);
279 static void fio_exit fio_spliceio_unregister(void)
281 unregister_ioengine(&ioengine);