blob: bd59b28a6cf99f0900f978d175cbb68c336dfa00 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
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 Axboe5f350952006-11-07 15:20:59 +010011
12#include "../fio.h"
13#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020014
Jens Axboe2866c822006-10-09 15:57:48 +020015static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
16{
Jens Axboe53cdc682006-10-18 11:50:58 +020017 struct fio_file *f = io_u->file;
18 unsigned long long real_off = io_u->offset - f->file_offset;
Jens Axboe2866c822006-10-09 15:57:48 +020019
20 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010021 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020022 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010023 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
Jens Axboeb907a5b2006-10-24 14:46:13 +020024 else if (io_u->ddir == DDIR_SYNC) {
25 if (msync(f->mmap, f->file_size, MS_SYNC))
26 io_u->error = errno;
27 }
Jens Axboe2866c822006-10-09 15:57:48 +020028
29 /*
30 * not really direct, but should drop the pages from the cache
31 */
Jens Axboeb907a5b2006-10-24 14:46:13 +020032 if (td->odirect && io_u->ddir != DDIR_SYNC) {
Jens Axboecec6b552007-02-06 20:15:38 +010033 if (msync(f->mmap + real_off, io_u->xfer_buflen, MS_SYNC) < 0)
Jens Axboe2866c822006-10-09 15:57:48 +020034 io_u->error = errno;
Jens Axboecec6b552007-02-06 20:15:38 +010035 if (madvise(f->mmap + real_off, io_u->xfer_buflen, MADV_DONTNEED) < 0)
Jens Axboe2866c822006-10-09 15:57:48 +020036 io_u->error = errno;
37 }
38
Jens Axboe36167d82007-02-18 05:41:31 +010039 if (io_u->error)
Jens Axboee1161c32007-02-22 19:36:48 +010040 td_verror(td, io_u->error, "sync");
Jens Axboe2866c822006-10-09 15:57:48 +020041
Jens Axboe36167d82007-02-18 05:41:31 +010042 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +020043}
44
Jens Axboe948f9b22007-02-20 19:49:57 +010045static int fio_mmapio_init(struct thread_data *td)
46{
47 struct fio_file *f;
48 int i;
49
Jens Axboe413dd452007-02-23 09:26:09 +010050 if (!td_write(td))
Jens Axboe948f9b22007-02-20 19:49:57 +010051 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 Axboee1161c32007-02-22 19:36:48 +010059 td_verror(td, errno, "ftruncate");
Jens Axboe948f9b22007-02-20 19:49:57 +010060 return 1;
61 }
62 }
63
64 return 0;
65}
66
Jens Axboe5f350952006-11-07 15:20:59 +010067static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +020068 .name = "mmap",
69 .version = FIO_IOOPS_VERSION,
Jens Axboe2866c822006-10-09 15:57:48 +020070 .queue = fio_mmapio_queue,
Jens Axboe948f9b22007-02-20 19:49:57 +010071 .init = fio_mmapio_init,
Jens Axboed9257be2006-10-18 15:06:22 +020072 .flags = FIO_SYNCIO | FIO_MMAPIO,
Jens Axboe2866c822006-10-09 15:57:48 +020073};
Jens Axboe5f350952006-11-07 15:20:59 +010074
75static void fio_init fio_mmapio_register(void)
76{
77 register_ioengine(&ioengine);
78}
79
80static void fio_exit fio_mmapio_unregister(void)
81{
82 unregister_ioengine(&ioengine);
83}