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 | 4f5af7b | 2009-06-03 08:45:40 +0200 | [diff] [blame] | 15 | #include "../verify.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 16 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 17 | /* |
Jens Axboe | ff455a0 | 2009-06-29 11:54:56 +0200 | [diff] [blame] | 18 | * Limits us to 1GB of mapped files in total |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 19 | */ |
Jens Axboe | ff455a0 | 2009-06-29 11:54:56 +0200 | [diff] [blame] | 20 | #define MMAP_TOTAL_SZ (1 * 1024 * 1024 * 1024UL) |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 21 | |
| 22 | static unsigned long mmap_map_size; |
| 23 | static unsigned long mmap_map_mask; |
| 24 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 25 | struct fio_mmap_data { |
| 26 | void *mmap_ptr; |
| 27 | size_t mmap_sz; |
| 28 | off_t mmap_off; |
| 29 | }; |
| 30 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 31 | static int fio_mmap_file(struct thread_data *td, struct fio_file *f, |
| 32 | size_t length, off_t off) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 33 | { |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 34 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 35 | int flags = 0; |
Jens Axboe | c97d836 | 2007-05-21 14:52:43 +0200 | [diff] [blame] | 36 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 37 | if (td_rw(td)) |
| 38 | flags = PROT_READ | PROT_WRITE; |
| 39 | else if (td_write(td)) { |
| 40 | flags = PROT_WRITE; |
| 41 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 42 | if (td->o.verify != VERIFY_NONE) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 43 | flags |= PROT_READ; |
| 44 | } else |
| 45 | flags = PROT_READ; |
| 46 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 47 | fmd->mmap_ptr = mmap(NULL, length, flags, MAP_SHARED, f->fd, off); |
| 48 | if (fmd->mmap_ptr == MAP_FAILED) { |
| 49 | fmd->mmap_ptr = NULL; |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 50 | td_verror(td, errno, "mmap"); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 51 | goto err; |
| 52 | } |
| 53 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 54 | if (!td_random(td)) { |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 55 | if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_SEQUENTIAL) < 0) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 56 | td_verror(td, errno, "madvise"); |
| 57 | goto err; |
| 58 | } |
| 59 | } else { |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 60 | if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_RANDOM) < 0) { |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 61 | td_verror(td, errno, "madvise"); |
| 62 | goto err; |
| 63 | } |
| 64 | } |
Yoshinori Sato | 06f63e5 | 2015-01-07 12:16:36 +0900 | [diff] [blame^] | 65 | if (posix_madvise(fmd->mmap_ptr, length, POSIX_MADV_DONTNEED) < 0) { |
| 66 | td_verror(td, errno, "madvise"); |
| 67 | goto err; |
| 68 | } |
| 69 | |
| 70 | #ifdef FIO_MADV_FREE |
| 71 | if (f->filetype == FIO_TYPE_BD) |
| 72 | (void) posix_madvise(fmd->mmap_ptr, fmd->mmap_sz, FIO_MADV_FREE); |
| 73 | #endif |
| 74 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 75 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 76 | err: |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 77 | if (td->error && fmd->mmap_ptr) |
| 78 | munmap(fmd->mmap_ptr, length); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 79 | |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 80 | return td->error; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 83 | /* |
| 84 | * Just mmap an appropriate portion, we cannot mmap the full extent |
| 85 | */ |
| 86 | static int fio_mmapio_prep_limited(struct thread_data *td, struct io_u *io_u) |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 87 | { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 88 | struct fio_file *f = io_u->file; |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 89 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 90 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 91 | if (io_u->buflen > mmap_map_size) { |
| 92 | log_err("fio: bs too big for mmap engine\n"); |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 93 | return EIO; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 94 | } |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 95 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 96 | fmd->mmap_sz = mmap_map_size; |
| 97 | if (fmd->mmap_sz > f->io_size) |
| 98 | fmd->mmap_sz = f->io_size; |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 99 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 100 | fmd->mmap_off = io_u->offset; |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 101 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 102 | return fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off); |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Attempt to mmap the entire file |
| 107 | */ |
| 108 | static int fio_mmapio_prep_full(struct thread_data *td, struct io_u *io_u) |
| 109 | { |
| 110 | struct fio_file *f = io_u->file; |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 111 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 112 | int ret; |
| 113 | |
| 114 | if (fio_file_partial_mmap(f)) |
| 115 | return EINVAL; |
Jens Axboe | e8f198b | 2014-12-16 10:35:07 -0700 | [diff] [blame] | 116 | if (io_u->offset != (size_t) io_u->offset || |
| 117 | f->io_size != (size_t) f->io_size) { |
| 118 | fio_file_set_partial_mmap(f); |
| 119 | return EINVAL; |
| 120 | } |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 121 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 122 | fmd->mmap_sz = f->io_size; |
| 123 | fmd->mmap_off = 0; |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 124 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 125 | ret = fio_mmap_file(td, f, fmd->mmap_sz, fmd->mmap_off); |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 126 | if (ret) |
| 127 | fio_file_set_partial_mmap(f); |
| 128 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 129 | return ret; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 132 | static int fio_mmapio_prep(struct thread_data *td, struct io_u *io_u) |
| 133 | { |
| 134 | struct fio_file *f = io_u->file; |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 135 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 136 | int ret; |
| 137 | |
Jens Axboe | 8f933ca | 2009-07-04 20:29:04 +0200 | [diff] [blame] | 138 | /* |
| 139 | * It fits within existing mapping, use it |
| 140 | */ |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 141 | if (io_u->offset >= fmd->mmap_off && |
| 142 | io_u->offset + io_u->buflen < fmd->mmap_off + fmd->mmap_sz) |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 143 | goto done; |
| 144 | |
Jens Axboe | 8f933ca | 2009-07-04 20:29:04 +0200 | [diff] [blame] | 145 | /* |
| 146 | * unmap any existing mapping |
| 147 | */ |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 148 | if (fmd->mmap_ptr) { |
| 149 | if (munmap(fmd->mmap_ptr, fmd->mmap_sz) < 0) |
Jens Axboe | 8f933ca | 2009-07-04 20:29:04 +0200 | [diff] [blame] | 150 | return errno; |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 151 | fmd->mmap_ptr = NULL; |
Jens Axboe | 8f933ca | 2009-07-04 20:29:04 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 154 | if (fio_mmapio_prep_full(td, io_u)) { |
| 155 | td_clear_error(td); |
| 156 | ret = fio_mmapio_prep_limited(td, io_u); |
| 157 | if (ret) |
| 158 | return ret; |
| 159 | } |
| 160 | |
| 161 | done: |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 162 | io_u->mmap_data = fmd->mmap_ptr + io_u->offset - fmd->mmap_off - |
Jens Axboe | ed47cbf | 2009-07-03 22:52:38 +0200 | [diff] [blame] | 163 | f->file_offset; |
| 164 | return 0; |
| 165 | } |
| 166 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 167 | static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u) |
| 168 | { |
| 169 | struct fio_file *f = io_u->file; |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 170 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 171 | |
| 172 | fio_ro_check(td, io_u); |
| 173 | |
| 174 | if (io_u->ddir == DDIR_READ) |
| 175 | memcpy(io_u->xfer_buf, io_u->mmap_data, io_u->xfer_buflen); |
| 176 | else if (io_u->ddir == DDIR_WRITE) |
| 177 | memcpy(io_u->mmap_data, io_u->xfer_buf, io_u->xfer_buflen); |
Jens Axboe | 5f9099e | 2009-06-16 22:40:26 +0200 | [diff] [blame] | 178 | else if (ddir_sync(io_u->ddir)) { |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 179 | if (msync(fmd->mmap_ptr, fmd->mmap_sz, MS_SYNC)) { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 180 | io_u->error = errno; |
| 181 | td_verror(td, io_u->error, "msync"); |
| 182 | } |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 183 | } else if (io_u->ddir == DDIR_TRIM) { |
| 184 | int ret = do_io_u_trim(td, io_u); |
| 185 | |
| 186 | if (!ret) |
| 187 | td_verror(td, io_u->error, "trim"); |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 190 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 191 | /* |
| 192 | * not really direct, but should drop the pages from the cache |
| 193 | */ |
Jens Axboe | ff58fce | 2010-08-25 12:02:08 +0200 | [diff] [blame] | 194 | if (td->o.odirect && ddir_rw(io_u->ddir)) { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 195 | if (msync(io_u->mmap_data, io_u->xfer_buflen, MS_SYNC) < 0) { |
| 196 | io_u->error = errno; |
| 197 | td_verror(td, io_u->error, "msync"); |
| 198 | } |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 199 | if (posix_madvise(io_u->mmap_data, io_u->xfer_buflen, POSIX_MADV_DONTNEED) < 0) { |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 200 | io_u->error = errno; |
| 201 | td_verror(td, io_u->error, "madvise"); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | return FIO_Q_COMPLETED; |
| 206 | } |
| 207 | |
| 208 | static int fio_mmapio_init(struct thread_data *td) |
| 209 | { |
Jens Axboe | 913ea0d | 2011-01-22 15:11:13 -0700 | [diff] [blame] | 210 | struct thread_options *o = &td->o; |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 211 | unsigned long shift, mask; |
| 212 | |
Jens Axboe | 913ea0d | 2011-01-22 15:11:13 -0700 | [diff] [blame] | 213 | if ((td->o.rw_min_bs & page_mask) && |
| 214 | (o->odirect || o->fsync_blocks || o->fdatasync_blocks)) { |
| 215 | log_err("fio: mmap options dictate a minimum block size of " |
Jens Axboe | 4e0a8fa | 2013-04-15 11:40:57 +0200 | [diff] [blame] | 216 | "%llu bytes\n", (unsigned long long) page_size); |
Jens Axboe | 913ea0d | 2011-01-22 15:11:13 -0700 | [diff] [blame] | 217 | return 1; |
| 218 | } |
| 219 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 220 | mmap_map_size = MMAP_TOTAL_SZ / td->o.nr_files; |
| 221 | mask = mmap_map_size; |
| 222 | shift = 0; |
| 223 | do { |
| 224 | mask >>= 1; |
| 225 | if (!mask) |
| 226 | break; |
| 227 | shift++; |
| 228 | } while (1); |
Bruce Cran | 93bcfd2 | 2012-02-20 20:18:19 +0100 | [diff] [blame] | 229 | |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 230 | mmap_map_mask = 1UL << shift; |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 234 | static int fio_mmapio_open_file(struct thread_data *td, struct fio_file *f) |
| 235 | { |
| 236 | struct fio_mmap_data *fmd; |
| 237 | int ret; |
| 238 | |
| 239 | ret = generic_open_file(td, f); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | |
| 243 | fmd = calloc(1, sizeof(*fmd)); |
| 244 | if (!fmd) { |
| 245 | int fio_unused ret; |
| 246 | ret = generic_close_file(td, f); |
| 247 | return 1; |
| 248 | } |
| 249 | |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 250 | FILE_SET_ENG_DATA(f, fmd); |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int fio_mmapio_close_file(struct thread_data *td, struct fio_file *f) |
| 255 | { |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 256 | struct fio_mmap_data *fmd = FILE_ENG_DATA(f); |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 257 | |
Jens Axboe | aa31de7 | 2014-12-15 08:26:11 -0700 | [diff] [blame] | 258 | FILE_SET_ENG_DATA(f, NULL); |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 259 | free(fmd); |
Jens Axboe | e124390 | 2014-12-16 10:46:24 -0700 | [diff] [blame] | 260 | fio_file_clear_partial_mmap(f); |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 261 | |
| 262 | return generic_close_file(td, f); |
| 263 | } |
| 264 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 265 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 266 | .name = "mmap", |
| 267 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | ac89311 | 2009-06-02 13:06:01 +0200 | [diff] [blame] | 268 | .init = fio_mmapio_init, |
| 269 | .prep = fio_mmapio_prep, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 270 | .queue = fio_mmapio_queue, |
Jens Axboe | eea6bed | 2014-12-14 19:17:27 -0700 | [diff] [blame] | 271 | .open_file = fio_mmapio_open_file, |
| 272 | .close_file = fio_mmapio_close_file, |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 273 | .get_file_size = generic_get_file_size, |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame] | 274 | .flags = FIO_SYNCIO | FIO_NOEXTEND, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 275 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 276 | |
| 277 | static void fio_init fio_mmapio_register(void) |
| 278 | { |
| 279 | register_ioengine(&ioengine); |
| 280 | } |
| 281 | |
| 282 | static void fio_exit fio_mmapio_unregister(void) |
| 283 | { |
| 284 | unregister_ioengine(&ioengine); |
| 285 | } |