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

Improve CPU usage efficiency #1448

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Changes from 2 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
42 changes: 31 additions & 11 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::process::{self, ExitStatus};
use std::ptr::null_mut;
use std::str;
use std::sync::{Arc, OnceLock};
use std::time::Instant;

use libc::c_void;
use ntapi::ntexapi::SYSTEM_PROCESS_INFORMATION;
Expand Down Expand Up @@ -47,6 +48,8 @@ use windows::Win32::System::Threading::{
};
use windows::Win32::UI::Shell::CommandLineToArgvW;

use super::MINIMUM_CPU_UPDATE_INTERVAL;

impl fmt::Display for ProcessStatus {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match *self {
Expand Down Expand Up @@ -196,6 +199,7 @@ struct CPUsageCalculationValues {
old_process_user_cpu: u64,
old_system_sys_cpu: u64,
old_system_user_cpu: u64,
last_update: Instant,
}

impl CPUsageCalculationValues {
Expand All @@ -205,6 +209,7 @@ impl CPUsageCalculationValues {
old_process_user_cpu: 0,
old_system_sys_cpu: 0,
old_system_user_cpu: 0,
last_update: Instant::now(),
}
}
}
Expand Down Expand Up @@ -962,18 +967,33 @@ pub(crate) fn compute_cpu_usage(p: &mut ProcessInner, nb_cpus: u64) {
if let Some(handle) = p.get_handle() {
let _err = GetProcessTimes(handle, &mut ftime, &mut ftime, &mut fsys, &mut fuser);
}
// FIXME: should these values be stored in one place to make use of
// `MINIMUM_CPU_UPDATE_INTERVAL`?
let _err = GetSystemTimes(
Some(&mut fglobal_idle_time),
Some(&mut fglobal_kernel_time),
Some(&mut fglobal_user_time),
);

let sys = filetime_to_u64(fsys);
let user = filetime_to_u64(fuser);
let global_kernel_time = filetime_to_u64(fglobal_kernel_time);
let global_user_time = filetime_to_u64(fglobal_user_time);
// by default, use previous values
let mut global_kernel_time: u64 = p.cpu_calc_values.old_system_sys_cpu;
Copy link
Owner

Choose a reason for hiding this comment

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

At this point, why not just return the previously computed 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.

I apologize for my significant oversight. I revised the code to return the unchanged process CPU usage if the last_update field hasn't exceeded the time it takes to update the CPU values. It should be good now. Again, passed all cargo fmt, clippy and test commands.

let mut global_user_time: u64 = p.cpu_calc_values.old_system_user_cpu;
let mut sys: u64 = p.cpu_calc_values.old_process_sys_cpu;
let mut user: u64 = p.cpu_calc_values.old_process_user_cpu;

// check if the time since we last updated these previous values
// exceeds MINIMUM_CPU_UPDATE_INTERVAL. If so, these values may have
// update, so we need to get the most recent values
if p.cpu_calc_values.last_update.elapsed() > MINIMUM_CPU_UPDATE_INTERVAL {
// system times have changed, we need to get most recent system times
// and update the cpu times cache, as well as global_kernel_time and global_user_time

let _err = GetSystemTimes(
Some(&mut fglobal_idle_time),
Some(&mut fglobal_kernel_time),
Some(&mut fglobal_user_time),
);

// set the times to most recent
sys = filetime_to_u64(fsys);
user = filetime_to_u64(fuser);
global_kernel_time = filetime_to_u64(fglobal_kernel_time);
global_user_time = filetime_to_u64(fglobal_user_time);
p.cpu_calc_values.last_update = Instant::now();
}

let delta_global_kernel_time =
check_sub(global_kernel_time, p.cpu_calc_values.old_system_sys_cpu);
Expand Down
Loading