[PATCH] SG IO engine: reduce allocations and memory leaks
[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;
155
156 return io_u->error;
157}
158
159static void fio_spliceio_cleanup(struct thread_data *td)
160{
161 struct spliceio_data *sd = td->io_ops->data;
162
163 if (sd) {
164 close(sd->pipe[0]);
165 close(sd->pipe[1]);
166 free(sd);
167 td->io_ops->data = NULL;
168 }
169}
170
171static int fio_spliceio_init(struct thread_data *td)
172{
173 struct spliceio_data *sd = malloc(sizeof(*sd));
174
175 sd->last_io_u = NULL;
176 if (pipe(sd->pipe) < 0) {
177 td_verror(td, errno);
178 free(sd);
179 return 1;
180 }
181
182 td->io_ops->data = sd;
183 return 0;
184}
185
5f350952 186static struct ioengine_ops ioengine = {
2866c82d
JA
187 .name = "splice",
188 .version = FIO_IOOPS_VERSION,
189 .init = fio_spliceio_init,
190 .queue = fio_spliceio_queue,
191 .getevents = fio_spliceio_getevents,
192 .event = fio_spliceio_event,
193 .cleanup = fio_spliceio_cleanup,
2866c82d
JA
194 .flags = FIO_SYNCIO,
195};
34cfcdaf
JA
196
197#else /* FIO_HAVE_SPLICE */
198
199/*
200 * When we have a proper configure system in place, we simply wont build
201 * and install this io engine. For now install a crippled version that
202 * just complains and fails to load.
203 */
204static int fio_spliceio_init(struct thread_data fio_unused *td)
205{
206 fprintf(stderr, "fio: splice not available\n");
207 return 1;
208}
209
5f350952 210static struct ioengine_ops ioengine = {
34cfcdaf
JA
211 .name = "splice",
212 .version = FIO_IOOPS_VERSION,
213 .init = fio_spliceio_init,
214};
215
216#endif
5f350952
JA
217
218static void fio_init fio_spliceio_register(void)
219{
220 register_ioengine(&ioengine);
221}
222
223static void fio_exit fio_spliceio_unregister(void)
224{
225 unregister_ioengine(&ioengine);
226}