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 | 95bcd81 | 2007-02-11 01:01:57 +0100 | [diff] [blame] | 40 | td_verror(td, io_u->error); |
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 | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 45 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 46 | .name = "mmap", |
| 47 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 48 | .queue = fio_mmapio_queue, |
Jens Axboe | d9257be | 2006-10-18 15:06:22 +0200 | [diff] [blame] | 49 | .flags = FIO_SYNCIO | FIO_MMAPIO, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 50 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 51 | |
| 52 | static void fio_init fio_mmapio_register(void) |
| 53 | { |
| 54 | register_ioengine(&ioengine); |
| 55 | } |
| 56 | |
| 57 | static void fio_exit fio_mmapio_unregister(void) |
| 58 | { |
| 59 | unregister_ioengine(&ioengine); |
| 60 | } |