15 #ifdef FIO_HAVE_SPLICE
17 struct spliceio_data {
18 struct io_u *last_io_u;
22 static int fio_spliceio_getevents(struct thread_data *td, int fio_unused min,
23 int max, struct timespec fio_unused *t)
28 * we can only have one finished io_u for sync io, since the depth
31 if (list_empty(&td->io_u_busylist))
37 static struct io_u *fio_spliceio_event(struct thread_data *td, int event)
39 struct spliceio_data *sd = td->io_ops->data;
47 * For splice reading, we unfortunately cannot (yet) vmsplice the other way.
48 * So just splice the data from the file into the pipe, and use regular
49 * read to fill the buffer. Doesn't make a lot of sense, but...
51 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
53 struct spliceio_data *sd = td->io_ops->data;
54 struct fio_file *f = io_u->file;
55 int ret, ret2, buflen;
59 offset = io_u->offset;
60 buflen = io_u->buflen;
63 int this_len = buflen;
65 if (this_len > SPLICE_DEF_SIZE)
66 this_len = SPLICE_DEF_SIZE;
68 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
70 if (errno == ENODATA || errno == EAGAIN)
79 ret2 = read(sd->pipe[0], p, ret);
92 * For splice writing, we can vmsplice our data buffer directly into a
93 * pipe and then splice that to a file.
95 static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
97 struct spliceio_data *sd = td->io_ops->data;
98 struct iovec iov[1] = {
100 .iov_base = io_u->buf,
101 .iov_len = io_u->buflen,
104 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
105 struct fio_file *f = io_u->file;
106 off_t off = io_u->offset;
109 while (iov[0].iov_len) {
110 if (poll(&pfd, 1, -1) < 0)
113 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
117 iov[0].iov_len -= ret;
118 iov[0].iov_base += ret;
121 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
132 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
134 struct spliceio_data *sd = td->io_ops->data;
137 if (io_u->ddir == DDIR_READ)
138 ret = fio_splice_read(td, io_u);
139 else if (io_u->ddir == DDIR_WRITE)
140 ret = fio_splice_write(td, io_u);
142 ret = fsync(io_u->file->fd);
144 if (ret != io_u->buflen) {
146 io_u->resid = io_u->buflen - ret;
147 io_u->error = ENODATA;
153 sd->last_io_u = io_u;
158 static void fio_spliceio_cleanup(struct thread_data *td)
160 struct spliceio_data *sd = td->io_ops->data;
166 td->io_ops->data = NULL;
170 static int fio_spliceio_init(struct thread_data *td)
172 struct spliceio_data *sd = malloc(sizeof(*sd));
174 sd->last_io_u = NULL;
175 if (pipe(sd->pipe) < 0) {
176 td_verror(td, errno);
181 td->io_ops->data = sd;
185 static struct ioengine_ops ioengine = {
187 .version = FIO_IOOPS_VERSION,
188 .init = fio_spliceio_init,
189 .queue = fio_spliceio_queue,
190 .getevents = fio_spliceio_getevents,
191 .event = fio_spliceio_event,
192 .cleanup = fio_spliceio_cleanup,
196 #else /* FIO_HAVE_SPLICE */
199 * When we have a proper configure system in place, we simply wont build
200 * and install this io engine. For now install a crippled version that
201 * just complains and fails to load.
203 static int fio_spliceio_init(struct thread_data fio_unused *td)
205 fprintf(stderr, "fio: splice not available\n");
209 static struct ioengine_ops ioengine = {
211 .version = FIO_IOOPS_VERSION,
212 .init = fio_spliceio_init,
217 static void fio_init fio_spliceio_register(void)
219 register_ioengine(&ioengine);
222 static void fio_exit fio_spliceio_unregister(void)
224 unregister_ioengine(&ioengine);