Adding userspace_libaio_reap option
[fio.git] / engines / libaio.c
CommitLineData
2866c82d 1/*
da751ca9
JA
2 * libaio engine
3 *
4 * IO engine using the Linux native aio interface.
2866c82d
JA
5 *
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
11#include <assert.h>
5f350952
JA
12
13#include "../fio.h"
2866c82d 14
34cfcdaf
JA
15#ifdef FIO_HAVE_LIBAIO
16
2866c82d
JA
17#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
18
19struct libaio_data {
20 io_context_t aio_ctx;
21 struct io_event *aio_events;
755200a3 22 struct iocb **iocbs;
7e77dd02 23 struct io_u **io_us;
755200a3 24 int iocbs_nr;
2866c82d
JA
25};
26
7a16dd02 27static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
2866c82d 28{
53cdc686
JA
29 struct fio_file *f = io_u->file;
30
2866c82d 31 if (io_u->ddir == DDIR_READ)
cec6b55d 32 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
87dc1ab1 33 else if (io_u->ddir == DDIR_WRITE)
cec6b55d 34 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
5f9099ea 35 else if (ddir_sync(io_u->ddir))
87dc1ab1 36 io_prep_fsync(&io_u->iocb, f->fd);
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;
f423479d
JA
44 struct io_event *ev;
45 struct io_u *io_u;
2866c82d 46
f423479d
JA
47 ev = ld->aio_events + event;
48 io_u = ev_to_iou(ev);
49
50 if (ev->res != io_u->xfer_buflen) {
51 if (ev->res > io_u->xfer_buflen)
52 io_u->error = -ev->res;
53 else
54 io_u->resid = io_u->xfer_buflen - ev->res;
55 } else
56 io_u->error = 0;
57
58 return io_u;
2866c82d
JA
59}
60
675012f0
DE
61struct aio_ring {
62 unsigned id; /** kernel internal index number */
63 unsigned nr; /** number of io_events */
64 unsigned head;
65 unsigned tail;
66
67 unsigned magic;
68 unsigned compat_features;
69 unsigned incompat_features;
70 unsigned header_length; /** size of aio_ring */
71
72 struct io_event events[0];
73};
74
75#define AIO_RING_MAGIC 0xa10a10a1
76
77static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
78 struct io_event *events)
79{
80 long i = 0;
81 unsigned head;
82 struct aio_ring *ring = (struct aio_ring*)aio_ctx;
83
84 while (i < max) {
85 head = ring->head;
86
87 if (head == ring->tail) {
88 /* There are no more completions */
89 break;
90 } else {
91 /* There is another completion to reap */
92 events[i] = ring->events[head];
93 read_barrier();
94 ring->head = (head + 1) % ring->nr;
95 i++;
96 }
97 }
98
99 return i;
100}
101
e7d2e616
JA
102static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
103 unsigned int max, struct timespec *t)
2866c82d
JA
104{
105 struct libaio_data *ld = td->io_ops->data;
0b7fdba7
DE
106 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
107 int r, events = 0;
2866c82d
JA
108
109 do {
675012f0
DE
110 if (td->o.userspace_libaio_reap == 1
111 && actual_min == 0
112 && ((struct aio_ring *)(ld->aio_ctx))->magic
113 == AIO_RING_MAGIC) {
114 r = user_io_getevents(ld->aio_ctx, max,
115 ld->aio_events + events);
116 } else {
117 r = io_getevents(ld->aio_ctx, actual_min,
118 max, ld->aio_events + events, t);
119 }
0b7fdba7
DE
120 if (r >= 0)
121 events += r;
122 else if (r == -EAGAIN)
2866c82d 123 usleep(100);
0b7fdba7
DE
124 } while (events < min);
125
126 return r < 0 ? r : events;
2866c82d
JA
127}
128
129static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
130{
131 struct libaio_data *ld = td->io_ops->data;
2866c82d 132
7101d9c2
JA
133 fio_ro_check(td, io_u);
134
2dc1bbeb 135 if (ld->iocbs_nr == (int) td->o.iodepth)
755200a3
JA
136 return FIO_Q_BUSY;
137
138 /*
139 * fsync is tricky, since it can fail and we need to do it
140 * serialized with other io. the reason is that linux doesn't
141 * support aio fsync yet. So return busy for the case where we
142 * have pending io, to let fio complete those first.
143 */
f011531e 144 if (ddir_sync(io_u->ddir)) {
755200a3
JA
145 if (ld->iocbs_nr)
146 return FIO_Q_BUSY;
5f9099ea 147
f011531e 148 do_io_u_sync(td, io_u);
755200a3
JA
149 return FIO_Q_COMPLETED;
150 }
151
a5f3027c
JA
152 if (io_u->ddir == DDIR_TRIM) {
153 if (ld->iocbs_nr)
154 return FIO_Q_BUSY;
155
156 do_io_u_trim(td, io_u);
157 return FIO_Q_COMPLETED;
158 }
159
755200a3 160 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
7e77dd02 161 ld->io_us[ld->iocbs_nr] = io_u;
755200a3
JA
162 ld->iocbs_nr++;
163 return FIO_Q_QUEUED;
164}
165
7e77dd02
JA
166static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
167 unsigned int nr)
168{
169 struct timeval now;
170 unsigned int i;
171
12d9d841
JA
172 if (!fio_fill_issue_time(td))
173 return;
174
7e77dd02
JA
175 fio_gettime(&now, NULL);
176
177 for (i = 0; i < nr; i++) {
178 struct io_u *io_u = io_us[i];
179
180 memcpy(&io_u->issue_time, &now, sizeof(now));
181 io_u_queued(td, io_u);
182 }
183}
184
755200a3
JA
185static int fio_libaio_commit(struct thread_data *td)
186{
187 struct libaio_data *ld = td->io_ops->data;
188 struct iocb **iocbs;
7e77dd02 189 struct io_u **io_us;
5e00c2c4 190 int ret;
755200a3
JA
191
192 if (!ld->iocbs_nr)
193 return 0;
194
7e77dd02 195 io_us = ld->io_us;
755200a3 196 iocbs = ld->iocbs;
2866c82d 197 do {
5e00c2c4
JA
198 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
199 if (ret > 0) {
7e77dd02 200 fio_libaio_queued(td, io_us, ret);
838bc709 201 io_u_mark_submit(td, ret);
5e00c2c4 202 ld->iocbs_nr -= ret;
7e77dd02 203 io_us += ret;
755200a3 204 iocbs += ret;
5e00c2c4 205 ret = 0;
838bc709
JA
206 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
207 if (!ret)
208 io_u_mark_submit(td, ret);
2866c82d 209 continue;
838bc709 210 } else
2866c82d 211 break;
5e00c2c4 212 } while (ld->iocbs_nr);
2866c82d 213
36167d82 214 return ret;
2866c82d
JA
215}
216
217static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
218{
219 struct libaio_data *ld = td->io_ops->data;
220
221 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
222}
223
224static void fio_libaio_cleanup(struct thread_data *td)
225{
226 struct libaio_data *ld = td->io_ops->data;
227
228 if (ld) {
229 io_destroy(ld->aio_ctx);
7e77dd02
JA
230 free(ld->aio_events);
231 free(ld->iocbs);
232 free(ld->io_us);
2866c82d 233 free(ld);
2866c82d
JA
234 }
235}
236
237static int fio_libaio_init(struct thread_data *td)
238{
239 struct libaio_data *ld = malloc(sizeof(*ld));
c1db2dce 240 int err;
2866c82d
JA
241
242 memset(ld, 0, sizeof(*ld));
c1db2dce
JA
243
244 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
245 if (err) {
246 td_verror(td, -err, "io_queue_init");
75de55ac 247 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
cb781c75 248 free(ld);
2866c82d
JA
249 return 1;
250 }
251
2dc1bbeb
JA
252 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
253 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
254 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
755200a3 255 memset(ld->iocbs, 0, sizeof(struct iocb *));
2dc1bbeb
JA
256 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
257 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
755200a3
JA
258 ld->iocbs_nr = 0;
259
2866c82d
JA
260 td->io_ops->data = ld;
261 return 0;
262}
263
5f350952 264static struct ioengine_ops ioengine = {
2866c82d
JA
265 .name = "libaio",
266 .version = FIO_IOOPS_VERSION,
267 .init = fio_libaio_init,
268 .prep = fio_libaio_prep,
269 .queue = fio_libaio_queue,
755200a3 270 .commit = fio_libaio_commit,
2866c82d
JA
271 .cancel = fio_libaio_cancel,
272 .getevents = fio_libaio_getevents,
273 .event = fio_libaio_event,
274 .cleanup = fio_libaio_cleanup,
b5af8293
JA
275 .open_file = generic_open_file,
276 .close_file = generic_close_file,
df9c26b1 277 .get_file_size = generic_get_file_size,
2866c82d 278};
34cfcdaf
JA
279
280#else /* FIO_HAVE_LIBAIO */
281
282/*
283 * When we have a proper configure system in place, we simply wont build
284 * and install this io engine. For now install a crippled version that
285 * just complains and fails to load.
286 */
287static int fio_libaio_init(struct thread_data fio_unused *td)
288{
a3edaf76 289 log_err("fio: libaio not available\n");
34cfcdaf
JA
290 return 1;
291}
292
5f350952 293static struct ioengine_ops ioengine = {
34cfcdaf
JA
294 .name = "libaio",
295 .version = FIO_IOOPS_VERSION,
296 .init = fio_libaio_init,
297};
298
299#endif
5f350952
JA
300
301static void fio_init fio_libaio_register(void)
302{
303 register_ioengine(&ioengine);
304}
305
306static void fio_exit fio_libaio_unregister(void)
307{
308 unregister_ioengine(&ioengine);
309}