I am tuning a high performance multi-threaded application and I suspect that false sharing might be a cause for why performance is bad. How can I verify that this is the case?
I am running C++ on Ubuntu 12.04 using gcc 4.82.
I'll post something I just came across from C++ Concurrency in Action.
One way to test for false sharing is to add huge blocks of padding between data elements that can be concurrently accessed by different threads.
struct protected_data
{
std::mutex m;
char padding[65536];
my_data data_to_protect;
};
If this improves performance then you know that false sharing was a problem.
The answer to this is pretty much the same as any other performance question which goes along the lines of "How do I know if X is slowing my program down?" or "Will Y be faster" and that answer is you break out your profiler and you profile it.
For this particular example you would be interested in whether an unusual amount of time is spent on instructions which access memory. Also if you are using your CPU vendor's profiler (CODEXL for AMD or VTune Pro for Intel) then you can profile by cache misses and see which lines of code and instructions are flushing your cache lines.
You might want to read this article for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With