[PATCH] Shrink io_u a little
[fio.git] / engines / fio-engine-mmap.c
CommitLineData
2866c82d
JA
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>
11#include "fio.h"
12#include "os.h"
13
14struct mmapio_data {
15 struct io_u *last_io_u;
16};
17
18static int fio_mmapio_getevents(struct thread_data *td, int fio_unused min,
19 int max, struct timespec fio_unused *t)
20{
21 assert(max <= 1);
22
23 /*
24 * we can only have one finished io_u for sync io, since the depth
25 * is always 1
26 */
27 if (list_empty(&td->io_u_busylist))
28 return 0;
29
30 return 1;
31}
32
33static struct io_u *fio_mmapio_event(struct thread_data *td, int event)
34{
35 struct mmapio_data *sd = td->io_ops->data;
36
37 assert(event == 0);
38
39 return sd->last_io_u;
40}
41
42
43static int fio_mmapio_queue(struct thread_data *td, struct io_u *io_u)
44{
53cdc686
JA
45 struct fio_file *f = io_u->file;
46 unsigned long long real_off = io_u->offset - f->file_offset;
2866c82d
JA
47 struct mmapio_data *sd = td->io_ops->data;
48
49 if (io_u->ddir == DDIR_READ)
53cdc686 50 memcpy(io_u->buf, f->mmap + real_off, io_u->buflen);
2866c82d 51 else
53cdc686 52 memcpy(f->mmap + real_off, io_u->buf, io_u->buflen);
2866c82d
JA
53
54 /*
55 * not really direct, but should drop the pages from the cache
56 */
57 if (td->odirect) {
53cdc686 58 if (msync(f->mmap + real_off, io_u->buflen, MS_SYNC) < 0)
2866c82d 59 io_u->error = errno;
53cdc686 60 if (madvise(f->mmap + real_off, io_u->buflen, MADV_DONTNEED) < 0)
2866c82d
JA
61 io_u->error = errno;
62 }
63
64 if (!io_u->error)
65 sd->last_io_u = io_u;
66
67 return io_u->error;
68}
69
7a16dd02
JA
70static int fio_mmapio_sync(struct thread_data fio_unused *td,
71 struct fio_file *f)
2866c82d 72{
53cdc686 73 return msync(f->mmap, f->file_size, MS_SYNC);
2866c82d
JA
74}
75
76static void fio_mmapio_cleanup(struct thread_data *td)
77{
78 if (td->io_ops->data) {
79 free(td->io_ops->data);
80 td->io_ops->data = NULL;
81 }
82}
83
84static int fio_mmapio_init(struct thread_data *td)
85{
86 struct mmapio_data *sd = malloc(sizeof(*sd));
87
88 sd->last_io_u = NULL;
89 td->io_ops->data = sd;
90 return 0;
91}
92
93struct ioengine_ops ioengine = {
94 .name = "mmap",
95 .version = FIO_IOOPS_VERSION,
96 .init = fio_mmapio_init,
97 .queue = fio_mmapio_queue,
98 .getevents = fio_mmapio_getevents,
99 .event = fio_mmapio_event,
100 .cleanup = fio_mmapio_cleanup,
101 .sync = fio_mmapio_sync,
d9257bea 102 .flags = FIO_SYNCIO | FIO_MMAPIO,
2866c82d 103};