Short Answer
The number of physical cores:
> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores : 2
The number of virtual cores (e.g. 2x number of physical cores with hyper threading):
> cat /proc/cpuinfo | grep -c "cpu cores"
4
If you have more than one CPU/processor (in this sense) on your mainboard this does not properly work. See section "Several CPUs per mainboard". This may be the case if you work on a computing cluster or on a high-end desktop workstation for CAD/Engineering-purposes.
Long Answer
The command cat /proc/cpuinfo
should print out the number of physical cores in the line cpu cores
. But it prints out this information for each virtual core. Thus, if you want to have the number of physical core, you just take the first occurrence of the line cpu cores
, which is
> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores : 2
Alternatively, if you are looking for the number of virtual cores, you count the number of times the line cpu cores
is found, which is
> cat /proc/cpuinfo | grep -c "cpu cores"
4
Example
I have got a Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
(4 physical cores, hyperthreading). Lets see what I get:
> cat /proc/cpuinfo | grep "cpu cores"
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
cpu cores : 4
Number of physical cores:
> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores : 4
Number of virtual cores:
> cat /proc/cpuinfo | grep -l "cpu cores"
8
You could also take the last found processor number and increment it by one
> cat /proc/cpuinfo | grep "processor" | tail -1
processor : 7
# +1
Several CPUs per mainboard
I just looked onto one node of a computing cluster, on which I am currently working: 1 node has 4 CPUs (Intel Xeon) with each 8 physical cores; each CPU supports hyptherthreading; therefore, each CPU has 16 virtual cores; summing it up, the one node has 32 physical and 64 virtual cores;
cat /proc/cpuinfo
prints out information for each virtual core. Thus, we get 64 'packages' of information. If we have such a setup, we need to consider the row physical id
in cat /proc/cpuinfo
.
This is the output I get:
> cat /proc/cpuinfo | grep -m 1 "cpu cores"
cpu cores : 8
> cat /proc/cpuinfo | grep -c "cpu cores"
64
> cat /proc/cpuinfo | grep "physical id" | tail -1
physical id : 3
# this result +1 => number of CPUs
Thus, "number behind cpu cores
" x "number behind physical id
+ 1" is the number of physical cores one our node (8 x (3 + 1)). The 64 is the number of virtual cores.
Nomenclature
Since everyone uses cores, CPU and processor in another context, I introduce a nomenclature for my answer here:
- processor: the whole piece of hardware (e.g. my Intel® Core™ i7-3740QM Processor)
- CPU: CPU = processor
- core or physical core: number of physical calculation units in the CPU
- thread or virtual core: number of threads that can run on parallel on one CPU; if the (intel-)processor is able to perform hyperthreading the number of virtual cores is twice the number of physical cores (I am not sure how other processor vendors call this feature);
__Example: __
- processor: Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
- source: Intel Ark
Data:
- CPU number: 1
- processor number: 1
- cores (or physical cores): 4
- threads (or virtual cores): 8
cpu cores
line seems to show the number of cores on the processor (socket) that particular core is on. I get output identical to your example on one dual-socket dual-core system (that has a total of 4 cores, not 8). – ilkkachu Jul 12 '17 at 09:57cpu cores: 2
line stems from the fact, that a total of 2xphysical cores are on the host machine. – FloHe Jul 12 '17 at 10:06cat /proc/cpuinfo
should print out the number of physical cores. But it prints out this information for each virtual core. Thus I would expect that you have a dual core CPU with hyperthreading (or how its called by non-intel processors). – daniel.heydebreck Jul 12 '17 at 11:39Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
has 4 physical cores and supports hyperthreading. If I printcat /proc/cpuinfo
I get 8 times the linecpu cores : 4
because I have 8 virtual cores (2 per physical core) and thiscpu cores
information is printed out once per virtual core. – daniel.heydebreck Jul 12 '17 at 11:42