Hi everyone. I am trying to get the CPU usage of processes belonging to Docker containers. My attempt is as follows:
fn update_cpu_process_info(system_info: &mut SysInfo) {
let mut system = System::new_all();
std::thread::sleep(Duration::from_millis(200));
system.refresh_processes_specifics(
ProcessesToUpdate::All,
ProcessRefreshKind::new().with_cpu());
std::thread::sleep(Duration::from_millis(200));
for proc in &mut system_info.processes {
let pid : Pid = Pid::from_u32(proc.pid);
if let Some(docker_proc) = system.process(pid) {
println!("PID: {}", docker_proc.pid());
println!("{}", docker_proc.cpu_usage());
}
}
}
I have the PIDs of the Docker container processes stored in the SysInfo struct in the processes field. It should be noted that I have verified that the processes are running and correspond to the containers.
The problem is that for the cpu_usage field I get a value of 0 for all processes. I can access all other fields like mem_usage without problems and get a reasonable value, but cpu_usage is always 0. What could be happening? What is the correct way to get that value?
Any help is appreciated.