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 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 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
31 size_t length, off_t off)
33 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
37 flags = PROT_READ | PROT_WRITE;
38 else if (td_write(td)) {
41 if (td->o.verify != VERIFY_NONE)
46 fmd->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
47 if (fmd->mmap_ptr == MAP_FAILED) {
49 td_verror(td, errno, "mmap");
54 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) {
55 td_verror(td, errno, "madvise");
59 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) {
60 td_verror(td, errno, "madvise");
64 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_DONTNEED) < 0) {
65 td_verror(td, errno, "madvise");
70 if (f->filetype == FIO_TYPE_BD)
71 (void) posix_madvise(fmd->mmap_ptr, fmd->mmap_sz, FIO_MADV_FREE);
75 if (td->error && fmd->mmap_ptr)
76 munmap(fmd->mmap_ptr, length);
82 * Just mmap an appropriate portion, we cannot mmap the full extent
84 static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
86 struct fio_file *f = io_u->file;
87 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
89 if (io_u->buflen > mmap_map_size) {
90 log_err("fio: bs too big for mmap engine\n");
94 fmd->mmap_sz = mmap_map_size;
95 if (fmd->mmap_sz > f->io_size)
96 fmd->mmap_sz = f->io_size;
98 fmd->mmap_off = io_u->offset;
100 return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
104 * Attempt to mmap the entire file
106 static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u)
108 struct fio_file *f = io_u->file;
109 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
112 if (fio_file_partial_mmap(f))
114 if (io_u->offset != (size_t) io_u->offset ||
115 f->io_size != (size_t) f->io_size) {
116 fio_file_set_partial_mmap(f);
120 fmd->mmap_sz = f->io_size;
123 ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
125 fio_file_set_partial_mmap(f);
130 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
132 struct fio_file *f = io_u->file;
133 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
137 * It fits within existing mapping, use it
139 if (io_u->offset >= fmd->mmap_off &&
140 io_u->offset + io_u->buflen < fmd->mmap_off + fmd->mmap_sz)
144 * unmap any existing mapping
147 if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0)
149 fmd->mmap_ptr = NULL;
152 if (fio_mmapio_prep_full(td, io_u)) {
154 ret = fio_mmapio_prep_limited(td, io_u);
160 io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off -
165 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
167 struct fio_file *f = io_u->file;
168 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
170 fio_ro_check(td, io_u);
172 if (io_u->ddir == DDIR_READ)
173 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
174 else if (io_u->ddir == DDIR_WRITE)
175 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
176 else if (ddir_sync(io_u->ddir)) {
177 if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) {
179 td_verror(td, io_u->error, "msync");
181 } else if (io_u->ddir == DDIR_TRIM) {
182 int ret = do_io_u_trim(td, io_u);
185 td_verror(td, io_u->error, "trim");
190 * not really direct, but should drop the pages from the cache
192 if (td->o.odirect && ddir_rw(io_u->ddir)) {
193 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
195 td_verror(td, io_u->error, "msync");
197 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
199 td_verror(td, io_u->error, "madvise");
203 return FIO_Q_COMPLETED;
206 static int fio_mmapio_init(struct thread_data *td)
208 struct thread_options *o = &td->o;
210 if ((o->rw_min_bs & page_mask) &&
211 (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) {
212 log_err("fio: mmap options dictate a minimum block size of "
213 "%llu bytes\n", (unsigned long long) page_size);
217 mmap_map_size = MMAP_TOTAL_SZ / o->nr_files;
221 static int fio_mmapio_open_file(struct thread_data *td, struct fio_file *f)
223 struct fio_mmap_data *fmd;
226 ret = generic_open_file(td, f);
230 fmd = calloc(1, sizeof(*fmd));
232 int fio_unused __ret;
233 __ret = generic_close_file(td, f);
237 FILE_SET_ENG_DATA(f, fmd);
241 static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f)
243 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
245 FILE_SET_ENG_DATA(f, NULL);
247 fio_file_clear_partial_mmap(f);
249 return generic_close_file(td, f);
252 static struct ioengine_ops ioengine = {
254 .version = FIO_IOOPS_VERSION,
255 .init = fio_mmapio_init,
256 .prep = fio_mmapio_prep,
257 .queue = fio_mmapio_queue,
258 .open_file = fio_mmapio_open_file,
259 .close_file = fio_mmapio_close_file,
260 .get_file_size = generic_get_file_size,
261 .flags = FIO_SYNCIO | FIO_NOEXTEND,
264 static void fio_init fio_mmapio_register(void)
266 register_ioengine(&ioengine);
269 static void fio_exit fio_mmapio_unregister(void)
271 unregister_ioengine(&ioengine);