2 * regular read/write sync io engine
15 static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
17 struct fio_file *f = io_u->file;
18 unsigned long long real_off = io_u->offset - f->file_offset;
20 if (io_u->ddir == DDIR_READ)
21 memcpy(io_u->xfer_buf, f->mmap + real_off, io_u->xfer_buflen);
22 else if (io_u->ddir == DDIR_WRITE)
23 memcpy(f->mmap + real_off, io_u->xfer_buf, io_u->xfer_buflen);
24 else if (io_u->ddir == DDIR_SYNC) {
25 if (msync(f->mmap, f->file_size, MS_SYNC))
30 * not really direct, but should drop the pages from the cache
32 if (td->odirect && io_u->ddir != DDIR_SYNC) {
33 if (msync(f->mmap + real_off, io_u->xfer_buflen, MS_SYNC) < 0)
35 if (madvise(f->mmap + real_off, io_u->xfer_buflen, MADV_DONTNEED) < 0)
40 td_verror(td, io_u->error, "sync");
42 return FIO_Q_COMPLETED;
45 static int fio_mmapio_init(struct thread_data *td)
54 * We need to truncate the files to the right size, if
55 * we are writing to it.
57 for_each_file(td, f, i) {
58 if (ftruncate(f->fd, f->file_size) < 0) {
59 td_verror(td, errno, "ftruncate");
67 static int fio_mmapio_open(struct thread_data *td, struct fio_file *f)
71 ret = generic_open_file(td, f);
76 flags = PROT_READ | PROT_WRITE;
77 else if (td_write(td)) {
80 if (td->verify != VERIFY_NONE)
85 f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset);
86 if (f->mmap == MAP_FAILED) {
88 td_verror(td, errno, "mmap");
92 if (file_invalidate_cache(td, f))
96 if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) {
97 td_verror(td, errno, "madvise");
101 if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) {
102 td_verror(td, errno, "madvise");
111 munmap(f->mmap, f->file_size);
112 generic_close_file(td, f);
116 static void fio_mmapio_close(struct thread_data fio_unused *td,
120 munmap(f->mmap, f->file_size);
125 static struct ioengine_ops ioengine = {
127 .version = FIO_IOOPS_VERSION,
128 .queue = fio_mmapio_queue,
129 .init = fio_mmapio_init,
130 .open_file = fio_mmapio_open,
131 .close_file = fio_mmapio_close,
135 static void fio_init fio_mmapio_register(void)
137 register_ioengine(&ioengine);
140 static void fio_exit fio_mmapio_unregister(void)
142 unregister_ioengine(&ioengine);