71abfb9ac546247cd339033373ebbbda35778d54
[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 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
26                          size_t length, off_t off)
27 {
28         int flags = 0;
29
30         if (td_rw(td))
31                 flags = PROT_READ | PROT_WRITE;
32         else if (td_write(td)) {
33                 flags = PROT_WRITE;
34
35                 if (td->o.verify != VERIFY_NONE)
36                         flags |= PROT_READ;
37         } else
38                 flags = PROT_READ;
39
40         f->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
41         if (f->mmap_ptr == MAP_FAILED) {
42                 f->mmap_ptr = NULL;
43                 td_verror(td, errno, "mmap");
44                 goto err;
45         }
46
47         if (!td_random(td)) {
48                 if (madvise(f->mmap_ptr, length, MADV_SEQUENTIAL) < 0) {
49                         td_verror(td, errno, "madvise");
50                         goto err;
51                 }
52         } else {
53                 if (madvise(f->mmap_ptr, length, MADV_RANDOM) < 0) {
54                         td_verror(td, errno, "madvise");
55                         goto err;
56                 }
57         }
58
59 err:
60         if (td->error && f->mmap_ptr)
61                 munmap(f->mmap_ptr, length);
62                 
63         return td->error;
64 }
65
66 /*
67  * Just mmap an appropriate portion, we cannot mmap the full extent
68  */
69 static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u)
70 {
71         struct fio_file *f = io_u->file;
72
73         if (io_u->buflen > mmap_map_size) {
74                 log_err("fio: bs too big for mmap engine\n");
75                 return EIO;
76         }
77
78         if (f->mmap_ptr) {
79                 if (munmap(f->mmap_ptr, f->mmap_sz) < 0)
80                         return errno;
81                 f->mmap_ptr = NULL;
82         }
83
84         f->mmap_sz = mmap_map_size;
85         if (f->mmap_sz  > f->io_size)
86                 f->mmap_sz = f->io_size;
87
88         f->mmap_off = io_u->offset;
89
90         return fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
91 }
92
93 /*
94  * Attempt to mmap the entire file
95  */
96 static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u)
97 {
98         struct fio_file *f = io_u->file;
99         int ret;
100
101         if (fio_file_partial_mmap(f))
102                 return EINVAL;
103
104         if (f->mmap_ptr) {
105                 if (munmap(f->mmap_ptr, f->mmap_sz) < 0)
106                         return errno;
107                 f->mmap_ptr = NULL;
108         }
109
110         f->mmap_sz = f->io_size;
111         f->mmap_off = 0;
112
113         ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
114         if (ret)
115                 fio_file_set_partial_mmap(f);
116
117         return ret;
118 }
119
120 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
121 {
122         struct fio_file *f = io_u->file;
123         int ret;
124
125         if (io_u->offset >= f->mmap_off &&
126             io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz)
127                 goto done;
128
129         if (fio_mmapio_prep_full(td, io_u)) {
130                 td_clear_error(td);
131                 ret = fio_mmapio_prep_limited(td, io_u);
132                 if (ret)
133                         return ret;
134         }
135
136 done:
137         io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off -
138                                 f->file_offset;
139         return 0;
140 }
141
142 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
143 {
144         struct fio_file *f = io_u->file;
145
146         fio_ro_check(td, io_u);
147
148         if (io_u->ddir == DDIR_READ)
149                 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
150         else if (io_u->ddir == DDIR_WRITE)
151                 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
152         else if (ddir_sync(io_u->ddir)) {
153                 if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) {
154                         io_u->error = errno;
155                         td_verror(td, io_u->error, "msync");
156                 }
157         }
158
159         /*
160          * not really direct, but should drop the pages from the cache
161          */
162         if (td->o.odirect && !ddir_sync(io_u->ddir)) {
163                 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
164                         io_u->error = errno;
165                         td_verror(td, io_u->error, "msync");
166                 }
167                 if (madvise(io_u->mmap_data, io_u->xfer_buflen,  MADV_DONTNEED) < 0) {
168                         io_u->error = errno;
169                         td_verror(td, io_u->error, "madvise");
170                 }
171         }
172
173         return FIO_Q_COMPLETED;
174 }
175
176 static int fio_mmapio_init(struct thread_data *td)
177 {
178         unsigned long shift, mask;
179
180         mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
181         mask = mmap_map_size;
182         shift = 0;
183         do {
184                 mask >>= 1;
185                 if (!mask)
186                         break;
187                 shift++;
188         } while (1);
189                 
190         mmap_map_mask = 1UL << shift;
191         return 0;
192 }
193
194 static struct ioengine_ops ioengine = {
195         .name           = "mmap",
196         .version        = FIO_IOOPS_VERSION,
197         .init           = fio_mmapio_init,
198         .prep           = fio_mmapio_prep,
199         .queue          = fio_mmapio_queue,
200         .open_file      = generic_open_file,
201         .close_file     = generic_close_file,
202         .get_file_size  = generic_get_file_size,
203         .flags          = FIO_SYNCIO | FIO_NOEXTEND,
204 };
205
206 static void fio_init fio_mmapio_register(void)
207 {
208         register_ioengine(&ioengine);
209 }
210
211 static void fio_exit fio_mmapio_unregister(void)
212 {
213         unregister_ioengine(&ioengine);
214 }