2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
2015-01-30 13:31:43 +03:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Serving of pprof-like profiles.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"fmt"
|
|
|
|
|
"internal/trace"
|
2016-05-24 12:50:38 +01:00
|
|
|
"io"
|
2015-01-30 13:31:43 +03:00
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
2017-09-19 18:18:09 +09:00
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
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
|
|
|
"sort"
|
2017-11-02 19:17:39 -04:00
|
|
|
"strconv"
|
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
|
|
|
"time"
|
2017-02-17 00:17:26 -05:00
|
|
|
|
|
|
|
|
"github.com/google/pprof/profile"
|
2015-01-30 13:31:43 +03:00
|
|
|
)
|
|
|
|
|
|
2017-09-19 18:18:09 +09:00
|
|
|
func goCmd() string {
|
|
|
|
|
var exeSuffix string
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
exeSuffix = ".exe"
|
|
|
|
|
}
|
|
|
|
|
path := filepath.Join(runtime.GOROOT(), "bin", "go"+exeSuffix)
|
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
|
return path
|
|
|
|
|
}
|
|
|
|
|
return "go"
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-30 13:31:43 +03:00
|
|
|
func init() {
|
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
|
|
|
http.HandleFunc("/io", serveSVGProfile(pprofByGoroutine(computePprofIO)))
|
|
|
|
|
http.HandleFunc("/block", serveSVGProfile(pprofByGoroutine(computePprofBlock)))
|
|
|
|
|
http.HandleFunc("/syscall", serveSVGProfile(pprofByGoroutine(computePprofSyscall)))
|
|
|
|
|
http.HandleFunc("/sched", serveSVGProfile(pprofByGoroutine(computePprofSched)))
|
|
|
|
|
|
|
|
|
|
http.HandleFunc("/spanio", serveSVGProfile(pprofBySpan(computePprofIO)))
|
|
|
|
|
http.HandleFunc("/spanblock", serveSVGProfile(pprofBySpan(computePprofBlock)))
|
|
|
|
|
http.HandleFunc("/spansyscall", serveSVGProfile(pprofBySpan(computePprofSyscall)))
|
|
|
|
|
http.HandleFunc("/spansched", serveSVGProfile(pprofBySpan(computePprofSched)))
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Record represents one entry in pprof-like profiles.
|
|
|
|
|
type Record struct {
|
|
|
|
|
stk []*trace.Frame
|
|
|
|
|
n uint64
|
|
|
|
|
time int64
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// interval represents a time interval in the trace.
|
|
|
|
|
type interval struct {
|
|
|
|
|
begin, end int64 // nanoseconds.
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pprofByGoroutine(compute func(io.Writer, map[uint64][]interval, []*trace.Event) error) func(w io.Writer, r *http.Request) error {
|
|
|
|
|
return func(w io.Writer, r *http.Request) error {
|
|
|
|
|
id := r.FormValue("id")
|
|
|
|
|
events, err := parseEvents()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
gToIntervals, err := pprofMatchingGoroutines(id, events)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return compute(w, gToIntervals, events)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func pprofBySpan(compute func(io.Writer, map[uint64][]interval, []*trace.Event) error) func(w io.Writer, r *http.Request) error {
|
|
|
|
|
return func(w io.Writer, r *http.Request) error {
|
|
|
|
|
filter, err := newSpanFilter(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
gToIntervals, err := pprofMatchingSpans(filter)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
events, _ := parseEvents()
|
|
|
|
|
|
|
|
|
|
return compute(w, gToIntervals, events)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 19:17:39 -04:00
|
|
|
// pprofMatchingGoroutines parses the goroutine type id string (i.e. pc)
|
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
|
|
|
// and returns the ids of goroutines of the matching type and its interval.
|
2017-11-02 19:17:39 -04:00
|
|
|
// If the id string is empty, returns nil without an error.
|
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
|
|
|
func pprofMatchingGoroutines(id string, events []*trace.Event) (map[uint64][]interval, error) {
|
2017-11-02 19:17:39 -04:00
|
|
|
if id == "" {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
pc, err := strconv.ParseUint(id, 10, 64) // id is string
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("invalid goroutine type: %v", id)
|
|
|
|
|
}
|
|
|
|
|
analyzeGoroutines(events)
|
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 res map[uint64][]interval
|
2017-11-02 19:17:39 -04:00
|
|
|
for _, g := range gs {
|
|
|
|
|
if g.PC != pc {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if res == 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
|
|
|
res = make(map[uint64][]interval)
|
|
|
|
|
}
|
|
|
|
|
endTime := g.EndTime
|
|
|
|
|
if g.EndTime == 0 {
|
|
|
|
|
endTime = lastTimestamp() // the trace doesn't include the goroutine end event. Use the trace end time.
|
2017-11-02 19:17:39 -04:00
|
|
|
}
|
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
|
|
|
res[g.ID] = []interval{{begin: g.StartTime, end: endTime}}
|
2017-11-02 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
if len(res) == 0 && id != "" {
|
|
|
|
|
return nil, fmt.Errorf("failed to find matching goroutines for id: %s", id)
|
|
|
|
|
}
|
|
|
|
|
return res, 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
|
|
|
// pprofMatchingSpans returns the time intervals of matching spans
|
|
|
|
|
// grouped by the goroutine id. If the filter is nil, returns nil without an error.
|
|
|
|
|
func pprofMatchingSpans(filter *spanFilter) (map[uint64][]interval, error) {
|
|
|
|
|
res, err := analyzeAnnotations()
|
2015-01-30 13:31:43 +03:00
|
|
|
if err != 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
|
|
|
return nil, err
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
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 filter == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gToIntervals := make(map[uint64][]interval)
|
|
|
|
|
for id, spans := range res.spans {
|
|
|
|
|
for _, s := range spans {
|
|
|
|
|
if filter.match(id, s) {
|
|
|
|
|
gToIntervals[s.G] = append(gToIntervals[s.G], interval{begin: s.firstTimestamp(), end: s.lastTimestamp()})
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
for g, intervals := range gToIntervals {
|
|
|
|
|
// in order to remove nested spans and
|
|
|
|
|
// consider only the outermost spans,
|
|
|
|
|
// first, we sort based on the start time
|
|
|
|
|
// and then scan through to select only the outermost spans.
|
|
|
|
|
sort.Slice(intervals, func(i, j int) bool {
|
|
|
|
|
x := intervals[i].begin
|
|
|
|
|
y := intervals[j].begin
|
|
|
|
|
if x == y {
|
|
|
|
|
return intervals[i].end < intervals[j].end
|
|
|
|
|
}
|
|
|
|
|
return x < y
|
|
|
|
|
})
|
|
|
|
|
var lastTimestamp int64
|
|
|
|
|
var n int
|
|
|
|
|
// select only the outermost spans.
|
|
|
|
|
for _, i := range intervals {
|
|
|
|
|
if lastTimestamp <= i.begin {
|
|
|
|
|
intervals[n] = i // new non-overlapping span starts.
|
|
|
|
|
lastTimestamp = i.end
|
|
|
|
|
n++
|
|
|
|
|
} // otherwise, skip because this span overlaps with a previous span.
|
|
|
|
|
}
|
|
|
|
|
gToIntervals[g] = intervals[:n]
|
|
|
|
|
}
|
|
|
|
|
return gToIntervals, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// computePprofIO generates IO pprof-like profile (time spent in IO wait, currently only network blocking event).
|
|
|
|
|
func computePprofIO(w io.Writer, gToIntervals map[uint64][]interval, events []*trace.Event) error {
|
2015-01-30 13:31:43 +03:00
|
|
|
prof := make(map[uint64]Record)
|
|
|
|
|
for _, ev := range events {
|
|
|
|
|
if ev.Type != trace.EvGoBlockNet || ev.Link == nil || ev.StkID == 0 || len(ev.Stk) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
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
|
|
|
overlapping := pprofOverlappingDuration(gToIntervals, ev)
|
|
|
|
|
if overlapping > 0 {
|
|
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += overlapping.Nanoseconds()
|
|
|
|
|
prof[ev.StkID] = rec
|
2017-11-02 19:17:39 -04:00
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// computePprofBlock generates blocking pprof-like profile (time spent blocked on synchronization primitives).
|
|
|
|
|
func computePprofBlock(w io.Writer, gToIntervals map[uint64][]interval, events []*trace.Event) error {
|
2015-01-30 13:31:43 +03:00
|
|
|
prof := make(map[uint64]Record)
|
|
|
|
|
for _, ev := range events {
|
|
|
|
|
switch ev.Type {
|
|
|
|
|
case trace.EvGoBlockSend, trace.EvGoBlockRecv, trace.EvGoBlockSelect,
|
runtime, cmd/trace: track goroutines blocked on GC assists
Currently when a goroutine blocks on a GC assist, it emits a generic
EvGoBlock event. Since assist blocking events and, in particular, the
length of the blocked assist queue, are important for diagnosing GC
behavior, this commit adds a new EvGoBlockGC event for blocking on a
GC assist. The trace viewer uses this event to report a "waiting on
GC" count in the "Goroutines" row. This makes sense because, unlike
other blocked goroutines, these goroutines do have work to do, so
being blocked on a GC assist is quite similar to being in the
"runnable" state, which we also report in the trace viewer.
Change-Id: Ic21a326992606b121ea3d3d00110d8d1fdc7a5ef
Reviewed-on: https://go-review.googlesource.com/30704
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
2016-10-08 18:38:35 -04:00
|
|
|
trace.EvGoBlockSync, trace.EvGoBlockCond, trace.EvGoBlockGC:
|
2017-11-02 19:17:39 -04:00
|
|
|
// TODO(hyangah): figure out why EvGoBlockGC should be here.
|
|
|
|
|
// EvGoBlockGC indicates the goroutine blocks on GC assist, not
|
|
|
|
|
// on synchronization primitives.
|
2015-01-30 13:31:43 +03:00
|
|
|
default:
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if ev.Link == nil || ev.StkID == 0 || len(ev.Stk) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
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
|
|
|
overlapping := pprofOverlappingDuration(gToIntervals, ev)
|
|
|
|
|
if overlapping > 0 {
|
|
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += overlapping.Nanoseconds()
|
|
|
|
|
prof[ev.StkID] = rec
|
2017-11-02 19:17:39 -04:00
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// computePprofSyscall generates syscall pprof-like profile (time spent blocked in syscalls).
|
|
|
|
|
func computePprofSyscall(w io.Writer, gToIntervals map[uint64][]interval, events []*trace.Event) error {
|
2015-01-30 13:31:43 +03:00
|
|
|
prof := make(map[uint64]Record)
|
|
|
|
|
for _, ev := range events {
|
|
|
|
|
if ev.Type != trace.EvGoSysCall || ev.Link == nil || ev.StkID == 0 || len(ev.Stk) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
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
|
|
|
overlapping := pprofOverlappingDuration(gToIntervals, ev)
|
|
|
|
|
if overlapping > 0 {
|
|
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += overlapping.Nanoseconds()
|
|
|
|
|
prof[ev.StkID] = rec
|
2017-11-02 19:17:39 -04:00
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// computePprofSched generates scheduler latency pprof-like profile
|
2015-01-30 13:31:43 +03:00
|
|
|
// (time between a goroutine become runnable and actually scheduled for execution).
|
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
|
|
|
func computePprofSched(w io.Writer, gToIntervals map[uint64][]interval, events []*trace.Event) error {
|
2015-01-30 13:31:43 +03:00
|
|
|
prof := make(map[uint64]Record)
|
|
|
|
|
for _, ev := range events {
|
|
|
|
|
if (ev.Type != trace.EvGoUnblock && ev.Type != trace.EvGoCreate) ||
|
|
|
|
|
ev.Link == nil || ev.StkID == 0 || len(ev.Stk) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
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
|
|
|
overlapping := pprofOverlappingDuration(gToIntervals, ev)
|
|
|
|
|
if overlapping > 0 {
|
|
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += overlapping.Nanoseconds()
|
|
|
|
|
prof[ev.StkID] = rec
|
2017-11-02 19:17:39 -04:00
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
// pprofOverlappingDuration returns the overlapping duration between
|
|
|
|
|
// the time intervals in gToIntervals and the specified event.
|
|
|
|
|
// If gToIntervals is nil, this simply returns the event's duration.
|
|
|
|
|
func pprofOverlappingDuration(gToIntervals map[uint64][]interval, ev *trace.Event) time.Duration {
|
|
|
|
|
if gToIntervals == nil { // No filtering.
|
|
|
|
|
return time.Duration(ev.Link.Ts-ev.Ts) * time.Nanosecond
|
|
|
|
|
}
|
|
|
|
|
intervals := gToIntervals[ev.G]
|
|
|
|
|
if len(intervals) == 0 {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var overlapping time.Duration
|
|
|
|
|
for _, i := range intervals {
|
|
|
|
|
if o := overlappingDuration(i.begin, i.end, ev.Ts, ev.Link.Ts); o > 0 {
|
|
|
|
|
overlapping += o
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return overlapping
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 12:50:38 +01:00
|
|
|
// serveSVGProfile serves pprof-like profile generated by prof as svg.
|
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
|
|
|
func serveSVGProfile(prof func(w io.Writer, r *http.Request) error) http.HandlerFunc {
|
2016-05-24 12:50:38 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2017-11-02 19:17:39 -04:00
|
|
|
|
|
|
|
|
if r.FormValue("raw") != "" {
|
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
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 := prof(w, r); err != nil {
|
2017-11-02 19:17:39 -04:00
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
w.Header().Set("X-Go-Pprof", "1")
|
|
|
|
|
http.Error(w, fmt.Sprintf("failed to get profile: %v", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-24 12:50:38 +01:00
|
|
|
blockf, err := ioutil.TempFile("", "block")
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, fmt.Sprintf("failed to create temp file: %v", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer func() {
|
|
|
|
|
blockf.Close()
|
|
|
|
|
os.Remove(blockf.Name())
|
|
|
|
|
}()
|
|
|
|
|
blockb := bufio.NewWriter(blockf)
|
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 := prof(blockb, r); err != nil {
|
2016-05-24 12:50:38 +01:00
|
|
|
http.Error(w, fmt.Sprintf("failed to generate profile: %v", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := blockb.Flush(); err != nil {
|
|
|
|
|
http.Error(w, fmt.Sprintf("failed to flush temp file: %v", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := blockf.Close(); err != nil {
|
|
|
|
|
http.Error(w, fmt.Sprintf("failed to close temp file: %v", err), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
svgFilename := blockf.Name() + ".svg"
|
2017-09-19 18:18:09 +09:00
|
|
|
if output, err := exec.Command(goCmd(), "tool", "pprof", "-svg", "-output", svgFilename, blockf.Name()).CombinedOutput(); err != nil {
|
2016-05-24 12:50:38 +01:00
|
|
|
http.Error(w, fmt.Sprintf("failed to execute go tool pprof: %v\n%s", err, output), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer os.Remove(svgFilename)
|
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
|
http.ServeFile(w, r, svgFilename)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
}
|
2016-04-12 20:50:59 +02:00
|
|
|
|
|
|
|
|
func buildProfile(prof map[uint64]Record) *profile.Profile {
|
|
|
|
|
p := &profile.Profile{
|
|
|
|
|
PeriodType: &profile.ValueType{Type: "trace", Unit: "count"},
|
|
|
|
|
Period: 1,
|
|
|
|
|
SampleType: []*profile.ValueType{
|
|
|
|
|
{Type: "contentions", Unit: "count"},
|
|
|
|
|
{Type: "delay", Unit: "nanoseconds"},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
locs := make(map[uint64]*profile.Location)
|
|
|
|
|
funcs := make(map[string]*profile.Function)
|
|
|
|
|
for _, rec := range prof {
|
|
|
|
|
var sloc []*profile.Location
|
|
|
|
|
for _, frame := range rec.stk {
|
|
|
|
|
loc := locs[frame.PC]
|
|
|
|
|
if loc == nil {
|
|
|
|
|
fn := funcs[frame.File+frame.Fn]
|
|
|
|
|
if fn == nil {
|
|
|
|
|
fn = &profile.Function{
|
|
|
|
|
ID: uint64(len(p.Function) + 1),
|
|
|
|
|
Name: frame.Fn,
|
|
|
|
|
SystemName: frame.Fn,
|
|
|
|
|
Filename: frame.File,
|
|
|
|
|
}
|
|
|
|
|
p.Function = append(p.Function, fn)
|
|
|
|
|
funcs[frame.File+frame.Fn] = fn
|
|
|
|
|
}
|
|
|
|
|
loc = &profile.Location{
|
|
|
|
|
ID: uint64(len(p.Location) + 1),
|
|
|
|
|
Address: frame.PC,
|
|
|
|
|
Line: []profile.Line{
|
|
|
|
|
profile.Line{
|
|
|
|
|
Function: fn,
|
|
|
|
|
Line: int64(frame.Line),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
p.Location = append(p.Location, loc)
|
|
|
|
|
locs[frame.PC] = loc
|
|
|
|
|
}
|
|
|
|
|
sloc = append(sloc, loc)
|
|
|
|
|
}
|
|
|
|
|
p.Sample = append(p.Sample, &profile.Sample{
|
|
|
|
|
Value: []int64{int64(rec.n), rec.time},
|
|
|
|
|
Location: sloc,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return p
|
|
|
|
|
}
|