Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overhead of Spin Loop in terms of cache coherence

Say a thread in one core is spinning on a variable which will be updated by a thread running on another core. My question is what is the overhead at cache level. Will the waiting thread cache the variable and therefore does not cause any traffic on the bus until the writing thread writes to that variable?

How can this overhead be reduced. Does x86 pause instruction help?

like image 784
MetallicPriest Avatar asked Oct 11 '22 01:10

MetallicPriest


2 Answers

I believe all modern x86 CPUs use the MESI protocol. So the spinning "reader" thread will likely have a cached copy of the data in either "exclusive" or "shared" mode, generating no memory bus traffic while you spin.

It is only when the other core writes to the location that it will have to perform cross-core communication.

[update]

A "spinlock" like this is only a good idea if you will not be spinning for very long. If it may be a while before the variable gets updated, use a mutex + condition variable instead, which will put your thread to sleep so that it adds no overhead while it waits.

(Incidentally, I suspect a lot of people -- including me -- are wondering "what are you actually trying to do?")

like image 65
Nemo Avatar answered Oct 12 '22 14:10

Nemo


If you spin lock for short intervals you are usually fine. However there is a timer interrupt on Linux (and I assume similar on other OSes) so if you spin lock for 10 ms or close to it you will see a cache disturbance.

I have heard its possible to modify the Linux kernel to prevent all interrupts on specific cores and this disturbance goes away, but I don't know what is involved in doing this.

like image 21
Peter Lawrey Avatar answered Oct 12 '22 14:10

Peter Lawrey