How can I get samples when running go with pkg/profile enabled?

眉间皱痕 提交于 2019-12-11 17:12:45

问题


I have the following in my main block:

func main() {
    defer profile.Start().Stop()

    fmt.Println("running version", version, "built on", date)
    fmt.Println()
    cmd.Execute()

    time.Sleep(2 * time.Second)
}

where cmd is a cobra subcommand. I do a go build, and then I run the binary. I can see that it generates a pprof file:

2018/09/13 18:43:26 profile: cpu profiling enabled, /tmp/profile705487093/cpu.pprof
... output deleted ...
2018/09/13 18:43:31 profile: cpu profiling disabled, /tmp/profile705487093/cpu.pprof

Then I'm trying to analyze it, using:

go tool pprof /root/code/debug/evented /tmp/profile705487093/cpu.pprof

But when pprof opens, I see this:

File: evented
Type: cpu
Time: Sep 13, 2018 at 6:43pm (UTC)
Duration: 5.49s, Total samples = 0

In case it helps, I'm running go version go1.11 linux/amd64 on a Ubuntu 16.04.5 LTS. Not sure whether it matters, but I'm trying to inspect the pprof output on a DigitalOcean droplet.

Is there something that I'm doing wrong? Thanks!


回答1:


After looking a bit through the comments of the profile pkg, I managed to get some samples, by doing this:

runtime.SetCPUProfileRate(5000)

before calling the defer profile.Start().Stop() line.



来源:https://stackoverflow.com/questions/52320597/how-can-i-get-samples-when-running-go-with-pkg-profile-enabled

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!