[PATCH] syslet: async_head_user struct should be permanent
[fio.git] / engines / splice.c
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>
11
12 #include "../fio.h"
13 #include "../os.h"
14
15 #ifdef FIO_HAVE_SPLICE
16
17 struct spliceio_data {
18         struct io_u *last_io_u;
19         int pipe[2];
20 };
21
22 static 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
37 static 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  */
51 static int fio_splice_read(struct thread_data *td, struct io_u *io_u)
52 {
53         struct spliceio_data *sd = td->io_ops->data;
54         struct fio_file *f = io_u->file;
55         int ret, ret2, buflen;
56         off_t offset;
57         void *p;
58
59         offset = io_u->offset;
60         buflen = io_u->xfer_buflen;
61         p = io_u->xfer_buf;
62         while (buflen) {
63                 int this_len = buflen;
64
65                 if (this_len > SPLICE_DEF_SIZE)
66                         this_len = SPLICE_DEF_SIZE;
67
68                 ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE);
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->xfer_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  */
95 static 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->xfer_buf,
101                         .iov_len = io_u->xfer_buflen,
102                 }
103         };
104         struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, };
105         struct fio_file *f = io_u->file;
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) {
121                         ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0);
122                         if (ret2 < 0)
123                                 return errno;
124
125                         ret -= ret2;
126                 }
127         }
128
129         return io_u->xfer_buflen;
130 }
131
132 static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u)
133 {
134         struct spliceio_data *sd = td->io_ops->data;
135         int ret;
136
137         if (io_u->ddir == DDIR_READ)
138                 ret = fio_splice_read(td, io_u);
139         else if (io_u->ddir == DDIR_WRITE)
140                 ret = fio_splice_write(td, io_u);
141         else
142                 ret = fsync(io_u->file->fd);
143
144         if (ret != (int) io_u->xfer_buflen) {
145                 if (ret > 0) {
146                         io_u->resid = io_u->xfer_buflen - ret;
147                         io_u->error = 0;
148                         return ret;
149                 } else
150                         io_u->error = errno;
151         }
152
153         if (!io_u->error)
154                 sd->last_io_u = io_u;
155         else
156                 td_verror(td, io_u->error);
157
158         return io_u->error;
159 }
160
161 static 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
173 static 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
188 static struct ioengine_ops ioengine = {
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,
196         .flags          = FIO_SYNCIO,
197 };
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  */
206 static int fio_spliceio_init(struct thread_data fio_unused *td)
207 {
208         fprintf(stderr, "fio: splice not available\n");
209         return 1;
210 }
211
212 static struct ioengine_ops ioengine = {
213         .name           = "splice",
214         .version        = FIO_IOOPS_VERSION,
215         .init           = fio_spliceio_init,
216 };
217
218 #endif
219
220 static void fio_init fio_spliceio_register(void)
221 {
222         register_ioengine(&ioengine);
223 }
224
225 static void fio_exit fio_spliceio_unregister(void)
226 {
227         unregister_ioengine(&ioengine);
228 }