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