From cec414cd25b68279ccfdb93a73ca8c99320ecfc1 Mon Sep 17 00:00:00 2001 From: Joshua Yanovski Date: Thu, 8 Sep 2022 17:44:30 -0700 Subject: [PATCH] High priority rayon threads (if they're pinned). --- Cargo.lock | 17 +++++++++++++++-- common/state/Cargo.toml | 1 + common/state/src/state.rs | 18 ++++++++++++++++++ rust-toolchain | 2 +- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad54f5b97e..7429a0b570 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3053,9 +3053,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.121" +version = "0.2.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f" +checksum = "8371e4e5341c3a96db127eb2465ac681ced4c433e01dd0e938adbef26ba93ba5" [[package]] name = "libgit2-sys" @@ -6079,6 +6079,18 @@ dependencies = [ "syn 1.0.90", ] +[[package]] +name = "thread-priority" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a8a950b52fd40d98ac6ed41c7fa9e8dd62b131f48b74f418e810476b01a7460" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "log", + "winapi 0.3.9", +] + [[package]] name = "thread_local" version = "1.1.4" @@ -6705,6 +6717,7 @@ dependencies = [ "serde", "specs", "tar", + "thread-priority", "toml", "tracing", "vek 0.15.8", diff --git a/common/state/Cargo.toml b/common/state/Cargo.toml index 5cb9c3d7df..604b8efb6b 100644 --- a/common/state/Cargo.toml +++ b/common/state/Cargo.toml @@ -19,6 +19,7 @@ common-base = { package = "veloren-common-base", path = "../base" } core_affinity = "0.5" rayon = "1.5" num_cpus = "1.0" +thread-priority = { version = "0.9.2" } tracing = { version = "0.1", default-features = false } vek = { version = "0.15.8", features = ["serde"] } diff --git a/common/state/src/state.rs b/common/state/src/state.rs index 2c5d3334ca..2bee8bb7a1 100644 --- a/common/state/src/state.rs +++ b/common/state/src/state.rs @@ -35,6 +35,7 @@ use specs::{ storage::{MaskedStorage as EcsMaskedStorage, Storage as EcsStorage}, Component, DispatcherBuilder, Entity as EcsEntity, WorldExt, }; +use thread_priority::{ThreadBuilder, ThreadPriority}; use std::sync::Arc; use vek::*; @@ -124,11 +125,28 @@ impl State { GameMode::Singleplayer => num_cpu / 4, }*/num_cpu.max(common::consts::MIN_RECOMMENDED_RAYON_THREADS); let core_ids = /*(rayon_threads >= 16).then(|| */core_affinity::get_core_ids().unwrap_or(vec![])/*).unwrap_or(vec![])*/; + let core_count = core_ids.len(); let rayon_pool = Arc::new( ThreadPoolBuilder::new() .num_threads(rayon_threads/*.saturating_sub(rayon_offset)*/) // .thread_name(move |i| format!("rayon-{}", i)) .thread_name(move |i| format!("rayon-{}-{}", thread_name_infix, i)) + .spawn_handler(move |thread| { + let mut b = ThreadBuilder::default(); + if let Some(name) = thread.name() { + b = b.name(name.to_owned()); + } + if let Some(stack_size) = thread.stack_size() { + b = b.stack_size(stack_size); + } + // pinned rayon threads run with high priority + let index = thread.index(); + if index.checked_sub(rayon_offset).map_or(false, |i| i < core_count) { + b = b.priority(ThreadPriority::Max); + } + b.spawn_careless(|| thread.run())?; + Ok(()) + }) .start_handler(move |i| { if let Some(&core_id) = i.checked_sub(rayon_offset).and_then(|i| core_ids.get(i)) { core_affinity::set_for_current(core_id); diff --git a/rust-toolchain b/rust-toolchain index e22800a26e..62193b6c62 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2022-08-18 +nightly-2022-09-08