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