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;
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,
71 .off1 = offsetof(struct psyncv2_options, uncached),
72 .help = "Use RWF_UNCACHED for buffered read/writes",
73 .category = FIO_OPT_C_ENGINE,
74 .group = FIO_OPT_G_INVALID,
82 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
84 struct fio_file *f = io_u->file;
86 if (!ddir_rw(io_u->ddir))
89 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
92 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
93 td_verror(td, errno, "lseek");
100 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
102 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
103 LAST_POS(io_u->file) = io_u->offset + ret;
105 if (ret != (int) io_u->xfer_buflen) {
107 io_u->resid = io_u->xfer_buflen - ret;
109 return FIO_Q_COMPLETED;
115 io_u_log_error(td, io_u);
116 td_verror(td, io_u->error, "xfer");
119 return FIO_Q_COMPLETED;
122 #ifdef CONFIG_PWRITEV
123 static enum fio_q_status fio_pvsyncio_queue(struct thread_data *td,
126 struct syncio_data *sd = td->io_ops_data;
127 struct iovec *iov = &sd->iovecs[0];
128 struct fio_file *f = io_u->file;
131 fio_ro_check(td, io_u);
133 iov->iov_base = io_u->xfer_buf;
134 iov->iov_len = io_u->xfer_buflen;
136 if (io_u->ddir == DDIR_READ)
137 ret = preadv(f->fd, iov, 1, io_u->offset);
138 else if (io_u->ddir == DDIR_WRITE)
139 ret = pwritev(f->fd, iov, 1, io_u->offset);
140 else if (io_u->ddir == DDIR_TRIM) {
141 do_io_u_trim(td, io_u);
142 return FIO_Q_COMPLETED;
144 ret = do_io_u_sync(td, io_u);
146 return fio_io_end(td, io_u, ret);
150 #ifdef FIO_HAVE_PWRITEV2
151 static enum fio_q_status fio_pvsyncio2_queue(struct thread_data *td,
154 struct syncio_data *sd = td->io_ops_data;
155 struct psyncv2_options *o = td->eo;
156 struct iovec *iov = &sd->iovecs[0];
157 struct fio_file *f = io_u->file;
160 fio_ro_check(td, io_u);
163 (rand_between(&sd->rand_state, 1, 100) <= o->hipri_percentage))
165 if (!td->o.odirect && o->uncached)
166 flags |= RWF_UNCACHED;
168 iov->iov_base = io_u->xfer_buf;
169 iov->iov_len = io_u->xfer_buflen;
171 if (io_u->ddir == DDIR_READ)
172 ret = preadv2(f->fd, iov, 1, io_u->offset, flags);
173 else if (io_u->ddir == DDIR_WRITE)
174 ret = pwritev2(f->fd, iov, 1, io_u->offset, flags);
175 else if (io_u->ddir == DDIR_TRIM) {
176 do_io_u_trim(td, io_u);
177 return FIO_Q_COMPLETED;
179 ret = do_io_u_sync(td, io_u);
181 return fio_io_end(td, io_u, ret);
185 static enum fio_q_status fio_psyncio_queue(struct thread_data *td,
188 struct fio_file *f = io_u->file;
191 fio_ro_check(td, io_u);
193 if (io_u->ddir == DDIR_READ)
194 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
195 else if (io_u->ddir == DDIR_WRITE)
196 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
197 else if (io_u->ddir == DDIR_TRIM) {
198 do_io_u_trim(td, io_u);
199 return FIO_Q_COMPLETED;
201 ret = do_io_u_sync(td, io_u);
203 return fio_io_end(td, io_u, ret);
206 static enum fio_q_status fio_syncio_queue(struct thread_data *td,
209 struct fio_file *f = io_u->file;
212 fio_ro_check(td, io_u);
214 if (io_u->ddir == DDIR_READ)
215 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
216 else if (io_u->ddir == DDIR_WRITE)
217 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
218 else if (io_u->ddir == DDIR_TRIM) {
219 do_io_u_trim(td, io_u);
220 return FIO_Q_COMPLETED;
222 ret = do_io_u_sync(td, io_u);
224 return fio_io_end(td, io_u, ret);
227 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
229 const struct timespec fio_unused *t)
231 struct syncio_data *sd = td->io_ops_data;
240 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
244 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
246 struct syncio_data *sd = td->io_ops_data;
248 return sd->io_us[event];
251 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
253 struct syncio_data *sd = td->io_ops_data;
255 if (ddir_sync(io_u->ddir))
258 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
259 io_u->ddir == sd->last_ddir)
265 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
268 sd->io_us[idx] = io_u;
269 sd->iovecs[idx].iov_base = io_u->xfer_buf;
270 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
271 sd->last_offset = io_u->offset + io_u->xfer_buflen;
272 sd->last_file = io_u->file;
273 sd->last_ddir = io_u->ddir;
274 sd->queued_bytes += io_u->xfer_buflen;
278 static enum fio_q_status fio_vsyncio_queue(struct thread_data *td,
281 struct syncio_data *sd = td->io_ops_data;
283 fio_ro_check(td, io_u);
285 if (!fio_vsyncio_append(td, io_u)) {
286 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
288 * If we can't append and have stuff queued, tell fio to
289 * commit those first and then retry this io
293 if (ddir_sync(io_u->ddir)) {
294 int ret = do_io_u_sync(td, io_u);
296 return fio_io_end(td, io_u, ret);
300 sd->queued_bytes = 0;
301 fio_vsyncio_set_iov(sd, io_u, 0);
303 if (sd->queued == td->o.iodepth) {
304 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
308 dprint(FD_IO, "vsyncio_queue: append\n");
309 fio_vsyncio_set_iov(sd, io_u, sd->queued);
312 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
317 * Check that we transferred all bytes, or saw an error, etc
319 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
321 struct syncio_data *sd = td->io_ops_data;
327 * transferred everything, perfect
329 if (bytes == sd->queued_bytes)
333 for (i = 0; i < sd->queued; i++) {
339 unsigned int this_io;
342 if (this_io > io_u->xfer_buflen)
343 this_io = io_u->xfer_buflen;
345 io_u->resid = io_u->xfer_buflen - this_io;
352 td_verror(td, err, "xfer vsync");
359 static int fio_vsyncio_commit(struct thread_data *td)
361 struct syncio_data *sd = td->io_ops_data;
368 io_u_mark_submit(td, sd->queued);
371 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
374 td_verror(td, errno, "lseek");
378 if (sd->last_ddir == DDIR_READ)
379 ret = readv(f->fd, sd->iovecs, sd->queued);
381 ret = writev(f->fd, sd->iovecs, sd->queued);
383 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
384 sd->events = sd->queued;
386 return fio_vsyncio_end(td, ret);
389 static int fio_vsyncio_init(struct thread_data *td)
391 struct syncio_data *sd;
393 sd = malloc(sizeof(*sd));
394 memset(sd, 0, sizeof(*sd));
395 sd->last_offset = -1ULL;
396 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
397 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
398 init_rand(&sd->rand_state, 0);
400 td->io_ops_data = sd;
404 static void fio_vsyncio_cleanup(struct thread_data *td)
406 struct syncio_data *sd = td->io_ops_data;
415 static struct ioengine_ops ioengine_rw = {
417 .version = FIO_IOOPS_VERSION,
418 .prep = fio_syncio_prep,
419 .queue = fio_syncio_queue,
420 .open_file = generic_open_file,
421 .close_file = generic_close_file,
422 .get_file_size = generic_get_file_size,
426 static struct ioengine_ops ioengine_prw = {
428 .version = FIO_IOOPS_VERSION,
429 .queue = fio_psyncio_queue,
430 .open_file = generic_open_file,
431 .close_file = generic_close_file,
432 .get_file_size = generic_get_file_size,
436 static struct ioengine_ops ioengine_vrw = {
438 .version = FIO_IOOPS_VERSION,
439 .init = fio_vsyncio_init,
440 .cleanup = fio_vsyncio_cleanup,
441 .queue = fio_vsyncio_queue,
442 .commit = fio_vsyncio_commit,
443 .event = fio_vsyncio_event,
444 .getevents = fio_vsyncio_getevents,
445 .open_file = generic_open_file,
446 .close_file = generic_close_file,
447 .get_file_size = generic_get_file_size,
451 #ifdef CONFIG_PWRITEV
452 static struct ioengine_ops ioengine_pvrw = {
454 .version = FIO_IOOPS_VERSION,
455 .init = fio_vsyncio_init,
456 .cleanup = fio_vsyncio_cleanup,
457 .queue = fio_pvsyncio_queue,
458 .open_file = generic_open_file,
459 .close_file = generic_close_file,
460 .get_file_size = generic_get_file_size,
465 #ifdef FIO_HAVE_PWRITEV2
466 static struct ioengine_ops ioengine_pvrw2 = {
468 .version = FIO_IOOPS_VERSION,
469 .init = fio_vsyncio_init,
470 .cleanup = fio_vsyncio_cleanup,
471 .queue = fio_pvsyncio2_queue,
472 .open_file = generic_open_file,
473 .close_file = generic_close_file,
474 .get_file_size = generic_get_file_size,
477 .option_struct_size = sizeof(struct psyncv2_options),
481 static void fio_init fio_syncio_register(void)
483 register_ioengine(&ioengine_rw);
484 register_ioengine(&ioengine_prw);
485 register_ioengine(&ioengine_vrw);
486 #ifdef CONFIG_PWRITEV
487 register_ioengine(&ioengine_pvrw);
489 #ifdef FIO_HAVE_PWRITEV2
490 register_ioengine(&ioengine_pvrw2);
494 static void fio_exit fio_syncio_unregister(void)
496 unregister_ioengine(&ioengine_rw);
497 unregister_ioengine(&ioengine_prw);
498 unregister_ioengine(&ioengine_vrw);
499 #ifdef CONFIG_PWRITEV
500 unregister_ioengine(&ioengine_pvrw);
502 #ifdef FIO_HAVE_PWRITEV2
503 unregister_ioengine(&ioengine_pvrw2);