blob: bcc296ffdbc2c1046e9d91774381c34748b4d1ea [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * libaio engine
3 *
4 * IO engine using the Linux native aio interface.
Jens Axboe2866c822006-10-09 15:57:48 +02005 *
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
11#include <assert.h>
Jens Axboe67bf9822013-01-10 11:23:19 +010012#include <libaio.h>
Jens Axboe5f350952006-11-07 15:20:59 +010013
14#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020015
Jens Axboe2866c822006-10-09 15:57:48 +020016struct libaio_data {
17 io_context_t aio_ctx;
18 struct io_event *aio_events;
Jens Axboe755200a2007-02-19 13:08:12 +010019 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +010020 struct io_u **io_us;
Jens Axboe241eb082014-09-30 13:33:23 -060021
22 /*
23 * Basic ring buffer. 'head' is incremented in _queue(), and
24 * 'tail' is incremented in _commit(). We keep 'queued' so
25 * that we know if the ring is full or empty, when
26 * 'head' == 'tail'. 'entries' is the ring size, and
27 * 'is_pow2' is just an optimization to use AND instead of
28 * modulus to get the remainder on ring increment.
29 */
30 int is_pow2;
31 unsigned int entries;
32 unsigned int queued;
33 unsigned int head;
34 unsigned int tail;
Jens Axboe2866c822006-10-09 15:57:48 +020035};
36
Steven Langde890a12011-11-09 14:03:34 +010037struct libaio_options {
38 struct thread_data *td;
39 unsigned int userspace_reap;
40};
41
42static struct fio_option options[] = {
43 {
44 .name = "userspace_reap",
Jens Axboee8b0e952012-03-19 14:37:08 +010045 .lname = "Libaio userspace reaping",
Steven Langde890a12011-11-09 14:03:34 +010046 .type = FIO_OPT_STR_SET,
47 .off1 = offsetof(struct libaio_options, userspace_reap),
48 .help = "Use alternative user-space reap implementation",
Jens Axboee90a0ad2013-04-10 19:43:59 +020049 .category = FIO_OPT_C_ENGINE,
Jens Axboe5a3cd5f2013-04-11 13:48:13 +020050 .group = FIO_OPT_G_LIBAIO,
Steven Langde890a12011-11-09 14:03:34 +010051 },
52 {
53 .name = NULL,
54 },
55};
56
Jens Axboe241eb082014-09-30 13:33:23 -060057static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
58 unsigned int add)
59{
60 if (ld->is_pow2)
61 *val = (*val + add) & (ld->entries - 1);
62 else
63 *val = (*val + add) % ld->entries;
64}
65
Jens Axboe7a16dd02006-10-18 17:21:58 +020066static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020067{
Jens Axboe53cdc682006-10-18 11:50:58 +020068 struct fio_file *f = io_u->file;
69
Jens Axboe2866c822006-10-09 15:57:48 +020070 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010071 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
Jens Axboe87dc1ab2006-10-24 14:41:26 +020072 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010073 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
Jens Axboe5f9099e2009-06-16 22:40:26 +020074 else if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020075 io_prep_fsync(&io_u->iocb, f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020076
77 return 0;
78}
79
80static struct io_u *fio_libaio_event(struct thread_data *td, int event)
81{
82 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010083 struct io_event *ev;
84 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020085
Jens Axboef4234792007-03-02 15:16:03 +010086 ev = ld->aio_events + event;
Jens Axboe0cc6ee52013-01-02 12:55:41 +010087 io_u = container_of(ev->obj, struct io_u, iocb);
Jens Axboef4234792007-03-02 15:16:03 +010088
89 if (ev->res != io_u->xfer_buflen) {
90 if (ev->res > io_u->xfer_buflen)
91 io_u->error = -ev->res;
92 else
93 io_u->resid = io_u->xfer_buflen - ev->res;
94 } else
95 io_u->error = 0;
96
97 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020098}
99
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600100struct aio_ring {
101 unsigned id; /** kernel internal index number */
102 unsigned nr; /** number of io_events */
103 unsigned head;
104 unsigned tail;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600105
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600106 unsigned magic;
107 unsigned compat_features;
108 unsigned incompat_features;
109 unsigned header_length; /** size of aio_ring */
110
111 struct io_event events[0];
112};
113
114#define AIO_RING_MAGIC 0xa10a10a1
115
116static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
Jens Axboec44b1ff2011-08-30 20:43:53 -0600117 struct io_event *events)
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600118{
119 long i = 0;
120 unsigned head;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600121 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600122
123 while (i < max) {
124 head = ring->head;
125
126 if (head == ring->tail) {
127 /* There are no more completions */
128 break;
129 } else {
130 /* There is another completion to reap */
131 events[i] = ring->events[head];
132 read_barrier();
Jens Axboec44b1ff2011-08-30 20:43:53 -0600133 ring->head = (head + 1) % ring->nr;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600134 i++;
135 }
136 }
137
138 return i;
139}
140
Jens Axboee7d2e612007-12-11 10:49:39 +0100141static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
142 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +0200143{
144 struct libaio_data *ld = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100145 struct libaio_options *o = td->eo;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200146 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
147 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200148
149 do {
Steven Langde890a12011-11-09 14:03:34 +0100150 if (o->userspace_reap == 1
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600151 && actual_min == 0
152 && ((struct aio_ring *)(ld->aio_ctx))->magic
153 == AIO_RING_MAGIC) {
154 r = user_io_getevents(ld->aio_ctx, max,
155 ld->aio_events + events);
156 } else {
157 r = io_getevents(ld->aio_ctx, actual_min,
158 max, ld->aio_events + events, t);
159 }
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200160 if (r >= 0)
161 events += r;
162 else if (r == -EAGAIN)
Jens Axboe2866c822006-10-09 15:57:48 +0200163 usleep(100);
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200164 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200165
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200166 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200167}
168
169static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
170{
171 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200172
Jens Axboe7101d9c2007-09-12 13:12:39 +0200173 fio_ro_check(td, io_u);
174
Jens Axboe241eb082014-09-30 13:33:23 -0600175 if (ld->queued == td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100176 return FIO_Q_BUSY;
177
178 /*
179 * fsync is tricky, since it can fail and we need to do it
180 * serialized with other io. the reason is that linux doesn't
181 * support aio fsync yet. So return busy for the case where we
182 * have pending io, to let fio complete those first.
183 */
Jens Axboef0115312010-03-09 21:47:15 +0100184 if (ddir_sync(io_u->ddir)) {
Jens Axboe241eb082014-09-30 13:33:23 -0600185 if (ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100186 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100187
Jens Axboef0115312010-03-09 21:47:15 +0100188 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200189 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100190 }
191
Jens Axboea5f30272010-07-19 16:19:55 -0600192 if (io_u->ddir == DDIR_TRIM) {
Jens Axboe241eb082014-09-30 13:33:23 -0600193 if (ld->queued)
Jens Axboea5f30272010-07-19 16:19:55 -0600194 return FIO_Q_BUSY;
195
196 do_io_u_trim(td, io_u);
197 return FIO_Q_COMPLETED;
198 }
199
Jens Axboe241eb082014-09-30 13:33:23 -0600200 ld->iocbs[ld->head] = &io_u->iocb;
201 ld->io_us[ld->head] = io_u;
202 ring_inc(ld, &ld->head, 1);
203 ld->queued++;
Jens Axboe755200a2007-02-19 13:08:12 +0100204 return FIO_Q_QUEUED;
205}
206
Jens Axboe7e77dd02007-02-20 10:57:34 +0100207static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
208 unsigned int nr)
209{
210 struct timeval now;
211 unsigned int i;
212
Jens Axboe12d9d842008-10-16 21:26:10 +0200213 if (!fio_fill_issue_time(td))
214 return;
215
Jens Axboe7e77dd02007-02-20 10:57:34 +0100216 fio_gettime(&now, NULL);
217
218 for (i = 0; i < nr; i++) {
219 struct io_u *io_u = io_us[i];
220
221 memcpy(&io_u->issue_time, &now, sizeof(now));
222 io_u_queued(td, io_u);
223 }
224}
225
Jens Axboe755200a2007-02-19 13:08:12 +0100226static int fio_libaio_commit(struct thread_data *td)
227{
228 struct libaio_data *ld = td->io_ops->data;
229 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100230 struct io_u **io_us;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100231 int ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100232
Jens Axboe241eb082014-09-30 13:33:23 -0600233 if (!ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100234 return 0;
235
Jens Axboe2866c822006-10-09 15:57:48 +0200236 do {
Jens Axboe241eb082014-09-30 13:33:23 -0600237 long nr = ld->queued;
238
239 nr = min((unsigned int) nr, ld->entries - ld->tail);
240 io_us = ld->io_us + ld->tail;
241 iocbs = ld->iocbs + ld->tail;
242
243 ret = io_submit(ld->aio_ctx, nr, iocbs);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100244 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100245 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200246 io_u_mark_submit(td, ret);
Jens Axboe241eb082014-09-30 13:33:23 -0600247
248 ld->queued -= ret;
249 ring_inc(ld, &ld->tail, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100250 ret = 0;
Jens Axboe241eb082014-09-30 13:33:23 -0600251 } else if (ret == -EINTR || !ret) {
Jens Axboe838bc702008-05-22 13:08:23 +0200252 if (!ret)
253 io_u_mark_submit(td, ret);
Jens Axboe2866c822006-10-09 15:57:48 +0200254 continue;
Jens Axboe241eb082014-09-30 13:33:23 -0600255 } else if (ret == -EAGAIN) {
256 /*
257 * If we get EAGAIN, we should break out without
258 * error and let the upper layer reap some
259 * events for us.
260 */
261 ret = 0;
262 break;
Jens Axboe838bc702008-05-22 13:08:23 +0200263 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200264 break;
Jens Axboe241eb082014-09-30 13:33:23 -0600265 } while (ld->head != ld->tail);
Jens Axboe2866c822006-10-09 15:57:48 +0200266
Jens Axboe36167d82007-02-18 05:41:31 +0100267 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200268}
269
270static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
271{
272 struct libaio_data *ld = td->io_ops->data;
273
274 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
275}
276
277static void fio_libaio_cleanup(struct thread_data *td)
278{
279 struct libaio_data *ld = td->io_ops->data;
280
281 if (ld) {
282 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100283 free(ld->aio_events);
284 free(ld->iocbs);
285 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200286 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200287 }
288}
289
290static int fio_libaio_init(struct thread_data *td)
291{
Jens Axboec90e1012012-09-04 11:47:00 -0600292 struct libaio_options *o = td->eo;
Jens Axboe241eb082014-09-30 13:33:23 -0600293 struct libaio_data *ld;
Jens Axboec90e1012012-09-04 11:47:00 -0600294 int err = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200295
Jens Axboe241eb082014-09-30 13:33:23 -0600296 ld = calloc(1, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100297
Jens Axboec90e1012012-09-04 11:47:00 -0600298 /*
299 * First try passing in 0 for queue depth, since we don't
300 * care about the user ring. If that fails, the kernel is too old
301 * and we need the right depth.
302 */
303 if (!o->userspace_reap)
Jens Axboec224ec02012-09-06 09:07:44 -0600304 err = io_queue_init(INT_MAX, &ld->aio_ctx);
Jens Axboec90e1012012-09-04 11:47:00 -0600305 if (o->userspace_reap || err == -EINVAL)
306 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
Jens Axboec1db2dc2007-03-20 10:42:07 +0100307 if (err) {
308 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200309 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100310 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200311 return 1;
312 }
313
Jens Axboe241eb082014-09-30 13:33:23 -0600314 ld->entries = td->o.iodepth;
315 ld->is_pow2 = is_power_of_2(ld->entries);
316 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
317 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
318 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100319
Jens Axboe2866c822006-10-09 15:57:48 +0200320 td->io_ops->data = ld;
321 return 0;
322}
323
Jens Axboe5f350952006-11-07 15:20:59 +0100324static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100325 .name = "libaio",
326 .version = FIO_IOOPS_VERSION,
327 .init = fio_libaio_init,
328 .prep = fio_libaio_prep,
329 .queue = fio_libaio_queue,
330 .commit = fio_libaio_commit,
331 .cancel = fio_libaio_cancel,
332 .getevents = fio_libaio_getevents,
333 .event = fio_libaio_event,
334 .cleanup = fio_libaio_cleanup,
335 .open_file = generic_open_file,
336 .close_file = generic_close_file,
337 .get_file_size = generic_get_file_size,
338 .options = options,
339 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200340};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100341
Jens Axboe5f350952006-11-07 15:20:59 +0100342static void fio_init fio_libaio_register(void)
343{
344 register_ioengine(&ioengine);
345}
346
347static void fio_exit fio_libaio_unregister(void)
348{
349 unregister_ioengine(&ioengine);
350}