… | |
… | |
87 | /* TODO: resize cq/sq size independently */ |
87 | /* TODO: resize cq/sq size independently */ |
88 | |
88 | |
89 | #include <sys/timerfd.h> |
89 | #include <sys/timerfd.h> |
90 | #include <sys/mman.h> |
90 | #include <sys/mman.h> |
91 | #include <poll.h> |
91 | #include <poll.h> |
|
|
92 | #include <stdint.h> |
92 | |
93 | |
93 | #define IOURING_INIT_ENTRIES 32 |
94 | #define IOURING_INIT_ENTRIES 32 |
94 | |
95 | |
95 | /*****************************************************************************/ |
96 | /*****************************************************************************/ |
96 | /* syscall wrapdadoop - this section has the raw api/abi definitions */ |
97 | /* syscall wrapdadoop - this section has the raw api/abi definitions */ |
… | |
… | |
173 | __u32 resv[4]; |
174 | __u32 resv[4]; |
174 | struct io_sqring_offsets sq_off; |
175 | struct io_sqring_offsets sq_off; |
175 | struct io_cqring_offsets cq_off; |
176 | struct io_cqring_offsets cq_off; |
176 | }; |
177 | }; |
177 | |
178 | |
|
|
179 | #define IORING_SETUP_CQSIZE 0x00000008 |
|
|
180 | |
178 | #define IORING_OP_POLL_ADD 6 |
181 | #define IORING_OP_POLL_ADD 6 |
179 | #define IORING_OP_POLL_REMOVE 7 |
182 | #define IORING_OP_POLL_REMOVE 7 |
|
|
183 | #define IORING_OP_TIMEOUT 11 |
|
|
184 | #define IORING_OP_TIMEOUT_REMOVE 12 |
|
|
185 | |
|
|
186 | /* relative or absolute, reference clock is CLOCK_MONOTONIC */ |
|
|
187 | struct iouring_kernel_timespec |
|
|
188 | { |
|
|
189 | int64_t tv_sec; |
|
|
190 | long long tv_nsec; |
|
|
191 | }; |
|
|
192 | |
|
|
193 | #define IORING_TIMEOUT_ABS 0x00000001 |
180 | |
194 | |
181 | #define IORING_ENTER_GETEVENTS 0x01 |
195 | #define IORING_ENTER_GETEVENTS 0x01 |
182 | |
196 | |
183 | #define IORING_OFF_SQ_RING 0x00000000ULL |
197 | #define IORING_OFF_SQ_RING 0x00000000ULL |
184 | #define IORING_OFF_CQ_RING 0x08000000ULL |
198 | #define IORING_OFF_CQ_RING 0x08000000ULL |
185 | #define IORING_OFF_SQES 0x10000000ULL |
199 | #define IORING_OFF_SQES 0x10000000ULL |
186 | |
200 | |
187 | #define IORING_FEAT_SINGLE_MMAP 0x1 |
201 | #define IORING_FEAT_SINGLE_MMAP 0x00000001 |
188 | #define IORING_FEAT_NODROP 0x2 |
202 | #define IORING_FEAT_NODROP 0x00000002 |
189 | #define IORING_FEAT_SUBMIT_STABLE 0x4 |
203 | #define IORING_FEAT_SUBMIT_STABLE 0x00000004 |
190 | |
204 | |
191 | inline_size |
205 | inline_size |
192 | int |
206 | int |
193 | evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) |
207 | evsys_io_uring_setup (unsigned entries, struct io_uring_params *params) |
194 | { |
208 | { |
… | |
… | |
214 | |
228 | |
215 | /* the submit/completion queue entries */ |
229 | /* the submit/completion queue entries */ |
216 | #define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
230 | #define EV_SQES ((struct io_uring_sqe *) iouring_sqes) |
217 | #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)) |
218 | |
232 | |
|
|
233 | inline_speed |
|
|
234 | int |
|
|
235 | iouring_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? */ |
|
|
254 | static void |
|
|
255 | iouring_poll (EV_P_ ev_tstamp timeout); |
|
|
256 | |
219 | static |
257 | static |
220 | struct io_uring_sqe * |
258 | struct io_uring_sqe * |
221 | iouring_sqe_get (EV_P) |
259 | iouring_sqe_get (EV_P) |
222 | { |
260 | { |
|
|
261 | unsigned tail; |
|
|
262 | |
|
|
263 | for (;;) |
|
|
264 | { |
223 | unsigned tail = EV_SQ_VAR (tail); |
265 | tail = EV_SQ_VAR (tail); |
224 | |
266 | |
225 | 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 */ |
226 | { |
286 | } |
227 | /* queue full, flush */ |
|
|
228 | evsys_io_uring_enter (iouring_fd, iouring_to_submit, 0, 0, 0, 0); |
|
|
229 | iouring_to_submit = 0; |
|
|
230 | } |
|
|
231 | |
287 | |
232 | 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)));*/ |
233 | |
289 | |
234 | return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
290 | return EV_SQES + (tail & EV_SQ_VAR (ring_mask)); |
235 | } |
291 | } |
236 | |
292 | |
237 | inline_size |
293 | inline_size |
… | |
… | |
289 | iouring_tfd = -1; |
345 | iouring_tfd = -1; |
290 | iouring_sq_ring = MAP_FAILED; |
346 | iouring_sq_ring = MAP_FAILED; |
291 | iouring_cq_ring = MAP_FAILED; |
347 | iouring_cq_ring = MAP_FAILED; |
292 | iouring_sqes = MAP_FAILED; |
348 | iouring_sqes = MAP_FAILED; |
293 | |
349 | |
|
|
350 | if (!have_monotonic) /* cannot really happen, but what if11 */ |
|
|
351 | return -1; |
|
|
352 | |
294 | for (;;) |
353 | for (;;) |
295 | { |
354 | { |
296 | iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
355 | iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
297 | |
356 | |
298 | if (iouring_fd >= 0) |
357 | if (iouring_fd >= 0) |
299 | break; /* yippie */ |
358 | break; /* yippie */ |
300 | |
359 | |
301 | if (errno != EINVAL) |
360 | if (errno != EINVAL) |
302 | return -1; /* we failed */ |
361 | return -1; /* we failed */ |
|
|
362 | |
|
|
363 | #if TODO |
|
|
364 | if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP | IORING_FEAT_SUBMIT_STABLE)) |
|
|
365 | return -1; /* we require the above features */ |
|
|
366 | #endif |
303 | |
367 | |
304 | /* EINVAL: lots of possible reasons, but maybe |
368 | /* EINVAL: lots of possible reasons, but maybe |
305 | * it is because we hit the unqueryable hardcoded size limit |
369 | * it is because we hit the unqueryable hardcoded size limit |
306 | */ |
370 | */ |
307 | |
371 | |
… | |
… | |
378 | { |
442 | { |
379 | /* we assume the sqe's are all "properly" initialised */ |
443 | /* we assume the sqe's are all "properly" initialised */ |
380 | struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
444 | struct io_uring_sqe *sqe = iouring_sqe_get (EV_A); |
381 | sqe->opcode = IORING_OP_POLL_REMOVE; |
445 | sqe->opcode = IORING_OP_POLL_REMOVE; |
382 | 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); |
383 | sqe->user_data = -1; |
453 | sqe->user_data = (uint64_t)-1; |
384 | iouring_sqe_submit (EV_A_ sqe); |
454 | iouring_sqe_submit (EV_A_ sqe); |
385 | |
455 | |
386 | /* increment generation counter to avoid handling old events */ |
456 | /* increment generation counter to avoid handling old events */ |
387 | ++anfds [fd].egen; |
457 | ++anfds [fd].egen; |
388 | } |
458 | } |
… | |
… | |
429 | { |
499 | { |
430 | int fd = cqe->user_data & 0xffffffffU; |
500 | int fd = cqe->user_data & 0xffffffffU; |
431 | uint32_t gen = cqe->user_data >> 32; |
501 | uint32_t gen = cqe->user_data >> 32; |
432 | int res = cqe->res; |
502 | int res = cqe->res; |
433 | |
503 | |
434 | /* ignore fd removal events, if there are any. TODO: verify */ |
504 | /* user_data -1 is a remove that we are not atm. interested in */ |
435 | /* TODO: yes, this triggers */ |
|
|
436 | if (cqe->user_data == (__u64)-1) |
505 | if (cqe->user_data == (uint64_t)-1) |
437 | return; |
506 | return; |
438 | |
507 | |
439 | 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)); |
440 | |
509 | |
441 | /* documentation lies, of course. the result value is NOT like |
510 | /* documentation lies, of course. the result value is NOT like |
… | |
… | |
443 | * error numbers. fortunate, as otherwise there would be no way |
512 | * error numbers. fortunate, as otherwise there would be no way |
444 | * to get error codes at all. still, why not document this? |
513 | * to get error codes at all. still, why not document this? |
445 | */ |
514 | */ |
446 | |
515 | |
447 | /* ignore event if generation doesn't match */ |
516 | /* ignore event if generation doesn't match */ |
|
|
517 | /* other than skipping removal events, */ |
448 | /* this should actually be very rare */ |
518 | /* this should actually be very rare */ |
449 | if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
519 | if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
450 | return; |
520 | return; |
451 | |
521 | |
452 | if (ecb_expect_false (res < 0)) |
522 | if (ecb_expect_false (res < 0)) |
453 | { |
523 | { |
454 | //TODO: EINVAL handling (was something failed with this fd) |
524 | /*TODO: EINVAL handling (was something failed with this fd)*/ |
455 | //TODO: EBUSY happens when? |
525 | /*TODO: EBUSY happens when?*/ |
456 | |
526 | |
457 | if (res == -EBADF) |
527 | if (res == -EBADF) |
458 | { |
528 | { |
459 | assert (("libev: event loop rejected bad fd", res != -EBADF)); |
529 | assert (("libev: event loop rejected bad fd", res != -EBADF)); |
460 | fd_kill (EV_A_ fd); |
530 | fd_kill (EV_A_ fd); |
… | |
… | |
566 | |
636 | |
567 | static void |
637 | static void |
568 | iouring_poll (EV_P_ ev_tstamp timeout) |
638 | iouring_poll (EV_P_ ev_tstamp timeout) |
569 | { |
639 | { |
570 | /* 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 */ |
571 | if (iouring_handle_cq (EV_A)) |
645 | if (iouring_handle_cq (EV_A) || fdchangecnt) |
572 | timeout = EV_TS_CONST (0.); |
646 | timeout = EV_TS_CONST (0.); |
573 | else |
647 | else |
574 | /* no events, so maybe wait for some */ |
648 | /* no events, so maybe wait for some */ |
575 | iouring_tfd_update (EV_A_ timeout); |
649 | iouring_tfd_update (EV_A_ timeout); |
576 | |
650 | |
577 | /* 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 */ |
578 | if (timeout || iouring_to_submit) |
652 | if (timeout || iouring_to_submit) |
579 | { |
653 | { |
580 | int res; |
654 | int res = iouring_enter (EV_A_ timeout); |
581 | |
|
|
582 | EV_RELEASE_CB; |
|
|
583 | |
|
|
584 | res = evsys_io_uring_enter (iouring_fd, iouring_to_submit, 1, |
|
|
585 | timeout > EV_TS_CONST (0.) ? IORING_ENTER_GETEVENTS : 0, 0, 0); |
|
|
586 | iouring_to_submit = 0; |
|
|
587 | |
|
|
588 | EV_ACQUIRE_CB; |
|
|
589 | |
655 | |
590 | if (ecb_expect_false (res < 0)) |
656 | if (ecb_expect_false (res < 0)) |
591 | if (errno == EINTR) |
657 | if (errno == EINTR) |
592 | /* 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 */; |
593 | else |
661 | else |
594 | ev_syserr ("(libev) iouring setup"); |
662 | ev_syserr ("(libev) iouring setup"); |
595 | else |
663 | else |
596 | iouring_handle_cq (EV_A); |
664 | iouring_handle_cq (EV_A); |
597 | } |
665 | } |