From 59ec59c6401aa221c555f49291f58afc1ff61f24 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Thu, 8 Sep 2022 11:23:41 +0900 Subject: [PATCH 01/15] add system information plugin and update relevant examples --- crates/bevy_diagnostic/Cargo.toml | 16 +++++ crates/bevy_diagnostic/src/lib.rs | 6 +- .../system_information_diagnostics_plugin.rs | 64 +++++++++++++++++++ examples/diagnostics/log_diagnostics.rs | 2 + 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs diff --git a/crates/bevy_diagnostic/Cargo.toml b/crates/bevy_diagnostic/Cargo.toml index 295a969f32e14..fdd2ad62c2fa9 100644 --- a/crates/bevy_diagnostic/Cargo.toml +++ b/crates/bevy_diagnostic/Cargo.toml @@ -16,3 +16,19 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.9.0-dev" } bevy_log = { path = "../bevy_log", version = "0.9.0-dev" } bevy_time = { path = "../bevy_time", version = "0.9.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.9.0-dev" } + +# iOS or MacOS +[target.'cfg(any(target_os="ios",target_os="macos"))'.dependencies.sysinfo] +version = "0.26.2" +features = ["apple-app-store"] +default-features = false + +# use the unknown ci feature for RPi +[target.'cfg(all(target_os="linux",any(target_arch="aarch64",target_arch="arm")))'.dependencies.sysinfo] +version = "0.26.2" +features = ["unknown-ci"] # this features prevents linking to the C interface, which causes the build issues +default-features = false + +# general case +[target.'cfg(all(not(any(target_os="ios",target_os="macos")),not(all(target_os="linux",any(target_arch="aarch64",target_arch="arm")))))'.dependencies.sysinfo] +version = "0.26.2" diff --git a/crates/bevy_diagnostic/src/lib.rs b/crates/bevy_diagnostic/src/lib.rs index 076618bcde060..f38448fc46b65 100644 --- a/crates/bevy_diagnostic/src/lib.rs +++ b/crates/bevy_diagnostic/src/lib.rs @@ -2,12 +2,14 @@ mod diagnostic; mod entity_count_diagnostics_plugin; mod frame_time_diagnostics_plugin; mod log_diagnostics_plugin; +mod system_information_diagnostics_plugin; + +use bevy_app::prelude::*; pub use diagnostic::*; pub use entity_count_diagnostics_plugin::EntityCountDiagnosticsPlugin; pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin; pub use log_diagnostics_plugin::LogDiagnosticsPlugin; - -use bevy_app::prelude::*; +pub use system_information_diagnostics_plugin::SystemInformationDiagnosticsPlugin; /// Adds core diagnostics resources to an App. #[derive(Default)] diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs new file mode 100644 index 0000000000000..6936b0a5a96a6 --- /dev/null +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -0,0 +1,64 @@ +use crate::{Diagnostic, DiagnosticId, Diagnostics}; +use bevy_app::{App, Plugin}; +use bevy_ecs::prelude::{ResMut, Resource}; +use sysinfo::{CpuExt, System, SystemExt}; + +#[derive(Resource)] +pub struct SystemInfo { + pub sys: System, +} + +impl Default for SystemInfo { + fn default() -> Self { + SystemInfo { + sys: System::new_all(), + } + } +} + +/// Adds a System Information Diagnostic, specifically "cpu_usage" (%) and "mem_usage" (%) +#[derive(Default)] +pub struct SystemInformationDiagnosticsPlugin; + +impl Plugin for SystemInformationDiagnosticsPlugin { + fn build(&self, app: &mut App) { + app.insert_resource(SystemInfo::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 susinfo: ResMut, + ) { + susinfo.sys.refresh_cpu(); + susinfo.sys.refresh_memory(); + let current_cpu_usage = { + let mut usage = 0.0; + let cpus = susinfo.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 + }; + let total_mem = susinfo.sys.total_memory() as f64 / 1073741824_f64; // `memory()` fns return a value in bytes + let used_mem = susinfo.sys.used_memory() as f64 / 1073741824_f64; + 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 as f64); + } +} diff --git a/examples/diagnostics/log_diagnostics.rs b/examples/diagnostics/log_diagnostics.rs index 3af2af71df870..ee5edcfeb44c8 100644 --- a/examples/diagnostics/log_diagnostics.rs +++ b/examples/diagnostics/log_diagnostics.rs @@ -17,5 +17,7 @@ fn main() { // .add_plugin(bevy::diagnostic::EntityCountDiagnosticsPlugin::default()) // Uncomment this to add an asset count diagnostics: // .add_plugin(bevy::asset::diagnostic::AssetCountDiagnosticsPlugin::::default()) + // Uncomment this to add an System Info diagnostics: + // .add_plugin(bevy::diagnostic::SystemInformationDiagnosticsPlugin::default()) .run(); } From b7c56807f282fe202cd2a760dd3e86002e7a2245 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Thu, 8 Sep 2022 11:43:20 +0900 Subject: [PATCH 02/15] fix docs and typo --- crates/bevy_diagnostic/Cargo.toml | 6 ++++-- .../src/system_information_diagnostics_plugin.rs | 2 +- examples/diagnostics/log_diagnostics.rs | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/bevy_diagnostic/Cargo.toml b/crates/bevy_diagnostic/Cargo.toml index fdd2ad62c2fa9..aeb67214a39dd 100644 --- a/crates/bevy_diagnostic/Cargo.toml +++ b/crates/bevy_diagnostic/Cargo.toml @@ -26,9 +26,11 @@ default-features = false # use the unknown ci feature for RPi [target.'cfg(all(target_os="linux",any(target_arch="aarch64",target_arch="arm")))'.dependencies.sysinfo] version = "0.26.2" -features = ["unknown-ci"] # this features prevents linking to the C interface, which causes the build issues +features = [ + "unknown-ci", +] # this features prevents linking to the C interface, which causes the build issues default-features = false # general case -[target.'cfg(all(not(any(target_os="ios",target_os="macos")),not(all(target_os="linux",any(target_arch="aarch64",target_arch="arm")))))'.dependencies.sysinfo] +[dependencies.sysinfo] version = "0.26.2" diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 6936b0a5a96a6..90dc035271605 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -16,7 +16,7 @@ impl Default for SystemInfo { } } -/// Adds a System Information Diagnostic, specifically "cpu_usage" (%) and "mem_usage" (%) +/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) #[derive(Default)] pub struct SystemInformationDiagnosticsPlugin; diff --git a/examples/diagnostics/log_diagnostics.rs b/examples/diagnostics/log_diagnostics.rs index ee5edcfeb44c8..436bba684f78e 100644 --- a/examples/diagnostics/log_diagnostics.rs +++ b/examples/diagnostics/log_diagnostics.rs @@ -17,7 +17,7 @@ fn main() { // .add_plugin(bevy::diagnostic::EntityCountDiagnosticsPlugin::default()) // Uncomment this to add an asset count diagnostics: // .add_plugin(bevy::asset::diagnostic::AssetCountDiagnosticsPlugin::::default()) - // Uncomment this to add an System Info diagnostics: + // Uncomment this to add system info diagnostics: // .add_plugin(bevy::diagnostic::SystemInformationDiagnosticsPlugin::default()) .run(); } From 19b6f31261c2a208a5e5ff6cab94a238b7124a35 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Fri, 2 Dec 2022 21:55:57 +0900 Subject: [PATCH 03/15] fix messed up cargo toml --- crates/bevy_diagnostic/Cargo.toml | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/crates/bevy_diagnostic/Cargo.toml b/crates/bevy_diagnostic/Cargo.toml index bf1f3f73b9375..b35d61caf5593 100644 --- a/crates/bevy_diagnostic/Cargo.toml +++ b/crates/bevy_diagnostic/Cargo.toml @@ -10,6 +10,12 @@ keywords = ["bevy"] [dependencies] +bevy_app = { path = "../bevy_app", version = "0.9.0" } +bevy_core = { path = "../bevy_core", version = "0.9.0" } +bevy_ecs = { path = "../bevy_ecs", version = "0.9.0" } +bevy_log = { path = "../bevy_log", version = "0.9.0" } +bevy_time = { path = "../bevy_time", version = "0.9.0" } +bevy_utils = { path = "../bevy_utils", version = "0.9.0" } # iOS or MacOS [target.'cfg(any(target_os="ios",target_os="macos"))'.dependencies.sysinfo] @@ -28,11 +34,3 @@ default-features = false # general case [dependencies.sysinfo] version = "0.26.8" - -bevy_app = { path = "../bevy_app", version = "0.9.0" } -bevy_core = { path = "../bevy_core", version = "0.9.0" } -bevy_ecs = { path = "../bevy_ecs", version = "0.9.0" } -bevy_log = { path = "../bevy_log", version = "0.9.0" } -bevy_time = { path = "../bevy_time", version = "0.9.0" } -bevy_utils = { path = "../bevy_utils", version = "0.9.0" } - From 28dee0ee893e8be261f698085b0eaa98bb887256 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Sun, 4 Dec 2022 01:22:04 +0900 Subject: [PATCH 04/15] Update crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: François --- .../src/system_information_diagnostics_plugin.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 90dc035271605..1a4b86fe2a4e5 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -54,8 +54,9 @@ impl SystemInformationDiagnosticsPlugin { // average usage / cpus.len() as f32 }; - let total_mem = susinfo.sys.total_memory() as f64 / 1073741824_f64; // `memory()` fns return a value in bytes - let used_mem = susinfo.sys.used_memory() as f64 / 1073741824_f64; + // `memory()` fns return a value in bytes + let total_mem = susinfo.sys.total_memory() as f64 / 1_073_741_824_f64; + let used_mem = susinfo.sys.used_memory() as f64 / 1_073_741_824_f64; let current_used_mem = used_mem / total_mem * 100.0; diagnostics.add_measurement(Self::CPU_USAGE, || current_cpu_usage as f64); From c008554b51a55763ca1ba7e2016278f41e66fcee Mon Sep 17 00:00:00 2001 From: l1npengtul <35755164+l1npengtul@users.noreply.github.com> Date: Fri, 23 Dec 2022 16:46:37 +0900 Subject: [PATCH 05/15] simplify cargo toml according to 5454 --- crates/bevy_diagnostic/Cargo.toml | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/crates/bevy_diagnostic/Cargo.toml b/crates/bevy_diagnostic/Cargo.toml index b35d61caf5593..30b4217be1e8b 100644 --- a/crates/bevy_diagnostic/Cargo.toml +++ b/crates/bevy_diagnostic/Cargo.toml @@ -19,18 +19,11 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0" } # iOS or MacOS [target.'cfg(any(target_os="ios",target_os="macos"))'.dependencies.sysinfo] -version = "0.26.8" +version = "0.27.1" +# Some features of sysinfo are not supported by apple. This will disable those features on apple devices features = ["apple-app-store"] default-features = false -# use the unknown ci feature for RPi -[target.'cfg(all(target_os="linux",any(target_arch="aarch64",target_arch="arm")))'.dependencies.sysinfo] -version = "0.26.8" -features = [ - "unknown-ci", -] # this features prevents linking to the C interface, which causes the build issues -default-features = false - -# general case [dependencies.sysinfo] -version = "0.26.8" +version = "0.27.1" +default-features = false From 38e84423a6ba46e0a520628a7f66e12d77d75e56 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Fri, 23 Dec 2022 17:03:32 +0900 Subject: [PATCH 06/15] implement suggestions, remove sus (very sad) --- .../src/system_information_diagnostics_plugin.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 1a4b86fe2a4e5..42887d5bc98f4 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -3,6 +3,8 @@ 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, @@ -41,13 +43,13 @@ impl SystemInformationDiagnosticsPlugin { pub fn diagnostic_system( mut diagnostics: ResMut, - mut susinfo: ResMut, + mut sysinfo: ResMut, ) { - susinfo.sys.refresh_cpu(); - susinfo.sys.refresh_memory(); + sysinfo.sys.refresh_cpu(); + sysinfo.sys.refresh_memory(); let current_cpu_usage = { let mut usage = 0.0; - let cpus = susinfo.sys.cpus(); + 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 } @@ -55,11 +57,11 @@ impl SystemInformationDiagnosticsPlugin { usage / cpus.len() as f32 }; // `memory()` fns return a value in bytes - let total_mem = susinfo.sys.total_memory() as f64 / 1_073_741_824_f64; - let used_mem = susinfo.sys.used_memory() as f64 / 1_073_741_824_f64; + 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 as f64); + diagnostics.add_measurement(Self::MEM_USAGE, || current_used_mem); } } From e9d174ab4fba92e5881252a05f05508cc0660ce7 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Wed, 28 Dec 2022 16:01:46 +0900 Subject: [PATCH 07/15] add ghost plugin --- .../system_information_diagnostics_plugin.rs | 148 ++++++++++++------ 1 file changed, 97 insertions(+), 51 deletions(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 42887d5bc98f4..c8c242e7dfd7b 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -1,67 +1,113 @@ -use crate::{Diagnostic, DiagnosticId, Diagnostics}; -use bevy_app::{App, Plugin}; -use bevy_ecs::prelude::{ResMut, Resource}; -use sysinfo::{CpuExt, System, SystemExt}; +#[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}; -const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0; + 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 SystemInfo { + pub sys: System, + } -impl Default for SystemInfo { - fn default() -> Self { - SystemInfo { - sys: System::new_all(), + impl Default for SystemInfo { + fn default() -> Self { + SystemInfo { + sys: System::new_all(), + } + } + } + + /// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) + #[derive(Default)] + pub struct SystemInformationDiagnosticsPlugin; + + impl Plugin for SystemInformationDiagnosticsPlugin { + fn build(&self, app: &mut App) { + app.insert_resource(SystemInfo::default()); + app.add_startup_system(Self::setup_system) + .add_system(Self::diagnostic_system); } } -} -/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) -#[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.insert_resource(SystemInfo::default()); - app.add_startup_system(Self::setup_system) - .add_system(Self::diagnostic_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("%")); + } + + 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); + } } } -impl SystemInformationDiagnosticsPlugin { - pub const CPU_USAGE: DiagnosticId = - DiagnosticId::from_u128(78494871623549551581510633532637320956); - pub const MEM_USAGE: DiagnosticId = - DiagnosticId::from_u128(42846254859293759601295317811892519825); +#[cfg(not(all( + any( + target_os = "linux", + target_os = "windows", + target_os = "android", + target_os = "macos" + ), + not(feature = "bevy_dynamic_plugin") +)))] +mod internal { + /// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) + #[derive(Default)] + pub struct SystemInformationDiagnosticsPlugin; - 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 Plugin for SystemInformationDiagnosticsPlugin { + fn build(&self, app: &mut App) { + app.add_startup_system(Self::setup_system) + .add_system(Self::diagnostic_system); + } } - 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; + impl SystemInformationDiagnosticsPlugin { + pub const CPU_USAGE: DiagnosticId = + DiagnosticId::from_u128(78494871623549551581510633532637320956); + pub const MEM_USAGE: DiagnosticId = + DiagnosticId::from_u128(42846254859293759601295317811892519825); - diagnostics.add_measurement(Self::CPU_USAGE, || current_cpu_usage as f64); - diagnostics.add_measurement(Self::MEM_USAGE, || current_used_mem); + pub fn setup_system(_diagnostics: ResMut) {} + + pub fn diagnostic_system(_diagnostics: ResMut, _sysinfo: ResMut) {} } } + +pub use internal::*; From d6695eb96818c3a8ba0caa1d388085aca768778a Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Wed, 28 Dec 2022 16:06:29 +0900 Subject: [PATCH 08/15] fix compile error, let me out of HS omori --- .../src/system_information_diagnostics_plugin.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index c8c242e7dfd7b..37196958a1c07 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -87,6 +87,11 @@ mod internal { 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 %) #[derive(Default)] pub struct SystemInformationDiagnosticsPlugin; @@ -104,9 +109,9 @@ mod internal { pub const MEM_USAGE: DiagnosticId = DiagnosticId::from_u128(42846254859293759601295317811892519825); - pub fn setup_system(_diagnostics: ResMut) {} - - pub fn diagnostic_system(_diagnostics: ResMut, _sysinfo: ResMut) {} + pub fn setup_system(_diagnostics: ResMut) { + warn!("SystemInformationDiagnosticsPlugin: This platform and/or configuration is not supported!") + } } } From a2f20bc4cace58eb49474b9d6b834d0f947df132 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Wed, 28 Dec 2022 16:07:40 +0900 Subject: [PATCH 09/15] add warning about unsupporoted platforms on doc for plugin --- .../src/system_information_diagnostics_plugin.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 37196958a1c07..d2c1c001bc9ab 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -29,7 +29,8 @@ mod internal { } /// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %) - #[derive(Default)] + /// + /// NOT supported on non-linux, android, windows, or macos targets and NOT supported when using bevy-dynamic/ #[derive(Default)] pub struct SystemInformationDiagnosticsPlugin; impl Plugin for SystemInformationDiagnosticsPlugin { @@ -93,6 +94,8 @@ mod internal { 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; From 08b5295479312c121044854cd1df605f4cc1139e Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Wed, 28 Dec 2022 16:14:12 +0900 Subject: [PATCH 10/15] madotsuki you forgot to derive madotsuki no dont go to the balcony --- .../src/system_information_diagnostics_plugin.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index d2c1c001bc9ab..ed6f9743d2c30 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -30,7 +30,8 @@ mod internal { /// 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)] + /// NOT supported on non-linux, android, windows, or macos targets and NOT supported when using bevy-dynamic + #[derive(Default)] pub struct SystemInformationDiagnosticsPlugin; impl Plugin for SystemInformationDiagnosticsPlugin { From 7fc4b259a0cc5e85e835bb9e41186e56e4f7af36 Mon Sep 17 00:00:00 2001 From: l1npengtul Date: Wed, 28 Dec 2022 16:18:40 +0900 Subject: [PATCH 11/15] sunny removes the unused import warningand fixes the nonexistant system error instead of white space --- crates/bevy_diagnostic/src/lib.rs | 2 +- .../src/system_information_diagnostics_plugin.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/bevy_diagnostic/src/lib.rs b/crates/bevy_diagnostic/src/lib.rs index 24601ef399f5c..90fb370f0a0be 100644 --- a/crates/bevy_diagnostic/src/lib.rs +++ b/crates/bevy_diagnostic/src/lib.rs @@ -5,7 +5,6 @@ mod log_diagnostics_plugin; mod system_information_diagnostics_plugin; use bevy_app::prelude::*; -use bevy_log::info; pub use diagnostic::*; pub use entity_count_diagnostics_plugin::EntityCountDiagnosticsPlugin; pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin; @@ -52,6 +51,7 @@ fn log_system_info() { not(feature = "bevy_dynamic_plugin") ))] { + use bevy_log::info; use sysinfo::{CpuExt, SystemExt}; let mut sys = sysinfo::System::new(); diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index ed6f9743d2c30..9e099a591dd96 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -102,8 +102,7 @@ mod internal { impl Plugin for SystemInformationDiagnosticsPlugin { fn build(&self, app: &mut App) { - app.add_startup_system(Self::setup_system) - .add_system(Self::diagnostic_system); + app.add_startup_system(Self::setup_system); } } From 1ae255e88fc28798b08d2f80a779181dd353ac41 Mon Sep 17 00:00:00 2001 From: l1npengtul <35755164+l1npengtul@users.noreply.github.com> Date: Wed, 28 Dec 2022 17:23:55 +0900 Subject: [PATCH 12/15] add back #bevy --- crates/bevy_diagnostic/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/bevy_diagnostic/Cargo.toml b/crates/bevy_diagnostic/Cargo.toml index 8d7c950001ecd..7bd1249cbefaa 100644 --- a/crates/bevy_diagnostic/Cargo.toml +++ b/crates/bevy_diagnostic/Cargo.toml @@ -10,6 +10,7 @@ keywords = ["bevy"] [dependencies] +# bevy bevy_app = { path = "../bevy_app", version = "0.9.0" } bevy_core = { path = "../bevy_core", version = "0.9.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.9.0" } From 59f3e4c4926753ff672a25a4cc50ba56aab0ef02 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Thu, 29 Dec 2022 23:08:16 -0500 Subject: [PATCH 13/15] remove code duplication --- .../system_information_diagnostics_plugin.rs | 178 ++++++++---------- 1 file changed, 78 insertions(+), 100 deletions(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 9e099a591dd96..3fa848ef93213 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -1,121 +1,99 @@ -#[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}; - const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0; +use crate::{Diagnostic, DiagnosticId, Diagnostics}; +use bevy_app::{App, Plugin}; +use bevy_ecs::prelude::{ResMut, Resource}; +use sysinfo::{CpuExt, System, SystemExt}; - #[derive(Resource)] - pub struct SystemInfo { - pub sys: System, - } +const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0; - impl Default for SystemInfo { - fn default() -> Self { - SystemInfo { - sys: System::new_all(), - } +#[derive(Resource)] +pub struct SystemInfo { + pub sys: System, +} + +impl Default for SystemInfo { + fn default() -> Self { + SystemInfo { + 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, +/// * android, +/// * windows, +/// * macos +/// +/// NOT supported when using bevy_dynamic even when using previously mentioned targets +#[derive(Default)] +pub struct SystemInformationDiagnosticsPlugin; - impl Plugin for SystemInformationDiagnosticsPlugin { - fn build(&self, app: &mut App) { +impl Plugin for SystemInformationDiagnosticsPlugin { + fn build(&self, app: &mut App) { + #[cfg(all( + any( + target_os = "linux", + target_os = "windows", + target_os = "android", + target_os = "macos" + ), + not(feature = "bevy_dynamic_plugin") + ))] + { app.insert_resource(SystemInfo::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") + )))] + { + warn!("SystemInformationDiagnosticsPlugin: 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; +impl SystemInformationDiagnosticsPlugin { + pub const CPU_USAGE: DiagnosticId = + DiagnosticId::from_u128(78494871623549551581510633532637320956); + pub const MEM_USAGE: DiagnosticId = + DiagnosticId::from_u128(42846254859293759601295317811892519825); - /// 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 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 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; - 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::*; From b37721a426b97540b4e235a23310abe6bc96e0a8 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Thu, 29 Dec 2022 23:23:14 -0500 Subject: [PATCH 14/15] clean up --- .../system_information_diagnostics_plugin.rs | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 3fa848ef93213..342a2fa05dd4b 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -1,19 +1,16 @@ - -use crate::{Diagnostic, DiagnosticId, Diagnostics}; +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 struct SystemInfoResource { pub sys: System, } -impl Default for SystemInfo { +impl Default for SystemInfoResource { fn default() -> Self { - SystemInfo { + SystemInfoResource { sys: System::new_all(), } } @@ -23,16 +20,17 @@ impl Default for SystemInfo { /// /// Supported targets: /// * linux, -/// * android, /// * windows, +/// * android, /// * macos /// -/// NOT supported when using bevy_dynamic even when using previously mentioned targets +/// 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) { + // NOTE: sysinfo fails to compile when using bevy dynamic or on iOS and does nothing on wasm #[cfg(all( any( target_os = "linux", @@ -43,7 +41,7 @@ impl Plugin for SystemInformationDiagnosticsPlugin { not(feature = "bevy_dynamic_plugin") ))] { - app.insert_resource(SystemInfo::default()); + app.insert_resource(SystemInfoResource::default()); app.add_startup_system(Self::setup_system) .add_system(Self::diagnostic_system); } @@ -57,7 +55,7 @@ impl Plugin for SystemInformationDiagnosticsPlugin { not(feature = "bevy_dynamic_plugin") )))] { - warn!("SystemInformationDiagnosticsPlugin: This platform and/or configuration is not supported!") + bevy_log::warn!("This platform and/or configuration is not supported!") } } } @@ -75,7 +73,7 @@ impl SystemInformationDiagnosticsPlugin { pub fn diagnostic_system( mut diagnostics: ResMut, - mut sysinfo: ResMut, + mut sysinfo: ResMut, ) { sysinfo.sys.refresh_cpu(); sysinfo.sys.refresh_memory(); @@ -83,7 +81,7 @@ impl SystemInformationDiagnosticsPlugin { 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 + usage += cpu.cpu_usage(); // NOTE: this returns a value from 0.0 to 100.0 } // average usage / cpus.len() as f32 From c51e9fc65b8fc41026fe6614fa12f52d43872b82 Mon Sep 17 00:00:00 2001 From: IceSentry Date: Mon, 2 Jan 2023 14:18:36 -0500 Subject: [PATCH 15/15] fix cfg sys usage --- crates/bevy_diagnostic/src/lib.rs | 54 +----- .../system_information_diagnostics_plugin.rs | 174 ++++++++++++------ 2 files changed, 119 insertions(+), 109 deletions(-) diff --git a/crates/bevy_diagnostic/src/lib.rs b/crates/bevy_diagnostic/src/lib.rs index 90fb370f0a0be..b2d127f115600 100644 --- a/crates/bevy_diagnostic/src/lib.rs +++ b/crates/bevy_diagnostic/src/lib.rs @@ -18,62 +18,10 @@ pub struct DiagnosticsPlugin; impl Plugin for DiagnosticsPlugin { fn build(&self, app: &mut App) { app.init_resource::() - .add_startup_system(log_system_info); + .add_startup_system(system_information_diagnostics_plugin::internal::log_system_info); } } /// The width which diagnostic names will be printed as /// Plugin names should not be longer than this value pub const MAX_DIAGNOSTIC_NAME_WIDTH: usize = 32; - -#[derive(Debug)] -// This is required because the Debug trait doesn't detect it's used when it's only used in a print :( -#[allow(dead_code)] -struct SystemInfo { - os: String, - kernel: String, - cpu: String, - core_count: String, - memory: String, -} - -const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0; - -fn log_system_info() { - // 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") - ))] - { - use bevy_log::info; - use sysinfo::{CpuExt, SystemExt}; - - let mut sys = sysinfo::System::new(); - sys.refresh_cpu(); - sys.refresh_memory(); - - let info = SystemInfo { - os: sys - .long_os_version() - .unwrap_or_else(|| String::from("not available")), - kernel: sys - .kernel_version() - .unwrap_or_else(|| String::from("not available")), - cpu: sys.global_cpu_info().brand().trim().to_string(), - core_count: sys - .physical_core_count() - .map(|x| x.to_string()) - .unwrap_or_else(|| String::from("not available")), - // Convert from Bytes to GibiBytes since it's probably what people expect most of the time - memory: format!("{:.1} GiB", sys.total_memory() as f64 * BYTES_TO_GIB), - }; - - info!("{:?}", info); - } -} diff --git a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs index 342a2fa05dd4b..464fc06693da0 100644 --- a/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs +++ b/crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs @@ -1,20 +1,5 @@ -use crate::{Diagnostic, DiagnosticId, Diagnostics, BYTES_TO_GIB}; +use crate::DiagnosticId; use bevy_app::{App, Plugin}; -use bevy_ecs::prelude::{ResMut, Resource}; -use sysinfo::{CpuExt, System, SystemExt}; - -#[derive(Resource)] -pub struct SystemInfoResource { - pub sys: System, -} - -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 %) /// @@ -27,36 +12,10 @@ impl Default for SystemInfoResource { /// 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) { - // 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); - } - #[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!") - } + app.add_startup_system(internal::setup_system) + .add_system(internal::diagnostic_system); } } @@ -65,21 +24,62 @@ impl SystemInformationDiagnosticsPlugin { DiagnosticId::from_u128(78494871623549551581510633532637320956); pub const MEM_USAGE: DiagnosticId = DiagnosticId::from_u128(42846254859293759601295317811892519825); +} + +// 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") +))] +pub mod internal { + use bevy_ecs::{prelude::ResMut, system::Local}; + use bevy_log::info; + use sysinfo::{CpuExt, System, SystemExt}; + + use crate::{Diagnostic, Diagnostics}; - 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("%")); + const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0; + + pub(crate) fn setup_system(mut diagnostics: ResMut) { + diagnostics.add( + Diagnostic::new( + super::SystemInformationDiagnosticsPlugin::CPU_USAGE, + "cpu_usage", + 20, + ) + .with_suffix("%"), + ); + diagnostics.add( + Diagnostic::new( + super::SystemInformationDiagnosticsPlugin::MEM_USAGE, + "mem_usage", + 20, + ) + .with_suffix("%"), + ); } - pub fn diagnostic_system( + pub(crate) fn diagnostic_system( mut diagnostics: ResMut, - mut sysinfo: ResMut, + mut sysinfo: Local>, ) { - sysinfo.sys.refresh_cpu(); - sysinfo.sys.refresh_memory(); + if sysinfo.is_none() { + *sysinfo = Some(System::new_all()); + } + let Some(sys) = sysinfo.as_mut() else { + return; + }; + + sys.refresh_cpu(); + sys.refresh_memory(); let current_cpu_usage = { let mut usage = 0.0; - let cpus = sysinfo.sys.cpus(); + let cpus = sys.cpus(); for cpu in cpus { usage += cpu.cpu_usage(); // NOTE: this returns a value from 0.0 to 100.0 } @@ -87,11 +87,73 @@ impl SystemInformationDiagnosticsPlugin { 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 total_mem = sys.total_memory() as f64 / BYTES_TO_GIB; + let used_mem = 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); + diagnostics.add_measurement(super::SystemInformationDiagnosticsPlugin::CPU_USAGE, || { + current_cpu_usage as f64 + }); + diagnostics.add_measurement(super::SystemInformationDiagnosticsPlugin::MEM_USAGE, || { + current_used_mem + }); + } + + #[derive(Debug)] + // This is required because the Debug trait doesn't detect it's used when it's only used in a print :( + #[allow(dead_code)] + struct SystemInfo { + os: String, + kernel: String, + cpu: String, + core_count: String, + memory: String, + } + + pub(crate) fn log_system_info() { + let mut sys = sysinfo::System::new(); + sys.refresh_cpu(); + sys.refresh_memory(); + + let info = SystemInfo { + os: sys + .long_os_version() + .unwrap_or_else(|| String::from("not available")), + kernel: sys + .kernel_version() + .unwrap_or_else(|| String::from("not available")), + cpu: sys.global_cpu_info().brand().trim().to_string(), + core_count: sys + .physical_core_count() + .map(|x| x.to_string()) + .unwrap_or_else(|| String::from("not available")), + // Convert from Bytes to GibiBytes since it's probably what people expect most of the time + memory: format!("{:.1} GiB", sys.total_memory() as f64 * BYTES_TO_GIB), + }; + + info!("{:?}", info); + } +} + +#[cfg(not(all( + any( + target_os = "linux", + target_os = "windows", + target_os = "android", + target_os = "macos" + ), + not(feature = "bevy_dynamic_plugin") +)))] +pub mod internal { + pub(crate) fn setup_system() { + bevy_log::warn!("This platform and/or configuration is not supported!"); + } + + pub(crate) fn diagnostic_system() { + // no-op + } + + pub(crate) fn log_system_info() { + // no-op } }