[PATCH] Move td_verror() into io_ops->queue() hook
[fio.git] / engines / splice.c
CommitLineData
2866c82d
JA
1/*
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>
5f350952
JA
11
12#include "../fio.h"
13#include "../os.h"
2866c82d 14
34cfcdaf
JA
15#ifdef FIO_HAVE_SPLICE
16
2866c82d
JA
17struct spliceio_data {
18 struct io_u *last_io_u;
19 int pipe[2];
20};
21
2866c82d
JA
22static int fio_spliceio_getevents(struct thread_data *td, int fio_unused min,
23 int max, struct timespec fio_unused *t)
24{
25 assert(max <= 1);
26
27 /*
28 * we can only have one finished io_u for sync io, since the depth
29 * is always 1
30 */
31 if (list_empty(&td->io_u_busylist))
32 return 0;
33
34 return 1;
35}
36
37static struct io_u *fio_spliceio_event(struct thread_data *td, int event)
38{
39 struct spliceio_data *sd = td->io_ops->data;
40
41 assert(event == 0);
42
43 return sd->last_io_u;
44}
45
46/*
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...
50 */
51static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
52{
53 struct spliceio_data *sd = td->io_ops->data;
53cdc686 54 struct fio_file *f = io_u->file;
2866c82d
JA
55 int ret, ret2, buflen;
56 off_t offset;
57 void *p;
58
59 offset = io_u->offset;
cec6b55d
JA
60 buflen = io_u->xfer_buflen;
61 p = io_u->xfer_buf;
2866c82d
JA
62 while (buflen) {
63 int this_len = buflen;
64
65 if (this_len > SPLICE_DEF_SIZE)
66 this_len = SPLICE_DEF_SIZE;
67
53cdc686 68 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
2866c82d
JA
69 if (ret < 0) {
70 if (errno == ENODATA || errno == EAGAIN)
71 continue;
72
73 return errno;
74 }
75
76 buflen -= ret;
77
78 while (ret) {
79 ret2 = read(sd->pipe[0], p, ret);
80 if (ret2 < 0)
81 return errno;
82
83 ret -= ret2;
84 p += ret2;
85 }
86 }
87
cec6b55d 88 return io_u->xfer_buflen;
2866c82d
JA
89}
90
91/*
92 * For splice writing, we can vmsplice our data buffer directly into a
93 * pipe and then splice that to a file.
94 */
95static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
96{
97 struct spliceio_data *sd = td->io_ops->data;
98 struct iovec iov[1] = {
99 {
cec6b55d
JA
100 .iov_base = io_u->xfer_buf,
101 .iov_len = io_u->xfer_buflen,
2866c82d
JA
102 }
103 };
104 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
53cdc686 105 struct fio_file *f = io_u->file;
2866c82d
JA
106 off_t off = io_u->offset;
107 int ret, ret2;
108
109 while (iov[0].iov_len) {
110 if (poll(&pfd, 1, -1) < 0)
111 return errno;
112
113 ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK);
114 if (ret < 0)
115 return errno;
116
117 iov[0].iov_len -= ret;
118 iov[0].iov_base += ret;
119
120 while (ret) {
53cdc686 121 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
2866c82d
JA
122 if (ret2 < 0)
123 return errno;
124
125 ret -= ret2;
126 }
127 }
128
cec6b55d 129 return io_u->xfer_buflen;
2866c82d
JA
130}
131
132static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
133{
134 struct spliceio_data *sd = td->io_ops->data;
cec6b55d 135 int ret;
2866c82d
JA
136
137 if (io_u->ddir == DDIR_READ)
138 ret = fio_splice_read(td, io_u);
87dc1ab1 139 else if (io_u->ddir == DDIR_WRITE)
2866c82d 140 ret = fio_splice_write(td, io_u);
87dc1ab1
JA
141 else
142 ret = fsync(io_u->file->fd);
2866c82d 143
cec6b55d 144 if (ret != (int) io_u->xfer_buflen) {
2866c82d 145 if (ret > 0) {
cec6b55d
JA
146 io_u->resid = io_u->xfer_buflen - ret;
147 io_u->error = 0;
148 return ret;
2866c82d
JA
149 } else
150 io_u->error = errno;
151 }
152
153 if (!io_u->error)
154 sd->last_io_u = io_u;
95bcd815
JA
155 else
156 td_verror(td, io_u->error);
2866c82d
JA
157
158 return io_u->error;
159}
160
161static void fio_spliceio_cleanup(struct thread_data *td)
162{
163 struct spliceio_data *sd = td->io_ops->data;
164
165 if (sd) {
166 close(sd->pipe[0]);
167 close(sd->pipe[1]);
168 free(sd);
169 td->io_ops->data = NULL;
170 }
171}
172
173static int fio_spliceio_init(struct thread_data *td)
174{
175 struct spliceio_data *sd = malloc(sizeof(*sd));
176
177 sd->last_io_u = NULL;
178 if (pipe(sd->pipe) < 0) {
179 td_verror(td, errno);
180 free(sd);
181 return 1;
182 }
183
184 td->io_ops->data = sd;
185 return 0;
186}
187
5f350952 188static struct ioengine_ops ioengine = {
2866c82d
JA
189 .name = "splice",
190 .version = FIO_IOOPS_VERSION,
191 .init = fio_spliceio_init,
192 .queue = fio_spliceio_queue,
193 .getevents = fio_spliceio_getevents,
194 .event = fio_spliceio_event,
195 .cleanup = fio_spliceio_cleanup,
2866c82d
JA
196 .flags = FIO_SYNCIO,
197};
34cfcdaf
JA
198
199#else /* FIO_HAVE_SPLICE */
200
201/*
202 * When we have a proper configure system in place, we simply wont build
203 * and install this io engine. For now install a crippled version that
204 * just complains and fails to load.
205 */
206static int fio_spliceio_init(struct thread_data fio_unused *td)
207{
208 fprintf(stderr, "fio: splice not available\n");
209 return 1;
210}
211
5f350952 212static struct ioengine_ops ioengine = {
34cfcdaf
JA
213 .name = "splice",
214 .version = FIO_IOOPS_VERSION,
215 .init = fio_spliceio_init,
216};
217
218#endif
5f350952
JA
219
220static void fio_init fio_spliceio_register(void)
221{
222 register_ioengine(&ioengine);
223}
224
225static void fio_exit fio_spliceio_unregister(void)
226{
227 unregister_ioengine(&ioengine);
228}