Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Update sysinfo and improve its use a bit #7826

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_diagnostic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0" }
# MacOS
[target.'cfg(all(target_os="macos"))'.dependencies]
# Some features of sysinfo are not supported by apple. This will disable those features on apple devices
sysinfo = { version = "0.27.1", default-features = false, features = [
sysinfo = { version = "0.28.1", default-features = false, features = [
"apple-app-store",
] }

# Only include when not bevy_dynamic_plugin and on linux/windows/android
[target.'cfg(any(target_os = "linux", target_os = "windows", target_os = "android"))'.dependencies]
sysinfo = { version = "0.27.1", default-features = false }
sysinfo = { version = "0.28.1", default-features = false }
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl SystemInformationDiagnosticsPlugin {
pub mod internal {
use bevy_ecs::{prelude::ResMut, system::Local};
use bevy_log::info;
use sysinfo::{CpuExt, System, SystemExt};
use sysinfo::{CpuExt, CpuRefreshKind, RefreshKind, System, SystemExt};

use crate::{Diagnostic, Diagnostics};

Expand Down Expand Up @@ -69,23 +69,19 @@ pub mod internal {
mut sysinfo: Local<Option<System>>,
) {
if sysinfo.is_none() {
*sysinfo = Some(System::new_all());
*sysinfo = Some(System::new_with_specifics(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

System::new_all will retrieve a lot more information than what is being used here like all processes, network interfaces, etc... Since you only care about system memory and CPU usage, no need for everything else. Especially since you'll keep this one around! It should drop the memory impact by quite a lot.

RefreshKind::new()
.with_cpu(CpuRefreshKind::new().with_cpu_usage())
.with_memory(),
));
}
let Some(sys) = sysinfo.as_mut() else {
return;
};

sys.refresh_cpu();
sys.refresh_cpu_specifics(CpuRefreshKind::new().with_cpu_usage());
sys.refresh_memory();
let current_cpu_usage = {
let mut usage = 0.0;
let cpus = 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
};
let current_cpu_usage = sys.global_cpu_info().cpu_usage();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to go through all CPU to get the average information, it is already available in the global_cpu_info.

// `memory()` fns return a value in bytes
let total_mem = sys.total_memory() as f64 / BYTES_TO_GIB;
let used_mem = sys.used_memory() as f64 / BYTES_TO_GIB;
Expand Down