blob: f55e5c0703273357781f4af7e548ba2be629d94a [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
2 * splice io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10#include <sys/poll.h>
Jens Axboe5f350952006-11-07 15:20:59 +010011
12#include "../fio.h"
13#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020014
Jens Axboe34cfcda2006-11-03 14:00:45 +010015#ifdef FIO_HAVE_SPLICE
16
Jens Axboe2866c822006-10-09 15:57:48 +020017struct spliceio_data {
Jens Axboe2866c822006-10-09 15:57:48 +020018 int pipe[2];
19};
20
Jens Axboe2866c822006-10-09 15:57:48 +020021/*
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...
25 */
26static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
27{
28 struct spliceio_data *sd = td->io_ops->data;
Jens Axboe53cdc682006-10-18 11:50:58 +020029 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +020030 int ret, ret2, buflen;
31 off_t offset;
32 void *p;
33
34 offset = io_u->offset;
Jens Axboecec6b552007-02-06 20:15:38 +010035 buflen = io_u->xfer_buflen;
36 p = io_u->xfer_buf;
Jens Axboe2866c822006-10-09 15:57:48 +020037 while (buflen) {
38 int this_len = buflen;
39
40 if (this_len > SPLICE_DEF_SIZE)
41 this_len = SPLICE_DEF_SIZE;
42
Jens Axboe53cdc682006-10-18 11:50:58 +020043 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
Jens Axboe2866c822006-10-09 15:57:48 +020044 if (ret < 0) {
45 if (errno == ENODATA || errno == EAGAIN)
46 continue;
47
48 return errno;
49 }
50
51 buflen -= ret;
52
53 while (ret) {
54 ret2 = read(sd->pipe[0], p, ret);
55 if (ret2 < 0)
56 return errno;
57
58 ret -= ret2;
59 p += ret2;
60 }
61 }
62
Jens Axboecec6b552007-02-06 20:15:38 +010063 return io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020064}
65
66/*
67 * For splice writing, we can vmsplice our data buffer directly into a
68 * pipe and then splice that to a file.
69 */
70static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
71{
72 struct spliceio_data *sd = td->io_ops->data;
73 struct iovec iov[1] = {
74 {
Jens Axboecec6b552007-02-06 20:15:38 +010075 .iov_base = io_u->xfer_buf,
76 .iov_len = io_u->xfer_buflen,
Jens Axboe2866c822006-10-09 15:57:48 +020077 }
78 };
79 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
Jens Axboe53cdc682006-10-18 11:50:58 +020080 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +020081 off_t off = io_u->offset;
82 int ret, ret2;
83
84 while (iov[0].iov_len) {
85 if (poll(&pfd, 1, -1) < 0)
86 return errno;
87
88 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
89 if (ret < 0)
90 return errno;
91
92 iov[0].iov_len -= ret;
93 iov[0].iov_base += ret;
94
95 while (ret) {
Jens Axboe53cdc682006-10-18 11:50:58 +020096 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
Jens Axboe2866c822006-10-09 15:57:48 +020097 if (ret2 < 0)
98 return errno;
99
100 ret -= ret2;
101 }
102 }
103
Jens Axboecec6b552007-02-06 20:15:38 +0100104 return io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +0200105}
106
107static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
108{
Jens Axboecec6b552007-02-06 20:15:38 +0100109 int ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200110
111 if (io_u->ddir == DDIR_READ)
112 ret = fio_splice_read(td, io_u);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200113 else if (io_u->ddir == DDIR_WRITE)
Jens Axboe2866c822006-10-09 15:57:48 +0200114 ret = fio_splice_write(td, io_u);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200115 else
116 ret = fsync(io_u->file->fd);
Jens Axboe2866c822006-10-09 15:57:48 +0200117
Jens Axboecec6b552007-02-06 20:15:38 +0100118 if (ret != (int) io_u->xfer_buflen) {
Jens Axboe2866c822006-10-09 15:57:48 +0200119 if (ret > 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100120 io_u->resid = io_u->xfer_buflen - ret;
121 io_u->error = 0;
Jens Axboe36167d82007-02-18 05:41:31 +0100122 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200123 } else
124 io_u->error = errno;
125 }
126
Jens Axboe36167d82007-02-18 05:41:31 +0100127 if (io_u->error)
Jens Axboe95bcd812007-02-11 01:01:57 +0100128 td_verror(td, io_u->error);
Jens Axboe2866c822006-10-09 15:57:48 +0200129
Jens Axboe36167d82007-02-18 05:41:31 +0100130 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200131}
132
133static void fio_spliceio_cleanup(struct thread_data *td)
134{
135 struct spliceio_data *sd = td->io_ops->data;
136
137 if (sd) {
138 close(sd->pipe[0]);
139 close(sd->pipe[1]);
140 free(sd);
141 td->io_ops->data = NULL;
142 }
143}
144
145static int fio_spliceio_init(struct thread_data *td)
146{
147 struct spliceio_data *sd = malloc(sizeof(*sd));
148
Jens Axboe2866c822006-10-09 15:57:48 +0200149 if (pipe(sd->pipe) < 0) {
150 td_verror(td, errno);
151 free(sd);
152 return 1;
153 }
154
155 td->io_ops->data = sd;
156 return 0;
157}
158
Jens Axboe5f350952006-11-07 15:20:59 +0100159static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200160 .name = "splice",
161 .version = FIO_IOOPS_VERSION,
162 .init = fio_spliceio_init,
163 .queue = fio_spliceio_queue,
Jens Axboe2866c822006-10-09 15:57:48 +0200164 .cleanup = fio_spliceio_cleanup,
Jens Axboe2866c822006-10-09 15:57:48 +0200165 .flags = FIO_SYNCIO,
166};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100167
168#else /* FIO_HAVE_SPLICE */
169
170/*
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.
174 */
175static int fio_spliceio_init(struct thread_data fio_unused *td)
176{
177 fprintf(stderr, "fio: splice not available\n");
178 return 1;
179}
180
Jens Axboe5f350952006-11-07 15:20:59 +0100181static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100182 .name = "splice",
183 .version = FIO_IOOPS_VERSION,
184 .init = fio_spliceio_init,
185};
186
187#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100188
189static void fio_init fio_spliceio_register(void)
190{
191 register_ioengine(&ioengine);
192}
193
194static void fio_exit fio_spliceio_unregister(void)
195{
196 unregister_ioengine(&ioengine);
197}