4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
17 * Limits us to 2GB of mapped files in total
19 #define MMAP_TOTAL_SZ (2 * 1024 * 1024 * 1024UL)
21 static unsigned long mmap_map_size;
22 static unsigned long mmap_map_mask;
24 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
25 size_t length, off_t off)
31 flags = PROT_READ | PROT_WRITE;
32 else if (td_write(td)) {
35 if (td->o.verify != VERIFY_NONE)
40 f->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
41 if (f->mmap_ptr == MAP_FAILED) {
45 td_verror(td, err, "mmap");
50 if (madvise(f->mmap_ptr, length, MADV_SEQUENTIAL) < 0) {
51 td_verror(td, errno, "madvise");
55 if (madvise(f->mmap_ptr, length, MADV_RANDOM) < 0) {
56 td_verror(td, errno, "madvise");
65 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
67 struct fio_file *f = io_u->file;
70 if (io_u->buflen > mmap_map_size) {
71 log_err("fio: bs too big for mmap engine\n");
76 if (io_u->offset >= f->mmap_off &&
77 io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz)
81 if (munmap(f->mmap_ptr, f->mmap_sz) < 0) {
88 f->mmap_sz = mmap_map_size;
89 if (f->mmap_sz > f->io_size)
90 f->mmap_sz = f->io_size;
92 f->mmap_off = io_u->offset & ~mmap_map_mask;
93 if (io_u->offset + io_u->buflen >= f->mmap_off + f->mmap_sz)
94 f->mmap_off -= io_u->buflen;
96 ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
99 io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off -
105 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
107 struct fio_file *f = io_u->file;
109 fio_ro_check(td, io_u);
111 if (io_u->ddir == DDIR_READ)
112 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
113 else if (io_u->ddir == DDIR_WRITE)
114 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
115 else if (io_u->ddir == DDIR_SYNC) {
116 if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) {
118 td_verror(td, io_u->error, "msync");
123 * not really direct, but should drop the pages from the cache
125 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
126 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
128 td_verror(td, io_u->error, "msync");
130 if (madvise(io_u->mmap_data, io_u->xfer_buflen, MADV_DONTNEED) < 0) {
132 td_verror(td, io_u->error, "madvise");
136 return FIO_Q_COMPLETED;
139 static int fio_mmapio_init(struct thread_data *td)
141 unsigned long shift, mask;
143 mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
144 mask = mmap_map_size;
153 mmap_map_mask = 1UL << shift;
157 static struct ioengine_ops ioengine = {
159 .version = FIO_IOOPS_VERSION,
160 .init = fio_mmapio_init,
161 .prep = fio_mmapio_prep,
162 .queue = fio_mmapio_queue,
163 .open_file = generic_open_file,
164 .close_file = generic_close_file,
165 .get_file_size = generic_get_file_size,
166 .flags = FIO_SYNCIO | FIO_NOEXTEND,
169 static void fio_init fio_mmapio_register(void)
171 register_ioengine(&ioengine);
174 static void fio_exit fio_mmapio_unregister(void)
176 unregister_ioengine(&ioengine);