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