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