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 "-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%
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 (
"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
", a(42))
t1 := time.Now()
fmt.Printf("t=%v
", 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.
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.
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.