-
-
Notifications
You must be signed in to change notification settings - Fork 334
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
incorrect process cpu usage? #1225
Comments
Without code, not much I can do. |
below is the code i used #[derive(Debug)]
struct Output {
global_cpu_percentage: f64,
processes_status: Vec<ProcessStatus>
}
#[derive(Debug)]
struct ProcessStatus {
cpu_percentage: f64,
pid: u32,
name: String,
memory_bytes: f64,
}
fn main() {
let mut sys = sysinfo::System::new_all();
loop {
std::thread::sleep(std::time::Duration::from_secs(1));
sys.refresh_specifics(
sysinfo::RefreshKind::new()
.with_memory(sysinfo::MemoryRefreshKind::everything())
.with_cpu(sysinfo::CpuRefreshKind::everything()),
);
let global_cpu_percentage = sys.global_cpu_info().cpu_usage() as f64;
// list of process interested to be monitored by us, below is just a sample of process name only
let processes_name = vec![
"htop",
".vscode"
];
let processes_status = processes_name
.into_iter()
.flat_map(|process_name| sys.processes_by_name(&process_name))
.map(|process| process.pid())
.collect::<Vec<_>>()
.into_iter()
.filter_map(|pid| {
sys.refresh_process_specifics(
pid,
sysinfo::ProcessRefreshKind::new().with_cpu().with_memory(),
);
let process = sys.processes().get(&pid)?;
let name = process
.exe()
.unwrap()
.to_str()
.unwrap_or_else(|| process.name())
.to_string();
Some(ProcessStatus {
cpu_percentage: process.cpu_usage() as f64,
pid: pid.as_u32(),
name,
memory_bytes: process.memory() as f64,
})
})
.collect::<Vec<_>>();
let output = Output {
global_cpu_percentage,
processes_status
};
println!("{:?}", output);
}
} |
Code looks good. Not sure why you get processes CPU usage above 1200 when adding them though. As for global CPU usage, it's the total percentage of CPU usage across the system, so its maximum is 100% whereas processes can use more than one CPU so they can use more than 100%. |
we think the global cpu usage is correct, but not the processes cpu usage we tried to get the same information using the python package of psutil, |
One last question: what does |
i am not sure if this is the cause, if we refresh all the processes at start we dont get the random CPU spikes anymore sysinfo::RefreshKind::new()
.with_processes(sysinfo::ProcessRefreshKind::new().with_cpu().with_memory()) it seems that refreshing individual process by PID inside an iterator will cause the random CPU spike |
Ah ok, I got your issue. As explained in the documentation:
So the first CPU usage reading is always invalid. |
I think my answer was completely off target. Looking at your code again, you did it correctly. The only thing I'm suspicious about is the |
we made the changes as suggested, it seems the random CPU spike does not exist anymore, do you know what could cause this behavior in the in addition, we are not able to query the
|
So the call you wrote is exactly what you need to do first instead of Like mentioned, I think the issue is that |
I opened #1234 to remove the |
Describe the bug
we make an application that runs at 1Hz to get
we found that the cpu usage for the processes is inaccurate, we do get random spike and if we sum all the processes cpu usage, it is not the same as the overall CPU usage
below is the screenshot, the top graph (blue line) indicates the overall CPU usage, and the graph below shows each processes cpu usage.
if we sum the cpu usage of each processes on the 2nd graph(the red vertical line, with numbers dialog), the total cpu usage is > 1200%,
system:
version:
may we know what is the causes for this inaccuracy, and how can we improve it?
The text was updated successfully, but these errors were encountered: