blob: 0dca58371c9957582fbdb329ce7717fe3e1731c8 [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",
32 .type = FIO_OPT_STR_SET,
33 .off1 = offsetof(struct libaio_options, userspace_reap),
34 .help = "Use alternative user-space reap implementation",
Jens Axboee90a0ad2013-04-10 19:43:59 +020035 .category = FIO_OPT_C_ENGINE,
36 .category = FIO_OPT_G_LIBAIO,
Steven Langde890a12011-11-09 14:03:34 +010037 },
38 {
39 .name = NULL,
40 },
41};
42
Jens Axboe7a16dd02006-10-18 17:21:58 +020043static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020044{
Jens Axboe53cdc682006-10-18 11:50:58 +020045 struct fio_file *f = io_u->file;
46
Jens Axboe2866c822006-10-09 15:57:48 +020047 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010048 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 +020049 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010050 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 +020051 else if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020052 io_prep_fsync(&io_u->iocb, f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020053
54 return 0;
55}
56
57static struct io_u *fio_libaio_event(struct thread_data *td, int event)
58{
59 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010060 struct io_event *ev;
61 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020062
Jens Axboef4234792007-03-02 15:16:03 +010063 ev = ld->aio_events + event;
Jens Axboe0cc6ee52013-01-02 12:55:41 +010064 io_u = container_of(ev->obj, struct io_u, iocb);
Jens Axboef4234792007-03-02 15:16:03 +010065
66 if (ev->res != io_u->xfer_buflen) {
67 if (ev->res > io_u->xfer_buflen)
68 io_u->error = -ev->res;
69 else
70 io_u->resid = io_u->xfer_buflen - ev->res;
71 } else
72 io_u->error = 0;
73
74 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020075}
76
Dan Ehrenberg675012f2011-08-30 20:36:15 -060077struct aio_ring {
78 unsigned id; /** kernel internal index number */
79 unsigned nr; /** number of io_events */
80 unsigned head;
81 unsigned tail;
Jens Axboec44b1ff2011-08-30 20:43:53 -060082
Dan Ehrenberg675012f2011-08-30 20:36:15 -060083 unsigned magic;
84 unsigned compat_features;
85 unsigned incompat_features;
86 unsigned header_length; /** size of aio_ring */
87
88 struct io_event events[0];
89};
90
91#define AIO_RING_MAGIC 0xa10a10a1
92
93static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
Jens Axboec44b1ff2011-08-30 20:43:53 -060094 struct io_event *events)
Dan Ehrenberg675012f2011-08-30 20:36:15 -060095{
96 long i = 0;
97 unsigned head;
Jens Axboec44b1ff2011-08-30 20:43:53 -060098 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
Dan Ehrenberg675012f2011-08-30 20:36:15 -060099
100 while (i < max) {
101 head = ring->head;
102
103 if (head == ring->tail) {
104 /* There are no more completions */
105 break;
106 } else {
107 /* There is another completion to reap */
108 events[i] = ring->events[head];
109 read_barrier();
Jens Axboec44b1ff2011-08-30 20:43:53 -0600110 ring->head = (head + 1) % ring->nr;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600111 i++;
112 }
113 }
114
115 return i;
116}
117
Jens Axboee7d2e612007-12-11 10:49:39 +0100118static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
119 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +0200120{
121 struct libaio_data *ld = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100122 struct libaio_options *o = td->eo;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200123 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
124 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200125
126 do {
Steven Langde890a12011-11-09 14:03:34 +0100127 if (o->userspace_reap == 1
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600128 && actual_min == 0
129 && ((struct aio_ring *)(ld->aio_ctx))->magic
130 == AIO_RING_MAGIC) {
131 r = user_io_getevents(ld->aio_ctx, max,
132 ld->aio_events + events);
133 } else {
134 r = io_getevents(ld->aio_ctx, actual_min,
135 max, ld->aio_events + events, t);
136 }
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200137 if (r >= 0)
138 events += r;
139 else if (r == -EAGAIN)
Jens Axboe2866c822006-10-09 15:57:48 +0200140 usleep(100);
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200141 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200142
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200143 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200144}
145
146static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
147{
148 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200149
Jens Axboe7101d9c2007-09-12 13:12:39 +0200150 fio_ro_check(td, io_u);
151
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100152 if (ld->iocbs_nr == (int) td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100153 return FIO_Q_BUSY;
154
155 /*
156 * fsync is tricky, since it can fail and we need to do it
157 * serialized with other io. the reason is that linux doesn't
158 * support aio fsync yet. So return busy for the case where we
159 * have pending io, to let fio complete those first.
160 */
Jens Axboef0115312010-03-09 21:47:15 +0100161 if (ddir_sync(io_u->ddir)) {
Jens Axboe755200a2007-02-19 13:08:12 +0100162 if (ld->iocbs_nr)
163 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100164
Jens Axboef0115312010-03-09 21:47:15 +0100165 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200166 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100167 }
168
Jens Axboea5f30272010-07-19 16:19:55 -0600169 if (io_u->ddir == DDIR_TRIM) {
170 if (ld->iocbs_nr)
171 return FIO_Q_BUSY;
172
173 do_io_u_trim(td, io_u);
174 return FIO_Q_COMPLETED;
175 }
176
Jens Axboe755200a2007-02-19 13:08:12 +0100177 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100178 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +0100179 ld->iocbs_nr++;
180 return FIO_Q_QUEUED;
181}
182
Jens Axboe7e77dd02007-02-20 10:57:34 +0100183static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
184 unsigned int nr)
185{
186 struct timeval now;
187 unsigned int i;
188
Jens Axboe12d9d842008-10-16 21:26:10 +0200189 if (!fio_fill_issue_time(td))
190 return;
191
Jens Axboe7e77dd02007-02-20 10:57:34 +0100192 fio_gettime(&now, NULL);
193
194 for (i = 0; i < nr; i++) {
195 struct io_u *io_u = io_us[i];
196
197 memcpy(&io_u->issue_time, &now, sizeof(now));
198 io_u_queued(td, io_u);
199 }
200}
201
Jens Axboe755200a2007-02-19 13:08:12 +0100202static int fio_libaio_commit(struct thread_data *td)
203{
204 struct libaio_data *ld = td->io_ops->data;
205 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100206 struct io_u **io_us;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100207 int ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100208
209 if (!ld->iocbs_nr)
210 return 0;
211
Jens Axboe7e77dd02007-02-20 10:57:34 +0100212 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100213 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200214 do {
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100215 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
216 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100217 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200218 io_u_mark_submit(td, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100219 ld->iocbs_nr -= ret;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100220 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100221 iocbs += ret;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100222 ret = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200223 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
224 if (!ret)
225 io_u_mark_submit(td, ret);
Jens Axboe2866c822006-10-09 15:57:48 +0200226 continue;
Jens Axboe838bc702008-05-22 13:08:23 +0200227 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200228 break;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100229 } while (ld->iocbs_nr);
Jens Axboe2866c822006-10-09 15:57:48 +0200230
Jens Axboe36167d82007-02-18 05:41:31 +0100231 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200232}
233
234static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
235{
236 struct libaio_data *ld = td->io_ops->data;
237
238 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
239}
240
241static void fio_libaio_cleanup(struct thread_data *td)
242{
243 struct libaio_data *ld = td->io_ops->data;
244
245 if (ld) {
246 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100247 free(ld->aio_events);
248 free(ld->iocbs);
249 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200250 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200251 }
252}
253
254static int fio_libaio_init(struct thread_data *td)
255{
256 struct libaio_data *ld = malloc(sizeof(*ld));
Jens Axboec90e1012012-09-04 11:47:00 -0600257 struct libaio_options *o = td->eo;
258 int err = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200259
260 memset(ld, 0, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100261
Jens Axboec90e1012012-09-04 11:47:00 -0600262 /*
263 * First try passing in 0 for queue depth, since we don't
264 * care about the user ring. If that fails, the kernel is too old
265 * and we need the right depth.
266 */
267 if (!o->userspace_reap)
Jens Axboec224ec02012-09-06 09:07:44 -0600268 err = io_queue_init(INT_MAX, &ld->aio_ctx);
Jens Axboec90e1012012-09-04 11:47:00 -0600269 if (o->userspace_reap || err == -EINVAL)
270 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
Jens Axboec1db2dc2007-03-20 10:42:07 +0100271 if (err) {
272 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200273 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100274 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200275 return 1;
276 }
277
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100278 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
279 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
280 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
Jens Axboe755200a2007-02-19 13:08:12 +0100281 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100282 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
283 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100284 ld->iocbs_nr = 0;
285
Jens Axboe2866c822006-10-09 15:57:48 +0200286 td->io_ops->data = ld;
287 return 0;
288}
289
Jens Axboe5f350952006-11-07 15:20:59 +0100290static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100291 .name = "libaio",
292 .version = FIO_IOOPS_VERSION,
293 .init = fio_libaio_init,
294 .prep = fio_libaio_prep,
295 .queue = fio_libaio_queue,
296 .commit = fio_libaio_commit,
297 .cancel = fio_libaio_cancel,
298 .getevents = fio_libaio_getevents,
299 .event = fio_libaio_event,
300 .cleanup = fio_libaio_cleanup,
301 .open_file = generic_open_file,
302 .close_file = generic_close_file,
303 .get_file_size = generic_get_file_size,
304 .options = options,
305 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200306};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100307
Jens Axboe5f350952006-11-07 15:20:59 +0100308static void fio_init fio_libaio_register(void)
309{
310 register_ioengine(&ioengine);
311}
312
313static void fio_exit fio_libaio_unregister(void)
314{
315 unregister_ioengine(&ioengine);
316}