05a4d5163ce5dbd45a3ba351419417e642b65be1
[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
16 /*
17  * Limits us to 2GB of mapped files in total
18  */
19 #define MMAP_TOTAL_SZ   (2 * 1024 * 1024 * 1024UL)
20
21 static unsigned long mmap_map_size;
22 static unsigned long mmap_map_mask;
23
24 static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
25                          size_t length, off_t off)
26 {
27         int flags = 0;
28         int ret = 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                 int err = errno;
43
44                 f->mmap_ptr = NULL;
45                 td_verror(td, err, "mmap");
46                 if (err == EINVAL && f->io_size > 2*1024*1024*1024UL)
47                         log_err("fio: mmap size likely too large\n");
48                 goto err;
49         }
50
51         if (file_invalidate_cache(td, f))
52                 goto err;
53
54         if (!td_random(td)) {
55                 if (madvise(f->mmap_ptr, length, MADV_SEQUENTIAL) < 0) {
56                         td_verror(td, errno, "madvise");
57                         goto err;
58                 }
59         } else {
60                 if (madvise(f->mmap_ptr, length, MADV_RANDOM) < 0) {
61                         td_verror(td, errno, "madvise");
62                         goto err;
63                 }
64         }
65
66 err:
67         return ret;
68 }
69
70 static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
71 {
72         struct fio_file *f = io_u->file;
73         int ret = 0;
74
75         if (io_u->buflen > mmap_map_size) {
76                 log_err("fio: bs too big for mmap engine\n");
77                 ret = EIO;
78                 goto err;
79         }
80
81         if (io_u->offset >= f->mmap_off &&
82             io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz)
83                 goto done;
84
85         if (f->mmap_ptr) {
86                 if (munmap(f->mmap_ptr, f->mmap_sz) < 0) {
87                         ret = errno;
88                         goto err;
89                 }
90                 f->mmap_ptr = NULL;
91         }
92
93         f->mmap_sz = mmap_map_size;
94         if (f->mmap_sz  > f->io_size)
95                 f->mmap_sz = f->io_size;
96
97         f->mmap_off = io_u->offset & ~mmap_map_mask;
98         if (io_u->offset + io_u->buflen >= f->mmap_off + f->mmap_sz)
99                 f->mmap_off -= io_u->buflen;
100
101         ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
102 done:
103         if (!ret)
104                 io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off -
105                                         f->file_offset;
106 err:
107         return ret;
108 }
109
110 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
111 {
112         struct fio_file *f = io_u->file;
113
114         fio_ro_check(td, io_u);
115
116         if (io_u->ddir == DDIR_READ)
117                 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
118         else if (io_u->ddir == DDIR_WRITE)
119                 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
120         else if (io_u->ddir == DDIR_SYNC) {
121                 if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) {
122                         io_u->error = errno;
123                         td_verror(td, io_u->error, "msync");
124                 }
125         }
126
127         /*
128          * not really direct, but should drop the pages from the cache
129          */
130         if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
131                 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
132                         io_u->error = errno;
133                         td_verror(td, io_u->error, "msync");
134                 }
135                 if (madvise(io_u->mmap_data, io_u->xfer_buflen,  MADV_DONTNEED) < 0) {
136                         io_u->error = errno;
137                         td_verror(td, io_u->error, "madvise");
138                 }
139         }
140
141         return FIO_Q_COMPLETED;
142 }
143
144 static int fio_mmapio_init(struct thread_data *td)
145 {
146         unsigned long shift, mask;
147
148         mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
149         mask = mmap_map_size;
150         shift = 0;
151         do {
152                 mask >>= 1;
153                 if (!mask)
154                         break;
155                 shift++;
156         } while (1);
157                 
158         mmap_map_mask = 1UL << shift;
159         return 0;
160 }
161
162 static struct ioengine_ops ioengine = {
163         .name           = "mmap",
164         .version        = FIO_IOOPS_VERSION,
165         .init           = fio_mmapio_init,
166         .prep           = fio_mmapio_prep,
167         .queue          = fio_mmapio_queue,
168         .open_file      = generic_open_file,
169         .close_file     = generic_close_file,
170         .get_file_size  = generic_get_file_size,
171         .flags          = FIO_SYNCIO | FIO_NOEXTEND,
172 };
173
174 static void fio_init fio_mmapio_register(void)
175 {
176         register_ioengine(&ioengine);
177 }
178
179 static void fio_exit fio_mmapio_unregister(void)
180 {
181         unregister_ioengine(&ioengine);
182 }