Commit | Line | Data |
---|---|---|
2866c82d | 1 | /* |
da751ca9 JA |
2 | * posixaio engine |
3 | * | |
4 | * IO engine that uses the posix defined aio interface. | |
2866c82d JA |
5 | * |
6 | */ | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <unistd.h> | |
10 | #include <errno.h> | |
bc733f3b | 11 | #include <fcntl.h> |
5f350952 JA |
12 | |
13 | #include "../fio.h" | |
2866c82d JA |
14 | |
15 | struct posixaio_data { | |
16 | struct io_u **aio_events; | |
207cb0f0 | 17 | unsigned int queued; |
2866c82d JA |
18 | }; |
19 | ||
69212fc4 | 20 | static unsigned long long ts_utime_since_now(const struct timespec *start) |
2866c82d | 21 | { |
2866c82d JA |
22 | struct timespec now; |
23 | ||
69212fc4 | 24 | if (fio_get_mono_time(&now) < 0) |
2866c82d | 25 | return 0; |
69212fc4 BVA |
26 | |
27 | return utime_since(start, &now); | |
2866c82d JA |
28 | } |
29 | ||
7a16dd02 JA |
30 | static int fio_posixaio_cancel(struct thread_data fio_unused *td, |
31 | struct io_u *io_u) | |
2866c82d | 32 | { |
53cdc686 JA |
33 | struct fio_file *f = io_u->file; |
34 | int r = aio_cancel(f->fd, &io_u->aiocb); | |
2866c82d | 35 | |
2faf9ec8 | 36 | if (r == AIO_ALLDONE || r == AIO_CANCELED) |
2866c82d JA |
37 | return 0; |
38 | ||
39 | return 1; | |
40 | } | |
41 | ||
7a16dd02 JA |
42 | static int fio_posixaio_prep(struct thread_data fio_unused *td, |
43 | struct io_u *io_u) | |
2866c82d | 44 | { |
e97c1442 | 45 | os_aiocb_t *aiocb = &io_u->aiocb; |
53cdc686 | 46 | struct fio_file *f = io_u->file; |
2866c82d | 47 | |
53cdc686 | 48 | aiocb->aio_fildes = f->fd; |
cec6b55d JA |
49 | aiocb->aio_buf = io_u->xfer_buf; |
50 | aiocb->aio_nbytes = io_u->xfer_buflen; | |
2866c82d | 51 | aiocb->aio_offset = io_u->offset; |
9918be5a | 52 | aiocb->aio_sigevent.sigev_notify = SIGEV_NONE; |
2866c82d JA |
53 | |
54 | io_u->seen = 0; | |
55 | return 0; | |
56 | } | |
57 | ||
3c77037e JA |
58 | #define SUSPEND_ENTRIES 8 |
59 | ||
e7d2e616 | 60 | static int fio_posixaio_getevents(struct thread_data *td, unsigned int min, |
1f440ece | 61 | unsigned int max, const struct timespec *t) |
2866c82d | 62 | { |
565e784d | 63 | struct posixaio_data *pd = td->io_ops_data; |
e97c1442 | 64 | os_aiocb_t *suspend_list[SUSPEND_ENTRIES]; |
2866c82d | 65 | struct timespec start; |
a3cc770d | 66 | int have_timeout = 0; |
f8326424 | 67 | int suspend_entries; |
2ae0b204 | 68 | struct io_u *io_u; |
a3cc770d | 69 | unsigned int r; |
2ae0b204 | 70 | int i; |
2866c82d | 71 | |
69212fc4 | 72 | if (t && fio_get_mono_time(&start) == 0) |
2866c82d | 73 | have_timeout = 1; |
5351f564 JA |
74 | else |
75 | memset(&start, 0, sizeof(start)); | |
2866c82d JA |
76 | |
77 | r = 0; | |
78 | restart: | |
effe99e4 | 79 | memset(suspend_list, 0, sizeof(suspend_list)); |
f8326424 | 80 | suspend_entries = 0; |
2ae0b204 | 81 | io_u_qiter(&td->io_u_all, io_u, i) { |
2866c82d JA |
82 | int err; |
83 | ||
2ae0b204 | 84 | if (io_u->seen || !(io_u->flags & IO_U_F_FLIGHT)) |
2866c82d JA |
85 | continue; |
86 | ||
87 | err = aio_error(&io_u->aiocb); | |
3c77037e JA |
88 | if (err == EINPROGRESS) { |
89 | if (suspend_entries < SUSPEND_ENTRIES) { | |
90 | suspend_list[suspend_entries] = &io_u->aiocb; | |
91 | suspend_entries++; | |
92 | } | |
3f344316 | 93 | continue; |
3c77037e | 94 | } |
3f344316 JA |
95 | |
96 | io_u->seen = 1; | |
207cb0f0 | 97 | pd->queued--; |
3f344316 JA |
98 | pd->aio_events[r++] = io_u; |
99 | ||
100 | if (err == ECANCELED) | |
101 | io_u->resid = io_u->xfer_buflen; | |
102 | else if (!err) { | |
103 | ssize_t retval = aio_return(&io_u->aiocb); | |
104 | ||
105 | io_u->resid = io_u->xfer_buflen - retval; | |
106 | } else | |
107 | io_u->error = err; | |
2866c82d JA |
108 | } |
109 | ||
110 | if (r >= min) | |
111 | return r; | |
112 | ||
113 | if (have_timeout) { | |
114 | unsigned long long usec; | |
115 | ||
116 | usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000); | |
117 | if (ts_utime_since_now(&start) > usec) | |
118 | return r; | |
119 | } | |
120 | ||
121 | /* | |
3c77037e | 122 | * must have some in-flight, wait for at least one |
2866c82d | 123 | */ |
e97c1442 | 124 | aio_suspend((const os_aiocb_t * const *)suspend_list, |
3c77037e | 125 | suspend_entries, t); |
2866c82d JA |
126 | goto restart; |
127 | } | |
128 | ||
129 | static struct io_u *fio_posixaio_event(struct thread_data *td, int event) | |
130 | { | |
565e784d | 131 | struct posixaio_data *pd = td->io_ops_data; |
2866c82d JA |
132 | |
133 | return pd->aio_events[event]; | |
134 | } | |
135 | ||
d3b07186 BVA |
136 | static enum fio_q_status fio_posixaio_queue(struct thread_data *td, |
137 | struct io_u *io_u) | |
2866c82d | 138 | { |
565e784d | 139 | struct posixaio_data *pd = td->io_ops_data; |
e97c1442 | 140 | os_aiocb_t *aiocb = &io_u->aiocb; |
2866c82d JA |
141 | int ret; |
142 | ||
7101d9c2 JA |
143 | fio_ro_check(td, io_u); |
144 | ||
2866c82d JA |
145 | if (io_u->ddir == DDIR_READ) |
146 | ret = aio_read(aiocb); | |
87dc1ab1 | 147 | else if (io_u->ddir == DDIR_WRITE) |
2866c82d | 148 | ret = aio_write(aiocb); |
a5f3027c JA |
149 | else if (io_u->ddir == DDIR_TRIM) { |
150 | if (pd->queued) | |
151 | return FIO_Q_BUSY; | |
152 | ||
153 | do_io_u_trim(td, io_u); | |
154 | return FIO_Q_COMPLETED; | |
155 | } else { | |
67bf9823 | 156 | #ifdef CONFIG_POSIXAIO_FSYNC |
87dc1ab1 | 157 | ret = aio_fsync(O_SYNC, aiocb); |
207cb0f0 JA |
158 | #else |
159 | if (pd->queued) | |
160 | return FIO_Q_BUSY; | |
161 | ||
f011531e | 162 | do_io_u_sync(td, io_u); |
207cb0f0 JA |
163 | return FIO_Q_COMPLETED; |
164 | #endif | |
165 | } | |
4c057b34 | 166 | |
95bcd815 | 167 | if (ret) { |
138c0e22 | 168 | int aio_err = errno; |
4c057b34 | 169 | |
afa16407 JA |
170 | /* |
171 | * At least OSX has a very low limit on the number of pending | |
def1d8e3 JA |
172 | * IOs, so if it returns EAGAIN, we are out of resources |
173 | * to queue more. Just return FIO_Q_BUSY to naturally | |
174 | * drop off at this depth. | |
afa16407 | 175 | */ |
4c057b34 | 176 | if (aio_err == EAGAIN) |
afa16407 JA |
177 | return FIO_Q_BUSY; |
178 | ||
4c057b34 | 179 | io_u->error = aio_err; |
e1161c32 | 180 | td_verror(td, io_u->error, "xfer"); |
36167d82 | 181 | return FIO_Q_COMPLETED; |
95bcd815 | 182 | } |
36167d82 | 183 | |
207cb0f0 | 184 | pd->queued++; |
36167d82 | 185 | return FIO_Q_QUEUED; |
2866c82d JA |
186 | } |
187 | ||
188 | static void fio_posixaio_cleanup(struct thread_data *td) | |
189 | { | |
565e784d | 190 | struct posixaio_data *pd = td->io_ops_data; |
2866c82d JA |
191 | |
192 | if (pd) { | |
193 | free(pd->aio_events); | |
194 | free(pd); | |
2866c82d JA |
195 | } |
196 | } | |
197 | ||
198 | static int fio_posixaio_init(struct thread_data *td) | |
199 | { | |
200 | struct posixaio_data *pd = malloc(sizeof(*pd)); | |
201 | ||
cb781c75 | 202 | memset(pd, 0, sizeof(*pd)); |
2dc1bbeb JA |
203 | pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *)); |
204 | memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *)); | |
2866c82d | 205 | |
565e784d | 206 | td->io_ops_data = pd; |
2866c82d JA |
207 | return 0; |
208 | } | |
209 | ||
5f350952 | 210 | static struct ioengine_ops ioengine = { |
2866c82d JA |
211 | .name = "posixaio", |
212 | .version = FIO_IOOPS_VERSION, | |
04ba61df | 213 | .flags = FIO_ASYNCIO_SYNC_TRIM, |
2866c82d JA |
214 | .init = fio_posixaio_init, |
215 | .prep = fio_posixaio_prep, | |
216 | .queue = fio_posixaio_queue, | |
217 | .cancel = fio_posixaio_cancel, | |
218 | .getevents = fio_posixaio_getevents, | |
219 | .event = fio_posixaio_event, | |
220 | .cleanup = fio_posixaio_cleanup, | |
b5af8293 JA |
221 | .open_file = generic_open_file, |
222 | .close_file = generic_close_file, | |
df9c26b1 | 223 | .get_file_size = generic_get_file_size, |
2866c82d | 224 | }; |
34cfcdaf | 225 | |
5f350952 JA |
226 | static void fio_init fio_posixaio_register(void) |
227 | { | |
228 | register_ioengine(&ioengine); | |
229 | } | |
230 | ||
231 | static void fio_exit fio_posixaio_unregister(void) | |
232 | { | |
233 | unregister_ioengine(&ioengine); | |
234 | } |