blob: acc125f48e46df799c7d7fdcf0c42daea9694145 [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 Axboe8d714a62007-02-10 12:59:48 +010021 struct io_u *sync_io_u;
Jens Axboe2866c822006-10-09 15:57:48 +020022};
23
Jens Axboe7a16dd02006-10-18 17:21:58 +020024static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +020025{
Jens Axboe53cdc682006-10-18 11:50:58 +020026 struct fio_file *f = io_u->file;
27
Jens Axboe2866c822006-10-09 15:57:48 +020028 if (io_u->ddir == DDIR_READ)
Jens Axboecec6b552007-02-06 20:15:38 +010029 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 +020030 else if (io_u->ddir == DDIR_WRITE)
Jens Axboecec6b552007-02-06 20:15:38 +010031 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 +020032 else if (io_u->ddir == DDIR_SYNC)
33 io_prep_fsync(&io_u->iocb, f->fd);
34 else
35 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +020036
37 return 0;
38}
39
40static struct io_u *fio_libaio_event(struct thread_data *td, int event)
41{
42 struct libaio_data *ld = td->io_ops->data;
43
Jens Axboe8d714a62007-02-10 12:59:48 +010044 if (ld->sync_io_u) {
45 struct io_u *ret = ld->sync_io_u;
46
47 ld->sync_io_u = NULL;
48 return ret;
49 }
50
Jens Axboe2866c822006-10-09 15:57:48 +020051 return ev_to_iou(ld->aio_events + event);
52}
53
54static int fio_libaio_getevents(struct thread_data *td, int min, int max,
55 struct timespec *t)
56{
57 struct libaio_data *ld = td->io_ops->data;
58 long r;
59
Jens Axboe8d714a62007-02-10 12:59:48 +010060 if (ld->sync_io_u)
61 return 1;
62
Jens Axboe2866c822006-10-09 15:57:48 +020063 do {
64 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
Jens Axboeccbb91c2007-01-08 22:27:05 +010065 if (r >= min)
66 break;
67 else if (r == -EAGAIN) {
Jens Axboe2866c822006-10-09 15:57:48 +020068 usleep(100);
69 continue;
70 } else if (r == -EINTR)
71 continue;
Jens Axboe84585002006-10-19 20:26:22 +020072 else if (r != 0)
Jens Axboe2866c822006-10-09 15:57:48 +020073 break;
74 } while (1);
75
Jens Axboeeaf09db2006-10-27 10:45:12 +020076 if (r < 0)
77 r = -r;
78
79 return (int) r;
Jens Axboe2866c822006-10-09 15:57:48 +020080}
81
82static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
83{
84 struct libaio_data *ld = td->io_ops->data;
85 struct iocb *iocb = &io_u->iocb;
86 long ret;
87
88 do {
89 ret = io_submit(ld->aio_ctx, 1, &iocb);
90 if (ret == 1)
91 return 0;
Jens Axboe84585002006-10-19 20:26:22 +020092 else if (ret == -EAGAIN || !ret)
Jens Axboe2866c822006-10-09 15:57:48 +020093 usleep(100);
94 else if (ret == -EINTR)
95 continue;
Jens Axboee5c40aa2007-02-10 12:57:55 +010096 else if (ret == -EINVAL && io_u->ddir == DDIR_SYNC) {
97 /*
98 * the async fsync doesn't currently seem to be
99 * supported, so just fsync if we fail with EINVAL
100 * for a sync. since buffered io is also sync
101 * with libaio (still), we don't have pending
102 * requests to flush first.
103 */
104 ret = fsync(io_u->file->fd);
Jens Axboe8d714a62007-02-10 12:59:48 +0100105 ld->sync_io_u = io_u;
Jens Axboee5c40aa2007-02-10 12:57:55 +0100106 break;
107 } else
Jens Axboe2866c822006-10-09 15:57:48 +0200108 break;
109 } while (1);
110
Jens Axboe353a7e02006-10-25 09:16:07 +0200111 if (ret <= 0) {
Jens Axboecec6b552007-02-06 20:15:38 +0100112 io_u->resid = io_u->xfer_buflen;
Jens Axboe353a7e02006-10-25 09:16:07 +0200113 io_u->error = -ret;
114 return 1;
115 }
Jens Axboe2866c822006-10-09 15:57:48 +0200116
Jens Axboe353a7e02006-10-25 09:16:07 +0200117 return 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200118}
119
120static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
121{
122 struct libaio_data *ld = td->io_ops->data;
123
124 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
125}
126
127static void fio_libaio_cleanup(struct thread_data *td)
128{
129 struct libaio_data *ld = td->io_ops->data;
130
131 if (ld) {
132 io_destroy(ld->aio_ctx);
133 if (ld->aio_events)
134 free(ld->aio_events);
135
136 free(ld);
137 td->io_ops->data = NULL;
138 }
139}
140
141static int fio_libaio_init(struct thread_data *td)
142{
143 struct libaio_data *ld = malloc(sizeof(*ld));
144
145 memset(ld, 0, sizeof(*ld));
146 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
147 td_verror(td, errno);
Jens Axboecb781c72006-11-07 14:02:48 +0100148 free(ld);
Jens Axboe2866c822006-10-09 15:57:48 +0200149 return 1;
150 }
151
152 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
Jens Axboe84585002006-10-19 20:26:22 +0200153 memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event));
Jens Axboe2866c822006-10-09 15:57:48 +0200154 td->io_ops->data = ld;
155 return 0;
156}
157
Jens Axboe5f350952006-11-07 15:20:59 +0100158static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200159 .name = "libaio",
160 .version = FIO_IOOPS_VERSION,
161 .init = fio_libaio_init,
162 .prep = fio_libaio_prep,
163 .queue = fio_libaio_queue,
164 .cancel = fio_libaio_cancel,
165 .getevents = fio_libaio_getevents,
166 .event = fio_libaio_event,
167 .cleanup = fio_libaio_cleanup,
Jens Axboe2866c822006-10-09 15:57:48 +0200168};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100169
170#else /* FIO_HAVE_LIBAIO */
171
172/*
173 * When we have a proper configure system in place, we simply wont build
174 * and install this io engine. For now install a crippled version that
175 * just complains and fails to load.
176 */
177static int fio_libaio_init(struct thread_data fio_unused *td)
178{
179 fprintf(stderr, "fio: libaio not available\n");
180 return 1;
181}
182
Jens Axboe5f350952006-11-07 15:20:59 +0100183static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100184 .name = "libaio",
185 .version = FIO_IOOPS_VERSION,
186 .init = fio_libaio_init,
187};
188
189#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100190
191static void fio_init fio_libaio_register(void)
192{
193 register_ioengine(&ioengine);
194}
195
196static void fio_exit fio_libaio_unregister(void)
197{
198 unregister_ioengine(&ioengine);
199}