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