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.
15 #include "../optgroup.h"
16 #include "../lib/rand.h"
19 * Sync engine uses engine_data to store last offset
21 #define LAST_POS(f) ((f)->engine_pos)
28 unsigned long queued_bytes;
30 unsigned long long last_offset;
31 struct fio_file *last_file;
32 enum fio_ddir last_ddir;
34 struct frand_state rand_state;
37 #ifdef FIO_HAVE_PWRITEV2
38 struct psyncv2_options {
41 unsigned int hipri_percentage;
42 unsigned int uncached;
46 static struct fio_option options[] = {
50 .type = FIO_OPT_STR_SET,
51 .off1 = offsetof(struct psyncv2_options, hipri),
52 .help = "Set RWF_HIPRI for pwritev2/preadv2",
53 .category = FIO_OPT_C_ENGINE,
54 .group = FIO_OPT_G_INVALID,
57 .name = "hipri_percentage",
58 .lname = "RWF_HIPRI_PERCENTAGE",
60 .off1 = offsetof(struct psyncv2_options, hipri_percentage),
64 .help = "Probabilistically set RWF_HIPRI for pwritev2/preadv2",
65 .category = FIO_OPT_C_ENGINE,
66 .group = FIO_OPT_G_INVALID,
72 .off1 = offsetof(struct psyncv2_options, uncached),
73 .help = "Use RWF_UNCACHED for buffered read/writes",
74 .category = FIO_OPT_C_ENGINE,
75 .group = FIO_OPT_G_INVALID,
79 .lname = "RWF_NOWAIT",
81 .off1 = offsetof(struct psyncv2_options, nowait),
82 .help = "Set RWF_NOWAIT for pwritev2/preadv2",
83 .category = FIO_OPT_C_ENGINE,
84 .group = FIO_OPT_G_INVALID,
92 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
94 struct fio_file *f = io_u->file;
96 if (!ddir_rw(io_u->ddir))
99 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
102 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
103 td_verror(td, errno, "lseek");
110 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
112 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
113 LAST_POS(io_u->file) = io_u->offset + ret;
115 if (ret != (int) io_u->xfer_buflen) {
117 io_u->resid = io_u->xfer_buflen - ret;
119 return FIO_Q_COMPLETED;
125 io_u_log_error(td, io_u);
126 td_verror(td, io_u->error, "xfer");
129 return FIO_Q_COMPLETED;
132 #ifdef CONFIG_PWRITEV
133 static enum fio_q_status fio_pvsyncio_queue(struct thread_data *td,
136 struct syncio_data *sd = td->io_ops_data;
137 struct iovec *iov = &sd->iovecs[0];
138 struct fio_file *f = io_u->file;
141 fio_ro_check(td, io_u);
143 iov->iov_base = io_u->xfer_buf;
144 iov->iov_len = io_u->xfer_buflen;
146 if (io_u->ddir == DDIR_READ)
147 ret = preadv(f->fd, iov, 1, io_u->offset);
148 else if (io_u->ddir == DDIR_WRITE)
149 ret = pwritev(f->fd, iov, 1, io_u->offset);
150 else if (io_u->ddir == DDIR_TRIM) {
151 do_io_u_trim(td, io_u);
152 return FIO_Q_COMPLETED;
154 ret = do_io_u_sync(td, io_u);
156 return fio_io_end(td, io_u, ret);
160 #ifdef FIO_HAVE_PWRITEV2
161 static enum fio_q_status fio_pvsyncio2_queue(struct thread_data *td,
164 struct syncio_data *sd = td->io_ops_data;
165 struct psyncv2_options *o = td->eo;
166 struct iovec *iov = &sd->iovecs[0];
167 struct fio_file *f = io_u->file;
170 fio_ro_check(td, io_u);
173 (rand_between(&sd->rand_state, 1, 100) <= o->hipri_percentage))
175 if (!td->o.odirect && o->uncached)
176 flags |= RWF_UNCACHED;
180 iov->iov_base = io_u->xfer_buf;
181 iov->iov_len = io_u->xfer_buflen;
183 if (io_u->ddir == DDIR_READ)
184 ret = preadv2(f->fd, iov, 1, io_u->offset, flags);
185 else if (io_u->ddir == DDIR_WRITE)
186 ret = pwritev2(f->fd, iov, 1, io_u->offset, flags);
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);
197 static enum fio_q_status fio_psyncio_queue(struct thread_data *td,
200 struct fio_file *f = io_u->file;
203 fio_ro_check(td, io_u);
205 if (io_u->ddir == DDIR_READ)
206 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
207 else if (io_u->ddir == DDIR_WRITE)
208 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
209 else if (io_u->ddir == DDIR_TRIM) {
210 do_io_u_trim(td, io_u);
211 return FIO_Q_COMPLETED;
213 ret = do_io_u_sync(td, io_u);
215 return fio_io_end(td, io_u, ret);
218 static enum fio_q_status fio_syncio_queue(struct thread_data *td,
221 struct fio_file *f = io_u->file;
224 fio_ro_check(td, io_u);
226 if (io_u->ddir == DDIR_READ)
227 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
228 else if (io_u->ddir == DDIR_WRITE)
229 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
230 else if (io_u->ddir == DDIR_TRIM) {
231 do_io_u_trim(td, io_u);
232 return FIO_Q_COMPLETED;
234 ret = do_io_u_sync(td, io_u);
236 return fio_io_end(td, io_u, ret);
239 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
241 const struct timespec fio_unused *t)
243 struct syncio_data *sd = td->io_ops_data;
252 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
256 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
258 struct syncio_data *sd = td->io_ops_data;
260 return sd->io_us[event];
263 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
265 struct syncio_data *sd = td->io_ops_data;
267 if (ddir_sync(io_u->ddir))
270 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
271 io_u->ddir == sd->last_ddir)
277 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
280 sd->io_us[idx] = io_u;
281 sd->iovecs[idx].iov_base = io_u->xfer_buf;
282 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
283 sd->last_offset = io_u->offset + io_u->xfer_buflen;
284 sd->last_file = io_u->file;
285 sd->last_ddir = io_u->ddir;
286 sd->queued_bytes += io_u->xfer_buflen;
290 static enum fio_q_status fio_vsyncio_queue(struct thread_data *td,
293 struct syncio_data *sd = td->io_ops_data;
295 fio_ro_check(td, io_u);
297 if (!fio_vsyncio_append(td, io_u)) {
298 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
300 * If we can't append and have stuff queued, tell fio to
301 * commit those first and then retry this io
305 if (ddir_sync(io_u->ddir)) {
306 int ret = do_io_u_sync(td, io_u);
308 return fio_io_end(td, io_u, ret);
312 sd->queued_bytes = 0;
313 fio_vsyncio_set_iov(sd, io_u, 0);
315 if (sd->queued == td->o.iodepth) {
316 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
320 dprint(FD_IO, "vsyncio_queue: append\n");
321 fio_vsyncio_set_iov(sd, io_u, sd->queued);
324 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
329 * Check that we transferred all bytes, or saw an error, etc
331 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
333 struct syncio_data *sd = td->io_ops_data;
339 * transferred everything, perfect
341 if (bytes == sd->queued_bytes)
345 for (i = 0; i < sd->queued; i++) {
351 unsigned int this_io;
354 if (this_io > io_u->xfer_buflen)
355 this_io = io_u->xfer_buflen;
357 io_u->resid = io_u->xfer_buflen - this_io;
364 td_verror(td, err, "xfer vsync");
371 static int fio_vsyncio_commit(struct thread_data *td)
373 struct syncio_data *sd = td->io_ops_data;
380 io_u_mark_submit(td, sd->queued);
383 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
386 td_verror(td, errno, "lseek");
390 if (sd->last_ddir == DDIR_READ)
391 ret = readv(f->fd, sd->iovecs, sd->queued);
393 ret = writev(f->fd, sd->iovecs, sd->queued);
395 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
396 sd->events = sd->queued;
398 return fio_vsyncio_end(td, ret);
401 static int fio_vsyncio_init(struct thread_data *td)
403 struct syncio_data *sd;
405 sd = malloc(sizeof(*sd));
406 memset(sd, 0, sizeof(*sd));
407 sd->last_offset = -1ULL;
408 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
409 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
410 init_rand(&sd->rand_state, 0);
412 td->io_ops_data = sd;
416 static void fio_vsyncio_cleanup(struct thread_data *td)
418 struct syncio_data *sd = td->io_ops_data;
427 static struct ioengine_ops ioengine_rw = {
429 .version = FIO_IOOPS_VERSION,
430 .prep = fio_syncio_prep,
431 .queue = fio_syncio_queue,
432 .open_file = generic_open_file,
433 .close_file = generic_close_file,
434 .get_file_size = generic_get_file_size,
438 static struct ioengine_ops ioengine_prw = {
440 .version = FIO_IOOPS_VERSION,
441 .queue = fio_psyncio_queue,
442 .open_file = generic_open_file,
443 .close_file = generic_close_file,
444 .get_file_size = generic_get_file_size,
448 static struct ioengine_ops ioengine_vrw = {
450 .version = FIO_IOOPS_VERSION,
451 .init = fio_vsyncio_init,
452 .cleanup = fio_vsyncio_cleanup,
453 .queue = fio_vsyncio_queue,
454 .commit = fio_vsyncio_commit,
455 .event = fio_vsyncio_event,
456 .getevents = fio_vsyncio_getevents,
457 .open_file = generic_open_file,
458 .close_file = generic_close_file,
459 .get_file_size = generic_get_file_size,
463 #ifdef CONFIG_PWRITEV
464 static struct ioengine_ops ioengine_pvrw = {
466 .version = FIO_IOOPS_VERSION,
467 .init = fio_vsyncio_init,
468 .cleanup = fio_vsyncio_cleanup,
469 .queue = fio_pvsyncio_queue,
470 .open_file = generic_open_file,
471 .close_file = generic_close_file,
472 .get_file_size = generic_get_file_size,
477 #ifdef FIO_HAVE_PWRITEV2
478 static struct ioengine_ops ioengine_pvrw2 = {
480 .version = FIO_IOOPS_VERSION,
481 .init = fio_vsyncio_init,
482 .cleanup = fio_vsyncio_cleanup,
483 .queue = fio_pvsyncio2_queue,
484 .open_file = generic_open_file,
485 .close_file = generic_close_file,
486 .get_file_size = generic_get_file_size,
489 .option_struct_size = sizeof(struct psyncv2_options),
493 static void fio_init fio_syncio_register(void)
495 register_ioengine(&ioengine_rw);
496 register_ioengine(&ioengine_prw);
497 register_ioengine(&ioengine_vrw);
498 #ifdef CONFIG_PWRITEV
499 register_ioengine(&ioengine_pvrw);
501 #ifdef FIO_HAVE_PWRITEV2
502 register_ioengine(&ioengine_pvrw2);
506 static void fio_exit fio_syncio_unregister(void)
508 unregister_ioengine(&ioengine_rw);
509 unregister_ioengine(&ioengine_prw);
510 unregister_ioengine(&ioengine_vrw);
511 #ifdef CONFIG_PWRITEV
512 unregister_ioengine(&ioengine_pvrw);
514 #ifdef FIO_HAVE_PWRITEV2
515 unregister_ioengine(&ioengine_pvrw2);