4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
17 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
19 struct fio_file *f = io_u->file;
20 unsigned long long real_off = io_u->offset - f->file_offset;
22 if (io_u->ddir == DDIR_READ)
23 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
24 else if (io_u->ddir == DDIR_WRITE)
25 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
26 else if (io_u->ddir == DDIR_SYNC) {
27 size_t len = (f->io_size + page_size - 1) & ~page_mask;
29 if (msync(f->mmap, len, MS_SYNC)) {
31 td_verror(td, io_u->error, "msync");
36 * not really direct, but should drop the pages from the cache
38 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
39 size_t len = (io_u->xfer_buflen + page_size - 1) & ~page_mask;
40 unsigned long long off = real_off & ~page_mask;
42 if (msync(f->mmap + off, len, MS_SYNC) < 0) {
44 td_verror(td, io_u->error, "msync");
46 if (madvise(f->mmap + off, len, MADV_DONTNEED) < 0) {
48 td_verror(td, io_u->error, "madvise");
52 return FIO_Q_COMPLETED;
55 static int fio_mmapio_open(struct thread_data *td, struct fio_file *f)
59 ret = generic_open_file(td, f);
64 * for size checkup, don't mmap anything.
70 flags = PROT_READ | PROT_WRITE;
71 else if (td_write(td)) {
74 if (td->o.verify != VERIFY_NONE)
79 f->mmap = mmap(NULL, f->io_size, flags, MAP_SHARED, f->fd, f->file_offset);
80 if (f->mmap == MAP_FAILED) {
82 td_verror(td, errno, "mmap");
86 if (file_invalidate_cache(td, f))
90 if (madvise(f->mmap, f->io_size, MADV_SEQUENTIAL) < 0) {
91 td_verror(td, errno, "madvise");
95 if (madvise(f->mmap, f->io_size, MADV_RANDOM) < 0) {
96 td_verror(td, errno, "madvise");
104 td->io_ops->close_file(td, f);
108 static void fio_mmapio_close(struct thread_data fio_unused *td,
112 munmap(f->mmap, f->io_size);
115 generic_close_file(td, f);
118 static struct ioengine_ops ioengine = {
120 .version = FIO_IOOPS_VERSION,
121 .queue = fio_mmapio_queue,
122 .open_file = fio_mmapio_open,
123 .close_file = fio_mmapio_close,
124 .flags = FIO_SYNCIO | FIO_NOEXTEND,
127 static void fio_init fio_mmapio_register(void)
129 register_ioengine(&ioengine);
132 static void fio_exit fio_mmapio_unregister(void)
134 unregister_ioengine(&ioengine);