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 (io_u->ddir == DDIR_SYNC)
36 if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) {
37 td_verror(td, errno, "lseek");
44 static int fio_io_end(struct thread_data *td, struct io_u *io_u, int ret)
46 if (ret != (int) io_u->xfer_buflen) {
48 io_u->resid = io_u->xfer_buflen - ret;
50 return FIO_Q_COMPLETED;
56 td_verror(td, io_u->error, "xfer");
58 return FIO_Q_COMPLETED;
61 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
63 struct fio_file *f = io_u->file;
66 fio_ro_check(td, io_u);
68 if (io_u->ddir == DDIR_READ)
69 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
70 else if (io_u->ddir == DDIR_WRITE)
71 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
75 return fio_io_end(td, io_u, ret);
78 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
80 struct fio_file *f = io_u->file;
83 fio_ro_check(td, io_u);
85 if (io_u->ddir == DDIR_READ)
86 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
87 else if (io_u->ddir == DDIR_WRITE)
88 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
92 return fio_io_end(td, io_u, ret);
95 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
97 struct timespec fio_unused *t)
99 struct syncio_data *sd = td->io_ops->data;
108 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
112 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
114 struct syncio_data *sd = td->io_ops->data;
116 return sd->io_us[event];
119 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
121 struct syncio_data *sd = td->io_ops->data;
123 if (io_u->ddir == DDIR_SYNC)
126 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
127 io_u->ddir == sd->last_ddir)
133 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
136 sd->io_us[index] = io_u;
137 sd->iovecs[index].iov_base = io_u->xfer_buf;
138 sd->iovecs[index].iov_len = io_u->xfer_buflen;
139 sd->last_offset = io_u->offset + io_u->xfer_buflen;
140 sd->last_file = io_u->file;
141 sd->last_ddir = io_u->ddir;
142 sd->queued_bytes += io_u->xfer_buflen;
146 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
148 struct syncio_data *sd = td->io_ops->data;
150 fio_ro_check(td, io_u);
152 if (!fio_vsyncio_append(td, io_u)) {
153 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
155 * If we can't append and have stuff queued, tell fio to
156 * commit those first and then retry this io
160 if (io_u->ddir == DDIR_SYNC) {
161 int ret = fsync(io_u->file->fd);
163 return fio_io_end(td, io_u, ret);
167 sd->queued_bytes = 0;
168 fio_vsyncio_set_iov(sd, io_u, 0);
170 if (sd->queued == td->o.iodepth) {
171 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
175 dprint(FD_IO, "vsyncio_queue: append\n");
176 fio_vsyncio_set_iov(sd, io_u, sd->queued);
179 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
184 * Check that we transferred all bytes, or saw an error, etc
186 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
188 struct syncio_data *sd = td->io_ops->data;
194 * transferred everything, perfect
196 if (bytes == sd->queued_bytes)
200 for (i = 0; i < sd->queued; i++) {
206 unsigned int this_io;
209 if (this_io > io_u->xfer_buflen)
210 this_io = io_u->xfer_buflen;
212 io_u->resid = io_u->xfer_buflen - this_io;
219 td_verror(td, err, "xfer vsync");
226 static int fio_vsyncio_commit(struct thread_data *td)
228 struct syncio_data *sd = td->io_ops->data;
235 io_u_mark_submit(td, sd->queued);
238 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
241 td_verror(td, errno, "lseek");
245 if (sd->last_ddir == DDIR_READ)
246 ret = readv(f->fd, sd->iovecs, sd->queued);
248 ret = writev(f->fd, sd->iovecs, sd->queued);
250 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
251 sd->events = sd->queued;
253 return fio_vsyncio_end(td, ret);
256 static int fio_vsyncio_init(struct thread_data *td)
258 struct syncio_data *sd;
260 sd = malloc(sizeof(*sd));
261 memset(sd, 0, sizeof(*sd));
262 sd->last_offset = -1ULL;
263 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
264 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
266 td->io_ops->data = sd;
270 static void fio_vsyncio_cleanup(struct thread_data *td)
272 struct syncio_data *sd = td->io_ops->data;
279 static struct ioengine_ops ioengine_rw = {
281 .version = FIO_IOOPS_VERSION,
282 .prep = fio_syncio_prep,
283 .queue = fio_syncio_queue,
284 .open_file = generic_open_file,
285 .close_file = generic_close_file,
286 .get_file_size = generic_get_file_size,
290 static struct ioengine_ops ioengine_prw = {
292 .version = FIO_IOOPS_VERSION,
293 .queue = fio_psyncio_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_vrw = {
302 .version = FIO_IOOPS_VERSION,
303 .init = fio_vsyncio_init,
304 .cleanup = fio_vsyncio_cleanup,
305 .queue = fio_vsyncio_queue,
306 .commit = fio_vsyncio_commit,
307 .event = fio_vsyncio_event,
308 .getevents = fio_vsyncio_getevents,
309 .open_file = generic_open_file,
310 .close_file = generic_close_file,
311 .get_file_size = generic_get_file_size,
315 static void fio_init fio_syncio_register(void)
317 register_ioengine(&ioengine_rw);
318 register_ioengine(&ioengine_prw);
319 register_ioengine(&ioengine_vrw);
322 static void fio_exit fio_syncio_unregister(void)
324 unregister_ioengine(&ioengine_rw);
325 unregister_ioengine(&ioengine_prw);
326 unregister_ioengine(&ioengine_vrw);