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;
44 static struct fio_option options[] = {
48 .type = FIO_OPT_STR_SET,
49 .off1 = offsetof(struct psyncv2_options, hipri),
50 .help = "Set RWF_HIPRI for pwritev2/preadv2",
51 .category = FIO_OPT_C_ENGINE,
52 .group = FIO_OPT_G_INVALID,
55 .name = "hipri_percentage",
56 .lname = "RWF_HIPRI_PERCENTAGE",
58 .off1 = offsetof(struct psyncv2_options, hipri_percentage),
62 .help = "Probabilistically set RWF_HIPRI for pwritev2/preadv2",
63 .category = FIO_OPT_C_ENGINE,
64 .group = FIO_OPT_G_INVALID,
72 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
74 struct fio_file *f = io_u->file;
76 if (!ddir_rw(io_u->ddir))
79 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
82 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
83 td_verror(td, errno, "lseek");
90 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
92 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
93 LAST_POS(io_u->file) = io_u->offset + ret;
95 if (ret != (int) io_u->xfer_buflen) {
97 io_u->resid = io_u->xfer_buflen - ret;
99 return FIO_Q_COMPLETED;
105 io_u_log_error(td, io_u);
106 td_verror(td, io_u->error, "xfer");
109 return FIO_Q_COMPLETED;
112 #ifdef CONFIG_PWRITEV
113 static enum fio_q_status
114 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 enum fio_q_status
142 fio_pvsyncio2_queue(struct thread_data *td, struct io_u *io_u)
144 struct syncio_data *sd = td->io_ops_data;
145 struct psyncv2_options *o = td->eo;
146 struct iovec *iov = &sd->iovecs[0];
147 struct fio_file *f = io_u->file;
150 fio_ro_check(td, io_u);
153 (rand32_between(&sd->rand_state, 1, 100) <= o->hipri_percentage))
156 iov->iov_base = io_u->xfer_buf;
157 iov->iov_len = io_u->xfer_buflen;
159 if (io_u->ddir == DDIR_READ)
160 ret = preadv2(f->fd, iov, 1, io_u->offset, flags);
161 else if (io_u->ddir == DDIR_WRITE)
162 ret = pwritev2(f->fd, iov, 1, io_u->offset, flags);
163 else if (io_u->ddir == DDIR_TRIM) {
164 do_io_u_trim(td, io_u);
165 return FIO_Q_COMPLETED;
167 ret = do_io_u_sync(td, io_u);
169 return fio_io_end(td, io_u, ret);
174 static enum fio_q_status
175 fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
177 struct fio_file *f = io_u->file;
180 fio_ro_check(td, io_u);
182 if (io_u->ddir == DDIR_READ)
183 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
184 else if (io_u->ddir == DDIR_WRITE)
185 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
186 else if (io_u->ddir == DDIR_TRIM) {
187 do_io_u_trim(td, io_u);
188 return FIO_Q_COMPLETED;
190 ret = do_io_u_sync(td, io_u);
192 return fio_io_end(td, io_u, ret);
195 static enum fio_q_status
196 fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
198 struct fio_file *f = io_u->file;
201 fio_ro_check(td, io_u);
203 if (io_u->ddir == DDIR_READ)
204 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
205 else if (io_u->ddir == DDIR_WRITE)
206 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
207 else if (io_u->ddir == DDIR_TRIM) {
208 do_io_u_trim(td, io_u);
209 return FIO_Q_COMPLETED;
211 ret = do_io_u_sync(td, io_u);
213 return fio_io_end(td, io_u, ret);
216 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
218 const struct timespec fio_unused *t)
220 struct syncio_data *sd = td->io_ops_data;
229 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
233 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
235 struct syncio_data *sd = td->io_ops_data;
237 return sd->io_us[event];
240 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
242 struct syncio_data *sd = td->io_ops_data;
244 if (ddir_sync(io_u->ddir))
247 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
248 io_u->ddir == sd->last_ddir)
254 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
257 sd->io_us[idx] = io_u;
258 sd->iovecs[idx].iov_base = io_u->xfer_buf;
259 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
260 sd->last_offset = io_u->offset + io_u->xfer_buflen;
261 sd->last_file = io_u->file;
262 sd->last_ddir = io_u->ddir;
263 sd->queued_bytes += io_u->xfer_buflen;
267 static enum fio_q_status
268 fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
270 struct syncio_data *sd = td->io_ops_data;
272 fio_ro_check(td, io_u);
274 if (!fio_vsyncio_append(td, io_u)) {
275 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
277 * If we can't append and have stuff queued, tell fio to
278 * commit those first and then retry this io
282 if (ddir_sync(io_u->ddir)) {
283 int ret = do_io_u_sync(td, io_u);
285 return fio_io_end(td, io_u, ret);
289 sd->queued_bytes = 0;
290 fio_vsyncio_set_iov(sd, io_u, 0);
292 if (sd->queued == td->o.iodepth) {
293 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
297 dprint(FD_IO, "vsyncio_queue: append\n");
298 fio_vsyncio_set_iov(sd, io_u, sd->queued);
301 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
306 * Check that we transferred all bytes, or saw an error, etc
308 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
310 struct syncio_data *sd = td->io_ops_data;
316 * transferred everything, perfect
318 if (bytes == sd->queued_bytes)
322 for (i = 0; i < sd->queued; i++) {
328 unsigned int this_io;
331 if (this_io > io_u->xfer_buflen)
332 this_io = io_u->xfer_buflen;
334 io_u->resid = io_u->xfer_buflen - this_io;
341 td_verror(td, err, "xfer vsync");
348 static int fio_vsyncio_commit(struct thread_data *td)
350 struct syncio_data *sd = td->io_ops_data;
357 io_u_mark_submit(td, sd->queued);
360 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
363 td_verror(td, errno, "lseek");
367 if (sd->last_ddir == DDIR_READ)
368 ret = readv(f->fd, sd->iovecs, sd->queued);
370 ret = writev(f->fd, sd->iovecs, sd->queued);
372 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
373 sd->events = sd->queued;
375 return fio_vsyncio_end(td, ret);
378 static int fio_vsyncio_init(struct thread_data *td)
380 struct syncio_data *sd;
382 sd = malloc(sizeof(*sd));
383 memset(sd, 0, sizeof(*sd));
384 sd->last_offset = -1ULL;
385 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
386 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
387 init_rand(&sd->rand_state, 0);
389 td->io_ops_data = sd;
393 static void fio_vsyncio_cleanup(struct thread_data *td)
395 struct syncio_data *sd = td->io_ops_data;
404 static struct ioengine_ops ioengine_rw = {
406 .version = FIO_IOOPS_VERSION,
407 .prep = fio_syncio_prep,
408 .queue = fio_syncio_queue,
409 .open_file = generic_open_file,
410 .close_file = generic_close_file,
411 .get_file_size = generic_get_file_size,
415 static struct ioengine_ops ioengine_prw = {
417 .version = FIO_IOOPS_VERSION,
418 .queue = fio_psyncio_queue,
419 .open_file = generic_open_file,
420 .close_file = generic_close_file,
421 .get_file_size = generic_get_file_size,
425 static struct ioengine_ops ioengine_vrw = {
427 .version = FIO_IOOPS_VERSION,
428 .init = fio_vsyncio_init,
429 .cleanup = fio_vsyncio_cleanup,
430 .queue = fio_vsyncio_queue,
431 .commit = fio_vsyncio_commit,
432 .event = fio_vsyncio_event,
433 .getevents = fio_vsyncio_getevents,
434 .open_file = generic_open_file,
435 .close_file = generic_close_file,
436 .get_file_size = generic_get_file_size,
440 #ifdef CONFIG_PWRITEV
441 static struct ioengine_ops ioengine_pvrw = {
443 .version = FIO_IOOPS_VERSION,
444 .init = fio_vsyncio_init,
445 .cleanup = fio_vsyncio_cleanup,
446 .queue = fio_pvsyncio_queue,
447 .open_file = generic_open_file,
448 .close_file = generic_close_file,
449 .get_file_size = generic_get_file_size,
454 #ifdef FIO_HAVE_PWRITEV2
455 static struct ioengine_ops ioengine_pvrw2 = {
457 .version = FIO_IOOPS_VERSION,
458 .init = fio_vsyncio_init,
459 .cleanup = fio_vsyncio_cleanup,
460 .queue = fio_pvsyncio2_queue,
461 .open_file = generic_open_file,
462 .close_file = generic_close_file,
463 .get_file_size = generic_get_file_size,
466 .option_struct_size = sizeof(struct psyncv2_options),
470 static void fio_init fio_syncio_register(void)
472 register_ioengine(&ioengine_rw);
473 register_ioengine(&ioengine_prw);
474 register_ioengine(&ioengine_vrw);
475 #ifdef CONFIG_PWRITEV
476 register_ioengine(&ioengine_pvrw);
478 #ifdef FIO_HAVE_PWRITEV2
479 register_ioengine(&ioengine_pvrw2);
483 static void fio_exit fio_syncio_unregister(void)
485 unregister_ioengine(&ioengine_rw);
486 unregister_ioengine(&ioengine_prw);
487 unregister_ioengine(&ioengine_vrw);
488 #ifdef CONFIG_PWRITEV
489 unregister_ioengine(&ioengine_pvrw);
491 #ifdef FIO_HAVE_PWRITEV2
492 unregister_ioengine(&ioengine_pvrw2);