blob: 6f625167e9b7d2f03350a72086fb67db1e421885 [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,
Jens Axboe0cbbc392014-09-30 16:04:12 -0600142 unsigned int max, const 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;
Jens Axboe0cbbc392014-09-30 16:04:12 -0600147 struct timespec __lt, *lt = NULL;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200148 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200149
Jens Axboe0cbbc392014-09-30 16:04:12 -0600150 if (t) {
151 __lt = *t;
152 lt = &__lt;
153 }
154
Jens Axboe2866c822006-10-09 15:57:48 +0200155 do {
Steven Langde890a12011-11-09 14:03:34 +0100156 if (o->userspace_reap == 1
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600157 && actual_min == 0
158 && ((struct aio_ring *)(ld->aio_ctx))->magic
159 == AIO_RING_MAGIC) {
160 r = user_io_getevents(ld->aio_ctx, max,
161 ld->aio_events + events);
162 } else {
163 r = io_getevents(ld->aio_ctx, actual_min,
Jens Axboe0cbbc392014-09-30 16:04:12 -0600164 max, ld->aio_events + events, lt);
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600165 }
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200166 if (r >= 0)
167 events += r;
168 else if (r == -EAGAIN)
Jens Axboe2866c822006-10-09 15:57:48 +0200169 usleep(100);
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200170 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200171
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200172 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200173}
174
175static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
176{
177 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200178
Jens Axboe7101d9c2007-09-12 13:12:39 +0200179 fio_ro_check(td, io_u);
180
Jens Axboe241eb082014-09-30 13:33:23 -0600181 if (ld->queued == td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100182 return FIO_Q_BUSY;
183
184 /*
185 * fsync is tricky, since it can fail and we need to do it
186 * serialized with other io. the reason is that linux doesn't
187 * support aio fsync yet. So return busy for the case where we
188 * have pending io, to let fio complete those first.
189 */
Jens Axboef0115312010-03-09 21:47:15 +0100190 if (ddir_sync(io_u->ddir)) {
Jens Axboe241eb082014-09-30 13:33:23 -0600191 if (ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100192 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100193
Jens Axboef0115312010-03-09 21:47:15 +0100194 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200195 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100196 }
197
Jens Axboea5f30272010-07-19 16:19:55 -0600198 if (io_u->ddir == DDIR_TRIM) {
Jens Axboe241eb082014-09-30 13:33:23 -0600199 if (ld->queued)
Jens Axboea5f30272010-07-19 16:19:55 -0600200 return FIO_Q_BUSY;
201
202 do_io_u_trim(td, io_u);
203 return FIO_Q_COMPLETED;
204 }
205
Jens Axboe241eb082014-09-30 13:33:23 -0600206 ld->iocbs[ld->head] = &io_u->iocb;
207 ld->io_us[ld->head] = io_u;
208 ring_inc(ld, &ld->head, 1);
209 ld->queued++;
Jens Axboe755200a2007-02-19 13:08:12 +0100210 return FIO_Q_QUEUED;
211}
212
Jens Axboe7e77dd02007-02-20 10:57:34 +0100213static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
214 unsigned int nr)
215{
216 struct timeval now;
217 unsigned int i;
218
Jens Axboe12d9d842008-10-16 21:26:10 +0200219 if (!fio_fill_issue_time(td))
220 return;
221
Jens Axboe7e77dd02007-02-20 10:57:34 +0100222 fio_gettime(&now, NULL);
223
224 for (i = 0; i < nr; i++) {
225 struct io_u *io_u = io_us[i];
226
227 memcpy(&io_u->issue_time, &now, sizeof(now));
228 io_u_queued(td, io_u);
229 }
230}
231
Jens Axboe755200a2007-02-19 13:08:12 +0100232static int fio_libaio_commit(struct thread_data *td)
233{
234 struct libaio_data *ld = td->io_ops->data;
235 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100236 struct io_u **io_us;
Jens Axboe841ffe52014-09-30 20:28:45 -0600237 struct timeval tv;
238 int ret, wait_start = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100239
Jens Axboe241eb082014-09-30 13:33:23 -0600240 if (!ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100241 return 0;
242
Jens Axboe2866c822006-10-09 15:57:48 +0200243 do {
Jens Axboe241eb082014-09-30 13:33:23 -0600244 long nr = ld->queued;
245
246 nr = min((unsigned int) nr, ld->entries - ld->tail);
247 io_us = ld->io_us + ld->tail;
248 iocbs = ld->iocbs + ld->tail;
249
250 ret = io_submit(ld->aio_ctx, nr, iocbs);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100251 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100252 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200253 io_u_mark_submit(td, ret);
Jens Axboe241eb082014-09-30 13:33:23 -0600254
255 ld->queued -= ret;
256 ring_inc(ld, &ld->tail, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100257 ret = 0;
Jens Axboe241eb082014-09-30 13:33:23 -0600258 } else if (ret == -EINTR || !ret) {
Jens Axboe838bc702008-05-22 13:08:23 +0200259 if (!ret)
260 io_u_mark_submit(td, ret);
Jens Axboe2866c822006-10-09 15:57:48 +0200261 continue;
Jens Axboe241eb082014-09-30 13:33:23 -0600262 } else if (ret == -EAGAIN) {
263 /*
264 * If we get EAGAIN, we should break out without
265 * error and let the upper layer reap some
Jens Axboe841ffe52014-09-30 20:28:45 -0600266 * events for us. If we have no queued IO, we
267 * must loop here. If we loop for more than 30s,
268 * just error out, something must be buggy in the
269 * IO path.
Jens Axboe241eb082014-09-30 13:33:23 -0600270 */
Jens Axboe841ffe52014-09-30 20:28:45 -0600271 if (ld->queued) {
272 ret = 0;
273 break;
274 }
275 if (!wait_start) {
276 fio_gettime(&tv, NULL);
277 wait_start = 0;
278 } else if (mtime_since_now(&tv) > 30000) {
279 log_err("fio: aio appears to be stalled, giving up\n");
280 break;
281 }
282 usleep(1);
283 continue;
Jens Axboe838bc702008-05-22 13:08:23 +0200284 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200285 break;
Jens Axboe44bbe102014-10-01 08:42:27 -0600286 } while (ld->queued);
Jens Axboe2866c822006-10-09 15:57:48 +0200287
Jens Axboe36167d82007-02-18 05:41:31 +0100288 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200289}
290
291static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
292{
293 struct libaio_data *ld = td->io_ops->data;
294
295 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
296}
297
298static void fio_libaio_cleanup(struct thread_data *td)
299{
300 struct libaio_data *ld = td->io_ops->data;
301
302 if (ld) {
303 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100304 free(ld->aio_events);
305 free(ld->iocbs);
306 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200307 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200308 }
309}
310
311static int fio_libaio_init(struct thread_data *td)
312{
Jens Axboec90e1012012-09-04 11:47:00 -0600313 struct libaio_options *o = td->eo;
Jens Axboe241eb082014-09-30 13:33:23 -0600314 struct libaio_data *ld;
Jens Axboec90e1012012-09-04 11:47:00 -0600315 int err = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200316
Jens Axboe241eb082014-09-30 13:33:23 -0600317 ld = calloc(1, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100318
Jens Axboec90e1012012-09-04 11:47:00 -0600319 /*
320 * First try passing in 0 for queue depth, since we don't
321 * care about the user ring. If that fails, the kernel is too old
322 * and we need the right depth.
323 */
324 if (!o->userspace_reap)
Jens Axboec224ec02012-09-06 09:07:44 -0600325 err = io_queue_init(INT_MAX, &ld->aio_ctx);
Jens Axboec90e1012012-09-04 11:47:00 -0600326 if (o->userspace_reap || err == -EINVAL)
327 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
Jens Axboec1db2dc2007-03-20 10:42:07 +0100328 if (err) {
329 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200330 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100331 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200332 return 1;
333 }
334
Jens Axboe241eb082014-09-30 13:33:23 -0600335 ld->entries = td->o.iodepth;
336 ld->is_pow2 = is_power_of_2(ld->entries);
337 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
338 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
339 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100340
Jens Axboe2866c822006-10-09 15:57:48 +0200341 td->io_ops->data = ld;
342 return 0;
343}
344
Jens Axboe5f350952006-11-07 15:20:59 +0100345static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100346 .name = "libaio",
347 .version = FIO_IOOPS_VERSION,
348 .init = fio_libaio_init,
349 .prep = fio_libaio_prep,
350 .queue = fio_libaio_queue,
351 .commit = fio_libaio_commit,
352 .cancel = fio_libaio_cancel,
353 .getevents = fio_libaio_getevents,
354 .event = fio_libaio_event,
355 .cleanup = fio_libaio_cleanup,
356 .open_file = generic_open_file,
357 .close_file = generic_close_file,
358 .get_file_size = generic_get_file_size,
359 .options = options,
360 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200361};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100362
Jens Axboe5f350952006-11-07 15:20:59 +0100363static void fio_init fio_libaio_register(void)
364{
365 register_ioengine(&ioengine);
366}
367
368static void fio_exit fio_libaio_unregister(void)
369{
370 unregister_ioengine(&ioengine);
371}