-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmod.rs
More file actions
71 lines (60 loc) · 2.01 KB
/
mod.rs
File metadata and controls
71 lines (60 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#[cfg(feature = "once_cell_try")]
use std::sync::OnceLock;
use io_uring::squeue::Entry;
use linux_raw_sys::io_uring::{IORING_RECVSEND_POLL_FIRST, io_uring_sqe};
#[cfg(not(feature = "once_cell_try"))]
use once_cell::sync::OnceCell as OnceLock;
pub fn is_op_supported(code: u8) -> bool {
static PROBE: OnceLock<io_uring::Probe> = OnceLock::new();
PROBE
.get_or_try_init(|| {
let mut probe = io_uring::Probe::new();
io_uring::IoUring::new(2)?
.submitter()
.register_probe(&mut probe)?;
std::io::Result::Ok(probe)
})
.map(|probe| probe.is_supported(code))
.unwrap_or_default()
}
/// The kernel version of Linux.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct KernelVersion {
pub major: u8,
pub minor: u8,
}
impl From<(u8, u8)> for KernelVersion {
fn from((major, minor): (u8, u8)) -> Self {
Self { major, minor }
}
}
/// Returns the kernel version of Linux, or `None` if it cannot be determined.
fn kernel_version() -> Option<KernelVersion> {
static VERSION: OnceLock<Option<KernelVersion>> = OnceLock::new();
*VERSION.get_or_init(|| {
let info = rustix::system::uname();
let version = info.release().to_str().ok()?;
let mut parts = version.split('.');
let major = parts.next()?.parse().ok()?;
let minor = parts.next()?.parse().ok()?;
Some(KernelVersion { major, minor })
})
}
pub fn is_kernel_at_least(v: impl Into<KernelVersion>) -> bool {
kernel_version()
.map(|kv| kv >= v.into())
.unwrap_or_default()
}
pub(crate) fn set_poll_first(mut entry: Entry, flag: bool) -> Entry {
let version = match entry.get_opcode() as u8 {
io_uring::opcode::Accept::CODE => (6, 10),
_ => (5, 19),
};
if flag && is_kernel_at_least(version) {
let sqe = &raw mut entry as *mut io_uring_sqe;
unsafe {
(*sqe).ioprio |= IORING_RECVSEND_POLL_FIRST as u16;
}
}
entry
}