blob: f17260d32911523b563d3c8493abcb3f67960eba [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 Axboe5f350952006-11-07 15:20:59 +010012
13#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020014
Jens Axboe34cfcda2006-11-03 14:00:45 +010015#ifdef FIO_HAVE_LIBAIO
16
Jens Axboe2866c822006-10-09 15:57:48 +020017struct libaio_data {
18 io_context_t aio_ctx;
19 struct io_event *aio_events;
Jens Axboe755200a2007-02-19 13:08:12 +010020 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +010021 struct io_u **io_us;
Jens Axboe755200a2007-02-19 13:08:12 +010022 int iocbs_nr;
Jens Axboe2866c822006-10-09 15:57:48 +020023};
24
Steven Langde890a12011-11-09 14:03:34 +010025struct libaio_options {
26 struct thread_data *td;
27 unsigned int userspace_reap;
28};
29
30static struct fio_option options[] = {
31 {
32 .name = "userspace_reap",
33 .type = FIO_OPT_STR_SET,
34 .off1 = offsetof(struct libaio_options, userspace_reap),
35 .help = "Use alternative user-space reap implementation",
36 },
37 {
38 .name = NULL,
39 },
40};
41
Jens Axboe7a16dd02006-10-18 17:21:58 +020042static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020043{
Jens Axboe53cdc682006-10-18 11:50:58 +020044 struct fio_file *f = io_u->file;
45
Jens Axboe2866c822006-10-09 15:57:48 +020046 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010047 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 +020048 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010049 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 +020050 else if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020051 io_prep_fsync(&io_u->iocb, f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020052
53 return 0;
54}
55
56static struct io_u *fio_libaio_event(struct thread_data *td, int event)
57{
58 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010059 struct io_event *ev;
60 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020061
Jens Axboef4234792007-03-02 15:16:03 +010062 ev = ld->aio_events + event;
Jens Axboe0cc6ee52013-01-02 12:55:41 +010063 io_u = container_of(ev->obj, struct io_u, iocb);
Jens Axboef4234792007-03-02 15:16:03 +010064
65 if (ev->res != io_u->xfer_buflen) {
66 if (ev->res > io_u->xfer_buflen)
67 io_u->error = -ev->res;
68 else
69 io_u->resid = io_u->xfer_buflen - ev->res;
70 } else
71 io_u->error = 0;
72
73 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020074}
75
Dan Ehrenberg675012f2011-08-30 20:36:15 -060076struct aio_ring {
77 unsigned id; /** kernel internal index number */
78 unsigned nr; /** number of io_events */
79 unsigned head;
80 unsigned tail;
Jens Axboec44b1ff2011-08-30 20:43:53 -060081
Dan Ehrenberg675012f2011-08-30 20:36:15 -060082 unsigned magic;
83 unsigned compat_features;
84 unsigned incompat_features;
85 unsigned header_length; /** size of aio_ring */
86
87 struct io_event events[0];
88};
89
90#define AIO_RING_MAGIC 0xa10a10a1
91
92static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
Jens Axboec44b1ff2011-08-30 20:43:53 -060093 struct io_event *events)
Dan Ehrenberg675012f2011-08-30 20:36:15 -060094{
95 long i = 0;
96 unsigned head;
Jens Axboec44b1ff2011-08-30 20:43:53 -060097 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
Dan Ehrenberg675012f2011-08-30 20:36:15 -060098
99 while (i < max) {
100 head = ring->head;
101
102 if (head == ring->tail) {
103 /* There are no more completions */
104 break;
105 } else {
106 /* There is another completion to reap */
107 events[i] = ring->events[head];
108 read_barrier();
Jens Axboec44b1ff2011-08-30 20:43:53 -0600109 ring->head = (head + 1) % ring->nr;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600110 i++;
111 }
112 }
113
114 return i;
115}
116
Jens Axboee7d2e612007-12-11 10:49:39 +0100117static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
118 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +0200119{
120 struct libaio_data *ld = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100121 struct libaio_options *o = td->eo;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200122 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
123 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200124
125 do {
Steven Langde890a12011-11-09 14:03:34 +0100126 if (o->userspace_reap == 1
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600127 && actual_min == 0
128 && ((struct aio_ring *)(ld->aio_ctx))->magic
129 == AIO_RING_MAGIC) {
130 r = user_io_getevents(ld->aio_ctx, max,
131 ld->aio_events + events);
132 } else {
133 r = io_getevents(ld->aio_ctx, actual_min,
134 max, ld->aio_events + events, t);
135 }
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200136 if (r >= 0)
137 events += r;
138 else if (r == -EAGAIN)
Jens Axboe2866c822006-10-09 15:57:48 +0200139 usleep(100);
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200140 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200141
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200142 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200143}
144
145static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
146{
147 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200148
Jens Axboe7101d9c2007-09-12 13:12:39 +0200149 fio_ro_check(td, io_u);
150
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100151 if (ld->iocbs_nr == (int) td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100152 return FIO_Q_BUSY;
153
154 /*
155 * fsync is tricky, since it can fail and we need to do it
156 * serialized with other io. the reason is that linux doesn't
157 * support aio fsync yet. So return busy for the case where we
158 * have pending io, to let fio complete those first.
159 */
Jens Axboef0115312010-03-09 21:47:15 +0100160 if (ddir_sync(io_u->ddir)) {
Jens Axboe755200a2007-02-19 13:08:12 +0100161 if (ld->iocbs_nr)
162 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100163
Jens Axboef0115312010-03-09 21:47:15 +0100164 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200165 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100166 }
167
Jens Axboea5f30272010-07-19 16:19:55 -0600168 if (io_u->ddir == DDIR_TRIM) {
169 if (ld->iocbs_nr)
170 return FIO_Q_BUSY;
171
172 do_io_u_trim(td, io_u);
173 return FIO_Q_COMPLETED;
174 }
175
Jens Axboe755200a2007-02-19 13:08:12 +0100176 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100177 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +0100178 ld->iocbs_nr++;
179 return FIO_Q_QUEUED;
180}
181
Jens Axboe7e77dd02007-02-20 10:57:34 +0100182static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
183 unsigned int nr)
184{
185 struct timeval now;
186 unsigned int i;
187
Jens Axboe12d9d842008-10-16 21:26:10 +0200188 if (!fio_fill_issue_time(td))
189 return;
190
Jens Axboe7e77dd02007-02-20 10:57:34 +0100191 fio_gettime(&now, NULL);
192
193 for (i = 0; i < nr; i++) {
194 struct io_u *io_u = io_us[i];
195
196 memcpy(&io_u->issue_time, &now, sizeof(now));
197 io_u_queued(td, io_u);
198 }
199}
200
Jens Axboe755200a2007-02-19 13:08:12 +0100201static int fio_libaio_commit(struct thread_data *td)
202{
203 struct libaio_data *ld = td->io_ops->data;
204 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100205 struct io_u **io_us;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100206 int ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100207
208 if (!ld->iocbs_nr)
209 return 0;
210
Jens Axboe7e77dd02007-02-20 10:57:34 +0100211 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100212 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200213 do {
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100214 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
215 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100216 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200217 io_u_mark_submit(td, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100218 ld->iocbs_nr -= ret;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100219 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100220 iocbs += ret;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100221 ret = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200222 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
223 if (!ret)
224 io_u_mark_submit(td, ret);
Jens Axboe2866c822006-10-09 15:57:48 +0200225 continue;
Jens Axboe838bc702008-05-22 13:08:23 +0200226 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200227 break;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100228 } while (ld->iocbs_nr);
Jens Axboe2866c822006-10-09 15:57:48 +0200229
Jens Axboe36167d82007-02-18 05:41:31 +0100230 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200231}
232
233static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
234{
235 struct libaio_data *ld = td->io_ops->data;
236
237 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
238}
239
240static void fio_libaio_cleanup(struct thread_data *td)
241{
242 struct libaio_data *ld = td->io_ops->data;
243
244 if (ld) {
245 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100246 free(ld->aio_events);
247 free(ld->iocbs);
248 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200249 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200250 }
251}
252
253static int fio_libaio_init(struct thread_data *td)
254{
255 struct libaio_data *ld = malloc(sizeof(*ld));
Jens Axboec90e1012012-09-04 11:47:00 -0600256 struct libaio_options *o = td->eo;
257 int err = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200258
259 memset(ld, 0, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100260
Jens Axboec90e1012012-09-04 11:47:00 -0600261 /*
262 * First try passing in 0 for queue depth, since we don't
263 * care about the user ring. If that fails, the kernel is too old
264 * and we need the right depth.
265 */
266 if (!o->userspace_reap)
Jens Axboec224ec02012-09-06 09:07:44 -0600267 err = io_queue_init(INT_MAX, &ld->aio_ctx);
Jens Axboec90e1012012-09-04 11:47:00 -0600268 if (o->userspace_reap || err == -EINVAL)
269 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
Jens Axboec1db2dc2007-03-20 10:42:07 +0100270 if (err) {
271 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200272 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100273 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200274 return 1;
275 }
276
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100277 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
278 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
279 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
Jens Axboe755200a2007-02-19 13:08:12 +0100280 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100281 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
282 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100283 ld->iocbs_nr = 0;
284
Jens Axboe2866c822006-10-09 15:57:48 +0200285 td->io_ops->data = ld;
286 return 0;
287}
288
Jens Axboe5f350952006-11-07 15:20:59 +0100289static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100290 .name = "libaio",
291 .version = FIO_IOOPS_VERSION,
292 .init = fio_libaio_init,
293 .prep = fio_libaio_prep,
294 .queue = fio_libaio_queue,
295 .commit = fio_libaio_commit,
296 .cancel = fio_libaio_cancel,
297 .getevents = fio_libaio_getevents,
298 .event = fio_libaio_event,
299 .cleanup = fio_libaio_cleanup,
300 .open_file = generic_open_file,
301 .close_file = generic_close_file,
302 .get_file_size = generic_get_file_size,
303 .options = options,
304 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200305};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100306
307#else /* FIO_HAVE_LIBAIO */
308
309/*
310 * When we have a proper configure system in place, we simply wont build
311 * and install this io engine. For now install a crippled version that
312 * just complains and fails to load.
313 */
314static int fio_libaio_init(struct thread_data fio_unused *td)
315{
Jens Axboea3edaf72010-09-26 10:53:40 +0900316 log_err("fio: libaio not available\n");
Jens Axboe34cfcda2006-11-03 14:00:45 +0100317 return 1;
318}
319
Jens Axboe5f350952006-11-07 15:20:59 +0100320static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100321 .name = "libaio",
322 .version = FIO_IOOPS_VERSION,
323 .init = fio_libaio_init,
324};
325
326#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100327
328static void fio_init fio_libaio_register(void)
329{
330 register_ioengine(&ioengine);
331}
332
333static void fio_exit fio_libaio_unregister(void)
334{
335 unregister_ioengine(&ioengine);
336}