4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
14 #include "../optgroup.h"
15 #include "../verify.h"
18 * Limits us to 1GiB of mapped files in total
20 #define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL)
22 static unsigned long mmap_map_size;
24 struct fio_mmap_data {
30 #ifdef CONFIG_HAVE_THP
36 static struct fio_option options[] = {
39 .lname = "Transparent Huge Pages",
41 .off1 = offsetof(struct mmap_options, thp),
42 .help = "Memory Advise Huge Page",
43 .category = FIO_OPT_C_ENGINE,
44 .group = FIO_OPT_G_MMAP,
52 static bool fio_madvise_file(struct thread_data *td, struct fio_file *f,
56 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
57 #ifdef CONFIG_HAVE_THP
58 struct mmap_options *o = td->eo;
60 /* Ignore errors on this optional advisory */
62 madvise(fmd->mmap_ptr, length, MADV_HUGEPAGE);
65 if (!td->o.fadvise_hint)
69 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) {
70 td_verror(td, errno, "madvise");
74 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) {
75 td_verror(td, errno, "madvise");
83 #ifdef CONFIG_HAVE_THP
84 static int fio_mmap_get_shared(struct thread_data *td)
86 struct mmap_options *o = td->eo;
93 static int fio_mmap_get_shared(struct thread_data *td)
99 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
100 size_t length, off_t off)
102 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
103 int flags = 0, shared = fio_mmap_get_shared(td);
105 if (td_rw(td) && !td->o.verify_only)
106 flags = PROT_READ | PROT_WRITE;
107 else if (td_write(td) && !td->o.verify_only) {
110 if (td->o.verify != VERIFY_NONE)
115 fmd->mmap_ptr = mmap(NULL, length, flags, shared, f->fd, off);
116 if (fmd->mmap_ptr == MAP_FAILED) {
117 fmd->mmap_ptr = NULL;
118 td_verror(td, errno, "mmap");
122 if (!fio_madvise_file(td, f, length))
125 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_DONTNEED) < 0) {
126 td_verror(td, errno, "madvise");
131 if (f->filetype == FIO_TYPE_BLOCK)
132 (void) posix_madvise(fmd->mmap_ptr, fmd->mmap_sz, FIO_MADV_FREE);
136 if (td->error && fmd->mmap_ptr)
137 munmap(fmd->mmap_ptr, length);
143 * Just mmap an appropriate portion, we cannot mmap the full extent
145 static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
147 struct fio_file *f = io_u->file;
148 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
150 if (io_u->buflen > mmap_map_size) {
151 log_err("fio: bs too big for mmap engine\n");
155 fmd->mmap_sz = mmap_map_size;
156 if (fmd->mmap_sz > f->io_size)
157 fmd->mmap_sz = f->io_size;
159 fmd->mmap_off = io_u->offset;
161 return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
165 * Attempt to mmap the entire file
167 static int fio_mmapio_prep_full(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);
173 if (fio_file_partial_mmap(f))
175 if (io_u->offset != (size_t) io_u->offset ||
176 f->io_size != (size_t) f->io_size) {
177 fio_file_set_partial_mmap(f);
181 fmd->mmap_sz = f->io_size;
184 ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
186 fio_file_set_partial_mmap(f);
191 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
193 struct fio_file *f = io_u->file;
194 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
198 * It fits within existing mapping, use it
200 if (io_u->offset >= fmd->mmap_off &&
201 io_u->offset + io_u->buflen <= fmd->mmap_off + fmd->mmap_sz)
205 * unmap any existing mapping
208 if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0)
210 fmd->mmap_ptr = NULL;
213 if (fio_mmapio_prep_full(td, io_u)) {
215 ret = fio_mmapio_prep_limited(td, io_u);
221 io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off -
226 static enum fio_q_status fio_mmapio_queue(struct thread_data *td,
229 struct fio_file *f = io_u->file;
230 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
232 fio_ro_check(td, io_u);
234 if (io_u->ddir == DDIR_READ)
235 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
236 else if (io_u->ddir == DDIR_WRITE)
237 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
238 else if (ddir_sync(io_u->ddir)) {
239 if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) {
241 td_verror(td, io_u->error, "msync");
243 } else if (io_u->ddir == DDIR_TRIM) {
244 int ret = do_io_u_trim(td, io_u);
247 td_verror(td, io_u->error, "trim");
252 * not really direct, but should drop the pages from the cache
254 if (td->o.odirect && ddir_rw(io_u->ddir)) {
255 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
257 td_verror(td, io_u->error, "msync");
259 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
261 td_verror(td, io_u->error, "madvise");
265 return FIO_Q_COMPLETED;
268 static int fio_mmapio_init(struct thread_data *td)
270 struct thread_options *o = &td->o;
272 if ((o->rw_min_bs & page_mask) &&
273 (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) {
274 log_err("fio: mmap options dictate a minimum block size of "
275 "%llu bytes\n", (unsigned long long) page_size);
279 mmap_map_size = MMAP_TOTAL_SZ / o->nr_files;
283 static int fio_mmapio_open_file(struct thread_data *td, struct fio_file *f)
285 struct fio_mmap_data *fmd;
288 ret = generic_open_file(td, f);
292 fmd = calloc(1, sizeof(*fmd));
294 int fio_unused __ret;
295 __ret = generic_close_file(td, f);
299 FILE_SET_ENG_DATA(f, fmd);
303 static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f)
305 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
307 FILE_SET_ENG_DATA(f, NULL);
309 fio_file_clear_partial_mmap(f);
311 return generic_close_file(td, f);
314 static struct ioengine_ops ioengine = {
316 .version = FIO_IOOPS_VERSION,
317 .init = fio_mmapio_init,
318 .prep = fio_mmapio_prep,
319 .queue = fio_mmapio_queue,
320 .open_file = fio_mmapio_open_file,
321 .close_file = fio_mmapio_close_file,
322 .get_file_size = generic_get_file_size,
323 .flags = FIO_SYNCIO | FIO_NOEXTEND,
324 #ifdef CONFIG_HAVE_THP
326 .option_struct_size = sizeof(struct mmap_options),
330 static void fio_init fio_mmapio_register(void)
332 register_ioengine(&ioengine);
335 static void fio_exit fio_mmapio_unregister(void)
337 unregister_ioengine(&ioengine);