ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev_iouring.c
(Generate patch)

Comparing libev/ev_iouring.c (file contents):
Revision 1.12 by root, Fri Dec 27 22:16:10 2019 UTC vs.
Revision 1.17 by root, Sat Dec 28 07:39:18 2019 UTC

228 228
229/* the submit/completion queue entries */ 229/* the submit/completion queue entries */
230#define EV_SQES ((struct io_uring_sqe *) iouring_sqes) 230#define EV_SQES ((struct io_uring_sqe *) iouring_sqes)
231#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes)) 231#define EV_CQES ((struct io_uring_cqe *)((char *)iouring_cq_ring + iouring_cq_cqes))
232 232
233inline_speed
234int
235iouring_enter (EV_P_ ev_tstamp timeout)
236{
237 int res;
238
239 EV_RELEASE_CB;
240
241 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
242 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
243
244 assert (("libev: io_uring_enter did not consume all sqes", (res < 0 || res == iouring_to_submit)));
245
246 iouring_to_submit = 0;
247
248 EV_ACQUIRE_CB;
249
250 return res;
251}
252
253/* TODO: can we move things around so we don't need this forward-reference? */
254static void
255iouring_poll (EV_P_ ev_tstamp timeout);
256
233static 257static
234struct io_uring_sqe * 258struct io_uring_sqe *
235iouring_sqe_get (EV_P) 259iouring_sqe_get (EV_P)
236{ 260{
261 unsigned tail;
262
263 for (;;)
264 {
237 unsigned tail = EV_SQ_VAR (tail); 265 tail = EV_SQ_VAR (tail);
238 266
239 if (tail + 1 - EV_SQ_VAR (head) > EV_SQ_VAR (ring_entries)) 267 if (ecb_expect_true (tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)))
268 break; /* whats the problem, we have free sqes */
269
270 /* queue full, need to flush and possibly handle some events */
271
272#if EV_FEATURE_CODE
273 /* first we ask the kernel nicely, most often this frees up some sqes */
274 int res = iouring_enter (EV_A_ EV_TS_CONST (0.));
275
276 ECB_MEMORY_FENCE_ACQUIRE; /* better safe than sorry */
277
278 if (res >= 0)
279 continue; /* yes, it worked, try again */
280#endif
281
282 /* some problem, possibly EBUSY - do the full poll and let it handle any issues */
283
284 iouring_poll (EV_A_ EV_TS_CONST (0.));
285 /* iouring_poll should have done ECB_MEMORY_FENCE_ACQUIRE for us */
240 { 286 }
241 /* queue full, flush */
242 evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0);
243 iouring_to_submit = 0;
244 }
245 287
246 assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries))); 288 /*assert (("libev: io_uring queue full after flush", tail + 1 - EV_SQ_VAR (head) <= EV_SQ_VAR (ring_entries)));*/
247 289
248 return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); 290 return EV_SQES + (tail & EV_SQ_VAR (ring_mask));
249} 291}
250 292
251inline_size 293inline_size
317 359
318 if (errno != EINVAL) 360 if (errno != EINVAL)
319 return -1; /* we failed */ 361 return -1; /* we failed */
320 362
321#if TODO 363#if TODO
322 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP)) 364 if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE))
323 return -1; /* we require the above features */ 365 return -1; /* we require the above features */
324#endif 366#endif
325 367
326 /* EINVAL: lots of possible reasons, but maybe 368 /* EINVAL: lots of possible reasons, but maybe
327 * it is because we hit the unqueryable hardcoded size limit 369 * it is because we hit the unqueryable hardcoded size limit
400 { 442 {
401 /* we assume the sqe's are all "properly" initialised */ 443 /* we assume the sqe's are all "properly" initialised */
402 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); 444 struct io_uring_sqe *sqe = iouring_sqe_get (EV_A);
403 sqe->opcode = IORING_OP_POLL_REMOVE; 445 sqe->opcode = IORING_OP_POLL_REMOVE;
404 sqe->fd = fd; 446 sqe->fd = fd;
447 /* Jens Axboe notified me that user_data is not what is documented, but is
448 * some kind of unique ID that has to match, otherwise the request cannot
449 * be removed. Since we don't *really* have that, we pass in the old
450 * generation counter - if that fails, too bad, it will hopefully be removed
451 * at close time and then be ignored. */
452 sqe->addr = (uint32_t)fd | ((__u64)(uint32_t)anfds [fd].egen << 32);
405 sqe->user_data = -1; 453 sqe->user_data = (uint64_t)-1;
406 iouring_sqe_submit (EV_A_ sqe); 454 iouring_sqe_submit (EV_A_ sqe);
407 455
408 /* increment generation counter to avoid handling old events */ 456 /* increment generation counter to avoid handling old events */
409 ++anfds [fd].egen; 457 ++anfds [fd].egen;
410 } 458 }
451{ 499{
452 int fd = cqe->user_data & 0xffffffffU; 500 int fd = cqe->user_data & 0xffffffffU;
453 uint32_t gen = cqe->user_data >> 32; 501 uint32_t gen = cqe->user_data >> 32;
454 int res = cqe->res; 502 int res = cqe->res;
455 503
456 /* ignore fd removal events, if there are any. TODO: verify */ 504 /* user_data -1 is a remove that we are not atm. interested in */
457 /* TODO: yes, this triggers */
458 if (cqe->user_data == (__u64)-1) 505 if (cqe->user_data == (uint64_t)-1)
459 return; 506 return;
460 507
461 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); 508 assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax));
462 509
463 /* documentation lies, of course. the result value is NOT like 510 /* documentation lies, of course. the result value is NOT like
465 * error numbers. fortunate, as otherwise there would be no way 512 * error numbers. fortunate, as otherwise there would be no way
466 * to get error codes at all. still, why not document this? 513 * to get error codes at all. still, why not document this?
467 */ 514 */
468 515
469 /* ignore event if generation doesn't match */ 516 /* ignore event if generation doesn't match */
517 /* other than skipping removal events, */
470 /* this should actually be very rare */ 518 /* this should actually be very rare */
471 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) 519 if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen))
472 return; 520 return;
473 521
474 if (ecb_expect_false (res < 0)) 522 if (ecb_expect_false (res < 0))
588 636
589static void 637static void
590iouring_poll (EV_P_ ev_tstamp timeout) 638iouring_poll (EV_P_ ev_tstamp timeout)
591{ 639{
592 /* if we have events, no need for extra syscalls, but we might have to queue events */ 640 /* if we have events, no need for extra syscalls, but we might have to queue events */
641 /* we also clar the timeout if there are outstanding fdchanges */
642 /* the latter should only happen if both the sq and cq are full, most likely */
643 /* because we have a lot of event sources that immediately complete */
644 /* TODO: fdchacngecnt is always 0 because fd_reify does not have two buffers yet */
593 if (iouring_handle_cq (EV_A)) 645 if (iouring_handle_cq (EV_A) || fdchangecnt)
594 timeout = EV_TS_CONST (0.); 646 timeout = EV_TS_CONST (0.);
595 else 647 else
596 /* no events, so maybe wait for some */ 648 /* no events, so maybe wait for some */
597 iouring_tfd_update (EV_A_ timeout); 649 iouring_tfd_update (EV_A_ timeout);
598 650
599 /* only enter the kernel if we have something to submit, or we need to wait */ 651 /* only enter the kernel if we have something to submit, or we need to wait */
600 if (timeout || iouring_to_submit) 652 if (timeout || iouring_to_submit)
601 { 653 {
602 int res; 654 int res = iouring_enter (EV_A_ timeout);
603
604 EV_RELEASE_CB;
605
606 res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1,
607 timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0);
608 iouring_to_submit = 0;
609
610 EV_ACQUIRE_CB;
611 655
612 if (ecb_expect_false (res < 0)) 656 if (ecb_expect_false (res < 0))
613 if (errno == EINTR) 657 if (errno == EINTR)
614 /* ignore */; 658 /* ignore */;
659 else if (errno == EBUSY)
660 /* cq full, cannot submit - should be rare because we flush the cq first, so simply ignore */;
615 else 661 else
616 ev_syserr ("(libev) iouring setup"); 662 ev_syserr ("(libev) iouring setup");
617 else 663 else
618 iouring_handle_cq (EV_A); 664 iouring_handle_cq (EV_A);
619 } 665 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines