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); |
5f9099ea | 35 | else if (ddir_sync(io_u->ddir)) |
87dc1ab1 JA |
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 | ||
e7d2e616 JA |
63 | static int fio_libaio_getevents(struct thread_data *td, unsigned int min, |
64 | unsigned int max, struct timespec *t) | |
2866c82d JA |
65 | { |
66 | struct libaio_data *ld = td->io_ops->data; | |
de9937d1 | 67 | int r; |
2866c82d JA |
68 | |
69 | do { | |
70 | r = io_getevents(ld->aio_ctx, min, max, ld->aio_events, t); | |
de9937d1 | 71 | if (r >= (int) min) |
ccbb91cb JA |
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 | |
7101d9c2 JA |
89 | fio_ro_check(td, io_u); |
90 | ||
2dc1bbeb | 91 | if (ld->iocbs_nr == (int) td->o.iodepth) |
755200a3 JA |
92 | return FIO_Q_BUSY; |
93 | ||
94 | /* | |
95 | * fsync is tricky, since it can fail and we need to do it | |
96 | * serialized with other io. the reason is that linux doesn't | |
97 | * support aio fsync yet. So return busy for the case where we | |
98 | * have pending io, to let fio complete those first. | |
99 | */ | |
f011531e | 100 | if (ddir_sync(io_u->ddir)) { |
755200a3 JA |
101 | if (ld->iocbs_nr) |
102 | return FIO_Q_BUSY; | |
5f9099ea | 103 | |
f011531e | 104 | do_io_u_sync(td, io_u); |
755200a3 JA |
105 | return FIO_Q_COMPLETED; |
106 | } | |
107 | ||
a5f3027c JA |
108 | if (io_u->ddir == DDIR_TRIM) { |
109 | if (ld->iocbs_nr) | |
110 | return FIO_Q_BUSY; | |
111 | ||
112 | do_io_u_trim(td, io_u); | |
113 | return FIO_Q_COMPLETED; | |
114 | } | |
115 | ||
755200a3 | 116 | ld->iocbs[ld->iocbs_nr] = &io_u->iocb; |
7e77dd02 | 117 | ld->io_us[ld->iocbs_nr] = io_u; |
755200a3 JA |
118 | ld->iocbs_nr++; |
119 | return FIO_Q_QUEUED; | |
120 | } | |
121 | ||
7e77dd02 JA |
122 | static void fio_libaio_queued(struct thread_data *td, struct io_u **io_us, |
123 | unsigned int nr) | |
124 | { | |
125 | struct timeval now; | |
126 | unsigned int i; | |
127 | ||
12d9d841 JA |
128 | if (!fio_fill_issue_time(td)) |
129 | return; | |
130 | ||
7e77dd02 JA |
131 | fio_gettime(&now, NULL); |
132 | ||
133 | for (i = 0; i < nr; i++) { | |
134 | struct io_u *io_u = io_us[i]; | |
135 | ||
136 | memcpy(&io_u->issue_time, &now, sizeof(now)); | |
137 | io_u_queued(td, io_u); | |
138 | } | |
139 | } | |
140 | ||
755200a3 JA |
141 | static int fio_libaio_commit(struct thread_data *td) |
142 | { | |
143 | struct libaio_data *ld = td->io_ops->data; | |
144 | struct iocb **iocbs; | |
7e77dd02 | 145 | struct io_u **io_us; |
5e00c2c4 | 146 | int ret; |
755200a3 JA |
147 | |
148 | if (!ld->iocbs_nr) | |
149 | return 0; | |
150 | ||
7e77dd02 | 151 | io_us = ld->io_us; |
755200a3 | 152 | iocbs = ld->iocbs; |
2866c82d | 153 | do { |
5e00c2c4 JA |
154 | ret = io_submit(ld->aio_ctx, ld->iocbs_nr, iocbs); |
155 | if (ret > 0) { | |
7e77dd02 | 156 | fio_libaio_queued(td, io_us, ret); |
838bc709 | 157 | io_u_mark_submit(td, ret); |
5e00c2c4 | 158 | ld->iocbs_nr -= ret; |
7e77dd02 | 159 | io_us += ret; |
755200a3 | 160 | iocbs += ret; |
5e00c2c4 | 161 | ret = 0; |
838bc709 JA |
162 | } else if (!ret || ret == -EAGAIN || ret == -EINTR) { |
163 | if (!ret) | |
164 | io_u_mark_submit(td, ret); | |
2866c82d | 165 | continue; |
838bc709 | 166 | } else |
2866c82d | 167 | break; |
5e00c2c4 | 168 | } while (ld->iocbs_nr); |
2866c82d | 169 | |
36167d82 | 170 | return ret; |
2866c82d JA |
171 | } |
172 | ||
173 | static int fio_libaio_cancel(struct thread_data *td, struct io_u *io_u) | |
174 | { | |
175 | struct libaio_data *ld = td->io_ops->data; | |
176 | ||
177 | return io_cancel(ld->aio_ctx, &io_u->iocb, ld->aio_events); | |
178 | } | |
179 | ||
180 | static void fio_libaio_cleanup(struct thread_data *td) | |
181 | { | |
182 | struct libaio_data *ld = td->io_ops->data; | |
183 | ||
184 | if (ld) { | |
185 | io_destroy(ld->aio_ctx); | |
7e77dd02 JA |
186 | free(ld->aio_events); |
187 | free(ld->iocbs); | |
188 | free(ld->io_us); | |
2866c82d | 189 | free(ld); |
2866c82d JA |
190 | } |
191 | } | |
192 | ||
193 | static int fio_libaio_init(struct thread_data *td) | |
194 | { | |
195 | struct libaio_data *ld = malloc(sizeof(*ld)); | |
c1db2dce | 196 | int err; |
2866c82d JA |
197 | |
198 | memset(ld, 0, sizeof(*ld)); | |
c1db2dce JA |
199 | |
200 | err = io_queue_init(td->o.iodepth, &ld->aio_ctx); | |
201 | if (err) { | |
202 | td_verror(td, -err, "io_queue_init"); | |
75de55ac | 203 | log_err("fio: check /proc/sys/fs/aio-max-nr\n"); |
cb781c75 | 204 | free(ld); |
2866c82d JA |
205 | return 1; |
206 | } | |
207 | ||
2dc1bbeb JA |
208 | ld->aio_events = malloc(td->o.iodepth * sizeof(struct io_event)); |
209 | memset(ld->aio_events, 0, td->o.iodepth * sizeof(struct io_event)); | |
210 | ld->iocbs = malloc(td->o.iodepth * sizeof(struct iocb *)); | |
755200a3 | 211 | memset(ld->iocbs, 0, sizeof(struct iocb *)); |
2dc1bbeb JA |
212 | ld->io_us = malloc(td->o.iodepth * sizeof(struct io_u *)); |
213 | memset(ld->io_us, 0, td->o.iodepth * sizeof(struct io_u *)); | |
755200a3 JA |
214 | ld->iocbs_nr = 0; |
215 | ||
2866c82d JA |
216 | td->io_ops->data = ld; |
217 | return 0; | |
218 | } | |
219 | ||
5f350952 | 220 | static struct ioengine_ops ioengine = { |
2866c82d JA |
221 | .name = "libaio", |
222 | .version = FIO_IOOPS_VERSION, | |
223 | .init = fio_libaio_init, | |
224 | .prep = fio_libaio_prep, | |
225 | .queue = fio_libaio_queue, | |
755200a3 | 226 | .commit = fio_libaio_commit, |
2866c82d JA |
227 | .cancel = fio_libaio_cancel, |
228 | .getevents = fio_libaio_getevents, | |
229 | .event = fio_libaio_event, | |
230 | .cleanup = fio_libaio_cleanup, | |
b5af8293 JA |
231 | .open_file = generic_open_file, |
232 | .close_file = generic_close_file, | |
df9c26b1 | 233 | .get_file_size = generic_get_file_size, |
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 | } |