Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * regular read/write sync io engine |
| 3 | * |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
| 10 | #include <sys/mman.h> |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 11 | |
| 12 | #include "../fio.h" |
| 13 | #include "../os.h" |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 14 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 15 | static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u) |
| 16 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 17 | struct fio_file *f = io_u->file; |
| 18 | unsigned long long real_off = io_u->offset - f->file_offset; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 19 | |
| 20 | if (io_u->ddir == DDIR_READ) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 21 | 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] | 22 | else if (io_u->ddir == DDIR_WRITE) |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 23 | 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] | 24 | else if (io_u->ddir == DDIR_SYNC) { |
| 25 | if (msync(f->mmap, f->file_size, MS_SYNC)) |
| 26 | io_u->error = errno; |
| 27 | } |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * not really direct, but should drop the pages from the cache |
| 31 | */ |
Jens Axboe | b907a5b | 2006-10-24 14:46:13 +0200 | [diff] [blame] | 32 | if (td->odirect && io_u->ddir != DDIR_SYNC) { |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 33 | if (msync(f->mmap + real_off, io_u->xfer_buflen, MS_SYNC) < 0) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 34 | io_u->error = errno; |
Jens Axboe | cec6b55 | 2007-02-06 20:15:38 +0100 | [diff] [blame] | 35 | if (madvise(f->mmap + real_off, io_u->xfer_buflen, MADV_DONTNEED) < 0) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 36 | io_u->error = errno; |
| 37 | } |
| 38 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 39 | if (io_u->error) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 40 | td_verror(td, io_u->error, "sync"); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 41 | |
Jens Axboe | 36167d8 | 2007-02-18 05:41:31 +0100 | [diff] [blame] | 42 | return FIO_Q_COMPLETED; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 45 | static int fio_mmapio_open(struct thread_data *td, struct fio_file *f) |
| 46 | { |
| 47 | int ret, flags; |
| 48 | |
| 49 | ret = generic_open_file(td, f); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | if (td_rw(td)) |
| 54 | flags = PROT_READ | PROT_WRITE; |
| 55 | else if (td_write(td)) { |
| 56 | flags = PROT_WRITE; |
| 57 | |
| 58 | if (td->verify != VERIFY_NONE) |
| 59 | flags |= PROT_READ; |
| 60 | } else |
| 61 | flags = PROT_READ; |
| 62 | |
| 63 | f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset); |
| 64 | if (f->mmap == MAP_FAILED) { |
| 65 | f->mmap = NULL; |
| 66 | td_verror(td, errno, "mmap"); |
| 67 | goto err; |
| 68 | } |
| 69 | |
| 70 | if (file_invalidate_cache(td, f)) |
| 71 | goto err; |
| 72 | |
| 73 | if (!td_random(td)) { |
| 74 | if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) { |
| 75 | td_verror(td, errno, "madvise"); |
| 76 | goto err; |
| 77 | } |
| 78 | } else { |
| 79 | if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) { |
| 80 | td_verror(td, errno, "madvise"); |
| 81 | goto err; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | |
| 87 | err: |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame^] | 88 | td->io_ops->close_file(td, f); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | static void fio_mmapio_close(struct thread_data fio_unused *td, |
| 93 | struct fio_file *f) |
| 94 | { |
| 95 | if (f->mmap) { |
| 96 | munmap(f->mmap, f->file_size); |
| 97 | f->mmap = NULL; |
| 98 | } |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame^] | 99 | generic_close_file(td, f); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 102 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 103 | .name = "mmap", |
| 104 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 105 | .queue = fio_mmapio_queue, |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 106 | .open_file = fio_mmapio_open, |
| 107 | .close_file = fio_mmapio_close, |
Jens Axboe | 0263882 | 2007-03-12 09:25:55 +0100 | [diff] [blame^] | 108 | .flags = FIO_SYNCIO | FIO_NOEXTEND, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 109 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 110 | |
| 111 | static void fio_init fio_mmapio_register(void) |
| 112 | { |
| 113 | register_ioengine(&ioengine); |
| 114 | } |
| 115 | |
| 116 | static void fio_exit fio_mmapio_unregister(void) |
| 117 | { |
| 118 | unregister_ioengine(&ioengine); |
| 119 | } |