4 * IO engine that transfers data by doing splices to/from pipes and
18 #ifdef FIO_HAVE_SPLICE
20 struct spliceio_data {
26 * vmsplice didn't use to support splicing to user space, this is the old
27 * variant of getting that job done. Doesn't make a lot of sense, but it
28 * uses splices to move data from the source into a pipe.
30 static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
32 struct spliceio_data *sd = td->io_ops->data;
33 struct fio_file *f = io_u->file;
34 int ret, ret2, buflen;
38 offset = io_u->offset;
39 buflen = io_u->xfer_buflen;
42 int this_len = buflen;
44 if (this_len > SPLICE_DEF_SIZE)
45 this_len = SPLICE_DEF_SIZE;
47 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
49 if (errno == ENODATA || errno == EAGAIN)
58 ret2 = read(sd->pipe[0], p, ret);
67 return io_u->xfer_buflen;
71 * We can now vmsplice into userspace, so do the transfer by splicing into
72 * a pipe and vmsplicing that into userspace.
74 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
76 struct spliceio_data *sd = td->io_ops->data;
77 struct fio_file *f = io_u->file;
79 int ret = 0 , buflen, mmap_len;
83 offset = io_u->offset;
84 mmap_len = buflen = io_u->xfer_buflen;
86 map = mmap(io_u->xfer_buf, buflen, PROT_READ, MAP_PRIVATE|OS_MAP_ANON, 0, 0);
87 if (map == MAP_FAILED) {
88 td_verror(td, errno, "mmap io_u");
94 int this_len = buflen;
97 if (this_len > SPLICE_DEF_SIZE) {
98 this_len = SPLICE_DEF_SIZE;
99 flags = SPLICE_F_MORE;
102 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len,flags);
104 if (errno == ENODATA || errno == EAGAIN)
107 td_verror(td, errno, "splice-from-fd");
116 while (iov.iov_len) {
117 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
119 td_verror(td, errno, "vmsplice");
122 td_verror(td, ENODATA, "vmsplice");
134 if (munmap(map, mmap_len) < 0) {
135 td_verror(td, errno, "munnap io_u");
141 return io_u->xfer_buflen;
145 * For splice writing, we can vmsplice our data buffer directly into a
146 * pipe and then splice that to a file.
148 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
150 struct spliceio_data *sd = td->io_ops->data;
152 .iov_base = io_u->xfer_buf,
153 .iov_len = io_u->xfer_buflen,
155 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
156 struct fio_file *f = io_u->file;
157 off_t off = io_u->offset;
160 while (iov.iov_len) {
161 if (poll(&pfd, 1, -1) < 0)
164 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
172 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
180 return io_u->xfer_buflen;
183 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
185 struct spliceio_data *sd = td->io_ops->data;
188 if (io_u->ddir == DDIR_READ) {
189 if (sd->vmsplice_to_user) {
190 ret = fio_splice_read(td, io_u);
192 * This kernel doesn't support vmsplice to user
193 * space. Reset the vmsplice_to_user flag, so that
194 * we retry below and don't hit this path again.
197 sd->vmsplice_to_user = 0;
199 if (!sd->vmsplice_to_user)
200 ret = fio_splice_read_old(td, io_u);
201 } else if (io_u->ddir == DDIR_WRITE)
202 ret = fio_splice_write(td, io_u);
204 ret = fsync(io_u->file->fd);
206 if (ret != (int) io_u->xfer_buflen) {
208 io_u->resid = io_u->xfer_buflen - ret;
210 return FIO_Q_COMPLETED;
216 td_verror(td, io_u->error, "xfer");
218 return FIO_Q_COMPLETED;
221 static void fio_spliceio_cleanup(struct thread_data *td)
223 struct spliceio_data *sd = td->io_ops->data;
229 td->io_ops->data = NULL;
233 static int fio_spliceio_init(struct thread_data *td)
235 struct spliceio_data *sd = malloc(sizeof(*sd));
237 if (pipe(sd->pipe) < 0) {
238 td_verror(td, errno, "pipe");
244 * Assume this work, we'll reset this if it doesn't
246 sd->vmsplice_to_user = 1;
249 * And if vmsplice_to_user works, we definitely need aligned
250 * buffers. Just set ->odirect to force that.
255 td->io_ops->data = sd;
259 static struct ioengine_ops ioengine = {
261 .version = FIO_IOOPS_VERSION,
262 .init = fio_spliceio_init,
263 .queue = fio_spliceio_queue,
264 .cleanup = fio_spliceio_cleanup,
265 .open_file = generic_open_file,
266 .close_file = generic_close_file,
270 #else /* FIO_HAVE_SPLICE */
273 * When we have a proper configure system in place, we simply wont build
274 * and install this io engine. For now install a crippled version that
275 * just complains and fails to load.
277 static int fio_spliceio_init(struct thread_data fio_unused *td)
279 fprintf(stderr, "fio: splice not available\n");
283 static struct ioengine_ops ioengine = {
285 .version = FIO_IOOPS_VERSION,
286 .init = fio_spliceio_init,
291 static void fio_init fio_spliceio_register(void)
293 register_ioengine(&ioengine);
296 static void fio_exit fio_spliceio_unregister(void)
298 unregister_ioengine(&ioengine);