Rate must always calculate bytes done
[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 17struct spliceio_data {
2866c82d
JA
18 int pipe[2];
19};
20
2866c82d
JA
21/*
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;
53cdc686 29 struct fio_file *f = io_u->file;
2866c82d
JA
30 int ret, ret2, buflen;
31 off_t offset;
32 void *p;
33
34 offset = io_u->offset;
cec6b55d
JA
35 buflen = io_u->xfer_buflen;
36 p = io_u->xfer_buf;
2866c82d
JA
37 while (buflen) {
38 int this_len = buflen;
39
40 if (this_len > SPLICE_DEF_SIZE)
41 this_len = SPLICE_DEF_SIZE;
42
53cdc686 43 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
2866c82d
JA
44 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
cec6b55d 63 return io_u->xfer_buflen;
2866c82d
JA
64}
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 {
cec6b55d
JA
75 .iov_base = io_u->xfer_buf,
76 .iov_len = io_u->xfer_buflen,
2866c82d
JA
77 }
78 };
79 struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
53cdc686 80 struct fio_file *f = io_u->file;
2866c82d
JA
81 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) {
53cdc686 96 ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
2866c82d
JA
97 if (ret2 < 0)
98 return errno;
99
100 ret -= ret2;
101 }
102 }
103
cec6b55d 104 return io_u->xfer_buflen;
2866c82d
JA
105}
106
107static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
108{
cec6b55d 109 int ret;
2866c82d
JA
110
111 if (io_u->ddir == DDIR_READ)
112 ret = fio_splice_read(td, io_u);
87dc1ab1 113 else if (io_u->ddir == DDIR_WRITE)
2866c82d 114 ret = fio_splice_write(td, io_u);
87dc1ab1
JA
115 else
116 ret = fsync(io_u->file->fd);
2866c82d 117
cec6b55d 118 if (ret != (int) io_u->xfer_buflen) {
22819ec2 119 if (ret >= 0) {
cec6b55d
JA
120 io_u->resid = io_u->xfer_buflen - ret;
121 io_u->error = 0;
36167d82 122 return FIO_Q_COMPLETED;
2866c82d
JA
123 } else
124 io_u->error = errno;
125 }
126
36167d82 127 if (io_u->error)
e1161c32 128 td_verror(td, io_u->error, "xfer");
2866c82d 129
36167d82 130 return FIO_Q_COMPLETED;
2866c82d
JA
131}
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
2866c82d 149 if (pipe(sd->pipe) < 0) {
e1161c32 150 td_verror(td, errno, "pipe");
2866c82d
JA
151 free(sd);
152 return 1;
153 }
154
155 td->io_ops->data = sd;
156 return 0;
157}
158
5f350952 159static struct ioengine_ops ioengine = {
2866c82d
JA
160 .name = "splice",
161 .version = FIO_IOOPS_VERSION,
162 .init = fio_spliceio_init,
163 .queue = fio_spliceio_queue,
2866c82d 164 .cleanup = fio_spliceio_cleanup,
2866c82d
JA
165 .flags = FIO_SYNCIO,
166};
34cfcdaf
JA
167
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
5f350952 181static struct ioengine_ops ioengine = {
34cfcdaf
JA
182 .name = "splice",
183 .version = FIO_IOOPS_VERSION,
184 .init = fio_spliceio_init,
185};
186
187#endif
5f350952
JA
188
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}