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