From 77dea4980c19ce94e03d7dbc90de5aafe0df05f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=A4rtens?= Date: Tue, 9 Mar 2021 11:52:57 +0100 Subject: [PATCH] get rid of a lazy_static and update agent multithreaded metrics --- common/ecs/src/system.rs | 22 +++++++++++----------- server/src/lib.rs | 10 ++++++++++ server/src/sys/agent.rs | 5 +++-- server/src/sys/metrics.rs | 15 +++++++++------ 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/common/ecs/src/system.rs b/common/ecs/src/system.rs index 349f16d30f..f1459967f4 100644 --- a/common/ecs/src/system.rs +++ b/common/ecs/src/system.rs @@ -10,7 +10,7 @@ pub enum ParMode { None, /* Job is not running at all */ Single, Rayon, - Exact(u16), + Exact(u32), } //TODO: make use of the phase of a system for advanced scheduling and logging @@ -54,7 +54,7 @@ pub struct CpuTimeStats { /// isn't running. `Single` means you are running single threaded. /// `Rayon` means you are running on the rayon threadpool. impl ParMode { - fn threads(&self, rayon_threads: u16) -> u16 { + fn threads(&self, rayon_threads: u32) -> u32 { match self { ParMode::None => 0, ParMode::Single => 1, @@ -144,8 +144,8 @@ impl CpuTimeStats { pub fn gen_stats( timelines: &HashMap, tick_work_start: Instant, - rayon_threads: u16, - physical_threads: u16, + rayon_threads: u32, + physical_threads: u32, ) -> HashMap { let mut result = HashMap::new(); let mut all = timelines @@ -171,7 +171,7 @@ pub fn gen_stats( let total = individual_cores_wanted .iter() .map(|(_, a)| a) - .sum::() + .sum::() .max(1) as f32; let total_or_max = total.max(physical_threads as f32); // update ALL states @@ -309,8 +309,8 @@ mod tests { #[test] fn single() { - const RAYON_THREADS: u16 = 4; - const PHYSICAL_THREADS: u16 = RAYON_THREADS; + const RAYON_THREADS: u32 = 4; + const PHYSICAL_THREADS: u32 = RAYON_THREADS; let tick_start = Instant::now(); let job_d = vec![(500, 1500, ParMode::Rayon)]; let timelines = mock_timelines(tick_start, job_d); @@ -336,8 +336,8 @@ mod tests { #[test] fn two_jobs() { - const RAYON_THREADS: u16 = 8; - const PHYSICAL_THREADS: u16 = RAYON_THREADS; + const RAYON_THREADS: u32 = 8; + const PHYSICAL_THREADS: u32 = RAYON_THREADS; let tick_start = Instant::now(); let job_d = vec![(2000, 3000, ParMode::Single), (5000, 6500, ParMode::Single)]; let timelines = mock_timelines(tick_start, job_d); @@ -375,8 +375,8 @@ mod tests { #[test] fn generate_stats() { - const RAYON_THREADS: u16 = 6; - const PHYSICAL_THREADS: u16 = RAYON_THREADS; + const RAYON_THREADS: u32 = 6; + const PHYSICAL_THREADS: u32 = RAYON_THREADS; let tick_start = Instant::now(); let job_d = vec![ (2000, 5000, ParMode::Rayon), diff --git a/server/src/lib.rs b/server/src/lib.rs index 119d1d216a..23416918bc 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -118,6 +118,12 @@ struct SpawnPoint(Vec3); #[derive(Copy, Clone, Default)] pub struct Tick(u64); +#[derive(Clone)] +pub struct HwStats { + hardware_threads: u32, + rayon_threads: u32, +} + // Start of Tick, used for metrics #[derive(Copy, Clone)] pub struct TickStart(Instant); @@ -178,6 +184,10 @@ impl Server { state .ecs_mut() .insert(LoginProvider::new(settings.auth_server_address.clone())); + state.ecs_mut().insert(HwStats { + hardware_threads: num_cpus::get() as u32, + rayon_threads: num_cpus::get() as u32, + }); state.ecs_mut().insert(Tick(0)); state.ecs_mut().insert(TickStart(Instant::now())); state.ecs_mut().insert(network_request_metrics); diff --git a/server/src/sys/agent.rs b/server/src/sys/agent.rs index 41262be87b..dd029b3546 100644 --- a/server/src/sys/agent.rs +++ b/server/src/sys/agent.rs @@ -23,7 +23,7 @@ use common::{ util::Dir, vol::ReadVol, }; -use common_ecs::{Job, Origin, Phase, System}; +use common_ecs::{Job, Origin, ParMode, Phase, System}; use rand::{thread_rng, Rng}; use rayon::iter::ParallelIterator; use specs::{ @@ -113,9 +113,10 @@ impl<'a> System<'a> for Sys { #[allow(clippy::or_fun_call)] // TODO: Pending review in #587 fn run( - _job: &mut Job, + job: &mut Job, (read_data, event_bus, mut agents, mut controllers): Self::SystemData, ) { + job.cpu_stats.measure(ParMode::Rayon); ( &read_data.entities, (&read_data.energies, &read_data.healths), diff --git a/server/src/sys/metrics.rs b/server/src/sys/metrics.rs index 0f648368fe..e8c21c680b 100644 --- a/server/src/sys/metrics.rs +++ b/server/src/sys/metrics.rs @@ -1,6 +1,6 @@ use crate::{ metrics::{EcsSystemMetrics, PhysicsMetrics, TickMetrics}, - Tick, TickStart, + HwStats, Tick, TickStart, }; use common::{resources::TimeOfDay, terrain::TerrainGrid}; use common_ecs::{Job, Origin, Phase, SysMetrics, System}; @@ -14,6 +14,7 @@ impl<'a> System<'a> for Sys { #[allow(clippy::type_complexity)] type SystemData = ( Option>, + ReadExpect<'a, HwStats>, ReadExpect<'a, Tick>, ReadExpect<'a, TimeOfDay>, ReadExpect<'a, TickStart>, @@ -33,6 +34,7 @@ impl<'a> System<'a> for Sys { _job: &mut Job, ( entities, + hw_stats, tick, time_of_day, tick_start, @@ -52,11 +54,12 @@ impl<'a> System<'a> for Sys { //this system hasn't run yet state.remove(Self::NAME); - lazy_static::lazy_static! { - static ref THREADS: u16 = num_cpus::get() as u16; - } - - for (name, stat) in common_ecs::gen_stats(&state, tick_start.0, *THREADS, *THREADS) { + for (name, stat) in common_ecs::gen_stats( + &state, + tick_start.0, + hw_stats.rayon_threads, + hw_stats.hardware_threads, + ) { export_ecs .system_start_time .with_label_values(&[&name])