blob: 7c44927a3b8b5a3ac39b37b4eb43ed6760582d87 [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"
14#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020015
Jens Axboe34cfcda2006-11-03 14:00:45 +010016#ifdef FIO_HAVE_LIBAIO
17
Jens Axboe2866c822006-10-09 15:57:48 +020018#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
19
20struct libaio_data {
21 io_context_t aio_ctx;
22 struct io_event *aio_events;
Jens Axboe755200a2007-02-19 13:08:12 +010023 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +010024 struct io_u **io_us;
Jens Axboe755200a2007-02-19 13:08:12 +010025 int iocbs_nr;
Jens Axboe2866c822006-10-09 15:57:48 +020026};
27
Jens Axboe7a16dd02006-10-18 17:21:58 +020028static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020029{
Jens Axboe53cdc682006-10-18 11:50:58 +020030 struct fio_file *f = io_u->file;
31
Jens Axboe2866c822006-10-09 15:57:48 +020032 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010033 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 +020034 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010035 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 +020036 else if (io_u->ddir == DDIR_SYNC)
37 io_prep_fsync(&io_u->iocb, f->fd);
38 else
39 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +020040
41 return 0;
42}
43
44static struct io_u *fio_libaio_event(struct thread_data *td, int event)
45{
46 struct libaio_data *ld = td->io_ops->data;
Jens Axboef4234792007-03-02 15:16:03 +010047 struct io_event *ev;
48 struct io_u *io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020049
Jens Axboef4234792007-03-02 15:16:03 +010050 ev = ld->aio_events + event;
51 io_u = ev_to_iou(ev);
52
53 if (ev->res != io_u->xfer_buflen) {
54 if (ev->res > io_u->xfer_buflen)
55 io_u->error = -ev->res;
56 else
57 io_u->resid = io_u->xfer_buflen - ev->res;
58 } else
59 io_u->error = 0;
60
61 return io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020062}
63
64static int fio_libaio_getevents(struct thread_data *td, int min, int max,
65 struct timespec *t)
66{
67 struct libaio_data *ld = td->io_ops->data;
68 long r;
69
70 do {
71 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
Jens Axboeccbb91c2007-01-08 22:27:05 +010072 if (r >= min)
73 break;
74 else if (r == -EAGAIN) {
Jens Axboe2866c822006-10-09 15:57:48 +020075 usleep(100);
76 continue;
77 } else if (r == -EINTR)
78 continue;
Jens Axboe84585002006-10-19 20:26:22 +020079 else if (r != 0)
Jens Axboe2866c822006-10-09 15:57:48 +020080 break;
81 } while (1);
82
Jens Axboe22819ec2007-02-18 07:47:14 +010083 return r;
Jens Axboe2866c822006-10-09 15:57:48 +020084}
85
86static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
87{
88 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +020089
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010090 if (ld->iocbs_nr == (int) td->o.iodepth)
Jens Axboe755200a2007-02-19 13:08:12 +010091 return FIO_Q_BUSY;
92
93 /*
94 * fsync is tricky, since it can fail and we need to do it
95 * serialized with other io. the reason is that linux doesn't
96 * support aio fsync yet. So return busy for the case where we
97 * have pending io, to let fio complete those first.
98 */
99 if (io_u->ddir == DDIR_SYNC) {
100 if (ld->iocbs_nr)
101 return FIO_Q_BUSY;
102 if (fsync(io_u->file->fd) < 0)
103 io_u->error = errno;
104
105 return FIO_Q_COMPLETED;
106 }
107
108 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100109 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +0100110 ld->iocbs_nr++;
111 return FIO_Q_QUEUED;
112}
113
Jens Axboe7e77dd02007-02-20 10:57:34 +0100114static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
115 unsigned int nr)
116{
117 struct timeval now;
118 unsigned int i;
119
120 fio_gettime(&now, NULL);
121
122 for (i = 0; i < nr; i++) {
123 struct io_u *io_u = io_us[i];
124
125 memcpy(&io_u->issue_time, &now, sizeof(now));
126 io_u_queued(td, io_u);
127 }
128}
129
Jens Axboe755200a2007-02-19 13:08:12 +0100130static int fio_libaio_commit(struct thread_data *td)
131{
132 struct libaio_data *ld = td->io_ops->data;
133 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100134 struct io_u **io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100135 int ret, iocbs_nr;
136
137 if (!ld->iocbs_nr)
138 return 0;
139
140 iocbs_nr = ld->iocbs_nr;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100141 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100142 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200143 do {
Jens Axboe755200a2007-02-19 13:08:12 +0100144 ret = io_submit(ld->aio_ctx, iocbs_nr, iocbs);
145 if (ret == iocbs_nr) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100146 fio_libaio_queued(td, io_us, ret);
Jens Axboe755200a2007-02-19 13:08:12 +0100147 ret = 0;
148 break;
149 } else if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100150 fio_libaio_queued(td, io_us, ret);
151 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100152 iocbs += ret;
153 iocbs_nr -= ret;
154 continue;
155 } else if (ret == -EAGAIN || !ret)
Jens Axboe2866c822006-10-09 15:57:48 +0200156 usleep(100);
157 else if (ret == -EINTR)
158 continue;
Jens Axboe755200a2007-02-19 13:08:12 +0100159 else
Jens Axboe2866c822006-10-09 15:57:48 +0200160 break;
161 } while (1);
162
Jens Axboe755200a2007-02-19 13:08:12 +0100163 if (!ret)
164 ld->iocbs_nr = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200165
Jens Axboe36167d82007-02-18 05:41:31 +0100166 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200167}
168
169static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
170{
171 struct libaio_data *ld = td->io_ops->data;
172
173 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
174}
175
176static void fio_libaio_cleanup(struct thread_data *td)
177{
178 struct libaio_data *ld = td->io_ops->data;
179
180 if (ld) {
181 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100182 free(ld->aio_events);
183 free(ld->iocbs);
184 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200185 free(ld);
186 td->io_ops->data = NULL;
187 }
188}
189
190static int fio_libaio_init(struct thread_data *td)
191{
192 struct libaio_data *ld = malloc(sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100193 int err;
Jens Axboe2866c822006-10-09 15:57:48 +0200194
195 memset(ld, 0, sizeof(*ld));
Jens Axboec1db2dc2007-03-20 10:42:07 +0100196
197 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
198 if (err) {
199 td_verror(td, -err, "io_queue_init");
Jens Axboecb781c72006-11-07 14:02:48 +0100200 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200201 return 1;
202 }
203
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100204 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
205 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
206 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
Jens Axboe755200a2007-02-19 13:08:12 +0100207 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100208 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
209 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100210 ld->iocbs_nr = 0;
211
Jens Axboe2866c822006-10-09 15:57:48 +0200212 td->io_ops->data = ld;
213 return 0;
214}
215
Jens Axboe5f350952006-11-07 15:20:59 +0100216static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200217 .name = "libaio",
218 .version = FIO_IOOPS_VERSION,
219 .init = fio_libaio_init,
220 .prep = fio_libaio_prep,
221 .queue = fio_libaio_queue,
Jens Axboe755200a2007-02-19 13:08:12 +0100222 .commit = fio_libaio_commit,
Jens Axboe2866c822006-10-09 15:57:48 +0200223 .cancel = fio_libaio_cancel,
224 .getevents = fio_libaio_getevents,
225 .event = fio_libaio_event,
226 .cleanup = fio_libaio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100227 .open_file = generic_open_file,
228 .close_file = generic_close_file,
Jens Axboe2866c822006-10-09 15:57:48 +0200229};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100230
231#else /* FIO_HAVE_LIBAIO */
232
233/*
234 * When we have a proper configure system in place, we simply wont build
235 * and install this io engine. For now install a crippled version that
236 * just complains and fails to load.
237 */
238static int fio_libaio_init(struct thread_data fio_unused *td)
239{
240 fprintf(stderr, "fio: libaio not available\n");
241 return 1;
242}
243
Jens Axboe5f350952006-11-07 15:20:59 +0100244static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100245 .name = "libaio",
246 .version = FIO_IOOPS_VERSION,
247 .init = fio_libaio_init,
248};
249
250#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100251
252static void fio_init fio_libaio_register(void)
253{
254 register_ioengine(&ioengine);
255}
256
257static void fio_exit fio_libaio_unregister(void)
258{
259 unregister_ioengine(&ioengine);
260}