Find out number of cores / CPUs for a linux system
Tuesday, September 7th, 2010If you need to find out the number of CPUs or CPU cores or cores per CPU of your system, you could look it up in /proc/cpuinfo but it’s quite hard to figure out the right parameters. A good overview on the parameters for different system configurations can be found here. If you want to put the actual numbers in variables, here is a nice way to do it:
export CORES_PER_CPU=`grep -c "physical id.*: 0" /proc/cpuinfo` export CPU_TOTAL=`grep -c "core id.*: 0" /proc/cpuinfo` export CORE_TOTAL=`grep -c processor /proc/cpuinfo`
/proc/cpuinfo shows an entry for each CPU core. The physical id is incremented for each physical CPU. If the entry has the same physical id as another core, the core belongs to the same CPU. Therefore counting the number of entries with physical id set to 0 results in the number of cores per CPU. The core id is incremented for each core on a physical CPU. Therefore counting the number of entries with core id set to 0 results in the number of physical CPUs. The total number of cores can be retrieved quite easily by counting the number of processor entries.
Unfortunately the above method does not work on all systems. I noticed on some systems with single core processors, that the values core id and physical id are not present.
I searched for official documentation on the proc filesystem, but only found the following document which doesn’t describe the cpuinfo values:
If someone happens to know a better documentation I would be glad if he/she would share it with me!