4 * IO engine that transfers data by doing splices to/from pipes and
17 #ifdef FIO_HAVE_SPLICE
19 struct spliceio_data {
24 * For splice reading, we unfortunately cannot (yet) vmsplice the other way.
25 * So just splice the data from the file into the pipe, and use regular
26 * read to fill the buffer. Doesn't make a lot of sense, but...
28 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
30 struct spliceio_data *sd = td->io_ops->data;
31 struct fio_file *f = io_u->file;
32 int ret, ret2, buflen;
36 offset = io_u->offset;
37 buflen = io_u->xfer_buflen;
40 int this_len = buflen;
42 if (this_len > SPLICE_DEF_SIZE)
43 this_len = SPLICE_DEF_SIZE;
45 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
47 if (errno == ENODATA || errno == EAGAIN)
56 ret2 = read(sd->pipe[0], p, ret);
65 return io_u->xfer_buflen;
69 * For splice writing, we can vmsplice our data buffer directly into a
70 * pipe and then splice that to a file.
72 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
74 struct spliceio_data *sd = td->io_ops->data;
75 struct iovec iov[1] = {
77 .iov_base = io_u->xfer_buf,
78 .iov_len = io_u->xfer_buflen,
81 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
82 struct fio_file *f = io_u->file;
83 off_t off = io_u->offset;
86 while (iov[0].iov_len) {
87 if (poll(&pfd, 1, -1) < 0)
90 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
94 iov[0].iov_len -= ret;
95 iov[0].iov_base += ret;
98 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
106 return io_u->xfer_buflen;
109 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
113 if (io_u->ddir == DDIR_READ)
114 ret = fio_splice_read(td, io_u);
115 else if (io_u->ddir == DDIR_WRITE)
116 ret = fio_splice_write(td, io_u);
118 ret = fsync(io_u->file->fd);
120 if (ret != (int) io_u->xfer_buflen) {
122 io_u->resid = io_u->xfer_buflen - ret;
124 return FIO_Q_COMPLETED;
130 td_verror(td, io_u->error, "xfer");
132 return FIO_Q_COMPLETED;
135 static void fio_spliceio_cleanup(struct thread_data *td)
137 struct spliceio_data *sd = td->io_ops->data;
143 td->io_ops->data = NULL;
147 static int fio_spliceio_init(struct thread_data *td)
149 struct spliceio_data *sd = malloc(sizeof(*sd));
151 if (pipe(sd->pipe) < 0) {
152 td_verror(td, errno, "pipe");
157 td->io_ops->data = sd;
161 static struct ioengine_ops ioengine = {
163 .version = FIO_IOOPS_VERSION,
164 .init = fio_spliceio_init,
165 .queue = fio_spliceio_queue,
166 .cleanup = fio_spliceio_cleanup,
167 .open_file = generic_open_file,
168 .close_file = generic_close_file,
172 #else /* FIO_HAVE_SPLICE */
175 * When we have a proper configure system in place, we simply wont build
176 * and install this io engine. For now install a crippled version that
177 * just complains and fails to load.
179 static int fio_spliceio_init(struct thread_data fio_unused *td)
181 fprintf(stderr, "fio: splice not available\n");
185 static struct ioengine_ops ioengine = {
187 .version = FIO_IOOPS_VERSION,
188 .init = fio_spliceio_init,
193 static void fio_init fio_spliceio_register(void)
195 register_ioengine(&ioengine);
198 static void fio_exit fio_spliceio_unregister(void)
200 unregister_ioengine(&ioengine);