How to know number of cores of a system in Linux?
To get a complete picture you need to look at the number of threads per core, cores per socket and sockets. If you multiply these numbers you will get the number of CPUs on your system.
CPUs = Threads per core X cores per socket X sockets
CPUs are what you see when you run htop
(these do not equate to physical CPUs).
Here is an example from a desktop machine:
$ lscpu | grep -E '^Thread|^Core|^Socket|^CPU\('
CPU(s): 8
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
And a server:
$ lscpu | grep -E '^Thread|^Core|^Socket|^CPU\('
CPU(s): 32
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 2
The output of nproc
corresponds to the CPU count from lscpu
. For the desktop machine above this should match the 8 CPU(s) reported by lscpu
:
$ nproc --all
8
The output of /proc/cpuinfo
should match this information, for example on the desktop system above we can see there are 8 processors (CPUs) and 4 cores (core id 0-3):
$ grep -E 'processor|core id' /proc/cpuinfo
processor : 0
core id : 0
processor : 1
core id : 0
processor : 2
core id : 1
processor : 3
core id : 1
processor : 4
core id : 2
processor : 5
core id : 2
processor : 6
core id : 3
processor : 7
core id : 3
The cpu cores
reported by /proc/cpuinfo
corresponds to the Core(s) per socket
reported by lscpu
. For the desktop machine above this should match the 4 Core(s) per socket reported by lscpu:
$ grep -m 1 'cpu cores' /proc/cpuinfo
cpu cores : 4
To specifically answer your question you tell how many cores you have by multiplying the number of cores you have per socket by the number of sockets you have.
Cores = Cores per socket X Sockets
For the example systems above the desktop has 4 cores:
$ echo "Cores = $(( $(lscpu | awk '/^Socket\(s\)/{ print $2 }') * $(lscpu | awk '/^Core\(s\) per socket/{ print $4 }') ))"
Cores = 4
While the server has 16:
$ echo "Cores = $(( $(lscpu | awk '/^Socket\(s\)/{ print $2 }') * $(lscpu | awk '/^Core\(s\) per socket/{ print $4 }') ))"
Cores = 16
Another useful utility is dmidecode
which outputs per socket information. In the case of the server system listed above we expect to see 8 cores per socket and 16 threads per socket:
$ sudo dmidecode -t 4 | grep -E 'Socket Designation|Count'
Socket Designation: CPU1
Core Count: 8
Thread Count: 16
Socket Designation: CPU2
Core Count: 8
Thread Count: 16
The lscpu
command has a number of useful options that you may like to check out, for example:
$ lscpu --all --extended
$ lscpu --all --parse=CPU,SOCKET,CORE | grep -v '^#'
See man lscpu
for details.
In summary:
- You need to be aware of sockets, cores and threads
- You need to be careful of the term CPU as it means different things in different contexts
You have to look at sockets and cores per socket. In this case you have 1 physical CPU (socket) which has 4 cores (cores per socket).
You can get this information by nproc(1)
command
$ nproc --all
12
It does not require root privileges.