Commit | Line | Data |
---|---|---|
2866c82d | 1 | /* |
da751ca9 JA |
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. | |
2866c82d JA |
6 | * |
7 | */ | |
8 | #include <stdio.h> | |
9 | #include <stdlib.h> | |
10 | #include <unistd.h> | |
11 | #include <errno.h> | |
2866c82d | 12 | #include <sys/mman.h> |
5f350952 JA |
13 | |
14 | #include "../fio.h" | |
2866c82d | 15 | |
2866c82d JA |
16 | static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u) |
17 | { | |
53cdc686 JA |
18 | struct fio_file *f = io_u->file; |
19 | unsigned long long real_off = io_u->offset - f->file_offset; | |
2866c82d | 20 | |
7101d9c2 JA |
21 | fio_ro_check(td, io_u); |
22 | ||
2866c82d | 23 | if (io_u->ddir == DDIR_READ) |
cec6b55d | 24 | memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen); |
87dc1ab1 | 25 | else if (io_u->ddir == DDIR_WRITE) |
cec6b55d | 26 | memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen); |
b907a5b5 | 27 | else if (io_u->ddir == DDIR_SYNC) { |
7bb48f84 | 28 | size_t len = (f->io_size + page_size - 1) & ~page_mask; |
cfc99db7 JA |
29 | |
30 | if (msync(f->mmap, len, MS_SYNC)) { | |
b907a5b5 | 31 | io_u->error = errno; |
cfc99db7 JA |
32 | td_verror(td, io_u->error, "msync"); |
33 | } | |
b907a5b5 | 34 | } |
2866c82d JA |
35 | |
36 | /* | |
37 | * not really direct, but should drop the pages from the cache | |
38 | */ | |
2dc1bbeb | 39 | if (td->o.odirect && io_u->ddir != DDIR_SYNC) { |
cfc99db7 JA |
40 | size_t len = (io_u->xfer_buflen + page_size - 1) & ~page_mask; |
41 | unsigned long long off = real_off & ~page_mask; | |
42 | ||
43 | if (msync(f->mmap + off, len, MS_SYNC) < 0) { | |
2866c82d | 44 | io_u->error = errno; |
cfc99db7 JA |
45 | td_verror(td, io_u->error, "msync"); |
46 | } | |
47 | if (madvise(f->mmap + off, len, MADV_DONTNEED) < 0) { | |
2866c82d | 48 | io_u->error = errno; |
cfc99db7 JA |
49 | td_verror(td, io_u->error, "madvise"); |
50 | } | |
2866c82d JA |
51 | } |
52 | ||
36167d82 | 53 | return FIO_Q_COMPLETED; |
2866c82d JA |
54 | } |
55 | ||
b5af8293 JA |
56 | static int fio_mmapio_open(struct thread_data *td, struct fio_file *f) |
57 | { | |
58 | int ret, flags; | |
59 | ||
60 | ret = generic_open_file(td, f); | |
61 | if (ret) | |
62 | return ret; | |
63 | ||
c97d8369 JA |
64 | /* |
65 | * for size checkup, don't mmap anything. | |
66 | */ | |
67 | if (!f->io_size) | |
68 | return 0; | |
69 | ||
b5af8293 JA |
70 | if (td_rw(td)) |
71 | flags = PROT_READ | PROT_WRITE; | |
72 | else if (td_write(td)) { | |
73 | flags = PROT_WRITE; | |
74 | ||
2dc1bbeb | 75 | if (td->o.verify != VERIFY_NONE) |
b5af8293 JA |
76 | flags |= PROT_READ; |
77 | } else | |
78 | flags = PROT_READ; | |
79 | ||
7bb48f84 | 80 | f->mmap = mmap(NULL, f->io_size, flags, MAP_SHARED, f->fd, f->file_offset); |
b5af8293 | 81 | if (f->mmap == MAP_FAILED) { |
8de43fd2 JA |
82 | int err = errno; |
83 | ||
b5af8293 | 84 | f->mmap = NULL; |
8de43fd2 JA |
85 | td_verror(td, err, "mmap"); |
86 | if (err == EINVAL && f->io_size > 2*1024*1024*1024UL) | |
87 | log_err("fio: mmap size likely too large\n"); | |
b5af8293 JA |
88 | goto err; |
89 | } | |
90 | ||
91 | if (file_invalidate_cache(td, f)) | |
92 | goto err; | |
93 | ||
94 | if (!td_random(td)) { | |
7bb48f84 | 95 | if (madvise(f->mmap, f->io_size, MADV_SEQUENTIAL) < 0) { |
b5af8293 JA |
96 | td_verror(td, errno, "madvise"); |
97 | goto err; | |
98 | } | |
99 | } else { | |
7bb48f84 | 100 | if (madvise(f->mmap, f->io_size, MADV_RANDOM) < 0) { |
b5af8293 JA |
101 | td_verror(td, errno, "madvise"); |
102 | goto err; | |
103 | } | |
104 | } | |
105 | ||
106 | return 0; | |
107 | ||
108 | err: | |
0263882a | 109 | td->io_ops->close_file(td, f); |
b5af8293 JA |
110 | return 1; |
111 | } | |
112 | ||
6977bcd0 JA |
113 | static int fio_mmapio_close(struct thread_data fio_unused *td, |
114 | struct fio_file *f) | |
b5af8293 | 115 | { |
6977bcd0 JA |
116 | int ret = 0, ret2; |
117 | ||
b5af8293 | 118 | if (f->mmap) { |
6977bcd0 JA |
119 | if (munmap(f->mmap, f->io_size) < 0) |
120 | ret = errno; | |
121 | ||
b5af8293 JA |
122 | f->mmap = NULL; |
123 | } | |
6977bcd0 JA |
124 | |
125 | ret2 = generic_close_file(td, f); | |
126 | if (!ret && ret2) | |
127 | ret = ret2; | |
128 | ||
129 | return ret; | |
b5af8293 JA |
130 | } |
131 | ||
5f350952 | 132 | static struct ioengine_ops ioengine = { |
2866c82d JA |
133 | .name = "mmap", |
134 | .version = FIO_IOOPS_VERSION, | |
2866c82d | 135 | .queue = fio_mmapio_queue, |
b5af8293 JA |
136 | .open_file = fio_mmapio_open, |
137 | .close_file = fio_mmapio_close, | |
df9c26b1 | 138 | .get_file_size = generic_get_file_size, |
0263882a | 139 | .flags = FIO_SYNCIO | FIO_NOEXTEND, |
2866c82d | 140 | }; |
5f350952 JA |
141 | |
142 | static void fio_init fio_mmapio_register(void) | |
143 | { | |
144 | register_ioengine(&ioengine); | |
145 | } | |
146 | ||
147 | static void fio_exit fio_mmapio_unregister(void) | |
148 | { | |
149 | unregister_ioengine(&ioengine); | |
150 | } |