limit tokio threads to 1/4 of num_cpus

make sure that rayon has at least 2 threads even on 1cpu systems to increase stability
This commit is contained in:
Marcel Märtens
2021-05-04 19:42:45 +02:00
parent 6e3a74b476
commit 0142cf37f0
7 changed files with 13 additions and 3 deletions

2
Cargo.lock generated
View File

@ -5607,6 +5607,7 @@ version = "0.9.0"
dependencies = [ dependencies = [
"bincode", "bincode",
"hashbrown", "hashbrown",
"num_cpus",
"rayon", "rayon",
"scopeguard", "scopeguard",
"serde", "serde",
@ -5766,6 +5767,7 @@ dependencies = [
"clap", "clap",
"crossterm 0.19.0", "crossterm 0.19.0",
"lazy_static", "lazy_static",
"num_cpus",
"ron", "ron",
"serde", "serde",
"signal-hook 0.3.8", "signal-hook 0.3.8",

View File

@ -18,6 +18,7 @@ common-ecs = { package = "veloren-common-ecs", path = "../ecs" }
common-base = { package = "veloren-common-base", path = "../base" } common-base = { package = "veloren-common-base", path = "../base" }
rayon = "1.5" rayon = "1.5"
num_cpus = "1.0"
tracing = { version = "0.1", default-features = false } tracing = { version = "0.1", default-features = false }
vek = { version = "=0.14.1", features = ["serde"] } vek = { version = "=0.14.1", features = ["serde"] }

View File

@ -103,6 +103,9 @@ impl State {
let thread_pool = Arc::new( let thread_pool = Arc::new(
ThreadPoolBuilder::new() ThreadPoolBuilder::new()
.num_threads(
num_cpus::get().max(2), /* Have AT LEAST 2 rayon threads */
)
.thread_name(move |i| format!("rayon-{}-{}", thread_name_infix, i)) .thread_name(move |i| format!("rayon-{}-{}", thread_name_infix, i))
.build() .build()
.unwrap(), .unwrap(),
@ -208,7 +211,7 @@ impl State {
ecs.insert(Vec::<common::outcome::Outcome>::new()); ecs.insert(Vec::<common::outcome::Outcome>::new());
ecs.insert(common::CachedSpatialGrid::default()); ecs.insert(common::CachedSpatialGrid::default());
let slow_limit = thread_pool.current_num_threads().max(2) as u64; let slow_limit = num_cpus::get().max(2) as u64;
let slow_limit = slow_limit / 2 + slow_limit / 4; let slow_limit = slow_limit / 2 + slow_limit / 4;
tracing::trace!(?slow_limit, "Slow Thread limit"); tracing::trace!(?slow_limit, "Slow Thread limit");
ecs.insert(SlowJobPool::new(slow_limit, Arc::clone(&thread_pool))); ecs.insert(SlowJobPool::new(slow_limit, Arc::clone(&thread_pool)));

View File

@ -28,6 +28,7 @@ common-net = { package = "veloren-common-net", path = "../common/net" }
common-frontend = { package = "veloren-common-frontend", path = "../common/frontend" } common-frontend = { package = "veloren-common-frontend", path = "../common/frontend" }
tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] } tokio = { version = "1", default-features = false, features = ["rt-multi-thread"] }
num_cpus = "1.0"
ansi-parser = "0.7" ansi-parser = "0.7"
clap = "2.33" clap = "2.33"
crossterm = "0.19" crossterm = "0.19"

View File

@ -117,9 +117,12 @@ fn main() -> io::Result<()> {
path path
}; };
// We don't need that many threads in the async pool, at least 2 but generally
// 25% of all available will do
let runtime = Arc::new( let runtime = Arc::new(
tokio::runtime::Builder::new_multi_thread() tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
.worker_threads((num_cpus::get() / 4).max(2))
.thread_name_fn(|| { .thread_name_fn(|| {
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0); static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst); let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);

View File

@ -66,7 +66,7 @@ impl ClientInit {
Arc::new( Arc::new(
runtime::Builder::new_multi_thread() runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
.worker_threads(if cores > 4 { cores - 1 } else { cores }) .worker_threads((cores / 4).max(2))
.thread_name_fn(|| { .thread_name_fn(|| {
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0); static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst); let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);

View File

@ -87,7 +87,7 @@ impl Singleplayer {
let runtime = Arc::new( let runtime = Arc::new(
tokio::runtime::Builder::new_multi_thread() tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
.worker_threads(if cores > 4 { cores - 1 } else { cores }) .worker_threads((cores / 4).max(2))
.thread_name_fn(|| { .thread_name_fn(|| {
static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0); static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst); let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);