Skip to content

Commit 72664b6

Browse files
authored
Merge pull request #292 from George-Miao/fix/driver-type
2 parents 111c8d0 + 2eb98d5 commit 72664b6

File tree

5 files changed

+130
-130
lines changed

5 files changed

+130
-130
lines changed

compio-driver/src/driver_type.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
use std::sync::atomic::{AtomicU8, Ordering};
2+
3+
const UNINIT: u8 = u8::MAX;
4+
const IO_URING: u8 = 0;
5+
const POLLING: u8 = 1;
6+
const IOCP: u8 = 2;
7+
8+
static DRIVER_TYPE: AtomicU8 = AtomicU8::new(UNINIT);
9+
10+
/// Representing underlying driver type the fusion driver is using
11+
#[repr(u8)]
12+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13+
pub enum DriverType {
14+
/// Using `polling` driver
15+
Poll = POLLING,
16+
17+
/// Using `io-uring` driver
18+
IoUring = IO_URING,
19+
20+
/// Using `iocp` driver
21+
IOCP = IOCP,
22+
}
23+
24+
impl DriverType {
25+
fn from_num(n: u8) -> Self {
26+
match n {
27+
IO_URING => Self::IoUring,
28+
POLLING => Self::Poll,
29+
IOCP => Self::IOCP,
30+
_ => unreachable!("invalid driver type"),
31+
}
32+
}
33+
34+
/// Get the underlying driver type
35+
fn get() -> DriverType {
36+
cfg_if::cfg_if! {
37+
if #[cfg(windows)] {
38+
DriverType::IOCP
39+
} else if #[cfg(all(target_os = "linux", feature = "polling", feature = "io-uring"))] {
40+
use io_uring::opcode::*;
41+
42+
// Add more opcodes here if used
43+
const USED_OP: &[u8] = &[
44+
Read::CODE,
45+
Readv::CODE,
46+
Write::CODE,
47+
Writev::CODE,
48+
Fsync::CODE,
49+
Accept::CODE,
50+
Connect::CODE,
51+
RecvMsg::CODE,
52+
SendMsg::CODE,
53+
AsyncCancel::CODE,
54+
OpenAt::CODE,
55+
Close::CODE,
56+
Shutdown::CODE,
57+
// Linux kernel 5.19
58+
#[cfg(any(
59+
feature = "io-uring-sqe128",
60+
feature = "io-uring-cqe32",
61+
feature = "io-uring-socket"
62+
))]
63+
Socket::CODE,
64+
];
65+
66+
(|| {
67+
let uring = io_uring::IoUring::new(2)?;
68+
let mut probe = io_uring::Probe::new();
69+
uring.submitter().register_probe(&mut probe)?;
70+
if USED_OP.iter().all(|op| probe.is_supported(*op)) {
71+
std::io::Result::Ok(DriverType::IoUring)
72+
} else {
73+
Ok(DriverType::Poll)
74+
}
75+
})()
76+
.unwrap_or(DriverType::Poll) // Should we fail here?
77+
} else if #[cfg(all(target_os = "linux", feature = "io-uring"))] {
78+
DriverType::IoUring
79+
} else if #[cfg(unix)] {
80+
DriverType::Poll
81+
} else {
82+
compile_error!("unsupported platform");
83+
}
84+
}
85+
}
86+
87+
/// Get the underlying driver type and cache it. Following calls will return
88+
/// the cached value.
89+
pub fn current() -> DriverType {
90+
match DRIVER_TYPE.load(Ordering::Acquire) {
91+
UNINIT => {}
92+
x => return DriverType::from_num(x),
93+
}
94+
let dev_ty = Self::get();
95+
96+
DRIVER_TYPE.store(dev_ty as u8, Ordering::Release);
97+
98+
dev_ty
99+
}
100+
101+
/// Check if the current driver is `polling`
102+
pub fn is_polling() -> bool {
103+
Self::current() == DriverType::Poll
104+
}
105+
106+
/// Check if the current driver is `io-uring`
107+
pub fn is_iouring() -> bool {
108+
Self::current() == DriverType::IoUring
109+
}
110+
111+
/// Check if the current driver is `iocp`
112+
pub fn is_iocp() -> bool {
113+
Self::current() == DriverType::IOCP
114+
}
115+
}

compio-driver/src/fusion/mod.rs

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,99 +10,13 @@ pub(crate) mod op;
1010
pub use std::os::fd::{AsRawFd, OwnedFd, RawFd};
1111
use std::{io, task::Poll, time::Duration};
1212

