Merge branch 'master' into gfio
[fio.git] / engines / libaio.c
1 /*
2  * libaio engine
3  *
4  * IO engine using the Linux native aio interface.
5  *
6  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <assert.h>
12
13 #include "../fio.h"
14
15 #ifdef FIO_HAVE_LIBAIO
16
17 #define ev_to_iou(ev)   (struct io_u *) ((unsigned long) (ev)->obj)
18
19 struct libaio_data {
20         io_context_t aio_ctx;
21         struct io_event *aio_events;
22         struct iocb **iocbs;
23         struct io_u **io_us;
24         int iocbs_nr;
25 };
26
27 struct libaio_options {
28         struct thread_data *td;
29         unsigned int userspace_reap;
30 };
31
32 static struct fio_option options[] = {
33         {
34                 .name   = "userspace_reap",
35                 .lname  = "Libaio userspace reaping",
36                 .type   = FIO_OPT_STR_SET,
37                 .off1   = offsetof(struct libaio_options, userspace_reap),
38                 .help   = "Use alternative user-space reap implementation",
39                 .category = FIO_OPT_C_IO,
40         },
41         {
42                 .name   = NULL,
43         },
44 };
45
46 static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u)
47 {
48         struct fio_file *f = io_u->file;
49
50         if (io_u->ddir == DDIR_READ)
51                 io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
52         else if (io_u->ddir == DDIR_WRITE)
53                 io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset);
54         else if (ddir_sync(io_u->ddir))
55                 io_prep_fsync(&io_u->iocb, f->fd);
56
57         return 0;
58 }
59
60 static struct io_u *fio_libaio_event(struct thread_data *td, int event)
61 {
62         struct libaio_data *ld = td->io_ops->data;
63         struct io_event *ev;
64         struct io_u *io_u;
65
66         ev = ld->aio_events + event;
67         io_u = ev_to_iou(ev);
68
69         if (ev->res != io_u->xfer_buflen) {
70                 if (ev->res > io_u->xfer_buflen)
71                         io_u->error = -ev->res;
72                 else
73                         io_u->resid = io_u->xfer_buflen - ev->res;
74         } else
75                 io_u->error = 0;
76
77         return io_u;
78 }
79
80 struct aio_ring {
81         unsigned id;             /** kernel internal index number */
82         unsigned nr;             /** number of io_events */
83         unsigned head;
84         unsigned tail;
85
86         unsigned magic;
87         unsigned compat_features;
88         unsigned incompat_features;
89         unsigned header_length; /** size of aio_ring */
90
91         struct io_event events[0];
92 };
93
94 #define AIO_RING_MAGIC  0xa10a10a1
95
96 static int user_io_getevents(io_context_t aio_ctx, unsigned int max,
97                              struct io_event *events)
98 {
99         long i = 0;
100         unsigned head;
101         struct aio_ring *ring = (struct aio_ring*) aio_ctx;
102
103         while (i < max) {
104                 head = ring->head;
105
106                 if (head == ring->tail) {
107                         /* There are no more completions */
108                         break;
109                 } else {
110                         /* There is another completion to reap */
111                         events[i] = ring->events[head];
112                         read_barrier();
113                         ring->head = (head + 1) % ring->nr;
114                         i++;
115                 }
116         }
117
118         return i;
119 }
120
121 static int fio_libaio_getevents(struct thread_data *td, unsigned int min,
122                                 unsigned int max, struct timespec *t)
123 {
124         struct libaio_data *ld = td->io_ops->data;
125         struct libaio_options *o = td->eo;
126         unsigned actual_min = td->o.iodepth_batch_complete == 0 ? 0 : min;
127         int r, events = 0;
128
129         do {
130                 if (o->userspace_reap == 1
131                     && actual_min == 0
132                     && ((struct aio_ring *)(ld->aio_ctx))->magic
133                                 == AIO_RING_MAGIC) {
134                         r = user_io_getevents(ld->aio_ctx, max,
135                                 ld->aio_events + events);
136                 } else {
137                         r = io_getevents(ld->aio_ctx, actual_min,
138                                 max, ld->aio_events + events, t);
139                 }
140                 if (r >= 0)
141                         events += r;
142                 else if (r == -EAGAIN)
143                         usleep(100);
144         } while (events < min);
145
146         return r < 0 ? r : events;
147 }
148
149 static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
150 {
151         struct libaio_data *ld = td->io_ops->data;
152
153         fio_ro_check(td, io_u);
154
155         if (ld->iocbs_nr == (int) td->o.iodepth)
156                 return FIO_Q_BUSY;
157
158         /*
159          * fsync is tricky, since it can fail and we need to do it
160          * serialized with other io. the reason is that linux doesn't
161          * support aio fsync yet. So return busy for the case where we
162          * have pending io, to let fio complete those first.
163          */
164         if (ddir_sync(io_u->ddir)) {
165                 if (ld->iocbs_nr)
166                         return FIO_Q_BUSY;
167
168                 do_io_u_sync(td, io_u);
169                 return FIO_Q_COMPLETED;
170         }
171
172         if (io_u->ddir == DDIR_TRIM) {
173                 if (ld->iocbs_nr)
174                         return FIO_Q_BUSY;
175
176                 do_io_u_trim(td, io_u);
177                 return FIO_Q_COMPLETED;
178         }
179
180         ld->iocbs[ld->iocbs_nr] = &io_u->iocb;
181         ld->io_us[ld->iocbs_nr] = io_u;
182         ld->iocbs_nr++;
183         return FIO_Q_QUEUED;
184 }
185
186 static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us,
187                               unsigned int nr)
188 {
189         struct timeval now;
190         unsigned int i;
191
192         if (!fio_fill_issue_time(td))
193                 return;
194
195         fio_gettime(&now, NULL);
196
197         for (i = 0; i < nr; i++) {
198                 struct io_u *io_u = io_us[i];
199
200                 memcpy(&io_u->issue_time, &now, sizeof(now));
201                 io_u_queued(td, io_u);
202         }
203 }
204
205 static int fio_libaio_commit(struct thread_data *td)
206 {
207         struct libaio_data *ld = td->io_ops->data;
208         struct iocb **iocbs;
209         struct io_u **io_us;
210         int ret;
211
212         if (!ld->iocbs_nr)
213                 return 0;
214
215         io_us = ld->io_us;
216         iocbs = ld->iocbs;
217         do {
218                 ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs);
219                 if (ret > 0) {
220                         fio_libaio_queued(td, io_us, ret);
221                         io_u_mark_submit(td, ret);
222                         ld->iocbs_nr -= ret;
223                         io_us += ret;
224                         iocbs += ret;
225                         ret = 0;
226                 } else if (!ret || ret == -EAGAIN || ret == -EINTR) {
227                         if (!ret)
228                                 io_u_mark_submit(td, ret);
229                         continue;
230                 } else
231                         break;
232         } while (ld->iocbs_nr);
233
234         return ret;
235 }
236
237 static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
238 {
239         struct libaio_data *ld = td->io_ops->data;
240
241         return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
242 }
243
244 static void fio_libaio_cleanup(struct thread_data *td)
245 {
246         struct libaio_data *ld = td->io_ops->data;
247
248         if (ld) {
249                 io_destroy(ld->aio_ctx);
250                 free(ld->aio_events);
251                 free(ld->iocbs);
252                 free(ld->io_us);
253                 free(ld);
254         }
255 }
256
257 static int fio_libaio_init(struct thread_data *td)
258 {
259         struct libaio_data *ld = malloc(sizeof(*ld));
260         struct libaio_options *o = td->eo;
261         int err = 0;
262
263         memset(ld, 0, sizeof(*ld));
264
265         /*
266          * First try passing in 0 for queue depth, since we don't
267          * care about the user ring. If that fails, the kernel is too old
268          * and we need the right depth.
269          */
270         if (!o->userspace_reap)
271                 err = io_queue_init(INT_MAX, &ld->aio_ctx);
272         if (o->userspace_reap || err == -EINVAL)
273                 err = io_queue_init(td->o.iodepth, &ld->aio_ctx);
274         if (err) {
275                 td_verror(td, -err, "io_queue_init");
276                 log_err("fio: check /proc/sys/fs/aio-max-nr\n");
277                 free(ld);
278                 return 1;
279         }
280
281         ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event));
282         memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event));
283         ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *));
284         memset(ld->iocbs, 0, sizeof(struct iocb *));
285         ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *));
286         memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *));
287         ld->iocbs_nr = 0;
288
289         td->io_ops->data = ld;
290         return 0;
291 }
292
293 static struct ioengine_ops ioengine = {
294         .name                   = "libaio",
295         .version                = FIO_IOOPS_VERSION,
296         .init                   = fio_libaio_init,
297         .prep                   = fio_libaio_prep,
298         .queue                  = fio_libaio_queue,
299         .commit                 = fio_libaio_commit,
300         .cancel                 = fio_libaio_cancel,
301         .getevents              = fio_libaio_getevents,
302         .event                  = fio_libaio_event,
303         .cleanup                = fio_libaio_cleanup,
304         .open_file              = generic_open_file,
305         .close_file             = generic_close_file,
306         .get_file_size          = generic_get_file_size,
307         .options                = options,
308         .option_struct_size     = sizeof(struct libaio_options),
309 };
310
311 #else /* FIO_HAVE_LIBAIO */
312
313 /*
314  * When we have a proper configure system in place, we simply wont build
315  * and install this io engine. For now install a crippled version that
316  * just complains and fails to load.
317  */
318 static int fio_libaio_init(struct thread_data fio_unused *td)
319 {
320         log_err("fio: libaio not available\n");
321         return 1;
322 }
323
324 static struct ioengine_ops ioengine = {
325         .name           = "libaio",
326         .version        = FIO_IOOPS_VERSION,
327         .init           = fio_libaio_init,
328 };
329
330 #endif
331
332 static void fio_init fio_libaio_register(void)
333 {
334         register_ioengine(&ioengine);
335 }
336
337 static void fio_exit fio_libaio_unregister(void)
338 {
339         unregister_ioengine(&ioengine);
340 }