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