Closed reza-ebrahimi closed 3 years ago
You are not using the measurement correctly — when cpu_load_aggregate()
returns, you call done()
immediately, which completely misses the point of why this is split into two steps :) You're measuring a tiny microsecond interval, not the interval you want to measure. The thread::sleep(Duration::from_secs(1));
in the example is there for a reason.
With a loop, what you probably want to do is store the cpu_load_aggregate()
result for the next iteration and look at the current one. Something along the lines of:
/* every 250 ms */ {
if let Ok(cpu) = load {
let cpu = cpu.done().unwrap(); /* … */
}
load = sys.cpu_load_aggregate();
}
@unrelentingtech Thank you, It works now :)
My
4 CPU cores
are in100%
load but thecpu_load_aggregate
output rarely goes to10%
of usage. Most of the times all of them are0
.Following code is called every
250ms
: