blob: c837ab6bb9e4e9af10553bff980cc318e7274821 [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
Jens Axboe7a16dd02006-10-18 17:21:58 +020027static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020028{
Jens Axboe53cdc682006-10-18 11:50:58 +020029 struct fio_file *f = io_u->file;
30
Jens Axboe2866c822006-10-09 15:57:48 +020031 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010032 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 +020033 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010034 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 +020035 else if (ddir_sync(io_u->ddir))
Jens Axboe87dc1ab2006-10-24 14:41:26 +020036 io_prep_fsync(&io_u->iocb, f->fd);
Jens Axboe2866c822006-10-09 15:57:48 +020037
38 return 0;
39}
40
41static struct io_u *fio_libaio_event(struct thread_data *td, int event)
42{
43 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010044 struct io_event *ev;
45 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020046
Jens Axboef4234792007-03-02 15:16:03 +010047 ev = ld->aio_events + event;
48 io_u = ev_to_iou(ev);
49
50 if (ev->res != io_u->xfer_buflen) {
51 if (ev->res > io_u->xfer_buflen)
52 io_u->error = -ev->res;
53 else
54 io_u->resid = io_u->xfer_buflen - ev->res;
55 } else
56 io_u->error = 0;
57
58 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020059}
60
Jens Axboee7d2e612007-12-11 10:49:39 +010061static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
62 unsigned int max, struct timespec *t)
Jens Axboe2866c822006-10-09 15:57:48 +020063{
64 struct libaio_data *ld = td->io_ops->data;
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +020065 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
66 int r, events = 0;
Jens Axboe2866c822006-10-09 15:57:48 +020067
68 do {
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +020069 r = io_getevents(ld->aio_ctx, actual_min, max, ld->aio_events + events, t);
70 if (r >= 0)
71 events += r;
72 else if (r == -EAGAIN)
Jens Axboe2866c822006-10-09 15:57:48 +020073 usleep(100);
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +020074 } while (events < min);
Jens Axboe2866c822006-10-09 15:57:48 +020075
Dan Ehrenberg0b7fdba2011-07-23 13:36:46 +020076 return r < 0 ? r : events;
Jens Axboe2866c822006-10-09 15:57:48 +020077}
78
79static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
80{
81 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +020082
Jens Axboe7101d9c2007-09-12 13:12:39 +020083 fio_ro_check(td, io_u);
84
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010085 if (ld->iocbs_nr == (int) td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +010086 return FIO_Q_BUSY;
87
88 /*
89 * fsync is tricky, since it can fail and we need to do it
90 * serialized with other io. the reason is that linux doesn't
91 * support aio fsync yet. So return busy for the case where we
92 * have pending io, to let fio complete those first.
93 */
Jens Axboef0115312010-03-09 21:47:15 +010094 if (ddir_sync(io_u->ddir)) {
Jens Axboe755200a2007-02-19 13:08:12 +010095 if (ld->iocbs_nr)
96 return FIO_Q_BUSY;
Jens Axboe755200a2007-02-19 13:08:12 +010097
Jens Axboef0115312010-03-09 21:47:15 +010098 do_io_u_sync(td, io_u);
Jens Axboe5f9099e2009-06-16 22:40:26 +020099 return FIO_Q_COMPLETED;
Jens Axboe755200a2007-02-19 13:08:12 +0100100 }
101
Jens Axboea5f30272010-07-19 16:19:55 -0600102 if (io_u->ddir == DDIR_TRIM) {
103 if (ld->iocbs_nr)
104 return FIO_Q_BUSY;
105
106 do_io_u_trim(td, io_u);
107 return FIO_Q_COMPLETED;
108 }
109
Jens Axboe755200a2007-02-19 13:08:12 +0100110 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100111 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +0100112 ld->iocbs_nr++;
113 return FIO_Q_QUEUED;
114}
115
Jens Axboe7e77dd02007-02-20 10:57:34 +0100116static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
117 unsigned int nr)
118{
119 struct timeval now;
120 unsigned int i;
121
Jens Axboe12d9d842008-10-16 21:26:10 +0200122 if (!fio_fill_issue_time(td))
123 return;
124
Jens Axboe7e77dd02007-02-20 10:57:34 +0100125 fio_gettime(&now, NULL);
126
127 for (i = 0; i < nr; i++) {
128 struct io_u *io_u = io_us[i];
129
130 memcpy(&io_u->issue_time, &now, sizeof(now));
131 io_u_queued(td, io_u);
132 }
133}
134
Jens Axboe755200a2007-02-19 13:08:12 +0100135static int fio_libaio_commit(struct thread_data *td)
136{
137 struct libaio_data *ld = td->io_ops->data;
138 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100139 struct io_u **io_us;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100140 int ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100141
142 if (!ld->iocbs_nr)
143 return 0;
144
Jens Axboe7e77dd02007-02-20 10:57:34 +0100145 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100146 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200147 do {
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100148 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
149 if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100150 fio_libaio_queued(td, io_us, ret);
Jens Axboe838bc702008-05-22 13:08:23 +0200151 io_u_mark_submit(td, ret);
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100152 ld->iocbs_nr -= ret;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100153 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100154 iocbs += ret;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100155 ret = 0;
Jens Axboe838bc702008-05-22 13:08:23 +0200156 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
157 if (!ret)
158 io_u_mark_submit(td, ret);
Jens Axboe2866c822006-10-09 15:57:48 +0200159 continue;
Jens Axboe838bc702008-05-22 13:08:23 +0200160 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200161 break;
Jens Axboe5e00c2c2008-01-18 10:26:58 +0100162 } while (ld->iocbs_nr);
Jens Axboe2866c822006-10-09 15:57:48 +0200163
Jens Axboe36167d82007-02-18 05:41:31 +0100164 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200165}
166
167static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
168{
169 struct libaio_data *ld = td->io_ops->data;
170
171 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
172}
173
174static void fio_libaio_cleanup(struct thread_data *td)
175{
176 struct libaio_data *ld = td->io_ops->data;
177
178 if (ld) {
179 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100180 free(ld->aio_events);
181 free(ld->iocbs);
182 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200183 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200184 }
185}
186
187static int fio_libaio_init(struct thread_data *td)
188{
189 struct libaio_data *ld = malloc(sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100190 int err;
Jens Axboe2866c822006-10-09 15:57:48 +0200191
192 memset(ld, 0, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100193
194 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
195 if (err) {
196 td_verror(td, -err, "io_queue_init");
Jens Axboe75de55a2008-04-10 20:52:40 +0200197 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100198 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200199 return 1;
200 }
201
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100202 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
203 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
204 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
Jens Axboe755200a2007-02-19 13:08:12 +0100205 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100206 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
207 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100208 ld->iocbs_nr = 0;
209
Jens Axboe2866c822006-10-09 15:57:48 +0200210 td->io_ops->data = ld;
211 return 0;
212}
213
Jens Axboe5f350952006-11-07 15:20:59 +0100214static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200215 .name = "libaio",
216 .version = FIO_IOOPS_VERSION,
217 .init = fio_libaio_init,
218 .prep = fio_libaio_prep,
219 .queue = fio_libaio_queue,
Jens Axboe755200a2007-02-19 13:08:12 +0100220 .commit = fio_libaio_commit,
Jens Axboe2866c822006-10-09 15:57:48 +0200221 .cancel = fio_libaio_cancel,
222 .getevents = fio_libaio_getevents,
223 .event = fio_libaio_event,
224 .cleanup = fio_libaio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100225 .open_file = generic_open_file,
226 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100227 .get_file_size = generic_get_file_size,
Jens Axboe2866c822006-10-09 15:57:48 +0200228};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100229
230#else /* FIO_HAVE_LIBAIO */
231
232/*
233 * When we have a proper configure system in place, we simply wont build
234 * and install this io engine. For now install a crippled version that
235 * just complains and fails to load.
236 */
237static int fio_libaio_init(struct thread_data fio_unused *td)
238{
Jens Axboea3edaf72010-09-26 10:53:40 +0900239 log_err("fio: libaio not available\n");
Jens Axboe34cfcda2006-11-03 14:00:45 +0100240 return 1;
241}
242
Jens Axboe5f350952006-11-07 15:20:59 +0100243static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100244 .name = "libaio",
245 .version = FIO_IOOPS_VERSION,
246 .init = fio_libaio_init,
247};
248
249#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100250
251static void fio_init fio_libaio_register(void)
252{
253 register_ioengine(&ioengine);
254}
255
256static void fio_exit fio_libaio_unregister(void)
257{
258 unregister_ioengine(&ioengine);
259}