Commit | Line | Data |
---|---|---|
2866c82d | 1 | /* |
a31041ea | 2 | * sync/psync engine |
da751ca9 JA |
3 | * |
4 | * IO engine that does regular read(2)/write(2) with lseek(2) to transfer | |
a31041ea | 5 | * data and IO engine that does regular pread(2)/pwrite(2) to transfer data. |
2866c82d JA |
6 | * |
7 | */ | |
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <unistd.h> | |
5921e80c | 11 | #include <sys/uio.h> |
2866c82d JA |
12 | #include <errno.h> |
13 | #include <assert.h> | |
5f350952 JA |
14 | |
15 | #include "../fio.h" | |
2866c82d | 16 | |
1d2af02a JA |
17 | struct syncio_data { |
18 | struct iovec *iovecs; | |
19 | struct io_u **io_us; | |
20 | unsigned int queued; | |
e51cf72c | 21 | unsigned int events; |
1d2af02a JA |
22 | unsigned long queued_bytes; |
23 | ||
24 | unsigned long long last_offset; | |
25 | struct fio_file *last_file; | |
26 | enum fio_ddir last_ddir; | |
27 | }; | |
28 | ||
2866c82d JA |
29 | static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u) |
30 | { | |
53cdc686 JA |
31 | struct fio_file *f = io_u->file; |
32 | ||
5f9099ea | 33 | if (ddir_sync(io_u->ddir)) |
87dc1ab1 JA |
34 | return 0; |
35 | ||
e943b878 JA |
36 | if (f->file_pos != -1ULL && f->file_pos == io_u->offset) |
37 | return 0; | |
38 | ||
53cdc686 | 39 | if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) { |
e1161c32 | 40 | td_verror(td, errno, "lseek"); |
2866c82d JA |
41 | return 1; |
42 | } | |
43 | ||
44 | return 0; | |
45 | } | |
46 | ||
2bd3eabc | 47 | static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret) |
2866c82d | 48 | { |
e943b878 JA |
49 | if (io_u->file && ret >= 0) |
50 | io_u->file->file_pos = io_u->offset + ret; | |
51 | ||
cec6b55d | 52 | if (ret != (int) io_u->xfer_buflen) { |
22819ec2 | 53 | if (ret >= 0) { |
cec6b55d JA |
54 | io_u->resid = io_u->xfer_buflen - ret; |
55 | io_u->error = 0; | |
36167d82 | 56 | return FIO_Q_COMPLETED; |
2866c82d JA |
57 | } else |
58 | io_u->error = errno; | |
59 | } | |
60 | ||
36167d82 | 61 | if (io_u->error) |
e1161c32 | 62 | td_verror(td, io_u->error, "xfer"); |
2866c82d | 63 | |
36167d82 | 64 | return FIO_Q_COMPLETED; |
2866c82d JA |
65 | } |
66 | ||
44f29692 JA |
67 | static int fio_syncio_sync(struct thread_data *td, struct io_u *io_u) |
68 | { | |
69 | int ret; | |
70 | ||
71 | if (io_u->ddir == DDIR_SYNC) { | |
72 | ret = fsync(io_u->file->fd); | |
73 | } else if (io_u->ddir == DDIR_DATASYNC) { | |
74 | #ifdef FIO_HAVE_FDATASYNC | |
75 | ret = fdatasync(io_u->file->fd); | |
76 | #else | |
77 | ret = io_u->xfer_buflen; | |
78 | io_u->error = EINVAL; | |
79 | #endif | |
80 | } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE) | |
81 | ret = do_sync_file_range(td, io_u->file); | |
82 | else { | |
83 | ret = io_u->xfer_buflen; | |
84 | io_u->error = EINVAL; | |
85 | } | |
86 | ||
87 | return fio_io_end(td, io_u, ret); | |
88 | } | |
89 | ||
2bd3eabc | 90 | static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u) |
a31041ea | 91 | { |
2bd3eabc JA |
92 | struct fio_file *f = io_u->file; |
93 | int ret; | |
94 | ||
95 | fio_ro_check(td, io_u); | |
96 | ||
97 | if (io_u->ddir == DDIR_READ) | |
98 | ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); | |
99 | else if (io_u->ddir == DDIR_WRITE) | |
100 | ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); | |
101 | else | |
44f29692 | 102 | return fio_syncio_sync(td, io_u); |
2bd3eabc JA |
103 | |
104 | return fio_io_end(td, io_u, ret); | |
105 | } | |
106 | ||
107 | static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u) | |
108 | { | |
109 | struct fio_file *f = io_u->file; | |
110 | int ret; | |
111 | ||
112 | fio_ro_check(td, io_u); | |
113 | ||
114 | if (io_u->ddir == DDIR_READ) | |
115 | ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen); | |
116 | else if (io_u->ddir == DDIR_WRITE) | |
117 | ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen); | |
118 | else | |
44f29692 | 119 | return fio_syncio_sync(td, io_u); |
2bd3eabc JA |
120 | |
121 | return fio_io_end(td, io_u, ret); | |
a31041ea | 122 | } |
123 | ||
1d2af02a JA |
124 | static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min, |
125 | unsigned int max, | |
126 | struct timespec fio_unused *t) | |
127 | { | |
128 | struct syncio_data *sd = td->io_ops->data; | |
129 | int ret; | |
130 | ||
131 | if (min) { | |
e51cf72c JA |
132 | ret = sd->events; |
133 | sd->events = 0; | |
1d2af02a JA |
134 | } else |
135 | ret = 0; | |
136 | ||
137 | dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret); | |
138 | return ret; | |
139 | } | |
140 | ||
141 | static struct io_u *fio_vsyncio_event(struct thread_data *td, int event) | |
142 | { | |
143 | struct syncio_data *sd = td->io_ops->data; | |
144 | ||
145 | return sd->io_us[event]; | |
146 | } | |
147 | ||
148 | static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u) | |
149 | { | |
150 | struct syncio_data *sd = td->io_ops->data; | |
151 | ||
5f9099ea | 152 | if (ddir_sync(io_u->ddir)) |
1d2af02a JA |
153 | return 0; |
154 | ||
155 | if (io_u->offset == sd->last_offset && io_u->file == sd->last_file && | |
156 | io_u->ddir == sd->last_ddir) | |
157 | return 1; | |
158 | ||
159 | return 0; | |
160 | } | |
161 | ||
162 | static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u, | |
163 | int index) | |
164 | { | |
165 | sd->io_us[index] = io_u; | |
166 | sd->iovecs[index].iov_base = io_u->xfer_buf; | |
167 | sd->iovecs[index].iov_len = io_u->xfer_buflen; | |
168 | sd->last_offset = io_u->offset + io_u->xfer_buflen; | |
169 | sd->last_file = io_u->file; | |
170 | sd->last_ddir = io_u->ddir; | |
171 | sd->queued_bytes += io_u->xfer_buflen; | |
172 | sd->queued++; | |
173 | } | |
174 | ||
175 | static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u) | |
176 | { | |
177 | struct syncio_data *sd = td->io_ops->data; | |
178 | ||
179 | fio_ro_check(td, io_u); | |
180 | ||
181 | if (!fio_vsyncio_append(td, io_u)) { | |
182 | dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued); | |
183 | /* | |
184 | * If we can't append and have stuff queued, tell fio to | |
185 | * commit those first and then retry this io | |
186 | */ | |
187 | if (sd->queued) | |
188 | return FIO_Q_BUSY; | |
44f29692 JA |
189 | if (ddir_sync(io_u->ddir)) |
190 | return fio_syncio_sync(td, io_u); | |
cc9159c3 | 191 | |
1d2af02a JA |
192 | sd->queued = 0; |
193 | sd->queued_bytes = 0; | |
194 | fio_vsyncio_set_iov(sd, io_u, 0); | |
195 | } else { | |
196 | if (sd->queued == td->o.iodepth) { | |
197 | dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued); | |
198 | return FIO_Q_BUSY; | |
199 | } | |
200 | ||
201 | dprint(FD_IO, "vsyncio_queue: append\n"); | |
202 | fio_vsyncio_set_iov(sd, io_u, sd->queued); | |
203 | } | |
204 | ||
205 | dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued); | |
206 | return FIO_Q_QUEUED; | |
207 | } | |
208 | ||
209 | /* | |
210 | * Check that we transferred all bytes, or saw an error, etc | |
211 | */ | |
212 | static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes) | |
213 | { | |
214 | struct syncio_data *sd = td->io_ops->data; | |
215 | struct io_u *io_u; | |
216 | unsigned int i; | |
217 | int err; | |
218 | ||
219 | /* | |
220 | * transferred everything, perfect | |
221 | */ | |
222 | if (bytes == sd->queued_bytes) | |
223 | return 0; | |
224 | ||
225 | err = errno; | |
226 | for (i = 0; i < sd->queued; i++) { | |
227 | io_u = sd->io_us[i]; | |
228 | ||
229 | if (bytes == -1) { | |
230 | io_u->error = err; | |
231 | } else { | |
232 | unsigned int this_io; | |
233 | ||
234 | this_io = bytes; | |
235 | if (this_io > io_u->xfer_buflen) | |
236 | this_io = io_u->xfer_buflen; | |
237 | ||
238 | io_u->resid = io_u->xfer_buflen - this_io; | |
239 | io_u->error = 0; | |
240 | bytes -= this_io; | |
241 | } | |
242 | } | |
243 | ||
244 | if (bytes == -1) { | |
245 | td_verror(td, err, "xfer vsync"); | |
246 | return -err; | |
247 | } | |
248 | ||
249 | return 0; | |
250 | } | |
251 | ||
252 | static int fio_vsyncio_commit(struct thread_data *td) | |
253 | { | |
254 | struct syncio_data *sd = td->io_ops->data; | |
255 | struct fio_file *f; | |
256 | ssize_t ret; | |
257 | ||
258 | if (!sd->queued) | |
259 | return 0; | |
260 | ||
838bc709 | 261 | io_u_mark_submit(td, sd->queued); |
1d2af02a JA |
262 | f = sd->last_file; |
263 | ||
264 | if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) { | |
265 | int err = -errno; | |
266 | ||
267 | td_verror(td, errno, "lseek"); | |
268 | return err; | |
269 | } | |
270 | ||
271 | if (sd->last_ddir == DDIR_READ) | |
272 | ret = readv(f->fd, sd->iovecs, sd->queued); | |
273 | else | |
274 | ret = writev(f->fd, sd->iovecs, sd->queued); | |
275 | ||
276 | dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret); | |
e51cf72c JA |
277 | sd->events = sd->queued; |
278 | sd->queued = 0; | |
1d2af02a JA |
279 | return fio_vsyncio_end(td, ret); |
280 | } | |
281 | ||
282 | static int fio_vsyncio_init(struct thread_data *td) | |
283 | { | |
284 | struct syncio_data *sd; | |
285 | ||
286 | sd = malloc(sizeof(*sd)); | |
287 | memset(sd, 0, sizeof(*sd)); | |
288 | sd->last_offset = -1ULL; | |
289 | sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec)); | |
290 | sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); | |
291 | ||
292 | td->io_ops->data = sd; | |
293 | return 0; | |
294 | } | |
295 | ||
296 | static void fio_vsyncio_cleanup(struct thread_data *td) | |
297 | { | |
298 | struct syncio_data *sd = td->io_ops->data; | |
299 | ||
300 | free(sd->iovecs); | |
301 | free(sd->io_us); | |
302 | free(sd); | |
1d2af02a JA |
303 | } |
304 | ||
a31041ea | 305 | static struct ioengine_ops ioengine_rw = { |
2866c82d JA |
306 | .name = "sync", |
307 | .version = FIO_IOOPS_VERSION, | |
2866c82d JA |
308 | .prep = fio_syncio_prep, |
309 | .queue = fio_syncio_queue, | |
b5af8293 JA |
310 | .open_file = generic_open_file, |
311 | .close_file = generic_close_file, | |
df9c26b1 | 312 | .get_file_size = generic_get_file_size, |
2866c82d JA |
313 | .flags = FIO_SYNCIO, |
314 | }; | |
5f350952 | 315 | |
a31041ea | 316 | static struct ioengine_ops ioengine_prw = { |
317 | .name = "psync", | |
318 | .version = FIO_IOOPS_VERSION, | |
2bd3eabc | 319 | .queue = fio_psyncio_queue, |
a31041ea | 320 | .open_file = generic_open_file, |
321 | .close_file = generic_close_file, | |
df9c26b1 | 322 | .get_file_size = generic_get_file_size, |
a31041ea | 323 | .flags = FIO_SYNCIO, |
324 | }; | |
325 | ||
1d2af02a JA |
326 | static struct ioengine_ops ioengine_vrw = { |
327 | .name = "vsync", | |
328 | .version = FIO_IOOPS_VERSION, | |
329 | .init = fio_vsyncio_init, | |
330 | .cleanup = fio_vsyncio_cleanup, | |
331 | .queue = fio_vsyncio_queue, | |
332 | .commit = fio_vsyncio_commit, | |
333 | .event = fio_vsyncio_event, | |
334 | .getevents = fio_vsyncio_getevents, | |
335 | .open_file = generic_open_file, | |
336 | .close_file = generic_close_file, | |
df9c26b1 | 337 | .get_file_size = generic_get_file_size, |
1d2af02a JA |
338 | .flags = FIO_SYNCIO, |
339 | }; | |
340 | ||
5f350952 JA |
341 | static void fio_init fio_syncio_register(void) |
342 | { | |
a31041ea | 343 | register_ioengine(&ioengine_rw); |
344 | register_ioengine(&ioengine_prw); | |
1d2af02a | 345 | register_ioengine(&ioengine_vrw); |
5f350952 JA |
346 | } |
347 | ||
348 | static void fio_exit fio_syncio_unregister(void) | |
349 | { | |
a31041ea | 350 | unregister_ioengine(&ioengine_rw); |
351 | unregister_ioengine(&ioengine_prw); | |
1d2af02a | 352 | unregister_ioengine(&ioengine_vrw); |
5f350952 | 353 | } |