Make SystemInfo a Resource (#12584)
# 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 <alice.i.cecile@gmail.com> Co-authored-by: Afonso Lage <lage.afonso@gmail.com>
This commit is contained in:
parent
34c8778bf0
commit
85b488b73d
@ -19,7 +19,7 @@ pub use entity_count_diagnostics_plugin::EntityCountDiagnosticsPlugin;
|
|||||||
pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin;
|
pub use frame_time_diagnostics_plugin::FrameTimeDiagnosticsPlugin;
|
||||||
pub use log_diagnostics_plugin::LogDiagnosticsPlugin;
|
pub use log_diagnostics_plugin::LogDiagnosticsPlugin;
|
||||||
#[cfg(feature = "sysinfo_plugin")]
|
#[cfg(feature = "sysinfo_plugin")]
|
||||||
pub use system_information_diagnostics_plugin::SystemInformationDiagnosticsPlugin;
|
pub use system_information_diagnostics_plugin::{SystemInfo, SystemInformationDiagnosticsPlugin};
|
||||||
|
|
||||||
use bevy_app::prelude::*;
|
use bevy_app::prelude::*;
|
||||||
|
|
||||||
@ -28,12 +28,11 @@ use bevy_app::prelude::*;
|
|||||||
pub struct DiagnosticsPlugin;
|
pub struct DiagnosticsPlugin;
|
||||||
|
|
||||||
impl Plugin for 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")]
|
#[cfg(feature = "sysinfo_plugin")]
|
||||||
_app.init_resource::<DiagnosticsStore>().add_systems(
|
app.init_resource::<system_information_diagnostics_plugin::SystemInfo>();
|
||||||
Startup,
|
|
||||||
system_information_diagnostics_plugin::internal::log_system_info,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
use crate::DiagnosticPath;
|
use crate::DiagnosticPath;
|
||||||
use bevy_app::prelude::*;
|
use bevy_app::prelude::*;
|
||||||
|
use bevy_ecs::system::Resource;
|
||||||
|
|
||||||
/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %)
|
/// Adds a System Information Diagnostic, specifically `cpu_usage` (in %) and `mem_usage` (in %)
|
||||||
///
|
///
|
||||||
@ -24,10 +25,27 @@ impl Plugin for SystemInformationDiagnosticsPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SystemInformationDiagnosticsPlugin {
|
impl SystemInformationDiagnosticsPlugin {
|
||||||
|
/// Total system cpu usage in %
|
||||||
pub const CPU_USAGE: DiagnosticPath = DiagnosticPath::const_new("system/cpu_usage");
|
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");
|
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
|
// NOTE: sysinfo fails to compile when using bevy dynamic or on iOS and does nothing on wasm
|
||||||
#[cfg(all(
|
#[cfg(all(
|
||||||
any(
|
any(
|
||||||
@ -45,7 +63,7 @@ pub mod internal {
|
|||||||
|
|
||||||
use crate::{Diagnostic, Diagnostics, DiagnosticsStore};
|
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;
|
const BYTES_TO_GIB: f64 = 1.0 / 1024.0 / 1024.0 / 1024.0;
|
||||||
|
|
||||||
@ -87,25 +105,15 @@ pub mod internal {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
impl Default for SystemInfo {
|
||||||
// This is required because the Debug trait doesn't detect it's used when it's only used in a print :(
|
fn default() -> Self {
|
||||||
#[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(
|
let sys = System::new_with_specifics(
|
||||||
RefreshKind::new()
|
RefreshKind::new()
|
||||||
.with_cpu(CpuRefreshKind::new())
|
.with_cpu(CpuRefreshKind::new())
|
||||||
.with_memory(MemoryRefreshKind::new().with_ram()),
|
.with_memory(MemoryRefreshKind::new().with_ram()),
|
||||||
);
|
);
|
||||||
|
|
||||||
let info = SystemInfo {
|
let system_info = SystemInfo {
|
||||||
os: System::long_os_version().unwrap_or_else(|| String::from("not available")),
|
os: System::long_os_version().unwrap_or_else(|| String::from("not available")),
|
||||||
kernel: System::kernel_version().unwrap_or_else(|| String::from("not available")),
|
kernel: System::kernel_version().unwrap_or_else(|| String::from("not available")),
|
||||||
cpu: sys
|
cpu: sys
|
||||||
@ -121,7 +129,9 @@ pub mod internal {
|
|||||||
memory: format!("{:.1} GiB", sys.total_memory() as f64 * BYTES_TO_GIB),
|
memory: format!("{:.1} GiB", sys.total_memory() as f64 * BYTES_TO_GIB),
|
||||||
};
|
};
|
||||||
|
|
||||||
info!("{:?}", info);
|
info!("{:?}", system_info);
|
||||||
|
system_info
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -143,7 +153,16 @@ pub mod internal {
|
|||||||
// no-op
|
// no-op
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn log_system_info() {
|
impl Default for super::SystemInfo {
|
||||||
// no-op
|
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(),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user