diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 9e099a591dd96..342a2fa05dd4b 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -1,121 +1,97 @@ -#[cfg(all( - any( - target_os = "linux", - target_os = "windows", - target_os = "android", - target_os = "macos" - ), - not(feature = "bevy_dynamic_plugin") -))] -mod internal { - use crate::{Diagnostic, DiagnosticId, Diagnostics}; - use bevy_app::{App, Plugin}; - use bevy_ecs::prelude::{ResMut, Resource}; - use sysinfo::{CpuExt, System, SystemExt}; +use crate::{Diagnostic, DiagnosticId, Diagnostics, BYTES_TO_GIB}; +use bevy_app::{App, Plugin}; +use bevy_ecs::prelude::{ResMut, Resource}; +use sysinfo::{CpuExt, System, SystemExt}; - const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0; - - #[derive(Resource)] - pub struct SystemInfo { - pub sys: System, - } +#[derive(Resource)] +pub struct SystemInfoResource { + pub sys: System, +} - impl Default for SystemInfo { - fn default() -> Self { - SystemInfo { - sys: System::new_all(), - } +impl Default for SystemInfoResource { + fn default() -> Self { + SystemInfoResource { + sys: System::new_all(), } } +} - /// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) - /// - /// NOT supported on non-linux, android, windows, or macos targets and NOT supported when using bevy-dynamic - #[derive(Default)] - pub struct SystemInformationDiagnosticsPlugin; +/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) +/// +/// Supported targets: +/// * linux, +/// * windows, +/// * android, +/// * macos +/// +/// NOT supported when using the `bevy/dynamic` feature even when using previously mentioned targets +#[derive(Default)] +pub struct SystemInformationDiagnosticsPlugin; - impl Plugin for SystemInformationDiagnosticsPlugin { - fn build(&self, app: &mut App) { - app.insert_resource(SystemInfo::default()); +impl Plugin for SystemInformationDiagnosticsPlugin { + fn build(&self, app: &mut App) { + // NOTE: sysinfo fails to compile when using bevy dynamic or on iOS and does nothing on wasm + #[cfg(all( + any( + target_os = "linux", + target_os = "windows", + target_os = "android", + target_os = "macos" + ), + not(feature = "bevy_dynamic_plugin") + ))] + { + app.insert_resource(SystemInfoResource::default()); app.add_startup_system(Self::setup_system) .add_system(Self::diagnostic_system); } - } - - impl SystemInformationDiagnosticsPlugin { - pub const CPU_USAGE: DiagnosticId = - DiagnosticId::from_u128(78494871623549551581510633532637320956); - pub const MEM_USAGE: DiagnosticId = - DiagnosticId::from_u128(42846254859293759601295317811892519825); - - pub fn setup_system(mut diagnostics: ResMut) { - diagnostics.add(Diagnostic::new(Self::CPU_USAGE, "cpu_usage", 20).with_suffix("%")); - diagnostics.add(Diagnostic::new(Self::MEM_USAGE, "mem_usage", 20).with_suffix("%")); - } - - pub fn diagnostic_system( - mut diagnostics: ResMut, - mut sysinfo: ResMut, - ) { - sysinfo.sys.refresh_cpu(); - sysinfo.sys.refresh_memory(); - let current_cpu_usage = { - let mut usage = 0.0; - let cpus = sysinfo.sys.cpus(); - for cpu in cpus { - usage += cpu.cpu_usage(); // note for future maintainers: this returns a value from 0.0 to 100.0 - } - // average - usage / cpus.len() as f32 - }; - // `memory()` fns return a value in bytes - let total_mem = sysinfo.sys.total_memory() as f64 / BYTES_TO_GIB; - let used_mem = sysinfo.sys.used_memory() as f64 / BYTES_TO_GIB; - let current_used_mem = used_mem / total_mem * 100.0; - - diagnostics.add_measurement(Self::CPU_USAGE, || current_cpu_usage as f64); - diagnostics.add_measurement(Self::MEM_USAGE, || current_used_mem); + #[cfg(not(all( + any( + target_os = "linux", + target_os = "windows", + target_os = "android", + target_os = "macos" + ), + not(feature = "bevy_dynamic_plugin") + )))] + { + bevy_log::warn!("This platform and/or configuration is not supported!") } } } -#[cfg(not(all( - any( - target_os = "linux", - target_os = "windows", - target_os = "android", - target_os = "macos" - ), - not(feature = "bevy_dynamic_plugin") -)))] -mod internal { - use crate::{DiagnosticId, Diagnostics}; - use bevy_app::{App, Plugin}; - use bevy_ecs::prelude::ResMut; - use bevy_log::warn; - - /// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) - /// - /// NOT supported on non-linux, android, windows, or macos targets and NOT supported when using bevy-dynamic/ - #[derive(Default)] - pub struct SystemInformationDiagnosticsPlugin; +impl SystemInformationDiagnosticsPlugin { + pub const CPU_USAGE: DiagnosticId = + DiagnosticId::from_u128(78494871623549551581510633532637320956); + pub const MEM_USAGE: DiagnosticId = + DiagnosticId::from_u128(42846254859293759601295317811892519825); - impl Plugin for SystemInformationDiagnosticsPlugin { - fn build(&self, app: &mut App) { - app.add_startup_system(Self::setup_system); - } + pub fn setup_system(mut diagnostics: ResMut) { + diagnostics.add(Diagnostic::new(Self::CPU_USAGE, "cpu_usage", 20).with_suffix("%")); + diagnostics.add(Diagnostic::new(Self::MEM_USAGE, "mem_usage", 20).with_suffix("%")); } - impl SystemInformationDiagnosticsPlugin { - pub const CPU_USAGE: DiagnosticId = - DiagnosticId::from_u128(78494871623549551581510633532637320956); - pub const MEM_USAGE: DiagnosticId = - DiagnosticId::from_u128(42846254859293759601295317811892519825); + pub fn diagnostic_system( + mut diagnostics: ResMut, + mut sysinfo: ResMut, + ) { + sysinfo.sys.refresh_cpu(); + sysinfo.sys.refresh_memory(); + let current_cpu_usage = { + let mut usage = 0.0; + let cpus = sysinfo.sys.cpus(); + for cpu in cpus { + usage += cpu.cpu_usage(); // NOTE: this returns a value from 0.0 to 100.0 + } + // average + usage / cpus.len() as f32 + }; + // `memory()` fns return a value in bytes + let total_mem = sysinfo.sys.total_memory() as f64 / BYTES_TO_GIB; + let used_mem = sysinfo.sys.used_memory() as f64 / BYTES_TO_GIB; + let current_used_mem = used_mem / total_mem * 100.0; - pub fn setup_system(_diagnostics: ResMut) { - warn!("SystemInformationDiagnosticsPlugin: This platform and/or configuration is not supported!") - } + diagnostics.add_measurement(Self::CPU_USAGE, || current_cpu_usage as f64); + diagnostics.add_measurement(Self::MEM_USAGE, || current_used_mem); } } - -pub use internal::*;