Add process cpu/memory usage to SystemInformationDiagnosticsPlugin (#18279)
# Objective - Adding process specific cpu/mem usages to SystemInformationDiagnosticsPlugin - Fixes #18135 ## Solution - Adding two new stats by using code provided in #18135 ## Testing - Tested by adding SystemInformationDiagnosticsPlugin into enabling_disabling_diagnostics example - Tested only on Linux (Ubuntu 24.04.2 LTS) --- ## Showcase Example output: > 2025-03-12T18:20:45.355206Z INFO bevy diagnostic: fps : 144.139984 (avg 143.968838) > 2025-03-12T18:20:45.355229Z INFO bevy diagnostic: system/cpu_usage : 17.299578% (avg 16.410863%) > 2025-03-12T18:20:45.355235Z INFO bevy diagnostic: frame_time : 6.939720ms (avg 6.953508ms) > 2025-03-12T18:20:45.355239Z INFO bevy diagnostic: frame_count : 1271.000000 > 2025-03-12T18:20:45.355243Z INFO bevy diagnostic: process/cpu_usage: 172.151901% (avg 165.337555%) > 2025-03-12T18:20:45.355247Z INFO bevy diagnostic: process/mem_usage: 400.472656% (avg 400.478516%) > 2025-03-12T18:20:45.355250Z INFO bevy diagnostic: system/mem_usage : 34.244571% (avg 34.356289%)
This commit is contained in:
parent
b7d5254762
commit
79655269f5
@ -29,9 +29,13 @@ impl Plugin for SystemInformationDiagnosticsPlugin {
|
|||||||
|
|
||||||
impl SystemInformationDiagnosticsPlugin {
|
impl SystemInformationDiagnosticsPlugin {
|
||||||
/// Total system cpu usage in %
|
/// Total system cpu usage in %
|
||||||
pub const CPU_USAGE: DiagnosticPath = DiagnosticPath::const_new("system/cpu_usage");
|
pub const SYSTEM_CPU_USAGE: DiagnosticPath = DiagnosticPath::const_new("system/cpu_usage");
|
||||||
/// Total system memory usage in %
|
/// Total system memory usage in %
|
||||||
pub const MEM_USAGE: DiagnosticPath = DiagnosticPath::const_new("system/mem_usage");
|
pub const SYSTEM_MEM_USAGE: DiagnosticPath = DiagnosticPath::const_new("system/mem_usage");
|
||||||
|
/// Process cpu usage in %
|
||||||
|
pub const PROCESS_CPU_USAGE: DiagnosticPath = DiagnosticPath::const_new("process/cpu_usage");
|
||||||
|
/// Process memory usage in %
|
||||||
|
pub const PROCESS_MEM_USAGE: DiagnosticPath = DiagnosticPath::const_new("process/mem_usage");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A resource that stores diagnostic information about the system.
|
/// A resource that stores diagnostic information about the system.
|
||||||
@ -90,15 +94,26 @@ pub mod internal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn setup_system(mut diagnostics: ResMut<DiagnosticsStore>) {
|
fn setup_system(mut diagnostics: ResMut<DiagnosticsStore>) {
|
||||||
diagnostics
|
diagnostics.add(
|
||||||
.add(Diagnostic::new(SystemInformationDiagnosticsPlugin::CPU_USAGE).with_suffix("%"));
|
Diagnostic::new(SystemInformationDiagnosticsPlugin::SYSTEM_CPU_USAGE).with_suffix("%"),
|
||||||
diagnostics
|
);
|
||||||
.add(Diagnostic::new(SystemInformationDiagnosticsPlugin::MEM_USAGE).with_suffix("%"));
|
diagnostics.add(
|
||||||
|
Diagnostic::new(SystemInformationDiagnosticsPlugin::SYSTEM_MEM_USAGE).with_suffix("%"),
|
||||||
|
);
|
||||||
|
diagnostics.add(
|
||||||
|
Diagnostic::new(SystemInformationDiagnosticsPlugin::PROCESS_CPU_USAGE).with_suffix("%"),
|
||||||
|
);
|
||||||
|
diagnostics.add(
|
||||||
|
Diagnostic::new(SystemInformationDiagnosticsPlugin::PROCESS_MEM_USAGE)
|
||||||
|
.with_suffix("GiB"),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SysinfoRefreshData {
|
struct SysinfoRefreshData {
|
||||||
current_cpu_usage: f64,
|
system_cpu_usage: f64,
|
||||||
current_used_mem: f64,
|
system_mem_usage: f64,
|
||||||
|
process_cpu_usage: f64,
|
||||||
|
process_mem_usage: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default)]
|
||||||
@ -135,18 +150,31 @@ pub mod internal {
|
|||||||
let sys = Arc::clone(sysinfo);
|
let sys = Arc::clone(sysinfo);
|
||||||
let task = thread_pool.spawn(async move {
|
let task = thread_pool.spawn(async move {
|
||||||
let mut sys = sys.lock().unwrap();
|
let mut sys = sys.lock().unwrap();
|
||||||
|
let pid = sysinfo::get_current_pid().expect("Failed to get current process ID");
|
||||||
|
sys.refresh_processes(sysinfo::ProcessesToUpdate::Some(&[pid]), true);
|
||||||
|
|
||||||
sys.refresh_cpu_specifics(CpuRefreshKind::nothing().with_cpu_usage());
|
sys.refresh_cpu_specifics(CpuRefreshKind::nothing().with_cpu_usage());
|
||||||
sys.refresh_memory();
|
sys.refresh_memory();
|
||||||
let current_cpu_usage = sys.global_cpu_usage().into();
|
let system_cpu_usage = sys.global_cpu_usage().into();
|
||||||
// `memory()` fns return a value in bytes
|
let total_mem = sys.total_memory() as f64;
|
||||||
let total_mem = sys.total_memory() as f64 / BYTES_TO_GIB;
|
let used_mem = sys.used_memory() as f64;
|
||||||
let used_mem = sys.used_memory() as f64 / BYTES_TO_GIB;
|
let system_mem_usage = used_mem / total_mem * 100.0;
|
||||||
let current_used_mem = used_mem / total_mem * 100.0;
|
|
||||||
|
let process_mem_usage = sys
|
||||||
|
.process(pid)
|
||||||
|
.map(|p| p.memory() as f64 * BYTES_TO_GIB)
|
||||||
|
.unwrap_or(0.0);
|
||||||
|
|
||||||
|
let process_cpu_usage = sys
|
||||||
|
.process(pid)
|
||||||
|
.map(|p| p.cpu_usage() as f64 / sys.cpus().len() as f64)
|
||||||
|
.unwrap_or(0.0);
|
||||||
|
|
||||||
SysinfoRefreshData {
|
SysinfoRefreshData {
|
||||||
current_cpu_usage,
|
system_cpu_usage,
|
||||||
current_used_mem,
|
system_mem_usage,
|
||||||
|
process_cpu_usage,
|
||||||
|
process_mem_usage,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
tasks.tasks.push(task);
|
tasks.tasks.push(task);
|
||||||
@ -160,12 +188,22 @@ pub mod internal {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
diagnostics.add_measurement(&SystemInformationDiagnosticsPlugin::CPU_USAGE, || {
|
diagnostics.add_measurement(
|
||||||
data.current_cpu_usage
|
&SystemInformationDiagnosticsPlugin::SYSTEM_CPU_USAGE,
|
||||||
});
|
|| data.system_cpu_usage,
|
||||||
diagnostics.add_measurement(&SystemInformationDiagnosticsPlugin::MEM_USAGE, || {
|
);
|
||||||
data.current_used_mem
|
diagnostics.add_measurement(
|
||||||
});
|
&SystemInformationDiagnosticsPlugin::SYSTEM_MEM_USAGE,
|
||||||
|
|| data.system_mem_usage,
|
||||||
|
);
|
||||||
|
diagnostics.add_measurement(
|
||||||
|
&SystemInformationDiagnosticsPlugin::PROCESS_CPU_USAGE,
|
||||||
|
|| data.process_cpu_usage,
|
||||||
|
);
|
||||||
|
diagnostics.add_measurement(
|
||||||
|
&SystemInformationDiagnosticsPlugin::PROCESS_MEM_USAGE,
|
||||||
|
|| data.process_mem_usage,
|
||||||
|
);
|
||||||
false
|
false
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user