Search code examples
pthreadsboost-asioperf

Why is pthread_cond_wait a big portion of total perf samples in my program?


I use perf to do performance profiling, and get the following flame graph. Notice that a big portion of the total samples is pthread_cond_wait. I used boost::asio but not sure where is pthread_cond_wait is called. Can anyone give me some clue why this is happening. Thanks.

enter image description here


Solution

  • That means you have a lot of lock contention. Without code there's little useful we can say, except generalities:

    • keep locks short
    • use atomics where possible
    • minimize resource sharing, so there is less need for synchronization of any kind, locking or otherwise
    • e.g. moving resources with their tasks is a good pattern to remove sharing
    • you might have too many threads; above a point you risk performance degradation due to increased lock contention

    Going from the flame-graph alone I will add the tip that it is possible to optimize Asio in the case of single threaded application. See Concurrency Hint.