From af7091182e0a2a2145f17a9a10daa6f58ee44e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Tue, 4 May 2021 21:41:08 +0200 Subject: [PATCH] use a constant for recommended_threads --- common/src/consts.rs | 4 ++++ common/state/src/state.rs | 10 ++++------ server-cli/src/main.rs | 5 +++-- voxygen/src/menu/main/client_init.rs | 4 +++- voxygen/src/singleplayer.rs | 5 +++-- 5 files changed, 17 insertions(+), 11 deletions(-) diff --git a/common/src/consts.rs b/common/src/consts.rs index 2e4897696b..f030d9742a 100644 --- a/common/src/consts.rs +++ b/common/src/consts.rs @@ -16,3 +16,7 @@ pub const WATER_DENSITY: f32 = 999.1026; pub const IRON_DENSITY: f32 = 7870.0; // pub const HUMAN_DENSITY: f32 = 1010.0; // real value pub const HUMAN_DENSITY: f32 = 990.0; // value we use to make humanoids gently float +// 1 thread might be used for long-running cpu intensive tasks, like chunk +// generation. having at least 2 helps not blocking in the main tick here +pub const MIN_RECOMMENDED_RAYON_THREADS: usize = 2; +pub const MIN_RECOMMENDED_TOKIO_THREADS: usize = 2; diff --git a/common/state/src/state.rs b/common/state/src/state.rs index 3819ad200f..236e5996cb 100644 --- a/common/state/src/state.rs +++ b/common/state/src/state.rs @@ -103,9 +103,7 @@ impl State { let thread_pool = Arc::new( ThreadPoolBuilder::new() - .num_threads( - num_cpus::get().max(2), /* Have AT LEAST 2 rayon threads */ - ) + .num_threads(num_cpus::get().max(common::consts::MIN_RECOMMENDED_RAYON_THREADS)) .thread_name(move |i| format!("rayon-{}-{}", thread_name_infix, i)) .build() .unwrap(), @@ -211,8 +209,8 @@ impl State { ecs.insert(Vec::::new()); ecs.insert(common::CachedSpatialGrid::default()); - let slow_limit = num_cpus::get().max(2) as u64; - let slow_limit = slow_limit / 2 + slow_limit / 4; + let num_cpu = num_cpus::get() as u64; + let slow_limit = (num_cpu / 2 + num_cpu / 4).max(1); tracing::trace!(?slow_limit, "Slow Thread limit"); ecs.insert(SlowJobPool::new(slow_limit, Arc::clone(&thread_pool))); @@ -324,7 +322,7 @@ impl State { /// Get a mutable reference to the internal ECS world. pub fn ecs_mut(&mut self) -> &mut specs::World { &mut self.ecs } - pub fn thread_pool(&self) -> Arc { Arc::clone(&self.thread_pool) } + pub fn thread_pool(&self) -> &Arc { &self.thread_pool } /// Get a reference to the `TerrainChanges` structure of the state. This /// contains information about terrain state that has changed since the diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index fb75296802..c8d1a6ac39 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -14,7 +14,7 @@ use crate::{ cmd::Message, shutdown_coordinator::ShutdownCoordinator, tui_runner::Tui, tuilog::TuiLog, }; use clap::{App, Arg, SubCommand}; -use common::clock::Clock; +use common::{clock::Clock, consts::MIN_RECOMMENDED_TOKIO_THREADS}; use common_base::span; use core::sync::atomic::{AtomicUsize, Ordering}; use server::{ @@ -119,10 +119,11 @@ fn main() -> io::Result<()> { // We don't need that many threads in the async pool, at least 2 but generally // 25% of all available will do + // TODO: evaluate std::thread::available_concurrency as a num_cpus replacement let runtime = Arc::new( tokio::runtime::Builder::new_multi_thread() .enable_all() - .worker_threads((num_cpus::get() / 4).max(2)) + .worker_threads((num_cpus::get() / 4).max(MIN_RECOMMENDED_TOKIO_THREADS)) .thread_name_fn(|| { static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0); let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst); diff --git a/voxygen/src/menu/main/client_init.rs b/voxygen/src/menu/main/client_init.rs index 9d4fc61504..de69abc5a5 100644 --- a/voxygen/src/menu/main/client_init.rs +++ b/voxygen/src/menu/main/client_init.rs @@ -3,6 +3,7 @@ use client::{ error::{Error as ClientError, NetworkConnectError, NetworkError}, Client, ServerInfo, }; +use common::consts::MIN_RECOMMENDED_TOKIO_THREADS; use crossbeam::channel::{unbounded, Receiver, Sender, TryRecvError}; use std::{ sync::{ @@ -62,11 +63,12 @@ impl ClientInit { let cancel2 = Arc::clone(&cancel); let runtime = runtime.unwrap_or_else(|| { + // TODO: evaluate std::thread::available_concurrency as a num_cpus replacement let cores = num_cpus::get(); Arc::new( runtime::Builder::new_multi_thread() .enable_all() - .worker_threads((cores / 4).max(2)) + .worker_threads((cores / 4).max(MIN_RECOMMENDED_TOKIO_THREADS)) .thread_name_fn(|| { static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0); let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst); diff --git a/voxygen/src/singleplayer.rs b/voxygen/src/singleplayer.rs index 68252fa2dd..25c5575482 100644 --- a/voxygen/src/singleplayer.rs +++ b/voxygen/src/singleplayer.rs @@ -1,4 +1,4 @@ -use common::clock::Clock; +use common::{clock::Clock, consts::MIN_RECOMMENDED_TOKIO_THREADS}; use crossbeam::channel::{bounded, unbounded, Receiver, Sender, TryRecvError}; use server::{ persistence::{DatabaseSettings, SqlLogMode}, @@ -82,12 +82,13 @@ impl Singleplayer { let settings = server::Settings::singleplayer(&server_data_dir); let editable_settings = server::EditableSettings::singleplayer(&server_data_dir); + // TODO: evaluate std::thread::available_concurrency as a num_cpus replacement let cores = num_cpus::get(); debug!("Creating a new runtime for server"); let runtime = Arc::new( tokio::runtime::Builder::new_multi_thread() .enable_all() - .worker_threads((cores / 4).max(2)) + .worker_threads((cores / 4).max(MIN_RECOMMENDED_TOKIO_THREADS)) .thread_name_fn(|| { static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0); let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);