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;
72 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
74 struct fio_file *f = io_u->file;
77 fio_ro_check(td, io_u);
79 if (io_u->ddir == DDIR_READ)
80 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
81 else if (io_u->ddir == DDIR_WRITE)
82 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
83 else if (io_u->ddir == DDIR_TRIM) {
84 do_io_u_trim(td, io_u);
85 return FIO_Q_COMPLETED;
87 ret = do_io_u_sync(td, io_u);
89 return fio_io_end(td, io_u, ret);
92 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
94 struct fio_file *f = io_u->file;
97 fio_ro_check(td, io_u);
99 if (io_u->ddir == DDIR_READ)
100 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
101 else if (io_u->ddir == DDIR_WRITE)
102 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
103 else if (io_u->ddir == DDIR_TRIM) {
104 do_io_u_trim(td, io_u);
105 return FIO_Q_COMPLETED;
107 ret = do_io_u_sync(td, io_u);
109 return fio_io_end(td, io_u, ret);
112 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
114 struct timespec fio_unused *t)
116 struct syncio_data *sd = td->io_ops->data;
125 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
129 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
131 struct syncio_data *sd = td->io_ops->data;
133 return sd->io_us[event];
136 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
138 struct syncio_data *sd = td->io_ops->data;
140 if (ddir_sync(io_u->ddir))
143 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
144 io_u->ddir == sd->last_ddir)
150 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
153 sd->io_us[idx] = io_u;
154 sd->iovecs[idx].iov_base = io_u->xfer_buf;
155 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
156 sd->last_offset = io_u->offset + io_u->xfer_buflen;
157 sd->last_file = io_u->file;
158 sd->last_ddir = io_u->ddir;
159 sd->queued_bytes += io_u->xfer_buflen;
163 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
165 struct syncio_data *sd = td->io_ops->data;
167 fio_ro_check(td, io_u);
169 if (!fio_vsyncio_append(td, io_u)) {
170 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
172 * If we can't append and have stuff queued, tell fio to
173 * commit those first and then retry this io
177 if (ddir_sync(io_u->ddir)) {
178 int ret = do_io_u_sync(td, io_u);
180 return fio_io_end(td, io_u, ret);
184 sd->queued_bytes = 0;
185 fio_vsyncio_set_iov(sd, io_u, 0);
187 if (sd->queued == td->o.iodepth) {
188 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
192 dprint(FD_IO, "vsyncio_queue: append\n");
193 fio_vsyncio_set_iov(sd, io_u, sd->queued);
196 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
201 * Check that we transferred all bytes, or saw an error, etc
203 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
205 struct syncio_data *sd = td->io_ops->data;
211 * transferred everything, perfect
213 if (bytes == sd->queued_bytes)
217 for (i = 0; i < sd->queued; i++) {
223 unsigned int this_io;
226 if (this_io > io_u->xfer_buflen)
227 this_io = io_u->xfer_buflen;
229 io_u->resid = io_u->xfer_buflen - this_io;
236 td_verror(td, err, "xfer vsync");
243 static int fio_vsyncio_commit(struct thread_data *td)
245 struct syncio_data *sd = td->io_ops->data;
252 io_u_mark_submit(td, sd->queued);
255 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
258 td_verror(td, errno, "lseek");
262 if (sd->last_ddir == DDIR_READ)
263 ret = readv(f->fd, sd->iovecs, sd->queued);
265 ret = writev(f->fd, sd->iovecs, sd->queued);
267 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
268 sd->events = sd->queued;
270 return fio_vsyncio_end(td, ret);
273 static int fio_vsyncio_init(struct thread_data *td)
275 struct syncio_data *sd;
277 sd = malloc(sizeof(*sd));
278 memset(sd, 0, sizeof(*sd));
279 sd->last_offset = -1ULL;
280 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
281 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
283 td->io_ops->data = sd;
287 static void fio_vsyncio_cleanup(struct thread_data *td)
289 struct syncio_data *sd = td->io_ops->data;
296 static struct ioengine_ops ioengine_rw = {
298 .version = FIO_IOOPS_VERSION,
299 .prep = fio_syncio_prep,
300 .queue = fio_syncio_queue,
301 .open_file = generic_open_file,
302 .close_file = generic_close_file,
303 .get_file_size = generic_get_file_size,
307 static struct ioengine_ops ioengine_prw = {
309 .version = FIO_IOOPS_VERSION,
310 .queue = fio_psyncio_queue,
311 .open_file = generic_open_file,
312 .close_file = generic_close_file,
313 .get_file_size = generic_get_file_size,
317 static struct ioengine_ops ioengine_vrw = {
319 .version = FIO_IOOPS_VERSION,
320 .init = fio_vsyncio_init,
321 .cleanup = fio_vsyncio_cleanup,
322 .queue = fio_vsyncio_queue,
323 .commit = fio_vsyncio_commit,
324 .event = fio_vsyncio_event,
325 .getevents = fio_vsyncio_getevents,
326 .open_file = generic_open_file,
327 .close_file = generic_close_file,
328 .get_file_size = generic_get_file_size,
332 static void fio_init fio_syncio_register(void)
334 register_ioengine(&ioengine_rw);
335 register_ioengine(&ioengine_prw);
336 register_ioengine(&ioengine_vrw);
339 static void fio_exit fio_syncio_unregister(void)
341 unregister_ioengine(&ioengine_rw);
342 unregister_ioengine(&ioengine_prw);
343 unregister_ioengine(&ioengine_vrw);