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 struct fio_mmap_data {
31 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
32 size_t length, off_t off)
34 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
38 flags = PROT_READ | PROT_WRITE;
39 else if (td_write(td)) {
42 if (td->o.verify != VERIFY_NONE)
47 fmd->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
48 if (fmd->mmap_ptr == MAP_FAILED) {
50 td_verror(td, errno, "mmap");
55 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) {
56 td_verror(td, errno, "madvise");
60 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) {
61 td_verror(td, errno, "madvise");
65 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_DONTNEED) < 0) {
66 td_verror(td, errno, "madvise");
71 if (f->filetype == FIO_TYPE_BD)
72 (void) posix_madvise(fmd->mmap_ptr, fmd->mmap_sz, FIO_MADV_FREE);
77 if (td->error && fmd->mmap_ptr)
78 munmap(fmd->mmap_ptr, length);
84 * Just mmap an appropriate portion, we cannot mmap the full extent
86 static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
88 struct fio_file *f = io_u->file;
89 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
91 if (io_u->buflen > mmap_map_size) {
92 log_err("fio: bs too big for mmap engine\n");
96 fmd->mmap_sz = mmap_map_size;
97 if (fmd->mmap_sz > f->io_size)
98 fmd->mmap_sz = f->io_size;
100 fmd->mmap_off = io_u->offset;
102 return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
106 * Attempt to mmap the entire file
108 static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u)
110 struct fio_file *f = io_u->file;
111 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
114 if (fio_file_partial_mmap(f))
116 if (io_u->offset != (size_t) io_u->offset ||
117 f->io_size != (size_t) f->io_size) {
118 fio_file_set_partial_mmap(f);
122 fmd->mmap_sz = f->io_size;
125 ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
127 fio_file_set_partial_mmap(f);
132 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
134 struct fio_file *f = io_u->file;
135 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
139 * It fits within existing mapping, use it
141 if (io_u->offset >= fmd->mmap_off &&
142 io_u->offset + io_u->buflen < fmd->mmap_off + fmd->mmap_sz)
146 * unmap any existing mapping
149 if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0)
151 fmd->mmap_ptr = NULL;
154 if (fio_mmapio_prep_full(td, io_u)) {
156 ret = fio_mmapio_prep_limited(td, io_u);
162 io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off -
167 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
169 struct fio_file *f = io_u->file;
170 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
172 fio_ro_check(td, io_u);
174 if (io_u->ddir == DDIR_READ)
175 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
176 else if (io_u->ddir == DDIR_WRITE)
177 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
178 else if (ddir_sync(io_u->ddir)) {
179 if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) {
181 td_verror(td, io_u->error, "msync");
183 } else if (io_u->ddir == DDIR_TRIM) {
184 int ret = do_io_u_trim(td, io_u);
187 td_verror(td, io_u->error, "trim");
192 * not really direct, but should drop the pages from the cache
194 if (td->o.odirect && ddir_rw(io_u->ddir)) {
195 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
197 td_verror(td, io_u->error, "msync");
199 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
201 td_verror(td, io_u->error, "madvise");
205 return FIO_Q_COMPLETED;
208 static int fio_mmapio_init(struct thread_data *td)
210 struct thread_options *o = &td->o;
211 unsigned long shift, mask;
213 if ((td->o.rw_min_bs & page_mask) &&
214 (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) {
215 log_err("fio: mmap options dictate a minimum block size of "
216 "%llu bytes\n", (unsigned long long) page_size);
220 mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
221 mask = mmap_map_size;
230 mmap_map_mask = 1UL << shift;
234 static int fio_mmapio_open_file(struct thread_data *td, struct fio_file *f)
236 struct fio_mmap_data *fmd;
239 ret = generic_open_file(td, f);
243 fmd = calloc(1, sizeof(*fmd));
245 int fio_unused __ret;
246 __ret = generic_close_file(td, f);
250 FILE_SET_ENG_DATA(f, fmd);
254 static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f)
256 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
258 FILE_SET_ENG_DATA(f, NULL);
260 fio_file_clear_partial_mmap(f);
262 return generic_close_file(td, f);
265 static struct ioengine_ops ioengine = {
267 .version = FIO_IOOPS_VERSION,
268 .init = fio_mmapio_init,
269 .prep = fio_mmapio_prep,
270 .queue = fio_mmapio_queue,
271 .open_file = fio_mmapio_open_file,
272 .close_file = fio_mmapio_close_file,
273 .get_file_size = generic_get_file_size,
274 .flags = FIO_SYNCIO | FIO_NOEXTEND,
277 static void fio_init fio_mmapio_register(void)
279 register_ioengine(&ioengine);
282 static void fio_exit fio_mmapio_unregister(void)
284 unregister_ioengine(&ioengine);