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