From 5adb0a1ad6100c2c5e6f0c58a142faeb2d185632 Mon Sep 17 00:00:00 2001 From: gusto Date: Sat, 9 Nov 2024 04:53:54 +0200 Subject: [PATCH] Min max and avg total bandwidth per node (#50) --- simlib/netrunner/src/network/mod.rs | 32 ++++++++++++++++++++-- simlib/netrunner/src/runner/sync_runner.rs | 5 +--- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/simlib/netrunner/src/network/mod.rs b/simlib/netrunner/src/network/mod.rs index 39fcb16..1fec225 100644 --- a/simlib/netrunner/src/network/mod.rs +++ b/simlib/netrunner/src/network/mod.rs @@ -4,7 +4,7 @@ use std::{ ops::Add, str::FromStr, sync::{ - atomic::{AtomicU32, Ordering}, + atomic::{AtomicU32, AtomicU64, Ordering}, Arc, }, time::{Duration, Instant}, @@ -113,6 +113,7 @@ struct NodeNetworkCapacity { capacity_bps: Option, current_load: Mutex, load_to_flush: AtomicU32, + total_incomming_bandwidth: AtomicU64, } impl NodeNetworkCapacity { @@ -121,10 +122,13 @@ impl NodeNetworkCapacity { capacity_bps, current_load: Mutex::new(0), load_to_flush: AtomicU32::new(0), + total_incomming_bandwidth: AtomicU64::new(0), } } fn increase_load(&self, load: u32) -> bool { + self.total_incomming_bandwidth + .fetch_add(load as u64, Ordering::Relaxed); if let Some(capacity_bps) = self.capacity_bps { let mut current_load = self.current_load.lock(); if *current_load + load <= capacity_bps { @@ -173,6 +177,9 @@ where pub struct NetworkState { pub total_outbound_bandwidth: u64, pub total_inbound_bandwidth: u64, + pub min_node_total_bandwidth: f64, + pub max_node_total_bandwidth: f64, + pub avg_node_total_bandwidth: f64, } impl Network @@ -193,7 +200,28 @@ where } } - pub fn bandwidth_results(&self) -> NetworkState { + pub fn network_state(&mut self) -> NetworkState { + self.state.min_node_total_bandwidth = self + .node_network_capacity + .values() + .map(|c| c.total_incomming_bandwidth.load(Ordering::Relaxed) as f64) + .min_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap_or(f64::INFINITY); // Default to INFINITY if no elements are present + + self.state.max_node_total_bandwidth = self + .node_network_capacity + .values() + .map(|c| c.total_incomming_bandwidth.load(Ordering::Relaxed) as f64) + .max_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap_or(f64::NEG_INFINITY); + + self.state.avg_node_total_bandwidth = self + .node_network_capacity + .values() + .map(|c| c.total_incomming_bandwidth.load(Ordering::Relaxed) as f64) + .sum::() + / self.node_network_capacity.len() as f64; + self.state.clone() } diff --git a/simlib/netrunner/src/runner/sync_runner.rs b/simlib/netrunner/src/runner/sync_runner.rs index 92b6580..dc93bd3 100644 --- a/simlib/netrunner/src/runner/sync_runner.rs +++ b/simlib/netrunner/src/runner/sync_runner.rs @@ -55,10 +55,7 @@ where } } } - tracing::info!( - "Total bandwidth results: {:?}", - inner_runner.network.bandwidth_results() - ); + tracing::info!("Network state: {:?}", inner_runner.network.network_state()); Ok(()) }); Ok(SimulationRunnerHandle {