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