libaio engine: print warning for depth > 1 and buffered IO
[fio.git] / engines / splice.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * splice engine
3 *
4 * IO engine that transfers data by doing splices to/from pipes and
5 * the files.
2866c82d
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
12#include <assert.h>
13#include <sys/poll.h>
5f350952
JA
14
15#include "../fio.h"
2866c82d 16
34cfcdaf
JA
17#ifdef FIO_HAVE_SPLICE
18
2866c82d 19struct spliceio_data {
2866c82d 20 int pipe[2];
f24254e1 21 int vmsplice_to_user;
2866c82d
JA
22};
23
2866c82d 24/*
f24254e1
JA
25 * vmsplice didn't use to support splicing to user space, this is the old
26 * variant of getting that job done. Doesn't make a lot of sense, but it
27 * uses splices to move data from the source into a pipe.
2866c82d 28 */
f24254e1 29static int fio_splice_read_old(struct thread_data *td, struct io_u *io_u)
2866c82d
JA
30{
31 struct spliceio_data *sd = td->io_ops->data;
53cdc686 32 struct fio_file *f = io_u->file;
2866c82d
JA
33 int ret, ret2, buflen;
34 off_t offset;
35 void *p;
36
37 offset = io_u->offset;
cec6b55d
JA
38 buflen = io_u->xfer_buflen;
39 p = io_u->xfer_buf;
2866c82d
JA
40 while (buflen) {
41 int this_len = buflen;
42
43 if (this_len > SPLICE_DEF_SIZE)
44 this_len = SPLICE_DEF_SIZE;
45
53cdc686 46 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
2866c82d
JA
47 if (ret < 0) {
48 if (errno == ENODATA || errno == EAGAIN)
49 continue;
50
b4ba5f30 51 return -errno;
2866c82d
JA
52 }
53
54 buflen -= ret;
55
56 while (ret) {
57 ret2 = read(sd->pipe[0], p, ret);
58 if (ret2 < 0)
b4ba5f30 59 return -errno;
2866c82d
JA
60
61 ret -= ret2;
62 p += ret2;
63 }
64 }
65
cec6b55d 66 return io_u->xfer_buflen;
2866c82d
JA
67}
68
f24254e1
JA
69/*
70 * We can now vmsplice into userspace, so do the transfer by splicing into
71 * a pipe and vmsplicing that into userspace.
72 */
73static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
74{
75 struct spliceio_data *sd = td->io_ops->data;
76 struct fio_file *f = io_u->file;
77 struct iovec iov;
78 int ret, buflen;
79 off_t offset;
80 void *p;
81
82 offset = io_u->offset;
83 buflen = io_u->xfer_buflen;
84 p = io_u->xfer_buf;
85 while (buflen) {
86 int this_len = buflen;
87
88 if (this_len > SPLICE_DEF_SIZE)
89 this_len = SPLICE_DEF_SIZE;
90
91 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
92 if (ret < 0) {
93 if (errno == ENODATA || errno == EAGAIN)
94 continue;
95
96 return -errno;
97 }
98
99 buflen -= ret;
100 iov.iov_base = p;
101 iov.iov_len = ret;
102 p += ret;
103
104 while (iov.iov_len) {
105 ret = vmsplice(sd->pipe[0], &iov, 1, SPLICE_F_MOVE);
106 if (ret < 0)
107 return -errno;
108 else if (!ret)
109 return -ENODATA;
110
111 iov.iov_len -= ret;
112 iov.iov_base += ret;
113 }
114 }
115
116 return io_u->xfer_buflen;
117}
118
119
2866c82d
JA
120/*
121 * For splice writing, we can vmsplice our data buffer directly into a
122 * pipe and then splice that to a file.
123 */
124static int fio_splice_write(struct thread_data *td, struct io_u *io_u)
125{
126 struct spliceio_data *sd = td->io_ops->data;
f24254e1
JA
127 struct iovec iov = {
128 .iov_base = io_u->xfer_buf,
129 .iov_len = io_u->xfer_buflen,
2866c82d
JA
130 };
131 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
53cdc686 132 struct fio_file *f = io_u->file;
2866c82d
JA
133 off_t off = io_u->offset;
134 int ret, ret2;
135
f24254e1 136 while (iov.iov_len) {
2866c82d
JA
137 if (poll(&pfd, 1, -1) < 0)
138 return errno;
139
f24254e1 140 ret = vmsplice(sd->pipe[1], &iov, 1, SPLICE_F_NONBLOCK);
2866c82d 141 if (ret < 0)
b4ba5f30 142 return -errno;
2866c82d 143
f24254e1
JA
144 iov.iov_len -= ret;
145 iov.iov_base += ret;
2866c82d
JA
146
147 while (ret) {
53cdc686 148 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
2866c82d 149 if (ret2 < 0)
b4ba5f30 150 return -errno;
2866c82d
JA
151
152 ret -= ret2;
153 }
154 }
155
cec6b55d 156 return io_u->xfer_buflen;
2866c82d
JA
157}
158
159static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
160{
f24254e1 161 struct spliceio_data *sd = td->io_ops->data;
cec6b55d 162 int ret;
2866c82d 163
f24254e1
JA
164 if (io_u->ddir == DDIR_READ) {
165 if (sd->vmsplice_to_user)
166 ret = fio_splice_read(td, io_u);
167 else
168 ret = fio_splice_read_old(td, io_u);
169 } else if (io_u->ddir == DDIR_WRITE)
2866c82d 170 ret = fio_splice_write(td, io_u);
87dc1ab1
JA
171 else
172 ret = fsync(io_u->file->fd);
2866c82d 173
cec6b55d 174 if (ret != (int) io_u->xfer_buflen) {
22819ec2 175 if (ret >= 0) {
cec6b55d
JA
176 io_u->resid = io_u->xfer_buflen - ret;
177 io_u->error = 0;
36167d82 178 return FIO_Q_COMPLETED;
2866c82d
JA
179 } else
180 io_u->error = errno;
181 }
182
36167d82 183 if (io_u->error)
e1161c32 184 td_verror(td, io_u->error, "xfer");
2866c82d 185
36167d82 186 return FIO_Q_COMPLETED;
2866c82d
JA
187}
188
189static void fio_spliceio_cleanup(struct thread_data *td)
190{
191 struct spliceio_data *sd = td->io_ops->data;
192
193 if (sd) {
194 close(sd->pipe[0]);
195 close(sd->pipe[1]);
196 free(sd);
197 td->io_ops->data = NULL;
198 }
199}
200
201static int fio_spliceio_init(struct thread_data *td)
202{
203 struct spliceio_data *sd = malloc(sizeof(*sd));
204
2866c82d 205 if (pipe(sd->pipe) < 0) {
e1161c32 206 td_verror(td, errno, "pipe");
2866c82d
JA
207 free(sd);
208 return 1;
209 }
210
f24254e1
JA
211 /*
212 * need some check for enabling this, for now just leave it disabled
213 */
214 sd->vmsplice_to_user = 0;
215
2866c82d
JA
216 td->io_ops->data = sd;
217 return 0;
218}
219
5f350952 220static struct ioengine_ops ioengine = {
2866c82d
JA
221 .name = "splice",
222 .version = FIO_IOOPS_VERSION,
223 .init = fio_spliceio_init,
224 .queue = fio_spliceio_queue,
2866c82d 225 .cleanup = fio_spliceio_cleanup,
b5af8293
JA
226 .open_file = generic_open_file,
227 .close_file = generic_close_file,
2866c82d
JA
228 .flags = FIO_SYNCIO,
229};
34cfcdaf
JA
230
231#else /* FIO_HAVE_SPLICE */
232
233/*
234 * When we have a proper configure system in place, we simply wont build
235 * and install this io engine. For now install a crippled version that
236 * just complains and fails to load.
237 */
238static int fio_spliceio_init(struct thread_data fio_unused *td)
239{
240 fprintf(stderr, "fio: splice not available\n");
241 return 1;
242}
243
5f350952 244static struct ioengine_ops ioengine = {
34cfcdaf
JA
245 .name = "splice",
246 .version = FIO_IOOPS_VERSION,
247 .init = fio_spliceio_init,
248};
249
250#endif
5f350952
JA
251
252static void fio_init fio_spliceio_register(void)
253{
254 register_ioengine(&ioengine);
255}
256
257static void fio_exit fio_spliceio_unregister(void)
258{
259 unregister_ioengine(&ioengine);
260}