Commit | Line | Data |
---|---|---|
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 | 19 | struct spliceio_data { |
2866c82d JA |
20 | int pipe[2]; |
21 | }; | |
22 | ||
2866c82d JA |
23 | /* |
24 | * For splice reading, we unfortunately cannot (yet) vmsplice the other way. | |
25 | * So just splice the data from the file into the pipe, and use regular | |
26 | * read to fill the buffer. Doesn't make a lot of sense, but... | |
27 | */ | |
28 | static int fio_splice_read(struct thread_data *td, struct io_u *io_u) | |
29 | { | |
30 | struct spliceio_data *sd = td->io_ops->data; | |
53cdc686 | 31 | struct fio_file *f = io_u->file; |
2866c82d JA |
32 | int ret, ret2, buflen; |
33 | off_t offset; | |
34 | void *p; | |
35 | ||
36 | offset = io_u->offset; | |
cec6b55d JA |
37 | buflen = io_u->xfer_buflen; |
38 | p = io_u->xfer_buf; | |
2866c82d JA |
39 | while (buflen) { |
40 | int this_len = buflen; | |
41 | ||
42 | if (this_len > SPLICE_DEF_SIZE) | |
43 | this_len = SPLICE_DEF_SIZE; | |
44 | ||
53cdc686 | 45 | ret = splice(f->fd, &offset, sd->pipe[1], NULL, this_len, SPLICE_F_MORE); |
2866c82d JA |
46 | if (ret < 0) { |
47 | if (errno == ENODATA || errno == EAGAIN) | |
48 | continue; | |
49 | ||
b4ba5f30 | 50 | return -errno; |
2866c82d JA |
51 | } |
52 | ||
53 | buflen -= ret; | |
54 | ||
55 | while (ret) { | |
56 | ret2 = read(sd->pipe[0], p, ret); | |
57 | if (ret2 < 0) | |
b4ba5f30 | 58 | return -errno; |
2866c82d JA |
59 | |
60 | ret -= ret2; | |
61 | p += ret2; | |
62 | } | |
63 | } | |
64 | ||
cec6b55d | 65 | return io_u->xfer_buflen; |
2866c82d JA |
66 | } |
67 | ||
68 | /* | |
69 | * For splice writing, we can vmsplice our data buffer directly into a | |
70 | * pipe and then splice that to a file. | |
71 | */ | |
72 | static int fio_splice_write(struct thread_data *td, struct io_u *io_u) | |
73 | { | |
74 | struct spliceio_data *sd = td->io_ops->data; | |
75 | struct iovec iov[1] = { | |
76 | { | |
cec6b55d JA |
77 | .iov_base = io_u->xfer_buf, |
78 | .iov_len = io_u->xfer_buflen, | |
2866c82d JA |
79 | } |
80 | }; | |
81 | struct pollfd pfd = { .fd = sd->pipe[1], .events = POLLOUT, }; | |
53cdc686 | 82 | struct fio_file *f = io_u->file; |
2866c82d JA |
83 | off_t off = io_u->offset; |
84 | int ret, ret2; | |
85 | ||
86 | while (iov[0].iov_len) { | |
87 | if (poll(&pfd, 1, -1) < 0) | |
88 | return errno; | |
89 | ||
90 | ret = vmsplice(sd->pipe[1], iov, 1, SPLICE_F_NONBLOCK); | |
91 | if (ret < 0) | |
b4ba5f30 | 92 | return -errno; |
2866c82d JA |
93 | |
94 | iov[0].iov_len -= ret; | |
95 | iov[0].iov_base += ret; | |
96 | ||
97 | while (ret) { | |
53cdc686 | 98 | ret2 = splice(sd->pipe[0], NULL, f->fd, &off, ret, 0); |
2866c82d | 99 | if (ret2 < 0) |
b4ba5f30 | 100 | return -errno; |
2866c82d JA |
101 | |
102 | ret -= ret2; | |
103 | } | |
104 | } | |
105 | ||
cec6b55d | 106 | return io_u->xfer_buflen; |
2866c82d JA |
107 | } |
108 | ||
109 | static int fio_spliceio_queue(struct thread_data *td, struct io_u *io_u) | |
110 | { | |
cec6b55d | 111 | int ret; |
2866c82d JA |
112 | |
113 | if (io_u->ddir == DDIR_READ) | |
114 | ret = fio_splice_read(td, io_u); | |
87dc1ab1 | 115 | else if (io_u->ddir == DDIR_WRITE) |
2866c82d | 116 | ret = fio_splice_write(td, io_u); |
87dc1ab1 JA |
117 | else |
118 | ret = fsync(io_u->file->fd); | |
2866c82d | 119 | |
cec6b55d | 120 | if (ret != (int) io_u->xfer_buflen) { |
22819ec2 | 121 | if (ret >= 0) { |
cec6b55d JA |
122 | io_u->resid = io_u->xfer_buflen - ret; |
123 | io_u->error = 0; | |
36167d82 | 124 | return FIO_Q_COMPLETED; |
2866c82d JA |
125 | } else |
126 | io_u->error = errno; | |
127 | } | |
128 | ||
36167d82 | 129 | if (io_u->error) |
e1161c32 | 130 | td_verror(td, io_u->error, "xfer"); |
2866c82d | 131 | |
36167d82 | 132 | return FIO_Q_COMPLETED; |
2866c82d JA |
133 | } |
134 | ||
135 | static void fio_spliceio_cleanup(struct thread_data *td) | |
136 | { | |
137 | struct spliceio_data *sd = td->io_ops->data; | |
138 | ||
139 | if (sd) { | |
140 | close(sd->pipe[0]); | |
141 | close(sd->pipe[1]); | |
142 | free(sd); | |
143 | td->io_ops->data = NULL; | |
144 | } | |
145 | } | |
146 | ||
147 | static int fio_spliceio_init(struct thread_data *td) | |
148 | { | |
149 | struct spliceio_data *sd = malloc(sizeof(*sd)); | |
150 | ||
2866c82d | 151 | if (pipe(sd->pipe) < 0) { |
e1161c32 | 152 | td_verror(td, errno, "pipe"); |
2866c82d JA |
153 | free(sd); |
154 | return 1; | |
155 | } | |
156 | ||
157 | td->io_ops->data = sd; | |
158 | return 0; | |
159 | } | |
160 | ||
5f350952 | 161 | static struct ioengine_ops ioengine = { |
2866c82d JA |
162 | .name = "splice", |
163 | .version = FIO_IOOPS_VERSION, | |
164 | .init = fio_spliceio_init, | |
165 | .queue = fio_spliceio_queue, | |
2866c82d | 166 | .cleanup = fio_spliceio_cleanup, |
b5af8293 JA |
167 | .open_file = generic_open_file, |
168 | .close_file = generic_close_file, | |
2866c82d JA |
169 | .flags = FIO_SYNCIO, |
170 | }; | |
34cfcdaf JA |
171 | |
172 | #else /* FIO_HAVE_SPLICE */ | |
173 | ||
174 | /* | |
175 | * When we have a proper configure system in place, we simply wont build | |
176 | * and install this io engine. For now install a crippled version that | |
177 | * just complains and fails to load. | |
178 | */ | |
179 | static int fio_spliceio_init(struct thread_data fio_unused *td) | |
180 | { | |
181 | fprintf(stderr, "fio: splice not available\n"); | |
182 | return 1; | |
183 | } | |
184 | ||
5f350952 | 185 | static struct ioengine_ops ioengine = { |
34cfcdaf JA |
186 | .name = "splice", |
187 | .version = FIO_IOOPS_VERSION, | |
188 | .init = fio_spliceio_init, | |
189 | }; | |
190 | ||
191 | #endif | |
5f350952 JA |
192 | |
193 | static void fio_init fio_spliceio_register(void) | |
194 | { | |
195 | register_ioengine(&ioengine); | |
196 | } | |
197 | ||
198 | static void fio_exit fio_spliceio_unregister(void) | |
199 | { | |
200 | unregister_ioengine(&ioengine); | |
201 | } |