Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1 | /* |
Jens Axboe | da751ca | 2007-03-14 10:59:33 +0100 | [diff] [blame] | 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. |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 6 | * |
| 7 | */ |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <unistd.h> |
| 11 | #include <errno.h> |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 12 | #include <sys/mman.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 13 | |
| 14 | #include "../fio.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 15 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 16 | static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u) |
| 17 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 18 | struct fio_file *f = io_u->file; |
| 19 | unsigned long long real_off = io_u->offset - f->file_offset; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 20 | |
Jens Axboe | 7101d9c | 2007-09-12 13:12:39 +0200 | [diff] [blame] | 21 | fio_ro_check(td, io_u); |
| 22 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 23 | if (io_u->ddir == DDIR_READ) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 24 | memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen); |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 25 | else if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 26 | memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen); |
Jens Axboe | b907a5b | 2006-10-24 14:46:13 +0200 | [diff] [blame] | 27 | else if (io_u->ddir == DDIR_SYNC) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 28 | size_t len = (f->io_size + page_size - 1) & ~page_mask; |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 29 | |
| 30 | if (msync(f->mmap, len, MS_SYNC)) { |
Jens Axboe | b907a5b | 2006-10-24 14:46:13 +0200 | [diff] [blame] | 31 | io_u->error = errno; |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 32 | td_verror(td, io_u->error, "msync"); |
| 33 | } |
Jens Axboe | b907a5b | 2006-10-24 14:46:13 +0200 | [diff] [blame] | 34 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * not really direct, but should drop the pages from the cache |
| 38 | */ |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 39 | if (td->o.odirect && io_u->ddir != DDIR_SYNC) { |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 40 | size_t len = (io_u->xfer_buflen + page_size - 1) & ~page_mask; |
| 41 | unsigned long long off = real_off & ~page_mask; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 42 | |
Jens Axboe | cfc99db | 2007-03-14 10:34:47 +0100 | [diff] [blame] | 43 | if (msync(f->mmap + off, len, MS_SYNC) < 0) { |
| 44 | io_u->error = errno; |
| 45 | td_verror(td, io_u->error, "msync"); |
| 46 | } |
| 47 | if (madvise(f->mmap + off, len, MADV_DONTNEED) < 0) { |
| 48 | io_u->error = errno; |
| 49 | td_verror(td, io_u->error, "madvise"); |
| 50 | } |
| 51 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 52 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 53 | return FIO_Q_COMPLETED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 56 | static int fio_mmapio_open(struct thread_data *td, struct fio_file *f) |
| 57 | { |
| 58 | int ret, flags; |
| 59 | |
| 60 | ret = generic_open_file(td, f); |
| 61 | if (ret) |
| 62 | return ret; |
| 63 | |
Jens Axboe | c97d836 | 2007-05-21 14:52:43 +0200 | [diff] [blame] | 64 | /* |
| 65 | * for size checkup, don't mmap anything. |
| 66 | */ |
| 67 | if (!f->io_size) |
| 68 | return 0; |
| 69 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 70 | if (td_rw(td)) |
| 71 | flags = PROT_READ | PROT_WRITE; |
| 72 | else if (td_write(td)) { |
| 73 | flags = PROT_WRITE; |
| 74 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 75 | if (td->o.verify != VERIFY_NONE) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 76 | flags |= PROT_READ; |
| 77 | } else |
| 78 | flags = PROT_READ; |
| 79 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 80 | f->mmap = mmap(NULL, f->io_size, flags, MAP_SHARED, f->fd, f->file_offset); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 81 | if (f->mmap == MAP_FAILED) { |
| 82 | f->mmap = NULL; |
| 83 | td_verror(td, errno, "mmap"); |
| 84 | goto err; |
| 85 | } |
| 86 | |
| 87 | if (file_invalidate_cache(td, f)) |
| 88 | goto err; |
| 89 | |
| 90 | if (!td_random(td)) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 91 | if (madvise(f->mmap, f->io_size, MADV_SEQUENTIAL) < 0) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 92 | td_verror(td, errno, "madvise"); |
| 93 | goto err; |
| 94 | } |
| 95 | } else { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 96 | if (madvise(f->mmap, f->io_size, MADV_RANDOM) < 0) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 97 | td_verror(td, errno, "madvise"); |
| 98 | goto err; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | |
| 104 | err: |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame] | 105 | td->io_ops->close_file(td, f); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 106 | return 1; |
| 107 | } |
| 108 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 109 | static int fio_mmapio_close(struct thread_data fio_unused *td, |
| 110 | struct fio_file *f) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 111 | { |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 112 | int ret = 0, ret2; |
| 113 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 114 | if (f->mmap) { |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 115 | if (munmap(f->mmap, f->io_size) < 0) |
| 116 | ret = errno; |
| 117 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 118 | f->mmap = NULL; |
| 119 | } |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 120 | |
| 121 | ret2 = generic_close_file(td, f); |
| 122 | if (!ret && ret2) |
| 123 | ret = ret2; |
| 124 | |
| 125 | return ret; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 128 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 129 | .name = "mmap", |
| 130 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 131 | .queue = fio_mmapio_queue, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 132 | .open_file = fio_mmapio_open, |
| 133 | .close_file = fio_mmapio_close, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 134 | .get_file_size = generic_get_file_size, |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame] | 135 | .flags = FIO_SYNCIO | FIO_NOEXTEND, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 136 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 137 | |
| 138 | static void fio_init fio_mmapio_register(void) |
| 139 | { |
| 140 | register_ioengine(&ioengine); |
| 141 | } |
| 142 | |
| 143 | static void fio_exit fio_mmapio_unregister(void) |
| 144 | { |
| 145 | unregister_ioengine(&ioengine); |
| 146 | } |