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