856547befda3dea5e42ece3a123ed35c70663f66
[fio.git] / engines / mmap.c
1 /*
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.
6  *
7  */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <sys/mman.h>
12
13 #include "../fio.h"
14 #include "../verify.h"
15
16 /*
17  * Limits us to 1GiB of mapped files in total
18  */
19 #define MMAP_TOTAL_SZ   (1 * 1024 * 1024 * 1024UL)
20
21 static unsigned long mmap_map_size;
22
23 struct fio_mmap_data {
24         void *mmap_ptr;
25         size_t mmap_sz;
26         off_t mmap_off;
27 };
28
29 static bool fio_madvise_file(struct thread_data *td, struct fio_file *f,
30                              size_t length)
31
32 {
33         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
34
35         if (!td->o.fadvise_hint)
36                 return true;
37
38         if (!td_random(td)) {
39                 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) {
40                         td_verror(td, errno, "madvise");
41                         return false;
42                 }
43         } else {
44                 if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) {
45                         td_verror(td, errno, "madvise");
46                         return false;
47                 }
48         }
49
50         return true;
51 }
52
53 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
54                          size_t length, off_t off)
55 {
56         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
57         int flags = 0;
58
59         if (td_rw(td) && !td->o.verify_only)
60                 flags = PROT_READ | PROT_WRITE;
61         else if (td_write(td) && !td->o.verify_only) {
62                 flags = PROT_WRITE;
63
64                 if (td->o.verify != VERIFY_NONE)
65                         flags |= PROT_READ;
66         } else
67                 flags = PROT_READ;
68
69         fmd->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
70         if (fmd->mmap_ptr == MAP_FAILED) {
71                 fmd->mmap_ptr = NULL;
72                 td_verror(td, errno, "mmap");
73                 goto err;
74         }
75
76         if (!fio_madvise_file(td, f, length))
77                 goto err;
78
79         if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_DONTNEED) < 0) {
80                 td_verror(td, errno, "madvise");
81                 goto err;
82         }
83
84 #ifdef FIO_MADV_FREE
85         if (f->filetype == FIO_TYPE_BLOCK)
86                 (void) posix_madvise(fmd->mmap_ptr, fmd->mmap_sz, FIO_MADV_FREE);
87 #endif
88
89 err:
90         if (td->error && fmd->mmap_ptr)
91                 munmap(fmd->mmap_ptr, length);
92
93         return td->error;
94 }
95
96 /*
97  * Just mmap an appropriate portion, we cannot mmap the full extent
98  */
99 static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
100 {
101         struct fio_file *f = io_u->file;
102         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
103
104         if (io_u->buflen > mmap_map_size) {
105                 log_err("fio: bs too big for mmap engine\n");
106                 return EIO;
107         }
108
109         fmd->mmap_sz = mmap_map_size;
110         if (fmd->mmap_sz  > f->io_size)
111                 fmd->mmap_sz = f->io_size;
112
113         fmd->mmap_off = io_u->offset;
114
115         return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
116 }
117
118 /*
119  * Attempt to mmap the entire file
120  */
121 static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u)
122 {
123         struct fio_file *f = io_u->file;
124         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
125         int ret;
126
127         if (fio_file_partial_mmap(f))
128                 return EINVAL;
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         }
134
135         fmd->mmap_sz = f->io_size;
136         fmd->mmap_off = 0;
137
138         ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off);
139         if (ret)
140                 fio_file_set_partial_mmap(f);
141
142         return ret;
143 }
144
145 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
146 {
147         struct fio_file *f = io_u->file;
148         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
149         int ret;
150
151         /*
152          * It fits within existing mapping, use it
153          */
154         if (io_u->offset >= fmd->mmap_off &&
155             io_u->offset + io_u->buflen <= fmd->mmap_off + fmd->mmap_sz)
156                 goto done;
157
158         /*
159          * unmap any existing mapping
160          */
161         if (fmd->mmap_ptr) {
162                 if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0)
163                         return errno;
164                 fmd->mmap_ptr = NULL;
165         }
166
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
174 done:
175         io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off -
176                                 f->file_offset;
177         return 0;
178 }
179
180 static enum fio_q_status
181 fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
182 {
183         struct fio_file *f = io_u->file;
184         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
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);
192         else if (ddir_sync(io_u->ddir)) {
193                 if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) {
194                         io_u->error = errno;
195                         td_verror(td, io_u->error, "msync");
196                 }
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");
202         }
203
204
205         /*
206          * not really direct, but should drop the pages from the cache
207          */
208         if (td->o.odirect && ddir_rw(io_u->ddir)) {
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                 }
213                 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
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 {
224         struct thread_options *o = &td->o;
225
226         if ((o->rw_min_bs & page_mask) &&
227             (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) {
228                 log_err("fio: mmap options dictate a minimum block size of "
229                         "%llu bytes\n", (unsigned long long) page_size);
230                 return 1;
231         }
232
233         mmap_map_size = MMAP_TOTAL_SZ / o->nr_files;
234         return 0;
235 }
236
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) {
248                 int fio_unused __ret;
249                 __ret = generic_close_file(td, f);
250                 return 1;
251         }
252
253         FILE_SET_ENG_DATA(f, fmd);
254         return 0;
255 }
256
257 static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f)
258 {
259         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
260
261         FILE_SET_ENG_DATA(f, NULL);
262         free(fmd);
263         fio_file_clear_partial_mmap(f);
264
265         return generic_close_file(td, f);
266 }
267
268 static struct ioengine_ops ioengine = {
269         .name           = "mmap",
270         .version        = FIO_IOOPS_VERSION,
271         .init           = fio_mmapio_init,
272         .prep           = fio_mmapio_prep,
273         .queue          = fio_mmapio_queue,
274         .open_file      = fio_mmapio_open_file,
275         .close_file     = fio_mmapio_close_file,
276         .get_file_size  = generic_get_file_size,
277         .flags          = FIO_SYNCIO | FIO_NOEXTEND,
278 };
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 }