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_sync(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)
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_syncio_sync(struct thread_data *td, struct io_u *io_u)
71 if (io_u->ddir == DDIR_SYNC) {
72 ret = fsync(io_u->file->fd);
73 } else if (io_u->ddir == DDIR_DATASYNC) {
74 #ifdef FIO_HAVE_FDATASYNC
75 ret = fdatasync(io_u->file->fd);
77 ret = io_u->xfer_buflen;
80 } else if (io_u->ddir == DDIR_SYNC_FILE_RANGE)
81 ret = do_sync_file_range(td, io_u->file);
83 ret = io_u->xfer_buflen;
87 return fio_io_end(td, io_u, ret);
90 static int fio_psyncio_queue(struct thread_data *td, struct io_u *io_u)
92 struct fio_file *f = io_u->file;
95 fio_ro_check(td, io_u);
97 if (io_u->ddir == DDIR_READ)
98 ret = pread(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
99 else if (io_u->ddir == DDIR_WRITE)
100 ret = pwrite(f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
102 return fio_syncio_sync(td, io_u);
104 return fio_io_end(td, io_u, ret);
107 static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u)
109 struct fio_file *f = io_u->file;
112 fio_ro_check(td, io_u);
114 if (io_u->ddir == DDIR_READ)
115 ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
116 else if (io_u->ddir == DDIR_WRITE)
117 ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen);
119 return fio_syncio_sync(td, io_u);
121 return fio_io_end(td, io_u, ret);
124 static int fio_vsyncio_getevents(struct thread_data *td, unsigned int min,
126 struct timespec fio_unused *t)
128 struct syncio_data *sd = td->io_ops->data;
137 dprint(FD_IO, "vsyncio_getevents: min=%d,max=%d: %d\n", min, max, ret);
141 static struct io_u *fio_vsyncio_event(struct thread_data *td, int event)
143 struct syncio_data *sd = td->io_ops->data;
145 return sd->io_us[event];
148 static int fio_vsyncio_append(struct thread_data *td, struct io_u *io_u)
150 struct syncio_data *sd = td->io_ops->data;
152 if (ddir_sync(io_u->ddir))
155 if (io_u->offset == sd->last_offset && io_u->file == sd->last_file &&
156 io_u->ddir == sd->last_ddir)
162 static void fio_vsyncio_set_iov(struct syncio_data *sd, struct io_u *io_u,
165 sd->io_us[index] = io_u;
166 sd->iovecs[index].iov_base = io_u->xfer_buf;
167 sd->iovecs[index].iov_len = io_u->xfer_buflen;
168 sd->last_offset = io_u->offset + io_u->xfer_buflen;
169 sd->last_file = io_u->file;
170 sd->last_ddir = io_u->ddir;
171 sd->queued_bytes += io_u->xfer_buflen;
175 static int fio_vsyncio_queue(struct thread_data *td, struct io_u *io_u)
177 struct syncio_data *sd = td->io_ops->data;
179 fio_ro_check(td, io_u);
181 if (!fio_vsyncio_append(td, io_u)) {
182 dprint(FD_IO, "vsyncio_queue: no append (%d)\n", sd->queued);
184 * If we can't append and have stuff queued, tell fio to
185 * commit those first and then retry this io
189 if (ddir_sync(io_u->ddir))
190 return fio_syncio_sync(td, io_u);
193 sd->queued_bytes = 0;
194 fio_vsyncio_set_iov(sd, io_u, 0);
196 if (sd->queued == td->o.iodepth) {
197 dprint(FD_IO, "vsyncio_queue: max depth %d\n", sd->queued);
201 dprint(FD_IO, "vsyncio_queue: append\n");
202 fio_vsyncio_set_iov(sd, io_u, sd->queued);
205 dprint(FD_IO, "vsyncio_queue: depth now %d\n", sd->queued);
210 * Check that we transferred all bytes, or saw an error, etc
212 static int fio_vsyncio_end(struct thread_data *td, ssize_t bytes)
214 struct syncio_data *sd = td->io_ops->data;
220 * transferred everything, perfect
222 if (bytes == sd->queued_bytes)
226 for (i = 0; i < sd->queued; i++) {
232 unsigned int this_io;
235 if (this_io > io_u->xfer_buflen)
236 this_io = io_u->xfer_buflen;
238 io_u->resid = io_u->xfer_buflen - this_io;
245 td_verror(td, err, "xfer vsync");
252 static int fio_vsyncio_commit(struct thread_data *td)
254 struct syncio_data *sd = td->io_ops->data;
261 io_u_mark_submit(td, sd->queued);
264 if (lseek(f->fd, sd->io_us[0]->offset, SEEK_SET) == -1) {
267 td_verror(td, errno, "lseek");
271 if (sd->last_ddir == DDIR_READ)
272 ret = readv(f->fd, sd->iovecs, sd->queued);
274 ret = writev(f->fd, sd->iovecs, sd->queued);
276 dprint(FD_IO, "vsyncio_commit: %d\n", (int) ret);
277 sd->events = sd->queued;
279 return fio_vsyncio_end(td, ret);
282 static int fio_vsyncio_init(struct thread_data *td)
284 struct syncio_data *sd;
286 sd = malloc(sizeof(*sd));
287 memset(sd, 0, sizeof(*sd));
288 sd->last_offset = -1ULL;
289 sd->iovecs = malloc(td->o.iodepth * sizeof(struct iovec));
290 sd->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
292 td->io_ops->data = sd;
296 static void fio_vsyncio_cleanup(struct thread_data *td)
298 struct syncio_data *sd = td->io_ops->data;
305 static struct ioengine_ops ioengine_rw = {
307 .version = FIO_IOOPS_VERSION,
308 .prep = fio_syncio_prep,
309 .queue = fio_syncio_queue,
310 .open_file = generic_open_file,
311 .close_file = generic_close_file,
312 .get_file_size = generic_get_file_size,
316 static struct ioengine_ops ioengine_prw = {
318 .version = FIO_IOOPS_VERSION,
319 .queue = fio_psyncio_queue,
320 .open_file = generic_open_file,
321 .close_file = generic_close_file,
322 .get_file_size = generic_get_file_size,
326 static struct ioengine_ops ioengine_vrw = {
328 .version = FIO_IOOPS_VERSION,
329 .init = fio_vsyncio_init,
330 .cleanup = fio_vsyncio_cleanup,
331 .queue = fio_vsyncio_queue,
332 .commit = fio_vsyncio_commit,
333 .event = fio_vsyncio_event,
334 .getevents = fio_vsyncio_getevents,
335 .open_file = generic_open_file,
336 .close_file = generic_close_file,
337 .get_file_size = generic_get_file_size,
341 static void fio_init fio_syncio_register(void)
343 register_ioengine(&ioengine_rw);
344 register_ioengine(&ioengine_prw);
345 register_ioengine(&ioengine_vrw);
348 static void fio_exit fio_syncio_unregister(void)
350 unregister_ioengine(&ioengine_rw);
351 unregister_ioengine(&ioengine_prw);
352 unregister_ioengine(&ioengine_vrw);