2015-01-30 03:31:43 -07: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 12:16:50 -06:00
|
|
|
"cmd/internal/browser"
|
2015-01-30 03:31:43 -07:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2016-05-03 08:44:25 -06:00
|
|
|
"html/template"
|
2015-01-30 03:31:43 -07:00
|
|
|
"internal/trace"
|
2016-05-24 05:50:38 -06:00
|
|
|
"io"
|
2016-05-03 08:44:25 -06:00
|
|
|
"log"
|
2015-01-30 03:31:43 -07:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2018-02-06 11:21:39 -07:00
|
|
|
"runtime"
|
2018-02-06 12:56:30 -07:00
|
|
|
"runtime/debug"
|
2015-01-30 03:31:43 -07:00
|
|
|
"sync"
|
2018-02-06 11:21:39 -07:00
|
|
|
|
|
|
|
_ "net/http/pprof" // Required to use pprof
|
2015-01-30 03:31:43 -07: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 05:33:33 -06:00
|
|
|
go tool trace [flags] [pkg.test] trace.out
|
2016-05-24 05:50:38 -06:00
|
|
|
|
|
|
|
Generate a pprof-like profile from the trace:
|
|
|
|
go tool trace -pprof=TYPE [pkg.test] trace.out
|
|
|
|
|
2016-04-24 05:33:33 -06: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 03:31:43 -07:00
|
|
|
|
2016-05-24 05:50:38 -06:00
|
|
|
Supported profile types are:
|
|
|
|
- net: network blocking profile
|
|
|
|
- sync: synchronization blocking profile
|
|
|
|
- syscall: syscall blocking profile
|
|
|
|
- sched: scheduler latency profile
|
|
|
|
|
2015-01-30 03:31:43 -07:00
|
|
|
Flags:
|
|
|
|
-http=addr: HTTP service address (e.g., ':6060')
|
2016-05-24 05:50:38 -06:00
|
|
|
-pprof=type: print a pprof-like profile instead
|
2017-08-25 10:56:29 -06:00
|
|
|
-d: print debug info such as parsed events
|
2017-02-21 06:20:58 -07: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 03:31:43 -07:00
|
|
|
`
|
|
|
|
|
|
|
|
var (
|
2016-05-24 05:50:38 -06: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 10:56:29 -06:00
|
|
|
debugFlag = flag.Bool("d", false, "print debug information such as parsed events list")
|
2015-01-30 03:31:43 -07: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 05:33:33 -06: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 03:31:43 -07: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 12:24:02 -06:00
|
|
|
var pprofFunc func(io.Writer, *http.Request) error
|
2016-05-24 05:50:38 -06: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 12:24:02 -06:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofIO)
|
2016-05-24 05:50:38 -06: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 12:24:02 -06:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofBlock)
|
2016-05-24 05:50:38 -06: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 12:24:02 -06:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofSyscall)
|
2016-05-24 05:50:38 -06: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 12:24:02 -06:00
|
|
|
pprofFunc = pprofByGoroutine(computePprofSched)
|
2016-05-24 05:50:38 -06: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 12:24:02 -06:00
|
|
|
if err := pprofFunc(os.Stdout, &http.Request{}); err != nil {
|
2016-05-24 05:50:38 -06:00
|
|
|
dief("failed to generate pprof: %v\n", err)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
if *pprofFlag != "" {
|
|
|
|
dief("unknown pprof type %s\n", *pprofFlag)
|
|
|
|
}
|
|
|
|
|
2015-01-30 03:31:43 -07:00
|
|
|
ln, err := net.Listen("tcp", *httpFlag)
|
|
|
|
if err != nil {
|
|
|
|
dief("failed to create server socket: %v\n", err)
|
|
|
|
}
|
2016-05-03 08:44:25 -06:00
|
|
|
|
2017-08-25 10:56:29 -06:00
|
|
|
log.Print("Parsing trace...")
|
2017-12-12 16:20:06 -07:00
|
|
|
res, err := parseTrace()
|
2016-05-03 08:44:25 -06:00
|
|
|
if err != nil {
|
|
|
|
dief("%v\n", err)
|
|
|
|
}
|
|
|
|
|
2017-08-25 10:56:29 -06:00
|
|
|
if *debugFlag {
|
2017-12-12 16:20:06 -07:00
|
|
|
trace.Print(res.Events)
|
2017-08-25 10:56:29 -06:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2018-02-06 11:21:39 -07:00
|
|
|
reportMemoryUsage("after parsing trace")
|
2018-02-06 12:56:30 -07:00
|
|
|
debug.FreeOSMemory()
|
2017-08-25 10:56:29 -06:00
|
|
|
|
|
|
|
log.Print("Splitting trace...")
|
2018-02-06 12:34:32 -07:00
|
|
|
ranges = splitTrace(res)
|
2018-02-06 11:21:39 -07:00
|
|
|
reportMemoryUsage("after spliting trace")
|
2018-02-06 12:56:30 -07:00
|
|
|
debug.FreeOSMemory()
|
2016-05-03 08:44:25 -06:00
|
|
|
|
2017-08-25 10:56:29 -06:00
|
|
|
addr := "http://" + ln.Addr().String()
|
|
|
|
log.Printf("Opening browser. Trace viewer is listening on %s", addr)
|
|
|
|
browser.Open(addr)
|
2015-01-30 03:31:43 -07:00
|
|
|
|
|
|
|
// Start http server.
|
|
|
|
http.HandleFunc("/", httpMain)
|
|
|
|
err = http.Serve(ln, nil)
|
|
|
|
dief("failed to start http server: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2016-05-03 08:44:25 -06:00
|
|
|
var ranges []Range
|
|
|
|
|
2015-01-30 03:31:43 -07:00
|
|
|
var loader struct {
|
2017-12-12 16:20:06 -07:00
|
|
|
once sync.Once
|
|
|
|
res trace.ParseResult
|
|
|
|
err error
|
2015-01-30 03:31:43 -07:00
|
|
|
}
|
|
|
|
|
2017-12-12 16:20:06 -07:00
|
|
|
// parseEvents is a compatibility wrapper that returns only
|
|
|
|
// the Events part of trace.ParseResult returned by parseTrace.
|
2015-01-30 03:31:43 -07:00
|
|
|
func parseEvents() ([]*trace.Event, error) {
|
2017-12-12 16:20:06 -07:00
|
|
|
res, err := parseTrace()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res.Events, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTrace() (trace.ParseResult, error) {
|
2015-01-30 03:31:43 -07:00
|
|
|
loader.once.Do(func() {
|
2016-04-24 05:33:33 -06:00
|
|
|
tracef, err := os.Open(traceFile)
|
2015-01-30 03:31:43 -07: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 16:20:06 -07:00
|
|
|
res, err := trace.Parse(bufio.NewReader(tracef), programBinary)
|
2015-01-30 03:31:43 -07:00
|
|
|
if err != nil {
|
|
|
|
loader.err = fmt.Errorf("failed to parse trace: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-12-12 16:20:06 -07:00
|
|
|
loader.res = res
|
2015-01-30 03:31:43 -07:00
|
|
|
})
|
2017-12-12 16:20:06 -07:00
|
|
|
return loader.res, loader.err
|
2015-01-30 03:31:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// httpMain serves the starting page.
|
|
|
|
func httpMain(w http.ResponseWriter, r *http.Request) {
|
2016-05-03 08:44:25 -06:00
|
|
|
if err := templMain.Execute(w, ranges); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2015-01-30 03:31:43 -07:00
|
|
|
}
|
|
|
|
|
2016-05-03 08:44:25 -06:00
|
|
|
var templMain = template.Must(template.New("").Parse(`
|
2015-01-30 03:31:43 -07:00
|
|
|
<html>
|
|
|
|
<body>
|
2016-05-03 08:44:25 -06: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 03:31:43 -07:00
|
|
|
<a href="/goroutines">Goroutine analysis</a><br>
|
2017-11-02 17:17:39 -06: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 14:20:30 -07:00
|
|
|
<a href="/usertasks">User-defined tasks</a><br>
|
runtime/trace: rename "Span" with "Region"
"Span" is a commonly used term in many distributed tracing systems
(Dapper, OpenCensus, OpenTracing, ...). They use it to refer to a
period of time, not necessarily tied into execution of underlying
processor, thread, or goroutine, unlike the "Span" of runtime/trace
package.
Since distributed tracing and go runtime execution tracing are
already similar enough to cause confusion, this CL attempts to avoid
using the same word if possible.
"Region" is being used in a certain tracing system to refer to a code
region which is pretty close to what runtime/trace.Span currently
refers to. So, replace that.
https://software.intel.com/en-us/itc-user-and-reference-guide-defining-and-recording-functions-or-regions
This CL also tweaks APIs a bit based on jbd and heschi's comments:
NewContext -> NewTask
and it now returns a Task object that exports End method.
StartSpan -> StartRegion
and it now returns a Region object that exports End method.
Also, changed WithSpan to WithRegion and it now takes func() with no
context. Another thought is to get rid of WithRegion. It is a nice
concept but in practice, it seems problematic (a lot of code churn,
and polluting stack trace). Already, the tracing concept is very low
level, and we hope this API to be used with great care.
Recommended usage will be
defer trace.StartRegion(ctx, "someRegion").End()
Left old APIs untouched in this CL. Once the usage of them are cleaned
up, they will be removed in a separate CL.
Change-Id: I73880635e437f3aad51314331a035dd1459b9f3a
Reviewed-on: https://go-review.googlesource.com/108296
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: JBD <jbd@google.com>
2018-04-19 12:58:42 -06:00
|
|
|
<a href="/userregions">User-defined regions</a><br>
|
2015-01-30 03:31:43 -07:00
|
|
|
</body>
|
|
|
|
</html>
|
2016-05-03 08:44:25 -06:00
|
|
|
`))
|
2015-01-30 03:31:43 -07:00
|
|
|
|
|
|
|
func dief(msg string, args ...interface{}) {
|
|
|
|
fmt.Fprintf(os.Stderr, msg, args...)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-02-06 11:21:39 -07: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)
|
|
|
|
}
|