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

Fix CPU computation on linux processes when not all of them are refreshed at once #1365

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
15 changes: 1 addition & 14 deletions src/unix/linux/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ macro_rules! to_str {
pub(crate) struct CpusWrapper {
pub(crate) global_cpu: CpuUsage,
pub(crate) cpus: Vec<Cpu>,
/// Field set to `false` in `update_cpus` and to `true` in `refresh_processes_specifics`.
///
/// The reason behind this is to avoid calling the `update_cpus` more than necessary.
/// For example when running `refresh_all` or `refresh_specifics`.
need_cpus_update: bool,
got_cpu_frequency: bool,
/// This field is needed to prevent updating when not enough time passed since last update.
last_update: Option<Instant>,
Expand All @@ -34,7 +29,6 @@ impl CpusWrapper {
Self {
global_cpu: CpuUsage::default(),
cpus: Vec::with_capacity(4),
need_cpus_update: true,
got_cpu_frequency: false,
last_update: None,
}
Expand All @@ -45,9 +39,7 @@ impl CpusWrapper {
only_update_global_cpu: bool,
refresh_kind: CpuRefreshKind,
) {
if self.need_cpus_update {
self.refresh(only_update_global_cpu, refresh_kind);
}
self.refresh(only_update_global_cpu, refresh_kind);
}

pub(crate) fn refresh(&mut self, only_update_global_cpu: bool, refresh_kind: CpuRefreshKind) {
Expand Down Expand Up @@ -76,7 +68,6 @@ impl CpusWrapper {
};
let buf = BufReader::new(f);

self.need_cpus_update = false;
let mut i: usize = 0;
let mut it = buf.split(b'\n');

Expand Down Expand Up @@ -193,10 +184,6 @@ impl CpusWrapper {
pub(crate) fn is_empty(&self) -> bool {
self.cpus.is_empty()
}

pub(crate) fn set_need_cpus_update(&mut self) {
self.need_cpus_update = true;
}
}

/// Struct containing values to compute a CPU usage.
Expand Down
5 changes: 1 addition & 4 deletions src/unix/linux/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ impl SystemInner {
processes_to_update,
refresh_kind,
);
if matches!(processes_to_update, ProcessesToUpdate::All) {
self.update_procs_cpu(refresh_kind);
self.cpus.set_need_cpus_update();
}
self.update_procs_cpu(refresh_kind);
nb_updated
}

Expand Down