1 |
/* |
2 |
* libev linux aio fd activity backend |
3 |
* |
4 |
* Copyright (c) 2019 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
* All rights reserved. |
6 |
* |
7 |
* Redistribution and use in source and binary forms, with or without modifica- |
8 |
* tion, are permitted provided that the following conditions are met: |
9 |
* |
10 |
* 1. Redistributions of source code must retain the above copyright notice, |
11 |
* this list of conditions and the following disclaimer. |
12 |
* |
13 |
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
* notice, this list of conditions and the following disclaimer in the |
15 |
* documentation and/or other materials provided with the distribution. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 |
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 |
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 |
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
23 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
25 |
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
* |
28 |
* Alternatively, the contents of this file may be used under the terms of |
29 |
* the GNU General Public License ("GPL") version 2 or any later version, |
30 |
* in which case the provisions of the GPL are applicable instead of |
31 |
* the above. If you wish to allow the use of your version of this file |
32 |
* only under the terms of the GPL and not to allow others to use your |
33 |
* version of this file under the BSD license, indicate your decision |
34 |
* by deleting the provisions above and replace them with the notice |
35 |
* and other provisions required by the GPL. If you do not delete the |
36 |
* provisions above, a recipient may use your version of this file under |
37 |
* either the BSD or the GPL. |
38 |
*/ |
39 |
|
40 |
#define EPOLL_FALLBACK 1 |
41 |
|
42 |
#include <sys/time.h> /* actually linux/time.h, but we must assume they are compatible */ |
43 |
#include <poll.h> |
44 |
#include <linux/aio_abi.h> |
45 |
|
46 |
#if EPOLL_FALLBACK |
47 |
# include <sys/epoll.h> |
48 |
#endif |
49 |
|
50 |
/* we try to fill 4kB pages exactly. |
51 |
* the ring buffer header is 32 bytes, every io event is 32 bytes. |
52 |
* the kernel takes the io event number, doubles it, adds 2, adds the ring buffer. |
53 |
* therefore the calculation below will use "exactly" 4kB for the ring buffer |
54 |
*/ |
55 |
#define EV_LINUXAIO_DEPTH (128 / 2 - 2 - 1) /* max. number of io events per batch */ |
56 |
|
57 |
/*****************************************************************************/ |
58 |
/* syscall wrapdadoop */ |
59 |
|
60 |
#include <sys/syscall.h> /* no glibc wrappers */ |
61 |
|
62 |
/* aio_abi.h is not versioned in any way, so we cannot test for its existance */ |
63 |
#define IOCB_CMD_POLL 5 |
64 |
|
65 |
/* taken from linux/fs/aio.c */ |
66 |
#define AIO_RING_MAGIC 0xa10a10a1 |
67 |
#define AIO_RING_INCOMPAT_FEATURES 0 |
68 |
struct aio_ring |
69 |
{ |
70 |
unsigned id; /* kernel internal index number */ |
71 |
unsigned nr; /* number of io_events */ |
72 |
unsigned head; /* Written to by userland or by kernel. */ |
73 |
unsigned tail; |
74 |
|
75 |
unsigned magic; |
76 |
unsigned compat_features; |
77 |
unsigned incompat_features; |
78 |
unsigned header_length; /* size of aio_ring */ |
79 |
|
80 |
struct io_event io_events[0]; |
81 |
}; |
82 |
|
83 |
inline_size |
84 |
int |
85 |
ev_io_setup (unsigned nr_events, aio_context_t *ctx_idp) |
86 |
{ |
87 |
return syscall (SYS_io_setup, nr_events, ctx_idp); |
88 |
} |
89 |
|
90 |
inline_size |
91 |
int |
92 |
ev_io_destroy (aio_context_t ctx_id) |
93 |
{ |
94 |
return syscall (SYS_io_destroy, ctx_id); |
95 |
} |
96 |
|
97 |
inline_size |
98 |
int |
99 |
ev_io_submit (aio_context_t ctx_id, long nr, struct iocb *cbp[]) |
100 |
{ |
101 |
return syscall (SYS_io_submit, ctx_id, nr, cbp); |
102 |
} |
103 |
|
104 |
inline_size |
105 |
int |
106 |
ev_io_cancel (aio_context_t ctx_id, struct iocb *cbp, struct io_event *result) |
107 |
{ |
108 |
return syscall (SYS_io_cancel, ctx_id, cbp, result); |
109 |
} |
110 |
|
111 |
inline_size |
112 |
int |
113 |
ev_io_getevents (aio_context_t ctx_id, long min_nr, long nr, struct io_event *events, struct timespec *timeout) |
114 |
{ |
115 |
return syscall (SYS_io_getevents, ctx_id, min_nr, nr, events, timeout); |
116 |
} |
117 |
|
118 |
/*****************************************************************************/ |
119 |
/* actual backed implementation */ |
120 |
|
121 |
/* we use out own wrapper structure in acse we ever want to do something "clever" */ |
122 |
typedef struct aniocb |
123 |
{ |
124 |
struct iocb io; |
125 |
/*int inuse;*/ |
126 |
} *ANIOCBP; |
127 |
|
128 |
inline_size |
129 |
void |
130 |
linuxaio_array_needsize_iocbp (ANIOCBP *base, int count) |
131 |
{ |
132 |
/* TODO: quite the overhead to allocate every iocb separately, maybe use our own alocator? */ |
133 |
while (count--) |
134 |
{ |
135 |
*base = (ANIOCBP)ev_malloc (sizeof (**base)); |
136 |
/* TODO: full zero initialize required? */ |
137 |
memset (*base, 0, sizeof (**base)); |
138 |
/* would be nice to initialize fd/data as well, but array_needsize API doesn't support that */ |
139 |
(*base)->io.aio_lio_opcode = IOCB_CMD_POLL; |
140 |
++base; |
141 |
} |
142 |
} |
143 |
|
144 |
ecb_cold |
145 |
static void |
146 |
linuxaio_free_iocbp (EV_P) |
147 |
{ |
148 |
while (linuxaio_iocbpmax--) |
149 |
ev_free (linuxaio_iocbps [linuxaio_iocbpmax]); |
150 |
|
151 |
linuxaio_iocbpmax = 0; /* next resize will completely reallocate the array, at some overhead */ |
152 |
} |
153 |
|
154 |
static void |
155 |
linuxaio_modify (EV_P_ int fd, int oev, int nev) |
156 |
{ |
157 |
array_needsize (ANIOCBP, linuxaio_iocbps, linuxaio_iocbpmax, fd + 1, linuxaio_array_needsize_iocbp); |
158 |
struct aniocb *iocb = linuxaio_iocbps [fd]; |
159 |
|
160 |
#if EPOLL_FALLBACK |
161 |
if (iocb->io.aio_reqprio < 0) |
162 |
{ |
163 |
epoll_ctl (backend_fd, EPOLL_CTL_DEL, fd, 0); |
164 |
iocb->io.aio_reqprio = 0; |
165 |
} |
166 |
#endif |
167 |
|
168 |
if (iocb->io.aio_buf) |
169 |
ev_io_cancel (linuxaio_ctx, &iocb->io, (struct io_event *)0); /* always returns an error relevant kernels */ |
170 |
|
171 |
if (nev) |
172 |
{ |
173 |
iocb->io.aio_data = fd; |
174 |
iocb->io.aio_fildes = fd; |
175 |
iocb->io.aio_buf = |
176 |
(nev & EV_READ ? POLLIN : 0) |
177 |
| (nev & EV_WRITE ? POLLOUT : 0); |
178 |
|
179 |
/* queue iocb up for io_submit */ |
180 |
/* this assumes we only ever get one call per fd per loop iteration */ |
181 |
++linuxaio_submitcnt; |
182 |
array_needsize (struct iocb *, linuxaio_submits, linuxaio_submitmax, linuxaio_submitcnt, array_needsize_noinit); |
183 |
linuxaio_submits [linuxaio_submitcnt - 1] = &iocb->io; |
184 |
} |
185 |
} |
186 |
|
187 |
static void |
188 |
linuxaio_parse_events (EV_P_ struct io_event *ev, int nr) |
189 |
{ |
190 |
while (nr) |
191 |
{ |
192 |
int fd = ev->data; |
193 |
int res = ev->res; |
194 |
|
195 |
assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
196 |
|
197 |
/* linux aio is oneshot: rearm fd */ |
198 |
linuxaio_iocbps [fd]->io.aio_buf = 0; |
199 |
anfds [fd].events = 0; |
200 |
fd_change (EV_A_ fd, 0); |
201 |
|
202 |
/* feed events, we do not expect or handle POLLNVAL */ |
203 |
if (ecb_expect_false (res & POLLNVAL)) |
204 |
fd_kill (EV_A_ fd); |
205 |
else |
206 |
fd_event ( |
207 |
EV_A_ |
208 |
fd, |
209 |
(res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
210 |
| (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
211 |
); |
212 |
|
213 |
--nr; |
214 |
++ev; |
215 |
} |
216 |
} |
217 |
|
218 |
/* get any events from ringbuffer, return true if any were handled */ |
219 |
static int |
220 |
linuxaio_get_events_from_ring (EV_P) |
221 |
{ |
222 |
struct aio_ring *ring = (struct aio_ring *)linuxaio_ctx; |
223 |
|
224 |
/* the kernel reads and writes both of these variables, */ |
225 |
/* as a C extension, we assume that volatile use here */ |
226 |
/* both makes reads atomic and once-only */ |
227 |
unsigned head = *(volatile unsigned *)&ring->head; |
228 |
unsigned tail = *(volatile unsigned *)&ring->tail; |
229 |
|
230 |
if (head == tail) |
231 |
return 0; |
232 |
|
233 |
/* bail out if the ring buffer doesn't match the expected layout */ |
234 |
if (ecb_expect_false (ring->magic != AIO_RING_MAGIC) |
235 |
|| ring->incompat_features != AIO_RING_INCOMPAT_FEATURES |
236 |
|| ring->header_length != sizeof (struct aio_ring)) /* TODO: or use it to find io_event[0]? */ |
237 |
return 0; |
238 |
|
239 |
/* make sure the events up to tail are visible */ |
240 |
ECB_MEMORY_FENCE_ACQUIRE; |
241 |
|
242 |
/* parse all available events, but only once, to avoid starvation */ |
243 |
if (tail > head) /* normal case around */ |
244 |
linuxaio_parse_events (EV_A_ ring->io_events + head, tail - head); |
245 |
else /* wrapped around */ |
246 |
{ |
247 |
linuxaio_parse_events (EV_A_ ring->io_events + head, ring->nr - head); |
248 |
linuxaio_parse_events (EV_A_ ring->io_events, tail); |
249 |
} |
250 |
|
251 |
/* as an extension to C, we hope that the volatile will makethis atomic and once-only */ |
252 |
*(volatile unsigned *)&ring->head = tail; |
253 |
/* make sure kernel can see our new head value - probably not required */ |
254 |
ECB_MEMORY_FENCE_RELEASE; |
255 |
|
256 |
return 1; |
257 |
} |
258 |
|
259 |
/* read at least one event from kernel, or timeout */ |
260 |
inline_size |
261 |
void |
262 |
linuxaio_get_events (EV_P_ ev_tstamp timeout) |
263 |
{ |
264 |
struct timespec ts; |
265 |
struct io_event ioev; |
266 |
int res; |
267 |
|
268 |
if (linuxaio_get_events_from_ring (EV_A)) |
269 |
return; |
270 |
|
271 |
/* no events, so wait for at least one, then poll ring buffer again */ |
272 |
/* this degrades to one event per loop iteration */ |
273 |
/* if the ring buffer changes layout, but so be it */ |
274 |
|
275 |
ts.tv_sec = (long)timeout; |
276 |
ts.tv_nsec = (long)((timeout - ts.tv_sec) * 1e9); |
277 |
|
278 |
res = ev_io_getevents (linuxaio_ctx, 1, 1, &ioev, &ts); |
279 |
|
280 |
if (res < 0) |
281 |
if (errno == EINTR) |
282 |
/* ignored */; |
283 |
else |
284 |
ev_syserr ("(libev) linuxaio io_getevents"); |
285 |
else if (res) |
286 |
{ |
287 |
/* at least one event received, handle it and any remaining ones in the ring buffer */ |
288 |
linuxaio_parse_events (EV_A_ &ioev, 1); |
289 |
linuxaio_get_events_from_ring (EV_A); |
290 |
} |
291 |
} |
292 |
|
293 |
#if EPOLL_FALLBACK |
294 |
static void |
295 |
linuxaio_rearm_epoll (EV_P_ struct iocb *iocb, int op) |
296 |
{ |
297 |
struct epoll_event eev; |
298 |
|
299 |
eev.events = EPOLLONESHOT; |
300 |
if (iocb->aio_buf & POLLIN ) eev.events |= EPOLLIN ; |
301 |
if (iocb->aio_buf & POLLOUT) eev.events |= EPOLLOUT; |
302 |
eev.data.fd = iocb->aio_fildes; |
303 |
|
304 |
if (epoll_ctl (backend_fd, op, iocb->aio_fildes, &eev) < 0) |
305 |
ev_syserr ("(libeio) linuxaio epoll_ctl"); |
306 |
} |
307 |
#endif |
308 |
|
309 |
static void |
310 |
linuxaio_poll (EV_P_ ev_tstamp timeout) |
311 |
{ |
312 |
int submitted; |
313 |
|
314 |
/* first phase: submit new iocbs */ |
315 |
|
316 |
/* io_submit might return less than the requested number of iocbs */ |
317 |
/* this is, afaics, only because of errors, but we go by the book and use a loop, */ |
318 |
/* which allows us to pinpoint the errornous iocb */ |
319 |
for (submitted = 0; submitted < linuxaio_submitcnt; ) |
320 |
{ |
321 |
int res = ev_io_submit (linuxaio_ctx, linuxaio_submitcnt - submitted, linuxaio_submits + submitted); |
322 |
|
323 |
if (ecb_expect_false (res < 0)) |
324 |
if (errno == EAGAIN) |
325 |
{ |
326 |
/* This happens when the ring buffer is full, at least. I assume this means |
327 |
* that the event was queued synchronously during io_submit, and thus |
328 |
* the buffer overflowed. |
329 |
* In this case, we just try in next loop iteration. |
330 |
* This should not result in a few fds taking priority, as the interface |
331 |
* is one-shot, and we submit iocb's in a round-robin fashion. |
332 |
*/ |
333 |
memmove (linuxaio_submits, linuxaio_submits + submitted, (linuxaio_submitcnt - submitted) * sizeof (*linuxaio_submits)); |
334 |
linuxaio_submitcnt -= submitted; |
335 |
timeout = 0; |
336 |
break; |
337 |
} |
338 |
#if EPOLL_FALLBACK |
339 |
else if (errno == EINVAL) |
340 |
{ |
341 |
/* This happens for unsupported fds, officially, but in my testing, |
342 |
* also randomly happens for supported fds. We fall back to good old |
343 |
* poll() here, under the assumption that this is a very rare case. |
344 |
* See https://lore.kernel.org/patchwork/patch/1047453/ for evidence |
345 |
* that the problem is known, but ignored. |
346 |
*/ |
347 |
struct iocb *iocb = linuxaio_submits [submitted]; |
348 |
res = 1; /* skip this iocb */ |
349 |
|
350 |
linuxaio_rearm_epoll (EV_A_ iocb, EPOLL_CTL_ADD); |
351 |
iocb->aio_reqprio = -1; /* mark iocb as epoll */ |
352 |
} |
353 |
#endif |
354 |
else |
355 |
ev_syserr ("(libev) linuxaio io_submit"); |
356 |
|
357 |
submitted += res; |
358 |
} |
359 |
|
360 |
linuxaio_submitcnt = 0; |
361 |
|
362 |
/* second phase: fetch and parse events */ |
363 |
|
364 |
linuxaio_get_events (EV_A_ timeout); |
365 |
} |
366 |
|
367 |
#if EPOLL_FALLBACK |
368 |
|
369 |
static void |
370 |
linuxaio_epoll_cb (EV_P_ struct ev_io *w, int revents) |
371 |
{ |
372 |
struct epoll_event events[16]; |
373 |
|
374 |
for (;;) |
375 |
{ |
376 |
int idx; |
377 |
int res = epoll_wait (backend_fd, events, sizeof (events) / sizeof (events [0]), 0); |
378 |
|
379 |
if (ecb_expect_false (res < 0)) |
380 |
ev_syserr ("(libev) linuxaio epoll_wait"); |
381 |
else if (!res) |
382 |
break; |
383 |
|
384 |
for (idx = res; idx--; ) |
385 |
{ |
386 |
int fd = events [idx].data.fd; |
387 |
uint32_t ev = events [idx].events; |
388 |
|
389 |
assert (("libev: iocb fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
390 |
|
391 |
linuxaio_rearm_epoll (EV_A_ &linuxaio_iocbps [fd]->io, EPOLL_CTL_MOD); |
392 |
|
393 |
fd_event (EV_A_ fd, |
394 |
(ev & (EPOLLOUT | EPOLLERR | EPOLLHUP) ? EV_WRITE : 0) |
395 |
| (ev & (EPOLLIN | EPOLLERR | EPOLLHUP) ? EV_READ : 0)); |
396 |
} |
397 |
|
398 |
if (res < sizeof (events) / sizeof (events [0])) |
399 |
break; |
400 |
} |
401 |
} |
402 |
|
403 |
#endif |
404 |
|
405 |
inline_size |
406 |
int |
407 |
linuxaio_init (EV_P_ int flags) |
408 |
{ |
409 |
/* would be great to have a nice test for IOCB_CMD_POLL instead */ |
410 |
/* also: test some semi-common fd types, such as files and ttys in recommended_backends */ |
411 |
#if EPOLL_FALLBACK |
412 |
/* 4.19 made epoll work */ |
413 |
if (ev_linux_version () < 0x041300) |
414 |
return 0; |
415 |
#else |
416 |
/* 4.18 introduced IOCB_CMD_POLL */ |
417 |
if (ev_linux_version () < 0x041200) |
418 |
return 0; |
419 |
#endif |
420 |
|
421 |
linuxaio_ctx = 0; |
422 |
if (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
423 |
return 0; |
424 |
|
425 |
#if EPOLL_FALLBACK |
426 |
backend_fd = ev_epoll_create (); |
427 |
if (backend_fd < 0) |
428 |
{ |
429 |
ev_io_destroy (linuxaio_ctx); |
430 |
return 0; |
431 |
} |
432 |
|
433 |
ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ); |
434 |
ev_io_start (EV_A_ &linuxaio_epoll_w); |
435 |
ev_unref (EV_A); /* watcher should not keep loop alive */ |
436 |
#endif |
437 |
|
438 |
backend_modify = linuxaio_modify; |
439 |
backend_poll = linuxaio_poll; |
440 |
|
441 |
linuxaio_iocbpmax = 0; |
442 |
linuxaio_iocbps = 0; |
443 |
|
444 |
linuxaio_submits = 0; |
445 |
linuxaio_submitmax = 0; |
446 |
linuxaio_submitcnt = 0; |
447 |
|
448 |
return EVBACKEND_LINUXAIO; |
449 |
} |
450 |
|
451 |
inline_size |
452 |
void |
453 |
linuxaio_destroy (EV_P) |
454 |
{ |
455 |
#if EPOLL_FALLBACK |
456 |
close (backend_fd); |
457 |
#endif |
458 |
linuxaio_free_iocbp (EV_A); |
459 |
ev_io_destroy (linuxaio_ctx); |
460 |
} |
461 |
|
462 |
inline_size |
463 |
void |
464 |
linuxaio_fork (EV_P) |
465 |
{ |
466 |
/* this frees all iocbs, which is very heavy-handed */ |
467 |
linuxaio_destroy (EV_A); |
468 |
linuxaio_submitcnt = 0; /* all pointers were invalidated */ |
469 |
|
470 |
linuxaio_ctx = 0; |
471 |
while (ev_io_setup (EV_LINUXAIO_DEPTH, &linuxaio_ctx) < 0) |
472 |
ev_syserr ("(libev) linuxaio io_setup"); |
473 |
|
474 |
#if EPOLL_FALLBACK |
475 |
while ((backend_fd = ev_epoll_create ()) < 0) |
476 |
ev_syserr ("(libev) linuxaio epoll_create"); |
477 |
|
478 |
ev_io_stop (EV_A_ &linuxaio_epoll_w); |
479 |
ev_io_init (EV_A_ &linuxaio_epoll_w, linuxaio_epoll_cb, backend_fd, EV_READ); |
480 |
ev_io_start (EV_A_ &linuxaio_epoll_w); |
481 |
#endif |
482 |
|
483 |
fd_rearm_all (EV_A); |
484 |
} |
485 |
|