Commit | Line | Data |
---|---|---|
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 |
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; | |
755200a3 | 22 | struct iocb **iocbs; |
7e77dd02 | 23 | struct io_u **io_us; |
755200a3 | 24 | int iocbs_nr; |
2866c82d JA |
25 | }; |
26 | ||
7a16dd02 | 27 | static int fio_libaio_prep(struct thread_data fio_unused *td, struct io_u *io_u) |
2866c82d | 28 | { |
53cdc686 JA |
29 | struct fio_file *f = io_u->file; |
30 | ||
2866c82d | 31 | if (io_u->ddir == DDIR_READ) |
cec6b55d | 32 | io_prep_pread(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
87dc1ab1 | 33 | else if (io_u->ddir == DDIR_WRITE) |
cec6b55d | 34 | io_prep_pwrite(&io_u->iocb, f->fd, io_u->xfer_buf, io_u->xfer_buflen, io_u->offset); |
87dc1ab1 JA |
35 | else if (io_u->ddir == DDIR_SYNC) |
36 | io_prep_fsync(&io_u->iocb, f->fd); | |
37 | else | |
38 | return 1; | |
2866c82d JA |
39 | |
40 | return 0; | |
41 | } | |
42 | ||
43 | static struct io_u *fio_libaio_event(struct thread_data *td, int event) | |
44 | { | |
45 | struct libaio_data *ld = td->io_ops->data; | |
f423479d JA |
46 | struct io_event *ev; |
47 | struct io_u *io_u; | |
2866c82d | 48 | |
f423479d JA |
49 | ev = ld->aio_events + event; |
50 | io_u = ev_to_iou(ev); | |
51 | ||
52 | if (ev->res != io_u->xfer_buflen) { | |
53 | if (ev->res > io_u->xfer_buflen) | |
54 | io_u->error = -ev->res; | |
55 | else | |
56 | io_u->resid = io_u->xfer_buflen - ev->res; | |
57 | } else | |
58 | io_u->error = 0; | |
59 | ||
60 | return io_u; | |
2866c82d JA |
61 | } |
62 | ||
63 | static int fio_libaio_getevents(struct thread_data *td, int min, int max, | |
64 | struct timespec *t) | |
65 | { | |
66 | struct libaio_data *ld = td->io_ops->data; | |
67 | long r; | |
68 | ||
69 | do { | |
70 | r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t); | |
ccbb91cb JA |
71 | if (r >= min) |
72 | break; | |
73 | else if (r == -EAGAIN) { | |
2866c82d JA |
74 | usleep(100); |
75 | continue; | |
76 | } else if (r == -EINTR) | |
77 | continue; | |
84585003 | 78 | else if (r != 0) |
2866c82d JA |
79 | break; |
80 | } while (1); | |
81 | ||
22819ec2 | 82 | return r; |
2866c82d JA |
83 | } |
84 | ||
85 | static int fio_libaio_queue(struct thread_data *td, struct io_u *io_u) | |
86 | { | |
87 | struct libaio_data *ld = td->io_ops->data; | |
2866c82d | 88 | |
2dc1bbeb | 89 | if (ld->iocbs_nr == (int) td->o.iodepth) |
755200a3 JA |
90 | return FIO_Q_BUSY; |
91 | ||
92 | /* | |
93 | * fsync is tricky, since it can fail and we need to do it | |
94 | * serialized with other io. the reason is that linux doesn't | |
95 | * support aio fsync yet. So return busy for the case where we | |
96 | * have pending io, to let fio complete those first. | |
97 | */ | |
98 | if (io_u->ddir == DDIR_SYNC) { | |
99 | if (ld->iocbs_nr) | |
100 | return FIO_Q_BUSY; | |
101 | if (fsync(io_u->file->fd) < 0) | |
102 | io_u->error = errno; | |
103 | ||
104 | return FIO_Q_COMPLETED; | |
105 | } | |
106 | ||
107 | ld->iocbs[ld->iocbs_nr] = &io_u->iocb; | |
7e77dd02 | 108 | ld->io_us[ld->iocbs_nr] = io_u; |
755200a3 JA |
109 | ld->iocbs_nr++; |
110 | return FIO_Q_QUEUED; | |
111 | } | |
112 | ||
7e77dd02 JA |
113 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
114 | unsigned int nr) | |
115 | { | |
116 | struct timeval now; | |
117 | unsigned int i; | |
118 | ||
119 | fio_gettime(&now, NULL); | |
120 | ||
121 | for (i = 0; i < nr; i++) { | |
122 | struct io_u *io_u = io_us[i]; | |
123 | ||
124 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
125 | io_u_queued(td, io_u); | |
126 | } | |
127 | } | |
128 | ||
755200a3 JA |
129 | static int fio_libaio_commit(struct thread_data *td) |
130 | { | |
131 | struct libaio_data *ld = td->io_ops->data; | |
132 | struct iocb **iocbs; | |
7e77dd02 | 133 | struct io_u **io_us; |
755200a3 JA |
134 | int ret, iocbs_nr; |
135 | ||
136 | if (!ld->iocbs_nr) | |
137 | return 0; | |
138 | ||
139 | iocbs_nr = ld->iocbs_nr; | |
7e77dd02 | 140 | io_us = ld->io_us; |
755200a3 | 141 | iocbs = ld->iocbs; |
2866c82d | 142 | do { |
755200a3 JA |
143 | ret = io_submit(ld->aio_ctx, iocbs_nr, iocbs); |
144 | if (ret == iocbs_nr) { | |
7e77dd02 | 145 | fio_libaio_queued(td, io_us, ret); |
755200a3 JA |
146 | ret = 0; |
147 | break; | |
148 | } else if (ret > 0) { | |
7e77dd02 JA |
149 | fio_libaio_queued(td, io_us, ret); |
150 | io_us += ret; | |
755200a3 JA |
151 | iocbs += ret; |
152 | iocbs_nr -= ret; | |
153 | continue; | |
154 | } else if (ret == -EAGAIN || !ret) | |
2866c82d JA |
155 | usleep(100); |
156 | else if (ret == -EINTR) | |
157 | continue; | |
755200a3 | 158 | else |
2866c82d JA |
159 | break; |
160 | } while (1); | |
161 | ||
755200a3 JA |
162 | if (!ret) |
163 | ld->iocbs_nr = 0; | |
2866c82d | 164 | |
36167d82 | 165 | return ret; |
2866c82d JA |
166 | } |
167 | ||
168 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) | |
169 | { | |
170 | struct libaio_data *ld = td->io_ops->data; | |
171 | ||
172 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); | |
173 | } | |
174 | ||
175 | static void fio_libaio_cleanup(struct thread_data *td) | |
176 | { | |
177 | struct libaio_data *ld = td->io_ops->data; | |
178 | ||
179 | if (ld) { | |
180 | io_destroy(ld->aio_ctx); | |
7e77dd02 JA |
181 | free(ld->aio_events); |
182 | free(ld->iocbs); | |
183 | free(ld->io_us); | |
2866c82d JA |
184 | free(ld); |
185 | td->io_ops->data = NULL; | |
186 | } | |
187 | } | |
188 | ||
189 | static int fio_libaio_init(struct thread_data *td) | |
190 | { | |
191 | struct libaio_data *ld = malloc(sizeof(*ld)); | |
7d059f88 | 192 | static int warn_print; |
c1db2dce | 193 | int err; |
2866c82d | 194 | |
7d059f88 JA |
195 | if (td->o.iodepth > 1 && !td->o.odirect && !warn_print) { |
196 | log_info("fio: libaio engine is only async for non-buffered IO\n"); | |
197 | warn_print = 1; | |
198 | } | |
199 | ||
2866c82d | 200 | memset(ld, 0, sizeof(*ld)); |
c1db2dce JA |
201 | |
202 | err = io_queue_init(td->o.iodepth, &ld->aio_ctx); | |
203 | if (err) { | |
204 | td_verror(td, -err, "io_queue_init"); | |
cb781c75 | 205 | free(ld); |
2866c82d JA |
206 | return 1; |
207 | } | |
208 | ||
2dc1bbeb JA |
209 | ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event)); |
210 | memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event)); | |
211 | ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *)); | |
755200a3 | 212 | memset(ld->iocbs, 0, sizeof(struct iocb *)); |
2dc1bbeb JA |
213 | ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
214 | memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); | |
755200a3 JA |
215 | ld->iocbs_nr = 0; |
216 | ||
2866c82d JA |
217 | td->io_ops->data = ld; |
218 | return 0; | |
219 | } | |
220 | ||
5f350952 | 221 | static struct ioengine_ops ioengine = { |
2866c82d JA |
222 | .name = "libaio", |
223 | .version = FIO_IOOPS_VERSION, | |
224 | .init = fio_libaio_init, | |
225 | .prep = fio_libaio_prep, | |
226 | .queue = fio_libaio_queue, | |
755200a3 | 227 | .commit = fio_libaio_commit, |
2866c82d JA |
228 | .cancel = fio_libaio_cancel, |
229 | .getevents = fio_libaio_getevents, | |
230 | .event = fio_libaio_event, | |
231 | .cleanup = fio_libaio_cleanup, | |
b5af8293 JA |
232 | .open_file = generic_open_file, |
233 | .close_file = generic_close_file, | |
2866c82d | 234 | }; |
34cfcdaf JA |
235 | |
236 | #else /* FIO_HAVE_LIBAIO */ | |
237 | ||
238 | /* | |
239 | * When we have a proper configure system in place, we simply wont build | |
240 | * and install this io engine. For now install a crippled version that | |
241 | * just complains and fails to load. | |
242 | */ | |
243 | static int fio_libaio_init(struct thread_data fio_unused *td) | |
244 | { | |
245 | fprintf(stderr, "fio: libaio not available\n"); | |
246 | return 1; | |
247 | } | |
248 | ||
5f350952 | 249 | static struct ioengine_ops ioengine = { |
34cfcdaf JA |
250 | .name = "libaio", |
251 | .version = FIO_IOOPS_VERSION, | |
252 | .init = fio_libaio_init, | |
253 | }; | |
254 | ||
255 | #endif | |
5f350952 JA |
256 | |
257 | static void fio_init fio_libaio_register(void) | |
258 | { | |
259 | register_ioengine(&ioengine); | |
260 | } | |
261 | ||
262 | static void fio_exit fio_libaio_unregister(void) | |
263 | { | |
264 | unregister_ioengine(&ioengine); | |
265 | } |