Go分析器(pprof)的时间差异

huangapple go评论69阅读模式
英文:

Go profiler (pprof) timing discrepancy

问题

当我使用Linux的time实用程序测量我的Go程序的运行时间时,我得到以下输出:

real    1m9.177s
user    7m1.788s
sys     0m39.016s

当我查看相同程序在Go的pprof CPU分析器中的输出时,我得到以下输出:

进入交互模式(输入“help”获取命令)
(pprof) top
143.32s of 176s total (81.43%)

pprof从哪里获取这个176s的数值?它既不是时钟时间也不是CPU时间。我正在使用GOMAXPROCS设置为8来运行此程序,我有一种感觉这与此有关。pprof如何准确测量运行时间,为什么它与Linux的time不同?

英文:

When I measure the run time of my Go program with Linux's time utility, I get the following output:

real    1m9.177s
user    7m1.788s
sys     0m39.016s

When I look at the output of the same exact program execution in Go's pprof CPU profiler, I get the following output:

Entering interactive mode (type "help" for commands)
(pprof) top
143.32s of 176s total (81.43%)

Where is the pprof getting this 176s figure from? It is neither the clock time nor the CPU time. I am running this program with GOMAXPROCS set to 8, and I have a feeling this has something to do with it. How exactly does pprof measure run time, and why is it different from linux's time?

答案1

得分: 4

time实用程序和pprof工具以不同的方式运作,并且应该以不同的方式使用。不要试图将它们调和在一起。time测量程序的准确时间;pprof测量函数和方法的相对时间。pprof是一种侵入性的统计采样。当启用CPU分析时,Go程序每秒停止约100次,并记录一个样本,其中包含当前执行的goroutine堆栈上的程序计数器。采样是有成本的,并且结果是对实际总体的估计。

使用time来测量程序的整体实际CPU时间,即绝对时间。使用pprof来估计函数和方法中花费大部分时间的地方,使用相对时间百分比。pprof可以识别瓶颈。对于实际的CPU时间,请使用Go的testing包基准测试。

有关示例,请参见Profiling Go Programs

通过使用Go的分析工具来识别和纠正特定的瓶颈,我们可以使Go循环查找程序运行速度提高一个数量级,并且使用的内存减少6倍。

英文:

The time utility and the pprof tool operate in different ways and should be used in different ways. Don't try to reconcile them. time measures near exact times for the program; pprof measures relative times for functions and methods. pprof is an intrusive statistical sample. When CPU profiling is enabled, the Go program stops about 100 times per second and records a sample consisting of the program counters on the currently executing goroutine's stack. Sampling has a cost and the results are estimates of the actual population.

Use time to measure the overall actual CPU time for the program, that is absolute time. Use pprof to estimate where most of the time was spent in the functions and methods using relative time percentages. pprof identifies the bottlenecks. For actual CPU times, use Go testing package benchmarks.

For an example, see Profiling Go Programs:

> By using Go's profiling tools to identify and correct specific
> bottlenecks, we can make the Go loop finding program run an order of
> magnitude faster and use 6x less memory.

答案2

得分: -1

这看起来像是一个错误。可能是一些已知的错误。为了输出176s,pprof会将已知的采样周期乘以样本数量。这应该相对准确地反映出进程所花费的总CPU时间(系统 + 用户)。

你的程序有没有可能通过子进程生成一些工作?这可能是解释差异的一种方式。

英文:

This looks like some bug. Possibly some known bug. What pprof does in order to output 176s is multiplying known sampling period by number of samples. And it should be reasonably accurate w.r.t. total cpu time spent by the process (system + user).

Is there any chance that your program spawns some work via subprocesses? That could be one way to explain difference.

huangapple
  • 本文由 发表于 2017年2月23日 11:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/42406702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定