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