Refactor #includes and headers
[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 int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
181 {
182         struct fio_file *f = io_u->file;
183         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
184
185         fio_ro_check(td, io_u);
186
187         if (io_u->ddir == DDIR_READ)
188                 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
189         else if (io_u->ddir == DDIR_WRITE)
190                 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
191         else if (ddir_sync(io_u->ddir)) {
192                 if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) {
193                         io_u->error = errno;
194                         td_verror(td, io_u->error, "msync");
195                 }
196         } else if (io_u->ddir == DDIR_TRIM) {
197                 int ret = do_io_u_trim(td, io_u);
198
199                 if (!ret)
200                         td_verror(td, io_u->error, "trim");
201         }
202
203
204         /*
205          * not really direct, but should drop the pages from the cache
206          */
207         if (td->o.odirect && ddir_rw(io_u->ddir)) {
208                 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
209                         io_u->error = errno;
210                         td_verror(td, io_u->error, "msync");
211                 }
212                 if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) {
213                         io_u->error = errno;
214                         td_verror(td, io_u->error, "madvise");
215                 }
216         }
217
218         return FIO_Q_COMPLETED;
219 }
220
221 static int fio_mmapio_init(struct thread_data *td)
222 {
223         struct thread_options *o = &td->o;
224
225         if ((o->rw_min_bs & page_mask) &&
226             (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) {
227                 log_err("fio: mmap options dictate a minimum block size of "
228                         "%llu bytes\n", (unsigned long long) page_size);
229                 return 1;
230         }
231
232         mmap_map_size = MMAP_TOTAL_SZ / o->nr_files;
233         return 0;
234 }
235
236 static int fio_mmapio_open_file(struct thread_data *td, struct fio_file *f)
237 {
238         struct fio_mmap_data *fmd;
239         int ret;
240
241         ret = generic_open_file(td, f);
242         if (ret)
243                 return ret;
244
245         fmd = calloc(1, sizeof(*fmd));
246         if (!fmd) {
247                 int fio_unused __ret;
248                 __ret = generic_close_file(td, f);
249                 return 1;
250         }
251
252         FILE_SET_ENG_DATA(f, fmd);
253         return 0;
254 }
255
256 static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f)
257 {
258         struct fio_mmap_data *fmd = FILE_ENG_DATA(f);
259
260         FILE_SET_ENG_DATA(f, NULL);
261         free(fmd);
262         fio_file_clear_partial_mmap(f);
263
264         return generic_close_file(td, f);
265 }
266
267 static struct ioengine_ops ioengine = {
268         .name           = "mmap",
269         .version        = FIO_IOOPS_VERSION,
270         .init           = fio_mmapio_init,
271         .prep           = fio_mmapio_prep,
272         .queue          = fio_mmapio_queue,
273         .open_file      = fio_mmapio_open_file,
274         .close_file     = fio_mmapio_close_file,
275         .get_file_size  = generic_get_file_size,
276         .flags          = FIO_SYNCIO | FIO_NOEXTEND,
277 };
278
279 static void fio_init fio_mmapio_register(void)
280 {
281         register_ioengine(&ioengine);
282 }
283
284 static void fio_exit fio_mmapio_unregister(void)
285 {
286         unregister_ioengine(&ioengine);
287 }