blob: 8f677115056d2e922bda4eaa5f766423db07b6ec [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 Axboe87dc1ab2006-10-24 14:41:26 +020035 else if (io_u->ddir == DDIR_SYNC)
36 io_prep_fsync(&io_u->iocb, f->fd);
37 else
38 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +020039
40 return 0;
41}
42
43static struct io_u *fio_libaio_event(struct thread_data *td, int event)
44{
45 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010046 struct io_event *ev;
47 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020048
Jens Axboef4234792007-03-02 15:16:03 +010049 ev = ld->aio_events + event;
50 io_u = ev_to_iou(ev);
51
52 if (ev->res != io_u->xfer_buflen) {
53 if (ev->res > io_u->xfer_buflen)
54 io_u->error = -ev->res;
55 else
56 io_u->resid = io_u->xfer_buflen - ev->res;
57 } else
58 io_u->error = 0;
59
60 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020061}
62
63static int fio_libaio_getevents(struct thread_data *td, int min, int max,
64 struct timespec *t)
65{
66 struct libaio_data *ld = td->io_ops->data;
67 long r;
68
69 do {
70 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
Jens Axboeccbb91c2007-01-08 22:27:05 +010071 if (r >= min)
72 break;
73 else if (r == -EAGAIN) {
Jens Axboe2866c822006-10-09 15:57:48 +020074 usleep(100);
75 continue;
76 } else if (r == -EINTR)
77 continue;
Jens Axboe84585002006-10-19 20:26:22 +020078 else if (r != 0)
Jens Axboe2866c822006-10-09 15:57:48 +020079 break;
80 } while (1);
81
Jens Axboe22819ec2007-02-18 07:47:14 +010082 return r;
Jens Axboe2866c822006-10-09 15:57:48 +020083}
84
85static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
86{
87 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +020088
Jens Axboe7101d9c2007-09-12 13:12:39 +020089 fio_ro_check(td, io_u);
90
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010091 if (ld->iocbs_nr == (int) td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +010092 return FIO_Q_BUSY;
93
94 /*
95 * fsync is tricky, since it can fail and we need to do it
96 * serialized with other io. the reason is that linux doesn't
97 * support aio fsync yet. So return busy for the case where we
98 * have pending io, to let fio complete those first.
99 */
100 if (io_u->ddir == DDIR_SYNC) {
101 if (ld->iocbs_nr)
102 return FIO_Q_BUSY;
103 if (fsync(io_u->file->fd) < 0)
104 io_u->error = errno;
105
106 return FIO_Q_COMPLETED;
107 }
108
109 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100110 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +0100111 ld->iocbs_nr++;
112 return FIO_Q_QUEUED;
113}
114
Jens Axboe7e77dd02007-02-20 10:57:34 +0100115static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
116 unsigned int nr)
117{
118 struct timeval now;
119 unsigned int i;
120
121 fio_gettime(&now, NULL);
122
123 for (i = 0; i < nr; i++) {
124 struct io_u *io_u = io_us[i];
125
126 memcpy(&io_u->issue_time, &now, sizeof(now));
127 io_u_queued(td, io_u);
128 }
129}
130
Jens Axboe755200a2007-02-19 13:08:12 +0100131static int fio_libaio_commit(struct thread_data *td)
132{
133 struct libaio_data *ld = td->io_ops->data;
134 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100135 struct io_u **io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100136 int ret, iocbs_nr;
137
138 if (!ld->iocbs_nr)
139 return 0;
140
141 iocbs_nr = ld->iocbs_nr;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100142 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100143 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200144 do {
Jens Axboe755200a2007-02-19 13:08:12 +0100145 ret = io_submit(ld->aio_ctx, iocbs_nr, iocbs);
146 if (ret == iocbs_nr) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100147 fio_libaio_queued(td, io_us, ret);
Jens Axboe755200a2007-02-19 13:08:12 +0100148 ret = 0;
149 break;
150 } else if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100151 fio_libaio_queued(td, io_us, ret);
152 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100153 iocbs += ret;
154 iocbs_nr -= ret;
155 continue;
156 } else if (ret == -EAGAIN || !ret)
Jens Axboe2866c822006-10-09 15:57:48 +0200157 usleep(100);
158 else if (ret == -EINTR)
159 continue;
Jens Axboe755200a2007-02-19 13:08:12 +0100160 else
Jens Axboe2866c822006-10-09 15:57:48 +0200161 break;
162 } while (1);
163
Jens Axboe755200a2007-02-19 13:08:12 +0100164 if (!ret)
165 ld->iocbs_nr = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200166
Jens Axboe36167d82007-02-18 05:41:31 +0100167 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200168}
169
170static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
171{
172 struct libaio_data *ld = td->io_ops->data;
173
174 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
175}
176
177static void fio_libaio_cleanup(struct thread_data *td)
178{
179 struct libaio_data *ld = td->io_ops->data;
180
181 if (ld) {
182 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100183 free(ld->aio_events);
184 free(ld->iocbs);
185 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200186 free(ld);
187 td->io_ops->data = NULL;
188 }
189}
190
191static int fio_libaio_init(struct thread_data *td)
192{
193 struct libaio_data *ld = malloc(sizeof(*ld));
Jens Axboe7d059f82007-06-18 09:48:57 +0200194 static int warn_print;
Jens Axboec1db2dc2007-03-20 10:42:07 +0100195 int err;
Jens Axboe2866c822006-10-09 15:57:48 +0200196
Jens Axboe7d059f82007-06-18 09:48:57 +0200197 if (td->o.iodepth > 1 && !td->o.odirect && !warn_print) {
198 log_info("fio: libaio engine is only async for non-buffered IO\n");
199 warn_print = 1;
200 }
201
Jens Axboe2866c822006-10-09 15:57:48 +0200202 memset(ld, 0, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100203
204 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
205 if (err) {
206 td_verror(td, -err, "io_queue_init");
Jens Axboecb781c72006-11-07 14:02:48 +0100207 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200208 return 1;
209 }
210
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100211 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
212 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
213 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
Jens Axboe755200a2007-02-19 13:08:12 +0100214 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100215 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
216 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100217 ld->iocbs_nr = 0;
218
Jens Axboe2866c822006-10-09 15:57:48 +0200219 td->io_ops->data = ld;
220 return 0;
221}
222
Jens Axboe5f350952006-11-07 15:20:59 +0100223static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200224 .name = "libaio",
225 .version = FIO_IOOPS_VERSION,
226 .init = fio_libaio_init,
227 .prep = fio_libaio_prep,
228 .queue = fio_libaio_queue,
Jens Axboe755200a2007-02-19 13:08:12 +0100229 .commit = fio_libaio_commit,
Jens Axboe2866c822006-10-09 15:57:48 +0200230 .cancel = fio_libaio_cancel,
231 .getevents = fio_libaio_getevents,
232 .event = fio_libaio_event,
233 .cleanup = fio_libaio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100234 .open_file = generic_open_file,
235 .close_file = generic_close_file,
Jens Axboe2866c822006-10-09 15:57:48 +0200236};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100237
238#else /* FIO_HAVE_LIBAIO */
239
240/*
241 * When we have a proper configure system in place, we simply wont build
242 * and install this io engine. For now install a crippled version that
243 * just complains and fails to load.
244 */
245static int fio_libaio_init(struct thread_data fio_unused *td)
246{
247 fprintf(stderr, "fio: libaio not available\n");
248 return 1;
249}
250
Jens Axboe5f350952006-11-07 15:20:59 +0100251static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100252 .name = "libaio",
253 .version = FIO_IOOPS_VERSION,
254 .init = fio_libaio_init,
255};
256
257#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100258
259static void fio_init fio_libaio_register(void)
260{
261 register_ioengine(&ioengine);
262}
263
264static void fio_exit fio_libaio_unregister(void)
265{
266 unregister_ioengine(&ioengine);
267}