4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
17 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
19 struct fio_file *f = io_u->file;
20 unsigned long long real_off = io_u->offset - f->file_offset;
22 fio_ro_check(td, io_u);
24 if (io_u->ddir == DDIR_READ)
25 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
26 else if (io_u->ddir == DDIR_WRITE)
27 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
28 else if (io_u->ddir == DDIR_SYNC) {
29 size_t len = (f->io_size + page_size - 1) & ~page_mask;
31 if (msync(f->mmap, len, MS_SYNC)) {
33 td_verror(td, io_u->error, "msync");
38 * not really direct, but should drop the pages from the cache
40 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
41 size_t len = (io_u->xfer_buflen + page_size - 1) & ~page_mask;
42 unsigned long long off = real_off & ~page_mask;
44 if (msync(f->mmap + off, len, MS_SYNC) < 0) {
46 td_verror(td, io_u->error, "msync");
48 if (madvise(f->mmap + off, len, MADV_DONTNEED) < 0) {
50 td_verror(td, io_u->error, "madvise");
54 return FIO_Q_COMPLETED;
57 static int fio_mmapio_open(struct thread_data *td, struct fio_file *f)
61 ret = generic_open_file(td, f);
66 * for size checkup, don't mmap anything.
72 flags = PROT_READ | PROT_WRITE;
73 else if (td_write(td)) {
76 if (td->o.verify != VERIFY_NONE)
81 f->mmap = mmap(NULL, f->io_size, flags, MAP_SHARED, f->fd, f->file_offset);
82 if (f->mmap == MAP_FAILED) {
84 td_verror(td, errno, "mmap");
88 if (file_invalidate_cache(td, f))
92 if (madvise(f->mmap, f->io_size, MADV_SEQUENTIAL) < 0) {
93 td_verror(td, errno, "madvise");
97 if (madvise(f->mmap, f->io_size, MADV_RANDOM) < 0) {
98 td_verror(td, errno, "madvise");
106 td->io_ops->close_file(td, f);
110 static int fio_mmapio_close(struct thread_data fio_unused *td,
116 if (munmap(f->mmap, f->io_size) < 0)
122 ret2 = generic_close_file(td, f);
129 static struct ioengine_ops ioengine = {
131 .version = FIO_IOOPS_VERSION,
132 .queue = fio_mmapio_queue,
133 .open_file = fio_mmapio_open,
134 .close_file = fio_mmapio_close,
135 .flags = FIO_SYNCIO | FIO_NOEXTEND,
138 static void fio_init fio_mmapio_register(void)
140 register_ioengine(&ioengine);
143 static void fio_exit fio_mmapio_unregister(void)
145 unregister_ioengine(&ioengine);