13-
pub use driver_type::DriverType;
1413
pub(crate) use iour::{sockaddr_storage, socklen_t};
1514
pub use iour::{OpCode as IourOpCode, OpEntry};
1615
pub use poll::{Decision, OpCode as PollOpCode};
1716

17+
pub use crate::driver_type::DriverType; // Re-export so current user won't be broken
1818
use crate::{Key, OutEntries, ProactorBuilder};
1919

20-
mod driver_type {
21-
use std::sync::atomic::{AtomicU8, Ordering};
22-
23-
const UNINIT: u8 = u8::MAX;
24-
const IO_URING: u8 = 0;
25-
const POLLING: u8 = 1;
26-
27-
static DRIVER_TYPE: AtomicU8 = AtomicU8::new(UNINIT);
28-
29-
/// Representing underlying driver type the fusion driver is using
30-
#[repr(u8)]
31-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32-
pub enum DriverType {
33-
/// Using `polling` driver
34-
Poll = POLLING,
35-
36-
/// Using `io-uring` driver
37-
IoUring = IO_URING,
38-
}
39-
40-
impl DriverType {
41-
fn from_num(n: u8) -> Self {
42-
match n {
43-
IO_URING => Self::IoUring,
44-
POLLING => Self::Poll,
45-
_ => unreachable!("invalid driver type"),
46-
}
47-
}
48-
49-
/// Get the underlying driver type
50-
pub fn current() -> DriverType {
51-
match DRIVER_TYPE.load(Ordering::Acquire) {
52-
UNINIT => {}
53-
x => return DriverType::from_num(x),
54-
}
55-
56-
let dev_ty = if uring_available() {
57-
DriverType::IoUring
58-
} else {
59-
DriverType::Poll
60-
};
61-
62-
DRIVER_TYPE.store(dev_ty as u8, Ordering::Release);
63-
64-
dev_ty
65-
}
66-
}
67-
68-
fn uring_available() -> bool {
69-
use io_uring::opcode::*;
70-
71-
// Add more opcodes here if used
72-
const USED_OP: &[u8] = &[
73-
Read::CODE,
74-
Readv::CODE,
75-
Write::CODE,
76-
Writev::CODE,
77-
Fsync::CODE,
78-
Accept::CODE,
79-
Connect::CODE,
80-
RecvMsg::CODE,
81-
SendMsg::CODE,
82-
AsyncCancel::CODE,
83-
OpenAt::CODE,
84-
Close::CODE,
85-
Shutdown::CODE,
86-
// Linux kernel 5.19
87-
#[cfg(any(
88-
feature = "io-uring-sqe128",
89-
feature = "io-uring-cqe32",
90-
feature = "io-uring-socket"
91-
))]
92-
Socket::CODE,
93-
];
94-
95-
Ok(())
96-
.and_then(|_| {
97-
let uring = io_uring::IoUring::new(2)?;
98-
let mut probe = io_uring::Probe::new();
99-
uring.submitter().register_probe(&mut probe)?;
100-
std::io::Result::Ok(USED_OP.iter().all(|op| probe.is_supported(*op)))
101-
})
102-
.unwrap_or(false)
103-
}
104-
}
105-
10620
/// Fused [`OpCode`]
10721
///
10822
/// This trait encapsulates both operation for `io-uring` and `polling`
@@ -131,6 +45,7 @@ impl Driver {
13145
DriverType::IoUring => Ok(Self {
13246
fuse: FuseDriver::IoUring(iour::Driver::new(builder)?),
13347
}),
48+
_ => unreachable!("Fuse driver will only be enabled on linux"),
13449
}
13550
}
13651

compio-driver/src/fusion/op.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ macro_rules! op {
6161
DriverType::IoUring => Self {
6262
inner: [< $name Inner >]::IoUring(iour::$name::new($($arg),*)),
6363
},
64+
_ => unreachable!("Fuse driver will only be enabled on linux"),
6465
}
6566
}
6667
}

compio-driver/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub use asyncify::*;
3737
mod fd;
3838
pub use fd::*;
3939

40+
mod driver_type;
41+
pub use driver_type::*;
42+
4043
cfg_if::cfg_if! {
4144
if #[cfg(windows)] {
4245
#[path = "iocp/mod.rs"]

flake.lock

Lines changed: 9 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)