procfs icon indicating copy to clipboard operation
procfs copied to clipboard

Make CpuInfo iterable

Open conqp opened this issue 1 year ago • 2 comments

I have a use case where I want to check all cores in /proc/cpuinfo for certain properties. While it is currently possible to iterate over the cores by iterating over the range of CPUs and then accessing each one with the respective getter functions that take the index, this is very ugly and cumbersome.

Hence, I suggest to implement an iterator for CpuInfo that yields structs representing information of the respective core.

conqp avatar Jul 03 '23 13:07 conqp

Hi :wave:

Sorry, I'm not quite understanding your use-case, and why it's cumbersome with the current API. Can you elaborate? It's not obvious to me why iterating over CpuInfo is better than iterating over the CpuInfo.cpus

eminence avatar Aug 18 '23 03:08 eminence

Iterating over the public Vec CpuInfo.cpus's values will only yield raw HashMap<String, String>s instead of objects that have parsed properties of the actual CPU core. As I want to be able to e.g. access core.model_name() for each core, I cannot do this with your current API, since CpuInfo.model_name() currently requires the passing of the index of the desired core, in which case I did not have to iterate in the first place, but would have to check CpuInfo.cpus's length and iterate over an appropriate range to call CpuInfo.model_name() on each and every index. Anyhow, I meanwhile wrote an own crate, that fits my needs: https://github.com/conqp/proc_cpuinfo

conqp avatar Aug 18 '23 09:08 conqp