Add FIO_OPT_G_IO_ENG category
[fio.git] / engines / libaio.c
... / ...
CommitLineData
1/*
2 * libaio engine
3 *
4 * IO engine using the Linux native aio interface.
5 *
6 */
7#include <stdio.h>
8#include <stdlib.h>
9#include <unistd.h>
10#include <errno.h>
11#include <assert.h>
12
13#include "../fio.h"
14
15#ifdef FIO_HAVE_LIBAIO
16
17#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;
22 struct iocb **iocbs;
23 struct io_u **io_us;
24 int iocbs_nr;
25};
26
27struct libaio_options {
28 struct thread_data *td;
29 unsigned int userspace_reap;
30};
31
32static struct fio_option options[] = {
33 {
34 .name = "userspace_reap",
35 .type = FIO_OPT_STR_SET,
36 .off1 = offsetof(struct libaio_options, userspace_reap),
37 .help = "Use alternative user-space reap implementation",
38 .category = FIO_OPT_G_IO_ENG,
39 },
40 {
41 .name = NULL,
42 },
43};
44
45static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
46{
47 struct fio_file *f = io_u->file;
48
49 if (io_u->ddir == DDIR_READ)
50 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
51 else if (io_u->ddir == DDIR_WRITE)
52 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
53 else if (ddir_sync(io_u->ddir))
54 io_prep_fsync(&io_u->iocb, f->fd);
55
56 return 0;
57}
58
59static struct io_u *fio_libaio_event(struct thread_data *td, int event)
60{
61 struct libaio_data *ld = td->io_ops->data;
62 struct io_event *ev;
63 struct io_u *io_u;
64
65 ev = ld->aio_events + event;
66 io_u = ev_to_iou(ev);
67
68 if (ev->res != io_u->xfer_buflen) {
69 if (ev->res > io_u->xfer_buflen)
70 io_u->error = -ev->res;
71 else
72 io_u->resid = io_u->xfer_buflen - ev->res;
73 } else
74 io_u->error = 0;
75
76 return io_u;
77}
78
79struct aio_ring {
80 unsigned id; /** kernel internal index number */
81 unsigned nr; /** number of io_events */
82 unsigned head;
83 unsigned tail;
84
85 unsigned magic;
86 unsigned compat_features;
87 unsigned incompat_features;
88 unsigned header_length; /** size of aio_ring */
89
90 struct io_event events[0];
91};
92
93#define AIO_RING_MAGIC 0xa10a10a1
94
95static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
96 struct io_event *events)
97{
98 long i = 0;
99 unsigned head;
100 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
101
102 while (i < max) {
103 head = ring->head;
104
105 if (head == ring->tail) {
106 /* There are no more completions */
107 break;
108 } else {
109 /* There is another completion to reap */
110 events[i] = ring->events[head];
111 read_barrier();
112 ring->head = (head + 1) % ring->nr;
113 i++;
114 }
115 }
116
117 return i;
118}
119
120static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
121 unsigned int max, struct timespec *t)
122{
123 struct libaio_data *ld = td->io_ops->data;
124 struct libaio_options *o = td->eo;
125 unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
126 int r, events = 0;
127
128 do {
129 if (o->userspace_reap == 1
130 && actual_min == 0
131 && ((struct aio_ring *)(ld->aio_ctx))->magic
132 == AIO_RING_MAGIC) {
133 r = user_io_getevents(ld->aio_ctx, max,
134 ld->aio_events + events);
135 } else {
136 r = io_getevents(ld->aio_ctx, actual_min,
137 max, ld->aio_events + events, t);
138 }
139 if (r >= 0)
140 events += r;
141 else if (r == -EAGAIN)
142 usleep(100);
143 } while (events < min);
144
145 return r < 0 ? r : events;
146}
147
148static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
149{
150 struct libaio_data *ld = td->io_ops->data;
151
152 fio_ro_check(td, io_u);
153
154 if (ld->iocbs_nr == (int) td->o.iodepth)
155 return FIO_Q_BUSY;
156
157 /*
158 * fsync is tricky, since it can fail and we need to do it
159 * serialized with other io. the reason is that linux doesn't
160 * support aio fsync yet. So return busy for the case where we
161 * have pending io, to let fio complete those first.
162 */
163 if (ddir_sync(io_u->ddir)) {
164 if (ld->iocbs_nr)
165 return FIO_Q_BUSY;
166
167 do_io_u_sync(td, io_u);
168 return FIO_Q_COMPLETED;
169 }
170
171 if (io_u->ddir == DDIR_TRIM) {
172 if (ld->iocbs_nr)
173 return FIO_Q_BUSY;
174
175 do_io_u_trim(td, io_u);
176 return FIO_Q_COMPLETED;
177 }
178
179 ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
180 ld->io_us[ld->iocbs_nr] = io_u;
181 ld->iocbs_nr++;
182 return FIO_Q_QUEUED;
183}
184
185static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
186 unsigned int nr)
187{
188 struct timeval now;
189 unsigned int i;
190
191 if (!fio_fill_issue_time(td))
192 return;
193
194 fio_gettime(&now, NULL);
195
196 for (i = 0; i < nr; i++) {
197 struct io_u *io_u = io_us[i];
198
199 memcpy(&io_u->issue_time, &now, sizeof(now));
200 io_u_queued(td, io_u);
201 }
202}
203
204static int fio_libaio_commit(struct thread_data *td)
205{
206 struct libaio_data *ld = td->io_ops->data;
207 struct iocb **iocbs;
208 struct io_u **io_us;
209 int ret;
210
211 if (!ld->iocbs_nr)
212 return 0;
213
214 io_us = ld->io_us;
215 iocbs = ld->iocbs;
216 do {
217 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
218 if (ret > 0) {
219 fio_libaio_queued(td, io_us, ret);
220 io_u_mark_submit(td, ret);
221 ld->iocbs_nr -= ret;
222 io_us += ret;
223 iocbs += ret;
224 ret = 0;
225 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
226 if (!ret)
227 io_u_mark_submit(td, ret);
228 continue;
229 } else
230 break;
231 } while (ld->iocbs_nr);
232
233 return ret;
234}
235
236static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
237{
238 struct libaio_data *ld = td->io_ops->data;
239
240 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
241}
242
243static void fio_libaio_cleanup(struct thread_data *td)
244{
245 struct libaio_data *ld = td->io_ops->data;
246
247 if (ld) {
248 io_destroy(ld->aio_ctx);
249 free(ld->aio_events);
250 free(ld->iocbs);
251 free(ld->io_us);
252 free(ld);
253 }
254}
255
256static int fio_libaio_init(struct thread_data *td)
257{
258 struct libaio_data *ld = malloc(sizeof(*ld));
259 int err;
260
261 memset(ld, 0, sizeof(*ld));
262
263 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
264 if (err) {
265 td_verror(td, -err, "io_queue_init");
266 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
267 free(ld);
268 return 1;
269 }
270
271 ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
272 memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
273 ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
274 memset(ld->iocbs, 0, sizeof(struct iocb *));
275 ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
276 memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
277 ld->iocbs_nr = 0;
278
279 td->io_ops->data = ld;
280 return 0;
281}
282
283static struct ioengine_ops ioengine = {
284 .name = "libaio",
285 .version = FIO_IOOPS_VERSION,
286 .init = fio_libaio_init,
287 .prep = fio_libaio_prep,
288 .queue = fio_libaio_queue,
289 .commit = fio_libaio_commit,
290 .cancel = fio_libaio_cancel,
291 .getevents = fio_libaio_getevents,
292 .event = fio_libaio_event,
293 .cleanup = fio_libaio_cleanup,
294 .open_file = generic_open_file,
295 .close_file = generic_close_file,
296 .get_file_size = generic_get_file_size,
297 .options = options,
298 .option_struct_size = sizeof(struct libaio_options),
299};
300
301#else /* FIO_HAVE_LIBAIO */
302
303/*
304 * When we have a proper configure system in place, we simply wont build
305 * and install this io engine. For now install a crippled version that
306 * just complains and fails to load.
307 */
308static int fio_libaio_init(struct thread_data fio_unused *td)
309{
310 log_err("fio: libaio not available\n");
311 return 1;
312}
313
314static struct ioengine_ops ioengine = {
315 .name = "libaio",
316 .version = FIO_IOOPS_VERSION,
317 .init = fio_libaio_init,
318};
319
320#endif
321
322static void fio_init fio_libaio_register(void)
323{
324 register_ioengine(&ioengine);
325}
326
327static void fio_exit fio_libaio_unregister(void)
328{
329 unregister_ioengine(&ioengine);
330}