blob: 69add78a2f95b9d72dea9f093ef174667f09958c [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 Axboe4f5af7b2009-06-03 08:45:40 +020015#include "../verify.h"
Jens Axboe2866c822006-10-09 15:57:48 +020016
Jens Axboeac893112009-06-02 13:06:01 +020017/*
Jens Axboeff455a02009-06-29 11:54:56 +020018 * Limits us to 1GB of mapped files in total
Jens Axboeac893112009-06-02 13:06:01 +020019 */
Jens Axboeff455a02009-06-29 11:54:56 +020020#define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL)
Jens Axboeac893112009-06-02 13:06:01 +020021
22static unsigned long mmap_map_size;
23static unsigned long mmap_map_mask;
24
Jens Axboeeea6bed2014-12-14 19:17:27 -070025struct fio_mmap_data {
26 void *mmap_ptr;
27 size_t mmap_sz;
28 off_t mmap_off;
29};
30
Jens Axboeac893112009-06-02 13:06:01 +020031static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
32 size_t length, off_t off)
Jens Axboe2866c822006-10-09 15:57:48 +020033{
Jens Axboeaa31de72014-12-15 08:26:11 -070034 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
Jens Axboeac893112009-06-02 13:06:01 +020035 int flags = 0;
Jens Axboec97d8362007-05-21 14:52:43 +020036
Jens Axboeb5af8292007-03-08 12:43:13 +010037 if (td_rw(td))
38 flags = PROT_READ | PROT_WRITE;
39 else if (td_write(td)) {
40 flags = PROT_WRITE;
41
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010042 if (td->o.verify != VERIFY_NONE)
Jens Axboeb5af8292007-03-08 12:43:13 +010043 flags |= PROT_READ;
44 } else
45 flags = PROT_READ;
46
Jens Axboeeea6bed2014-12-14 19:17:27 -070047 fmd->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
48 if (fmd->mmap_ptr == MAP_FAILED) {
49 fmd->mmap_ptr = NULL;
Jens Axboeed47cbf2009-07-03 22:52:38 +020050 td_verror(td, errno, "mmap");
Jens Axboeb5af8292007-03-08 12:43:13 +010051 goto err;
52 }
53
Jens Axboeb5af8292007-03-08 12:43:13 +010054 if (!td_random(td)) {
Jens Axboeeea6bed2014-12-14 19:17:27 -070055 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010056 td_verror(td, errno, "madvise");
57 goto err;
58 }
59 } else {
Jens Axboeeea6bed2014-12-14 19:17:27 -070060 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010061 td_verror(td, errno, "madvise");
62 goto err;
63 }
64 }
Yoshinori Sato06f63e52015-01-07 12:16:36 +090065 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_DONTNEED) < 0) {
66 td_verror(td, errno, "madvise");
67 goto err;
68 }
69
70#ifdef FIO_MADV_FREE
71 if (f->filetype == FIO_TYPE_BD)
72 (void) posix_madvise(fmd->mmap_ptr, fmd->mmap_sz, FIO_MADV_FREE);
73#endif
74
Jens Axboeb5af8292007-03-08 12:43:13 +010075
Jens Axboeb5af8292007-03-08 12:43:13 +010076err:
Jens Axboeeea6bed2014-12-14 19:17:27 -070077 if (td->error && fmd->mmap_ptr)
78 munmap(fmd->mmap_ptr, length);
Bruce Cran93bcfd22012-02-20 20:18:19 +010079
Jens Axboeed47cbf2009-07-03 22:52:38 +020080 return td->error;
Jens Axboeb5af8292007-03-08 12:43:13 +010081}
82
Jens Axboeed47cbf2009-07-03 22:52:38 +020083/*
84 * Just mmap an appropriate portion, we cannot mmap the full extent
85 */
86static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
Jens Axboeb5af8292007-03-08 12:43:13 +010087{
Jens Axboeac893112009-06-02 13:06:01 +020088 struct fio_file *f = io_u->file;
Jens Axboeaa31de72014-12-15 08:26:11 -070089 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
Jens Axboe6977bcd2008-03-01 15:55:36 +010090
Jens Axboeac893112009-06-02 13:06:01 +020091 if (io_u->buflen > mmap_map_size) {
92 log_err("fio: bs too big for mmap engine\n");
Jens Axboeed47cbf2009-07-03 22:52:38 +020093 return EIO;
Jens Axboeb5af8292007-03-08 12:43:13 +010094 }
Jens Axboe6977bcd2008-03-01 15:55:36 +010095
Jens Axboeeea6bed2014-12-14 19:17:27 -070096 fmd->mmap_sz = mmap_map_size;
97 if (fmd->mmap_sz > f->io_size)
98 fmd->mmap_sz = f->io_size;
Jens Axboeac893112009-06-02 13:06:01 +020099
Jens Axboeeea6bed2014-12-14 19:17:27 -0700100 fmd->mmap_off = io_u->offset;
Jens Axboeac893112009-06-02 13:06:01 +0200101
Jens Axboeeea6bed2014-12-14 19:17:27 -0700102 return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
Jens Axboeed47cbf2009-07-03 22:52:38 +0200103}
104
105/*
106 * Attempt to mmap the entire file
107 */
108static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u)
109{
110 struct fio_file *f = io_u->file;
Jens Axboeaa31de72014-12-15 08:26:11 -0700111 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
Jens Axboeed47cbf2009-07-03 22:52:38 +0200112 int ret;
113
114 if (fio_file_partial_mmap(f))
115 return EINVAL;
Jens Axboee8f198b2014-12-16 10:35:07 -0700116 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);
119 return EINVAL;
120 }
Jens Axboeed47cbf2009-07-03 22:52:38 +0200121
Jens Axboeeea6bed2014-12-14 19:17:27 -0700122 fmd->mmap_sz = f->io_size;
123 fmd->mmap_off = 0;
Jens Axboeed47cbf2009-07-03 22:52:38 +0200124
Jens Axboeeea6bed2014-12-14 19:17:27 -0700125 ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
Jens Axboeed47cbf2009-07-03 22:52:38 +0200126 if (ret)
127 fio_file_set_partial_mmap(f);
128
Jens Axboe6977bcd2008-03-01 15:55:36 +0100129 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +0100130}
131
Jens Axboeed47cbf2009-07-03 22:52:38 +0200132static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
133{
134 struct fio_file *f = io_u->file;
Jens Axboeaa31de72014-12-15 08:26:11 -0700135 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
Jens Axboeed47cbf2009-07-03 22:52:38 +0200136 int ret;
137
Jens Axboe8f933ca2009-07-04 20:29:04 +0200138 /*
139 * It fits within existing mapping, use it
140 */
Jens Axboeeea6bed2014-12-14 19:17:27 -0700141 if (io_u->offset >= fmd->mmap_off &&
142 io_u->offset + io_u->buflen < fmd->mmap_off + fmd->mmap_sz)
Jens Axboeed47cbf2009-07-03 22:52:38 +0200143 goto done;
144
Jens Axboe8f933ca2009-07-04 20:29:04 +0200145 /*
146 * unmap any existing mapping
147 */
Jens Axboeeea6bed2014-12-14 19:17:27 -0700148 if (fmd->mmap_ptr) {
149 if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0)
Jens Axboe8f933ca2009-07-04 20:29:04 +0200150 return errno;
Jens Axboeeea6bed2014-12-14 19:17:27 -0700151 fmd->mmap_ptr = NULL;
Jens Axboe8f933ca2009-07-04 20:29:04 +0200152 }
153
Jens Axboeed47cbf2009-07-03 22:52:38 +0200154 if (fio_mmapio_prep_full(td, io_u)) {
155 td_clear_error(td);
156 ret = fio_mmapio_prep_limited(td, io_u);
157 if (ret)
158 return ret;
159 }
160
161done:
Jens Axboeeea6bed2014-12-14 19:17:27 -0700162 io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off -
Jens Axboeed47cbf2009-07-03 22:52:38 +0200163 f->file_offset;
164 return 0;
165}
166
Jens Axboeac893112009-06-02 13:06:01 +0200167static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
168{
169 struct fio_file *f = io_u->file;
Jens Axboeaa31de72014-12-15 08:26:11 -0700170 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
Jens Axboeac893112009-06-02 13:06:01 +0200171
172 fio_ro_check(td, io_u);
173
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);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200178 else if (ddir_sync(io_u->ddir)) {
Jens Axboeeea6bed2014-12-14 19:17:27 -0700179 if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) {
Jens Axboeac893112009-06-02 13:06:01 +0200180 io_u->error = errno;
181 td_verror(td, io_u->error, "msync");
182 }
Jens Axboeff58fce2010-08-25 12:02:08 +0200183 } else if (io_u->ddir == DDIR_TRIM) {
184 int ret = do_io_u_trim(td, io_u);
185
186 if (!ret)
187 td_verror(td, io_u->error, "trim");
Jens Axboeac893112009-06-02 13:06:01 +0200188 }
189
Jens Axboeff58fce2010-08-25 12:02:08 +0200190
Jens Axboeac893112009-06-02 13:06:01 +0200191 /*
192 * not really direct, but should drop the pages from the cache
193 */
Jens Axboeff58fce2010-08-25 12:02:08 +0200194 if (td->o.odirect && ddir_rw(io_u->ddir)) {
Jens Axboeac893112009-06-02 13:06:01 +0200195 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
196 io_u->error = errno;
197 td_verror(td, io_u->error, "msync");
198 }
Bruce Cran03e20d62011-01-02 20:14:54 +0100199 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
Jens Axboeac893112009-06-02 13:06:01 +0200200 io_u->error = errno;
201 td_verror(td, io_u->error, "madvise");
202 }
203 }
204
205 return FIO_Q_COMPLETED;
206}
207
208static int fio_mmapio_init(struct thread_data *td)
209{
Jens Axboe913ea0d2011-01-22 15:11:13 -0700210 struct thread_options *o = &td->o;
Jens Axboeac893112009-06-02 13:06:01 +0200211 unsigned long shift, mask;
212
Jens Axboe913ea0d2011-01-22 15:11:13 -0700213 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 "
Jens Axboe4e0a8fa2013-04-15 11:40:57 +0200216 "%llu bytes\n", (unsigned long long) page_size);
Jens Axboe913ea0d2011-01-22 15:11:13 -0700217 return 1;
218 }
219
Jens Axboeac893112009-06-02 13:06:01 +0200220 mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
221 mask = mmap_map_size;
222 shift = 0;
223 do {
224 mask >>= 1;
225 if (!mask)
226 break;
227 shift++;
228 } while (1);
Bruce Cran93bcfd22012-02-20 20:18:19 +0100229
Jens Axboeac893112009-06-02 13:06:01 +0200230 mmap_map_mask = 1UL << shift;
231 return 0;
232}
233
Jens Axboeeea6bed2014-12-14 19:17:27 -0700234static int fio_mmapio_open_file(struct thread_data *td, struct fio_file *f)
235{
236 struct fio_mmap_data *fmd;
237 int ret;
238
239 ret = generic_open_file(td, f);
240 if (ret)
241 return ret;
242
243 fmd = calloc(1, sizeof(*fmd));
244 if (!fmd) {
245 int fio_unused ret;
246 ret = generic_close_file(td, f);
247 return 1;
248 }
249
Jens Axboeaa31de72014-12-15 08:26:11 -0700250 FILE_SET_ENG_DATA(f, fmd);
Jens Axboeeea6bed2014-12-14 19:17:27 -0700251 return 0;
252}
253
254static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f)
255{
Jens Axboeaa31de72014-12-15 08:26:11 -0700256 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
Jens Axboeeea6bed2014-12-14 19:17:27 -0700257
Jens Axboeaa31de72014-12-15 08:26:11 -0700258 FILE_SET_ENG_DATA(f, NULL);
Jens Axboeeea6bed2014-12-14 19:17:27 -0700259 free(fmd);
Jens Axboee1243902014-12-16 10:46:24 -0700260 fio_file_clear_partial_mmap(f);
Jens Axboeeea6bed2014-12-14 19:17:27 -0700261
262 return generic_close_file(td, f);
263}
264
Jens Axboe5f350952006-11-07 15:20:59 +0100265static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200266 .name = "mmap",
267 .version = FIO_IOOPS_VERSION,
Jens Axboeac893112009-06-02 13:06:01 +0200268 .init = fio_mmapio_init,
269 .prep = fio_mmapio_prep,
Jens Axboe2866c822006-10-09 15:57:48 +0200270 .queue = fio_mmapio_queue,
Jens Axboeeea6bed2014-12-14 19:17:27 -0700271 .open_file = fio_mmapio_open_file,
272 .close_file = fio_mmapio_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100273 .get_file_size = generic_get_file_size,
Jens Axboe02638822007-03-12 09:25:55 +0100274 .flags = FIO_SYNCIO | FIO_NOEXTEND,
Jens Axboe2866c822006-10-09 15:57:48 +0200275};
Jens Axboe5f350952006-11-07 15:20:59 +0100276
277static void fio_init fio_mmapio_register(void)
278{
279 register_ioengine(&ioengine);
280}
281
282static void fio_exit fio_mmapio_unregister(void)
283{
284 unregister_ioengine(&ioengine);
285}