Be safe and re-prep requeue io_u's
[fio.git] / engines / libaio.c
CommitLineData
2866c82d
JA
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>
5f350952
JA
10
11#include "../fio.h"
12#include "../os.h"
2866c82d 13
34cfcdaf
JA
14#ifdef FIO_HAVE_LIBAIO
15
2866c82d
JA
16#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
17
18struct libaio_data {
19 io_context_t aio_ctx;
20 struct io_event *aio_events;
755200a3
JA
21 struct iocb **iocbs;
22 int iocbs_nr;
2866c82d
JA
23};
24
7a16dd02 25static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
2866c82d 26{
53cdc686
JA
27 struct fio_file *f = io_u->file;
28
2866c82d 29 if (io_u->ddir == DDIR_READ)
cec6b55d 30 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
87dc1ab1 31 else if (io_u->ddir == DDIR_WRITE)
cec6b55d 32 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
87dc1ab1
JA
33 else if (io_u->ddir == DDIR_SYNC)
34 io_prep_fsync(&io_u->iocb, f->fd);
35 else
36 return 1;
2866c82d
JA
37
38 return 0;
39}
40
41static struct io_u *fio_libaio_event(struct thread_data *td, int event)
42{
43 struct libaio_data *ld = td->io_ops->data;
44
45 return ev_to_iou(ld->aio_events + event);
46}
47
48static int fio_libaio_getevents(struct thread_data *td, int min, int max,
49 struct timespec *t)
50{
51 struct libaio_data *ld = td->io_ops->data;
52 long r;
53
54 do {
55 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
ccbb91cb
JA
56 if (r >= min)
57 break;
58 else if (r == -EAGAIN) {
2866c82d
JA
59 usleep(100);
60 continue;
61 } else if (r == -EINTR)
62 continue;
84585003 63 else if (r != 0)
2866c82d
JA
64 break;
65 } while (1);
66
22819ec2 67 return r;
2866c82d
JA
68}
69
70static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
71{
72 struct libaio_data *ld = td->io_ops->data;
2866c82d 73
755200a3
JA
74 if (ld->iocbs_nr == (int) td->iodepth)
75 return FIO_Q_BUSY;
76
77 /*
78 * fsync is tricky, since it can fail and we need to do it
79 * serialized with other io. the reason is that linux doesn't
80 * support aio fsync yet. So return busy for the case where we
81 * have pending io, to let fio complete those first.
82 */
83 if (io_u->ddir == DDIR_SYNC) {
84 if (ld->iocbs_nr)
85 return FIO_Q_BUSY;
86 if (fsync(io_u->file->fd) < 0)
87 io_u->error = errno;
88
89 return FIO_Q_COMPLETED;
90 }
91
92 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
93 ld->iocbs_nr++;
94 return FIO_Q_QUEUED;
95}
96
97static int fio_libaio_commit(struct thread_data *td)
98{
99 struct libaio_data *ld = td->io_ops->data;
100 struct iocb **iocbs;
101 int ret, iocbs_nr;
102
103 if (!ld->iocbs_nr)
104 return 0;
105
106 iocbs_nr = ld->iocbs_nr;
107 iocbs = ld->iocbs;
2866c82d 108 do {
755200a3
JA
109 ret = io_submit(ld->aio_ctx, iocbs_nr, iocbs);
110 if (ret == iocbs_nr) {
111 ret = 0;
112 break;
113 } else if (ret > 0) {
114 iocbs += ret;
115 iocbs_nr -= ret;
116 continue;
117 } else if (ret == -EAGAIN || !ret)
2866c82d
JA
118 usleep(100);
119 else if (ret == -EINTR)
120 continue;
755200a3 121 else
2866c82d
JA
122 break;
123 } while (1);
124
755200a3
JA
125 if (!ret)
126 ld->iocbs_nr = 0;
2866c82d 127
36167d82 128 return ret;
2866c82d
JA
129}
130
131static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
132{
133 struct libaio_data *ld = td->io_ops->data;
134
135 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
136}
137
138static void fio_libaio_cleanup(struct thread_data *td)
139{
140 struct libaio_data *ld = td->io_ops->data;
141
142 if (ld) {
143 io_destroy(ld->aio_ctx);
144 if (ld->aio_events)
145 free(ld->aio_events);
755200a3
JA
146 if (ld->iocbs)
147 free(ld->iocbs);
2866c82d
JA
148
149 free(ld);
150 td->io_ops->data = NULL;
151 }
152}
153
154static int fio_libaio_init(struct thread_data *td)
155{
156 struct libaio_data *ld = malloc(sizeof(*ld));
157
158 memset(ld, 0, sizeof(*ld));
159 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
160 td_verror(td, errno);
cb781c75 161 free(ld);
2866c82d
JA
162 return 1;
163 }
164
165 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
84585003 166 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
755200a3
JA
167 ld->iocbs = malloc(td->iodepth * sizeof(struct iocb *));
168 memset(ld->iocbs, 0, sizeof(struct iocb *));
169 ld->iocbs_nr = 0;
170
2866c82d
JA
171 td->io_ops->data = ld;
172 return 0;
173}
174
5f350952 175static struct ioengine_ops ioengine = {
2866c82d
JA
176 .name = "libaio",
177 .version = FIO_IOOPS_VERSION,
178 .init = fio_libaio_init,
179 .prep = fio_libaio_prep,
180 .queue = fio_libaio_queue,
755200a3 181 .commit = fio_libaio_commit,
2866c82d
JA
182 .cancel = fio_libaio_cancel,
183 .getevents = fio_libaio_getevents,
184 .event = fio_libaio_event,
185 .cleanup = fio_libaio_cleanup,
2866c82d 186};
34cfcdaf
JA
187
188#else /* FIO_HAVE_LIBAIO */
189
190/*
191 * When we have a proper configure system in place, we simply wont build
192 * and install this io engine. For now install a crippled version that
193 * just complains and fails to load.
194 */
195static int fio_libaio_init(struct thread_data fio_unused *td)
196{
197 fprintf(stderr, "fio: libaio not available\n");
198 return 1;
199}
200
5f350952 201static struct ioengine_ops ioengine = {
34cfcdaf
JA
202 .name = "libaio",
203 .version = FIO_IOOPS_VERSION,
204 .init = fio_libaio_init,
205};
206
207#endif
5f350952
JA
208
209static void fio_init fio_libaio_register(void)
210{
211 register_ioengine(&ioengine);
212}
213
214static void fio_exit fio_libaio_unregister(void)
215{
216 unregister_ioengine(&ioengine);
217}