let f = File::open("/proc/cpuinfo")
.block_error("load", "Your system doesn't support /proc/cpuinfo")?;
let f = BufReader::new(f);
let mut logical_cores = 0;
for line in f.lines().scan((), |_, x| x.ok()) {
// TODO: Does this value always represent the correct number of logical cores?
if line.starts_with("siblings") {
let split: Vec<&str> = (&line).split(' ').collect();
logical_cores = split[1]
.parse::<u32>()
.block_error("load", "Invalid Cpu info format!")?;
break;
}
}
My system supports /proc/cpuinfo, but it doesn't have any lines starting with siblings. The format may have changed (I'm now on linux kernel 5.7.0).
Reading /sys/devices/system/cpu/present might be a better alternative. On my system, its value is 0-5, I believe representing the fact that the six CPUs numbered 0 to 5 are all present. The fact it doesn't just say 6 might make it a little more complicated to parse.
Other options include calling lscpu (although that requires that util-linux is installed), or delegating the task to another crate, such as cpuinfo (I have created PR #748 which would fix this issue by using the cpuinfo crate).
Also, if we still can't find the number of CPUs, 1 might be a more sensible value than 0.
A robust solution would be to find out how lscpu actually works and treat that as gospel. The source is here: https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/ but I haven't been able to find it with a quick look around.
@Thra11 If #859 didn't fix this for you then please re-open
Having updated to HEAD, currently "idling" at 18.31 load, so something's not quite right.
Will try to debug it. Btw, I don't appear to have the option to reopen this.
Hmmm. Looks like it's probably correct actually, my system's just too busy to get anywhere near the default thresholds at the moment. It is correctly dividing 18.31 by 6 logical cores to get a used_perc of 3.05, which is way over the default critical value of 0.9, but appears to be correct.
Most helpful comment
A robust solution would be to find out how
lscpuactually works and treat that as gospel. The source is here: https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/ but I haven't been able to find it with a quick look around.