15 #ifdef FIO_HAVE_SPLICE
17 struct spliceio_data {
22 * For splice reading, we unfortunately cannot (yet) vmsplice the other way.
23 * So just splice the data from the file into the pipe, and use regular
24 * read to fill the buffer. Doesn't make a lot of sense, but...
26 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
28 struct spliceio_data *sd = td->io_ops->data;
29 struct fio_file *f = io_u->file;
30 int ret, ret2, buflen;
34 offset = io_u->offset;
35 buflen = io_u->xfer_buflen;
38 int this_len = buflen;
40 if (this_len > SPLICE_DEF_SIZE)
41 this_len = SPLICE_DEF_SIZE;
43 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
45 if (errno == ENODATA || errno == EAGAIN)
54 ret2 = read(sd->pipe[0], p, ret);
63 return io_u->xfer_buflen;
67 * For splice writing, we can vmsplice our data buffer directly into a
68 * pipe and then splice that to a file.
70 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
72 struct spliceio_data *sd = td->io_ops->data;
73 struct iovec iov[1] = {
75 .iov_base = io_u->xfer_buf,
76 .iov_len = io_u->xfer_buflen,
79 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
80 struct fio_file *f = io_u->file;
81 off_t off = io_u->offset;
84 while (iov[0].iov_len) {
85 if (poll(&pfd, 1, -1) < 0)
88 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
92 iov[0].iov_len -= ret;
93 iov[0].iov_base += ret;
96 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
104 return io_u->xfer_buflen;
107 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
111 if (io_u->ddir == DDIR_READ)
112 ret = fio_splice_read(td, io_u);
113 else if (io_u->ddir == DDIR_WRITE)
114 ret = fio_splice_write(td, io_u);
116 ret = fsync(io_u->file->fd);
118 if (ret != (int) io_u->xfer_buflen) {
120 io_u->resid = io_u->xfer_buflen - ret;
122 return FIO_Q_COMPLETED;
128 td_verror(td, io_u->error, "xfer");
130 return FIO_Q_COMPLETED;
133 static void fio_spliceio_cleanup(struct thread_data *td)
135 struct spliceio_data *sd = td->io_ops->data;
141 td->io_ops->data = NULL;
145 static int fio_spliceio_init(struct thread_data *td)
147 struct spliceio_data *sd = malloc(sizeof(*sd));
149 if (pipe(sd->pipe) < 0) {
150 td_verror(td, errno, "pipe");
155 td->io_ops->data = sd;
159 static struct ioengine_ops ioengine = {
161 .version = FIO_IOOPS_VERSION,
162 .init = fio_spliceio_init,
163 .queue = fio_spliceio_queue,
164 .cleanup = fio_spliceio_cleanup,
168 #else /* FIO_HAVE_SPLICE */
171 * When we have a proper configure system in place, we simply wont build
172 * and install this io engine. For now install a crippled version that
173 * just complains and fails to load.
175 static int fio_spliceio_init(struct thread_data fio_unused *td)
177 fprintf(stderr, "fio: splice not available\n");
181 static struct ioengine_ops ioengine = {
183 .version = FIO_IOOPS_VERSION,
184 .init = fio_spliceio_init,
189 static void fio_init fio_spliceio_register(void)
191 register_ioengine(&ioengine);
194 static void fio_exit fio_spliceio_unregister(void)
196 unregister_ioengine(&ioengine);