blob: fde68f1d9b0b5b6a7e3e6f404a003a02f1b4bf45 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * mmap engine
3 *
4 * IO engine that reads/writes from files by doing memcpy to/from
5 * a memory mapped region of the file.
Jens Axboe2866c822006-10-09 15:57:48 +02006 *
7 */
8#include <stdio.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <errno.h>
Jens Axboe2866c822006-10-09 15:57:48 +020012#include <sys/mman.h>
Jens Axboe5f350952006-11-07 15:20:59 +010013
14#include "../fio.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020015#include "../verify.h"
Jens Axboe2866c822006-10-09 15:57:48 +020016
Jens Axboeac893112009-06-02 13:06:01 +020017/*
18 * Limits us to 2GB of mapped files in total
19 */
20#define MMAP_TOTAL_SZ (2 * 1024 * 1024 * 1024UL)
21
22static unsigned long mmap_map_size;
23static unsigned long mmap_map_mask;
24
25static int fio_mmap_file(struct thread_data *td, struct fio_file *f,
26 size_t length, off_t off)
Jens Axboe2866c822006-10-09 15:57:48 +020027{
Jens Axboeac893112009-06-02 13:06:01 +020028 int flags = 0;
29 int ret = 0;
Jens Axboec97d8362007-05-21 14:52:43 +020030
Jens Axboeb5af8292007-03-08 12:43:13 +010031 if (td_rw(td))
32 flags = PROT_READ | PROT_WRITE;
33 else if (td_write(td)) {
34 flags = PROT_WRITE;
35
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010036 if (td->o.verify != VERIFY_NONE)
Jens Axboeb5af8292007-03-08 12:43:13 +010037 flags |= PROT_READ;
38 } else
39 flags = PROT_READ;
40
Jens Axboeac893112009-06-02 13:06:01 +020041 f->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off);
42 if (f->mmap_ptr == MAP_FAILED) {
Jens Axboe8de43fd2009-05-20 10:49:44 +020043 int err = errno;
44
Jens Axboeac893112009-06-02 13:06:01 +020045 f->mmap_ptr = NULL;
Jens Axboe8de43fd2009-05-20 10:49:44 +020046 td_verror(td, err, "mmap");
Jens Axboeb5af8292007-03-08 12:43:13 +010047 goto err;
48 }
49
Jens Axboeb5af8292007-03-08 12:43:13 +010050 if (!td_random(td)) {
Jens Axboeac893112009-06-02 13:06:01 +020051 if (madvise(f->mmap_ptr, length, MADV_SEQUENTIAL) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010052 td_verror(td, errno, "madvise");
53 goto err;
54 }
55 } else {
Jens Axboeac893112009-06-02 13:06:01 +020056 if (madvise(f->mmap_ptr, length, MADV_RANDOM) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010057 td_verror(td, errno, "madvise");
58 goto err;
59 }
60 }
61
Jens Axboeb5af8292007-03-08 12:43:13 +010062err:
Jens Axboeac893112009-06-02 13:06:01 +020063 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +010064}
65
Jens Axboeac893112009-06-02 13:06:01 +020066static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u)
Jens Axboeb5af8292007-03-08 12:43:13 +010067{
Jens Axboeac893112009-06-02 13:06:01 +020068 struct fio_file *f = io_u->file;
69 int ret = 0;
Jens Axboe6977bcd2008-03-01 15:55:36 +010070
Jens Axboeac893112009-06-02 13:06:01 +020071 if (io_u->buflen > mmap_map_size) {
72 log_err("fio: bs too big for mmap engine\n");
73 ret = EIO;
74 goto err;
Jens Axboeb5af8292007-03-08 12:43:13 +010075 }
Jens Axboe6977bcd2008-03-01 15:55:36 +010076
Jens Axboeac893112009-06-02 13:06:01 +020077 if (io_u->offset >= f->mmap_off &&
78 io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz)
79 goto done;
Jens Axboe6977bcd2008-03-01 15:55:36 +010080
Jens Axboeac893112009-06-02 13:06:01 +020081 if (f->mmap_ptr) {
82 if (munmap(f->mmap_ptr, f->mmap_sz) < 0) {
83 ret = errno;
84 goto err;
85 }
86 f->mmap_ptr = NULL;
87 }
88
89 f->mmap_sz = mmap_map_size;
90 if (f->mmap_sz > f->io_size)
91 f->mmap_sz = f->io_size;
92
93 f->mmap_off = io_u->offset & ~mmap_map_mask;
94 if (io_u->offset + io_u->buflen >= f->mmap_off + f->mmap_sz)
95 f->mmap_off -= io_u->buflen;
96
97 ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
98done:
99 if (!ret)
100 io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off -
101 f->file_offset;
102err:
Jens Axboe6977bcd2008-03-01 15:55:36 +0100103 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +0100104}
105
Jens Axboeac893112009-06-02 13:06:01 +0200106static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
107{
108 struct fio_file *f = io_u->file;
109
110 fio_ro_check(td, io_u);
111
112 if (io_u->ddir == DDIR_READ)
113 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
114 else if (io_u->ddir == DDIR_WRITE)
115 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
116 else if (io_u->ddir == DDIR_SYNC) {
117 if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) {
118 io_u->error = errno;
119 td_verror(td, io_u->error, "msync");
120 }
121 }
122
123 /*
124 * not really direct, but should drop the pages from the cache
125 */
126 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
127 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
128 io_u->error = errno;
129 td_verror(td, io_u->error, "msync");
130 }
131 if (madvise(io_u->mmap_data, io_u->xfer_buflen, MADV_DONTNEED) < 0) {
132 io_u->error = errno;
133 td_verror(td, io_u->error, "madvise");
134 }
135 }
136
137 return FIO_Q_COMPLETED;
138}
139
140static int fio_mmapio_init(struct thread_data *td)
141{
142 unsigned long shift, mask;
143
144 mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
145 mask = mmap_map_size;
146 shift = 0;
147 do {
148 mask >>= 1;
149 if (!mask)
150 break;
151 shift++;
152 } while (1);
153
154 mmap_map_mask = 1UL << shift;
155 return 0;
156}
157
Jens Axboe5f350952006-11-07 15:20:59 +0100158static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200159 .name = "mmap",
160 .version = FIO_IOOPS_VERSION,
Jens Axboeac893112009-06-02 13:06:01 +0200161 .init = fio_mmapio_init,
162 .prep = fio_mmapio_prep,
Jens Axboe2866c822006-10-09 15:57:48 +0200163 .queue = fio_mmapio_queue,
Jens Axboeac893112009-06-02 13:06:01 +0200164 .open_file = generic_open_file,
165 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100166 .get_file_size = generic_get_file_size,
Jens Axboe02638822007-03-12 09:25:55 +0100167 .flags = FIO_SYNCIO | FIO_NOEXTEND,
Jens Axboe2866c822006-10-09 15:57:48 +0200168};
Jens Axboe5f350952006-11-07 15:20:59 +0100169
170static void fio_init fio_mmapio_register(void)
171{
172 register_ioengine(&ioengine);
173}
174
175static void fio_exit fio_mmapio_unregister(void)
176{
177 unregister_ioengine(&ioengine);
178}