[PATCH] Rename io engines
[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;
60 buflen = io_u->buflen;
61 p = io_u->buf;
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
88 return io_u->buflen;
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 {
100 .iov_base = io_u->buf,
101 .iov_len = io_u->buflen,
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
129 return io_u->buflen;
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;
6040dabc 135 unsigned 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
6040dabc 144 if (ret != io_u->buflen) {
2866c82d
JA
145 if (ret > 0) {
146 io_u->resid = io_u->buflen - ret;
147 io_u->error = ENODATA;
148 } else
149 io_u->error = errno;
150 }
151
152 if (!io_u->error)
153 sd->last_io_u = io_u;
154
155 return io_u->error;
156}
157
158static void fio_spliceio_cleanup(struct thread_data *td)
159{
160 struct spliceio_data *sd = td->io_ops->data;
161
162 if (sd) {
163 close(sd->pipe[0]);
164 close(sd->pipe[1]);
165 free(sd);
166 td->io_ops->data = NULL;
167 }
168}
169
170static int fio_spliceio_init(struct thread_data *td)
171{
172 struct spliceio_data *sd = malloc(sizeof(*sd));
173
174 sd->last_io_u = NULL;
175 if (pipe(sd->pipe) < 0) {
176 td_verror(td, errno);
177 free(sd);
178 return 1;
179 }
180
181 td->io_ops->data = sd;
182 return 0;
183}
184
5f350952 185static struct ioengine_ops ioengine = {
2866c82d
JA
186 .name = "splice",
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,
2866c82d
JA
193 .flags = FIO_SYNCIO,
194};
34cfcdaf
JA
195
196#else /* FIO_HAVE_SPLICE */
197
198/*
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.
202 */
203static int fio_spliceio_init(struct thread_data fio_unused *td)
204{
205 fprintf(stderr, "fio: splice not available\n");
206 return 1;
207}
208
5f350952 209static struct ioengine_ops ioengine = {
34cfcdaf
JA
210 .name = "splice",
211 .version = FIO_IOOPS_VERSION,
212 .init = fio_spliceio_init,
213};
214
215#endif
5f350952
JA
216
217static void fio_init fio_spliceio_register(void)
218{
219 register_ioengine(&ioengine);
220}
221
222static void fio_exit fio_spliceio_unregister(void)
223{
224 unregister_ioengine(&ioengine);
225}