How to get function breakdown from Go profile

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

How to get function breakdown from Go profile

问题

我一直在尝试在Linux上使用pprof来进行Go语言的性能分析,但是没有得到函数信息。我做错了什么?以下是我的构建/运行步骤:

$ rm -f silly
$ go build -gcflags "-N -l" silly.go
$ rm -f silly.prof
$ ./silly --cpuprofile silly.prof
fib(42)=267914296
t=1.758997214s
$ go tool pprof --text silly.prof
1.75s of 1.75s total (  100%)
      flat  flat%   sum%        cum   cum%
     1.75s   100%   100%      1.75s   100%

我期望从pprof的输出中获得更多详细信息。"t=1.75..."这一行表示程序运行花费了1.75秒的时间,这似乎足够的时间以便以分析器的100 Hz采样率收集样本。

以下是程序的代码:

package main

import (
    "flag"
    "fmt"
    "log"
    "os"
    "runtime/pprof"
    "time"
)

func b(n int) int {
    if n < 2 {
        return n
    } else {
        return a(n-1) + b(n-2)
    }
}

func a(n int) int {
    if n < 2 {
        return n
    } else {
        return a(n-1) + b(n-2)
    }
}

var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")

func main() {
    flag.Parse()
    if *cpuprofile != "" {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    }

    t0 := time.Now()
    fmt.Printf("fib(42)=%v\n", a(42))
    t1 := time.Now()
    fmt.Printf("t=%v\n", t1.Sub(t0))
}

我在Red Hat Enterprise Linux Server release 7.0上运行,使用的Go版本是go1.4 linux/amd64。

英文:

I've been trying to use pprof for Go on Linux, but get no function information. What am I doing wrong? Here are my build/run steps:

$ rm -f silly
$ go build -gcflags &quot;-N -l&quot; silly.go
$ rm -f silly.prof
$ ./silly --cpuprofile silly.prof
fib(42)=267914296
t=1.758997214s
$ go tool pprof --text silly.prof
1.75s of 1.75s total (  100%)
      flat  flat%   sum%        cum   cum%
     1.75s   100%   100%      1.75s   100%

I was expecting more detail in the output from pprof. The "t=1.75..." line indicates that the program took 1.75 sec to run, which seems ample time to collect samples at the profiler's 100 Hz sampling rate.

Here is the program:

package main

import (
    &quot;flag&quot;
    &quot;fmt&quot;
    &quot;log&quot;
    &quot;os&quot;
    &quot;runtime/pprof&quot;
    &quot;time&quot;
)

func b(n int) int {
    if n &lt; 2 {
        return n
    } else {
        return a(n-1) + b(n-2)
    }
}

func a(n int) int {
    if n &lt; 2 {
        return n
    } else {
        return a(n-1) + b(n-2)
    }
}

var cpuprofile = flag.String(&quot;cpuprofile&quot;, &quot;&quot;, &quot;write cpu profile to file&quot;)

func main() {
    flag.Parse()
    if *cpuprofile != &quot;&quot; {
        f, err := os.Create(*cpuprofile)
        if err != nil {
            log.Fatal(err)
        }
        pprof.StartCPUProfile(f)
        defer pprof.StopCPUProfile()
    }

    t0 := time.Now()
    fmt.Printf(&quot;fib(42)=%v\n&quot;, a(42))
    t1 := time.Now()
    fmt.Printf(&quot;t=%v\n&quot;, t1.Sub(t0))
}

I'm running on I'm on Red Hat Enterprise Linux Server release 7.0, using Go version go1.4 linux/amd64.

答案1

得分: 1

原来错误在于在调用pprof工具时省略了二进制文件的名称。正确的调用方式是:

$ go tool pprof --text silly silly.prof
1750ms of 1750ms total (  100%)
      flat  flat%   sum%        cum   cum%
    1060ms 60.57% 60.57%     1750ms   100%  main.a
     690ms 39.43%   100%     1750ms   100%  main.b
         0     0%   100%     1750ms   100%  main.main
         0     0%   100%     1750ms   100%  runtime.goexit
         0     0%   100%     1750ms   100%  runtime.main

我在问题中省略了silly,即要进行性能分析的二进制文件的名称。

英文:

It turns out the mistake was omitting the binary name when invoking the pprof tool. The correct invocation is:

$ go tool pprof --text silly silly.prof
1750ms of 1750ms total (  100%)
      flat  flat%   sum%        cum   cum%
    1060ms 60.57% 60.57%     1750ms   100%  main.a
     690ms 39.43%   100%     1750ms   100%  main.b
         0     0%   100%     1750ms   100%  main.main
         0     0%   100%     1750ms   100%  runtime.goexit
         0     0%   100%     1750ms   100%  runtime.main

The line in my question omitted silly, i.e. the binary file to be profiled.

答案2

得分: 0

你在1.7秒内无法获得足够的分析样本。我喜欢至少进行30秒或更长时间的分析。

一旦你有足够大的分析样本,尝试使用pprof进行交互式操作(不使用--text选项)。你可以以几种不同的方式查看统计信息,还可以输出其他各种可视化格式。

https://blog.golang.org/profiling-go-programs

英文:

You're not going to get enough profiling samples in 1.7s. I like to profile for at least 30s, or more.

Once you have a large enough profile, try using pprof interactively (without the --text option). You'll be able to view the stats a few different ways, as well as output the various other visualization formats.

https://blog.golang.org/profiling-go-programs

huangapple
  • 本文由 发表于 2015年1月23日 06:58:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/28100203.html
匿名

发表评论

匿名网友

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

确定