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_pos)
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 FIO_HAVE_PWRITEV2
36 struct psyncv2_options {
42 static struct fio_option options[] = {
46 .type = FIO_OPT_STR_SET,
47 .off1 = offsetof(struct psyncv2_options, hipri),
48 .help = "Set RWF_HIPRI for pwritev2/preadv2",
49 .category = FIO_OPT_C_ENGINE,
50 .group = FIO_OPT_G_INVALID,
56 .off1 = offsetof(struct psyncv2_options, stream),
57 .help = "Set expected write life time",
58 .category = FIO_OPT_C_ENGINE,
59 .group = FIO_OPT_G_INVALID,
62 .oval = RWF_WRITE_LIFE_SHORT,
65 .oval = RWF_WRITE_LIFE_MEDIUM,
68 .oval = RWF_WRITE_LIFE_LONG,
71 .oval = RWF_WRITE_LIFE_EXTREME,
81 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
83 struct fio_file *f = io_u->file;
85 if (!ddir_rw(io_u->ddir))
88 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
91 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
92 td_verror(td, errno, "lseek");
99 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
101 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
102 LAST_POS(io_u->file) = io_u->offset + ret;
104 if (ret != (int) io_u->xfer_buflen) {
106 io_u->resid = io_u->xfer_buflen - ret;
108 return FIO_Q_COMPLETED;
114 io_u_log_error(td, io_u);
115 td_verror(td, io_u->error, "xfer");
118 return FIO_Q_COMPLETED;
121 #ifdef CONFIG_PWRITEV
122 static int fio_pvsyncio_queue(struct thread_data *td, struct io_u *io_u)
124 struct syncio_data *sd = td->io_ops_data;
125 struct iovec *iov = &sd->iovecs[0];
126 struct fio_file *f = io_u->file;
129 fio_ro_check(td, io_u);
131 iov->iov_base = io_u->xfer_buf;
132 iov->iov_len = io_u->xfer_buflen;
134 if (io_u->ddir == DDIR_READ)
135 ret = preadv(f->fd, iov, 1, io_u->offset);
136 else if (io_u->ddir == DDIR_WRITE)
137 ret = pwritev(f->fd, iov, 1, io_u->offset);
138 else if (io_u->ddir == DDIR_TRIM) {
139 do_io_u_trim(td, io_u);
140 return FIO_Q_COMPLETED;
142 ret = do_io_u_sync(td, io_u);
144 return fio_io_end(td, io_u, ret);
148 #ifdef FIO_HAVE_PWRITEV2
149 static int fio_pvsyncio2_queue(struct thread_data *td, struct io_u *io_u)
151 struct syncio_data *sd = td->io_ops_data;
152 struct psyncv2_options *o = td->eo;
153 struct iovec *iov = &sd->iovecs[0];
154 struct fio_file *f = io_u->file;
157 fio_ro_check(td, io_u);
164 iov->iov_base = io_u->xfer_buf;
165 iov->iov_len = io_u->xfer_buflen;
167 if (io_u->ddir == DDIR_READ)
168 ret = preadv2(f->fd, iov, 1, io_u->offset, flags);
169 else if (io_u->ddir == DDIR_WRITE)
170 ret = pwritev2(f->fd, iov, 1, io_u->offset, flags);
171 else if (io_u->ddir == DDIR_TRIM) {
172 do_io_u_trim(td, io_u);
173 return FIO_Q_COMPLETED;
175 ret = do_io_u_sync(td, io_u);
177 return fio_io_end(td, io_u, ret);
182 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
184 struct fio_file *f = io_u->file;
187 fio_ro_check(td, io_u);
189 if (io_u->ddir == DDIR_READ)
190 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
191 else if (io_u->ddir == DDIR_WRITE)
192 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
193 else if (io_u->ddir == DDIR_TRIM) {
194 do_io_u_trim(td, io_u);
195 return FIO_Q_COMPLETED;
197 ret = do_io_u_sync(td, io_u);
199 return fio_io_end(td, io_u, ret);
202 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
204 struct fio_file *f = io_u->file;
207 fio_ro_check(td, io_u);
209 if (io_u->ddir == DDIR_READ)
210 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
211 else if (io_u->ddir == DDIR_WRITE)
212 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
213 else if (io_u->ddir == DDIR_TRIM) {
214 do_io_u_trim(td, io_u);
215 return FIO_Q_COMPLETED;
217 ret = do_io_u_sync(td, io_u);
219 return fio_io_end(td, io_u, ret);
222 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
224 const struct timespec fio_unused *t)
226 struct syncio_data *sd = td->io_ops_data;
235 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
239 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
241 struct syncio_data *sd = td->io_ops_data;
243 return sd->io_us[event];
246 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
248 struct syncio_data *sd = td->io_ops_data;
250 if (ddir_sync(io_u->ddir))
253 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
254 io_u->ddir == sd->last_ddir)
260 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
263 sd->io_us[idx] = io_u;
264 sd->iovecs[idx].iov_base = io_u->xfer_buf;
265 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
266 sd->last_offset = io_u->offset + io_u->xfer_buflen;
267 sd->last_file = io_u->file;
268 sd->last_ddir = io_u->ddir;
269 sd->queued_bytes += io_u->xfer_buflen;
273 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
275 struct syncio_data *sd = td->io_ops_data;
277 fio_ro_check(td, io_u);
279 if (!fio_vsyncio_append(td, io_u)) {
280 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
282 * If we can't append and have stuff queued, tell fio to
283 * commit those first and then retry this io
287 if (ddir_sync(io_u->ddir)) {
288 int ret = do_io_u_sync(td, io_u);
290 return fio_io_end(td, io_u, ret);
294 sd->queued_bytes = 0;
295 fio_vsyncio_set_iov(sd, io_u, 0);
297 if (sd->queued == td->o.iodepth) {
298 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
302 dprint(FD_IO, "vsyncio_queue: append\n");
303 fio_vsyncio_set_iov(sd, io_u, sd->queued);
306 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
311 * Check that we transferred all bytes, or saw an error, etc
313 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
315 struct syncio_data *sd = td->io_ops_data;
321 * transferred everything, perfect
323 if (bytes == sd->queued_bytes)
327 for (i = 0; i < sd->queued; i++) {
333 unsigned int this_io;
336 if (this_io > io_u->xfer_buflen)
337 this_io = io_u->xfer_buflen;
339 io_u->resid = io_u->xfer_buflen - this_io;
346 td_verror(td, err, "xfer vsync");
353 static int fio_vsyncio_commit(struct thread_data *td)
355 struct syncio_data *sd = td->io_ops_data;
362 io_u_mark_submit(td, sd->queued);
365 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
368 td_verror(td, errno, "lseek");
372 if (sd->last_ddir == DDIR_READ)
373 ret = readv(f->fd, sd->iovecs, sd->queued);
375 ret = writev(f->fd, sd->iovecs, sd->queued);
377 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
378 sd->events = sd->queued;
380 return fio_vsyncio_end(td, ret);
383 static int fio_vsyncio_init(struct thread_data *td)
385 struct syncio_data *sd;
387 sd = malloc(sizeof(*sd));
388 memset(sd, 0, sizeof(*sd));
389 sd->last_offset = -1ULL;
390 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
391 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
393 td->io_ops_data = sd;
397 static void fio_vsyncio_cleanup(struct thread_data *td)
399 struct syncio_data *sd = td->io_ops_data;
408 static struct ioengine_ops ioengine_rw = {
410 .version = FIO_IOOPS_VERSION,
411 .prep = fio_syncio_prep,
412 .queue = fio_syncio_queue,
413 .open_file = generic_open_file,
414 .close_file = generic_close_file,
415 .get_file_size = generic_get_file_size,
419 static struct ioengine_ops ioengine_prw = {
421 .version = FIO_IOOPS_VERSION,
422 .queue = fio_psyncio_queue,
423 .open_file = generic_open_file,
424 .close_file = generic_close_file,
425 .get_file_size = generic_get_file_size,
429 static struct ioengine_ops ioengine_vrw = {
431 .version = FIO_IOOPS_VERSION,
432 .init = fio_vsyncio_init,
433 .cleanup = fio_vsyncio_cleanup,
434 .queue = fio_vsyncio_queue,
435 .commit = fio_vsyncio_commit,
436 .event = fio_vsyncio_event,
437 .getevents = fio_vsyncio_getevents,
438 .open_file = generic_open_file,
439 .close_file = generic_close_file,
440 .get_file_size = generic_get_file_size,
444 #ifdef CONFIG_PWRITEV
445 static struct ioengine_ops ioengine_pvrw = {
447 .version = FIO_IOOPS_VERSION,
448 .init = fio_vsyncio_init,
449 .cleanup = fio_vsyncio_cleanup,
450 .queue = fio_pvsyncio_queue,
451 .open_file = generic_open_file,
452 .close_file = generic_close_file,
453 .get_file_size = generic_get_file_size,
458 #ifdef FIO_HAVE_PWRITEV2
459 static struct ioengine_ops ioengine_pvrw2 = {
461 .version = FIO_IOOPS_VERSION,
462 .init = fio_vsyncio_init,
463 .cleanup = fio_vsyncio_cleanup,
464 .queue = fio_pvsyncio2_queue,
465 .open_file = generic_open_file,
466 .close_file = generic_close_file,
467 .get_file_size = generic_get_file_size,
470 .option_struct_size = sizeof(struct psyncv2_options),
474 static void fio_init fio_syncio_register(void)
476 register_ioengine(&ioengine_rw);
477 register_ioengine(&ioengine_prw);
478 register_ioengine(&ioengine_vrw);
479 #ifdef CONFIG_PWRITEV
480 register_ioengine(&ioengine_pvrw);
482 #ifdef FIO_HAVE_PWRITEV2
483 register_ioengine(&ioengine_pvrw2);
487 static void fio_exit fio_syncio_unregister(void)
489 unregister_ioengine(&ioengine_rw);
490 unregister_ioengine(&ioengine_prw);
491 unregister_ioengine(&ioengine_vrw);
492 #ifdef CONFIG_PWRITEV
493 unregister_ioengine(&ioengine_pvrw);
495 #ifdef FIO_HAVE_PWRITEV2
496 unregister_ioengine(&ioengine_pvrw2);