Generating execution trace diagram for a go program

1,892 views
Skip to first unread message

Shan Valleru

unread,
Aug 19, 2015, 4:32:16 PM8/19/15
to golang-nuts
In Go 1.5, it seems like you can generate a execution flow diagram (like this - https://talks.golang.org/2015/dynamic-tools/trace.png) for a go program using trace command.
Anybody know the detailed steps for generating such a flow diagram from a go program?

Egon

unread,
Aug 20, 2015, 6:40:49 AM8/20/15
to golang-nuts
On Wednesday, 19 August 2015 23:32:16 UTC+3, Shan Valleru wrote:
In Go 1.5, it seems like you can generate a execution flow diagram (like this - https://talks.golang.org/2015/dynamic-tools/trace.png) for a go program using trace command.
Anybody know the detailed steps for generating such a flow diagram from a go program?

Here's the code I used to create a 10s trace from a server. You should be able to modify it to your own needs.

import "runtime/trace"

func trace10(w http.ResponseWriter, r *http.Request) {
f, err := os.Create(time.Now().Format("2006-01-02T150405.pprof"))
if err != nil {
panic(err)
}
defer f.Close()

if err := trace.Start(f); err != nil {
panic(err)
}
defer trace.Stop()

// All the important stuff is happening in other goroutines so we just wait here
time.Sleep(10 * time.Second)
}

Once you have integrated that into your own code whatever way you need:

// on Linux you can create/analyze it as:
go build .
./program
go tool trace program 2015-08-20T133508.pprof

// on Windows
go build .
program.exe
go tool trace program.exe 2015-08-20T133508.pprof

ondrej...@gmail.com

unread,
Aug 4, 2016, 12:34:31 PM8/4/16
to golang-nuts
This is excellent, really helpful. The -help flag only mentions testing and casual searching only again pops up with info on more testing, I couldn't see anything on actually using it for running a generic binary. Perhaps it would be worth adding this to the documentation?

+ things like not needing the binary as an argument since 1.7 etc.

Or maybe just an example in the package file https://golang.org/pkg/runtime/trace/
Reply all
Reply to author
Forward
0 new messages