Linux: Show the number of CPU cores and sockets on your system
brian_s 270002K5X3 Comments (5) Visits (74523)
Finding the number of CPU cores on a Linux server can be challenging. The way /proc/cpuinfo displays information makes it hard to distinguish between real CPU cores and logical hyperthreading CPU's.
For example, consider if I run the following command on my Linux laptop:
$ cat /proc/cpuinfo | grep processor
At first glance it appears that my laptop has 4 CPU cores in it. However, it really has 2 CPU cores with hyperthreading enabled. The hyperthreading presents 2 logical CPU's to the operating system for each actual core so it effectively doubles the number of logical CPU's your system will see in /proc/cpuinfo.
The fields under /proc/cpuinfo that you need to compare to find the number of cores are "physical id" and "cored id". The "physical id" will identifier for the physical CPU socket. So to find the number of actual CPU sockets being used you can run this command:
In this example the computer only has 1 physical socket being used.
To show the total number of cores, you need to look at both "physical id" and "core id". "core id" is the identifier for each core on a CPU socket. If we run this command:
Put every 2 lines together (i.e. line 1 and 2, lines 3 and 4, lines 5 and 6, and lines 7 and 8) to see information about the 4 logical CPU's on the system. If you compare lines 1 and 2 to lines 3 and 4 you will see that they are identical. This is because lines 1-4 are actually a single CPU core that are presented as 2 logical CPU's due to hyperthreading. So based on this output we can see there are 2 unique CPU cores (lines 1-4 and lines 5-8).
Using the following command we can tie all of this together to show the number of cores on a system:
This command squishes the /proc/cpuinfo physical id and core id for each logical CPU on to the same line, and then sorts them and runs uniq to remove the extra hyperthreading logical CPU's. The final result is the number of CPU cores on your Linux system.
As previously mentioned you can also run this command to see the number of physical CPU sockets in use:
So in our example here we have a single CPU socket and 2 CPU cores.