blob: d4f48303becb8af99a63bcc0e1667a030bdd0a66 [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 Axboe17876dc2014-10-07 20:03:14 -060016static int fio_libaio_commit(struct thread_data *td);
17
Jens Axboe2866c822006-10-09 15:57:48 +020018struct libaio_data {
19 io_context_t aio_ctx;
20 struct io_event *aio_events;
Jens Axboe755200a2007-02-19 13:08:12 +010021 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +010022 struct io_u **io_us;
Jens Axboe241eb082014-09-30 13:33:23 -060023
24 /*
25 * Basic ring buffer. 'head' is incremented in _queue(), and
26 * 'tail' is incremented in _commit(). We keep 'queued' so
27 * that we know if the ring is full or empty, when
28 * 'head' == 'tail'. 'entries' is the ring size, and
29 * 'is_pow2' is just an optimization to use AND instead of
30 * modulus to get the remainder on ring increment.
31 */
32 int is_pow2;
33 unsigned int entries;
34 unsigned int queued;
35 unsigned int head;
36 unsigned int tail;
Jens Axboe2866c822006-10-09 15:57:48 +020037};
38
Steven Langde890a12011-11-09 14:03:34 +010039struct libaio_options {
Jens Axboe6a605302014-10-29 08:30:07 -060040 void *pad;
Steven Langde890a12011-11-09 14:03:34 +010041 unsigned int userspace_reap;
42};
43
44static struct fio_option options[] = {
45 {
46 .name = "userspace_reap",
Jens Axboee8b0e952012-03-19 14:37:08 +010047 .lname = "Libaio userspace reaping",
Steven Langde890a12011-11-09 14:03:34 +010048 .type = FIO_OPT_STR_SET,
49 .off1 = offsetof(struct libaio_options, userspace_reap),
50 .help = "Use alternative user-space reap implementation",
Jens Axboee90a0ad2013-04-10 19:43:59 +020051 .category = FIO_OPT_C_ENGINE,
Jens Axboe5a3cd5f2013-04-11 13:48:13 +020052 .group = FIO_OPT_G_LIBAIO,
Steven Langde890a12011-11-09 14:03:34 +010053 },
54 {
55 .name = NULL,
56 },
57};
58
Jens Axboe241eb082014-09-30 13:33:23 -060059static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
60 unsigned int add)
61{
62 if (ld->is_pow2)
63 *val = (*val + add) & (ld->entries - 1);
64 else
65 *val = (*val + add) % ld->entries;
66}
67
Jens Axboe7a16dd02006-10-18 17:21:58 +020068static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020069{
Jens Axboe53cdc682006-10-18 11:50:58 +020070 struct fio_file *f = io_u->file;
71
Jens Axboe2866c822006-10-09 15:57:48 +020072 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010073 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 +020074 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010075 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 +020076 else if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020077 io_prep_fsync(&io_u->iocb, f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020078
79 return 0;
80}
81
82static struct io_u *fio_libaio_event(struct thread_data *td, int event)
83{
84 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010085 struct io_event *ev;
86 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020087
Jens Axboef4234792007-03-02 15:16:03 +010088 ev = ld->aio_events + event;
Jens Axboe0cc6ee52013-01-02 12:55:41 +010089 io_u = container_of(ev->obj, struct io_u, iocb);
Jens Axboef4234792007-03-02 15:16:03 +010090
91 if (ev->res != io_u->xfer_buflen) {
92 if (ev->res > io_u->xfer_buflen)
93 io_u->error = -ev->res;
94 else
95 io_u->resid = io_u->xfer_buflen - ev->res;
96 } else
97 io_u->error = 0;
98
99 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +0200100}
101
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600102struct aio_ring {
103 unsigned id; /** kernel internal index number */
104 unsigned nr; /** number of io_events */
105 unsigned head;
106 unsigned tail;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600107
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600108 unsigned magic;
109 unsigned compat_features;
110 unsigned incompat_features;
111 unsigned header_length; /** size of aio_ring */
112
113 struct io_event events[0];
114};
115
116#define AIO_RING_MAGIC 0xa10a10a1
117
118static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
Jens Axboec44b1ff2011-08-30 20:43:53 -0600119 struct io_event *events)
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600120{
121 long i = 0;
122 unsigned head;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600123 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600124
125 while (i < max) {
126 head = ring->head;
127
128 if (head == ring->tail) {
129 /* There are no more completions */
130 break;
131 } else {
132 /* There is another completion to reap */
133 events[i] = ring->events[head];
134 read_barrier();
Jens Axboec44b1ff2011-08-30 20:43:53 -0600135 ring->head = (head + 1) % ring->nr;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600136 i++;
137 }
138 }
139
140 return i;
141}
142
Jens Axboee7d2e612007-12-11 10:49:39 +0100143static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
Jens Axboe0cbbc392014-09-30 16:04:12 -0600144 unsigned int max, const struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +0200145{
146 struct libaio_data *ld = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100147 struct libaio_options *o = td->eo;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200148 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
Jens Axboe0cbbc392014-09-30 16:04:12 -0600149 struct timespec __lt, *lt = NULL;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200150 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200151
Jens Axboe0cbbc392014-09-30 16:04:12 -0600152 if (t) {
153 __lt = *t;
154 lt = &__lt;
155 }
156
Jens Axboe2866c822006-10-09 15:57:48 +0200157 do {
Steven Langde890a12011-11-09 14:03:34 +0100158 if (o->userspace_reap == 1
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600159 && actual_min == 0
160 && ((struct aio_ring *)(ld->aio_ctx))->magic
161 == AIO_RING_MAGIC) {
162 r = user_io_getevents(ld->aio_ctx, max,
163 ld->aio_events + events);
164 } else {
165 r = io_getevents(ld->aio_ctx, actual_min,
Jens Axboe0cbbc392014-09-30 16:04:12 -0600166 max, ld->aio_events + events, lt);
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600167 }
Andrey Kuzmin26166ef2014-10-09 20:14:27 -0600168 if (r > 0)
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200169 events += r;
Andrey Kuzmin26166ef2014-10-09 20:14:27 -0600170 else if ((min && r == 0) || r == -EAGAIN) {
Jens Axboe17876dc2014-10-07 20:03:14 -0600171 fio_libaio_commit(td);
Jens Axboe2866c822006-10-09 15:57:48 +0200172 usleep(100);
Jens Axboe17876dc2014-10-07 20:03:14 -0600173 } else if (r != -EINTR)
Jens Axboe0c195ae2014-10-03 13:10:23 -0600174 break;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200175 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200176
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200177 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200178}
179
180static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
181{
182 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200183
Jens Axboe7101d9c2007-09-12 13:12:39 +0200184 fio_ro_check(td, io_u);
185
Jens Axboe241eb082014-09-30 13:33:23 -0600186 if (ld->queued == td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100187 return FIO_Q_BUSY;
188
189 /*
190 * fsync is tricky, since it can fail and we need to do it
191 * serialized with other io. the reason is that linux doesn't
192 * support aio fsync yet. So return busy for the case where we
193 * have pending io, to let fio complete those first.
194 */
Jens Axboef0115312010-03-09 21:47:15 +0100195 if (ddir_sync(io_u->ddir)) {
Jens Axboe241eb082014-09-30 13:33:23 -0600196 if (ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100197 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100198
Jens Axboef0115312010-03-09 21:47:15 +0100199 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200200 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100201 }
202
Jens Axboea5f30272010-07-19 16:19:55 -0600203 if (io_u->ddir == DDIR_TRIM) {
Jens Axboe241eb082014-09-30 13:33:23 -0600204 if (ld->queued)
Jens Axboea5f30272010-07-19 16:19:55 -0600205 return FIO_Q_BUSY;
206
207 do_io_u_trim(td, io_u);
208 return FIO_Q_COMPLETED;
209 }
210
Jens Axboe241eb082014-09-30 13:33:23 -0600211 ld->iocbs[ld->head] = &io_u->iocb;
212 ld->io_us[ld->head] = io_u;
213 ring_inc(ld, &ld->head, 1);
214 ld->queued++;
Jens Axboe755200a2007-02-19 13:08:12 +0100215 return FIO_Q_QUEUED;
216}
217
Jens Axboe7e77dd02007-02-20 10:57:34 +0100218static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
219 unsigned int nr)
220{
221 struct timeval now;
222 unsigned int i;
223
Jens Axboe12d9d842008-10-16 21:26:10 +0200224 if (!fio_fill_issue_time(td))
225 return;
226
Jens Axboe7e77dd02007-02-20 10:57:34 +0100227 fio_gettime(&now, NULL);
228
229 for (i = 0; i < nr; i++) {
230 struct io_u *io_u = io_us[i];
231
232 memcpy(&io_u->issue_time, &now, sizeof(now));
233 io_u_queued(td, io_u);
234 }
235}
236
Jens Axboe755200a2007-02-19 13:08:12 +0100237static int fio_libaio_commit(struct thread_data *td)
238{
239 struct libaio_data *ld = td->io_ops->data;
240 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100241 struct io_u **io_us;
Jens Axboe841ffe52014-09-30 20:28:45 -0600242 struct timeval tv;
243 int ret, wait_start = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100244
Jens Axboe241eb082014-09-30 13:33:23 -0600245 if (!ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100246 return 0;
247
Jens Axboe2866c822006-10-09 15:57:48 +0200248 do {
Jens Axboe241eb082014-09-30 13:33:23 -0600249 long nr = ld->queued;
250
251 nr = min((unsigned int) nr, ld->entries - ld->tail);
252 io_us = ld->io_us + ld->tail;
253 iocbs = ld->iocbs + ld->tail;
254
255 ret = io_submit(ld->aio_ctx, nr, iocbs);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100256 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100257 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200258 io_u_mark_submit(td, ret);
Jens Axboe241eb082014-09-30 13:33:23 -0600259
260 ld->queued -= ret;
261 ring_inc(ld, &ld->tail, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100262 ret = 0;
Jens Axboe8bc76922014-10-01 08:48:54 -0600263 wait_start = 0;
Jens Axboe241eb082014-09-30 13:33:23 -0600264 } else if (ret == -EINTR || !ret) {
Jens Axboe838bc702008-05-22 13:08:23 +0200265 if (!ret)
266 io_u_mark_submit(td, ret);
Jens Axboe8bc76922014-10-01 08:48:54 -0600267 wait_start = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200268 continue;
Jens Axboe241eb082014-09-30 13:33:23 -0600269 } else if (ret == -EAGAIN) {
270 /*
271 * If we get EAGAIN, we should break out without
272 * error and let the upper layer reap some
Jens Axboe841ffe52014-09-30 20:28:45 -0600273 * events for us. If we have no queued IO, we
274 * must loop here. If we loop for more than 30s,
275 * just error out, something must be buggy in the
276 * IO path.
Jens Axboe241eb082014-09-30 13:33:23 -0600277 */
Jens Axboe841ffe52014-09-30 20:28:45 -0600278 if (ld->queued) {
279 ret = 0;
280 break;
281 }
282 if (!wait_start) {
283 fio_gettime(&tv, NULL);
Jens Axboe9b093d82014-10-14 12:17:00 -0600284 wait_start = 1;
Jens Axboe841ffe52014-09-30 20:28:45 -0600285 } else if (mtime_since_now(&tv) > 30000) {
286 log_err("fio: aio appears to be stalled, giving up\n");
287 break;
288 }
289 usleep(1);
290 continue;
Jens Axboe0c195ae2014-10-03 13:10:23 -0600291 } else if (ret == -ENOMEM) {
292 /*
293 * If we get -ENOMEM, reap events if we can. If
294 * we cannot, treat it as a fatal event since there's
295 * nothing we can do about it.
296 */
297 if (ld->queued)
298 ret = 0;
299 break;
Jens Axboe838bc702008-05-22 13:08:23 +0200300 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200301 break;
Jens Axboe44bbe102014-10-01 08:42:27 -0600302 } while (ld->queued);
Jens Axboe2866c822006-10-09 15:57:48 +0200303
Jens Axboe36167d82007-02-18 05:41:31 +0100304 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200305}
306
307static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
308{
309 struct libaio_data *ld = td->io_ops->data;
310
311 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
312}
313
314static void fio_libaio_cleanup(struct thread_data *td)
315{
316 struct libaio_data *ld = td->io_ops->data;
317
318 if (ld) {
319 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100320 free(ld->aio_events);
321 free(ld->iocbs);
322 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200323 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200324 }
325}
326
327static int fio_libaio_init(struct thread_data *td)
328{
Jens Axboec90e1012012-09-04 11:47:00 -0600329 struct libaio_options *o = td->eo;
Jens Axboe241eb082014-09-30 13:33:23 -0600330 struct libaio_data *ld;
Jens Axboec90e1012012-09-04 11:47:00 -0600331 int err = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200332
Jens Axboe241eb082014-09-30 13:33:23 -0600333 ld = calloc(1, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100334
Jens Axboec90e1012012-09-04 11:47:00 -0600335 /*
336 * First try passing in 0 for queue depth, since we don't
337 * care about the user ring. If that fails, the kernel is too old
338 * and we need the right depth.
339 */
340 if (!o->userspace_reap)
Jens Axboec224ec02012-09-06 09:07:44 -0600341 err = io_queue_init(INT_MAX, &ld->aio_ctx);
Jens Axboec90e1012012-09-04 11:47:00 -0600342 if (o->userspace_reap || err == -EINVAL)
343 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
Jens Axboec1db2dc2007-03-20 10:42:07 +0100344 if (err) {
345 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200346 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100347 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200348 return 1;
349 }
350
Jens Axboe241eb082014-09-30 13:33:23 -0600351 ld->entries = td->o.iodepth;
352 ld->is_pow2 = is_power_of_2(ld->entries);
353 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
354 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
355 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100356
Jens Axboe2866c822006-10-09 15:57:48 +0200357 td->io_ops->data = ld;
358 return 0;
359}
360
Jens Axboe5f350952006-11-07 15:20:59 +0100361static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100362 .name = "libaio",
363 .version = FIO_IOOPS_VERSION,
364 .init = fio_libaio_init,
365 .prep = fio_libaio_prep,
366 .queue = fio_libaio_queue,
367 .commit = fio_libaio_commit,
368 .cancel = fio_libaio_cancel,
369 .getevents = fio_libaio_getevents,
370 .event = fio_libaio_event,
371 .cleanup = fio_libaio_cleanup,
372 .open_file = generic_open_file,
373 .close_file = generic_close_file,
374 .get_file_size = generic_get_file_size,
375 .options = options,
376 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200377};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100378
Jens Axboe5f350952006-11-07 15:20:59 +0100379static void fio_init fio_libaio_register(void)
380{
381 register_ioengine(&ioengine);
382}
383
384static void fio_exit fio_libaio_unregister(void)
385{
386 unregister_ioengine(&ioengine);
387}