Skip to content

Commit

Permalink
Make SystemInfo a Resource (bevyengine#12584)
Browse files Browse the repository at this point in the history
# Objective

We already collect a lot of system information on startup when possible
but we don't make this information available. With the upcoming work on
a diagnostic overlay it would be useful to be able to display this
information.

## Solution

Make the already existing SystemInfo a Resource

---

## Changelog

Added `SystemInfo` Resource

---------

Co-authored-by: Alice Cecile <[email protected]>
Co-authored-by: Afonso Lage <[email protected]>
  • Loading branch information
3 people committed Mar 23, 2024
1 parent 34c8778 commit 85b488b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
11 changes: 5 additions & 6 deletions crates/bevy_diagnostic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use entity_count_diagnostics_plugin::EntityCountDiagnosticsPlugin;
pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin;
pub use log_diagnostics_plugin::LogDiagnosticsPlugin;
#[cfg(feature = "sysinfo_plugin")]
pub use system_information_diagnostics_plugin::SystemInformationDiagnosticsPlugin;
pub use system_information_diagnostics_plugin::{SystemInfo, SystemInformationDiagnosticsPlugin};

use bevy_app::prelude::*;

Expand All @@ -28,12 +28,11 @@ use bevy_app::prelude::*;
pub struct DiagnosticsPlugin;

impl Plugin for DiagnosticsPlugin {
fn build(&self, _app: &mut App) {
fn build(&self, app: &mut App) {
app.init_resource::<DiagnosticsStore>();

#[cfg(feature = "sysinfo_plugin")]
_app.init_resource::<DiagnosticsStore>().add_systems(
Startup,
system_information_diagnostics_plugin::internal::log_system_info,
);
app.init_resource::<system_information_diagnostics_plugin::SystemInfo>();
}
}

Expand Down
95 changes: 57 additions & 38 deletions crates/bevy_diagnostic/src/system_information_diagnostics_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::DiagnosticPath;
use bevy_app::prelude::*;
use bevy_ecs::system::Resource;

/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %)
///
Expand All @@ -24,10 +25,27 @@ impl Plugin for SystemInformationDiagnosticsPlugin {
}

impl SystemInformationDiagnosticsPlugin {
/// Total system cpu usage in %
pub const CPU_USAGE: DiagnosticPath = DiagnosticPath::const_new("system/cpu_usage");
/// Total system memory usage in %
pub const MEM_USAGE: DiagnosticPath = DiagnosticPath::const_new("system/mem_usage");
}

/// A resource that stores diagnostic information about the system.
/// This information can be useful for debugging and profiling purposes.
///
/// # See also
///
/// [`SystemInformationDiagnosticsPlugin`] for more information.
#[derive(Debug, Resource)]
pub struct SystemInfo {
pub os: String,
pub kernel: String,
pub cpu: String,
pub core_count: String,
pub memory: String,
}

// NOTE: sysinfo fails to compile when using bevy dynamic or on iOS and does nothing on wasm
#[cfg(all(
any(
Expand All @@ -45,7 +63,7 @@ pub mod internal {

use crate::{Diagnostic, Diagnostics, DiagnosticsStore};

use super::SystemInformationDiagnosticsPlugin;
use super::{SystemInfo, SystemInformationDiagnosticsPlugin};

const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0;

Expand Down Expand Up @@ -87,41 +105,33 @@ pub mod internal {
});
}

#[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 sys = System::new_with_specifics(
RefreshKind::new()
.with_cpu(CpuRefreshKind::new())
.with_memory(MemoryRefreshKind::new().with_ram()),
);

let info = SystemInfo {
os: System::long_os_version().unwrap_or_else(|| String::from("not available")),
kernel: System::kernel_version().unwrap_or_else(|| String::from("not available")),
cpu: sys
.cpus()
.first()
.map(|cpu| cpu.brand().trim().to_string())
.unwrap_or_else(|| String::from("not available")),
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);
impl Default for SystemInfo {
fn default() -> Self {
let sys = System::new_with_specifics(
RefreshKind::new()
.with_cpu(CpuRefreshKind::new())
.with_memory(MemoryRefreshKind::new().with_ram()),
);

let system_info = SystemInfo {
os: System::long_os_version().unwrap_or_else(|| String::from("not available")),
kernel: System::kernel_version().unwrap_or_else(|| String::from("not available")),
cpu: sys
.cpus()
.first()
.map(|cpu| cpu.brand().trim().to_string())
.unwrap_or_else(|| String::from("not available")),
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!("{:?}", system_info);
system_info
}
}
}

Expand All @@ -143,7 +153,16 @@ pub mod internal {
// no-op
}

pub(crate) fn log_system_info() {
// no-op
impl Default for super::SystemInfo {
fn default() -> Self {
let unknown = "Unknown".to_string();
Self {
os: unknown.clone(),
kernel: unknown.clone(),
cpu: unknown.clone(),
core_count: unknown.clone(),
memory: unknown.clone(),
}
}
}
}

0 comments on commit 85b488b

Please sign in to comment.