Merge branch 'dev' of https://github.com/smartxworks/fio
[fio.git] / engines / libaio.c
... / ...
CommitLineData
1/*
2 * libaio engine
3 *
4 * IO engine using the Linux native aio interface.
5 *
6 */
7#include <stdlib.h>
8#include <unistd.h>
9#include <errno.h>
10#include <libaio.h>
11#include <sys/time.h>
12#include <sys/resource.h>
13
14#include "../fio.h"
15#include "../lib/pow2.h"
16#include "../optgroup.h"
17#include "../lib/memalign.h"
18
19#ifndef IOCB_FLAG_HIPRI
20#define IOCB_FLAG_HIPRI (1 << 2)
21#endif
22
23#ifndef IOCTX_FLAG_IOPOLL
24#define IOCTX_FLAG_IOPOLL (1 << 0)
25#endif
26
27static int fio_libaio_commit(struct thread_data *td);
28
29struct libaio_data {
30 io_context_t aio_ctx;
31 struct io_event *aio_events;
32 struct iocb **iocbs;
33 struct io_u **io_us;
34
35 struct io_u **io_u_index;
36
37 /*
38 * Basic ring buffer. 'head' is incremented in _queue(), and
39 * 'tail' is incremented in _commit(). We keep 'queued' so
40 * that we know if the ring is full or empty, when
41 * 'head' == 'tail'. 'entries' is the ring size, and
42 * 'is_pow2' is just an optimization to use AND instead of
43 * modulus to get the remainder on ring increment.
44 */
45 int is_pow2;
46 unsigned int entries;
47 unsigned int queued;
48 unsigned int head;
49 unsigned int tail;
50};
51
52struct libaio_options {
53 void *pad;
54 unsigned int userspace_reap;
55 unsigned int hipri;
56};
57
58static struct fio_option options[] = {
59 {
60 .name = "userspace_reap",
61 .lname = "Libaio userspace reaping",
62 .type = FIO_OPT_STR_SET,
63 .off1 = offsetof(struct libaio_options, userspace_reap),
64 .help = "Use alternative user-space reap implementation",
65 .category = FIO_OPT_C_ENGINE,
66 .group = FIO_OPT_G_LIBAIO,
67 },
68 {
69 .name = "hipri",
70 .lname = "High Priority",
71 .type = FIO_OPT_STR_SET,
72 .off1 = offsetof(struct libaio_options, hipri),
73 .help = "Use polled IO completions",
74 .category = FIO_OPT_C_ENGINE,
75 .group = FIO_OPT_G_LIBAIO,
76 },
77 {
78 .name = NULL,
79 },
80};
81
82static inline void ring_inc(struct libaio_data *ld, unsigned int *val,
83 unsigned int add)
84{
85 if (ld->is_pow2)
86 *val = (*val + add) & (ld->entries - 1);
87 else
88 *val = (*val + add) % ld->entries;
89}
90
91static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
92{
93 struct fio_file *f = io_u->file;
94 struct libaio_options *o = td->eo;
95 struct iocb *iocb;
96
97 iocb = &io_u->iocb;
98
99 if (io_u->ddir == DDIR_READ) {
100 io_prep_pread(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
101 if (o->hipri)
102 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
103 } else if (io_u->ddir == DDIR_WRITE) {
104 io_prep_pwrite(iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
105 if (o->hipri)
106 iocb->u.c.flags |= IOCB_FLAG_HIPRI;
107 } else if (ddir_sync(io_u->ddir))
108 io_prep_fsync(iocb, f->fd);
109
110 return 0;
111}
112
113static struct io_u *fio_libaio_event(struct thread_data *td, int event)
114{
115 struct libaio_data *ld = td->io_ops_data;
116 struct io_event *ev;
117 struct io_u *io_u;
118
119 ev = ld->aio_events + event;
120 io_u = container_of(ev->obj, struct io_u, iocb);
121
122 if (ev->res != io_u->xfer_buflen) {
123 if (ev->res > io_u->xfer_buflen)
124 io_u->error = -ev->res;
125 else
126 io_u->resid = io_u->xfer_buflen - ev->res;
127 } else
128 io_u->error = 0;
129
130 return io_u;
131}
132
133struct aio_ring {
134 unsigned id; /** kernel internal index number */
135 unsigned nr; /** number of io_events */
136 unsigned head;
137 unsigned tail;
138
139 unsigned magic;
140 unsigned compat_features;
141 unsigned incompat_features;
142 unsigned header_length; /** size of aio_ring */
143
144 struct io_event events[0];
145};
146
147#define AIO_RING_MAGIC 0xa10a10a1
148
149static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
150 struct io_event *events)
151{
152 long i = 0;
153 unsigned head;
154 struct aio_ring *ring = (struct aio_ring*) aio_ctx;
155
156 while (i < max) {
157 head = ring->head;
158
159 if (head == ring->tail) {
160 /* There are no more completions */
161 break;
162 } else {
163 /* There is another completion to reap */
164 events[i] = ring->events[head];
165 read_barrier();
166 ring->head = (head + 1) % ring->nr;
167 i++;
168 }
169 }
170
171 return i;
172}
173
174static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
175 unsigned int max, const struct timespec *t)
176{
177 struct libaio_data *ld = td->io_ops_data;
178 struct libaio_options *o = td->eo;
179 unsigned actual_min = td->o.iodepth_batch_complete_min == 0 ? 0 : min;
180 struct timespec __lt, *lt = NULL;
181 int r, events = 0;
182
183 if (t) {
184 __lt = *t;
185 lt = &__lt;
186 }
187
188 do {
189 if (o->userspace_reap == 1
190 && actual_min == 0
191 && ((struct aio_ring *)(ld->aio_ctx))->magic
192 == AIO_RING_MAGIC) {
193 r = user_io_getevents(ld->aio_ctx, max,
194 ld->aio_events + events);
195 } else {
196 r = io_getevents(ld->aio_ctx, actual_min,
197 max, ld->aio_events + events, lt);
198 }
199 if (r > 0)
200 events += r;
201 else if ((min && r == 0) || r == -EAGAIN) {
202 fio_libaio_commit(td);
203 if (actual_min)
204 usleep(10);
205 } else if (r != -EINTR)
206 break;
207 } while (events < min);
208
209 return r < 0 ? r : events;
210}
211
212static enum fio_q_status fio_libaio_queue(struct thread_data *td,
213 struct io_u *io_u)
214{
215 struct libaio_data *ld = td->io_ops_data;
216
217 fio_ro_check(td, io_u);
218
219 if (ld->queued == td->o.iodepth)
220 return FIO_Q_BUSY;
221
222 /*
223 * fsync is tricky, since it can fail and we need to do it
224 * serialized with other io. the reason is that linux doesn't
225 * support aio fsync yet. So return busy for the case where we
226 * have pending io, to let fio complete those first.
227 */
228 if (ddir_sync(io_u->ddir)) {
229 if (ld->queued)
230 return FIO_Q_BUSY;
231
232 do_io_u_sync(td, io_u);
233 return FIO_Q_COMPLETED;
234 }
235
236 if (io_u->ddir == DDIR_TRIM) {
237 if (ld->queued)
238 return FIO_Q_BUSY;
239
240 do_io_u_trim(td, io_u);
241 io_u_mark_submit(td, 1);
242 io_u_mark_complete(td, 1);
243 return FIO_Q_COMPLETED;
244 }
245
246 ld->iocbs[ld->head] = &io_u->iocb;
247 ld->io_us[ld->head] = io_u;
248 ring_inc(ld, &ld->head, 1);
249 ld->queued++;
250 return FIO_Q_QUEUED;
251}
252
253static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
254 unsigned int nr)
255{
256 struct timespec now;
257 unsigned int i;
258
259 if (!fio_fill_issue_time(td))
260 return;
261
262 fio_gettime(&now, NULL);
263
264 for (i = 0; i < nr; i++) {
265 struct io_u *io_u = io_us[i];
266
267 memcpy(&io_u->issue_time, &now, sizeof(now));
268 io_u_queued(td, io_u);
269 }
270}
271
272static int fio_libaio_commit(struct thread_data *td)
273{
274 struct libaio_data *ld = td->io_ops_data;
275 struct iocb **iocbs;
276 struct io_u **io_us;
277 struct timespec ts;
278 int ret, wait_start = 0;
279
280 if (!ld->queued)
281 return 0;
282
283 do {
284 long nr = ld->queued;
285
286 nr = min((unsigned int) nr, ld->entries - ld->tail);
287 io_us = ld->io_us + ld->tail;
288 iocbs = ld->iocbs + ld->tail;
289
290 ret = io_submit(ld->aio_ctx, nr, iocbs);
291 if (ret > 0) {
292 fio_libaio_queued(td, io_us, ret);
293 io_u_mark_submit(td, ret);
294
295 ld->queued -= ret;
296 ring_inc(ld, &ld->tail, ret);
297 ret = 0;
298 wait_start = 0;
299 } else if (ret == -EINTR || !ret) {
300 if (!ret)
301 io_u_mark_submit(td, ret);
302 wait_start = 0;
303 continue;
304 } else if (ret == -EAGAIN) {
305 /*
306 * If we get EAGAIN, we should break out without
307 * error and let the upper layer reap some
308 * events for us. If we have no queued IO, we
309 * must loop here. If we loop for more than 30s,
310 * just error out, something must be buggy in the
311 * IO path.
312 */
313 if (ld->queued) {
314 ret = 0;
315 break;
316 }
317 if (!wait_start) {
318 fio_gettime(&ts, NULL);
319 wait_start = 1;
320 } else if (mtime_since_now(&ts) > 30000) {
321 log_err("fio: aio appears to be stalled, giving up\n");
322 break;
323 }
324 usleep(1);
325 continue;
326 } else if (ret == -ENOMEM) {
327 /*
328 * If we get -ENOMEM, reap events if we can. If
329 * we cannot, treat it as a fatal event since there's
330 * nothing we can do about it.
331 */
332 if (ld->queued)
333 ret = 0;
334 break;
335 } else
336 break;
337 } while (ld->queued);
338
339 return ret;
340}
341
342static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
343{
344 struct libaio_data *ld = td->io_ops_data;
345
346 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
347}
348
349static void fio_libaio_cleanup(struct thread_data *td)
350{
351 struct libaio_data *ld = td->io_ops_data;
352
353 if (ld) {
354 /*
355 * Work-around to avoid huge RCU stalls at exit time. If we
356 * don't do this here, then it'll be torn down by exit_aio().
357 * But for that case we can parallellize the freeing, thus
358 * speeding it up a lot.
359 */
360 if (!(td->flags & TD_F_CHILD))
361 io_destroy(ld->aio_ctx);
362 free(ld->aio_events);
363 free(ld->iocbs);
364 free(ld->io_us);
365 free(ld);
366 }
367}
368
369static int fio_libaio_old_queue_init(struct libaio_data *ld, unsigned int depth,
370 bool hipri)
371{
372 if (hipri) {
373 log_err("fio: polled aio not available on your platform\n");
374 return 1;
375 }
376
377 return io_queue_init(depth, &ld->aio_ctx);
378}
379
380static int fio_libaio_queue_init(struct libaio_data *ld, unsigned int depth,
381 bool hipri)
382{
383#ifdef __NR_sys_io_setup2
384 int ret, flags = 0;
385
386 if (hipri)
387 flags |= IOCTX_FLAG_IOPOLL;
388
389 ret = syscall(__NR_sys_io_setup2, depth, flags, NULL, NULL,
390 &ld->aio_ctx);
391 if (!ret)
392 return 0;
393 /* fall through to old syscall */
394#endif
395 return fio_libaio_old_queue_init(ld, depth, hipri);
396}
397
398static int fio_libaio_post_init(struct thread_data *td)
399{
400 struct libaio_data *ld = td->io_ops_data;
401 struct libaio_options *o = td->eo;
402 int err = 0;
403
404 err = fio_libaio_queue_init(ld, td->o.iodepth, o->hipri);
405 if (err) {
406 td_verror(td, -err, "io_queue_init");
407 return 1;
408 }
409
410 return 0;
411}
412
413static int fio_libaio_init(struct thread_data *td)
414{
415 struct libaio_data *ld;
416
417 ld = calloc(1, sizeof(*ld));
418
419 ld->entries = td->o.iodepth;
420 ld->is_pow2 = is_power_of_2(ld->entries);
421 ld->aio_events = calloc(ld->entries, sizeof(struct io_event));
422 ld->iocbs = calloc(ld->entries, sizeof(struct iocb *));
423 ld->io_us = calloc(ld->entries, sizeof(struct io_u *));
424
425 td->io_ops_data = ld;
426 return 0;
427}
428
429static struct ioengine_ops ioengine = {
430 .name = "libaio",
431 .version = FIO_IOOPS_VERSION,
432 .flags = FIO_ASYNCIO_SYNC_TRIM,
433 .init = fio_libaio_init,
434 .post_init = fio_libaio_post_init,
435 .prep = fio_libaio_prep,
436 .queue = fio_libaio_queue,
437 .commit = fio_libaio_commit,
438 .cancel = fio_libaio_cancel,
439 .getevents = fio_libaio_getevents,
440 .event = fio_libaio_event,
441 .cleanup = fio_libaio_cleanup,
442 .open_file = generic_open_file,
443 .close_file = generic_close_file,
444 .get_file_size = generic_get_file_size,
445 .options = options,
446 .option_struct_size = sizeof(struct libaio_options),
447};
448
449static void fio_init fio_libaio_register(void)
450{
451 register_ioengine(&ioengine);
452}
453
454static void fio_exit fio_libaio_unregister(void)
455{
456 unregister_ioengine(&ioengine);
457}