blob: 5b8d800c4190177ff33db12681a87c2659105761 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * mmap engine
3 *
4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
Jens Axboe2866c822006-10-09 15:57:48 +02006 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
Jens Axboe2866c822006-10-09 15:57:48 +020012#include <sys/mman.h>
Jens Axboe5f350952006-11-07 15:20:59 +010013
14#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020015
Jens Axboe2866c822006-10-09 15:57:48 +020016static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
17{
Jens Axboe53cdc682006-10-18 11:50:58 +020018 struct fio_file *f = io_u->file;
19 unsigned long long real_off = io_u->offset - f->file_offset;
Jens Axboe2866c822006-10-09 15:57:48 +020020
Jens Axboe7101d9c2007-09-12 13:12:39 +020021 fio_ro_check(td, io_u);
22
Jens Axboe2866c822006-10-09 15:57:48 +020023 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010024 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020025 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010026 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboeb907a5b2006-10-24 14:46:13 +020027 else if (io_u->ddir == DDIR_SYNC) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020028 size_t len = (f->io_size + page_size - 1) & ~page_mask;
Jens Axboecfc99db2007-03-14 10:34:47 +010029
30 if (msync(f->mmap, len, MS_SYNC)) {
Jens Axboeb907a5b2006-10-24 14:46:13 +020031 io_u->error = errno;
Jens Axboecfc99db2007-03-14 10:34:47 +010032 td_verror(td, io_u->error, "msync");
33 }
Jens Axboeb907a5b2006-10-24 14:46:13 +020034 }
Jens Axboe2866c822006-10-09 15:57:48 +020035
36 /*
37 * not really direct, but should drop the pages from the cache
38 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010039 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
Jens Axboecfc99db2007-03-14 10:34:47 +010040 size_t len = (io_u->xfer_buflen + page_size - 1) & ~page_mask;
41 unsigned long long off = real_off & ~page_mask;
Jens Axboe2866c822006-10-09 15:57:48 +020042
Jens Axboecfc99db2007-03-14 10:34:47 +010043 if (msync(f->mmap + off, len, MS_SYNC) < 0) {
44 io_u->error = errno;
45 td_verror(td, io_u->error, "msync");
46 }
47 if (madvise(f->mmap + off, len, MADV_DONTNEED) < 0) {
48 io_u->error = errno;
49 td_verror(td, io_u->error, "madvise");
50 }
51 }
Jens Axboe2866c822006-10-09 15:57:48 +020052
Jens Axboe36167d82007-02-18 05:41:31 +010053 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020054}
55
Jens Axboeb5af8292007-03-08 12:43:13 +010056static int fio_mmapio_open(struct thread_data *td, struct fio_file *f)
57{
58 int ret, flags;
59
60 ret = generic_open_file(td, f);
61 if (ret)
62 return ret;
63
Jens Axboec97d8362007-05-21 14:52:43 +020064 /*
65 * for size checkup, don't mmap anything.
66 */
67 if (!f->io_size)
68 return 0;
69
Jens Axboeb5af8292007-03-08 12:43:13 +010070 if (td_rw(td))
71 flags = PROT_READ | PROT_WRITE;
72 else if (td_write(td)) {
73 flags = PROT_WRITE;
74
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010075 if (td->o.verify != VERIFY_NONE)
Jens Axboeb5af8292007-03-08 12:43:13 +010076 flags |= PROT_READ;
77 } else
78 flags = PROT_READ;
79
Jens Axboe7bb48f82007-03-27 15:30:28 +020080 f->mmap = mmap(NULL, f->io_size, flags, MAP_SHARED, f->fd, f->file_offset);
Jens Axboeb5af8292007-03-08 12:43:13 +010081 if (f->mmap == MAP_FAILED) {
Jens Axboe8de43fd2009-05-20 10:49:44 +020082 int err = errno;
83
Jens Axboeb5af8292007-03-08 12:43:13 +010084 f->mmap = NULL;
Jens Axboe8de43fd2009-05-20 10:49:44 +020085 td_verror(td, err, "mmap");
86 if (err == EINVAL && f->io_size > 2*1024*1024*1024UL)
87 log_err("fio: mmap size likely too large\n");
Jens Axboeb5af8292007-03-08 12:43:13 +010088 goto err;
89 }
90
91 if (file_invalidate_cache(td, f))
92 goto err;
93
94 if (!td_random(td)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020095 if (madvise(f->mmap, f->io_size, MADV_SEQUENTIAL) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010096 td_verror(td, errno, "madvise");
97 goto err;
98 }
99 } else {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200100 if (madvise(f->mmap, f->io_size, MADV_RANDOM) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100101 td_verror(td, errno, "madvise");
102 goto err;
103 }
104 }
105
106 return 0;
107
108err:
Jens Axboe02638822007-03-12 09:25:55 +0100109 td->io_ops->close_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100110 return 1;
111}
112
Jens Axboe6977bcd2008-03-01 15:55:36 +0100113static int fio_mmapio_close(struct thread_data fio_unused *td,
114 struct fio_file *f)
Jens Axboeb5af8292007-03-08 12:43:13 +0100115{
Jens Axboe6977bcd2008-03-01 15:55:36 +0100116 int ret = 0, ret2;
117
Jens Axboeb5af8292007-03-08 12:43:13 +0100118 if (f->mmap) {
Jens Axboe6977bcd2008-03-01 15:55:36 +0100119 if (munmap(f->mmap, f->io_size) < 0)
120 ret = errno;
121
Jens Axboeb5af8292007-03-08 12:43:13 +0100122 f->mmap = NULL;
123 }
Jens Axboe6977bcd2008-03-01 15:55:36 +0100124
125 ret2 = generic_close_file(td, f);
126 if (!ret && ret2)
127 ret = ret2;
128
129 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +0100130}
131
Jens Axboe5f350952006-11-07 15:20:59 +0100132static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200133 .name = "mmap",
134 .version = FIO_IOOPS_VERSION,
Jens Axboe2866c822006-10-09 15:57:48 +0200135 .queue = fio_mmapio_queue,
Jens Axboeb5af8292007-03-08 12:43:13 +0100136 .open_file = fio_mmapio_open,
137 .close_file = fio_mmapio_close,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100138 .get_file_size = generic_get_file_size,
Jens Axboe02638822007-03-12 09:25:55 +0100139 .flags = FIO_SYNCIO | FIO_NOEXTEND,
Jens Axboe2866c822006-10-09 15:57:48 +0200140};
Jens Axboe5f350952006-11-07 15:20:59 +0100141
142static void fio_init fio_mmapio_register(void)
143{
144 register_ioengine(&ioengine);
145}
146
147static void fio_exit fio_mmapio_unregister(void)
148{
149 unregister_ioengine(&ioengine);
150}