blob: a659ba9bc141890b57ff0b29180eb446bf6dc17f [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
2 * native linux aio io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
Jens Axboe5f350952006-11-07 15:20:59 +010010
11#include "../fio.h"
12#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020013
Jens Axboe34cfcda2006-11-03 14:00:45 +010014#ifdef FIO_HAVE_LIBAIO
15
Jens Axboe2866c822006-10-09 15:57:48 +020016#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
17
18struct libaio_data {
19 io_context_t aio_ctx;
20 struct io_event *aio_events;
Jens Axboe755200a2007-02-19 13:08:12 +010021 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +010022 struct io_u **io_us;
Jens Axboe755200a2007-02-19 13:08:12 +010023 int iocbs_nr;
Jens Axboe2866c822006-10-09 15:57:48 +020024};
25
Jens Axboe7a16dd02006-10-18 17:21:58 +020026static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020027{
Jens Axboe53cdc682006-10-18 11:50:58 +020028 struct fio_file *f = io_u->file;
29
Jens Axboe2866c822006-10-09 15:57:48 +020030 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010031 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 +020032 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010033 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 +020034 else if (io_u->ddir == DDIR_SYNC)
35 io_prep_fsync(&io_u->iocb, f->fd);
36 else
37 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +020038
39 return 0;
40}
41
42static struct io_u *fio_libaio_event(struct thread_data *td, int event)
43{
44 struct libaio_data *ld = td->io_ops->data;
45
46 return ev_to_iou(ld->aio_events + event);
47}
48
49static int fio_libaio_getevents(struct thread_data *td, int min, int max,
50 struct timespec *t)
51{
52 struct libaio_data *ld = td->io_ops->data;
53 long r;
54
55 do {
56 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
Jens Axboeccbb91c2007-01-08 22:27:05 +010057 if (r >= min)
58 break;
59 else if (r == -EAGAIN) {
Jens Axboe2866c822006-10-09 15:57:48 +020060 usleep(100);
61 continue;
62 } else if (r == -EINTR)
63 continue;
Jens Axboe84585002006-10-19 20:26:22 +020064 else if (r != 0)
Jens Axboe2866c822006-10-09 15:57:48 +020065 break;
66 } while (1);
67
Jens Axboe22819ec2007-02-18 07:47:14 +010068 return r;
Jens Axboe2866c822006-10-09 15:57:48 +020069}
70
71static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
72{
73 struct libaio_data *ld = td->io_ops->data;
Jens Axboe2866c822006-10-09 15:57:48 +020074
Jens Axboe755200a2007-02-19 13:08:12 +010075 if (ld->iocbs_nr == (int) td->iodepth)
76 return FIO_Q_BUSY;
77
78 /*
79 * fsync is tricky, since it can fail and we need to do it
80 * serialized with other io. the reason is that linux doesn't
81 * support aio fsync yet. So return busy for the case where we
82 * have pending io, to let fio complete those first.
83 */
84 if (io_u->ddir == DDIR_SYNC) {
85 if (ld->iocbs_nr)
86 return FIO_Q_BUSY;
87 if (fsync(io_u->file->fd) < 0)
88 io_u->error = errno;
89
90 return FIO_Q_COMPLETED;
91 }
92
93 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
Jens Axboe7e77dd02007-02-20 10:57:34 +010094 ld->io_us[ld->iocbs_nr] = io_u;
Jens Axboe755200a2007-02-19 13:08:12 +010095 ld->iocbs_nr++;
96 return FIO_Q_QUEUED;
97}
98
Jens Axboe7e77dd02007-02-20 10:57:34 +010099static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
100 unsigned int nr)
101{
102 struct timeval now;
103 unsigned int i;
104
105 fio_gettime(&now, NULL);
106
107 for (i = 0; i < nr; i++) {
108 struct io_u *io_u = io_us[i];
109
110 memcpy(&io_u->issue_time, &now, sizeof(now));
111 io_u_queued(td, io_u);
112 }
113}
114
Jens Axboe755200a2007-02-19 13:08:12 +0100115static int fio_libaio_commit(struct thread_data *td)
116{
117 struct libaio_data *ld = td->io_ops->data;
118 struct iocb **iocbs;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100119 struct io_u **io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100120 int ret, iocbs_nr;
121
122 if (!ld->iocbs_nr)
123 return 0;
124
125 iocbs_nr = ld->iocbs_nr;
Jens Axboe7e77dd02007-02-20 10:57:34 +0100126 io_us = ld->io_us;
Jens Axboe755200a2007-02-19 13:08:12 +0100127 iocbs = ld->iocbs;
Jens Axboe2866c822006-10-09 15:57:48 +0200128 do {
Jens Axboe755200a2007-02-19 13:08:12 +0100129 ret = io_submit(ld->aio_ctx, iocbs_nr, iocbs);
130 if (ret == iocbs_nr) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100131 fio_libaio_queued(td, io_us, ret);
Jens Axboe755200a2007-02-19 13:08:12 +0100132 ret = 0;
133 break;
134 } else if (ret > 0) {
Jens Axboe7e77dd02007-02-20 10:57:34 +0100135 fio_libaio_queued(td, io_us, ret);
136 io_us += ret;
Jens Axboe755200a2007-02-19 13:08:12 +0100137 iocbs += ret;
138 iocbs_nr -= ret;
139 continue;
140 } else if (ret == -EAGAIN || !ret)
Jens Axboe2866c822006-10-09 15:57:48 +0200141 usleep(100);
142 else if (ret == -EINTR)
143 continue;
Jens Axboe755200a2007-02-19 13:08:12 +0100144 else
Jens Axboe2866c822006-10-09 15:57:48 +0200145 break;
146 } while (1);
147
Jens Axboe755200a2007-02-19 13:08:12 +0100148 if (!ret)
149 ld->iocbs_nr = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200150
Jens Axboe36167d82007-02-18 05:41:31 +0100151 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200152}
153
154static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
155{
156 struct libaio_data *ld = td->io_ops->data;
157
158 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
159}
160
161static void fio_libaio_cleanup(struct thread_data *td)
162{
163 struct libaio_data *ld = td->io_ops->data;
164
165 if (ld) {
166 io_destroy(ld->aio_ctx);
Jens Axboe7e77dd02007-02-20 10:57:34 +0100167 free(ld->aio_events);
168 free(ld->iocbs);
169 free(ld->io_us);
Jens Axboe2866c822006-10-09 15:57:48 +0200170 free(ld);
171 td->io_ops->data = NULL;
172 }
173}
174
175static int fio_libaio_init(struct thread_data *td)
176{
177 struct libaio_data *ld = malloc(sizeof(*ld));
178
179 memset(ld, 0, sizeof(*ld));
180 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
181 td_verror(td, errno);
Jens Axboecb781c72006-11-07 14:02:48 +0100182 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200183 return 1;
184 }
185
186 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
Jens Axboe84585002006-10-19 20:26:22 +0200187 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
Jens Axboe755200a2007-02-19 13:08:12 +0100188 ld->iocbs = malloc(td->iodepth * sizeof(struct iocb *));
189 memset(ld->iocbs, 0, sizeof(struct iocb *));
Jens Axboe7e77dd02007-02-20 10:57:34 +0100190 ld->io_us = malloc(td->iodepth * sizeof(struct io_u *));
191 memset(ld->io_us, 0, td->iodepth * sizeof(struct io_u *));
Jens Axboe755200a2007-02-19 13:08:12 +0100192 ld->iocbs_nr = 0;
193
Jens Axboe2866c822006-10-09 15:57:48 +0200194 td->io_ops->data = ld;
195 return 0;
196}
197
Jens Axboe5f350952006-11-07 15:20:59 +0100198static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200199 .name = "libaio",
200 .version = FIO_IOOPS_VERSION,
201 .init = fio_libaio_init,
202 .prep = fio_libaio_prep,
203 .queue = fio_libaio_queue,
Jens Axboe755200a2007-02-19 13:08:12 +0100204 .commit = fio_libaio_commit,
Jens Axboe2866c822006-10-09 15:57:48 +0200205 .cancel = fio_libaio_cancel,
206 .getevents = fio_libaio_getevents,
207 .event = fio_libaio_event,
208 .cleanup = fio_libaio_cleanup,
Jens Axboe2866c822006-10-09 15:57:48 +0200209};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100210
211#else /* FIO_HAVE_LIBAIO */
212
213/*
214 * When we have a proper configure system in place, we simply wont build
215 * and install this io engine. For now install a crippled version that
216 * just complains and fails to load.
217 */
218static int fio_libaio_init(struct thread_data fio_unused *td)
219{
220 fprintf(stderr, "fio: libaio not available\n");
221 return 1;
222}
223
Jens Axboe5f350952006-11-07 15:20:59 +0100224static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100225 .name = "libaio",
226 .version = FIO_IOOPS_VERSION,
227 .init = fio_libaio_init,
228};
229
230#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100231
232static void fio_init fio_libaio_register(void)
233{
234 register_ioengine(&ioengine);
235}
236
237static void fio_exit fio_libaio_unregister(void)
238{
239 unregister_ioengine(&ioengine);
240}