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"
17 #include "../lib/rand.h"
20 * Sync engine uses engine_data to store last offset
22 #define LAST_POS(f) ((f)->engine_pos)
29 unsigned long queued_bytes;
31 unsigned long long last_offset;
32 struct fio_file *last_file;
33 enum fio_ddir last_ddir;
35 struct frand_state rand_state;
38 #ifdef FIO_HAVE_PWRITEV2
39 struct psyncv2_options {
42 unsigned int hipri_percentage;
45 static struct fio_option options[] = {
49 .type = FIO_OPT_STR_SET,
50 .off1 = offsetof(struct psyncv2_options, hipri),
51 .help = "Set RWF_HIPRI for pwritev2/preadv2",
52 .category = FIO_OPT_C_ENGINE,
53 .group = FIO_OPT_G_INVALID,
56 .name = "hipri_percentage",
57 .lname = "RWF_HIPRI_PERCENTAGE",
59 .off1 = offsetof(struct psyncv2_options, hipri_percentage),
63 .help = "Probabilistically set RWF_HIPRI for pwritev2/preadv2",
64 .category = FIO_OPT_C_ENGINE,
65 .group = FIO_OPT_G_INVALID,
73 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
75 struct fio_file *f = io_u->file;
77 if (!ddir_rw(io_u->ddir))
80 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
83 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
84 td_verror(td, errno, "lseek");
91 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
93 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
94 LAST_POS(io_u->file) = io_u->offset + ret;
96 if (ret != (int) io_u->xfer_buflen) {
98 io_u->resid = io_u->xfer_buflen - ret;
100 return FIO_Q_COMPLETED;
106 io_u_log_error(td, io_u);
107 td_verror(td, io_u->error, "xfer");
110 return FIO_Q_COMPLETED;
113 #ifdef CONFIG_PWRITEV
114 static int fio_pvsyncio_queue(struct thread_data *td, struct io_u *io_u)
116 struct syncio_data *sd = td->io_ops_data;
117 struct iovec *iov = &sd->iovecs[0];
118 struct fio_file *f = io_u->file;
121 fio_ro_check(td, io_u);
123 iov->iov_base = io_u->xfer_buf;
124 iov->iov_len = io_u->xfer_buflen;
126 if (io_u->ddir == DDIR_READ)
127 ret = preadv(f->fd, iov, 1, io_u->offset);
128 else if (io_u->ddir == DDIR_WRITE)
129 ret = pwritev(f->fd, iov, 1, io_u->offset);
130 else if (io_u->ddir == DDIR_TRIM) {
131 do_io_u_trim(td, io_u);
132 return FIO_Q_COMPLETED;
134 ret = do_io_u_sync(td, io_u);
136 return fio_io_end(td, io_u, ret);
140 #ifdef FIO_HAVE_PWRITEV2
141 static int fio_pvsyncio2_queue(struct thread_data *td, struct io_u *io_u)
143 struct syncio_data *sd = td->io_ops_data;
144 struct psyncv2_options *o = td->eo;
145 struct iovec *iov = &sd->iovecs[0];
146 struct fio_file *f = io_u->file;
149 fio_ro_check(td, io_u);
152 (rand32_between(&sd->rand_state, 1, 100) <= o->hipri_percentage))
155 iov->iov_base = io_u->xfer_buf;
156 iov->iov_len = io_u->xfer_buflen;
158 if (io_u->ddir == DDIR_READ)
159 ret = preadv2(f->fd, iov, 1, io_u->offset, flags);
160 else if (io_u->ddir == DDIR_WRITE)
161 ret = pwritev2(f->fd, iov, 1, io_u->offset, flags);
162 else if (io_u->ddir == DDIR_TRIM) {
163 do_io_u_trim(td, io_u);
164 return FIO_Q_COMPLETED;
166 ret = do_io_u_sync(td, io_u);
168 return fio_io_end(td, io_u, ret);
173 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
175 struct fio_file *f = io_u->file;
178 fio_ro_check(td, io_u);
180 if (io_u->ddir == DDIR_READ)
181 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
182 else if (io_u->ddir == DDIR_WRITE)
183 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
184 else if (io_u->ddir == DDIR_TRIM) {
185 do_io_u_trim(td, io_u);
186 return FIO_Q_COMPLETED;
188 ret = do_io_u_sync(td, io_u);
190 return fio_io_end(td, io_u, ret);
193 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
195 struct fio_file *f = io_u->file;
198 fio_ro_check(td, io_u);
200 if (io_u->ddir == DDIR_READ)
201 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
202 else if (io_u->ddir == DDIR_WRITE)
203 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
204 else if (io_u->ddir == DDIR_TRIM) {
205 do_io_u_trim(td, io_u);
206 return FIO_Q_COMPLETED;
208 ret = do_io_u_sync(td, io_u);
210 return fio_io_end(td, io_u, ret);
213 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
215 const struct timespec fio_unused *t)
217 struct syncio_data *sd = td->io_ops_data;
226 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
230 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
232 struct syncio_data *sd = td->io_ops_data;
234 return sd->io_us[event];
237 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
239 struct syncio_data *sd = td->io_ops_data;
241 if (ddir_sync(io_u->ddir))
244 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
245 io_u->ddir == sd->last_ddir)
251 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
254 sd->io_us[idx] = io_u;
255 sd->iovecs[idx].iov_base = io_u->xfer_buf;
256 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
257 sd->last_offset = io_u->offset + io_u->xfer_buflen;
258 sd->last_file = io_u->file;
259 sd->last_ddir = io_u->ddir;
260 sd->queued_bytes += io_u->xfer_buflen;
264 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
266 struct syncio_data *sd = td->io_ops_data;
268 fio_ro_check(td, io_u);
270 if (!fio_vsyncio_append(td, io_u)) {
271 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
273 * If we can't append and have stuff queued, tell fio to
274 * commit those first and then retry this io
278 if (ddir_sync(io_u->ddir)) {
279 int ret = do_io_u_sync(td, io_u);
281 return fio_io_end(td, io_u, ret);
285 sd->queued_bytes = 0;
286 fio_vsyncio_set_iov(sd, io_u, 0);
288 if (sd->queued == td->o.iodepth) {
289 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
293 dprint(FD_IO, "vsyncio_queue: append\n");
294 fio_vsyncio_set_iov(sd, io_u, sd->queued);
297 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
302 * Check that we transferred all bytes, or saw an error, etc
304 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
306 struct syncio_data *sd = td->io_ops_data;
312 * transferred everything, perfect
314 if (bytes == sd->queued_bytes)
318 for (i = 0; i < sd->queued; i++) {
324 unsigned int this_io;
327 if (this_io > io_u->xfer_buflen)
328 this_io = io_u->xfer_buflen;
330 io_u->resid = io_u->xfer_buflen - this_io;
337 td_verror(td, err, "xfer vsync");
344 static int fio_vsyncio_commit(struct thread_data *td)
346 struct syncio_data *sd = td->io_ops_data;
353 io_u_mark_submit(td, sd->queued);
356 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
359 td_verror(td, errno, "lseek");
363 if (sd->last_ddir == DDIR_READ)
364 ret = readv(f->fd, sd->iovecs, sd->queued);
366 ret = writev(f->fd, sd->iovecs, sd->queued);
368 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
369 sd->events = sd->queued;
371 return fio_vsyncio_end(td, ret);
374 static int fio_vsyncio_init(struct thread_data *td)
376 struct syncio_data *sd;
378 sd = malloc(sizeof(*sd));
379 memset(sd, 0, sizeof(*sd));
380 sd->last_offset = -1ULL;
381 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
382 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
383 init_rand(&sd->rand_state, 0);
385 td->io_ops_data = sd;
389 static void fio_vsyncio_cleanup(struct thread_data *td)
391 struct syncio_data *sd = td->io_ops_data;
400 static struct ioengine_ops ioengine_rw = {
402 .version = FIO_IOOPS_VERSION,
403 .prep = fio_syncio_prep,
404 .queue = fio_syncio_queue,
405 .open_file = generic_open_file,
406 .close_file = generic_close_file,
407 .get_file_size = generic_get_file_size,
411 static struct ioengine_ops ioengine_prw = {
413 .version = FIO_IOOPS_VERSION,
414 .queue = fio_psyncio_queue,
415 .open_file = generic_open_file,
416 .close_file = generic_close_file,
417 .get_file_size = generic_get_file_size,
421 static struct ioengine_ops ioengine_vrw = {
423 .version = FIO_IOOPS_VERSION,
424 .init = fio_vsyncio_init,
425 .cleanup = fio_vsyncio_cleanup,
426 .queue = fio_vsyncio_queue,
427 .commit = fio_vsyncio_commit,
428 .event = fio_vsyncio_event,
429 .getevents = fio_vsyncio_getevents,
430 .open_file = generic_open_file,
431 .close_file = generic_close_file,
432 .get_file_size = generic_get_file_size,
436 #ifdef CONFIG_PWRITEV
437 static struct ioengine_ops ioengine_pvrw = {
439 .version = FIO_IOOPS_VERSION,
440 .init = fio_vsyncio_init,
441 .cleanup = fio_vsyncio_cleanup,
442 .queue = fio_pvsyncio_queue,
443 .open_file = generic_open_file,
444 .close_file = generic_close_file,
445 .get_file_size = generic_get_file_size,
450 #ifdef FIO_HAVE_PWRITEV2
451 static struct ioengine_ops ioengine_pvrw2 = {
453 .version = FIO_IOOPS_VERSION,
454 .init = fio_vsyncio_init,
455 .cleanup = fio_vsyncio_cleanup,
456 .queue = fio_pvsyncio2_queue,
457 .open_file = generic_open_file,
458 .close_file = generic_close_file,
459 .get_file_size = generic_get_file_size,
462 .option_struct_size = sizeof(struct psyncv2_options),
466 static void fio_init fio_syncio_register(void)
468 register_ioengine(&ioengine_rw);
469 register_ioengine(&ioengine_prw);
470 register_ioengine(&ioengine_vrw);
471 #ifdef CONFIG_PWRITEV
472 register_ioengine(&ioengine_pvrw);
474 #ifdef FIO_HAVE_PWRITEV2
475 register_ioengine(&ioengine_pvrw2);
479 static void fio_exit fio_syncio_unregister(void)
481 unregister_ioengine(&ioengine_rw);
482 unregister_ioengine(&ioengine_prw);
483 unregister_ioengine(&ioengine_vrw);
484 #ifdef CONFIG_PWRITEV
485 unregister_ioengine(&ioengine_pvrw);
487 #ifdef FIO_HAVE_PWRITEV2
488 unregister_ioengine(&ioengine_pvrw2);