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

Ray*_* Wu 5 go pprof

我正在优化 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
Run Code Online (Sandbox Code Playgroud)

代码片段是

     .          .    502:    w.mu.Lock()

     .          .    ... some calculation

     .   5.02mins    510:    w.mu.Unlock()
     .          .    511:
     .          .    512:    return nil
     .          .    513:}
Run Code Online (Sandbox Code Playgroud)

我不明白的是:

  • 为什么互斥量分析显示只有前 1 个有flat时间,其余的都是0
  • 如果显示正在等待,Lock则可能意味着计算时间过长,但是显示等待中是什么意思Unlock

pra*_*mic 4

为了回答你的第二个问题,互斥配置文件总是在路径上显示结果Unlock,而不是在Lock[1] 上。您的个人资料表明大多数争用发生在 上w.mu

本文档包含更多详细信息。

[1] 实现细节:这样做的原因是Unlock将锁交给下一个等待的锁,此时争用被记录在配置文件中。因此,堆栈跟踪似乎位于Unlock路径中。