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