-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add io_uring event loop backend #813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+278
−3
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ca703bf
Initial plan
Copilot 5cada92
Add io_uring event loop backend support using liburing
Copilot e7acef2
Address code review: fix sqe variable scope, improve error message
Copilot 6113c31
Restrict WITH_IO_URING option to Linux platform only
Copilot 95c4a6d
Apply review suggestions to io_uring backend
Copilot a717320
Add clarifying comment for error event mapping in io_uring backend
Copilot 10aba51
Fix io_uring.c build: use portable liburing APIs for older versions
Copilot bcb6f28
Compile event/io_uring.c with -std=gnu99 for liburing header compatib…
Copilot 800e971
Use -std=gnu99 globally on Linux instead of per-file override for io_…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ dist | |
| test | ||
| *_test | ||
| build | ||
| build-* | ||
| config.mk | ||
| hconfig.h | ||
| html/uploads | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,3 +38,6 @@ WITH_MBEDTLS=no | |
|
|
||
| # rudp | ||
| WITH_KCP=no | ||
|
|
||
| # event | ||
| WITH_IO_URING=no | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| #include "iowatcher.h" | ||
|
|
||
| #ifdef EVENT_IO_URING | ||
| #include "hplatform.h" | ||
| #include "hdef.h" | ||
| #include "hevent.h" | ||
|
|
||
| #include <liburing.h> | ||
| #include <poll.h> | ||
|
|
||
| #define IO_URING_ENTRIES 1024 | ||
| #define IO_URING_CANCEL_TAG ((void*)(uintptr_t)-1) | ||
|
|
||
| typedef struct io_uring_ctx_s { | ||
| struct io_uring ring; | ||
| int nfds; | ||
| } io_uring_ctx_t; | ||
|
|
||
| int iowatcher_init(hloop_t* loop) { | ||
| if (loop->iowatcher) return 0; | ||
| io_uring_ctx_t* ctx; | ||
| HV_ALLOC_SIZEOF(ctx); | ||
| int ret = io_uring_queue_init(IO_URING_ENTRIES, &ctx->ring, 0); | ||
| if (ret < 0) { | ||
| HV_FREE(ctx); | ||
| return ret; | ||
| } | ||
| ctx->nfds = 0; | ||
| loop->iowatcher = ctx; | ||
| return 0; | ||
| } | ||
|
|
||
| int iowatcher_cleanup(hloop_t* loop) { | ||
| if (loop->iowatcher == NULL) return 0; | ||
| io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; | ||
| io_uring_queue_exit(&ctx->ring); | ||
| HV_FREE(loop->iowatcher); | ||
| return 0; | ||
| } | ||
|
|
||
| static struct io_uring_sqe* io_uring_get_sqe_safe(struct io_uring* ring) { | ||
| struct io_uring_sqe* sqe = io_uring_get_sqe(ring); | ||
| if (sqe == NULL) { | ||
| // SQ is full, flush pending submissions and retry | ||
| io_uring_submit(ring); | ||
| sqe = io_uring_get_sqe(ring); | ||
| } | ||
| return sqe; | ||
| } | ||
|
|
||
| int iowatcher_add_event(hloop_t* loop, int fd, int events) { | ||
| if (loop->iowatcher == NULL) { | ||
| int ret = iowatcher_init(loop); | ||
| if (ret < 0) { | ||
| return ret; | ||
| } | ||
| } | ||
| io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; | ||
| hio_t* io = loop->ios.ptr[fd]; | ||
|
|
||
| unsigned poll_mask = 0; | ||
| // pre events | ||
| if (io->events & HV_READ) { | ||
| poll_mask |= POLLIN; | ||
| } | ||
| if (io->events & HV_WRITE) { | ||
| poll_mask |= POLLOUT; | ||
| } | ||
| // now events | ||
| if (events & HV_READ) { | ||
| poll_mask |= POLLIN; | ||
| } | ||
| if (events & HV_WRITE) { | ||
| poll_mask |= POLLOUT; | ||
| } | ||
|
|
||
| struct io_uring_sqe* sqe; | ||
| if (io->events != 0) { | ||
| // Cancel the existing poll request first | ||
| sqe = io_uring_get_sqe_safe(&ctx->ring); | ||
| if (sqe == NULL) return -1; | ||
| io_uring_prep_poll_remove(sqe, (uint64_t)fd); | ||
| io_uring_sqe_set_data(sqe, IO_URING_CANCEL_TAG); | ||
| } else { | ||
| ctx->nfds++; | ||
| } | ||
|
|
||
| // Add poll for the combined events | ||
| sqe = io_uring_get_sqe_safe(&ctx->ring); | ||
| if (sqe == NULL) return -1; | ||
| io_uring_prep_poll_add(sqe, fd, poll_mask); | ||
| io_uring_sqe_set_data(sqe, (void*)(uintptr_t)fd); | ||
|
|
||
| io_uring_submit(&ctx->ring); | ||
| return 0; | ||
| } | ||
|
|
||
| int iowatcher_del_event(hloop_t* loop, int fd, int events) { | ||
| io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; | ||
| if (ctx == NULL) return 0; | ||
| hio_t* io = loop->ios.ptr[fd]; | ||
|
|
||
| // Calculate remaining events | ||
| unsigned poll_mask = 0; | ||
| // pre events | ||
| if (io->events & HV_READ) { | ||
| poll_mask |= POLLIN; | ||
| } | ||
| if (io->events & HV_WRITE) { | ||
| poll_mask |= POLLOUT; | ||
| } | ||
| // now events | ||
| if (events & HV_READ) { | ||
| poll_mask &= ~POLLIN; | ||
| } | ||
| if (events & HV_WRITE) { | ||
| poll_mask &= ~POLLOUT; | ||
| } | ||
|
|
||
| // Cancel existing poll | ||
| struct io_uring_sqe* sqe = io_uring_get_sqe_safe(&ctx->ring); | ||
| if (sqe == NULL) return -1; | ||
| io_uring_prep_poll_remove(sqe, (uint64_t)fd); | ||
| io_uring_sqe_set_data(sqe, IO_URING_CANCEL_TAG); | ||
|
|
||
| if (poll_mask == 0) { | ||
| ctx->nfds--; | ||
| } else { | ||
| // Re-add with remaining events | ||
| sqe = io_uring_get_sqe_safe(&ctx->ring); | ||
| if (sqe == NULL) return -1; | ||
| io_uring_prep_poll_add(sqe, fd, poll_mask); | ||
| io_uring_sqe_set_data(sqe, (void*)(uintptr_t)fd); | ||
| } | ||
|
|
||
| io_uring_submit(&ctx->ring); | ||
| return 0; | ||
| } | ||
|
|
||
| int iowatcher_poll_events(hloop_t* loop, int timeout) { | ||
| io_uring_ctx_t* ctx = (io_uring_ctx_t*)loop->iowatcher; | ||
| if (ctx == NULL) return 0; | ||
| if (ctx->nfds == 0) return 0; | ||
|
|
||
| struct __kernel_timespec ts; | ||
| struct __kernel_timespec* tp = NULL; | ||
| if (timeout != INFINITE) { | ||
| ts.tv_sec = timeout / 1000; | ||
| ts.tv_nsec = (timeout % 1000) * 1000000LL; | ||
| tp = &ts; | ||
| } | ||
|
|
||
| struct io_uring_cqe* cqe; | ||
| int ret; | ||
| if (tp) { | ||
| ret = io_uring_wait_cqe_timeout(&ctx->ring, &cqe, tp); | ||
| } else { | ||
| ret = io_uring_wait_cqe(&ctx->ring, &cqe); | ||
| } | ||
| if (ret < 0) { | ||
| if (ret == -ETIME || ret == -EINTR) { | ||
| return 0; | ||
| } | ||
| perror("io_uring_wait_cqe"); | ||
| return ret; | ||
| } | ||
|
|
||
| int nevents = 0; | ||
| int sqe_queued = 0; | ||
| unsigned nready = io_uring_cq_ready(&ctx->ring); | ||
| unsigned i; | ||
| for (i = 0; i < nready; ++i) { | ||
| if (io_uring_peek_cqe(&ctx->ring, &cqe) != 0) break; | ||
| void* data = io_uring_cqe_get_data(cqe); | ||
| if (data == IO_URING_CANCEL_TAG) { | ||
| io_uring_cqe_seen(&ctx->ring, cqe); | ||
| continue; | ||
| } | ||
|
|
||
| int fd = (int)(uintptr_t)data; | ||
| if (fd < 0 || fd >= loop->ios.maxsize) { | ||
| io_uring_cqe_seen(&ctx->ring, cqe); | ||
| continue; | ||
| } | ||
| hio_t* io = loop->ios.ptr[fd]; | ||
| if (io == NULL) { | ||
| io_uring_cqe_seen(&ctx->ring, cqe); | ||
| continue; | ||
| } | ||
|
|
||
| if (cqe->res < 0) { | ||
| // Poll request failed: notify registered events, or both if none registered | ||
| io->revents |= (io->events ? io->events : HV_RDWR); | ||
| EVENT_PENDING(io); | ||
| ++nevents; | ||
| } else { | ||
| int revents = cqe->res; | ||
| if (revents & (POLLIN | POLLHUP | POLLERR)) { | ||
| io->revents |= HV_READ; | ||
| } | ||
| if (revents & (POLLOUT | POLLHUP | POLLERR)) { | ||
| io->revents |= HV_WRITE; | ||
| } | ||
| if (io->revents) { | ||
| EVENT_PENDING(io); | ||
| ++nevents; | ||
| } | ||
| } | ||
|
|
||
| io_uring_cqe_seen(&ctx->ring, cqe); | ||
|
|
||
| // io_uring POLL_ADD is one-shot, re-arm for the same events | ||
| unsigned remask = 0; | ||
| if (io->events & HV_READ) remask |= POLLIN; | ||
| if (io->events & HV_WRITE) remask |= POLLOUT; | ||
| if (remask) { | ||
| struct io_uring_sqe* sqe = io_uring_get_sqe_safe(&ctx->ring); | ||
| if (sqe) { | ||
| io_uring_prep_poll_add(sqe, fd, remask); | ||
| io_uring_sqe_set_data(sqe, (void*)(uintptr_t)fd); | ||
| sqe_queued = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (sqe_queued) { | ||
| io_uring_submit(&ctx->ring); | ||
| } | ||
|
|
||
| return nevents; | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,5 +98,6 @@ | |
|
|
||
| #cmakedefine WITH_WEPOLL 1 | ||
| #cmakedefine WITH_KCP 1 | ||
| #cmakedefine WITH_IO_URING 1 | ||
|
|
||
| #endif // HV_CONFIG_H_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LIBSunconditionally appendsuringwhenWITH_IO_URINGis ON. Since liburing is Linux-only, enabling this option on non-Linux platforms will fail at link time; consider guarding this with a Linux check (e.g.if(CMAKE_SYSTEM_NAME STREQUAL "Linux")) and emitting a clear configuration error otherwise.