Engines should not touch nr_open_files anymore
[fio.git] / engines / mmap.c
CommitLineData
2866c82d
JA
1/*
2 * regular read/write sync io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10#include <sys/mman.h>
5f350952
JA
11
12#include "../fio.h"
13#include "../os.h"
2866c82d 14
2866c82d
JA
15static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
16{
53cdc686
JA
17 struct fio_file *f = io_u->file;
18 unsigned long long real_off = io_u->offset - f->file_offset;
2866c82d
JA
19
20 if (io_u->ddir == DDIR_READ)
cec6b55d 21 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
87dc1ab1 22 else if (io_u->ddir == DDIR_WRITE)
cec6b55d 23 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
b907a5b5
JA
24 else if (io_u->ddir == DDIR_SYNC) {
25 if (msync(f->mmap, f->file_size, MS_SYNC))
26 io_u->error = errno;
27 }
2866c82d
JA
28
29 /*
30 * not really direct, but should drop the pages from the cache
31 */
b907a5b5 32 if (td->odirect && io_u->ddir != DDIR_SYNC) {
cec6b55d 33 if (msync(f->mmap + real_off, io_u->xfer_buflen, MS_SYNC) < 0)
2866c82d 34 io_u->error = errno;
cec6b55d 35 if (madvise(f->mmap + real_off, io_u->xfer_buflen, MADV_DONTNEED) < 0)
2866c82d
JA
36 io_u->error = errno;
37 }
38
36167d82 39 if (io_u->error)
e1161c32 40 td_verror(td, io_u->error, "sync");
2866c82d 41
36167d82 42 return FIO_Q_COMPLETED;
2866c82d
JA
43}
44
948f9b2e
JA
45static int fio_mmapio_init(struct thread_data *td)
46{
47 struct fio_file *f;
48 int i;
49
413dd459 50 if (!td_write(td))
948f9b2e
JA
51 return 0;
52
53 /*
54 * We need to truncate the files to the right size, if
55 * we are writing to it.
56 */
57 for_each_file(td, f, i) {
58 if (ftruncate(f->fd, f->file_size) < 0) {
e1161c32 59 td_verror(td, errno, "ftruncate");
948f9b2e
JA
60 return 1;
61 }
62 }
63
64 return 0;
65}
66
b5af8293
JA
67static int fio_mmapio_open(struct thread_data *td, struct fio_file *f)
68{
69 int ret, flags;
70
71 ret = generic_open_file(td, f);
72 if (ret)
73 return ret;
74
75 if (td_rw(td))
76 flags = PROT_READ | PROT_WRITE;
77 else if (td_write(td)) {
78 flags = PROT_WRITE;
79
80 if (td->verify != VERIFY_NONE)
81 flags |= PROT_READ;
82 } else
83 flags = PROT_READ;
84
85 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
86 if (f->mmap == MAP_FAILED) {
87 f->mmap = NULL;
88 td_verror(td, errno, "mmap");
89 goto err;
90 }
91
92 if (file_invalidate_cache(td, f))
93 goto err;
94
95 if (!td_random(td)) {
96 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
97 td_verror(td, errno, "madvise");
98 goto err;
99 }
100 } else {
101 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
102 td_verror(td, errno, "madvise");
103 goto err;
104 }
105 }
106
107 return 0;
108
109err:
110 if (f->mmap)
111 munmap(f->mmap, f->file_size);
112 generic_close_file(td, f);
113 return 1;
114}
115
116static void fio_mmapio_close(struct thread_data fio_unused *td,
117 struct fio_file *f)
118{
119 if (f->mmap) {
120 munmap(f->mmap, f->file_size);
121 f->mmap = NULL;
122 }
123}
124
5f350952 125static struct ioengine_ops ioengine = {
2866c82d
JA
126 .name = "mmap",
127 .version = FIO_IOOPS_VERSION,
2866c82d 128 .queue = fio_mmapio_queue,
948f9b2e 129 .init = fio_mmapio_init,
b5af8293
JA
130 .open_file = fio_mmapio_open,
131 .close_file = fio_mmapio_close,
132 .flags = FIO_SYNCIO,
2866c82d 133};
5f350952
JA
134
135static void fio_init fio_mmapio_register(void)
136{
137 register_ioengine(&ioengine);
138}
139
140static void fio_exit fio_mmapio_unregister(void)
141{
142 unregister_ioengine(&ioengine);
143}