4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
15 #include "../verify.h"
18 * Limits us to 1GB of mapped files in total
20 #define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL)
22 static unsigned long mmap_map_size;
23 static unsigned long mmap_map_mask;
25 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
26 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) {
43 td_verror(td, errno, "mmap");
48 if (posix_madvise(f->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) {
49 td_verror(td, errno, "madvise");
53 if (posix_madvise(f->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) {
54 td_verror(td, errno, "madvise");
60 if (td->error && f->mmap_ptr)
61 munmap(f->mmap_ptr, length);
67 * Just mmap an appropriate portion, we cannot mmap the full extent
69 static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
71 struct fio_file *f = io_u->file;
73 if (io_u->buflen > mmap_map_size) {
74 log_err("fio: bs too big for mmap engine\n");
78 f->mmap_sz = mmap_map_size;
79 if (f->mmap_sz > f->io_size)
80 f->mmap_sz = f->io_size;
82 f->mmap_off = io_u->offset;
84 return fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
88 * Attempt to mmap the entire file
90 static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u)
92 struct fio_file *f = io_u->file;
95 if (fio_file_partial_mmap(f))
98 f->mmap_sz = f->io_size;
101 ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
103 fio_file_set_partial_mmap(f);
108 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
110 struct fio_file *f = io_u->file;
114 * It fits within existing mapping, use it
116 if (io_u->offset >= f->mmap_off &&
117 io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz)
121 * unmap any existing mapping
124 if (munmap(f->mmap_ptr, f->mmap_sz) < 0)
129 if (fio_mmapio_prep_full(td, io_u)) {
131 ret = fio_mmapio_prep_limited(td, io_u);
137 io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off -
142 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
144 struct fio_file *f = io_u->file;
146 fio_ro_check(td, io_u);
148 if (io_u->ddir == DDIR_READ)
149 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
150 else if (io_u->ddir == DDIR_WRITE)
151 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
152 else if (ddir_sync(io_u->ddir)) {
153 if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) {
155 td_verror(td, io_u->error, "msync");
157 } else if (io_u->ddir == DDIR_TRIM) {
158 int ret = do_io_u_trim(td, io_u);
161 td_verror(td, io_u->error, "trim");
166 * not really direct, but should drop the pages from the cache
168 if (td->o.odirect && ddir_rw(io_u->ddir)) {
169 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
171 td_verror(td, io_u->error, "msync");
173 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
175 td_verror(td, io_u->error, "madvise");
179 return FIO_Q_COMPLETED;
182 static int fio_mmapio_init(struct thread_data *td)
184 struct thread_options *o = &td->o;
185 unsigned long shift, mask;
187 if ((td->o.rw_min_bs & page_mask) &&
188 (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) {
189 log_err("fio: mmap options dictate a minimum block size of "
190 "%lu bytes\n", page_size);
194 mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
195 mask = mmap_map_size;
204 mmap_map_mask = 1UL << shift;
208 static struct ioengine_ops ioengine = {
210 .version = FIO_IOOPS_VERSION,
211 .init = fio_mmapio_init,
212 .prep = fio_mmapio_prep,
213 .queue = fio_mmapio_queue,
214 .open_file = generic_open_file,
215 .close_file = generic_close_file,
216 .get_file_size = generic_get_file_size,
217 .flags = FIO_SYNCIO | FIO_NOEXTEND,
220 static void fio_init fio_mmapio_register(void)
222 register_ioengine(&ioengine);
225 static void fio_exit fio_mmapio_unregister(void)
227 unregister_ioengine(&ioengine);