Let fadvise_hint also apply too mmap engine and madvise
[fio.git] / engines / mmap.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * mmap engine
3 *
4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
2866c82d
JA
6 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
2866c82d 12#include <sys/mman.h>
5f350952
JA
13
14#include "../fio.h"
4f5af7b2 15#include "../verify.h"
2866c82d 16
ac893112 17/*
420b104a 18 * Limits us to 1GiB of mapped files in total
ac893112 19 */
ff455a04 20#define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL)
2866c82d 21
ac893112 22static unsigned long mmap_map_size;
2866c82d 23
03a32636
JA
24struct fio_mmap_data {
25 void *mmap_ptr;
26 size_t mmap_sz;
27 off_t mmap_off;
28};
29
c712c97a
JA
30static bool fio_madvise_file(struct thread_data *td, struct fio_file *f,
31 size_t length)
32
33{
34 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
35
36 if (!td->o.fadvise_hint)
37 return true;
38
39 if (!td_random(td)) {
40 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) {
41 td_verror(td, errno, "madvise");
42 return false;
43 }
44 } else {
45 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) {
46 td_verror(td, errno, "madvise");
47 return false;
48 }
49 }
50 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_DONTNEED) < 0) {
51 td_verror(td, errno, "madvise");
52 return false;
53 }
54
55#ifdef FIO_MADV_FREE
56 if (f->filetype == FIO_TYPE_BLOCK)
57 (void) posix_madvise(fmd->mmap_ptr, fmd->mmap_sz, FIO_MADV_FREE);
58#endif
59
60 return true;
61}
62
ac893112
JA
63static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
64 size_t length, off_t off)
b5af8293 65{
e19ccb55 66 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
ac893112 67 int flags = 0;
c97d8369 68
81a1ee61 69 if (td_rw(td) && !td->o.verify_only)
b5af8293 70 flags = PROT_READ | PROT_WRITE;
81a1ee61 71 else if (td_write(td) && !td->o.verify_only) {
b5af8293
JA
72 flags = PROT_WRITE;
73
2dc1bbeb 74 if (td->o.verify != VERIFY_NONE)
b5af8293
JA
75 flags |= PROT_READ;
76 } else
77 flags = PROT_READ;
78
03a32636
JA
79 fmd->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
80 if (fmd->mmap_ptr == MAP_FAILED) {
81 fmd->mmap_ptr = NULL;
ed47cbf7 82 td_verror(td, errno, "mmap");
b5af8293
JA
83 goto err;
84 }
85
c712c97a 86 if (!fio_madvise_file(td, f, length))
ecbfcd94 87 goto err;
ecbfcd94 88
b5af8293 89err:
03a32636
JA
90 if (td->error && fmd->mmap_ptr)
91 munmap(fmd->mmap_ptr, length);
93bcfd20 92
ed47cbf7 93 return td->error;
b5af8293
JA
94}
95
ed47cbf7
JA
96/*
97 * Just mmap an appropriate portion, we cannot mmap the full extent
98 */
99static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
b5af8293 100{
ac893112 101 struct fio_file *f = io_u->file;
e19ccb55 102 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
6977bcd0 103
ac893112
JA
104 if (io_u->buflen > mmap_map_size) {
105 log_err("fio: bs too big for mmap engine\n");
ed47cbf7 106 return EIO;
ac893112
JA
107 }
108
03a32636
JA
109 fmd->mmap_sz = mmap_map_size;
110 if (fmd->mmap_sz > f->io_size)
111 fmd->mmap_sz = f->io_size;
6977bcd0 112
03a32636 113 fmd->mmap_off = io_u->offset;
ac893112 114
03a32636 115 return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
ed47cbf7
JA
116}
117
118/*
119 * Attempt to mmap the entire file
120 */
121static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u)
122{
123 struct fio_file *f = io_u->file;
e19ccb55 124 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
ed47cbf7
JA
125 int ret;
126
127 if (fio_file_partial_mmap(f))
128 return EINVAL;
01626837
JA
129 if (io_u->offset != (size_t) io_u->offset ||
130 f->io_size != (size_t) f->io_size) {
131 fio_file_set_partial_mmap(f);
132 return EINVAL;
133 }
ed47cbf7 134
03a32636
JA
135 fmd->mmap_sz = f->io_size;
136 fmd->mmap_off = 0;
ed47cbf7 137
03a32636 138 ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
ed47cbf7
JA
139 if (ret)
140 fio_file_set_partial_mmap(f);
141
6977bcd0 142 return ret;
b5af8293
JA
143}
144
ed47cbf7
JA
145static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
146{
147 struct fio_file *f = io_u->file;
e19ccb55 148 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
ed47cbf7
JA
149 int ret;
150
8f933cae
JA
151 /*
152 * It fits within existing mapping, use it
153 */
03a32636 154 if (io_u->offset >= fmd->mmap_off &&
f23ff35e 155 io_u->offset + io_u->buflen <= fmd->mmap_off + fmd->mmap_sz)
ed47cbf7
JA
156 goto done;
157
8f933cae
JA
158 /*
159 * unmap any existing mapping
160 */
03a32636
JA
161 if (fmd->mmap_ptr) {
162 if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0)
8f933cae 163 return errno;
03a32636 164 fmd->mmap_ptr = NULL;
8f933cae
JA
165 }
166
ed47cbf7
JA
167 if (fio_mmapio_prep_full(td, io_u)) {
168 td_clear_error(td);
169 ret = fio_mmapio_prep_limited(td, io_u);
170 if (ret)
171 return ret;
172 }
173
174done:
03a32636 175 io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off -
ed47cbf7
JA
176 f->file_offset;
177 return 0;
178}
179
ac893112
JA
180static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
181{
182 struct fio_file *f = io_u->file;
e19ccb55 183 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
ac893112
JA
184
185 fio_ro_check(td, io_u);
186
187 if (io_u->ddir == DDIR_READ)
188 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
189 else if (io_u->ddir == DDIR_WRITE)
190 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
5f9099ea 191 else if (ddir_sync(io_u->ddir)) {
03a32636 192 if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) {
ac893112
JA
193 io_u->error = errno;
194 td_verror(td, io_u->error, "msync");
195 }
ff58fced
JA
196 } else if (io_u->ddir == DDIR_TRIM) {
197 int ret = do_io_u_trim(td, io_u);
198
199 if (!ret)
200 td_verror(td, io_u->error, "trim");
ac893112
JA
201 }
202
ff58fced 203
ac893112
JA
204 /*
205 * not really direct, but should drop the pages from the cache
206 */
ff58fced 207 if (td->o.odirect && ddir_rw(io_u->ddir)) {
ac893112
JA
208 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
209 io_u->error = errno;
210 td_verror(td, io_u->error, "msync");
211 }
03e20d68 212 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
ac893112
JA
213 io_u->error = errno;
214 td_verror(td, io_u->error, "madvise");
215 }
216 }
217
218 return FIO_Q_COMPLETED;
219}
220
221static int fio_mmapio_init(struct thread_data *td)
222{
913ea0db 223 struct thread_options *o = &td->o;
ac893112 224
487197d9 225 if ((o->rw_min_bs & page_mask) &&
913ea0db
JA
226 (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) {
227 log_err("fio: mmap options dictate a minimum block size of "
4e0a8fa2 228 "%llu bytes\n", (unsigned long long) page_size);
913ea0db
JA
229 return 1;
230 }
231
487197d9 232 mmap_map_size = MMAP_TOTAL_SZ / o->nr_files;
ac893112
JA
233 return 0;
234}
235
03a32636
JA
236static int fio_mmapio_open_file(struct thread_data *td, struct fio_file *f)
237{
238 struct fio_mmap_data *fmd;
239 int ret;
240
241 ret = generic_open_file(td, f);
242 if (ret)
243 return ret;
244
245 fmd = calloc(1, sizeof(*fmd));
246 if (!fmd) {
8a68c41c
JA
247 int fio_unused __ret;
248 __ret = generic_close_file(td, f);
03a32636
JA
249 return 1;
250 }
251
e19ccb55 252 FILE_SET_ENG_DATA(f, fmd);
03a32636
JA
253 return 0;
254}
255
256static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f)
257{
e19ccb55 258 struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
03a32636 259
e19ccb55 260 FILE_SET_ENG_DATA(f, NULL);
03a32636 261 free(fmd);
965f0a41 262 fio_file_clear_partial_mmap(f);
03a32636
JA
263
264 return generic_close_file(td, f);
265}
266
5f350952 267static struct ioengine_ops ioengine = {
2866c82d
JA
268 .name = "mmap",
269 .version = FIO_IOOPS_VERSION,
ac893112
JA
270 .init = fio_mmapio_init,
271 .prep = fio_mmapio_prep,
2866c82d 272 .queue = fio_mmapio_queue,
03a32636
JA
273 .open_file = fio_mmapio_open_file,
274 .close_file = fio_mmapio_close_file,
df9c26b1 275 .get_file_size = generic_get_file_size,
0263882a 276 .flags = FIO_SYNCIO | FIO_NOEXTEND,
2866c82d 277};
5f350952
JA
278
279static void fio_init fio_mmapio_register(void)
280{
281 register_ioengine(&ioengine);
282}
283
284static void fio_exit fio_mmapio_unregister(void)
285{
286 unregister_ioengine(&ioengine);
287}