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