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 | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 16 | /* |
| 17 | * Limits us to 2GB of mapped files in total |
| 18 | */ |
| 19 | #define MMAP_TOTAL_SZ (2 * 1024 * 1024 * 1024UL) |
| 20 | |
| 21 | static unsigned long mmap_map_size; |
| 22 | static unsigned long mmap_map_mask; |
| 23 | |
| 24 | static int fio_mmap_file(struct thread_data *td, struct fio_file *f, |
| 25 | size_t length, off_t off) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 26 | { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 27 | int flags = 0; |
| 28 | int ret = 0; |
Jens Axboe | c97d836 | 2007-05-21 14:52:43 +0200 | [diff] [blame] | 29 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 30 | if (td_rw(td)) |
| 31 | flags = PROT_READ | PROT_WRITE; |
| 32 | else if (td_write(td)) { |
| 33 | flags = PROT_WRITE; |
| 34 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 35 | if (td->o.verify != VERIFY_NONE) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 36 | flags |= PROT_READ; |
| 37 | } else |
| 38 | flags = PROT_READ; |
| 39 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 40 | f->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off); |
| 41 | if (f->mmap_ptr == MAP_FAILED) { |
Jens Axboe | 8de43fd | 2009-05-20 10:49:44 +0200 | [diff] [blame] | 42 | int err = errno; |
| 43 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 44 | f->mmap_ptr = NULL; |
Jens Axboe | 8de43fd | 2009-05-20 10:49:44 +0200 | [diff] [blame] | 45 | td_verror(td, err, "mmap"); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 46 | goto err; |
| 47 | } |
| 48 | |
| 49 | if (file_invalidate_cache(td, f)) |
| 50 | goto err; |
| 51 | |
| 52 | if (!td_random(td)) { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 53 | if (madvise(f->mmap_ptr, length, MADV_SEQUENTIAL) < 0) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 54 | td_verror(td, errno, "madvise"); |
| 55 | goto err; |
| 56 | } |
| 57 | } else { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 58 | if (madvise(f->mmap_ptr, length, MADV_RANDOM) < 0) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 59 | td_verror(td, errno, "madvise"); |
| 60 | goto err; |
| 61 | } |
| 62 | } |
| 63 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 64 | err: |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 65 | return ret; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 68 | static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 69 | { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 70 | struct fio_file *f = io_u->file; |
| 71 | int ret = 0; |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 72 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 73 | if (io_u->buflen > mmap_map_size) { |
| 74 | log_err("fio: bs too big for mmap engine\n"); |
| 75 | ret = EIO; |
| 76 | goto err; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 77 | } |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 78 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 79 | if (io_u->offset >= f->mmap_off && |
| 80 | io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz) |
| 81 | goto done; |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 82 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 83 | if (f->mmap_ptr) { |
| 84 | if (munmap(f->mmap_ptr, f->mmap_sz) < 0) { |
| 85 | ret = errno; |
| 86 | goto err; |
| 87 | } |
| 88 | f->mmap_ptr = NULL; |
| 89 | } |
| 90 | |
| 91 | f->mmap_sz = mmap_map_size; |
| 92 | if (f->mmap_sz > f->io_size) |
| 93 | f->mmap_sz = f->io_size; |
| 94 | |
| 95 | f->mmap_off = io_u->offset & ~mmap_map_mask; |
| 96 | if (io_u->offset + io_u->buflen >= f->mmap_off + f->mmap_sz) |
| 97 | f->mmap_off -= io_u->buflen; |
| 98 | |
| 99 | ret = fio_mmap_file(td, f, f->mmap_sz, f->mmap_off); |
| 100 | done: |
| 101 | if (!ret) |
| 102 | io_u->mmap_data = f->mmap_ptr + io_u->offset - f->mmap_off - |
| 103 | f->file_offset; |
| 104 | err: |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 105 | return ret; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 108 | static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u) |
| 109 | { |
| 110 | struct fio_file *f = io_u->file; |
| 111 | |
| 112 | fio_ro_check(td, io_u); |
| 113 | |
| 114 | if (io_u->ddir == DDIR_READ) |
| 115 | memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen); |
| 116 | else if (io_u->ddir == DDIR_WRITE) |
| 117 | memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen); |
| 118 | else if (io_u->ddir == DDIR_SYNC) { |
| 119 | if (msync(f->mmap_ptr, f->mmap_sz, MS_SYNC)) { |
| 120 | io_u->error = errno; |
| 121 | td_verror(td, io_u->error, "msync"); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * not really direct, but should drop the pages from the cache |
| 127 | */ |
| 128 | if (td->o.odirect && io_u->ddir != DDIR_SYNC) { |
| 129 | if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) { |
| 130 | io_u->error = errno; |
| 131 | td_verror(td, io_u->error, "msync"); |
| 132 | } |
| 133 | if (madvise(io_u->mmap_data, io_u->xfer_buflen, MADV_DONTNEED) < 0) { |
| 134 | io_u->error = errno; |
| 135 | td_verror(td, io_u->error, "madvise"); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return FIO_Q_COMPLETED; |
| 140 | } |
| 141 | |
| 142 | static int fio_mmapio_init(struct thread_data *td) |
| 143 | { |
| 144 | unsigned long shift, mask; |
| 145 | |
| 146 | mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files; |
| 147 | mask = mmap_map_size; |
| 148 | shift = 0; |
| 149 | do { |
| 150 | mask >>= 1; |
| 151 | if (!mask) |
| 152 | break; |
| 153 | shift++; |
| 154 | } while (1); |
| 155 | |
| 156 | mmap_map_mask = 1UL << shift; |
| 157 | return 0; |
| 158 | } |
| 159 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 160 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 161 | .name = "mmap", |
| 162 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 163 | .init = fio_mmapio_init, |
| 164 | .prep = fio_mmapio_prep, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 165 | .queue = fio_mmapio_queue, |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 166 | .open_file = generic_open_file, |
| 167 | .close_file = generic_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 168 | .get_file_size = generic_get_file_size, |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame] | 169 | .flags = FIO_SYNCIO | FIO_NOEXTEND, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 170 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 171 | |
| 172 | static void fio_init fio_mmapio_register(void) |
| 173 | { |
| 174 | register_ioengine(&ioengine); |
| 175 | } |
| 176 | |
| 177 | static void fio_exit fio_mmapio_unregister(void) |
| 178 | { |
| 179 | unregister_ioengine(&ioengine); |
| 180 | } |