From 83c9a62b7e1d78e791bd8f5102a1aa4e669b5761 Mon Sep 17 00:00:00 2001 From: numinex Date: Thu, 3 Jul 2025 12:33:15 +0200 Subject: [PATCH 1/2] feat(utils): introduce utils crate with compio utilities --- Cargo.toml | 4 +++- compio-utils/Cargo.toml | 19 ++++++++++++++++ compio-utils/src/lib.rs | 48 +++++++++++++++++++++++++++++++++++++++++ compio/Cargo.toml | 3 +++ compio/src/lib.rs | 3 +++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 compio-utils/Cargo.toml create mode 100644 compio-utils/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 899d4443..1c9696b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,8 @@ members = [ "compio-quic", "compio-runtime", "compio-signal", - "compio-tls", + "compio-tls", + "compio-utils", ] resolver = "2" @@ -38,6 +39,7 @@ compio-log = { path = "./compio-log", version = "0.1.0" } compio-tls = { path = "./compio-tls", version = "0.6.0", default-features = false } compio-process = { path = "./compio-process", version = "0.5.0" } compio-quic = { path = "./compio-quic", version = "0.4.0", default-features = false } +compio-utils = { path = "./compio-utils", version = "0.1.0" } bytes = "1.7.1" flume = "0.11.0" diff --git a/compio-utils/Cargo.toml b/compio-utils/Cargo.toml new file mode 100644 index 00000000..d797c54d --- /dev/null +++ b/compio-utils/Cargo.toml @@ -0,0 +1,19 @@ + +[package] +name = "compio-utils" +version = "0.1.0" +description = "Utilities for compio runtime" +edition = { workspace = true } +authors = { workspace = true } +readme = { workspace = true } +license = { workspace = true } +repository = { workspace = true } + +[package.metadata.docs.rs] +all-features = true +rustdoc-args = ["--cfg", "docsrs"] + +[dependencies] +nix = { workspace = true, features = ["sched"] } + + diff --git a/compio-utils/src/lib.rs b/compio-utils/src/lib.rs new file mode 100644 index 00000000..0ae7b17f --- /dev/null +++ b/compio-utils/src/lib.rs @@ -0,0 +1,48 @@ +/// Bind error +#[cfg(unix)] +pub type BindError = nix::Result; + +/// Bind error +#[cfg(windows)] +pub type BindError = std::io::Result; + +/// Bind current thread to given cpus +#[cfg(any(target_os = "android", target_os = "dragonfly", target_os = "linux"))] +pub fn bind_to_cpu_set(cpus: impl IntoIterator) -> BindError<()> { + let mut cpuset = nix::sched::CpuSet::new(); + for cpu in cpus { + cpuset.set(cpu)?; + } + let pid = nix::unistd::Pid::from_raw(0); + nix::sched::sched_setaffinity(pid, &cpuset) +} + +/// Bind current thread to given cpus(but not works for non-linux) +#[cfg(all( + unix, + not(any(target_os = "android", target_os = "dragonfly", target_os = "linux")) +))] +pub fn bind_to_cpu_set(_: impl IntoIterator) -> BindError<()> { + Ok(()) +} + +/// Bind current thread to given cpus +#[cfg(windows)] +pub fn bind_to_cpu_set(_: impl IntoIterator) -> BindError<()> { + Ok(()) +} + +#[cfg(all(test))] +mod tests { + use super::*; + + #[test] + fn bind_cpu() { + assert!(bind_to_cpu_set(Some(0)).is_ok()); + #[cfg(all( + unix, + any(target_os = "android", target_os = "dragonfly", target_os = "linux") + ))] + assert!(bind_to_cpu_set(Some(100000)).is_err()); + } +} diff --git a/compio/Cargo.toml b/compio/Cargo.toml index 45f2847a..b059e666 100644 --- a/compio/Cargo.toml +++ b/compio/Cargo.toml @@ -43,6 +43,7 @@ compio-log = { workspace = true } compio-tls = { workspace = true, optional = true } compio-process = { workspace = true, optional = true } compio-quic = { workspace = true, optional = true } +compio-utils = { workspace = true, optional = true } # Shared dev dependencies for all platforms [dev-dependencies] @@ -109,6 +110,7 @@ tls = ["dep:compio-tls"] native-tls = ["tls", "compio-tls/native-tls"] rustls = ["tls", "compio-tls/rustls"] process = ["dep:compio-process"] +utils = ["dep:compio-utils"] quic = ["dep:compio-quic"] h3 = ["quic", "compio-quic/h3"] all = [ @@ -121,6 +123,7 @@ all = [ "process", "quic", "h3", + "utils", ] arrayvec = ["compio-buf/arrayvec"] diff --git a/compio/src/lib.rs b/compio/src/lib.rs index 2aa68556..4844175f 100644 --- a/compio/src/lib.rs +++ b/compio/src/lib.rs @@ -50,6 +50,9 @@ pub use compio_signal as signal; #[cfg(feature = "tls")] #[doc(inline)] pub use compio_tls as tls; +#[cfg(feature = "utils")] +#[doc(inline)] +pub use compio_utils as utils; #[cfg(feature = "event")] #[doc(no_inline)] pub use runtime::event; From 5372c03283875165b53659bcabb81c5ab340a1a7 Mon Sep 17 00:00:00 2001 From: numinex Date: Thu, 3 Jul 2025 12:39:42 +0200 Subject: [PATCH 2/2] fix clippy error --- compio-utils/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compio-utils/src/lib.rs b/compio-utils/src/lib.rs index 0ae7b17f..ca298951 100644 --- a/compio-utils/src/lib.rs +++ b/compio-utils/src/lib.rs @@ -32,7 +32,7 @@ pub fn bind_to_cpu_set(_: impl IntoIterator) -> BindError<()> { Ok(()) } -#[cfg(all(test))] +#[cfg(test)] mod tests { use super::*;