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