[PATCH] Remember to check ioops version in loaded module
[fio.git] / engines / fio-engine-libaio.c
CommitLineData
2866c82d
JA
1/*
2 * native linux aio io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10#include "fio.h"
11#include "os.h"
12
13#define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj)
14
15struct libaio_data {
16 io_context_t aio_ctx;
17 struct io_event *aio_events;
18};
19
20static int fio_libaio_sync(struct thread_data *td)
21{
22 return fsync(td->fd);
23}
24
25static int fio_libaio_prep(struct thread_data *td, struct io_u *io_u)
26{
27 if (io_u->ddir == DDIR_READ)
28 io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
29 else
30 io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset);
31
32 return 0;
33}
34
35static struct io_u *fio_libaio_event(struct thread_data *td, int event)
36{
37 struct libaio_data *ld = td->io_ops->data;
38
39 return ev_to_iou(ld->aio_events + event);
40}
41
42static int fio_libaio_getevents(struct thread_data *td, int min, int max,
43 struct timespec *t)
44{
45 struct libaio_data *ld = td->io_ops->data;
46 long r;
47
48 do {
49 r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t);
50 if (r == -EAGAIN) {
51 usleep(100);
52 continue;
53 } else if (r == -EINTR)
54 continue;
55 else
56 break;
57 } while (1);
58
59 return (int) r;
60}
61
62static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u)
63{
64 struct libaio_data *ld = td->io_ops->data;
65 struct iocb *iocb = &io_u->iocb;
66 long ret;
67
68 do {
69 ret = io_submit(ld->aio_ctx, 1, &iocb);
70 if (ret == 1)
71 return 0;
72 else if (ret == -EAGAIN)
73 usleep(100);
74 else if (ret == -EINTR)
75 continue;
76 else
77 break;
78 } while (1);
79
80 return (int) ret;
81
82}
83
84static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u)
85{
86 struct libaio_data *ld = td->io_ops->data;
87
88 return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events);
89}
90
91static void fio_libaio_cleanup(struct thread_data *td)
92{
93 struct libaio_data *ld = td->io_ops->data;
94
95 if (ld) {
96 io_destroy(ld->aio_ctx);
97 if (ld->aio_events)
98 free(ld->aio_events);
99
100 free(ld);
101 td->io_ops->data = NULL;
102 }
103}
104
105static int fio_libaio_init(struct thread_data *td)
106{
107 struct libaio_data *ld = malloc(sizeof(*ld));
108
109 memset(ld, 0, sizeof(*ld));
110 if (io_queue_init(td->iodepth, &ld->aio_ctx)) {
111 td_verror(td, errno);
112 return 1;
113 }
114
115 ld->aio_events = malloc(td->iodepth * sizeof(struct io_event));
116 td->io_ops->data = ld;
117 return 0;
118}
119
120struct ioengine_ops ioengine = {
121 .name = "libaio",
122 .version = FIO_IOOPS_VERSION,
123 .init = fio_libaio_init,
124 .prep = fio_libaio_prep,
125 .queue = fio_libaio_queue,
126 .cancel = fio_libaio_cancel,
127 .getevents = fio_libaio_getevents,
128 .event = fio_libaio_event,
129 .cleanup = fio_libaio_cleanup,
130 .sync = fio_libaio_sync,
131};