Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to profile lock contentions under g++/std::mutex?

Question

Are there any open-source tools or does anyone have any techniques/code for profiling the degree of std::mutex contentions in running code?

I would like to count the percentage of lock contention at the granularity (either by time or number) of each std::mutex instance. If there is a drop-in tool that doesn't require recoding, that would be even better.

I am looking for a technique that will work with std::thread and g++ : at the exit of the application, I would like to dump out a profile of mutex contention statistics into a log file, so that I can monitor the quality of threading code under actual running contexts.

Note

I have seen this thread. Unfortunately, the answers either require a pile of cash or run on Windows.

like image 669
kfmfe04 Avatar asked Jun 27 '13 03:06

kfmfe04


1 Answers

I recommend something like AMD CodeXL or Intel VTune. CodeXL is free; Intel VTune has free academic license if that's applicable to you, or you can try a 30-day trial. Both of them work in Linux.

At the most basic level, these tools can identify hotspots by eg, measuring how much time you are spending inside methods of std::mutex. There are other more advanced analysis techniques/tools included in each tool that may help you even further. You don't need to change your code at all, although you may need to check that you compiled with debug symbols and/or haven't stripped the binaries. You will also probably want to stay away from extreme optimization levels like -O3, and stick to -O1, -O2 or -Og.

PS: As will all optimization inquiries, I must remind you to always measure where your performance problems actually are before you start optimizing. No matter how worried you are about lock contention, validate your concerns with a profiler before you spend huge efforts trying to alleviate whatever lock contention you may or may not be having.

like image 151
Nicu Stiurca Avatar answered Oct 24 '22 05:10

Nicu Stiurca