| 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 | #include <sys/mman.h> |
| 11 | |
| 12 | #include "../fio.h" |
| 13 | #include "../os.h" |
| 14 | |
| 15 | static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u) |
| 16 | { |
| 17 | struct fio_file *f = io_u->file; |
| 18 | unsigned long long real_off = io_u->offset - f->file_offset; |
| 19 | |
| 20 | if (io_u->ddir == DDIR_READ) |
| 21 | memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen); |
| 22 | else if (io_u->ddir == DDIR_WRITE) |
| 23 | memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen); |
| 24 | else if (io_u->ddir == DDIR_SYNC) { |
| 25 | if (msync(f->mmap, f->file_size, MS_SYNC)) |
| 26 | io_u->error = errno; |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * not really direct, but should drop the pages from the cache |
| 31 | */ |
| 32 | if (td->odirect && io_u->ddir != DDIR_SYNC) { |
| 33 | if (msync(f->mmap + real_off, io_u->xfer_buflen, MS_SYNC) < 0) |
| 34 | io_u->error = errno; |
| 35 | if (madvise(f->mmap + real_off, io_u->xfer_buflen, MADV_DONTNEED) < 0) |
| 36 | io_u->error = errno; |
| 37 | } |
| 38 | |
| 39 | if (io_u->error) |
| 40 | td_verror(td, io_u->error); |
| 41 | |
| 42 | return FIO_Q_COMPLETED; |
| 43 | } |
| 44 | |
| 45 | static int fio_mmapio_init(struct thread_data *td) |
| 46 | { |
| 47 | struct fio_file *f; |
| 48 | int i; |
| 49 | |
| 50 | if (td->ddir == DDIR_READ && !td_rw(td)) |
| 51 | return 0; |
| 52 | |
| 53 | /* |
| 54 | * We need to truncate the files to the right size, if |
| 55 | * we are writing to it. |
| 56 | */ |
| 57 | for_each_file(td, f, i) { |
| 58 | if (ftruncate(f->fd, f->file_size) < 0) { |
| 59 | td_verror(td, errno); |
| 60 | return 1; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static struct ioengine_ops ioengine = { |
| 68 | .name = "mmap", |
| 69 | .version = FIO_IOOPS_VERSION, |
| 70 | .queue = fio_mmapio_queue, |
| 71 | .init = fio_mmapio_init, |
| 72 | .flags = FIO_SYNCIO | FIO_MMAPIO, |
| 73 | }; |
| 74 | |
| 75 | static void fio_init fio_mmapio_register(void) |
| 76 | { |
| 77 | register_ioengine(&ioengine); |
| 78 | } |
| 79 | |
| 80 | static void fio_exit fio_mmapio_unregister(void) |
| 81 | { |
| 82 | unregister_ioengine(&ioengine); |
| 83 | } |