4

更新(2019 年 1 月 24 日):

这个问题是 4 年前就 Go 1.4 提出的(并且仍在获得意见)。从那时起,使用 pprof 进行分析已经发生了巨大变化。

原始问题:

我正在尝试分析我编写的基于 go martini 的服务器,我想分析单个请求,并获得函数的完整分解及其运行时持续时间。我尝试使用两者runtime/pprofnet/http/pprof但输出如下所示:

Total: 3 samples
       1  33.3%  33.3%        1  33.3% ExternalCode
       1  33.3%  66.7%        1  33.3% runtime.futex
       1  33.3% 100.0%        2  66.7% syscall.Syscall

网络视图也不是很有帮助。

我们定期分析另一个程序,输出似乎是我需要的:

20ms of 20ms total (  100%)
      flat  flat%  sum%        cum  cum%
      10ms 50.00% 50.00%     10ms 50.00%  runtime.duffcopy
      10ms 50.00%   100%     10ms 50.00%  runtime.fastrand1
         0     0%   100%     20ms   100%  main.func·004
         0     0%   100%     20ms   100%  main.pruneAlerts
         0     0%   100%     20ms   100%  runtime.memclr

我不知道差异来自哪里。

4

1 回答 1

7

pprof是一个基于定时器的采样分析器,最初来自gperftools套件。Rus Cox 后来将 pprof 工具移植到 Go:http ://research.swtch.com/pprof 。

这个基于计时器的分析器通过使用系统分析计时器工作,并在收到SIGPROF. 在运行中,当前设置为恒定的 100Hz。来自 pprof.go:

// The runtime routines allow a variable profiling rate,
// but in practice operating systems cannot trigger signals
// at more than about 500 Hz, and our processing of the
// signal is not cheap (mostly getting the stack trace).
// 100 Hz is a reasonable choice: it is frequent enough to
// produce useful data, rare enough not to bog down the
// system, and a nice round number to make it easy to
// convert sample counts to seconds.  Instead of requiring
// each client to specify the frequency, we hard code it.
const hz = 100

您可以通过自己调用runtime.SetCPUProfileRate和编写配置文件输出来设置此频率,Gperftools 允许您使用 设置此频率CPUPROFILE_FREQUENCY,但实际上它并没有那么有用。

为了对程序进行采样,它需要始终执行您要测量的内容。对空闲运行时进行采样并没有显示任何有用的信息。您通常做的是在基准测试或热循环中运行您想要的代码,使用尽可能多的 CPU 时间。在积累了足够多的样本后,所有函数都应该有足够的数量,以按比例显示每个函数花费了多少时间。

也可以看看:

于 2014-12-09T15:12:26.823 回答