Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

715 lines
21 KiB

  1. /*
  2. * libev linux io_uring fd activity backend
  3. *
  4. * Copyright (c) 2019-2020 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. * general notes about linux io_uring:
  41. *
  42. * a) it's the best interface I have seen so far. on linux.
  43. * b) best is not necessarily very good.
  44. * c) it's better than the aio mess, doesn't suffer from the fork problems
  45. * of linux aio or epoll and so on and so on. and you could do event stuff
  46. * without any syscalls. what's not to like?
  47. * d) ok, it's vastly more complex, but that's ok, really.
  48. * e) why two mmaps instead of one? one would be more space-efficient,
  49. * and I can't see what benefit two would have (other than being
  50. * somehow resizable/relocatable, but that's apparently not possible).
  51. * f) hmm, it's practically undebuggable (gdb can't access the memory, and
  52. * the bizarre way structure offsets are communicated makes it hard to
  53. * just print the ring buffer heads, even *iff* the memory were visible
  54. * in gdb. but then, that's also ok, really.
  55. * g) well, you cannot specify a timeout when waiting for events. no,
  56. * seriously, the interface doesn't support a timeout. never seen _that_
  57. * before. sure, you can use a timerfd, but that's another syscall
  58. * you could have avoided. overall, this bizarre omission smells
  59. * like a µ-optimisation by the io_uring author for his personal
  60. * applications, to the detriment of everybody else who just wants
  61. * an event loop. but, umm, ok, if that's all, it could be worse.
  62. * (from what I gather from the author Jens Axboe, it simply didn't
  63. * occur to him, and he made good on it by adding an unlimited number
  64. * of timeouts later :).
  65. * h) initially there was a hardcoded limit of 4096 outstanding events.
  66. * later versions not only bump this to 32k, but also can handle
  67. * an unlimited amount of events, so this only affects the batch size.
  68. * i) unlike linux aio, you *can* register more then the limit
  69. * of fd events. while early verisons of io_uring signalled an overflow
  70. * and you ended up getting wet. 5.5+ does not do this anymore.
  71. * j) but, oh my! it had exactly the same bugs as the linux aio backend,
  72. * where some undocumented poll combinations just fail. fortunately,
  73. * after finally reaching the author, he was more than willing to fix
  74. * this probably in 5.6+.
  75. * k) overall, the *API* itself is, I dare to say, not a total trainwreck.
  76. * once the bugs ae fixed (probably in 5.6+), it will be without
  77. * competition.
  78. */
  79. /* TODO: use internal TIMEOUT */
  80. /* TODO: take advantage of single mmap, NODROP etc. */
  81. /* TODO: resize cq/sq size independently */
  82. #include <sys/timerfd.h>
  83. #include <sys/mman.h>
  84. #include <poll.h>
  85. #include <stdint.h>
  86. #define IOURING_INIT_ENTRIES 32
  87. /*****************************************************************************/
  88. /* syscall wrapdadoop - this section has the raw api/abi definitions */
  89. #include <linux/fs.h>
  90. #include <linux/types.h>
  91. /* mostly directly taken from the kernel or documentation */
  92. struct io_uring_sqe
  93. {
  94. __u8 opcode;
  95. __u8 flags;
  96. __u16 ioprio;
  97. __s32 fd;
  98. union {
  99. __u64 off;
  100. __u64 addr2;
  101. };
  102. __u64 addr;
  103. __u32 len;
  104. union {
  105. __kernel_rwf_t rw_flags;
  106. __u32 fsync_flags;
  107. __u16 poll_events;
  108. __u32 sync_range_flags;
  109. __u32 msg_flags;
  110. __u32 timeout_flags;
  111. __u32 accept_flags;
  112. __u32 cancel_flags;
  113. __u32 open_flags;
  114. __u32 statx_flags;
  115. __u32 fadvise_advice;
  116. };
  117. __u64 user_data;
  118. union {
  119. __u16 buf_index;
  120. __u16 personality;
  121. __u64 __pad2[3];
  122. };
  123. };
  124. struct io_uring_cqe
  125. {
  126. __u64 user_data;
  127. __s32 res;
  128. __u32 flags;
  129. };
  130. struct io_sqring_offsets
  131. {
  132. __u32 head;
  133. __u32 tail;
  134. __u32 ring_mask;
  135. __u32 ring_entries;
  136. __u32 flags;
  137. __u32 dropped;
  138. __u32 array;
  139. __u32 resv1;
  140. __u64 resv2;
  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. struct io_uring_params
  153. {
  154. __u32 sq_entries;
  155. __u32 cq_entries;
  156. __u32 flags;
  157. __u32 sq_thread_cpu;
  158. __u32 sq_thread_idle;
  159. __u32 features;
  160. __u32 resv[4];
  161. struct io_sqring_offsets sq_off;
  162. struct io_cqring_offsets cq_off;
  163. };
  164. #define IORING_FEAT_SINGLE_MMAP 0x00000001
  165. #define IORING_FEAT_NODROP 0x00000002
  166. #define IORING_FEAT_SUBMIT_STABLE 0x00000004
  167. #define IORING_SETUP_CQSIZE 0x00000008
  168. #define IORING_SETUP_CLAMP 0x00000010
  169. #define IORING_OP_POLL_ADD 6
  170. #define IORING_OP_POLL_REMOVE 7
  171. #define IORING_OP_TIMEOUT 11
  172. #define IORING_OP_TIMEOUT_REMOVE 12
  173. #define IORING_REGISTER_EVENTFD 4
  174. #define IORING_REGISTER_EVENTFD_ASYNC 7
  175. #define IORING_REGISTER_PROBE 8
  176. #define IO_URING_OP_SUPPORTED 1
  177. struct io_uring_probe_op {
  178. __u8 op;
  179. __u8 resv;
  180. __u16 flags;
  181. __u32 resv2;
  182. };
  183. struct io_uring_probe
  184. {
  185. __u8 last_op;
  186. __u8 ops_len;
  187. __u16 resv;
  188. __u32 resv2[3];
  189. struct io_uring_probe_op ops[0];
  190. };
  191. /* relative or absolute, reference clock is CLOCK_MONOTONIC */
  192. struct iouring_kernel_timespec
  193. {
  194. int64_t tv_sec;
  195. long long tv_nsec;
  196. };
  197. #define IORING_TIMEOUT_ABS 0x00000001
  198. #define IORING_ENTER_GETEVENTS 0x01
  199. #define IORING_OFF_SQ_RING 0x00000000ULL
  200. #define IORING_OFF_SQES 0x10000000ULL
  201. #define IORING_FEAT_SINGLE_MMAP 0x00000001
  202. #define IORING_FEAT_NODROP 0x00000002
  203. #define IORING_FEAT_SUBMIT_STABLE 0x00000004
  204. inline_size
  205. int
  206. evsys_io_uring_setup (unsigned entries, struct io_uring_params *params)
  207. {
  208. return ev_syscall2 (SYS_io_uring_setup, entries, params);
  209. }
  210. inline_size
  211. int
  212. evsys_io_uring_enter (int fd, unsigned to_submit, unsigned min_complete, unsigned flags, const sigset_t *sig, size_t sigsz)
  213. {
  214. return ev_syscall6 (SYS_io_uring_enter, fd, to_submit, min_complete, flags, sig, sigsz);
  215. }
  216. inline_size
  217. int
  218. evsys_io_uring_register (unsigned int fd, unsigned int opcode, void *arg, unsigned int nr_args)
  219. {
  220. return ev_syscall4 (SYS_io_uring_register, fd, opcode, arg, nr_args);
  221. }
  222. /*****************************************************************************/
  223. /* actual backend implementation */
  224. /* we hope that volatile will make the compiler access this variables only once */
  225. #define EV_SQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_sq_ ## name)
  226. #define EV_CQ_VAR(name) *(volatile unsigned *)((char *)iouring_ring + iouring_cq_ ## name)
  227. /* the index array */
  228. #define EV_SQ_ARRAY ((unsigned *)((char *)iouring_ring + iouring_sq_array))
  229. /* the submit/completion queue entries */
  230. #define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
  231. #define EV_CQES ((struct io_uring_cqe *)((char *)iouring_ring + iouring_cq_cqes))
  232. inline_speed
  233. int
  234. iouring_enter (EV_P_ ev_tstamp timeout)
  235. {
  236. int res;
  237. EV_RELEASE_CB;
  238. res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
  239. timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
  240. assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit)));
  241. iouring_to_submit = 0;
  242. EV_ACQUIRE_CB;
  243. return res;
  244. }
  245. /* TODO: can we move things around so we don't need this forward-reference? */
  246. static void
  247. iouring_poll (EV_P_ ev_tstamp timeout);
  248. static
  249. struct io_uring_sqe *
  250. iouring_sqe_get (EV_P)
  251. {
  252. unsigned tail;
  253. for (;;)
  254. {
  255. tail = EV_SQ_VAR (tail);
  256. if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)))
  257. break; /* whats the problem, we have free sqes */
  258. /* queue full, need to flush and possibly handle some events */
  259. #if EV_FEATURE_CODE
  260. /* first we ask the kernel nicely, most often this frees up some sqes */
  261. int res = iouring_enter (EV_A_ EV_TS_CONST (0.));
  262. ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */
  263. if (res >= 0)
  264. continue; /* yes, it worked, try again */
  265. #endif
  266. /* some problem, possibly EBUSY - do the full poll and let it handle any issues */
  267. iouring_poll (EV_A_ EV_TS_CONST (0.));
  268. /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */
  269. }
  270. /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/
  271. return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
  272. }
  273. inline_size
  274. struct io_uring_sqe *
  275. iouring_sqe_submit (EV_P_ struct io_uring_sqe *sqe)
  276. {
  277. unsigned idx = sqe - EV_SQES;
  278. printf ("submit idx %d, op %d, fd %d, us5r %p, poll %d\n", idx, sqe->opcode, sqe->fd, sqe->user_data, sqe->poll_events);
  279. EV_SQ_ARRAY [idx] = idx;
  280. ECB_MEMORY_FENCE_RELEASE;
  281. ++EV_SQ_VAR (tail);
  282. /*ECB_MEMORY_FENCE_RELEASE; /* for the time being we assume this is not needed */
  283. ++iouring_to_submit;
  284. }
  285. /*****************************************************************************/
  286. /* when the timerfd expires we simply note the fact,
  287. * as the purpose of the timerfd is to wake us up, nothing else.
  288. * the next iteration should re-set it.
  289. */
  290. static void
  291. iouring_tfd_cb (EV_P_ struct ev_io *w, int revents)
  292. {
  293. iouring_tfd_to = EV_TSTAMP_HUGE;
  294. }
  295. /* called for full and partial cleanup */
  296. ecb_cold
  297. static int
  298. iouring_internal_destroy (EV_P)
  299. {
  300. close (iouring_tfd);
  301. close (iouring_fd);
  302. if (iouring_ring != MAP_FAILED) munmap (iouring_ring, iouring_ring_size);
  303. if (iouring_sqes != MAP_FAILED) munmap (iouring_sqes, iouring_sqes_size);
  304. if (ev_is_active (&iouring_tfd_w))
  305. {
  306. ev_ref (EV_A);
  307. ev_io_stop (EV_A_ &iouring_tfd_w);
  308. }
  309. }
  310. ecb_cold
  311. static int
  312. iouring_internal_init (EV_P)
  313. {
  314. struct io_uring_params params = { 0 };
  315. uint32_t sq_size, cq_size;
  316. params.flags = IORING_SETUP_CLAMP;
  317. iouring_to_submit = 0;
  318. iouring_tfd = -1;
  319. iouring_ring = MAP_FAILED;
  320. iouring_sqes = MAP_FAILED;
  321. if (!have_monotonic) /* cannot really happen, but what if11 */
  322. return -1;
  323. iouring_fd = evsys_io_uring_setup (iouring_entries, &params);
  324. if (iouring_fd < 0)
  325. return -1;
  326. if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEAT_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
  327. return -1; /* we require the above features */
  328. /* TODO: remember somehow whether our queue size has been clamped */
  329. sq_size = params.sq_off.array + params.sq_entries * sizeof (unsigned);
  330. cq_size = params.cq_off.cqes + params.cq_entries * sizeof (struct io_uring_cqe);
  331. iouring_ring_size = sq_size > cq_size ? sq_size : cq_size;
  332. iouring_sqes_size = params.sq_entries * sizeof (struct io_uring_sqe);
  333. iouring_ring = mmap (0, iouring_ring_size, PROT_READ | PROT_WRITE,
  334. MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQ_RING);
  335. iouring_sqes = mmap (0, iouring_sqes_size, PROT_READ | PROT_WRITE,
  336. MAP_SHARED | MAP_POPULATE, iouring_fd, IORING_OFF_SQES);
  337. if (iouring_ring == MAP_FAILED || iouring_sqes == MAP_FAILED)
  338. return -1;
  339. iouring_sq_head = params.sq_off.head;
  340. iouring_sq_tail = params.sq_off.tail;
  341. iouring_sq_ring_mask = params.sq_off.ring_mask;
  342. iouring_sq_ring_entries = params.sq_off.ring_entries;
  343. iouring_sq_flags = params.sq_off.flags;
  344. iouring_sq_dropped = params.sq_off.dropped;
  345. iouring_sq_array = params.sq_off.array;
  346. iouring_cq_head = params.cq_off.head;
  347. iouring_cq_tail = params.cq_off.tail;
  348. iouring_cq_ring_mask = params.cq_off.ring_mask;
  349. iouring_cq_ring_entries = params.cq_off.ring_entries;
  350. iouring_cq_overflow = params.cq_off.overflow;
  351. iouring_cq_cqes = params.cq_off.cqes;
  352. iouring_tfd_to = EV_TSTAMP_HUGE;
  353. iouring_tfd = timerfd_create (CLOCK_MONOTONIC, TFD_CLOEXEC);
  354. if (iouring_tfd < 0)
  355. return -1;
  356. return 0;
  357. }
  358. ecb_cold
  359. static void
  360. iouring_fork (EV_P)
  361. {
  362. iouring_internal_destroy (EV_A);
  363. while (iouring_internal_init (EV_A) < 0)
  364. ev_syserr ("(libev) io_uring_setup");
  365. fd_rearm_all (EV_A);
  366. ev_io_stop (EV_A_ &iouring_tfd_w);
  367. ev_io_set (EV_A_ &iouring_tfd_w, iouring_tfd, EV_READ);
  368. ev_io_start (EV_A_ &iouring_tfd_w);
  369. }
  370. /*****************************************************************************/
  371. static void
  372. iouring_modify (EV_P_ int fd, int oev, int nev)
  373. {
  374. if (oev)
  375. {
  376. /* we assume the sqe's are all "properly" initialised */
  377. struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
  378. sqe->opcode = IORING_OP_POLL_REMOVE;
  379. sqe->fd = fd;
  380. /* Jens Axboe notified me that user_data is not what is documented, but is
  381. * some kind of unique ID that has to match, otherwise the request cannot
  382. * be removed. Since we don't *really* have that, we pass in the old
  383. * generation counter - if that fails, too bad, it will hopefully be removed
  384. * at close time and then be ignored. */
  385. sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
  386. sqe->user_data = (uint64_t)-1;
  387. iouring_sqe_submit (EV_A_ sqe);
  388. /* increment generation counter to avoid handling old events */
  389. ++anfds [fd].egen;
  390. }
  391. if (nev)
  392. {
  393. struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
  394. sqe->opcode = IORING_OP_POLL_ADD;
  395. sqe->fd = fd;
  396. sqe->addr = 0;
  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. inline_size
  405. void
  406. iouring_tfd_update (EV_P_ ev_tstamp timeout)
  407. {
  408. ev_tstamp tfd_to = mn_now + timeout;
  409. /* we assume there will be many iterations per timer change, so
  410. * we only re-set the timerfd when we have to because its expiry
  411. * is too late.
  412. */
  413. if (ecb_expect_false (tfd_to < iouring_tfd_to))
  414. {
  415. struct itimerspec its;
  416. iouring_tfd_to = tfd_to;
  417. EV_TS_SET (its.it_interval, 0.);
  418. EV_TS_SET (its.it_value, tfd_to);
  419. if (timerfd_settime (iouring_tfd, TFD_TIMER_ABSTIME, &its, 0) < 0)
  420. assert (("libev: iouring timerfd_settime failed", 0));
  421. }
  422. }
  423. inline_size
  424. void
  425. iouring_process_cqe (EV_P_ struct io_uring_cqe *cqe)
  426. {
  427. int fd = cqe->user_data & 0xffffffffU;
  428. uint32_t gen = cqe->user_data >> 32;
  429. int res = cqe->res;
  430. /* user_data -1 is a remove that we are not atm. interested in */
  431. if (cqe->user_data == (uint64_t)-1)
  432. return;
  433. assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
  434. /* documentation lies, of course. the result value is NOT like
  435. * normal syscalls, but like linux raw syscalls, i.e. negative
  436. * error numbers. fortunate, as otherwise there would be no way
  437. * to get error codes at all. still, why not document this?
  438. */
  439. /* ignore event if generation doesn't match */
  440. /* other than skipping removal events, */
  441. /* this should actually be very rare */
  442. if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
  443. return;
  444. if (ecb_expect_false (res < 0))
  445. {
  446. /*TODO: EINVAL handling (was something failed with this fd)*/
  447. if (res == -EBADF)
  448. {
  449. assert (("libev: event loop rejected bad fd", res != -EBADF));
  450. fd_kill (EV_A_ fd);
  451. }
  452. else
  453. {
  454. errno = -res;
  455. ev_syserr ("(libev) IORING_OP_POLL_ADD");
  456. }
  457. return;
  458. }
  459. /* feed events, we do not expect or handle POLLNVAL */
  460. fd_event (
  461. EV_A_
  462. fd,
  463. (res & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
  464. | (res & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
  465. );
  466. /* io_uring is oneshot, so we need to re-arm the fd next iteration */
  467. /* this also means we usually have to do at least one syscall per iteration */
  468. anfds [fd].events = 0;
  469. fd_change (EV_A_ fd, EV_ANFD_REIFY);
  470. }
  471. /* called when the event queue overflows */
  472. ecb_cold
  473. static void
  474. iouring_overflow (EV_P)
  475. {
  476. /* we have two options, resize the queue (by tearing down
  477. * everything and recreating it, or living with it
  478. * and polling.
  479. * we implement this by resizing the queue, and, if that fails,
  480. * we just recreate the state on every failure, which
  481. * kind of is a very inefficient poll.
  482. * one danger is, due to the bios toward lower fds,
  483. * we will only really get events for those, so
  484. * maybe we need a poll() fallback, after all.
  485. */
  486. /*EV_CQ_VAR (overflow) = 0;*/ /* need to do this if we keep the state and poll manually */
  487. fd_rearm_all (EV_A);
  488. /* we double the size until we hit the hard-to-probe maximum */
  489. if (!iouring_max_entries)
  490. {
  491. iouring_entries <<= 1;
  492. iouring_fork (EV_A);
  493. }
  494. else
  495. {
  496. /* we hit the kernel limit, we should fall back to something else.
  497. * we can either poll() a few times and hope for the best,
  498. * poll always, or switch to epoll.
  499. * TODO: is this necessary with newer kernels?
  500. */
  501. iouring_internal_destroy (EV_A);
  502. /* this should make it so that on return, we don't call any uring functions */
  503. iouring_to_submit = 0;
  504. for (;;)
  505. {
  506. backend = epoll_init (EV_A_ 0);
  507. if (backend)
  508. break;
  509. ev_syserr ("(libev) iouring switch to epoll");
  510. }
  511. }
  512. }
  513. /* handle any events in the completion queue, return true if there were any */
  514. static int
  515. iouring_handle_cq (EV_P)
  516. {
  517. unsigned head, tail, mask;
  518. head = EV_CQ_VAR (head);
  519. ECB_MEMORY_FENCE_ACQUIRE;
  520. tail = EV_CQ_VAR (tail);
  521. if (head == tail)
  522. return 0;
  523. /* it can only overflow if we have events, yes, yes? */
  524. if (ecb_expect_false (EV_CQ_VAR (overflow)))
  525. {
  526. iouring_overflow (EV_A);
  527. return 1;
  528. }
  529. mask = EV_CQ_VAR (ring_mask);
  530. do
  531. iouring_process_cqe (EV_A_ &EV_CQES [head++ & mask]);
  532. while (head != tail);
  533. EV_CQ_VAR (head) = head;
  534. ECB_MEMORY_FENCE_RELEASE;
  535. return 1;
  536. }
  537. static void
  538. iouring_poll (EV_P_ ev_tstamp timeout)
  539. {
  540. /* if we have events, no need for extra syscalls, but we might have to queue events */
  541. /* we also clar the timeout if there are outstanding fdchanges */
  542. /* the latter should only happen if both the sq and cq are full, most likely */
  543. /* because we have a lot of event sources that immediately complete */
  544. /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
  545. if (iouring_handle_cq (EV_A) || fdchangecnt)
  546. timeout = EV_TS_CONST (0.);
  547. else
  548. /* no events, so maybe wait for some */
  549. iouring_tfd_update (EV_A_ timeout);
  550. /* only enter the kernel if we have something to submit, or we need to wait */
  551. if (timeout || iouring_to_submit)
  552. {
  553. int res = iouring_enter (EV_A_ timeout);
  554. if (ecb_expect_false (res < 0))
  555. if (errno == EINTR)
  556. /* ignore */;
  557. else if (errno == EBUSY)
  558. /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
  559. else
  560. ev_syserr ("(libev) iouring setup");
  561. else
  562. iouring_handle_cq (EV_A);
  563. }
  564. }
  565. inline_size
  566. int
  567. iouring_init (EV_P_ int flags)
  568. {
  569. iouring_entries = IOURING_INIT_ENTRIES;
  570. iouring_max_entries = 0;
  571. if (iouring_internal_init (EV_A) < 0)
  572. {
  573. iouring_internal_destroy (EV_A);
  574. return 0;
  575. }
  576. ev_io_init (&iouring_tfd_w, iouring_tfd_cb, iouring_tfd, EV_READ);
  577. ev_set_priority (&iouring_tfd_w, EV_MINPRI);
  578. ev_io_start (EV_A_ &iouring_tfd_w);
  579. ev_unref (EV_A); /* watcher should not keep loop alive */
  580. backend_modify = iouring_modify;
  581. backend_poll = iouring_poll;
  582. return EVBACKEND_IOURING;
  583. }
  584. inline_size
  585. void
  586. iouring_destroy (EV_P)
  587. {
  588. iouring_internal_destroy (EV_A);
  589. }