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 | 948f9b2 | 2007-02-20 19:49:57 +0100 | [diff] [blame] | 45 | static int fio_mmapio_init(struct thread_data *td) |
| 46 | { |
| 47 | struct fio_file *f; |
| 48 | int i; |
| 49 | |
| 50 | if (td->ddir == DDIR_READ && !td_rw(td)) |
| 51 | return 0; |
| 52 | |
| 53 | /* |
| 54 | * We need to truncate the files to the right size, if |
| 55 | * we are writing to it. |
| 56 | */ |
| 57 | for_each_file(td, f, i) { |
| 58 | if (ftruncate(f->fd, f->file_size) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame^] | 59 | td_verror(td, errno, "ftruncate"); |
Jens Axboe | 948f9b2 | 2007-02-20 19:49:57 +0100 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 67 | static struct ioengine_ops ioengine = { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 68 | .name = "mmap", |
| 69 | .version = FIO_IOOPS_VERSION, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 70 | .queue = fio_mmapio_queue, |
Jens Axboe | 948f9b2 | 2007-02-20 19:49:57 +0100 | [diff] [blame] | 71 | .init = fio_mmapio_init, |
Jens Axboe | d9257be | 2006-10-18 15:06:22 +0200 | [diff] [blame] | 72 | .flags = FIO_SYNCIO | FIO_MMAPIO, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 73 | }; |
Jens Axboe | 5f35095 | 2006-11-07 15:20:59 +0100 | [diff] [blame] | 74 | |
| 75 | static void fio_init fio_mmapio_register(void) |
| 76 | { |
| 77 | register_ioengine(&ioengine); |
| 78 | } |
| 79 | |
| 80 | static void fio_exit fio_mmapio_unregister(void) |
| 81 | { |
| 82 | unregister_ioengine(&ioengine); |
| 83 | } |