Commit | Line | Data |
---|---|---|
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> | |
5f350952 JA |
10 | |
11 | #include "../fio.h" | |
12 | #include "../os.h" | |
2866c82d | 13 | |
34cfcdaf JA |
14 | #ifdef FIO_HAVE_LIBAIO |
15 | ||
2866c82d JA |
16 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
17 | ||
18 | struct libaio_data { | |
19 | io_context_t aio_ctx; | |
20 | struct io_event *aio_events; | |
21 | }; | |
22 | ||
7a16dd02 | 23 | static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
2866c82d | 24 | { |
53cdc686 JA |
25 | struct fio_file *f = io_u->file; |
26 | ||
2866c82d | 27 | if (io_u->ddir == DDIR_READ) |
53cdc686 | 28 | io_prep_pread(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset); |
87dc1ab1 | 29 | else if (io_u->ddir == DDIR_WRITE) |
53cdc686 | 30 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->buf, io_u->buflen, io_u->offset); |
87dc1ab1 JA |
31 | else if (io_u->ddir == DDIR_SYNC) |
32 | io_prep_fsync(&io_u->iocb, f->fd); | |
33 | else | |
34 | return 1; | |
2866c82d JA |
35 | |
36 | return 0; | |
37 | } | |
38 | ||
39 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) | |
40 | { | |
41 | struct libaio_data *ld = td->io_ops->data; | |
42 | ||
43 | return ev_to_iou(ld->aio_events + event); | |
44 | } | |
45 | ||
46 | static int fio_libaio_getevents(struct thread_data *td, int min, int max, | |
47 | struct timespec *t) | |
48 | { | |
49 | struct libaio_data *ld = td->io_ops->data; | |
50 | long r; | |
51 | ||
52 | do { | |
53 | r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t); | |
54 | if (r == -EAGAIN) { | |
55 | usleep(100); | |
56 | continue; | |
57 | } else if (r == -EINTR) | |
58 | continue; | |
84585003 | 59 | else if (r != 0) |
2866c82d JA |
60 | break; |
61 | } while (1); | |
62 | ||
eaf09db4 JA |
63 | if (r < 0) |
64 | r = -r; | |
65 | ||
66 | return (int) r; | |
2866c82d JA |
67 | } |
68 | ||
69 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) | |
70 | { | |
71 | struct libaio_data *ld = td->io_ops->data; | |
72 | struct iocb *iocb = &io_u->iocb; | |
73 | long ret; | |
74 | ||
75 | do { | |
76 | ret = io_submit(ld->aio_ctx, 1, &iocb); | |
77 | if (ret == 1) | |
78 | return 0; | |
84585003 | 79 | else if (ret == -EAGAIN || !ret) |
2866c82d JA |
80 | usleep(100); |
81 | else if (ret == -EINTR) | |
82 | continue; | |
83 | else | |
84 | break; | |
85 | } while (1); | |
86 | ||
353a7e0e JA |
87 | if (ret <= 0) { |
88 | io_u->resid = io_u->buflen; | |
89 | io_u->error = -ret; | |
90 | return 1; | |
91 | } | |
2866c82d | 92 | |
353a7e0e | 93 | return 0; |
2866c82d JA |
94 | } |
95 | ||
96 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) | |
97 | { | |
98 | struct libaio_data *ld = td->io_ops->data; | |
99 | ||
100 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); | |
101 | } | |
102 | ||
103 | static void fio_libaio_cleanup(struct thread_data *td) | |
104 | { | |
105 | struct libaio_data *ld = td->io_ops->data; | |
106 | ||
107 | if (ld) { | |
108 | io_destroy(ld->aio_ctx); | |
109 | if (ld->aio_events) | |
110 | free(ld->aio_events); | |
111 | ||
112 | free(ld); | |
113 | td->io_ops->data = NULL; | |
114 | } | |
115 | } | |
116 | ||
117 | static int fio_libaio_init(struct thread_data *td) | |
118 | { | |
119 | struct libaio_data *ld = malloc(sizeof(*ld)); | |
120 | ||
121 | memset(ld, 0, sizeof(*ld)); | |
122 | if (io_queue_init(td->iodepth, &ld->aio_ctx)) { | |
123 | td_verror(td, errno); | |
cb781c75 | 124 | free(ld); |
2866c82d JA |
125 | return 1; |
126 | } | |
127 | ||
128 | ld->aio_events = malloc(td->iodepth * sizeof(struct io_event)); | |
84585003 | 129 | memset(ld->aio_events, 0, td->iodepth * sizeof(struct io_event)); |
2866c82d JA |
130 | td->io_ops->data = ld; |
131 | return 0; | |
132 | } | |
133 | ||
5f350952 | 134 | static struct ioengine_ops ioengine = { |
2866c82d JA |
135 | .name = "libaio", |
136 | .version = FIO_IOOPS_VERSION, | |
137 | .init = fio_libaio_init, | |
138 | .prep = fio_libaio_prep, | |
139 | .queue = fio_libaio_queue, | |
140 | .cancel = fio_libaio_cancel, | |
141 | .getevents = fio_libaio_getevents, | |
142 | .event = fio_libaio_event, | |
143 | .cleanup = fio_libaio_cleanup, | |
2866c82d | 144 | }; |
34cfcdaf JA |
145 | |
146 | #else /* FIO_HAVE_LIBAIO */ | |
147 | ||
148 | /* | |
149 | * When we have a proper configure system in place, we simply wont build | |
150 | * and install this io engine. For now install a crippled version that | |
151 | * just complains and fails to load. | |
152 | */ | |
153 | static int fio_libaio_init(struct thread_data fio_unused *td) | |
154 | { | |
155 | fprintf(stderr, "fio: libaio not available\n"); | |
156 | return 1; | |
157 | } | |
158 | ||
5f350952 | 159 | static struct ioengine_ops ioengine = { |
34cfcdaf JA |
160 | .name = "libaio", |
161 | .version = FIO_IOOPS_VERSION, | |
162 | .init = fio_libaio_init, | |
163 | }; | |
164 | ||
165 | #endif | |
5f350952 JA |
166 | |
167 | static void fio_init fio_libaio_register(void) | |
168 | { | |
169 | register_ioengine(&ioengine); | |
170 | } | |
171 | ||
172 | static void fio_exit fio_libaio_unregister(void) | |
173 | { | |
174 | unregister_ioengine(&ioengine); | |
175 | } |