cmd/trace: add option to output pprof files

The trace tool can generate some interesting profiles, but it was only
exposing them as svg through the web UI.  This adds command line options
to generate the raw pprof file.

Change-Id: I52e4f909fdca6f65c3616add444e3892783640f4
Reviewed-on: https://go-review.googlesource.com/23324
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Filippo Valsorda 2016-05-24 12:50:38 +01:00 committed by Russ Cox
parent 7f6eadb64f
commit f64c670181
2 changed files with 97 additions and 60 deletions

View file

@ -15,6 +15,14 @@ Generate a trace file with 'go test':
go test -trace trace.out pkg
View the trace in a web browser:
go tool trace trace.out
Generate a pprof-like profile from the trace:
go tool trace -pprof=TYPE trace.out > TYPE.pprof
Supported profile types are:
- net: network blocking profile
- sync: synchronization blocking profile
- syscall: syscall blocking profile
- sched: scheduler latency profile
*/
package main
@ -25,6 +33,7 @@ import (
"fmt"
"html/template"
"internal/trace"
"io"
"log"
"net"
"net/http"
@ -39,15 +48,27 @@ Given a trace file produced by 'go test':
Open a web browser displaying trace:
go tool trace [flags] [pkg.test] trace.out
Generate a pprof-like profile from the trace:
go tool trace -pprof=TYPE [pkg.test] trace.out
[pkg.test] argument is required for traces produced by Go 1.6 and below.
Go 1.7 does not require the binary argument.
Supported profile types are:
- net: network blocking profile
- sync: synchronization blocking profile
- syscall: syscall blocking profile
- sched: scheduler latency profile
Flags:
-http=addr: HTTP service address (e.g., ':6060')
-pprof=type: print a pprof-like profile instead
`
var (
httpFlag = flag.String("http", "localhost:0", "HTTP service address (e.g., ':6060')")
httpFlag = flag.String("http", "localhost:0", "HTTP service address (e.g., ':6060')")
pprofFlag = flag.String("pprof", "", "print a pprof-like profile instead")
// The binary file name, left here for serveSVGProfile.
programBinary string
@ -73,6 +94,27 @@ func main() {
flag.Usage()
}
var pprofFunc func(io.Writer) error
switch *pprofFlag {
case "net":
pprofFunc = pprofIO
case "sync":
pprofFunc = pprofBlock
case "syscall":
pprofFunc = pprofSyscall
case "sched":
pprofFunc = pprofSched
}
if pprofFunc != nil {
if err := pprofFunc(os.Stdout); err != nil {
dief("failed to generate pprof: %v\n", err)
}
os.Exit(0)
}
if *pprofFlag != "" {
dief("unknown pprof type %s\n", *pprofFlag)
}
ln, err := net.Listen("tcp", *httpFlag)
if err != nil {
dief("failed to create server socket: %v\n", err)