Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect false sharing slowing down performance?

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.

like image 254
Nathan Doromal Avatar asked Oct 16 '14 12:10

Nathan Doromal


2 Answers

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.

like image 58
Nathan Doromal Avatar answered Oct 01 '22 19:10

Nathan Doromal


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.

like image 32
sjdowling Avatar answered Oct 01 '22 17:10

sjdowling