blob: 6c5d73ed27dd1751ac98c1177fcdbf2bdf2fe487 [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);
Jens Axboe0c195ae2014-10-03 13:10:23 -0600170 else
171 break;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200172 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200173
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200174 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200175}
176
177static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
178{
179 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200180
Jens Axboe7101d9c2007-09-12 13:12:39 +0200181 fio_ro_check(td, io_u);
182
Jens Axboe241eb082014-09-30 13:33:23 -0600183 if (ld->queued == td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100184 return FIO_Q_BUSY;
185
186 /*
187 * fsync is tricky, since it can fail and we need to do it
188 * serialized with other io. the reason is that linux doesn't
189 * support aio fsync yet. So return busy for the case where we
190 * have pending io, to let fio complete those first.
191 */
Jens Axboef0115312010-03-09 21:47:15 +0100192 if (ddir_sync(io_u->ddir)) {
Jens Axboe241eb082014-09-30 13:33:23 -0600193 if (ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100194 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100195
Jens Axboef0115312010-03-09 21:47:15 +0100196 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200197 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100198 }
199
Jens Axboea5f30272010-07-19 16:19:55 -0600200 if (io_u->ddir == DDIR_TRIM) {
Jens Axboe241eb082014-09-30 13:33:23 -0600201 if (ld->queued)
Jens Axboea5f30272010-07-19 16:19:55 -0600202 return FIO_Q_BUSY;
203
204 do_io_u_trim(td, io_u);
205 return FIO_Q_COMPLETED;
206 }
207
Jens Axboe241eb082014-09-30 13:33:23 -0600208 ld->iocbs[ld->head] = &io_u->iocb;
209 ld->io_us[ld->head] = io_u;
210 ring_inc(ld, &ld->head, 1);
211 ld->queued++;
Jens Axboe755200a2007-02-19 13:08:12 +0100212 return FIO_Q_QUEUED;
213}
214
Jens Axboe7e77dd02007-02-20 10:57:34 +0100215static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
216 unsigned int nr)
217{
218 struct timeval now;
219 unsigned int i;
220
Jens Axboe12d9d842008-10-16 21:26:10 +0200221 if (!fio_fill_issue_time(td))
222 return;
223
Jens Axboe7e77dd02007-02-20 10:57:34 +0100224 fio_gettime(&now, NULL);
225
226 for (i = 0; i < nr; i++) {
227 struct io_u *io_u = io_us[i];
228
229 memcpy(&io_u->issue_time, &now, sizeof(now));
230 io_u_queued(td, io_u);
231 }
232}
233
Jens Axboe755200a2007-02-19 13:08:12 +0100234static int fio_libaio_commit(struct thread_data *td)
235{
236 struct libaio_data *ld = td->io_ops->data;
237 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100238 struct io_u **io_us;
Jens Axboe841ffe52014-09-30 20:28:45 -0600239 struct timeval tv;
240 int ret, wait_start = 0;
Jens Axboe755200a2007-02-19 13:08:12 +0100241
Jens Axboe241eb082014-09-30 13:33:23 -0600242 if (!ld->queued)
Jens Axboe755200a2007-02-19 13:08:12 +0100243 return 0;
244
Jens Axboe2866c822006-10-09 15:57:48 +0200245 do {
Jens Axboe241eb082014-09-30 13:33:23 -0600246 long nr = ld->queued;
247
248 nr = min((unsigned int) nr, ld->entries - ld->tail);
249 io_us = ld->io_us + ld->tail;
250 iocbs = ld->iocbs + ld->tail;
251
252 ret = io_submit(ld->aio_ctx, nr, iocbs);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100253 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100254 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200255 io_u_mark_submit(td, ret);
Jens Axboe241eb082014-09-30 13:33:23 -0600256
257 ld->queued -= ret;
258 ring_inc(ld, &ld->tail, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100259 ret = 0;
Jens Axboe8bc76922014-10-01 08:48:54 -0600260 wait_start = 0;
Jens Axboe241eb082014-09-30 13:33:23 -0600261 } else if (ret == -EINTR || !ret) {
Jens Axboe838bc702008-05-22 13:08:23 +0200262 if (!ret)
263 io_u_mark_submit(td, ret);
Jens Axboe8bc76922014-10-01 08:48:54 -0600264 wait_start = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200265 continue;
Jens Axboe241eb082014-09-30 13:33:23 -0600266 } else if (ret == -EAGAIN) {
267 /*
268 * If we get EAGAIN, we should break out without
269 * error and let the upper layer reap some
Jens Axboe841ffe52014-09-30 20:28:45 -0600270 * events for us. If we have no queued IO, we
271 * must loop here. If we loop for more than 30s,
272 * just error out, something must be buggy in the
273 * IO path.
Jens Axboe241eb082014-09-30 13:33:23 -0600274 */
Jens Axboe841ffe52014-09-30 20:28:45 -0600275 if (ld->queued) {
276 ret = 0;
277 break;
278 }
279 if (!wait_start) {
280 fio_gettime(&tv, NULL);
281 wait_start = 0;
282 } else if (mtime_since_now(&tv) > 30000) {
283 log_err("fio: aio appears to be stalled, giving up\n");
284 break;
285 }
286 usleep(1);
287 continue;
Jens Axboe0c195ae2014-10-03 13:10:23 -0600288 } else if (ret == -ENOMEM) {
289 /*
290 * If we get -ENOMEM, reap events if we can. If
291 * we cannot, treat it as a fatal event since there's
292 * nothing we can do about it.
293 */
294 if (ld->queued)
295 ret = 0;
296 break;
Jens Axboe838bc702008-05-22 13:08:23 +0200297 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200298 break;
Jens Axboe44bbe102014-10-01 08:42:27 -0600299 } while (ld->queued);
Jens Axboe2866c822006-10-09 15:57:48 +0200300
Jens Axboe36167d82007-02-18 05:41:31 +0100301 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200302}
303
304static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
305{
306 struct libaio_data *ld = td->io_ops->data;
307
308 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
309}
310
311static void fio_libaio_cleanup(struct thread_data *td)
312{
313 struct libaio_data *ld = td->io_ops->data;
314
315 if (ld) {
316 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100317 free(ld->aio_events);
318 free(ld->iocbs);
319 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200320 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200321 }
322}
323
324static int fio_libaio_init(struct thread_data *td)
325{
Jens Axboec90e1012012-09-04 11:47:00 -0600326 struct libaio_options *o = td->eo;
Jens Axboe241eb082014-09-30 13:33:23 -0600327 struct libaio_data *ld;
Jens Axboec90e1012012-09-04 11:47:00 -0600328 int err = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200329
Jens Axboe241eb082014-09-30 13:33:23 -0600330 ld = calloc(1, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100331
Jens Axboec90e1012012-09-04 11:47:00 -0600332 /*
333 * First try passing in 0 for queue depth, since we don't
334 * care about the user ring. If that fails, the kernel is too old
335 * and we need the right depth.
336 */
337 if (!o->userspace_reap)
Jens Axboec224ec02012-09-06 09:07:44 -0600338 err = io_queue_init(INT_MAX, &ld->aio_ctx);
Jens Axboec90e1012012-09-04 11:47:00 -0600339 if (o->userspace_reap || err == -EINVAL)
340 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
Jens Axboec1db2dc2007-03-20 10:42:07 +0100341 if (err) {
342 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200343 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100344 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200345 return 1;
346 }
347
Jens Axboe241eb082014-09-30 13:33:23 -0600348 ld->entries = td->o.iodepth;
349 ld->is_pow2 = is_power_of_2(ld->entries);
350 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
351 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
352 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100353
Jens Axboe2866c822006-10-09 15:57:48 +0200354 td->io_ops->data = ld;
355 return 0;
356}
357
Jens Axboe5f350952006-11-07 15:20:59 +0100358static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100359 .name = "libaio",
360 .version = FIO_IOOPS_VERSION,
361 .init = fio_libaio_init,
362 .prep = fio_libaio_prep,
363 .queue = fio_libaio_queue,
364 .commit = fio_libaio_commit,
365 .cancel = fio_libaio_cancel,
366 .getevents = fio_libaio_getevents,
367 .event = fio_libaio_event,
368 .cleanup = fio_libaio_cleanup,
369 .open_file = generic_open_file,
370 .close_file = generic_close_file,
371 .get_file_size = generic_get_file_size,
372 .options = options,
373 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200374};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100375
Jens Axboe5f350952006-11-07 15:20:59 +0100376static void fio_init fio_libaio_register(void)
377{
378 register_ioengine(&ioengine);
379}
380
381static void fio_exit fio_libaio_unregister(void)
382{
383 unregister_ioengine(&ioengine);
384}