4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
18 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
20 struct fio_file *f = io_u->file;
21 unsigned long long real_off = io_u->offset - f->file_offset;
23 if (io_u->ddir == DDIR_READ)
24 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
25 else if (io_u->ddir == DDIR_WRITE)
26 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
27 else if (io_u->ddir == DDIR_SYNC) {
28 size_t len = (f->file_size + page_size - 1) & ~page_mask;
30 if (msync(f->mmap, len, MS_SYNC)) {
32 td_verror(td, io_u->error, "msync");
37 * not really direct, but should drop the pages from the cache
39 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
40 size_t len = (io_u->xfer_buflen + page_size - 1) & ~page_mask;
41 unsigned long long off = real_off & ~page_mask;
43 if (msync(f->mmap + off, len, MS_SYNC) < 0) {
45 td_verror(td, io_u->error, "msync");
47 if (madvise(f->mmap + off, len, MADV_DONTNEED) < 0) {
49 td_verror(td, io_u->error, "madvise");
53 return FIO_Q_COMPLETED;
56 static int fio_mmapio_open(struct thread_data *td, struct fio_file *f)
60 ret = generic_open_file(td, f);
65 flags = PROT_READ | PROT_WRITE;
66 else if (td_write(td)) {
69 if (td->o.verify != VERIFY_NONE)
74 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
75 if (f->mmap == MAP_FAILED) {
77 td_verror(td, errno, "mmap");
81 if (file_invalidate_cache(td, f))
85 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
86 td_verror(td, errno, "madvise");
90 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
91 td_verror(td, errno, "madvise");
99 td->io_ops->close_file(td, f);
103 static void fio_mmapio_close(struct thread_data fio_unused *td,
107 munmap(f->mmap, f->file_size);
110 generic_close_file(td, f);
113 static struct ioengine_ops ioengine = {
115 .version = FIO_IOOPS_VERSION,
116 .queue = fio_mmapio_queue,
117 .open_file = fio_mmapio_open,
118 .close_file = fio_mmapio_close,
119 .flags = FIO_SYNCIO | FIO_NOEXTEND,
122 static void fio_init fio_mmapio_register(void)
124 register_ioengine(&ioengine);
127 static void fio_exit fio_mmapio_unregister(void)
129 unregister_ioengine(&ioengine);