From 28a3e9f724ddb0b9a2e880829805992b08d33559 Mon Sep 17 00:00:00 2001 From: not-matthias Date: Wed, 23 Apr 2025 18:10:41 +0200 Subject: [PATCH] feat: pin tokio worker threads to core 0 --- Cargo.lock | 28 ++++++++++++++++++++++++++++ Cargo.toml | 3 ++- src/main.rs | 25 +++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3e473d0..e76af897 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,6 +266,7 @@ dependencies = [ "base64", "clap", "console", + "core_affinity", "git2", "gql_client", "indicatif", @@ -328,6 +329,17 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" +[[package]] +name = "core_affinity" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a034b3a7b624016c6e13f5df875747cc25f884156aad2abd12b6c46797971342" +dependencies = [ + "libc", + "num_cpus", + "winapi", +] + [[package]] name = "cpufeatures" version = "0.2.16" @@ -681,6 +693,12 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "hex" version = "0.4.3" @@ -1214,6 +1232,16 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "number_prefix" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index c1d026ec..793400f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ serde = { version = "1.0.192", features = ["derive"] } serde_json = { version = "1.0.108", features = ["preserve_order"] } url = "2.4.1" sha256 = "1.4.0" -tokio = { version = "1", features = ["macros", "rt"] } +tokio = { version = "1", features = ["macros", "rt", "rt-multi-thread"] } tokio-tar = "0.3.1" md5 = "0.7.0" base64 = "0.21.0" @@ -46,6 +46,7 @@ sysinfo = { version = "0.33.1", features = ["serde"] } indicatif = "0.17.8" console = "0.15.8" async-trait = "0.1.82" +core_affinity = "0.8.3" [dev-dependencies] temp-env = { version = "0.3.6", features = ["async_closure"] } diff --git a/src/main.rs b/src/main.rs index ddc57bef..d1ffc5fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ mod run; mod setup; use console::style; +use core_affinity::CoreId; use lazy_static::lazy_static; use local_logger::clean_logger; use prelude::*; @@ -27,8 +28,28 @@ lazy_static! { format!("{VALGRIND_CODSPEED_BASE_VERSION}-0codspeed1"); } -#[tokio::main(flavor = "current_thread")] -async fn main() { +fn main() -> anyhow::Result<()> { + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(4) + .on_thread_start(|| { + // Core 0 is also responsible for interrupts and other system tasks. But + // this is fine, as we're not executing performance-critical code. + if !core_affinity::set_for_current(CoreId { id: 0 }) { + panic!("failed to pin worker thread to core 0"); + } + }) + .enable_all() + .build()?; + + // NOTE: The code above doesn't set the core affinity for the main thread. We should avoid doing so, as + // the affinity is passed down to child threads/processes. + + let _guard = runtime.enter(); + runtime.block_on(real_main()); + Ok(()) +} + +async fn real_main() { let res = crate::app::run().await; if let Err(err) = res { for cause in err.chain() {