I was googling about how I could find the number of CPUs in a machine and I found some posts but I am confused as some mentioned that you get the logical cores vs physical cores etc.
So what is the difference between logical and physical cores and is there a way I could get the physical cores only? Or does it make sense to include logical cores in our count?
-
4This question, even if interesting, has nothing to deal with Linux or Unix ...– binarymMar 27, 2020 at 10:56
5 Answers
Physical cores are just that, physical cores within the CPU. Logical cores are the abilities of a single core to do 2 or more things simultaneously. This grew out of the early Pentium 4 CPUs ability to do what was termed Hyper Threading (HTT).
It was a bit of a game that was being played where sub components of the core weren't being used for certain types of instructions while, another long running instruction might have been being executed. So the CPU could in effect work on 2 things simultaneously.
Newer cores are more full-fledged CPUs so they're working on multiple things simultaneously, but they aren't true CPUs as the physical cores are. You can read more about the limitations of the hyperthreading functionality vs. the physical capabilities of the core here on tomshardware in this article titled: Intel Core i5 And Core i7: Intel’s Mainstream Magnum Opus.
You can see the breakdown of your box using the lscpu
command:
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
CPU(s): 4
Thread(s) per core: 2
Core(s) per socket: 2
CPU socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 37
Stepping: 5
CPU MHz: 2667.000
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 3072K
NUMA node0 CPU(s): 0-3
In the above my Intel i5 laptop has 4 "CPUs" in total
CPU(s): 4
of which there are 2 physical cores (1 socket × 2 cores/socket = 2 cores)
Core(s) per socket: 2
CPU socket(s): 1
of which each can run up to 2 threads
Thread(s) per core: 2
at the same time. These threads are the core's logical capabilities.
-
1The raw info that
lscpu
uses is exposed here:cat /proc/cpuinfo
. What's your OS?– slm ♦Aug 27, 2013 at 19:16 -
1@ThomasWeller - see my other A'er here - unix.stackexchange.com/questions/113544/….– slm ♦Nov 7, 2018 at 8:47
-
3
Core(s) per socket
timesThread(s) per core: 2
=CPU(s): 4
? Correct? Oct 16, 2019 at 17:19 -
1
-
1@mbigras - correct. Be careful w/ the term thread since there are various types. Here 1 thread can consume a logical core, so 1-to-1.– slm ♦Jun 27, 2020 at 16:29
Physical cores are number of physical cores, actual hardware components.
Logical cores are the number of physical cores times the number of threads that can run on each core through the use of hyperthreading.
for example, my 4-core processor runs two threads per core, so I have 8 logical processors.
-
So this number makes sense only if using a processor that supports hyperthreading? And what does this mean?Still one thread will occupy each cpu at a time right?So what does this number mean?– JimAug 26, 2013 at 22:11
-
-
not necessarily, simultaneous multithreading, for example, allows for two threads to be run at the same time on a single core. There are a number of other advantages as well.– ashAug 26, 2013 at 22:15
-
Hyperthreading technology allows a single physical processor core to behave like two logical processors.
So a single processor core can execute two independent threads simultaneously.
Intel refers to a physical processor as a socket.
Hyperthreading makes a Physical Processor to behave like it has two Physical Processors, which are called Logical Processor. why?
While hyperthreading does not double the performance of a system, it can increase performance by better utilizing idle resources leading to greater throughput for certain important workload types. An application running on one logical processor of a busy core can expect slightly more than half of the throughput that it obtains while running alone on a non-hyperthreaded processor.
Summary
- Physical Processor are that we can see and feel.
- Logical Processor is like, a Physicals Core acting as Two Physical Core
-
-
The person who wrote this answer does not appear to understand the difference between a socket and a physical core. Today, most physical CPU hardware components that go into a single physical socket have multiple cores onboard. Nov 25, 2019 at 15:32
$ sudo dmidecode |egrep "Socket Designation: Proc|((Thread|Core) Count)"
Socket Designation: Proc 1
Core Count: 14
Thread Count: 28
Socket Designation: Proc 2
Core Count: 14
Thread Count: 28
Two sockets. Each socket has 14 physical cores. Each core has two threads (28/14). Total number of logical "cpus" or logical processing units is 56 (that's what "top" and some other commands would show you as number of "cpus").
-
What baffles me is that on my i3-3220 with your approach I get core count 2 and thread count 2 (=4) but
lscpu shows CPUs 4 and threads per 2 (=8) but
lshw -C cpu` shows 16 logical cpu's numbered 0 - 15 Feb 7, 2016 at 18:04 -
dmidecode interprets what's in BIOS.. not sure exactly what's wrong in your case, e.g. you may have hyperthreading disabled in BIOS or something like that. lscpu and lshw are OS commands so show what OS kernel has recognized. Intel spec for your processor show 2 cores 4 threads.– TagarFeb 8, 2016 at 5:10
-
Wouldn't having hyperthreading disabled result in fewer logical cores rather than more? Feb 8, 2016 at 18:17
-
I understood that you got 2 cores 2 threads reported by dmidecode instead of 2 cores 4 threads real number of threads/cores i3-3220 has. So it is smaller. And that was just a guess, I don't know if you actually have hyperthreading disabled in BIOS or not.– TagarFeb 8, 2016 at 21:36
-
It seems I've misinterpreted
lscpu
output and it's actually 4 threads total but "lshw -C cpu` shows 16 logical CPU’s" I'm not sure what to make of that. Feb 9, 2016 at 2:36
Logical cores (hyperthreading) are a cheaper and slower alternative to having multiple-physical cores
The Intel Manual Volume 3 System Programming Guide - 325384-056US September 2015 8.7 "INTEL HYPER-THREADING TECHNOLOGY ARCHITECTURE" describes HT briefly. It contains the following diagram:
TODO it is slower by how much percent in average in real applications?
Hyperthreading is possible because modern single CPUs cores already execute multiple instructions at once with the instruction pipeline https://en.wikipedia.org/wiki/Instruction_pipelining
The instruction pipeline is a separation of functions inside of a single core to ensure that each part of the circuit is used at any given time: reading memory, decoding instructions, executing instructions, etc.
Hyperthreading separates functions further by using:
a single backend, which actually runs the instructions with its pipeline.
Dual core has two backends, which explains the greater cost and performance.
two front-ends, which take two streams of instructions and order them in a way to maximize pipelining usage of the single backend by avoiding hazards.
Dual core would also have 2 front-ends, one for each backend.
There are edge cases where instruction reordering produces no benefit, making hyperthreading useless. But it produces a significant improvement in average.
Two hyperthreads in a single core share further cache levels (TODO how many? L1?) than two different cores, which share only L3, see:
- https://stackoverflow.com/questions/4802565/multiple-threads-and-cpu-cache/54018751#54018751
- https://stackoverflow.com/questions/944966/how-are-cache-memories-shared-in-multicore-intel-cpus/33510874#33510874
The interface that each hyperthread exposes to the operating system is similar to that of an actual core, and both can be controlled separately. Thus cat /proc/cpuinfo
shows me 4 processors, even though I only have 2 cores with 2 hyperthreads each.
Operating systems can however take advantage of knowing which hyperthreads are on the same core to run multiple threads of a given program on a single core, which might improve cache usage.
This LinusTechTips video contains a light-hearted non-technical explanation: https://www.youtube.com/watch?v=wnS50lJicXc
"Simultaneous multithreading" is a more general/less Intel-specific name for this type of technique.