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