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