简体   繁体   English

如何在go(profiling)中获得函数持续时间细分

[英]How to get a function-duration breakdown in go (profiling)

Update (Jan 24, 2019): 更新(2019年1月24日):

This question was asked 4 years ago about Go 1.4 (and is still getting views). 这个问题在4年前被问到关于Go 1.4(并且仍在获得观点)。 Profiling with pprof has changed dramatically since then. 从那时起,pprof的分析发生了巨大的变化。

Original Question: 原始问题:

I'm trying to profile a go martini based server I wrote, I want to profile a single request, and get the complete breakdown of the function with their runtime duration. 我正在尝试描述我写的基于马提尼的服务器,我想描述一个请求,并获得函数的完整细分及其运行时间。 I tried playing around with both runtime/pprof and net/http/pprof but the output looks like this: 我尝试使用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

The web view is not very helpful either. Web视图也不是很有帮助。

We regularly profile another program, and the output seems to be what I need: 我们经常介绍另一个程序,输出似乎是我需要的:

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

I can't tell where the difference is coming from. 我无法分辨出差异的来源。

pprof is a timer based sampling profiler, originally from the gperftools suite. pprof是一个基于计时器的采样分析器,最初来自gperftools套件。 Rus Cox later ported the pprof tools to Go: http://research.swtch.com/pprof . Rus Cox后来将pprof工具移植到Go: http ://research.swtch.com/pprof。

This timer based profiler works by using the system profiling timer, and recording statistics whenever it receives SIGPROF . 这个基于计时器的分析器使用系统分析计时器,并在收到SIGPROF时记录统计信息。 In go, this is currently set to a constant 100Hz. 在go中,目前设置为恒定的100Hz。 From pprof.go: 来自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

You can set this frequency by calling runtime.SetCPUProfileRate and writing the profile output yourself, and Gperftools allows you to set this frequency with CPUPROFILE_FREQUENCY , but in practice it's not that useful. 您可以通过调用runtime.SetCPUProfileRate并自己编写配置文件输出来设置此频率, CPUPROFILE_FREQUENCY允许您使用CPUPROFILE_FREQUENCY设置此频率,但实际上它并没有那么有用。

In order to sample a program, it needs to be doing what you're trying to measure at all times. 为了对程序进行抽样,它需要始终执行您要测量的内容。 Sampling the idle runtime isn't showing anything useful. 对空闲运行时进行采样并未显示任何有用的内容。 What you usually do is run the code you want in a benchmark, or in a hot loop, using as much CPU time as possible. 您通常使用尽可能多的CPU时间在基准测试或热循环中运行所需的代码。 After accumulating enough samples, there should be a sufficient number across all functions to show you proportionally how much time is spent in each function. 在累积足够的样本后,所有函数中应该有足够的数字来按比例显示每个函数花费的时间。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM