I would investigate how lscpu does it on Linux by seeing its source code and whether it is exposed outside of the kernel for normal user access.
|
return sysconf(_SC_NPROCESSORS_ONLN); |
This command gets the desired information:
lscpu | grep 'CPU(s):' | uniq | cut -d' ' -f4- | awk 'NR==1{$1=$1;print}'
sysconf(_SC_NPROCESSORS_ONLN) gets the number of online CPUs, if any of them are offline then you will get a value you are not looking for.
I might fix this in a future PR when I have time. Just wanted to document this here for later.