1 |
/* |
2 |
* libev linux io_uring 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 |
/* |
41 |
* general notes about linux io_uring: |
42 |
* |
43 |
* a) it's the best interface I have seen so far. on linux. |
44 |
* b) best is not necessarily very good. |
45 |
* c) it's better than the aio mess, doesn't suffer from the fork problems |
46 |
* of linux aio or epoll and so on and so on. and you could do event stuff |
47 |
* without any syscalls. what's not to like? |
48 |
* d) ok, it's vastly more complex, but that's ok, really. |
49 |
* e) why 3 mmaps instead of one? one would be more space-efficient, |
50 |
* and I can't see what benefit three would have (other than being |
51 |
* somehow resizable/relocatable, but that's apparently not possible). |
52 |
* (FIXME: newer kernels can use 2 mmaps only, need to look into this). |
53 |
* f) hmm, it's practiclaly undebuggable (gdb can't access the memory, and |
54 |
* the bizarre way structure offsets are communicated makes it hard to |
55 |
* just print the ring buffer heads, even *iff* the memory were visible |
56 |
* in gdb. but then, that's also ok, really. |
57 |
* g) well, you cannot specify a timeout when waiting for events. no, |
58 |
* seriously, the interface doesn't support a timeout. never seen _that_ |
59 |
* before. sure, you can use a timerfd, but that's another syscall |
60 |
* you could have avoided. overall, this bizarre omission smells |
61 |
* like a ยต-optimisation by the io_uring author for his personal |
62 |
* applications, to the detriment of everybody else who just wants |
63 |
* an event loop. but, umm, ok, if that's all, it could be worse. |
64 |
* (FIXME: jens mentioned timeout commands, need to investigate) |
65 |
* h) there is a hardcoded limit of 4096 outstanding events. okay, |
66 |
* at least there is no arbitrary low system-wide limit... |
67 |
* (FIXME: apparently, this was increased to 32768 in later kernels( |
68 |
* i) unlike linux aio, you *can* register more then the limit |
69 |
* of fd events, and the kernel will "gracefully" signal an |
70 |
* overflow, after which you could destroy and recreate the kernel |
71 |
* state, a bit bigger, or fall back to e.g. poll. thats not |
72 |
* totally insane, but kind of questions the point a high |
73 |
* performance I/O framework when it doesn't really work |
74 |
* under stress. |
75 |
* (FIXME: iouring should no longer drop events, need to investigate) |
76 |
* j) but, oh my! is has exactly the same bugs as the linux aio backend, |
77 |
* where some undocumented poll combinations just fail. |
78 |
* so we need epoll AGAIN as a fallback. AGAIN! epoll!! and of course, |
79 |
* this is completely undocumented, have I mantioned this already? |
80 |
* k) overall, the *API* itself is, I dare to say, not a total trainwreck. |
81 |
* the big isuess with it are the bugs requiring epoll, which might |
82 |
* or might not get fixed (do I hold my breath?). |
83 |
*/ |
84 |
|
85 |
#include <sys/timerfd.h> |
86 |
#include <sys/mman.h> |
87 |
#include <poll.h> |
88 |
|
89 |
#define IOURING_INIT_ENTRIES 32 |
90 |
|
91 |
/*****************************************************************************/ |
92 |
/* syscall wrapdadoop - this section has the raw api/abi definitions */ |
93 |
|
94 |
#include <linux/fs.h> |
95 |
#include <linux/types.h> |
96 |
|
97 |
/* mostly directly taken from the kernel or documentation */ |
98 |
|
99 |
struct io_uring_sqe |
100 |
{ |
101 |
__u8 opcode; |
102 |
__u8 flags; |
103 |
__u16 ioprio; |
104 |
__s32 fd; |
105 |
__u64 off; |
106 |
__u64 addr; |
107 |
__u32 len; |
108 |
union { |
109 |
__kernel_rwf_t rw_flags; |
110 |
__u32 fsync_flags; |
111 |
__u16 poll_events; |
112 |
__u32 sync_range_flags; |
113 |
__u32 msg_flags; |
114 |
}; |
115 |
__u64 user_data; |
116 |
union { |
117 |
__u16 buf_index; |
118 |
__u64 __pad2[3]; |
119 |
}; |
120 |
}; |
121 |
|
122 |
struct io_uring_cqe |
123 |
{ |
124 |
__u64 user_data; |
125 |
__s32 res; |
126 |
__u32 flags; |
127 |
}; |
128 |
|
129 |
struct io_sqring_offsets |
130 |
{ |
131 |
__u32 head; |
132 |
__u32 tail; |
133 |
__u32 ring_mask; |
134 |
__u32 ring_entries; |
135 |
__u32 flags; |
136 |
__u32 dropped; |
137 |
__u32 array; |
138 |
__u32 resv1; |
139 |
__u64 resv2; |
140 |
}; |
141 |
|
142 |
struct io_cqring_offsets |
143 |
{ |
144 |
__u32 head; |
145 |
__u32 tail; |
146 |
__u32 ring_mask; |
147 |
__u32 ring_entries; |
148 |
__u32 overflow; |
149 |
__u32 cqes; |
150 |
__u64 resv[2]; |
151 |
}; |
152 |
|
153 |
struct io_uring_params |
154 |
{ |
155 |
__u32 sq_entries; |
156 |
__u32 cq_entries; |
157 |
__u32 flags; |
158 |
__u32 sq_thread_cpu; |
159 |
__u32 sq_thread_idle; |
160 |
__u32 resv[5]; |
161 |
struct io_sqring_offsets sq_off; |
162 |
struct io_cqring_offsets cq_off; |
163 |
}; |
164 |
|
165 |
#define IORING_OP_POLL_ADD 6 |
166 |
#define IORING_OP_POLL_REMOVE 7 |
167 |
|
168 |
#define IORING_ENTER_GETEVENTS 0x01 |
169 |
|
170 |
#define IORING_OFF_SQ_RING 0x00000000ULL |
171 |
#define IORING_OFF_CQ_RING 0x08000000ULL |
172 |
#define IORING_OFF_SQES 0x10000000ULL |
173 |
|
174 |
inline_size |
175 |
int |
176 |
evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) |
177 |
{ |
178 |
return ev_syscall2 (SYS_io_uring_setup, entries, params); |
179 |
} |
180 |
|
181 |
inline_size |
182 |
int |
183 |
evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz) |
184 |
{ |
185 |
return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz); |
186 |
} |
187 |
|
188 |
/*****************************************************************************/ |
189 |
/* actual backed implementation */ |
190 |
|
191 |
/* we hope that volatile will make the compiler access this variables only once */ |
192 |
#define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_sq_ring + iouring_sq_ ## name) |
193 |
#define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_cq_ring + iouring_cq_ ## name) |
194 |
|
195 |
/* the index array */ |
196 |
#define EV_SQ_ARRAY ((unsigned *)((char *)iouring_sq_ring + iouring_sq_array)) |
197 |
|
198 |
/* the submit/completion queue entries */ |
199 |
#define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
200 |
#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) |
201 |
|
202 |
static |
203 |
struct io_uring_sqe * |
204 |
iouring_sqe_get (EV_P) |
205 |
{ |
206 |
unsigned tail = EV_SQ_VAR (tail); |
207 |
|
208 |
if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) |
209 |
{ |
210 |
/* queue full, flush */ |
211 |
evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0); |
212 |
iouring_to_submit = 0; |
213 |
} |
214 |
|
215 |
assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); |
216 |
|
217 |
return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
218 |
} |
219 |
|
220 |
inline_size |
221 |
struct io_uring_sqe * |
222 |
iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe) |
223 |
{ |
224 |
unsigned idx = sqe - EV_SQES; |
225 |
|
226 |
EV_SQ_ARRAY [idx] = idx; |
227 |
ECB_MEMORY_FENCE_RELEASE; |
228 |
++EV_SQ_VAR (tail); |
229 |
/*ECB_MEMORY_FENCE_RELEASE; /* for the time being we assume this is not needed */ |
230 |
++iouring_to_submit; |
231 |
} |
232 |
|
233 |
/*****************************************************************************/ |
234 |
|
235 |
/* when the timerfd expires we simply note the fact, |
236 |
* as the purpose of the timerfd is to wake us up, nothing else. |
237 |
* the next iteration should re-set it. |
238 |
*/ |
239 |
static void |
240 |
iouring_tfd_cb (EV_P_ struct ev_io *w, int revents) |
241 |
{ |
242 |
iouring_tfd_to = EV_TSTAMP_HUGE; |
243 |
} |
244 |
|
245 |
static void |
246 |
iouring_epoll_cb (EV_P_ struct ev_io *w, int revents) |
247 |
{ |
248 |
epoll_poll (EV_A_ 0); |
249 |
} |
250 |
|
251 |
/* called for full and partial cleanup */ |
252 |
ecb_cold |
253 |
static int |
254 |
iouring_internal_destroy (EV_P) |
255 |
{ |
256 |
close (iouring_tfd); |
257 |
close (iouring_fd); |
258 |
|
259 |
if (iouring_sq_ring != MAP_FAILED) munmap (iouring_sq_ring, iouring_sq_ring_size); |
260 |
if (iouring_cq_ring != MAP_FAILED) munmap (iouring_cq_ring, iouring_cq_ring_size); |
261 |
if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes , iouring_sqes_size ); |
262 |
|
263 |
if (ev_is_active (&iouring_epoll_w)) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_epoll_w); |
264 |
if (ev_is_active (&iouring_tfd_w )) ev_ref (EV_A); ev_io_stop (EV_A_ &iouring_tfd_w ); |
265 |
} |
266 |
|
267 |
ecb_cold |
268 |
static int |
269 |
iouring_internal_init (EV_P) |
270 |
{ |
271 |
struct io_uring_params params = { 0 }; |
272 |
|
273 |
iouring_to_submit = 0; |
274 |
|
275 |
iouring_tfd = -1; |
276 |
iouring_sq_ring = MAP_FAILED; |
277 |
iouring_cq_ring = MAP_FAILED; |
278 |
iouring_sqes = MAP_FAILED; |
279 |
|
280 |
for (;;) |
281 |
{ |
282 |
iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
283 |
|
284 |
if (iouring_fd >= 0) |
285 |
break; /* yippie */ |
286 |
|
287 |
if (errno != EINVAL) |
288 |
return -1; /* we failed */ |
289 |
|
290 |
/* EINVAL: lots of possible reasons, but maybe |
291 |
* it is because we hit the unqueryable hardcoded size limit |
292 |
*/ |
293 |
|
294 |
/* we hit the limit already, give up */ |
295 |
if (iouring_max_entries) |
296 |
return -1; |
297 |
|
298 |
/* first time we hit EINVAL? assume we hit the limit, so go back and retry */ |
299 |
iouring_entries >>= 1; |
300 |
iouring_max_entries = iouring_entries; |
301 |
} |
302 |
|
303 |
iouring_sq_ring_size = params.sq_off.array + params.sq_entries * sizeof (unsigned); |
304 |
iouring_cq_ring_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe); |
305 |
iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe); |
306 |
|
307 |
iouring_sq_ring = mmap (0, iouring_sq_ring_size, PROT_READ | PROT_WRITE, |
308 |
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING); |
309 |
iouring_cq_ring = mmap (0, iouring_cq_ring_size, PROT_READ | PROT_WRITE, |
310 |
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_CQ_RING); |
311 |
iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE, |
312 |
MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES); |
313 |
|
314 |
if (iouring_sq_ring == MAP_FAILED || iouring_cq_ring == MAP_FAILED || iouring_sqes == MAP_FAILED) |
315 |
return -1; |
316 |
|
317 |
iouring_sq_head = params.sq_off.head; |
318 |
iouring_sq_tail = params.sq_off.tail; |
319 |
iouring_sq_ring_mask = params.sq_off.ring_mask; |
320 |
iouring_sq_ring_entries = params.sq_off.ring_entries; |
321 |
iouring_sq_flags = params.sq_off.flags; |
322 |
iouring_sq_dropped = params.sq_off.dropped; |
323 |
iouring_sq_array = params.sq_off.array; |
324 |
|
325 |
iouring_cq_head = params.cq_off.head; |
326 |
iouring_cq_tail = params.cq_off.tail; |
327 |
iouring_cq_ring_mask = params.cq_off.ring_mask; |
328 |
iouring_cq_ring_entries = params.cq_off.ring_entries; |
329 |
iouring_cq_overflow = params.cq_off.overflow; |
330 |
iouring_cq_cqes = params.cq_off.cqes; |
331 |
|
332 |
iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC); |
333 |
|
334 |
if (iouring_tfd < 0) |
335 |
return iouring_tfd; |
336 |
|
337 |
iouring_tfd_to = EV_TSTAMP_HUGE; |
338 |
|
339 |
return 0; |
340 |
} |
341 |
|
342 |
ecb_cold |
343 |
static void |
344 |
iouring_fork (EV_P) |
345 |
{ |
346 |
iouring_internal_destroy (EV_A); |
347 |
|
348 |
while (iouring_internal_init (EV_A) < 0) |
349 |
ev_syserr ("(libev) io_uring_setup"); |
350 |
|
351 |
/* forking epoll should also effectively unregister all fds from the backend */ |
352 |
epoll_fork (EV_A); |
353 |
/* epoll_fork already did this. hopefully */ |
354 |
/*fd_rearm_all (EV_A);*/ |
355 |
|
356 |
ev_io_stop (EV_A_ &iouring_epoll_w); |
357 |
ev_io_set (EV_A_ &iouring_epoll_w, backend_fd, EV_READ); |
358 |
ev_io_start (EV_A_ &iouring_epoll_w); |
359 |
|
360 |
ev_io_stop (EV_A_ &iouring_tfd_w); |
361 |
ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ); |
362 |
ev_io_start (EV_A_ &iouring_tfd_w); |
363 |
} |
364 |
|
365 |
/*****************************************************************************/ |
366 |
|
367 |
static void |
368 |
iouring_modify (EV_P_ int fd, int oev, int nev) |
369 |
{ |
370 |
if (ecb_expect_false (anfds [fd].eflags)) |
371 |
{ |
372 |
/* we handed this fd over to epoll, so undo this first */ |
373 |
/* we do it manually because the optimisations on epoll_modify won't do us any good */ |
374 |
epoll_ctl (iouring_fd, EPOLL_CTL_DEL, fd, 0); |
375 |
anfds [fd].eflags = 0; |
376 |
oev = 0; |
377 |
} |
378 |
|
379 |
if (oev) |
380 |
{ |
381 |
/* we assume the sqe's are all "properly" initialised */ |
382 |
struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
383 |
sqe->opcode = IORING_OP_POLL_REMOVE; |
384 |
sqe->fd = fd; |
385 |
sqe->user_data = -1; |
386 |
iouring_sqe_submit (EV_A_ sqe); |
387 |
|
388 |
/* increment generation counter to avoid handling old events */ |
389 |
++anfds [fd].egen; |
390 |
} |
391 |
|
392 |
if (nev) |
393 |
{ |
394 |
struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
395 |
sqe->opcode = IORING_OP_POLL_ADD; |
396 |
sqe->fd = fd; |
397 |
sqe->user_data = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32); |
398 |
sqe->poll_events = |
399 |
(nev & EV_READ ? POLLIN : 0) |
400 |
| (nev & EV_WRITE ? POLLOUT : 0); |
401 |
iouring_sqe_submit (EV_A_ sqe); |
402 |
} |
403 |
} |
404 |
|
405 |
inline_size |
406 |
void |
407 |
iouring_tfd_update (EV_P_ ev_tstamp timeout) |
408 |
{ |
409 |
ev_tstamp tfd_to = mn_now + timeout; |
410 |
|
411 |
/* we assume there will be many iterations per timer change, so |
412 |
* we only re-set the timerfd when we have to because its expiry |
413 |
* is too late. |
414 |
*/ |
415 |
if (ecb_expect_false (tfd_to < iouring_tfd_to)) |
416 |
{ |
417 |
struct itimerspec its; |
418 |
|
419 |
iouring_tfd_to = tfd_to; |
420 |
EV_TS_SET (its.it_interval, 0.); |
421 |
EV_TS_SET (its.it_value, tfd_to); |
422 |
|
423 |
if (timerfd_settime (iouring_tfd, TFD_TIMER_ABSTIME, &its, 0) < 0) |
424 |
assert (("libev: iouring timerfd_settime failed", 0)); |
425 |
} |
426 |
} |
427 |
|
428 |
inline_size |
429 |
void |
430 |
iouring_process_cqe (EV_P_ struct io_uring_cqe *cqe) |
431 |
{ |
432 |
int fd = cqe->user_data & 0xffffffffU; |
433 |
uint32_t gen = cqe->user_data >> 32; |
434 |
int res = cqe->res; |
435 |
|
436 |
/* ignore fd removal events, if there are any. TODO: verify */ |
437 |
if (cqe->user_data == (__u64)-1) |
438 |
abort ();//D |
439 |
|
440 |
assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
441 |
|
442 |
/* documentation lies, of course. the result value is NOT like |
443 |
* normal syscalls, but like linux raw syscalls, i.e. negative |
444 |
* error numbers. fortunate, as otherwise there would be no way |
445 |
* to get error codes at all. still, why not document this? |
446 |
*/ |
447 |
|
448 |
/* ignore event if generation doesn't match */ |
449 |
/* this should actually be very rare */ |
450 |
if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
451 |
return; |
452 |
|
453 |
if (ecb_expect_false (res < 0)) |
454 |
{ |
455 |
if (res == -EINVAL) |
456 |
{ |
457 |
/* we assume this error code means the fd/poll combination is buggy |
458 |
* and fall back to epoll. |
459 |
* this error code might also indicate a bug, but the kernel doesn't |
460 |
* distinguish between those two conditions, so... sigh... |
461 |
*/ |
462 |
|
463 |
epoll_modify (EV_A_ fd, 0, anfds [fd].events); |
464 |
} |
465 |
else if (res == -EBADF) |
466 |
{ |
467 |
assert (("libev: event loop rejected bad fd", res != -EBADF)); |
468 |
fd_kill (EV_A_ fd); |
469 |
} |
470 |
else |
471 |
{ |
472 |
errno = -res; |
473 |
ev_syserr ("(libev) IORING_OP_POLL_ADD"); |
474 |
} |
475 |
|
476 |
return; |
477 |
} |
478 |
|
479 |
/* feed events, we do not expect or handle POLLNVAL */ |
480 |
fd_event ( |
481 |
EV_A_ |
482 |
fd, |
483 |
(res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0) |
484 |
| (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0) |
485 |
); |
486 |
|
487 |
/* io_uring is oneshot, so we need to re-arm the fd next iteration */ |
488 |
/* this also means we usually have to do at least one syscall per iteration */ |
489 |
anfds [fd].events = 0; |
490 |
fd_change (EV_A_ fd, EV_ANFD_REIFY); |
491 |
} |
492 |
|
493 |
/* called when the event queue overflows */ |
494 |
ecb_cold |
495 |
static void |
496 |
iouring_overflow (EV_P) |
497 |
{ |
498 |
/* we have two options, resize the queue (by tearing down |
499 |
* everything and recreating it, or living with it |
500 |
* and polling. |
501 |
* we implement this by resizing tghe queue, and, if that fails, |
502 |
* we just recreate the state on every failure, which |
503 |
* kind of is a very inefficient poll. |
504 |
* one danger is, due to the bios toward lower fds, |
505 |
* we will only really get events for those, so |
506 |
* maybe we need a poll() fallback, after all. |
507 |
*/ |
508 |
/*EV_CQ_VAR (overflow) = 0;*/ /* need to do this if we keep the state and poll manually */ |
509 |
|
510 |
fd_rearm_all (EV_A); |
511 |
|
512 |
/* we double the size until we hit the hard-to-probe maximum */ |
513 |
if (!iouring_max_entries) |
514 |
{ |
515 |
iouring_entries <<= 1; |
516 |
iouring_fork (EV_A); |
517 |
} |
518 |
else |
519 |
{ |
520 |
/* we hit the kernel limit, we should fall back to something else. |
521 |
* we can either poll() a few times and hope for the best, |
522 |
* poll always, or switch to epoll. |
523 |
* since we use epoll anyways, go epoll. |
524 |
*/ |
525 |
|
526 |
iouring_internal_destroy (EV_A); |
527 |
|
528 |
/* this should make it so that on return, we don'T call any uring functions */ |
529 |
iouring_to_submit = 0; |
530 |
|
531 |
for (;;) |
532 |
{ |
533 |
backend = epoll_init (EV_A_ 0); |
534 |
|
535 |
if (backend) |
536 |
break; |
537 |
|
538 |
ev_syserr ("(libev) iouring switch to epoll"); |
539 |
} |
540 |
} |
541 |
} |
542 |
|
543 |
/* handle any events in the completion queue, return true if there were any */ |
544 |
static int |
545 |
iouring_handle_cq (EV_P) |
546 |
{ |
547 |
unsigned head, tail, mask; |
548 |
|
549 |
head = EV_CQ_VAR (head); |
550 |
ECB_MEMORY_FENCE_ACQUIRE; |
551 |
tail = EV_CQ_VAR (tail); |
552 |
|
553 |
if (head == tail) |
554 |
return 0; |
555 |
|
556 |
/* it can only overflow if we have events, yes, yes? */ |
557 |
if (ecb_expect_false (EV_CQ_VAR (overflow))) |
558 |
{ |
559 |
iouring_overflow (EV_A); |
560 |
return 1; |
561 |
} |
562 |
|
563 |
mask = EV_CQ_VAR (ring_mask); |
564 |
|
565 |
do |
566 |
iouring_process_cqe (EV_A_ &EV_CQES [head++ & mask]); |
567 |
while (head != tail); |
568 |
|
569 |
EV_CQ_VAR (head) = head; |
570 |
ECB_MEMORY_FENCE_RELEASE; |
571 |
|
572 |
return 1; |
573 |
} |
574 |
|
575 |
static void |
576 |
iouring_poll (EV_P_ ev_tstamp timeout) |
577 |
{ |
578 |
/* if we have events, no need for extra syscalls, but we might have to queue events */ |
579 |
if (iouring_handle_cq (EV_A)) |
580 |
timeout = EV_TS_CONST (0.); |
581 |
else |
582 |
/* no events, so maybe wait for some */ |
583 |
iouring_tfd_update (EV_A_ timeout); |
584 |
|
585 |
/* only enter the kernel if we have something to submit, or we need to wait */ |
586 |
if (timeout || iouring_to_submit) |
587 |
{ |
588 |
int res; |
589 |
|
590 |
EV_RELEASE_CB; |
591 |
|
592 |
res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1, |
593 |
timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0); |
594 |
iouring_to_submit = 0; |
595 |
|
596 |
EV_ACQUIRE_CB; |
597 |
|
598 |
if (ecb_expect_false (res < 0)) |
599 |
if (errno == EINTR) |
600 |
/* ignore */; |
601 |
else |
602 |
ev_syserr ("(libev) iouring setup"); |
603 |
else |
604 |
iouring_handle_cq (EV_A); |
605 |
} |
606 |
} |
607 |
|
608 |
inline_size |
609 |
int |
610 |
iouring_init (EV_P_ int flags) |
611 |
{ |
612 |
if (!epoll_init (EV_A_ 0)) |
613 |
return 0; |
614 |
|
615 |
iouring_entries = IOURING_INIT_ENTRIES; |
616 |
iouring_max_entries = 0; |
617 |
|
618 |
if (iouring_internal_init (EV_A) < 0) |
619 |
{ |
620 |
iouring_internal_destroy (EV_A); |
621 |
return 0; |
622 |
} |
623 |
|
624 |
ev_io_init (&iouring_epoll_w, iouring_epoll_cb, backend_fd, EV_READ); |
625 |
ev_set_priority (&iouring_epoll_w, EV_MAXPRI); |
626 |
|
627 |
ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ); |
628 |
ev_set_priority (&iouring_tfd_w, EV_MAXPRI); |
629 |
|
630 |
ev_io_start (EV_A_ &iouring_epoll_w); |
631 |
ev_unref (EV_A); /* watcher should not keep loop alive */ |
632 |
|
633 |
ev_io_start (EV_A_ &iouring_tfd_w); |
634 |
ev_unref (EV_A); /* watcher should not keep loop alive */ |
635 |
|
636 |
backend_modify = iouring_modify; |
637 |
backend_poll = iouring_poll; |
638 |
|
639 |
return EVBACKEND_IOURING; |
640 |
} |
641 |
|
642 |
inline_size |
643 |
void |
644 |
iouring_destroy (EV_P) |
645 |
{ |
646 |
iouring_internal_destroy (EV_A); |
647 |
epoll_destroy (EV_A); |
648 |
} |
649 |
|