Skip to main content

gpu_async_reactor/host/
uring.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! io_uring: an [`Idle`] whose deadline sleep is one `IORING_OP_TIMEOUT` with a nanosecond
4//! timespec (no timer-slack rounding, and the same ring can carry network I/O), and whose line
5//! park is the [`StdIdle`] park (the CPU's word wait). The executor kind `io-uring`.
6
7use io_uring::{IoUring, opcode, types};
8
9use crate::core::line::CompletionLine;
10use crate::core::reactor::Idle;
11use crate::core::time::{Clock, DurNs, MonoNs};
12use crate::host::std_host::{StdClock, StdIdle};
13
14/// `ETIME`: the timeout expired (the expected completion of a timeout op).
15const ETIME: i32 = 62;
16
17/// The io_uring idle.
18pub struct UringIdle {
19    ring: IoUring,
20    clock: StdClock,
21    park: StdIdle,
22    failed_timeouts: u64,
23}
24
25impl UringIdle {
26    /// A ring of 8 entries, or why the host cannot make one (kernel without io_uring, seccomp,
27    /// `io_uring_disabled`).
28    ///
29    /// # Errors
30    /// The `io_uring_setup` error.
31    pub fn new(clock: StdClock, park: StdIdle) -> std::io::Result<Self> {
32        Ok(Self {
33            ring: IoUring::new(8)?,
34            clock,
35            park,
36            failed_timeouts: 0,
37        })
38    }
39
40    /// Timeout ops the ring refused; each such sleep ran as a thread sleep instead (reported,
41    /// never silent: a host exports this next to its executor label).
42    #[must_use]
43    pub fn failed_timeouts(&self) -> u64 {
44        self.failed_timeouts
45    }
46
47    /// Whether this host can set up an io_uring (`Err` = why not), probed once.
48    ///
49    /// # Errors
50    /// The `io_uring_setup` error.
51    pub fn probe() -> std::io::Result<()> {
52        IoUring::new(2).map(drop)
53    }
54
55    fn timeout(&mut self, ns: u64) -> std::io::Result<()> {
56        let ts = types::Timespec::new()
57            .sec(ns / 1_000_000_000)
58            .nsec(u32::try_from(ns % 1_000_000_000).unwrap_or(0));
59        let sqe = opcode::Timeout::new(&raw const ts).build();
60        // WHY: submits the timeout entry that bounds the idle on the io_uring.
61        // SAFETY: the entry points at `ts`, which lives until `submit_and_wait` below returns with
62        // the entry's completion; the ring is ours alone and was just drained.
63        // NO SAFE ALTERNATIVE: `SubmissionQueue::push` is unsafe because the kernel reads the entry's pointers after it returns.
64        unsafe { self.ring.submission().push(&sqe) }
65            .map_err(|_| std::io::Error::other("submission queue full"))?;
66        self.ring.submit_and_wait(1)?;
67        let cqe = self.ring.completion().next();
68        match cqe.map(|c| c.result()) {
69            Some(r) if r == -ETIME || r >= 0 => Ok(()),
70            Some(r) => Err(std::io::Error::from_raw_os_error(-r)),
71            None => Ok(()),
72        }
73    }
74}
75
76impl Idle for UringIdle {
77    fn sleep_until(&mut self, at: MonoNs) {
78        let left = at.since(self.clock.monotonic_ns());
79        if left.0 > 0 && self.timeout(left.0).is_err() {
80            self.failed_timeouts += 1;
81            self.park.sleep_until(at);
82        }
83    }
84
85    fn park_line<R: Fn(&CompletionLine) -> bool>(
86        &mut self,
87        line: &CompletionLine,
88        ready: R,
89        bound: DurNs,
90    ) -> bool {
91        self.park.park_line(line, ready, bound)
92    }
93}