… | |
… | |
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 | { |
… | |
… | |
289 | iouring_tfd = -1; |
303 | iouring_tfd = -1; |
290 | iouring_sq_ring = MAP_FAILED; |
304 | iouring_sq_ring = MAP_FAILED; |
291 | iouring_cq_ring = MAP_FAILED; |
305 | iouring_cq_ring = MAP_FAILED; |
292 | iouring_sqes = MAP_FAILED; |
306 | iouring_sqes = MAP_FAILED; |
293 | |
307 | |
|
|
308 | if (!have_monotonic) /* cannot really happen, but what if11 */ |
|
|
309 | return -1; |
|
|
310 | |
294 | for (;;) |
311 | for (;;) |
295 | { |
312 | { |
296 | iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
313 | iouring_fd = evsys_io_uring_setup (iouring_entries, ¶ms); |
297 | |
314 | |
298 | if (iouring_fd >= 0) |
315 | if (iouring_fd >= 0) |
299 | break; /* yippie */ |
316 | break; /* yippie */ |
300 | |
317 | |
301 | if (errno != EINVAL) |
318 | if (errno != EINVAL) |
302 | return -1; /* we failed */ |
319 | return -1; /* we failed */ |
|
|
320 | |
|
|
321 | #if TODO |
|
|
322 | if ((~params.features) & (IORING_FEAT_NODROP | IORING_FEATURE_SINGLE_MMAP)) |
|
|
323 | return -1; /* we require the above features */ |
|
|
324 | #endif |
303 | |
325 | |
304 | /* EINVAL: lots of possible reasons, but maybe |
326 | /* EINVAL: lots of possible reasons, but maybe |
305 | * it is because we hit the unqueryable hardcoded size limit |
327 | * it is because we hit the unqueryable hardcoded size limit |
306 | */ |
328 | */ |
307 | |
329 | |
… | |
… | |
430 | int fd = cqe->user_data & 0xffffffffU; |
452 | int fd = cqe->user_data & 0xffffffffU; |
431 | uint32_t gen = cqe->user_data >> 32; |
453 | uint32_t gen = cqe->user_data >> 32; |
432 | int res = cqe->res; |
454 | int res = cqe->res; |
433 | |
455 | |
434 | /* ignore fd removal events, if there are any. TODO: verify */ |
456 | /* ignore fd removal events, if there are any. TODO: verify */ |
|
|
457 | /* TODO: yes, this triggers */ |
435 | if (cqe->user_data == (__u64)-1) |
458 | if (cqe->user_data == (__u64)-1) |
436 | abort ();//D |
459 | return; |
437 | |
460 | |
438 | assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
461 | assert (("libev: io_uring fd must be in-bounds", fd >= 0 && fd < anfdmax)); |
439 | |
462 | |
440 | /* documentation lies, of course. the result value is NOT like |
463 | /* documentation lies, of course. the result value is NOT like |
441 | * normal syscalls, but like linux raw syscalls, i.e. negative |
464 | * normal syscalls, but like linux raw syscalls, i.e. negative |
… | |
… | |
448 | if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
471 | if (ecb_expect_false (gen != (uint32_t)anfds [fd].egen)) |
449 | return; |
472 | return; |
450 | |
473 | |
451 | if (ecb_expect_false (res < 0)) |
474 | if (ecb_expect_false (res < 0)) |
452 | { |
475 | { |
453 | //TODO: EINVAL handling (was something failed with this fd) |
476 | /*TODO: EINVAL handling (was something failed with this fd)*/ |
454 | //TODO: EBUSY happens when? |
477 | /*TODO: EBUSY happens when?*/ |
455 | |
478 | |
456 | if (res == -EBADF) |
479 | if (res == -EBADF) |
457 | { |
480 | { |
458 | assert (("libev: event loop rejected bad fd", res != -EBADF)); |
481 | assert (("libev: event loop rejected bad fd", res != -EBADF)); |
459 | fd_kill (EV_A_ fd); |
482 | fd_kill (EV_A_ fd); |
… | |
… | |
487 | iouring_overflow (EV_P) |
510 | iouring_overflow (EV_P) |
488 | { |
511 | { |
489 | /* we have two options, resize the queue (by tearing down |
512 | /* we have two options, resize the queue (by tearing down |
490 | * everything and recreating it, or living with it |
513 | * everything and recreating it, or living with it |
491 | * and polling. |
514 | * and polling. |
492 | * we implement this by resizing tghe queue, and, if that fails, |
515 | * we implement this by resizing the queue, and, if that fails, |
493 | * we just recreate the state on every failure, which |
516 | * we just recreate the state on every failure, which |
494 | * kind of is a very inefficient poll. |
517 | * kind of is a very inefficient poll. |
495 | * one danger is, due to the bios toward lower fds, |
518 | * one danger is, due to the bios toward lower fds, |
496 | * we will only really get events for those, so |
519 | * we will only really get events for those, so |
497 | * maybe we need a poll() fallback, after all. |
520 | * maybe we need a poll() fallback, after all. |
… | |
… | |
509 | else |
532 | else |
510 | { |
533 | { |
511 | /* we hit the kernel limit, we should fall back to something else. |
534 | /* we hit the kernel limit, we should fall back to something else. |
512 | * we can either poll() a few times and hope for the best, |
535 | * we can either poll() a few times and hope for the best, |
513 | * poll always, or switch to epoll. |
536 | * poll always, or switch to epoll. |
514 | * since we use epoll anyways, go epoll. |
537 | * TODO: is this necessary with newer kernels? |
515 | */ |
538 | */ |
516 | |
539 | |
517 | iouring_internal_destroy (EV_A); |
540 | iouring_internal_destroy (EV_A); |
518 | |
541 | |
519 | /* this should make it so that on return, we don'T call any uring functions */ |
542 | /* this should make it so that on return, we don't call any uring functions */ |
520 | iouring_to_submit = 0; |
543 | iouring_to_submit = 0; |
521 | |
544 | |
522 | for (;;) |
545 | for (;;) |
523 | { |
546 | { |
524 | backend = epoll_init (EV_A_ 0); |
547 | backend = epoll_init (EV_A_ 0); |