c95600548c645449a07946b7cabb8c7b6de5162b
[fio.git] / engines / libaio.c
1 /*
2  * native linux aio 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
11 #include "../fio.h"
12 #include "../os.h"
13
14 #ifdef FIO_HAVE_LIBAIO
15
16 #define ev_to_iou(ev)   (struct io_u *) ((unsigned long) (ev)->obj)
17
18 struct libaio_data {
19         io_context_t aio_ctx;
20         struct io_event *aio_events;
21 };
22
23 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
24 {
25         struct fio_file *f = io_u->file;
26
27         if (io_u->ddir == DDIR_READ)
28                 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
29         else if (io_u->ddir == DDIR_WRITE)
30                 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
31         else if (io_u->ddir == DDIR_SYNC)
32                 io_prep_fsync(&io_u->iocb, f->fd);
33         else
34                 return 1;
35
36         return 0;
37 }
38
39 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
40 {
41         struct libaio_data *ld = td->io_ops->data;
42
43         return ev_to_iou(ld->aio_events + event);
44 }
45
46 static int fio_libaio_getevents(struct thread_data *td, int min, int max,
47                                 struct timespec *t)
48 {
49         struct libaio_data *ld = td->io_ops->data;
50         long r;
51
52         do {
53                 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
54                 if (r >= min)
55                         break;
56                 else if (r == -EAGAIN) {
57                         usleep(100);
58                         continue;
59                 } else if (r == -EINTR)
60                         continue;
61                 else if (r != 0)
62                         break;
63         } while (1);
64
65         if (r < 0)
66                 r = -r;
67
68         return (int) r;
69 }
70
71 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
72 {
73         struct libaio_data *ld = td->io_ops->data;
74         struct iocb *iocb = &io_u->iocb;
75         long ret;
76
77         do {
78                 ret = io_submit(ld->aio_ctx, 1, &iocb);
79                 if (ret == 1)
80                         return 0;
81                 else if (ret == -EAGAIN || !ret)
82                         usleep(100);
83                 else if (ret == -EINTR)
84                         continue;
85                 else if (ret == -EINVAL && io_u->ddir == DDIR_SYNC) {
86                         /*
87                          * the async fsync doesn't currently seem to be
88                          * supported, so just fsync if we fail with EINVAL
89                          * for a sync. since buffered io is also sync
90                          * with libaio (still), we don't have pending
91                          * requests to flush first.
92                          */
93                         ret = fsync(io_u->file->fd);
94                         break;
95                 } else
96                         break;
97         } while (1);
98
99         if (ret <= 0) {
100                 io_u->resid = io_u->xfer_buflen;
101                 io_u->error = -ret;
102                 return 1;
103         }
104
105         return 0;
106 }
107
108 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
109 {
110         struct libaio_data *ld = td->io_ops->data;
111
112         return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
113 }
114
115 static void fio_libaio_cleanup(struct thread_data *td)
116 {
117         struct libaio_data *ld = td->io_ops->data;
118
119         if (ld) {
120                 io_destroy(ld->aio_ctx);
121                 if (ld->aio_events)
122                         free(ld->aio_events);
123
124                 free(ld);
125                 td->io_ops->data = NULL;
126         }
127 }
128
129 static int fio_libaio_init(struct thread_data *td)
130 {
131         struct libaio_data *ld = malloc(sizeof(*ld));
132
133         memset(ld, 0, sizeof(*ld));
134         if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
135                 td_verror(td, errno);
136                 free(ld);
137                 return 1;
138         }
139
140         ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
141         memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
142         td->io_ops->data = ld;
143         return 0;
144 }
145
146 static struct ioengine_ops ioengine = {
147         .name           = "libaio",
148         .version        = FIO_IOOPS_VERSION,
149         .init           = fio_libaio_init,
150         .prep           = fio_libaio_prep,
151         .queue          = fio_libaio_queue,
152         .cancel         = fio_libaio_cancel,
153         .getevents      = fio_libaio_getevents,
154         .event          = fio_libaio_event,
155         .cleanup        = fio_libaio_cleanup,
156 };
157
158 #else /* FIO_HAVE_LIBAIO */
159
160 /*
161  * When we have a proper configure system in place, we simply wont build
162  * and install this io engine. For now install a crippled version that
163  * just complains and fails to load.
164  */
165 static int fio_libaio_init(struct thread_data fio_unused *td)
166 {
167         fprintf(stderr, "fio: libaio not available\n");
168         return 1;
169 }
170
171 static struct ioengine_ops ioengine = {
172         .name           = "libaio",
173         .version        = FIO_IOOPS_VERSION,
174         .init           = fio_libaio_init,
175 };
176
177 #endif
178
179 static void fio_init fio_libaio_register(void)
180 {
181         register_ioengine(&ioengine);
182 }
183
184 static void fio_exit fio_libaio_unregister(void)
185 {
186         unregister_ioengine(&ioengine);
187 }