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