Hyper-Threading: How Does It Double CPU Throughput?
Join the DZone community and get the full member experience.Join For Free
the other day a customer asked me to do capacity planning for their web server farm. i was looking at the cpu graph for one of the web servers that had hyper-threading switched on and thought to myself: “this must be quite a misleading graph – it shows 30% cpu usage. it can’t really be that this server can handle 3 times more work?”
or can it?
i decided to do what we usually do in such case – i decided to test it and find out the truth. turns out – there’s more to it than meets the eye.
how intel hyper-threading works
before we get to my benchmark results, let’s talk a little bit about hyper-threading. according to intel , intel® hyper-threading technology (intel® ht technology) uses processor resources more efficiently, enabling multiple threads to run on each core. as a performance feature, intel ht technology also increases processor throughput, improving overall performance on threaded software.
sounds almost like magic, but in reality (and correct me if i’m wrong), what ht does essentially is – by presenting one cpu core as two cpus (threads rather), it allows you to offload task scheduling from kernel to cpu.
so for example if you just had one physical cpu core and two tasks with the same priority running in parallel, the kernel would have to constantly switch the context so that both tasks get a fair amount of cpu time. if, however, you have the cpu presented to you as two cpus, the kernel can give each task a cpu and take a vacation.
on the hardware level, it will still be one cpu doing the same amount of work, but there maybe some optimization to how that work is going to be executed.
here’s the problem that was driving me nuts: if ht does not actually give you twice more power and yet the system represents statistics for each cpu thread separately, then at 50% cpu utilization (as per mpstat on linux), the cpu should be maxed out.
so if i tried to model the scalability of that web server – a 12-core system with ht enabled (represented as 24 cpus on a system), assuming perfect linear scalability, here’s how it should look:
throughput (requests per second) | 9 | ,+++++++++++++++ | + | + 6 | + | + | + 3 | + | + | + 0 '-----+----+----+----+---- 1 6 12 18 24
in the example above, single cpu thread could process the request in 1.2s, which is why you see it max out at 9-10 requests/sec (12/1.2).
from the user perspective, this limitation would hit very unexpectedly, as one would expect 50% utilization to be… well, exactly that – 50% utilization.
in fact, the cpu utilization graph would look even more frustrating. for example if i were increasing the number of parallel requests linearly from 1 to 24, here’s how that relationship should look:
cpu utilization: 100% | ++++++++++++++ | . | . | . | . 50% | . | + | + | + | + 0% '----+----+----+----+---- 0 6 12 18 24
hence cpu utilization would skyrocket right at 12 cores from 50% to 100%, because in fact the system cpu would be 100% utilized at this point.
what happens in reality
naturally, i decided to run a benchmark and see if my assumptions are correct. the benchmark was pretty basic – i wrote a cpu-intensive php script, that took 1.2s to execute on the cpu i was testing this, and bashed it over http (apache) with ab at increasing concurrency. here’s the result:
raw data can be found here .
if this does not blow your mind, please go over the facts again and then back at the graph.
still not sure why i find this interesting? let me explain. if you look carefully, initially – at concurrency of 1 through 8 – it scales perfectly. so if you only had data for threads 1-8 (and you knew processes don’t incur coherency delays due to shared data structures), you’d probably predict that it will scale linearly until it reaches ~10 requests/sec at 12 cores, at which point adding more parallel requests would not have any benefits as the cpu would be saturated.
what happens in reality, though, is that past 8 parallel threads (hence, past 33% virtual cpu utilization), execution time starts to increase and maximum performance is only achieved at 24-32 concurrent requests. it looks like at the 33% mark there’s some kind of “throttling” happening.
in other words, to avoid a sharp performance hit past 50% cpu utilization, at 33% virtual thread utilization (i.e. 66% actual cpu utilization), the system gives the illusion of a performance limit – execution slows down so that the system only reaches the saturation point at 24 threads (visually, at 100% cpu utilization).
naturally then the question is – does it still make sense to run hyper-threading on a multi-core system? i see at least two drawbacks:
1. you don’t see the real picture of how utilized your system really is – if the cpu graph shows 30% utilization, your system may well be 60% utilized already.
2. past 60% physical utilization, execution speed of your requests will be throttled intentionally in order to provide higher system throughput.
so if you are optimizing for higher throughput – that may be fine. but if you are optimizing for response time, then you may consider running with ht turned off.
did i miss something?
Published at DZone with permission of Peter Zaitsev, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.