| 1 | /* |
| 2 | * regular read/write sync io engine |
| 3 | * |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
| 10 | |
| 11 | #include "../fio.h" |
| 12 | #include "../os.h" |
| 13 | |
| 14 | static int fio_syncio_prep(struct thread_data *td, struct io_u *io_u) |
| 15 | { |
| 16 | struct fio_file *f = io_u->file; |
| 17 | |
| 18 | if (io_u->ddir == DDIR_SYNC) |
| 19 | return 0; |
| 20 | if (io_u->offset == f->last_completed_pos) |
| 21 | return 0; |
| 22 | |
| 23 | if (lseek(f->fd, io_u->offset, SEEK_SET) == -1) { |
| 24 | td_verror(td, errno, "lseek"); |
| 25 | return 1; |
| 26 | } |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static int fio_syncio_queue(struct thread_data *td, struct io_u *io_u) |
| 32 | { |
| 33 | struct fio_file *f = io_u->file; |
| 34 | int ret; |
| 35 | |
| 36 | if (io_u->ddir == DDIR_READ) |
| 37 | ret = read(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
| 38 | else if (io_u->ddir == DDIR_WRITE) |
| 39 | ret = write(f->fd, io_u->xfer_buf, io_u->xfer_buflen); |
| 40 | else |
| 41 | ret = fsync(f->fd); |
| 42 | |
| 43 | if (ret != (int) io_u->xfer_buflen) { |
| 44 | if (ret >= 0) { |
| 45 | io_u->resid = io_u->xfer_buflen - ret; |
| 46 | io_u->error = 0; |
| 47 | return FIO_Q_COMPLETED; |
| 48 | } else |
| 49 | io_u->error = errno; |
| 50 | } |
| 51 | |
| 52 | if (io_u->error) |
| 53 | td_verror(td, io_u->error, "xfer"); |
| 54 | |
| 55 | return FIO_Q_COMPLETED; |
| 56 | } |
| 57 | |
| 58 | static struct ioengine_ops ioengine = { |
| 59 | .name = "sync", |
| 60 | .version = FIO_IOOPS_VERSION, |
| 61 | .prep = fio_syncio_prep, |
| 62 | .queue = fio_syncio_queue, |
| 63 | .flags = FIO_SYNCIO, |
| 64 | }; |
| 65 | |
| 66 | static void fio_init fio_syncio_register(void) |
| 67 | { |
| 68 | register_ioengine(&ioengine); |
| 69 | } |
| 70 | |
| 71 | static void fio_exit fio_syncio_unregister(void) |
| 72 | { |
| 73 | unregister_ioengine(&ioengine); |
| 74 | } |