0

编辑:当我将可执行文件添加到 pprof 调用时工作

我正在尝试使用来自https://github.com/pkg/profile的分析器来分析一个简单的程序:并使用工具 pprof。

package main

import "github.com/pkg/profile"

func main() {
    defer profile.Start().Stop()
    t1()
    t2()
}

func t1() {
    for i := 0; i < 9000000000; i++ {
        x := i * 2
        x += x
    }
}

func t2() {
    for i := 0; i < 1000000000; i++ {
        x := i * 2
        x += x
    }
}

这些示例显示了一个很好的表格,其中包含已调用的所有函数以及每个函数花费了多长时间,但我只看到几秒钟的 100% 使用率,没有更多信息

我该怎么做才能使其输出功能?它与代码完成时输出的“cpu profiling disabled”行有什么关系吗?

这是我用来生成输出的:

./test 
2016/12/16 11:04:39 profile: cpu profiling enabled, /tmp/profile176930291/cpu.pprof
2016/12/16 11:04:44 profile: cpu profiling disabled, /tmp/profile176930291/cpu.pprof
martin@martin-laptop:~/work/bin$ go tool pprof -text /tmp/profile176930291/cpu.pprof 
4.90s of 4.90s total (  100%)                                                                                                                                                                                                                       
      flat  flat%   sum%        cum   cum%
     4.90s   100%   100%      4.90s   100%  
4

1 回答 1

0

您是否收到如下错误:

tmp 的本地符号化失败:打开 /tmp/go-build594370835/command-line-arguments/_obj/exe/tmp:没有这样的文件或目录

如果是这样; 在源目录中,尝试:

go build tmp.go

重新运行go tool pprof -text ...然后导致:

go tool pprof -text /tmp/profile668503934/cpu.pprof
5040ms of 5040ms total (  100%)
      flat  flat%   sum%        cum   cum%
    4560ms 90.48% 90.48%     4560ms 90.48%  main.t1
     480ms  9.52%   100%      480ms  9.52%  main.t2
         0     0%   100%     5040ms   100%  main.main
         0     0%   100%     5040ms   100%  runtime.goexit
         0     0%   100%     5040ms   100%  runtime.main

它现在使用./tmp二进制文件中包含的符号。

我在用着go version go1.7.3 linux/amd64

于 2016-12-16T11:43:18.960 回答