Hyper-threading: what the second thread is worth
Video 019 of 140 in Virtualization Foundations, in the unit “CPUs: sockets, cores, threads and caches”. 3:37, with English and Hindi audio.
Intel calls it Hyper-Threading. Other chip makers call the same idea simultaneous multithreading, or SMT. Many server processors offer it, and it is often switched on. It makes one core look like two logical CPUs, but that is not the same as two cores. This article explains what the second thread really adds, where it helps and where it does not, and how to check whether it is on.
What you will learn
- Hyper-threading shows one core as two logical CPUs that share the same units.
- The second thread uses idle parts of the core while the first one waits.
- The commonly quoted gain lies between almost nothing and about thirty per cent.
- Never plan capacity as if a thread were a whole extra core.
A chef with two order tickets
Think of a kitchen. A core is one chef, and a chef cooks one dish at a time, with full attention. With hyper-threading, the chef keeps two order tickets open. When one dish waits for the oven, the chef works on the other.
But the chef still has one pair of hands and one set of knives. The second ticket does not make a second chef. In the same way, the second thread does not give a core a second set of its working parts.
What the second thread adds to one core
With one thread, the core works for that thread alone. When the thread stalls, for example while it waits for memory, the core's units sit idle. With two threads, the second one can use those idle units during the gap, so the core does more useful work in total.
Yet both threads still share the same units. When both are busy, each one gets less than a full core, and each one goes more slowly.
Note: The commonly quoted gain from a second thread lies somewhere between zero and about thirty per cent. It depends on the work, so it can be almost nothing.
Where a second thread helps and where it does not
- Waiting work gains the most: many small requests that often wait on memory or disk, such as a server answering many users.
- Busy work gains the least: heavy number crunching keeps every unit busy, so the two threads simply take turns.
- The only honest test is to measure your own workload, with hyper-threading on and then off.
On a shared host, a busy thread on the same core as yours can slow your work, even when your own count looks fine. The most common mistake is to plan as if each thread were a full extra core. Capacity then looks far bigger than it really is.
How to check whether hyper-threading is on
Run lscpu and look for the threads-per-core line. A value of two means each core shows two logical CPUs, and hyper-threading is on. A value of one means it is off, and each core is one logical CPU.
lscpu | grep 'Thread(s) per core'Note: When the output reads Thread(s) per core: 2, hyper-threading is on. Planning from the logical CPU count overstates the capacity of the machine.
Terms
- Hyper-threading
- Intel's name for simultaneous multithreading, which shows one core as two logical CPUs.
- Logical CPU
- One processing unit that Linux sees, which may be a whole core or one thread of a core.
- Thread
- One stream of work that a core runs. A core with hyper-threading runs two at a time.
Check yourself
Why does a second thread never double the speed of a core?
Both threads share the same working parts of the core. The second thread only uses the parts that are idle while the first one waits.
What does Thread(s) per core: 2 tell you?
Each core shows two logical CPUs, so hyper-threading is on.
Which kind of work gains the most from a second thread?
Work that often waits on memory or disk, such as many small requests. Heavy number crunching gains the least.
Previous: Sockets, cores and threads: what a CPU count means. Next: Clock speed, turbo and why GHz is not speed.