Search code examples
gopprof

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


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!


Solution

  • 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.