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.
18 * Sync engine uses engine_data to store last offset
20 #define LAST_POS(f) ((f)->engine_data)
27 unsigned long queued_bytes;
29 unsigned long long last_offset;
30 struct fio_file *last_file;
31 enum fio_ddir last_ddir;
34 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
36 struct fio_file *f = io_u->file;
38 if (!ddir_rw(io_u->ddir))
41 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
44 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
45 td_verror(td, errno, "lseek");
52 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
54 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
55 LAST_POS(io_u->file) = io_u->offset + ret;
57 if (ret != (int) io_u->xfer_buflen) {
59 io_u->resid = io_u->xfer_buflen - ret;
61 return FIO_Q_COMPLETED;
67 td_verror(td, io_u->error, "xfer");
69 return FIO_Q_COMPLETED;
73 static int fio_pvsyncio_queue(struct thread_data *td, struct io_u *io_u)
75 struct syncio_data *sd = td->io_ops->data;
76 struct iovec *iov = &sd->iovecs[0];
77 struct fio_file *f = io_u->file;
80 fio_ro_check(td, io_u);
82 iov->iov_base = io_u->xfer_buf;
83 iov->iov_len = io_u->xfer_buflen;
85 if (io_u->ddir == DDIR_READ)
86 ret = preadv(f->fd, iov, 1, io_u->offset);
87 else if (io_u->ddir == DDIR_WRITE)
88 ret = pwritev(f->fd, iov, 1, io_u->offset);
89 else if (io_u->ddir == DDIR_TRIM) {
90 do_io_u_trim(td, io_u);
91 return FIO_Q_COMPLETED;
93 ret = do_io_u_sync(td, io_u);
95 return fio_io_end(td, io_u, ret);
99 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
101 struct fio_file *f = io_u->file;
104 fio_ro_check(td, io_u);
106 if (io_u->ddir == DDIR_READ)
107 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
108 else if (io_u->ddir == DDIR_WRITE)
109 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
110 else if (io_u->ddir == DDIR_TRIM) {
111 do_io_u_trim(td, io_u);
112 return FIO_Q_COMPLETED;
114 ret = do_io_u_sync(td, io_u);
116 return fio_io_end(td, io_u, ret);
119 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
121 struct fio_file *f = io_u->file;
124 fio_ro_check(td, io_u);
126 if (io_u->ddir == DDIR_READ)
127 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
128 else if (io_u->ddir == DDIR_WRITE)
129 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
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);
139 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
141 const struct timespec fio_unused *t)
143 struct syncio_data *sd = td->io_ops->data;
152 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
156 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
158 struct syncio_data *sd = td->io_ops->data;
160 return sd->io_us[event];
163 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
165 struct syncio_data *sd = td->io_ops->data;
167 if (ddir_sync(io_u->ddir))
170 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
171 io_u->ddir == sd->last_ddir)
177 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
180 sd->io_us[idx] = io_u;
181 sd->iovecs[idx].iov_base = io_u->xfer_buf;
182 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
183 sd->last_offset = io_u->offset + io_u->xfer_buflen;
184 sd->last_file = io_u->file;
185 sd->last_ddir = io_u->ddir;
186 sd->queued_bytes += io_u->xfer_buflen;
190 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
192 struct syncio_data *sd = td->io_ops->data;
194 fio_ro_check(td, io_u);
196 if (!fio_vsyncio_append(td, io_u)) {
197 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
199 * If we can't append and have stuff queued, tell fio to
200 * commit those first and then retry this io
204 if (ddir_sync(io_u->ddir)) {
205 int ret = do_io_u_sync(td, io_u);
207 return fio_io_end(td, io_u, ret);
211 sd->queued_bytes = 0;
212 fio_vsyncio_set_iov(sd, io_u, 0);
214 if (sd->queued == td->o.iodepth) {
215 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
219 dprint(FD_IO, "vsyncio_queue: append\n");
220 fio_vsyncio_set_iov(sd, io_u, sd->queued);
223 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
228 * Check that we transferred all bytes, or saw an error, etc
230 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
232 struct syncio_data *sd = td->io_ops->data;
238 * transferred everything, perfect
240 if (bytes == sd->queued_bytes)
244 for (i = 0; i < sd->queued; i++) {
250 unsigned int this_io;
253 if (this_io > io_u->xfer_buflen)
254 this_io = io_u->xfer_buflen;
256 io_u->resid = io_u->xfer_buflen - this_io;
263 td_verror(td, err, "xfer vsync");
270 static int fio_vsyncio_commit(struct thread_data *td)
272 struct syncio_data *sd = td->io_ops->data;
279 io_u_mark_submit(td, sd->queued);
282 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
285 td_verror(td, errno, "lseek");
289 if (sd->last_ddir == DDIR_READ)
290 ret = readv(f->fd, sd->iovecs, sd->queued);
292 ret = writev(f->fd, sd->iovecs, sd->queued);
294 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
295 sd->events = sd->queued;
297 return fio_vsyncio_end(td, ret);
300 static int fio_vsyncio_init(struct thread_data *td)
302 struct syncio_data *sd;
304 sd = malloc(sizeof(*sd));
305 memset(sd, 0, sizeof(*sd));
306 sd->last_offset = -1ULL;
307 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
308 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
310 td->io_ops->data = sd;
314 static void fio_vsyncio_cleanup(struct thread_data *td)
316 struct syncio_data *sd = td->io_ops->data;
323 static struct ioengine_ops ioengine_rw = {
325 .version = FIO_IOOPS_VERSION,
326 .prep = fio_syncio_prep,
327 .queue = fio_syncio_queue,
328 .open_file = generic_open_file,
329 .close_file = generic_close_file,
330 .get_file_size = generic_get_file_size,
334 static struct ioengine_ops ioengine_prw = {
336 .version = FIO_IOOPS_VERSION,
337 .queue = fio_psyncio_queue,
338 .open_file = generic_open_file,
339 .close_file = generic_close_file,
340 .get_file_size = generic_get_file_size,
344 static struct ioengine_ops ioengine_vrw = {
346 .version = FIO_IOOPS_VERSION,
347 .init = fio_vsyncio_init,
348 .cleanup = fio_vsyncio_cleanup,
349 .queue = fio_vsyncio_queue,
350 .commit = fio_vsyncio_commit,
351 .event = fio_vsyncio_event,
352 .getevents = fio_vsyncio_getevents,
353 .open_file = generic_open_file,
354 .close_file = generic_close_file,
355 .get_file_size = generic_get_file_size,
359 #ifdef CONFIG_PWRITEV
360 static struct ioengine_ops ioengine_pvrw = {
362 .version = FIO_IOOPS_VERSION,
363 .init = fio_vsyncio_init,
364 .cleanup = fio_vsyncio_cleanup,
365 .queue = fio_pvsyncio_queue,
366 .open_file = generic_open_file,
367 .close_file = generic_close_file,
368 .get_file_size = generic_get_file_size,
373 static void fio_init fio_syncio_register(void)
375 register_ioengine(&ioengine_rw);
376 register_ioengine(&ioengine_prw);
377 register_ioengine(&ioengine_vrw);
378 #ifdef CONFIG_PWRITEV
379 register_ioengine(&ioengine_pvrw);
383 static void fio_exit fio_syncio_unregister(void)
385 unregister_ioengine(&ioengine_rw);
386 unregister_ioengine(&ioengine_prw);
387 unregister_ioengine(&ioengine_vrw);
388 #ifdef CONFIG_PWRITEV
389 unregister_ioengine(&ioengine_pvrw);