I3status-rust: load block fails to get cpu count

Created on 17 Jun 2020  路  6Comments  路  Source: greshake/i3status-rust

        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.

awaiting confirmation of fix bug

Most helpful comment

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.

All 6 comments

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.

I believe the journey starts here, with relevant bits here and here and here.

Haven't read through all of it yet, but I get the impression it's basically reading /sys/devices/system/cpu/present and doing stuff with the "cpu set" that it represents.

@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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

concatime picture concatime  路  4Comments

michaelmrose picture michaelmrose  路  4Comments

oschoudhury picture oschoudhury  路  6Comments

Rikorose picture Rikorose  路  5Comments

fedario picture fedario  路  7Comments