blob: f3d55c167691616a9d6478dba51e51154475ff3f [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>
12#include <assert.h>
13#include <sys/mman.h>
Jens Axboe5f350952006-11-07 15:20:59 +010014
15#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020016
Jens Axboe2866c822006-10-09 15:57:48 +020017static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
18{
Jens Axboe53cdc682006-10-18 11:50:58 +020019 struct fio_file *f = io_u->file;
20 unsigned long long real_off = io_u->offset - f->file_offset;
Jens Axboe2866c822006-10-09 15:57:48 +020021
22 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010023 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020024 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010025 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboeb907a5b2006-10-24 14:46:13 +020026 else if (io_u->ddir == DDIR_SYNC) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020027 size_t len = (f->io_size + page_size - 1) & ~page_mask;
Jens Axboecfc99db2007-03-14 10:34:47 +010028
29 if (msync(f->mmap, len, MS_SYNC)) {
Jens Axboeb907a5b2006-10-24 14:46:13 +020030 io_u->error = errno;
Jens Axboecfc99db2007-03-14 10:34:47 +010031 td_verror(td, io_u->error, "msync");
32 }
Jens Axboeb907a5b2006-10-24 14:46:13 +020033 }
Jens Axboe2866c822006-10-09 15:57:48 +020034
35 /*
36 * not really direct, but should drop the pages from the cache
37 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010038 if (td->o.odirect && io_u->ddir != DDIR_SYNC) {
Jens Axboecfc99db2007-03-14 10:34:47 +010039 size_t len = (io_u->xfer_buflen + page_size - 1) & ~page_mask;
40 unsigned long long off = real_off & ~page_mask;
Jens Axboe2866c822006-10-09 15:57:48 +020041
Jens Axboecfc99db2007-03-14 10:34:47 +010042 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 }
Jens Axboe2866c822006-10-09 15:57:48 +020051
Jens Axboe36167d82007-02-18 05:41:31 +010052 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020053}
54
Jens Axboeb5af8292007-03-08 12:43:13 +010055static 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
Jens Axboec97d8362007-05-21 14:52:43 +020063 /*
64 * for size checkup, don't mmap anything.
65 */
66 if (!f->io_size)
67 return 0;
68
Jens Axboeb5af8292007-03-08 12:43:13 +010069 if (td_rw(td))
70 flags = PROT_READ | PROT_WRITE;
71 else if (td_write(td)) {
72 flags = PROT_WRITE;
73
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010074 if (td->o.verify != VERIFY_NONE)
Jens Axboeb5af8292007-03-08 12:43:13 +010075 flags |= PROT_READ;
76 } else
77 flags = PROT_READ;
78
Jens Axboe7bb48f82007-03-27 15:30:28 +020079 f->mmap = mmap(NULL, f->io_size, flags, MAP_SHARED, f->fd, f->file_offset);
Jens Axboeb5af8292007-03-08 12:43:13 +010080 if (f->mmap == MAP_FAILED) {
81 f->mmap = NULL;
82 td_verror(td, errno, "mmap");
83 goto err;
84 }
85
86 if (file_invalidate_cache(td, f))
87 goto err;
88
89 if (!td_random(td)) {
Jens Axboe7bb48f82007-03-27 15:30:28 +020090 if (madvise(f->mmap, f->io_size, MADV_SEQUENTIAL) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010091 td_verror(td, errno, "madvise");
92 goto err;
93 }
94 } else {
Jens Axboe7bb48f82007-03-27 15:30:28 +020095 if (madvise(f->mmap, f->io_size, MADV_RANDOM) < 0) {
Jens Axboeb5af8292007-03-08 12:43:13 +010096 td_verror(td, errno, "madvise");
97 goto err;
98 }
99 }
100
101 return 0;
102
103err:
Jens Axboe02638822007-03-12 09:25:55 +0100104 td->io_ops->close_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100105 return 1;
106}
107
108static void fio_mmapio_close(struct thread_data fio_unused *td,
109 struct fio_file *f)
110{
111 if (f->mmap) {
Jens Axboe7bb48f82007-03-27 15:30:28 +0200112 munmap(f->mmap, f->io_size);
Jens Axboeb5af8292007-03-08 12:43:13 +0100113 f->mmap = NULL;
114 }
Jens Axboe02638822007-03-12 09:25:55 +0100115 generic_close_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100116}
117
Jens Axboe5f350952006-11-07 15:20:59 +0100118static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200119 .name = "mmap",
120 .version = FIO_IOOPS_VERSION,
Jens Axboe2866c822006-10-09 15:57:48 +0200121 .queue = fio_mmapio_queue,
Jens Axboeb5af8292007-03-08 12:43:13 +0100122 .open_file = fio_mmapio_open,
123 .close_file = fio_mmapio_close,
Jens Axboe02638822007-03-12 09:25:55 +0100124 .flags = FIO_SYNCIO | FIO_NOEXTEND,
Jens Axboe2866c822006-10-09 15:57:48 +0200125};
Jens Axboe5f350952006-11-07 15:20:59 +0100126
127static void fio_init fio_mmapio_register(void)
128{
129 register_ioengine(&ioengine);
130}
131
132static void fio_exit fio_mmapio_unregister(void)
133{
134 unregister_ioengine(&ioengine);
135}