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.
22 unsigned long queued_bytes;
24 unsigned long long last_offset;
25 struct fio_file *last_file;
26 enum fio_ddir last_ddir;
29 static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u)
31 struct fio_file *f = io_u->file;
33 if (!ddir_rw(io_u->ddir))
36 if (f->file_pos != -1ULL && f->file_pos == io_u->offset)
39 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
40 td_verror(td, errno, "lseek");
47 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
49 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
50 io_u->file->file_pos = io_u->offset + ret;
52 if (ret != (int) io_u->xfer_buflen) {
54 io_u->resid = io_u->xfer_buflen - ret;
56 return FIO_Q_COMPLETED;
62 td_verror(td, io_u->error, "xfer");
64 return FIO_Q_COMPLETED;
67 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
69 struct fio_file *f = io_u->file;
72 fio_ro_check(td, io_u);
74 if (io_u->ddir == DDIR_READ)
75 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
76 else if (io_u->ddir == DDIR_WRITE)
77 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
78 else if (io_u->ddir == DDIR_TRIM)
79 ret = do_io_u_trim(td, io_u);
81 ret = do_io_u_sync(td, io_u);
83 return fio_io_end(td, io_u, ret);
86 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
88 struct fio_file *f = io_u->file;
91 fio_ro_check(td, io_u);
93 if (io_u->ddir == DDIR_READ)
94 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
95 else if (io_u->ddir == DDIR_WRITE)
96 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
97 else if (io_u->ddir == DDIR_TRIM)
98 ret = do_io_u_trim(td, io_u);
100 ret = do_io_u_sync(td, io_u);
102 return fio_io_end(td, io_u, ret);
105 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
107 struct timespec fio_unused *t)
109 struct syncio_data *sd = td->io_ops->data;
118 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
122 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
124 struct syncio_data *sd = td->io_ops->data;
126 return sd->io_us[event];
129 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
131 struct syncio_data *sd = td->io_ops->data;
133 if (ddir_sync(io_u->ddir))
136 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
137 io_u->ddir == sd->last_ddir)
143 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
146 sd->io_us[idx] = io_u;
147 sd->iovecs[idx].iov_base = io_u->xfer_buf;
148 sd->iovecs[idx].iov_len = io_u->xfer_buflen;
149 sd->last_offset = io_u->offset + io_u->xfer_buflen;
150 sd->last_file = io_u->file;
151 sd->last_ddir = io_u->ddir;
152 sd->queued_bytes += io_u->xfer_buflen;
156 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
158 struct syncio_data *sd = td->io_ops->data;
160 fio_ro_check(td, io_u);
162 if (!fio_vsyncio_append(td, io_u)) {
163 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
165 * If we can't append and have stuff queued, tell fio to
166 * commit those first and then retry this io
170 if (ddir_sync(io_u->ddir)) {
171 int ret = do_io_u_sync(td, io_u);
173 return fio_io_end(td, io_u, ret);
177 sd->queued_bytes = 0;
178 fio_vsyncio_set_iov(sd, io_u, 0);
180 if (sd->queued == td->o.iodepth) {
181 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
185 dprint(FD_IO, "vsyncio_queue: append\n");
186 fio_vsyncio_set_iov(sd, io_u, sd->queued);
189 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
194 * Check that we transferred all bytes, or saw an error, etc
196 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
198 struct syncio_data *sd = td->io_ops->data;
204 * transferred everything, perfect
206 if (bytes == sd->queued_bytes)
210 for (i = 0; i < sd->queued; i++) {
216 unsigned int this_io;
219 if (this_io > io_u->xfer_buflen)
220 this_io = io_u->xfer_buflen;
222 io_u->resid = io_u->xfer_buflen - this_io;
229 td_verror(td, err, "xfer vsync");
236 static int fio_vsyncio_commit(struct thread_data *td)
238 struct syncio_data *sd = td->io_ops->data;
245 io_u_mark_submit(td, sd->queued);
248 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
251 td_verror(td, errno, "lseek");
255 if (sd->last_ddir == DDIR_READ)
256 ret = readv(f->fd, sd->iovecs, sd->queued);
258 ret = writev(f->fd, sd->iovecs, sd->queued);
260 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
261 sd->events = sd->queued;
263 return fio_vsyncio_end(td, ret);
266 static int fio_vsyncio_init(struct thread_data *td)
268 struct syncio_data *sd;
270 sd = malloc(sizeof(*sd));
271 memset(sd, 0, sizeof(*sd));
272 sd->last_offset = -1ULL;
273 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
274 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
276 td->io_ops->data = sd;
280 static void fio_vsyncio_cleanup(struct thread_data *td)
282 struct syncio_data *sd = td->io_ops->data;
289 static struct ioengine_ops ioengine_rw = {
291 .version = FIO_IOOPS_VERSION,
292 .prep = fio_syncio_prep,
293 .queue = fio_syncio_queue,
294 .open_file = generic_open_file,
295 .close_file = generic_close_file,
296 .get_file_size = generic_get_file_size,
300 static struct ioengine_ops ioengine_prw = {
302 .version = FIO_IOOPS_VERSION,
303 .queue = fio_psyncio_queue,
304 .open_file = generic_open_file,
305 .close_file = generic_close_file,
306 .get_file_size = generic_get_file_size,
310 static struct ioengine_ops ioengine_vrw = {
312 .version = FIO_IOOPS_VERSION,
313 .init = fio_vsyncio_init,
314 .cleanup = fio_vsyncio_cleanup,
315 .queue = fio_vsyncio_queue,
316 .commit = fio_vsyncio_commit,
317 .event = fio_vsyncio_event,
318 .getevents = fio_vsyncio_getevents,
319 .open_file = generic_open_file,
320 .close_file = generic_close_file,
321 .get_file_size = generic_get_file_size,
325 static void fio_init fio_syncio_register(void)
327 register_ioengine(&ioengine_rw);
328 register_ioengine(&ioengine_prw);
329 register_ioengine(&ioengine_vrw);
332 static void fio_exit fio_syncio_unregister(void)
334 unregister_ioengine(&ioengine_rw);
335 unregister_ioengine(&ioengine_prw);
336 unregister_ioengine(&ioengine_vrw);