blob: 9cc910d73bc19fdbb690baa5295131fc0f7addbc [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 Axboe755200a2007-02-19 13:08:12 +010021 int iocbs_nr;
Jens Axboe2866c822006-10-09 15:57:48 +020022};
23
Steven Langde890a12011-11-09 14:03:34 +010024struct libaio_options {
25 struct thread_data *td;
26 unsigned int userspace_reap;
27};
28
29static struct fio_option options[] = {
30 {
31 .name = "userspace_reap",
Jens Axboee8b0e952012-03-19 14:37:08 +010032 .lname = "Libaio userspace reaping",
Steven Langde890a12011-11-09 14:03:34 +010033 .type = FIO_OPT_STR_SET,
34 .off1 = offsetof(struct libaio_options, userspace_reap),
35 .help = "Use alternative user-space reap implementation",
Jens Axboee90a0ad2013-04-10 19:43:59 +020036 .category = FIO_OPT_C_ENGINE,
Jens Axboe5a3cd5f2013-04-11 13:48:13 +020037 .group = FIO_OPT_G_LIBAIO,
Steven Langde890a12011-11-09 14:03:34 +010038 },
39 {
40 .name = NULL,
41 },
42};
43
Jens Axboe7a16dd02006-10-18 17:21:58 +020044static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020045{
Jens Axboe53cdc682006-10-18 11:50:58 +020046 struct fio_file *f = io_u->file;
47
Jens Axboe2866c822006-10-09 15:57:48 +020048 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010049 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 +020050 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010051 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 +020052 else if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020053 io_prep_fsync(&io_u->iocb, f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020054
55 return 0;
56}
57
58static struct io_u *fio_libaio_event(struct thread_data *td, int event)
59{
60 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010061 struct io_event *ev;
62 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020063
Jens Axboef4234792007-03-02 15:16:03 +010064 ev = ld->aio_events + event;
Jens Axboe0cc6ee52013-01-02 12:55:41 +010065 io_u = container_of(ev->obj, struct io_u, iocb);
Jens Axboef4234792007-03-02 15:16:03 +010066
67 if (ev->res != io_u->xfer_buflen) {
68 if (ev->res > io_u->xfer_buflen)
69 io_u->error = -ev->res;
70 else
71 io_u->resid = io_u->xfer_buflen - ev->res;
72 } else
73 io_u->error = 0;
74
75 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020076}
77
Dan Ehrenberg675012f2011-08-30 20:36:15 -060078struct aio_ring {
79 unsigned id; /** kernel internal index number */
80 unsigned nr; /** number of io_events */
81 unsigned head;
82 unsigned tail;
Jens Axboec44b1ff2011-08-30 20:43:53 -060083
Dan Ehrenberg675012f2011-08-30 20:36:15 -060084 unsigned magic;
85 unsigned compat_features;
86 unsigned incompat_features;
87 unsigned header_length; /** size of aio_ring */
88
89 struct io_event events[0];
90};
91
92#define AIO_RING_MAGIC 0xa10a10a1
93
94static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
Jens Axboec44b1ff2011-08-30 20:43:53 -060095 struct io_event *events)
Dan Ehrenberg675012f2011-08-30 20:36:15 -060096{
97 long i = 0;
98 unsigned head;
Jens Axboec44b1ff2011-08-30 20:43:53 -060099 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600100
101 while (i < max) {
102 head = ring->head;
103
104 if (head == ring->tail) {
105 /* There are no more completions */
106 break;
107 } else {
108 /* There is another completion to reap */
109 events[i] = ring->events[head];
110 read_barrier();
Jens Axboec44b1ff2011-08-30 20:43:53 -0600111 ring->head = (head + 1) % ring->nr;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600112 i++;
113 }
114 }
115
116 return i;
117}
118
Jens Axboee7d2e612007-12-11 10:49:39 +0100119static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
120 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +0200121{
122 struct libaio_data *ld = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100123 struct libaio_options *o = td->eo;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200124 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
125 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200126
127 do {
Steven Langde890a12011-11-09 14:03:34 +0100128 if (o->userspace_reap == 1
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600129 && actual_min == 0
130 && ((struct aio_ring *)(ld->aio_ctx))->magic
131 == AIO_RING_MAGIC) {
132 r = user_io_getevents(ld->aio_ctx, max,
133 ld->aio_events + events);
134 } else {
135 r = io_getevents(ld->aio_ctx, actual_min,
136 max, ld->aio_events + events, t);
137 }
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200138 if (r >= 0)
139 events += r;
140 else if (r == -EAGAIN)
Jens Axboe2866c822006-10-09 15:57:48 +0200141 usleep(100);
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200142 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200143
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200144 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200145}
146
147static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
148{
149 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200150
Jens Axboe7101d9c2007-09-12 13:12:39 +0200151 fio_ro_check(td, io_u);
152
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100153 if (ld->iocbs_nr == (int) td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100154 return FIO_Q_BUSY;
155
156 /*
157 * fsync is tricky, since it can fail and we need to do it
158 * serialized with other io. the reason is that linux doesn't
159 * support aio fsync yet. So return busy for the case where we
160 * have pending io, to let fio complete those first.
161 */
Jens Axboef0115312010-03-09 21:47:15 +0100162 if (ddir_sync(io_u->ddir)) {
Jens Axboe755200a2007-02-19 13:08:12 +0100163 if (ld->iocbs_nr)
164 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100165
Jens Axboef0115312010-03-09 21:47:15 +0100166 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200167 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100168 }
169
Jens Axboea5f30272010-07-19 16:19:55 -0600170 if (io_u->ddir == DDIR_TRIM) {
171 if (ld->iocbs_nr)
172 return FIO_Q_BUSY;
173
174 do_io_u_trim(td, io_u);
175 return FIO_Q_COMPLETED;
176 }
177
Jens Axboe755200a2007-02-19 13:08:12 +0100178 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100179 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +0100180 ld->iocbs_nr++;
181 return FIO_Q_QUEUED;
182}
183
Jens Axboe7e77dd02007-02-20 10:57:34 +0100184static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
185 unsigned int nr)
186{
187 struct timeval now;
188 unsigned int i;
189
Jens Axboe12d9d842008-10-16 21:26:10 +0200190 if (!fio_fill_issue_time(td))
191 return;
192
Jens Axboe7e77dd02007-02-20 10:57:34 +0100193 fio_gettime(&now, NULL);
194
195 for (i = 0; i < nr; i++) {
196 struct io_u *io_u = io_us[i];
197
198 memcpy(&io_u->issue_time, &now, sizeof(now));
199 io_u_queued(td, io_u);
200 }
201}
202
Jens Axboe755200a2007-02-19 13:08:12 +0100203static int fio_libaio_commit(struct thread_data *td)
204{
205 struct libaio_data *ld = td->io_ops->data;
206 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100207 struct io_u **io_us;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100208 int ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100209
210 if (!ld->iocbs_nr)
211 return 0;
212
Jens Axboe7e77dd02007-02-20 10:57:34 +0100213 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100214 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200215 do {
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100216 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
217 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100218 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200219 io_u_mark_submit(td, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100220 ld->iocbs_nr -= ret;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100221 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100222 iocbs += ret;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100223 ret = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200224 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
225 if (!ret)
226 io_u_mark_submit(td, ret);
Jens Axboe2866c822006-10-09 15:57:48 +0200227 continue;
Jens Axboe838bc702008-05-22 13:08:23 +0200228 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200229 break;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100230 } while (ld->iocbs_nr);
Jens Axboe2866c822006-10-09 15:57:48 +0200231
Jens Axboe36167d82007-02-18 05:41:31 +0100232 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200233}
234
235static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
236{
237 struct libaio_data *ld = td->io_ops->data;
238
239 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
240}
241
242static void fio_libaio_cleanup(struct thread_data *td)
243{
244 struct libaio_data *ld = td->io_ops->data;
245
246 if (ld) {
247 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100248 free(ld->aio_events);
249 free(ld->iocbs);
250 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200251 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200252 }
253}
254
255static int fio_libaio_init(struct thread_data *td)
256{
257 struct libaio_data *ld = malloc(sizeof(*ld));
Jens Axboec90e1012012-09-04 11:47:00 -0600258 struct libaio_options *o = td->eo;
259 int err = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200260
261 memset(ld, 0, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100262
Jens Axboec90e1012012-09-04 11:47:00 -0600263 /*
264 * First try passing in 0 for queue depth, since we don't
265 * care about the user ring. If that fails, the kernel is too old
266 * and we need the right depth.
267 */
268 if (!o->userspace_reap)
Jens Axboec224ec02012-09-06 09:07:44 -0600269 err = io_queue_init(INT_MAX, &ld->aio_ctx);
Jens Axboec90e1012012-09-04 11:47:00 -0600270 if (o->userspace_reap || err == -EINVAL)
271 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
Jens Axboec1db2dc2007-03-20 10:42:07 +0100272 if (err) {
273 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200274 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100275 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200276 return 1;
277 }
278
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100279 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
280 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
281 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
Jens Axboe755200a2007-02-19 13:08:12 +0100282 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100283 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
284 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100285 ld->iocbs_nr = 0;
286
Jens Axboe2866c822006-10-09 15:57:48 +0200287 td->io_ops->data = ld;
288 return 0;
289}
290
Jens Axboe5f350952006-11-07 15:20:59 +0100291static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100292 .name = "libaio",
293 .version = FIO_IOOPS_VERSION,
294 .init = fio_libaio_init,
295 .prep = fio_libaio_prep,
296 .queue = fio_libaio_queue,
297 .commit = fio_libaio_commit,
298 .cancel = fio_libaio_cancel,
299 .getevents = fio_libaio_getevents,
300 .event = fio_libaio_event,
301 .cleanup = fio_libaio_cleanup,
302 .open_file = generic_open_file,
303 .close_file = generic_close_file,
304 .get_file_size = generic_get_file_size,
305 .options = options,
306 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200307};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100308
Jens Axboe5f350952006-11-07 15:20:59 +0100309static void fio_init fio_libaio_register(void)
310{
311 register_ioengine(&ioengine);
312}
313
314static void fio_exit fio_libaio_unregister(void)
315{
316 unregister_ioengine(&ioengine);
317}