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 | } | |
ecbfcd94 YS |
65 | 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 | ||
b5af8293 | 75 | |
b5af8293 | 76 | err: |
03a32636 JA |
77 | if (td->error && fmd->mmap_ptr) |
78 | munmap(fmd->mmap_ptr, length); | |
93bcfd20 | 79 | |
ed47cbf7 | 80 | return td->error; |
b5af8293 JA |
81 | } |
82 | ||
ed47cbf7 JA |
83 | /* |
84 | * Just mmap an appropriate portion, we cannot mmap the full extent | |
85 | */ | |
86 | static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u) | |
b5af8293 | 87 | { |
ac893112 | 88 | struct fio_file *f = io_u->file; |
e19ccb55 | 89 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
6977bcd0 | 90 | |
ac893112 JA |
91 | if (io_u->buflen > mmap_map_size) { |
92 | log_err("fio: bs too big for mmap engine\n"); | |
ed47cbf7 | 93 | return EIO; |
ac893112 JA |
94 | } |
95 | ||
03a32636 JA |
96 | fmd->mmap_sz = mmap_map_size; |
97 | if (fmd->mmap_sz > f->io_size) | |
98 | fmd->mmap_sz = f->io_size; | |
6977bcd0 | 99 | |
03a32636 | 100 | fmd->mmap_off = io_u->offset; |
ac893112 | 101 | |
03a32636 | 102 | return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off); |
ed47cbf7 JA |
103 | } |
104 | ||
105 | /* | |
106 | * Attempt to mmap the entire file | |
107 | */ | |
108 | static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u) | |
109 | { | |
110 | struct fio_file *f = io_u->file; | |
e19ccb55 | 111 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
ed47cbf7 JA |
112 | int ret; |
113 | ||
114 | if (fio_file_partial_mmap(f)) | |
115 | return EINVAL; | |
01626837 JA |
116 | 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 | } | |
ed47cbf7 | 121 | |
03a32636 JA |
122 | fmd->mmap_sz = f->io_size; |
123 | fmd->mmap_off = 0; | |
ed47cbf7 | 124 | |
03a32636 | 125 | ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off); |
ed47cbf7 JA |
126 | if (ret) |
127 | fio_file_set_partial_mmap(f); | |
128 | ||
6977bcd0 | 129 | return ret; |
b5af8293 JA |
130 | } |
131 | ||
ed47cbf7 JA |
132 | static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u) |
133 | { | |
134 | struct fio_file *f = io_u->file; | |
e19ccb55 | 135 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
ed47cbf7 JA |
136 | int ret; |
137 | ||
8f933cae JA |
138 | /* |
139 | * It fits within existing mapping, use it | |
140 | */ | |
03a32636 JA |
141 | if (io_u->offset >= fmd->mmap_off && |
142 | io_u->offset + io_u->buflen < fmd->mmap_off + fmd->mmap_sz) | |
ed47cbf7 JA |
143 | goto done; |
144 | ||
8f933cae JA |
145 | /* |
146 | * unmap any existing mapping | |
147 | */ | |
03a32636 JA |
148 | if (fmd->mmap_ptr) { |
149 | if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0) | |
8f933cae | 150 | return errno; |
03a32636 | 151 | fmd->mmap_ptr = NULL; |
8f933cae JA |
152 | } |
153 | ||
ed47cbf7 JA |
154 | 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 | ||
161 | done: | |
03a32636 | 162 | io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off - |
ed47cbf7 JA |
163 | f->file_offset; |
164 | return 0; | |
165 | } | |
166 | ||
ac893112 JA |
167 | static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u) |
168 | { | |
169 | struct fio_file *f = io_u->file; | |
e19ccb55 | 170 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
ac893112 JA |
171 | |
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); | |
5f9099ea | 178 | else if (ddir_sync(io_u->ddir)) { |
03a32636 | 179 | if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) { |
ac893112 JA |
180 | io_u->error = errno; |
181 | td_verror(td, io_u->error, "msync"); | |
182 | } | |
ff58fced JA |
183 | } 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"); | |
ac893112 JA |
188 | } |
189 | ||
ff58fced | 190 | |
ac893112 JA |
191 | /* |
192 | * not really direct, but should drop the pages from the cache | |
193 | */ | |
ff58fced | 194 | if (td->o.odirect && ddir_rw(io_u->ddir)) { |
ac893112 JA |
195 | 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 | } | |
03e20d68 | 199 | if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) { |
ac893112 JA |
200 | io_u->error = errno; |
201 | td_verror(td, io_u->error, "madvise"); | |
202 | } | |
203 | } | |
204 | ||
205 | return FIO_Q_COMPLETED; | |
206 | } | |
207 | ||
208 | static int fio_mmapio_init(struct thread_data *td) | |
209 | { | |
913ea0db | 210 | struct thread_options *o = &td->o; |
ac893112 JA |
211 | unsigned long shift, mask; |
212 | ||
913ea0db JA |
213 | 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 " | |
4e0a8fa2 | 216 | "%llu bytes\n", (unsigned long long) page_size); |
913ea0db JA |
217 | return 1; |
218 | } | |
219 | ||
ac893112 JA |
220 | 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); | |
93bcfd20 | 229 | |
ac893112 JA |
230 | mmap_map_mask = 1UL << shift; |
231 | return 0; | |
232 | } | |
233 | ||
03a32636 JA |
234 | static 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) { | |
8a68c41c JA |
245 | int fio_unused __ret; |
246 | __ret = generic_close_file(td, f); | |
03a32636 JA |
247 | return 1; |
248 | } | |
249 | ||
e19ccb55 | 250 | FILE_SET_ENG_DATA(f, fmd); |
03a32636 JA |
251 | return 0; |
252 | } | |
253 | ||
254 | static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f) | |
255 | { | |
e19ccb55 | 256 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
03a32636 | 257 | |
e19ccb55 | 258 | FILE_SET_ENG_DATA(f, NULL); |
03a32636 | 259 | free(fmd); |
965f0a41 | 260 | fio_file_clear_partial_mmap(f); |
03a32636 JA |
261 | |
262 | return generic_close_file(td, f); | |
263 | } | |
264 | ||
5f350952 | 265 | static struct ioengine_ops ioengine = { |
2866c82d JA |
266 | .name = "mmap", |
267 | .version = FIO_IOOPS_VERSION, | |
ac893112 JA |
268 | .init = fio_mmapio_init, |
269 | .prep = fio_mmapio_prep, | |
2866c82d | 270 | .queue = fio_mmapio_queue, |
03a32636 JA |
271 | .open_file = fio_mmapio_open_file, |
272 | .close_file = fio_mmapio_close_file, | |
df9c26b1 | 273 | .get_file_size = generic_get_file_size, |
0263882a | 274 | .flags = FIO_SYNCIO | FIO_NOEXTEND, |
2866c82d | 275 | }; |
5f350952 JA |
276 | |
277 | static void fio_init fio_mmapio_register(void) | |
278 | { | |
279 | register_ioengine(&ioengine); | |
280 | } | |
281 | ||
282 | static void fio_exit fio_mmapio_unregister(void) | |
283 | { | |
284 | unregister_ioengine(&ioengine); | |
285 | } |