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;
70 * We can now vmsplice into userspace, so do the transfer by splicing into
71 * a pipe and vmsplicing that into userspace.
73 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
75 struct spliceio_data *sd = td->io_ops->data;
76 struct fio_file *f = io_u->file;
82 offset = io_u->offset;
83 buflen = io_u->xfer_buflen;
86 int this_len = buflen;
88 if (this_len > SPLICE_DEF_SIZE)
89 this_len = SPLICE_DEF_SIZE;
91 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
93 if (errno == ENODATA || errno == EAGAIN)
104 while (iov.iov_len) {
105 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
116 return io_u->xfer_buflen;
120 * For splice writing, we can vmsplice our data buffer directly into a
121 * pipe and then splice that to a file.
123 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
125 struct spliceio_data *sd = td->io_ops->data;
127 .iov_base = io_u->xfer_buf,
128 .iov_len = io_u->xfer_buflen,
130 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
131 struct fio_file *f = io_u->file;
132 off_t off = io_u->offset;
135 while (iov.iov_len) {
136 if (poll(&pfd, 1, -1) < 0)
139 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
147 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
155 return io_u->xfer_buflen;
158 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
160 struct spliceio_data *sd = td->io_ops->data;
163 if (io_u->ddir == DDIR_READ) {
164 if (sd->vmsplice_to_user) {
165 ret = fio_splice_read(td, io_u);
167 * This kernel doesn't support vmsplice to user
168 * space. Reset the vmsplice_to_user flag, so that
169 * we retry below and don't hit this path again.
172 sd->vmsplice_to_user = 0;
174 if (!sd->vmsplice_to_user)
175 ret = fio_splice_read_old(td, io_u);
176 } else if (io_u->ddir == DDIR_WRITE)
177 ret = fio_splice_write(td, io_u);
179 ret = fsync(io_u->file->fd);
181 if (ret != (int) io_u->xfer_buflen) {
183 io_u->resid = io_u->xfer_buflen - ret;
185 return FIO_Q_COMPLETED;
191 td_verror(td, io_u->error, "xfer");
193 return FIO_Q_COMPLETED;
196 static void fio_spliceio_cleanup(struct thread_data *td)
198 struct spliceio_data *sd = td->io_ops->data;
204 td->io_ops->data = NULL;
208 static int fio_spliceio_init(struct thread_data *td)
210 struct spliceio_data *sd = malloc(sizeof(*sd));
212 if (pipe(sd->pipe) < 0) {
213 td_verror(td, errno, "pipe");
219 * Assume this work, we'll reset this if it doesn't
221 sd->vmsplice_to_user = 1;
223 td->io_ops->data = sd;
227 static struct ioengine_ops ioengine = {
229 .version = FIO_IOOPS_VERSION,
230 .init = fio_spliceio_init,
231 .queue = fio_spliceio_queue,
232 .cleanup = fio_spliceio_cleanup,
233 .open_file = generic_open_file,
234 .close_file = generic_close_file,
238 #else /* FIO_HAVE_SPLICE */
241 * When we have a proper configure system in place, we simply wont build
242 * and install this io engine. For now install a crippled version that
243 * just complains and fails to load.
245 static int fio_spliceio_init(struct thread_data fio_unused *td)
247 fprintf(stderr, "fio: splice not available\n");
251 static struct ioengine_ops ioengine = {
253 .version = FIO_IOOPS_VERSION,
254 .init = fio_spliceio_init,
259 static void fio_init fio_spliceio_register(void)
261 register_ioengine(&ioengine);
264 static void fio_exit fio_spliceio_unregister(void)
266 unregister_ioengine(&ioengine);