blob: bd9a9428661abe38048233c0c56c5be256ee7311 [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/*
Jens Axboeff455a02009-06-29 11:54:56 +020018 * Limits us to 1GB of mapped files in total
Jens Axboeac893112009-06-02 13:06:01 +020019 */
Jens Axboeff455a02009-06-29 11:54:56 +020020#define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL)
Jens Axboeac893112009-06-02 13:06:01 +020021
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
Jens Axboeff455a02009-06-29 11:54:56 +020093 f->mmap_off = io_u->offset;
Jens Axboeac893112009-06-02 13:06:01 +020094
95 ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off);
96done:
97 if (!ret)
98 io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off -
99 f->file_offset;
100err:
Jens Axboe6977bcd2008-03-01 15:55:36 +0100101 return ret;
Jens Axboeb5af8292007-03-08 12:43:13 +0100102}
103
Jens Axboeac893112009-06-02 13:06:01 +0200104static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
105{
106 struct fio_file *f = io_u->file;
107
108 fio_ro_check(td, io_u);
109
110 if (io_u->ddir == DDIR_READ)
111 memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen);
112 else if (io_u->ddir == DDIR_WRITE)
113 memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200114 else if (ddir_sync(io_u->ddir)) {
Jens Axboeac893112009-06-02 13:06:01 +0200115 if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) {
116 io_u->error = errno;
117 td_verror(td, io_u->error, "msync");
118 }
119 }
120
121 /*
122 * not really direct, but should drop the pages from the cache
123 */
Jens Axboe5f9099e2009-06-16 22:40:26 +0200124 if (td->o.odirect && !ddir_sync(io_u->ddir)) {
Jens Axboeac893112009-06-02 13:06:01 +0200125 if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) {
126 io_u->error = errno;
127 td_verror(td, io_u->error, "msync");
128 }
129 if (madvise(io_u->mmap_data, io_u->xfer_buflen, MADV_DONTNEED) < 0) {
130 io_u->error = errno;
131 td_verror(td, io_u->error, "madvise");
132 }
133 }
134
135 return FIO_Q_COMPLETED;
136}
137
138static int fio_mmapio_init(struct thread_data *td)
139{
140 unsigned long shift, mask;
141
142 mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files;
143 mask = mmap_map_size;
144 shift = 0;
145 do {
146 mask >>= 1;
147 if (!mask)
148 break;
149 shift++;
150 } while (1);
151
152 mmap_map_mask = 1UL << shift;
153 return 0;
154}
155
Jens Axboe5f350952006-11-07 15:20:59 +0100156static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200157 .name = "mmap",
158 .version = FIO_IOOPS_VERSION,
Jens Axboeac893112009-06-02 13:06:01 +0200159 .init = fio_mmapio_init,
160 .prep = fio_mmapio_prep,
Jens Axboe2866c822006-10-09 15:57:48 +0200161 .queue = fio_mmapio_queue,
Jens Axboeac893112009-06-02 13:06:01 +0200162 .open_file = generic_open_file,
163 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100164 .get_file_size = generic_get_file_size,
Jens Axboe02638822007-03-12 09:25:55 +0100165 .flags = FIO_SYNCIO | FIO_NOEXTEND,
Jens Axboe2866c822006-10-09 15:57:48 +0200166};
Jens Axboe5f350952006-11-07 15:20:59 +0100167
168static void fio_init fio_mmapio_register(void)
169{
170 register_ioengine(&ioengine);
171}
172
173static void fio_exit fio_mmapio_unregister(void)
174{
175 unregister_ioengine(&ioengine);
176}