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
4

1 回答 1

4

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

本文档包含更多细节。

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

于 2021-03-10T17:21:22.323 回答