4 * IO engine that does regular read(2)/write(2) with lseek(2) to transfer
5 * data and IO engine that does regular pread(2)/pwrite(2) to transfer data.
16 #include "../optgroup.h"
19 * Sync engine uses engine_data to store last offset
21 #define LAST_POS(f) ((f)->engine_data)
28 unsigned long queued_bytes;
30 unsigned long long last_offset;
31 struct fio_file *last_file;
32 enum fio_ddir last_ddir;
35 #ifdef CONFIG_PWRITEV2
36 struct psyncv2_options {
41 static struct fio_option options[] = {
45 .type = FIO_OPT_STR_SET,
46 .off1 = offsetof(struct psyncv2_options, hipri),
47 .help = "Set RWF_HIPRI for pwritev2/preadv2",
48 .category = FIO_OPT_C_ENGINE,
49 .group = FIO_OPT_G_INVALID,
57 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
59 struct fio_file *f = io_u->file;
61 if (!ddir_rw(io_u->ddir))
64 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
67 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
68 td_verror(td, errno, "lseek");
75 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
77 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
78 LAST_POS(io_u->file) = io_u->offset + ret;
80 if (ret != (int) io_u->xfer_buflen) {
82 io_u->resid = io_u->xfer_buflen - ret;
84 return FIO_Q_COMPLETED;
90 io_u_log_error(td, io_u);
91 td_verror(td, io_u->error, "xfer");
94 return FIO_Q_COMPLETED;
98 static int fio_pvsyncio_queue(struct thread_data *td, struct io_u *io_u)
100 struct syncio_data *sd = td->io_ops->data;
101 struct iovec *iov = &sd->iovecs[0];
102 struct fio_file *f = io_u->file;
105 fio_ro_check(td, io_u);
107 iov->iov_base = io_u->xfer_buf;
108 iov->iov_len = io_u->xfer_buflen;
110 if (io_u->ddir == DDIR_READ)
111 ret = preadv(f->fd, iov, 1, io_u->offset);
112 else if (io_u->ddir == DDIR_WRITE)
113 ret = pwritev(f->fd, iov, 1, io_u->offset);
114 else if (io_u->ddir == DDIR_TRIM) {
115 do_io_u_trim(td, io_u);
116 return FIO_Q_COMPLETED;
118 ret = do_io_u_sync(td, io_u);
120 return fio_io_end(td, io_u, ret);
124 #ifdef CONFIG_PWRITEV2
125 static int fio_pvsyncio2_queue(struct thread_data *td, struct io_u *io_u)
127 struct syncio_data *sd = td->io_ops->data;
128 struct psyncv2_options *o = td->eo;
129 struct iovec *iov = &sd->iovecs[0];
130 struct fio_file *f = io_u->file;
133 fio_ro_check(td, io_u);
138 iov->iov_base = io_u->xfer_buf;
139 iov->iov_len = io_u->xfer_buflen;
141 if (io_u->ddir == DDIR_READ)
142 ret = preadv2(f->fd, iov, 1, io_u->offset, flags);
143 else if (io_u->ddir == DDIR_WRITE)
144 ret = pwritev2(f->fd, iov, 1, io_u->offset, flags);
145 else if (io_u->ddir == DDIR_TRIM) {
146 do_io_u_trim(td, io_u);
147 return FIO_Q_COMPLETED;
149 ret = do_io_u_sync(td, io_u);
151 return fio_io_end(td, io_u, ret);
156 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
158 struct fio_file *f = io_u->file;
161 fio_ro_check(td, io_u);
163 if (io_u->ddir == DDIR_READ)
164 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
165 else if (io_u->ddir == DDIR_WRITE)
166 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
167 else if (io_u->ddir == DDIR_TRIM) {
168 do_io_u_trim(td, io_u);
169 return FIO_Q_COMPLETED;
171 ret = do_io_u_sync(td, io_u);
173 return fio_io_end(td, io_u, ret);
176 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
178 struct fio_file *f = io_u->file;
181 fio_ro_check(td, io_u);
183 if (io_u->ddir == DDIR_READ)
184 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
185 else if (io_u->ddir == DDIR_WRITE)
186 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
187 else if (io_u->ddir == DDIR_TRIM) {
188 do_io_u_trim(td, io_u);
189 return FIO_Q_COMPLETED;
191 ret = do_io_u_sync(td, io_u);
193 return fio_io_end(td, io_u, ret);
196 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
198 const struct timespec fio_unused *t)
200 struct syncio_data *sd = td->io_ops->data;
209 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
213 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
215 struct syncio_data *sd = td->io_ops->data;
217 return sd->io_us[event];
220 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
222 struct syncio_data *sd = td->io_ops->data;
224 if (ddir_sync(io_u->ddir))
227 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
228 io_u->ddir == sd->last_ddir)
234 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
237 sd->io_us[idx] = io_u;
238 sd->iovecs[idx].iov_base = io_u->xfer_buf;
239 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
240 sd->last_offset = io_u->offset + io_u->xfer_buflen;
241 sd->last_file = io_u->file;
242 sd->last_ddir = io_u->ddir;
243 sd->queued_bytes += io_u->xfer_buflen;
247 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
249 struct syncio_data *sd = td->io_ops->data;
251 fio_ro_check(td, io_u);
253 if (!fio_vsyncio_append(td, io_u)) {
254 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
256 * If we can't append and have stuff queued, tell fio to
257 * commit those first and then retry this io
261 if (ddir_sync(io_u->ddir)) {
262 int ret = do_io_u_sync(td, io_u);
264 return fio_io_end(td, io_u, ret);
268 sd->queued_bytes = 0;
269 fio_vsyncio_set_iov(sd, io_u, 0);
271 if (sd->queued == td->o.iodepth) {
272 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
276 dprint(FD_IO, "vsyncio_queue: append\n");
277 fio_vsyncio_set_iov(sd, io_u, sd->queued);
280 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
285 * Check that we transferred all bytes, or saw an error, etc
287 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
289 struct syncio_data *sd = td->io_ops->data;
295 * transferred everything, perfect
297 if (bytes == sd->queued_bytes)
301 for (i = 0; i < sd->queued; i++) {
307 unsigned int this_io;
310 if (this_io > io_u->xfer_buflen)
311 this_io = io_u->xfer_buflen;
313 io_u->resid = io_u->xfer_buflen - this_io;
320 td_verror(td, err, "xfer vsync");
327 static int fio_vsyncio_commit(struct thread_data *td)
329 struct syncio_data *sd = td->io_ops->data;
336 io_u_mark_submit(td, sd->queued);
339 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
342 td_verror(td, errno, "lseek");
346 if (sd->last_ddir == DDIR_READ)
347 ret = readv(f->fd, sd->iovecs, sd->queued);
349 ret = writev(f->fd, sd->iovecs, sd->queued);
351 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
352 sd->events = sd->queued;
354 return fio_vsyncio_end(td, ret);
357 static int fio_vsyncio_init(struct thread_data *td)
359 struct syncio_data *sd;
361 sd = malloc(sizeof(*sd));
362 memset(sd, 0, sizeof(*sd));
363 sd->last_offset = -1ULL;
364 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
365 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
367 td->io_ops->data = sd;
371 static void fio_vsyncio_cleanup(struct thread_data *td)
373 struct syncio_data *sd = td->io_ops->data;
382 static struct ioengine_ops ioengine_rw = {
384 .version = FIO_IOOPS_VERSION,
385 .prep = fio_syncio_prep,
386 .queue = fio_syncio_queue,
387 .open_file = generic_open_file,
388 .close_file = generic_close_file,
389 .get_file_size = generic_get_file_size,
393 static struct ioengine_ops ioengine_prw = {
395 .version = FIO_IOOPS_VERSION,
396 .queue = fio_psyncio_queue,
397 .open_file = generic_open_file,
398 .close_file = generic_close_file,
399 .get_file_size = generic_get_file_size,
403 static struct ioengine_ops ioengine_vrw = {
405 .version = FIO_IOOPS_VERSION,
406 .init = fio_vsyncio_init,
407 .cleanup = fio_vsyncio_cleanup,
408 .queue = fio_vsyncio_queue,
409 .commit = fio_vsyncio_commit,
410 .event = fio_vsyncio_event,
411 .getevents = fio_vsyncio_getevents,
412 .open_file = generic_open_file,
413 .close_file = generic_close_file,
414 .get_file_size = generic_get_file_size,
418 #ifdef CONFIG_PWRITEV
419 static struct ioengine_ops ioengine_pvrw = {
421 .version = FIO_IOOPS_VERSION,
422 .init = fio_vsyncio_init,
423 .cleanup = fio_vsyncio_cleanup,
424 .queue = fio_pvsyncio_queue,
425 .open_file = generic_open_file,
426 .close_file = generic_close_file,
427 .get_file_size = generic_get_file_size,
432 #ifdef CONFIG_PWRITEV2
433 static struct ioengine_ops ioengine_pvrw2 = {
435 .version = FIO_IOOPS_VERSION,
436 .init = fio_vsyncio_init,
437 .cleanup = fio_vsyncio_cleanup,
438 .queue = fio_pvsyncio2_queue,
439 .open_file = generic_open_file,
440 .close_file = generic_close_file,
441 .get_file_size = generic_get_file_size,
444 .option_struct_size = sizeof(struct psyncv2_options),
448 static void fio_init fio_syncio_register(void)
450 register_ioengine(&ioengine_rw);
451 register_ioengine(&ioengine_prw);
452 register_ioengine(&ioengine_vrw);
453 #ifdef CONFIG_PWRITEV
454 register_ioengine(&ioengine_pvrw);
456 #ifdef CONFIG_PWRITEV2
457 register_ioengine(&ioengine_pvrw2);
461 static void fio_exit fio_syncio_unregister(void)
463 unregister_ioengine(&ioengine_rw);
464 unregister_ioengine(&ioengine_prw);
465 unregister_ioengine(&ioengine_vrw);
466 #ifdef CONFIG_PWRITEV
467 unregister_ioengine(&ioengine_pvrw);
469 #ifdef CONFIG_PWRITEV2
470 unregister_ioengine(&ioengine_pvrw2);