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 | 14 | |
34cfcdaf JA |
15 | #ifdef FIO_HAVE_POSIXAIO |
16 | ||
2866c82d JA |
17 | struct posixaio_data { |
18 | struct io_u **aio_events; | |
207cb0f0 | 19 | unsigned int queued; |
2866c82d JA |
20 | }; |
21 | ||
22 | static int fill_timespec(struct timespec *ts) | |
23 | { | |
24 | #ifdef _POSIX_TIMERS | |
25 | if (!clock_gettime(CLOCK_MONOTONIC, ts)) | |
26 | return 0; | |
27 | ||
28 | perror("clock_gettime"); | |
29 | #endif | |
30 | return 1; | |
31 | } | |
32 | ||
33 | static unsigned long long ts_utime_since_now(struct timespec *t) | |
34 | { | |
35 | long long sec, nsec; | |
36 | struct timespec now; | |
37 | ||
38 | if (fill_timespec(&now)) | |
39 | return 0; | |
40 | ||
41 | sec = now.tv_sec - t->tv_sec; | |
42 | nsec = now.tv_nsec - t->tv_nsec; | |
43 | if (sec > 0 && nsec < 0) { | |
44 | sec--; | |
45 | nsec += 1000000000; | |
46 | } | |
47 | ||
48 | sec *= 1000000; | |
49 | nsec /= 1000; | |
50 | return sec + nsec; | |
51 | } | |
52 | ||
7a16dd02 JA |
53 | static int fio_posixaio_cancel(struct thread_data fio_unused *td, |
54 | struct io_u *io_u) | |
2866c82d | 55 | { |
53cdc686 JA |
56 | struct fio_file *f = io_u->file; |
57 | int r = aio_cancel(f->fd, &io_u->aiocb); | |
2866c82d | 58 | |
2faf9ec8 | 59 | if (r == AIO_ALLDONE || r == AIO_CANCELED) |
2866c82d JA |
60 | return 0; |
61 | ||
62 | return 1; | |
63 | } | |
64 | ||
7a16dd02 JA |
65 | static int fio_posixaio_prep(struct thread_data fio_unused *td, |
66 | struct io_u *io_u) | |
2866c82d JA |
67 | { |
68 | struct aiocb *aiocb = &io_u->aiocb; | |
53cdc686 | 69 | struct fio_file *f = io_u->file; |
2866c82d | 70 | |
53cdc686 | 71 | aiocb->aio_fildes = f->fd; |
cec6b55d JA |
72 | aiocb->aio_buf = io_u->xfer_buf; |
73 | aiocb->aio_nbytes = io_u->xfer_buflen; | |
2866c82d JA |
74 | aiocb->aio_offset = io_u->offset; |
75 | ||
76 | io_u->seen = 0; | |
77 | return 0; | |
78 | } | |
79 | ||
3c77037e JA |
80 | #define SUSPEND_ENTRIES 8 |
81 | ||
e7d2e616 JA |
82 | static int fio_posixaio_getevents(struct thread_data *td, unsigned int min, |
83 | unsigned int max, struct timespec *t) | |
2866c82d JA |
84 | { |
85 | struct posixaio_data *pd = td->io_ops->data; | |
3c77037e | 86 | struct aiocb *suspend_list[SUSPEND_ENTRIES]; |
01743ee1 | 87 | struct flist_head *entry; |
2866c82d | 88 | struct timespec start; |
a3cc770d | 89 | int have_timeout = 0; |
3c77037e | 90 | int suspend_entries = 0; |
a3cc770d | 91 | unsigned int r; |
2866c82d JA |
92 | |
93 | if (t && !fill_timespec(&start)) | |
94 | have_timeout = 1; | |
95 | ||
96 | r = 0; | |
565cc35f | 97 | memset(suspend_list, 0, sizeof(*suspend_list)); |
2866c82d | 98 | restart: |
01743ee1 JA |
99 | flist_for_each(entry, &td->io_u_busylist) { |
100 | struct io_u *io_u = flist_entry(entry, struct io_u, list); | |
2866c82d JA |
101 | int err; |
102 | ||
103 | if (io_u->seen) | |
104 | continue; | |
105 | ||
106 | err = aio_error(&io_u->aiocb); | |
3c77037e JA |
107 | if (err == EINPROGRESS) { |
108 | if (suspend_entries < SUSPEND_ENTRIES) { | |
109 | suspend_list[suspend_entries] = &io_u->aiocb; | |
110 | suspend_entries++; | |
111 | } | |
3f344316 | 112 | continue; |
3c77037e | 113 | } |
3f344316 JA |
114 | |
115 | io_u->seen = 1; | |
207cb0f0 | 116 | pd->queued--; |
3f344316 JA |
117 | pd->aio_events[r++] = io_u; |
118 | ||
119 | if (err == ECANCELED) | |
120 | io_u->resid = io_u->xfer_buflen; | |
121 | else if (!err) { | |
122 | ssize_t retval = aio_return(&io_u->aiocb); | |
123 | ||
124 | io_u->resid = io_u->xfer_buflen - retval; | |
125 | } else | |
126 | io_u->error = err; | |
2866c82d JA |
127 | } |
128 | ||
129 | if (r >= min) | |
130 | return r; | |
131 | ||
132 | if (have_timeout) { | |
133 | unsigned long long usec; | |
134 | ||
135 | usec = (t->tv_sec * 1000000) + (t->tv_nsec / 1000); | |
136 | if (ts_utime_since_now(&start) > usec) | |
137 | return r; | |
138 | } | |
139 | ||
140 | /* | |
3c77037e | 141 | * must have some in-flight, wait for at least one |
2866c82d | 142 | */ |
3c77037e JA |
143 | aio_suspend((const struct aiocb * const *)suspend_list, |
144 | suspend_entries, t); | |
2866c82d JA |
145 | goto restart; |
146 | } | |
147 | ||
148 | static struct io_u *fio_posixaio_event(struct thread_data *td, int event) | |
149 | { | |
150 | struct posixaio_data *pd = td->io_ops->data; | |
151 | ||
152 | return pd->aio_events[event]; | |
153 | } | |
154 | ||
155 | static int fio_posixaio_queue(struct thread_data fio_unused *td, | |
156 | struct io_u *io_u) | |
157 | { | |
207cb0f0 | 158 | struct posixaio_data *pd = td->io_ops->data; |
2866c82d JA |
159 | struct aiocb *aiocb = &io_u->aiocb; |
160 | int ret; | |
161 | ||
7101d9c2 JA |
162 | fio_ro_check(td, io_u); |
163 | ||
2866c82d JA |
164 | if (io_u->ddir == DDIR_READ) |
165 | ret = aio_read(aiocb); | |
87dc1ab1 | 166 | else if (io_u->ddir == DDIR_WRITE) |
2866c82d | 167 | ret = aio_write(aiocb); |
a5f3027c JA |
168 | else if (io_u->ddir == DDIR_TRIM) { |
169 | if (pd->queued) | |
170 | return FIO_Q_BUSY; | |
171 | ||
172 | do_io_u_trim(td, io_u); | |
173 | return FIO_Q_COMPLETED; | |
174 | } else { | |
207cb0f0 | 175 | #ifdef FIO_HAVE_POSIXAIO_FSYNC |
87dc1ab1 | 176 | ret = aio_fsync(O_SYNC, aiocb); |
207cb0f0 JA |
177 | #else |
178 | if (pd->queued) | |
179 | return FIO_Q_BUSY; | |
180 | ||
f011531e | 181 | do_io_u_sync(td, io_u); |
207cb0f0 JA |
182 | return FIO_Q_COMPLETED; |
183 | #endif | |
184 | } | |
185 | ||
95bcd815 | 186 | if (ret) { |
2866c82d | 187 | io_u->error = errno; |
e1161c32 | 188 | td_verror(td, io_u->error, "xfer"); |
36167d82 | 189 | return FIO_Q_COMPLETED; |
95bcd815 | 190 | } |
36167d82 | 191 | |
207cb0f0 | 192 | pd->queued++; |
36167d82 | 193 | return FIO_Q_QUEUED; |
2866c82d JA |
194 | } |
195 | ||
196 | static void fio_posixaio_cleanup(struct thread_data *td) | |
197 | { | |
198 | struct posixaio_data *pd = td->io_ops->data; | |
199 | ||
200 | if (pd) { | |
201 | free(pd->aio_events); | |
202 | free(pd); | |
2866c82d JA |
203 | } |
204 | } | |
205 | ||
206 | static int fio_posixaio_init(struct thread_data *td) | |
207 | { | |
208 | struct posixaio_data *pd = malloc(sizeof(*pd)); | |
209 | ||
cb781c75 | 210 | memset(pd, 0, sizeof(*pd)); |
2dc1bbeb JA |
211 | pd->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *)); |
212 | memset(pd->aio_events, 0, td->o.iodepth * sizeof(struct io_u *)); | |
2866c82d JA |
213 | |
214 | td->io_ops->data = pd; | |
215 | return 0; | |
216 | } | |
217 | ||
5f350952 | 218 | static struct ioengine_ops ioengine = { |
2866c82d JA |
219 | .name = "posixaio", |
220 | .version = FIO_IOOPS_VERSION, | |
221 | .init = fio_posixaio_init, | |
222 | .prep = fio_posixaio_prep, | |
223 | .queue = fio_posixaio_queue, | |
224 | .cancel = fio_posixaio_cancel, | |
225 | .getevents = fio_posixaio_getevents, | |
226 | .event = fio_posixaio_event, | |
227 | .cleanup = fio_posixaio_cleanup, | |
b5af8293 JA |
228 | .open_file = generic_open_file, |
229 | .close_file = generic_close_file, | |
df9c26b1 | 230 | .get_file_size = generic_get_file_size, |
2866c82d | 231 | }; |
34cfcdaf JA |
232 | |
233 | #else /* FIO_HAVE_POSIXAIO */ | |
234 | ||
235 | /* | |
236 | * When we have a proper configure system in place, we simply wont build | |
237 | * and install this io engine. For now install a crippled version that | |
238 | * just complains and fails to load. | |
239 | */ | |
240 | static int fio_posixaio_init(struct thread_data fio_unused *td) | |
241 | { | |
242 | fprintf(stderr, "fio: posixaio not available\n"); | |
243 | return 1; | |
244 | } | |
245 | ||
5f350952 | 246 | static struct ioengine_ops ioengine = { |
34cfcdaf JA |
247 | .name = "posixaio", |
248 | .version = FIO_IOOPS_VERSION, | |
249 | .init = fio_posixaio_init, | |
250 | }; | |
251 | ||
252 | #endif | |
5f350952 JA |
253 | |
254 | static void fio_init fio_posixaio_register(void) | |
255 | { | |
256 | register_ioengine(&ioengine); | |
257 | } | |
258 | ||
259 | static void fio_exit fio_posixaio_unregister(void) | |
260 | { | |
261 | unregister_ioengine(&ioengine); | |
262 | } |