2015-01-30 13:31:43 +03:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
2016-09-14 11:16:50 -07:00
|
|
|
"cmd/internal/browser"
|
2015-01-30 13:31:43 +03:00
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
2016-05-03 16:44:25 +02:00
|
|
|
"html/template"
|
2015-01-30 13:31:43 +03:00
|
|
|
"internal/trace"
|
2016-05-24 12:50:38 +01:00
|
|
|
"io"
|
2016-05-03 16:44:25 +02:00
|
|
|
"log"
|
2015-01-30 13:31:43 +03:00
|
|
|
"net"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
2018-02-06 13:21:39 -05:00
|
|
|
"runtime"
|
2018-02-06 14:56:30 -05:00
|
|
|
"runtime/debug"
|
2015-01-30 13:31:43 +03:00
|
|
|
"sync"
|
2018-02-06 13:21:39 -05:00
|
|
|
|
|
|
|
|
_ "net/http/pprof" // Required to use pprof
|
2015-01-30 13:31:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const usageMessage = "" +
|
|
|
|
|
`Usage of 'go tool trace':
|
|
|
|
|
Given a trace file produced by 'go test':
|
|
|
|
|
go test -trace=trace.out pkg
|
|
|
|
|
|
|
|
|
|
Open a web browser displaying trace:
|
2016-04-24 13:33:33 +02:00
|
|
|
go tool trace [flags] [pkg.test] trace.out
|
2016-05-24 12:50:38 +01:00
|
|
|
|
|
|
|
|
Generate a pprof-like profile from the trace:
|
|
|
|
|
go tool trace -pprof=TYPE [pkg.test] trace.out
|
|
|
|
|
|
2016-04-24 13:33:33 +02:00
|
|
|
[pkg.test] argument is required for traces produced by Go 1.6 and below.
|
|
|
|
|
Go 1.7 does not require the binary argument.
|
2015-01-30 13:31:43 +03:00
|
|
|
|
2016-05-24 12:50:38 +01:00
|
|
|
Supported profile types are:
|
|
|
|
|
- net: network blocking profile
|
|
|
|
|
- sync: synchronization blocking profile
|
|
|
|
|
- syscall: syscall blocking profile
|
|
|
|
|
- sched: scheduler latency profile
|
|
|
|
|
|
2015-01-30 13:31:43 +03:00
|
|
|
Flags:
|
|
|
|
|
-http=addr: HTTP service address (e.g., ':6060')
|
2016-05-24 12:50:38 +01:00
|
|
|
-pprof=type: print a pprof-like profile instead
|
2017-08-25 12:56:29 -04:00
|
|
|
-d: print debug info such as parsed events
|
2017-02-21 14:20:58 +01:00
|
|
|
|
|
|
|
|
Note that while the various profiles available when launching
|
|
|
|
|
'go tool trace' work on every browser, the trace viewer itself
|
|
|
|
|
(the 'view trace' page) comes from the Chrome/Chromium project
|
|
|
|
|
and is only actively tested on that browser.
|
2015-01-30 13:31:43 +03:00
|
|
|
`
|
|
|
|
|
|
|
|
|
|
var (
|
2016-05-24 12:50:38 +01:00
|
|
|
httpFlag = flag.String("http", "localhost:0", "HTTP service address (e.g., ':6060')")
|
|
|
|
|
pprofFlag = flag.String("pprof", "", "print a pprof-like profile instead")
|
2017-08-25 12:56:29 -04:00
|
|
|
debugFlag = flag.Bool("d", false, "print debug information such as parsed events list")
|
2015-01-30 13:31:43 +03:00
|
|
|
|
|
|
|
|
// The binary file name, left here for serveSVGProfile.
|
|
|
|
|
programBinary string
|
|
|
|
|
traceFile string
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
flag.Usage = func() {
|
|
|
|
|
fmt.Fprintln(os.Stderr, usageMessage)
|
|
|
|
|
os.Exit(2)
|
|
|
|
|
}
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
2016-04-24 13:33:33 +02:00
|
|
|
// Go 1.7 traces embed symbol info and does not require the binary.
|
|
|
|
|
// But we optionally accept binary as first arg for Go 1.5 traces.
|
|
|
|
|
switch flag.NArg() {
|
|
|
|
|
case 1:
|
|
|
|
|
traceFile = flag.Arg(0)
|
|
|
|
|
case 2:
|
|
|
|
|
programBinary = flag.Arg(0)
|
|
|
|
|
traceFile = flag.Arg(1)
|
|
|
|
|
default:
|
2015-01-30 13:31:43 +03:00
|
|
|
flag.Usage()
|
|
|
|
|
}
|
|
|
|
|
|
cmd/trace: pprof computation for span types
/spanio, /spanblock, /spansched, /spansyscall provide
the pprof-style summary of span execution's
io, block, scheduling, syscall latency distributions
respectively.
The computation logic for /io, /block, /sched, /syscall
analysis was refactored and extended for reuse in these
new types of analysis. Upon the analysis query, we create
a map of goroutine id to time intervals based on the query
parameter, that represents the interesting time intervals
of matching goroutines. Only the events from the matching
goroutines that fall into the intervals are considered
in the pprof computation.
The new endpoints are not yet hooked into other span
analysis page (e.g. /userspan) yet.
Change-Id: I80c3396e45a2d6631758710de67d132e5832c7ce
Reviewed-on: https://go-review.googlesource.com/105822
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-04 14:24:02 -04:00
|
|
|
var pprofFunc func(io.Writer, *http.Request) error
|
2016-05-24 12:50:38 +01:00
|
|
|
switch *pprofFlag {
|
|
|
|
|
case "net":
|
cmd/trace: pprof computation for span types
/spanio, /spanblock, /spansched, /spansyscall provide
the pprof-style summary of span execution's
io, block, scheduling, syscall latency distributions
respectively.
The computation logic for /io, /block, /sched, /syscall
analysis was refactored and extended for reuse in these
new types of analysis. Upon the analysis query, we create
a map of goroutine id to time intervals based on the query
parameter, that represents the interesting time intervals
of matching goroutines. Only the events from the matching
goroutines that fall into the intervals are considered
in the pprof computation.
The new endpoints are not yet hooked into other span
analysis page (e.g. /userspan) yet.
Change-Id: I80c3396e45a2d6631758710de67d132e5832c7ce
Reviewed-on: https://go-review.googlesource.com/105822
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-04 14:24:02 -04:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofIO)
|
2016-05-24 12:50:38 +01:00
|
|
|
case "sync":
|
cmd/trace: pprof computation for span types
/spanio, /spanblock, /spansched, /spansyscall provide
the pprof-style summary of span execution's
io, block, scheduling, syscall latency distributions
respectively.
The computation logic for /io, /block, /sched, /syscall
analysis was refactored and extended for reuse in these
new types of analysis. Upon the analysis query, we create
a map of goroutine id to time intervals based on the query
parameter, that represents the interesting time intervals
of matching goroutines. Only the events from the matching
goroutines that fall into the intervals are considered
in the pprof computation.
The new endpoints are not yet hooked into other span
analysis page (e.g. /userspan) yet.
Change-Id: I80c3396e45a2d6631758710de67d132e5832c7ce
Reviewed-on: https://go-review.googlesource.com/105822
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-04 14:24:02 -04:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofBlock)
|
2016-05-24 12:50:38 +01:00
|
|
|
case "syscall":
|
cmd/trace: pprof computation for span types
/spanio, /spanblock, /spansched, /spansyscall provide
the pprof-style summary of span execution's
io, block, scheduling, syscall latency distributions
respectively.
The computation logic for /io, /block, /sched, /syscall
analysis was refactored and extended for reuse in these
new types of analysis. Upon the analysis query, we create
a map of goroutine id to time intervals based on the query
parameter, that represents the interesting time intervals
of matching goroutines. Only the events from the matching
goroutines that fall into the intervals are considered
in the pprof computation.
The new endpoints are not yet hooked into other span
analysis page (e.g. /userspan) yet.
Change-Id: I80c3396e45a2d6631758710de67d132e5832c7ce
Reviewed-on: https://go-review.googlesource.com/105822
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-04 14:24:02 -04:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofSyscall)
|
2016-05-24 12:50:38 +01:00
|
|
|
case "sched":
|
cmd/trace: pprof computation for span types
/spanio, /spanblock, /spansched, /spansyscall provide
the pprof-style summary of span execution's
io, block, scheduling, syscall latency distributions
respectively.
The computation logic for /io, /block, /sched, /syscall
analysis was refactored and extended for reuse in these
new types of analysis. Upon the analysis query, we create
a map of goroutine id to time intervals based on the query
parameter, that represents the interesting time intervals
of matching goroutines. Only the events from the matching
goroutines that fall into the intervals are considered
in the pprof computation.
The new endpoints are not yet hooked into other span
analysis page (e.g. /userspan) yet.
Change-Id: I80c3396e45a2d6631758710de67d132e5832c7ce
Reviewed-on: https://go-review.googlesource.com/105822
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-04 14:24:02 -04:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofSched)
|
2016-05-24 12:50:38 +01:00
|
|
|
}
|
|
|
|
|
if pprofFunc != nil {
|
cmd/trace: pprof computation for span types
/spanio, /spanblock, /spansched, /spansyscall provide
the pprof-style summary of span execution's
io, block, scheduling, syscall latency distributions
respectively.
The computation logic for /io, /block, /sched, /syscall
analysis was refactored and extended for reuse in these
new types of analysis. Upon the analysis query, we create
a map of goroutine id to time intervals based on the query
parameter, that represents the interesting time intervals
of matching goroutines. Only the events from the matching
goroutines that fall into the intervals are considered
in the pprof computation.
The new endpoints are not yet hooked into other span
analysis page (e.g. /userspan) yet.
Change-Id: I80c3396e45a2d6631758710de67d132e5832c7ce
Reviewed-on: https://go-review.googlesource.com/105822
Reviewed-by: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-04-04 14:24:02 -04:00
|
|
|
if err := pprofFunc(os.Stdout, &http.Request{}); err != nil {
|
2016-05-24 12:50:38 +01:00
|
|
|
dief("failed to generate pprof: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
|
|
|
|
if *pprofFlag != "" {
|
|
|
|
|
dief("unknown pprof type %s\n", *pprofFlag)
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 13:31:43 +03:00
|
|
|
ln, err := net.Listen("tcp", *httpFlag)
|
|
|
|
|
if err != nil {
|
|
|
|
|
dief("failed to create server socket: %v\n", err)
|
|
|
|
|
}
|
2016-05-03 16:44:25 +02:00
|
|
|
|
2017-08-25 12:56:29 -04:00
|
|
|
log.Print("Parsing trace...")
|
2017-12-12 18:20:06 -05:00
|
|
|
res, err := parseTrace()
|
2016-05-03 16:44:25 +02:00
|
|
|
if err != nil {
|
|
|
|
|
dief("%v\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-25 12:56:29 -04:00
|
|
|
if *debugFlag {
|
2017-12-12 18:20:06 -05:00
|
|
|
trace.Print(res.Events)
|
2017-08-25 12:56:29 -04:00
|
|
|
os.Exit(0)
|
|
|
|
|
}
|
2018-02-06 13:21:39 -05:00
|
|
|
reportMemoryUsage("after parsing trace")
|
2018-02-06 14:56:30 -05:00
|
|
|
debug.FreeOSMemory()
|
2017-08-25 12:56:29 -04:00
|
|
|
|
|
|
|
|
log.Print("Splitting trace...")
|
2018-02-06 14:34:32 -05:00
|
|
|
ranges = splitTrace(res)
|
2018-02-06 13:21:39 -05:00
|
|
|
reportMemoryUsage("after spliting trace")
|
2018-02-06 14:56:30 -05:00
|
|
|
debug.FreeOSMemory()
|
2016-05-03 16:44:25 +02:00
|
|
|
|
2017-08-25 12:56:29 -04:00
|
|
|
addr := "http://" + ln.Addr().String()
|
|
|
|
|
log.Printf("Opening browser. Trace viewer is listening on %s", addr)
|
|
|
|
|
browser.Open(addr)
|
2015-01-30 13:31:43 +03:00
|
|
|
|
|
|
|
|
// Start http server.
|
|
|
|
|
http.HandleFunc("/", httpMain)
|
|
|
|
|
err = http.Serve(ln, nil)
|
|
|
|
|
dief("failed to start http server: %v\n", err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 16:44:25 +02:00
|
|
|
var ranges []Range
|
|
|
|
|
|
2015-01-30 13:31:43 +03:00
|
|
|
var loader struct {
|
2017-12-12 18:20:06 -05:00
|
|
|
once sync.Once
|
|
|
|
|
res trace.ParseResult
|
|
|
|
|
err error
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
2017-12-12 18:20:06 -05:00
|
|
|
// parseEvents is a compatibility wrapper that returns only
|
|
|
|
|
// the Events part of trace.ParseResult returned by parseTrace.
|
2015-01-30 13:31:43 +03:00
|
|
|
func parseEvents() ([]*trace.Event, error) {
|
2017-12-12 18:20:06 -05:00
|
|
|
res, err := parseTrace()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return res.Events, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseTrace() (trace.ParseResult, error) {
|
2015-01-30 13:31:43 +03:00
|
|
|
loader.once.Do(func() {
|
2016-04-24 13:33:33 +02:00
|
|
|
tracef, err := os.Open(traceFile)
|
2015-01-30 13:31:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
loader.err = fmt.Errorf("failed to open trace file: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer tracef.Close()
|
|
|
|
|
|
|
|
|
|
// Parse and symbolize.
|
2017-12-12 18:20:06 -05:00
|
|
|
res, err := trace.Parse(bufio.NewReader(tracef), programBinary)
|
2015-01-30 13:31:43 +03:00
|
|
|
if err != nil {
|
|
|
|
|
loader.err = fmt.Errorf("failed to parse trace: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2017-12-12 18:20:06 -05:00
|
|
|
loader.res = res
|
2015-01-30 13:31:43 +03:00
|
|
|
})
|
2017-12-12 18:20:06 -05:00
|
|
|
return loader.res, loader.err
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// httpMain serves the starting page.
|
|
|
|
|
func httpMain(w http.ResponseWriter, r *http.Request) {
|
2016-05-03 16:44:25 +02:00
|
|
|
if err := templMain.Execute(w, ranges); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-03 16:44:25 +02:00
|
|
|
var templMain = template.Must(template.New("").Parse(`
|
2015-01-30 13:31:43 +03:00
|
|
|
<html>
|
|
|
|
|
<body>
|
2016-05-03 16:44:25 +02:00
|
|
|
{{if $}}
|
|
|
|
|
{{range $e := $}}
|
|
|
|
|
<a href="/trace?start={{$e.Start}}&end={{$e.End}}">View trace ({{$e.Name}})</a><br>
|
|
|
|
|
{{end}}
|
|
|
|
|
<br>
|
|
|
|
|
{{else}}
|
|
|
|
|
<a href="/trace">View trace</a><br>
|
|
|
|
|
{{end}}
|
2015-01-30 13:31:43 +03:00
|
|
|
<a href="/goroutines">Goroutine analysis</a><br>
|
2017-11-02 19:17:39 -04:00
|
|
|
<a href="/io">Network blocking profile</a> (<a href="/io?raw=1" download="io.profile">⬇</a>)<br>
|
|
|
|
|
<a href="/block">Synchronization blocking profile</a> (<a href="/block?raw=1" download="block.profile">⬇</a>)<br>
|
|
|
|
|
<a href="/syscall">Syscall blocking profile</a> (<a href="/syscall?raw=1" download="syscall.profile">⬇</a>)<br>
|
|
|
|
|
<a href="/sched">Scheduler latency profile</a> (<a href="/sche?raw=1" download="sched.profile">⬇</a>)<br>
|
2018-01-22 16:20:30 -05:00
|
|
|
<a href="/usertasks">User-defined tasks</a><br>
|
2018-03-15 11:28:07 -04:00
|
|
|
<a href="/userspans">User-defined spans</a><br>
|
2015-01-30 13:31:43 +03:00
|
|
|
</body>
|
|
|
|
|
</html>
|
2016-05-03 16:44:25 +02:00
|
|
|
`))
|
2015-01-30 13:31:43 +03:00
|
|
|
|
|
|
|
|
func dief(msg string, args ...interface{}) {
|
|
|
|
|
fmt.Fprintf(os.Stderr, msg, args...)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2018-02-06 13:21:39 -05:00
|
|
|
|
|
|
|
|
var debugMemoryUsage bool
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
v := os.Getenv("DEBUG_MEMORY_USAGE")
|
|
|
|
|
debugMemoryUsage = v != ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func reportMemoryUsage(msg string) {
|
|
|
|
|
if !debugMemoryUsage {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var s runtime.MemStats
|
|
|
|
|
runtime.ReadMemStats(&s)
|
|
|
|
|
w := os.Stderr
|
|
|
|
|
fmt.Fprintf(w, "%s\n", msg)
|
|
|
|
|
fmt.Fprintf(w, " Alloc:\t%d Bytes\n", s.Alloc)
|
|
|
|
|
fmt.Fprintf(w, " Sys:\t%d Bytes\n", s.Sys)
|
|
|
|
|
fmt.Fprintf(w, " HeapReleased:\t%d Bytes\n", s.HeapReleased)
|
|
|
|
|
fmt.Fprintf(w, " HeapSys:\t%d Bytes\n", s.HeapSys)
|
|
|
|
|
fmt.Fprintf(w, " HeapInUse:\t%d Bytes\n", s.HeapInuse)
|
|
|
|
|
fmt.Fprintf(w, " HeapAlloc:\t%d Bytes\n", s.HeapAlloc)
|
|
|
|
|
var dummy string
|
|
|
|
|
fmt.Printf("Enter to continue...")
|
|
|
|
|
fmt.Scanf("%s", &dummy)
|
|
|
|
|
}
|