Skip to content

Add SubmitArgs::min_wait_usec (kernel 6.12) #315

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions io-uring-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn test<S: squeue::EntryMarker, C: cqueue::EntryMarker>(
tests::timeout::test_timeout_cancel(&mut ring, &test)?;
tests::timeout::test_timeout_abs(&mut ring, &test)?;
tests::timeout::test_timeout_submit_args(&mut ring, &test)?;
tests::timeout::test_timeout_submit_args_min_wait(&mut ring, &test)?;

// net
tests::net::test_tcp_write_read(&mut ring, &test)?;
Expand Down
52 changes: 52 additions & 0 deletions io-uring-test/src/tests/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,55 @@ pub fn test_timeout_submit_args<S: squeue::EntryMarker, C: cqueue::EntryMarker>(

Ok(())
}

pub fn test_timeout_submit_args_min_wait<S: squeue::EntryMarker, C: cqueue::EntryMarker>(
ring: &mut IoUring<S, C>,
test: &Test,
) -> anyhow::Result<()> {
require! {
test;
ring.params().is_feature_ext_arg();
ring.params().is_feature_min_timeout();
};

println!("test timeout_submit_args_min_wait");

let ts = types::Timespec::new().sec(2);
let args = types::SubmitArgs::new()
.timespec(&ts)
.min_wait_usec(1_000_000);

// timeout

let start = Instant::now();
match ring.submitter().submit_with_args(2, &args) {
Ok(_) => panic!(),
Err(ref err) if err.raw_os_error() == Some(libc::ETIME) => (),
Err(err) => return Err(err.into()),
}
assert_eq!(start.elapsed().as_secs(), 2);

assert!(ring.completion().next().is_none());

// no timeout

let nop_e = opcode::Nop::new();

unsafe {
ring.submission()
.push(&nop_e.build().user_data(0x1d).into())
.expect("queue is full");
}

let start = Instant::now();
ring.submitter().submit_with_args(2, &args)?;
assert_eq!(start.elapsed().as_secs(), 1);

let cqes: Vec<cqueue::Entry> = ring.completion().map(Into::into).collect();

assert_eq!(cqes.len(), 1);
assert_eq!(cqes[0].user_data(), 0x1d);
assert_eq!(cqes[0].result(), 0);

Ok(())
}
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,14 @@ impl Parameters {
self.0.features & sys::IORING_FEAT_RECVSEND_BUNDLE != 0
}

/// If this flag is set, applications can use the
/// [`SubmitArgs::min_wait_usec`](types::SubmitArgs::min_wait_usec) method to specify a timeout
/// after which the kernel will return as soon as a single completion is received instead of
/// waiting for the minimum specified by the application. Available since kernel 6.12.
pub fn is_feature_min_timeout(&self) -> bool {
self.0.features & sys::IORING_FEAT_MIN_TIMEOUT != 0
}

/// The number of submission queue entries allocated.
pub fn sq_entries(&self) -> u32 {
self.0.sq_entries
Expand Down
18 changes: 17 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<'prev, 'now> SubmitArgs<'prev, 'now> {
let args = sys::io_uring_getevents_arg {
sigmask: 0,
sigmask_sz: 0,
pad: 0,
min_wait_usec: 0,
ts: 0,
};

Expand All @@ -262,6 +262,22 @@ impl<'prev, 'now> SubmitArgs<'prev, 'now> {
}
}

/// Sets a timeout in microseconds to start waiting for a minimum of a single completion.
///
/// Once the timeout expires, the kernel will return when a single completion has been received
/// instead of waiting for the minimum amount of completions specified by the `want` parameter
/// in the call to [`Submitter::submit_and_wait`](crate::Submitter::submit_and_wait) or
/// [`Submitter::submit_with_args`](crate::Submitter::submit_with_args).
///
/// Available since 6.12. Use the
/// [`Parameters::is_feature_min_timeout`](crate::Parameters::is_feature_min_timeout) method to
/// check for availability.
#[inline]
pub fn min_wait_usec(mut self, min_wait_usec: u32) -> Self {
self.args.min_wait_usec = min_wait_usec;
self
}

#[inline]
pub fn timespec<'new>(mut self, timespec: &'new Timespec) -> SubmitArgs<'now, 'new> {
self.args.ts = cast_ptr(timespec) as _;
Expand Down
Loading