4 * IO engine that transfers data by doing splices to/from pipes and
18 #ifdef FIO_HAVE_SPLICE
20 struct spliceio_data {
25 * For splice reading, we unfortunately cannot (yet) vmsplice the other way.
26 * So just splice the data from the file into the pipe, and use regular
27 * read to fill the buffer. Doesn't make a lot of sense, but...
29 static int fio_splice_read(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 * For splice writing, we can vmsplice our data buffer directly into a
71 * pipe and then splice that to a file.
73 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
75 struct spliceio_data *sd = td->io_ops->data;
76 struct iovec iov[1] = {
78 .iov_base = io_u->xfer_buf,
79 .iov_len = io_u->xfer_buflen,
82 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
83 struct fio_file *f = io_u->file;
84 off_t off = io_u->offset;
87 while (iov[0].iov_len) {
88 if (poll(&pfd, 1, -1) < 0)
91 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
95 iov[0].iov_len -= ret;
96 iov[0].iov_base += ret;
99 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
107 return io_u->xfer_buflen;
110 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
114 if (io_u->ddir == DDIR_READ)
115 ret = fio_splice_read(td, io_u);
116 else if (io_u->ddir == DDIR_WRITE)
117 ret = fio_splice_write(td, io_u);
119 ret = fsync(io_u->file->fd);
121 if (ret != (int) io_u->xfer_buflen) {
123 io_u->resid = io_u->xfer_buflen - ret;
125 return FIO_Q_COMPLETED;
131 td_verror(td, io_u->error, "xfer");
133 return FIO_Q_COMPLETED;
136 static void fio_spliceio_cleanup(struct thread_data *td)
138 struct spliceio_data *sd = td->io_ops->data;
144 td->io_ops->data = NULL;
148 static int fio_spliceio_init(struct thread_data *td)
150 struct spliceio_data *sd = malloc(sizeof(*sd));
152 if (pipe(sd->pipe) < 0) {
153 td_verror(td, errno, "pipe");
158 td->io_ops->data = sd;
162 static struct ioengine_ops ioengine = {
164 .version = FIO_IOOPS_VERSION,
165 .init = fio_spliceio_init,
166 .queue = fio_spliceio_queue,
167 .cleanup = fio_spliceio_cleanup,
168 .open_file = generic_open_file,
169 .close_file = generic_close_file,
173 #else /* FIO_HAVE_SPLICE */
176 * When we have a proper configure system in place, we simply wont build
177 * and install this io engine. For now install a crippled version that
178 * just complains and fails to load.
180 static int fio_spliceio_init(struct thread_data fio_unused *td)
182 fprintf(stderr, "fio: splice not available\n");
186 static struct ioengine_ops ioengine = {
188 .version = FIO_IOOPS_VERSION,
189 .init = fio_spliceio_init,
194 static void fio_init fio_spliceio_register(void)
196 register_ioengine(&ioengine);
199 static void fio_exit fio_spliceio_unregister(void)
201 unregister_ioengine(&ioengine);