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