blob: a5e0c6fc25646329336bd91f35759bcec6f3d709 [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 Axboeac893112009-06-02 13:06:01 +020016/*
17 * Limits us to 2GB of mapped files in total
18 */
19#define MMAP_TOTAL_SZ (2 * 1024 * 1024 * 1024UL)
20
21static unsigned long mmap_map_size;
22static unsigned long mmap_map_mask;
23
24static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
25 size_t length, off_t off)
Jens Axboe2866c822006-10-09 15:57:48 +020026{
Jens Axboeac893112009-06-02 13:06:01 +020027 int flags = 0;
28 int ret = 0;
Jens Axboec97d8362007-05-21 14:52:43 +020029
Jens Axboeb5af8292007-03-08 12:43:13 +010030 if (td_rw(td))
31 flags = PROT_READ | PROT_WRITE;
32 else if (td_write(td)) {
33 flags = PROT_WRITE;
34
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010035 if (td->o.verify != VERIFY_NONE)
Jens Axboeb5af8292007-03-08 12:43:13 +010036 flags |= PROT_READ;
37 } else
38 flags = PROT_READ;
39
Jens Axboeac893112009-06-02 13:06:01 +020040 f->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
41 if (f->mmap_ptr == MAP_FAILED) {
Jens Axboe8de43fd2009-05-20 10:49:44 +020042 int err = errno;
43
Jens Axboeac893112009-06-02 13:06:01 +020044 f->mmap_ptr = NULL;
Jens Axboe8de43fd2009-05-20 10:49:44 +020045 td_verror(td, err, "mmap");
Jens Axboeb5af8292007-03-08 12:43:13 +010046 goto err;
47 }
48
49 if (file_invalidate_cache(td, f))
50 goto err;
51
52 if (!td_random(td)) {
Jens Axboeac893112009-06-02 13:06:01 +020053 if (madvise(f->mmap_ptr, length, MADV_SEQUENTIAL) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010054 td_verror(td, errno, "madvise");
55 goto err;
56 }
57 } else {
Jens Axboeac893112009-06-02 13:06:01 +020058 if (madvise(f->mmap_ptr, length, MADV_RANDOM) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010059 td_verror(td, errno, "madvise");
60 goto err;
61 }
62 }
63
Jens Axboeb5af8292007-03-08 12:43:13 +010064err:
Jens Axboeac893112009-06-02 13:06:01 +020065 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +010066}
67
Jens Axboeac893112009-06-02 13:06:01 +020068static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
Jens Axboeb5af8292007-03-08 12:43:13 +010069{
Jens Axboeac893112009-06-02 13:06:01 +020070 struct fio_file *f = io_u->file;
71 int ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +010072
Jens Axboeac893112009-06-02 13:06:01 +020073 if (io_u->buflen > mmap_map_size) {
74 log_err("fio: bs too big for mmap engine\n");
75 ret = EIO;
76 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +010077 }
Jens Axboe6977bcd2008-03-01 15:55:36 +010078
Jens Axboeac893112009-06-02 13:06:01 +020079 if (io_u->offset >= f->mmap_off &&
80 io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz)
81 goto done;
Jens Axboe6977bcd2008-03-01 15:55:36 +010082
Jens Axboeac893112009-06-02 13:06:01 +020083 if (f->mmap_ptr) {
84 if (munmap(f->mmap_ptr, f->mmap_sz) < 0) {
85 ret = errno;
86 goto err;
87 }
88 f->mmap_ptr = NULL;
89 }
90
91 f->mmap_sz = mmap_map_size;
92 if (f->mmap_sz > f->io_size)
93 f->mmap_sz = f->io_size;
94
95 f->mmap_off = io_u->offset & ~mmap_map_mask;
96 if (io_u->offset + io_u->buflen >= f->mmap_off + f->mmap_sz)
97 f->mmap_off -= io_u->buflen;
98
99 ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
100done:
101 if (!ret)
102 io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off -
103 f->file_offset;
104err:
Jens Axboe6977bcd2008-03-01 15:55:36 +0100105 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +0100106}
107
Jens Axboeac893112009-06-02 13:06:01 +0200108static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
109{
110 struct fio_file *f = io_u->file;
111
112 fio_ro_check(td, io_u);
113
114 if (io_u->ddir == DDIR_READ)
115 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
116 else if (io_u->ddir == DDIR_WRITE)
117 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
118 else if (io_u->ddir == DDIR_SYNC) {
119 if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) {
120 io_u->error = errno;
121 td_verror(td, io_u->error, "msync");
122 }
123 }
124
125 /*
126 * not really direct, but should drop the pages from the cache
127 */
128 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
129 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
130 io_u->error = errno;
131 td_verror(td, io_u->error, "msync");
132 }
133 if (madvise(io_u->mmap_data, io_u->xfer_buflen, MADV_DONTNEED) < 0) {
134 io_u->error = errno;
135 td_verror(td, io_u->error, "madvise");
136 }
137 }
138
139 return FIO_Q_COMPLETED;
140}
141
142static int fio_mmapio_init(struct thread_data *td)
143{
144 unsigned long shift, mask;
145
146 mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
147 mask = mmap_map_size;
148 shift = 0;
149 do {
150 mask >>= 1;
151 if (!mask)
152 break;
153 shift++;
154 } while (1);
155
156 mmap_map_mask = 1UL << shift;
157 return 0;
158}
159
Jens Axboe5f350952006-11-07 15:20:59 +0100160static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200161 .name = "mmap",
162 .version = FIO_IOOPS_VERSION,
Jens Axboeac893112009-06-02 13:06:01 +0200163 .init = fio_mmapio_init,
164 .prep = fio_mmapio_prep,
Jens Axboe2866c822006-10-09 15:57:48 +0200165 .queue = fio_mmapio_queue,
Jens Axboeac893112009-06-02 13:06:01 +0200166 .open_file = generic_open_file,
167 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100168 .get_file_size = generic_get_file_size,
Jens Axboe02638822007-03-12 09:25:55 +0100169 .flags = FIO_SYNCIO | FIO_NOEXTEND,
Jens Axboe2866c822006-10-09 15:57:48 +0200170};
Jens Axboe5f350952006-11-07 15:20:59 +0100171
172static void fio_init fio_mmapio_register(void)
173{
174 register_ioengine(&ioengine);
175}
176
177static void fio_exit fio_mmapio_unregister(void)
178{
179 unregister_ioengine(&ioengine);
180}