如何解释go pprof/mutex显示等待Unlock?

6

我正在优化Go程序的性能。在查看互斥锁分析时,我遇到了以下内容:

> go tool pprof -seconds 30 -ignore .*Shopify.*  http://HOST/debug/pprof/mutex
(pprof) top 20
Active filters:
ignore=.*Shopify.*
Showing nodes accounting for 3.08mins, 91.03% of 3.39mins total
Dropped 82 nodes (cum <= 0.02mins)
    flat  flat%   sum%        cum   cum%
3.08mins 91.03% 91.03%   3.08mins 91.03%  sync.(*Mutex).Unlock
       0     0% 91.03%   0.06mins  1.75%  ....func2
       0     0% 91.03%   0.06mins  1.75%  ....func3

代码片段是:

     .          .    502:    w.mu.Lock()

     .          .    ... some calculation

     .   5.02mins    510:    w.mu.Unlock()
     .          .    511:
     .          .    512:    return nil
     .          .    513:}

我不明白的是:

  • 为什么互斥锁分析只显示顶部 1 的flat时间,其余全部为0
  • 如果它显示等待Lock,那么很可能意味着计算时间过长,但如果它显示等待Unlock是什么意思?
1个回答

4
回答你的第二个问题,互斥锁(mutex)的分析结果总是在Unlock路径上显示,而不会显示在Lock路径上[1]。根据你的分析结果,争用最严重的地方是在w.mu上。 这个文档包含更多细节。
[1] 实现细节:原因是Unlock会将锁交给下一个等待的锁持有者,而正是在这一点上才记录争用情况,因此堆栈跟踪看起来在Unlock路径上。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接