Skip to content

Commit

Permalink
feat: support timeout with enter args
Browse files Browse the repository at this point in the history
  • Loading branch information
ihciah committed Jul 11, 2023
1 parent f891b70 commit 2ff01fa
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 15 deletions.
23 changes: 16 additions & 7 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,34 @@ version = "0.0.0"
# [dependencies] instead. In additional, if you want to know how runtime
# works, you can enable "debug" feature.
[dev-dependencies]
monoio = {path = "../monoio", default-features = false, features = ["async-cancel", "sync", "bytes", "iouring", "legacy", "macros", "utils"]}
monoio = { path = "../monoio", default-features = false, features = [
"async-cancel",
"sync",
"bytes",
"iouring",
"legacy",
"macros",
"utils",
"enter-args",
] }

# Enable tracing and tracing-subscriber for print out runtime debug
# tracing information. Add these only when you enable "debug" feature.
# tracing = "0.1"
# tracing-subscriber = "0.3"

# For hyper examples
hyper = {version = "0.14", features = ["http1", "client", "server", "stream"]}
hyper = { version = "0.14", features = ["http1", "client", "server", "stream"] }

# For h2 examples
bytes = {version = "1"}
h2 = {version = "0.3"}
http = {version = "0.2"}
bytes = { version = "1" }
h2 = { version = "0.3" }
http = { version = "0.2" }

# For hyper and h2 examples
monoio-compat = {path = "../monoio-compat"}
monoio-compat = { path = "../monoio-compat" }

tokio = {version = "1", default-features = false, features = ["io-util"]}
tokio = { version = "1", default-features = false, features = ["io-util"] }
tower-service = "0.3"

futures = "0.3"
Expand Down
6 changes: 5 additions & 1 deletion monoio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ async-cancel = []
# enanle zero copy(enable SOCK_ZEROCOPY + MSG_ZEROCOPY flag)
# WARNING: this feature may cause performance degradation
zero-copy = []
# splice op(require kernel 5.7+)
# will use enter+args to park with timeout on uring driver
# better performance but requires 5.11+
# has no effect on legacy driver
enter-args = []
# splice op(requires kernel 5.7+)
splice = []
# enable `async main` macros support
macros = ["monoio-macros"]
Expand Down
35 changes: 28 additions & 7 deletions monoio/src/driver/uring/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
time::Duration,
};

use io_uring::{cqueue, opcode, types::Timespec, IoUring};
use io_uring::{cqueue, types::Timespec, IoUring};
use lifecycle::Lifecycle;

use super::{
Expand All @@ -28,6 +28,7 @@ pub(crate) use waker::UnparkHandle;

#[allow(unused)]
pub(crate) const CANCEL_USERDATA: u64 = u64::MAX;
#[cfg(not(feature = "enter-args"))]
pub(crate) const TIMEOUT_USERDATA: u64 = u64::MAX - 1;
#[allow(unused)]
pub(crate) const EVENTFD_USERDATA: u64 = u64::MAX - 2;
Expand Down Expand Up @@ -160,7 +161,7 @@ impl IoUringDriver {

#[cfg(feature = "sync")]
fn install_eventfd(&self, inner: &mut UringInner, fd: RawFd) {
let entry = opcode::Read::new(io_uring::types::Fd(fd), self.eventfd_read_dst, 8)
let entry = io_uring::opcode::Read::new(io_uring::types::Fd(fd), self.eventfd_read_dst, 8)
.build()
.user_data(EVENTFD_USERDATA);

Expand All @@ -169,12 +170,13 @@ impl IoUringDriver {
inner.eventfd_installed = true;
}

#[cfg(not(feature = "enter-args"))]
fn install_timeout(&self, inner: &mut UringInner, duration: Duration) {
let timespec = timespec(duration);
unsafe {
std::ptr::replace(self.timespec, timespec);
}
let entry = opcode::Timeout::new(self.timespec as *const Timespec)
let entry = io_uring::opcode::Timeout::new(self.timespec as *const Timespec)
.build()
.user_data(TIMEOUT_USERDATA);

Expand Down Expand Up @@ -233,11 +235,30 @@ impl IoUringDriver {
self.install_eventfd(inner, inner.shared_waker.as_raw_fd());
}
if let Some(duration) = timeout {
self.install_timeout(inner, duration);
}
// Submit and Wait with timeout in an TimeoutOp way.
// Better compatibility(5.4+).
#[cfg(not(feature = "enter-args"))]
{
self.install_timeout(inner, duration);
inner.uring.submit_and_wait(1)?;
}

// Submit and Wait
inner.uring.submit_and_wait(1)?;
// Submit and Wait with enter args.
// Better performance(5.11+).
#[cfg(feature = "enter-args")]
{
let timespec = timespec(duration);
let args = io_uring::types::SubmitArgs::new().timespec(&timespec);
if let Err(e) = inner.uring.submitter().submit_with_args(1, &args) {
if e.raw_os_error() != Some(libc::ETIME) {
return Err(e);
}
}
}
} else {
// Submit and Wait without timeout
inner.uring.submit_and_wait(1)?;
}
} else {
// Submit only
inner.uring.submit()?;
Expand Down

0 comments on commit 2ff01fa

Please sign in to comment.