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