blob: 2b8c6dac533c8bf2c9f1cdd0cc02e7ba7a599b72 [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 +020017#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
18
19struct libaio_data {
20 io_context_t aio_ctx;
21 struct io_event *aio_events;
Jens Axboe755200a2007-02-19 13:08:12 +010022 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +010023 struct io_u **io_us;
Jens Axboe755200a2007-02-19 13:08:12 +010024 int iocbs_nr;
Jens Axboe2866c822006-10-09 15:57:48 +020025};
26
Steven Langde890a12011-11-09 14:03:34 +010027struct libaio_options {
28 struct thread_data *td;
29 unsigned int userspace_reap;
30};
31
32static struct fio_option options[] = {
33 {
34 .name = "userspace_reap",
Jens Axboee8b0e952012-03-19 14:37:08 +010035 .lname = "Libaio userspace reaping",
Steven Langde890a12011-11-09 14:03:34 +010036 .type = FIO_OPT_STR_SET,
37 .off1 = offsetof(struct libaio_options, userspace_reap),
38 .help = "Use alternative user-space reap implementation",
Jens Axboee8b0e952012-03-19 14:37:08 +010039 .category = FIO_OPT_C_IO,
Steven Langde890a12011-11-09 14:03:34 +010040 },
41 {
42 .name = NULL,
43 },
44};
45
Jens Axboe7a16dd02006-10-18 17:21:58 +020046static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020047{
Jens Axboe53cdc682006-10-18 11:50:58 +020048 struct fio_file *f = io_u->file;
49
Jens Axboe2866c822006-10-09 15:57:48 +020050 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010051 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 +020052 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010053 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 +020054 else if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020055 io_prep_fsync(&io_u->iocb, f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020056
57 return 0;
58}
59
60static struct io_u *fio_libaio_event(struct thread_data *td, int event)
61{
62 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010063 struct io_event *ev;
64 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020065
Jens Axboef4234792007-03-02 15:16:03 +010066 ev = ld->aio_events + event;
67 io_u = ev_to_iou(ev);
68
69 if (ev->res != io_u->xfer_buflen) {
70 if (ev->res > io_u->xfer_buflen)
71 io_u->error = -ev->res;
72 else
73 io_u->resid = io_u->xfer_buflen - ev->res;
74 } else
75 io_u->error = 0;
76
77 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020078}
79
Dan Ehrenberg675012f2011-08-30 20:36:15 -060080struct aio_ring {
81 unsigned id; /** kernel internal index number */
82 unsigned nr; /** number of io_events */
83 unsigned head;
84 unsigned tail;
Jens Axboec44b1ff2011-08-30 20:43:53 -060085
Dan Ehrenberg675012f2011-08-30 20:36:15 -060086 unsigned magic;
87 unsigned compat_features;
88 unsigned incompat_features;
89 unsigned header_length; /** size of aio_ring */
90
91 struct io_event events[0];
92};
93
94#define AIO_RING_MAGIC 0xa10a10a1
95
96static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
Jens Axboec44b1ff2011-08-30 20:43:53 -060097 struct io_event *events)
Dan Ehrenberg675012f2011-08-30 20:36:15 -060098{
99 long i = 0;
100 unsigned head;
Jens Axboec44b1ff2011-08-30 20:43:53 -0600101 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600102
103 while (i < max) {
104 head = ring->head;
105
106 if (head == ring->tail) {
107 /* There are no more completions */
108 break;
109 } else {
110 /* There is another completion to reap */
111 events[i] = ring->events[head];
112 read_barrier();
Jens Axboec44b1ff2011-08-30 20:43:53 -0600113 ring->head = (head + 1) % ring->nr;
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600114 i++;
115 }
116 }
117
118 return i;
119}
120
Jens Axboee7d2e612007-12-11 10:49:39 +0100121static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
122 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +0200123{
124 struct libaio_data *ld = td->io_ops->data;
Steven Langde890a12011-11-09 14:03:34 +0100125 struct libaio_options *o = td->eo;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200126 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
127 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200128
129 do {
Steven Langde890a12011-11-09 14:03:34 +0100130 if (o->userspace_reap == 1
Dan Ehrenberg675012f2011-08-30 20:36:15 -0600131 && actual_min == 0
132 && ((struct aio_ring *)(ld->aio_ctx))->magic
133 == AIO_RING_MAGIC) {
134 r = user_io_getevents(ld->aio_ctx, max,
135 ld->aio_events + events);
136 } else {
137 r = io_getevents(ld->aio_ctx, actual_min,
138 max, ld->aio_events + events, t);
139 }
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200140 if (r >= 0)
141 events += r;
142 else if (r == -EAGAIN)
Jens Axboe2866c822006-10-09 15:57:48 +0200143 usleep(100);
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200144 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +0200145
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +0200146 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +0200147}
148
149static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
150{
151 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +0200152
Jens Axboe7101d9c2007-09-12 13:12:39 +0200153 fio_ro_check(td, io_u);
154
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100155 if (ld->iocbs_nr == (int) td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +0100156 return FIO_Q_BUSY;
157
158 /*
159 * fsync is tricky, since it can fail and we need to do it
160 * serialized with other io. the reason is that linux doesn't
161 * support aio fsync yet. So return busy for the case where we
162 * have pending io, to let fio complete those first.
163 */
Jens Axboef0115312010-03-09 21:47:15 +0100164 if (ddir_sync(io_u->ddir)) {
Jens Axboe755200a2007-02-19 13:08:12 +0100165 if (ld->iocbs_nr)
166 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +0100167
Jens Axboef0115312010-03-09 21:47:15 +0100168 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +0200169 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100170 }
171
Jens Axboea5f30272010-07-19 16:19:55 -0600172 if (io_u->ddir == DDIR_TRIM) {
173 if (ld->iocbs_nr)
174 return FIO_Q_BUSY;
175
176 do_io_u_trim(td, io_u);
177 return FIO_Q_COMPLETED;
178 }
179
Jens Axboe755200a2007-02-19 13:08:12 +0100180 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100181 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +0100182 ld->iocbs_nr++;
183 return FIO_Q_QUEUED;
184}
185
Jens Axboe7e77dd02007-02-20 10:57:34 +0100186static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
187 unsigned int nr)
188{
189 struct timeval now;
190 unsigned int i;
191
Jens Axboe12d9d842008-10-16 21:26:10 +0200192 if (!fio_fill_issue_time(td))
193 return;
194
Jens Axboe7e77dd02007-02-20 10:57:34 +0100195 fio_gettime(&now, NULL);
196
197 for (i = 0; i < nr; i++) {
198 struct io_u *io_u = io_us[i];
199
200 memcpy(&io_u->issue_time, &now, sizeof(now));
201 io_u_queued(td, io_u);
202 }
203}
204
Jens Axboe755200a2007-02-19 13:08:12 +0100205static int fio_libaio_commit(struct thread_data *td)
206{
207 struct libaio_data *ld = td->io_ops->data;
208 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100209 struct io_u **io_us;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100210 int ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100211
212 if (!ld->iocbs_nr)
213 return 0;
214
Jens Axboe7e77dd02007-02-20 10:57:34 +0100215 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100216 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200217 do {
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100218 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
219 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100220 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200221 io_u_mark_submit(td, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100222 ld->iocbs_nr -= ret;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100223 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100224 iocbs += ret;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100225 ret = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200226 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
227 if (!ret)
228 io_u_mark_submit(td, ret);
Jens Axboe2866c822006-10-09 15:57:48 +0200229 continue;
Jens Axboe838bc702008-05-22 13:08:23 +0200230 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200231 break;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100232 } while (ld->iocbs_nr);
Jens Axboe2866c822006-10-09 15:57:48 +0200233
Jens Axboe36167d82007-02-18 05:41:31 +0100234 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200235}
236
237static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
238{
239 struct libaio_data *ld = td->io_ops->data;
240
241 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
242}
243
244static void fio_libaio_cleanup(struct thread_data *td)
245{
246 struct libaio_data *ld = td->io_ops->data;
247
248 if (ld) {
249 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100250 free(ld->aio_events);
251 free(ld->iocbs);
252 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200253 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200254 }
255}
256
257static int fio_libaio_init(struct thread_data *td)
258{
259 struct libaio_data *ld = malloc(sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100260 int err;
Jens Axboe2866c822006-10-09 15:57:48 +0200261
262 memset(ld, 0, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100263
264 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
265 if (err) {
266 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200267 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100268 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200269 return 1;
270 }
271
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100272 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
273 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
274 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
Jens Axboe755200a2007-02-19 13:08:12 +0100275 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100276 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
277 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100278 ld->iocbs_nr = 0;
279
Jens Axboe2866c822006-10-09 15:57:48 +0200280 td->io_ops->data = ld;
281 return 0;
282}
283
Jens Axboe5f350952006-11-07 15:20:59 +0100284static struct ioengine_ops ioengine = {
Steven Langde890a12011-11-09 14:03:34 +0100285 .name = "libaio",
286 .version = FIO_IOOPS_VERSION,
287 .init = fio_libaio_init,
288 .prep = fio_libaio_prep,
289 .queue = fio_libaio_queue,
290 .commit = fio_libaio_commit,
291 .cancel = fio_libaio_cancel,
292 .getevents = fio_libaio_getevents,
293 .event = fio_libaio_event,
294 .cleanup = fio_libaio_cleanup,
295 .open_file = generic_open_file,
296 .close_file = generic_close_file,
297 .get_file_size = generic_get_file_size,
298 .options = options,
299 .option_struct_size = sizeof(struct libaio_options),
Jens Axboe2866c822006-10-09 15:57:48 +0200300};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100301
302#else /* FIO_HAVE_LIBAIO */
303
304/*
305 * When we have a proper configure system in place, we simply wont build
306 * and install this io engine. For now install a crippled version that
307 * just complains and fails to load.
308 */
309static int fio_libaio_init(struct thread_data fio_unused *td)
310{
Jens Axboea3edaf72010-09-26 10:53:40 +0900311 log_err("fio: libaio not available\n");
Jens Axboe34cfcda2006-11-03 14:00:45 +0100312 return 1;
313}
314
Jens Axboe5f350952006-11-07 15:20:59 +0100315static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100316 .name = "libaio",
317 .version = FIO_IOOPS_VERSION,
318 .init = fio_libaio_init,
319};
320
321#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100322
323static void fio_init fio_libaio_register(void)
324{
325 register_ioengine(&ioengine);
326}
327
328static void fio_exit fio_libaio_unregister(void)
329{
330 unregister_ioengine(&ioengine);
331}