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"
|
2017-11-02 19:17:39 -04:00
|
|
|
"strconv"
|
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() {
|
2016-05-24 12:50:38 +01:00
|
|
|
http.HandleFunc("/io", serveSVGProfile(pprofIO))
|
|
|
|
|
http.HandleFunc("/block", serveSVGProfile(pprofBlock))
|
|
|
|
|
http.HandleFunc("/syscall", serveSVGProfile(pprofSyscall))
|
|
|
|
|
http.HandleFunc("/sched", serveSVGProfile(pprofSched))
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-02 19:17:39 -04:00
|
|
|
// pprofMatchingGoroutines parses the goroutine type id string (i.e. pc)
|
|
|
|
|
// and returns the ids of goroutines of the matching type.
|
|
|
|
|
// If the id string is empty, returns nil without an error.
|
|
|
|
|
func pprofMatchingGoroutines(id string, events []*trace.Event) (map[uint64]bool, error) {
|
|
|
|
|
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)
|
|
|
|
|
var res map[uint64]bool
|
|
|
|
|
for _, g := range gs {
|
|
|
|
|
if g.PC != pc {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if res == nil {
|
|
|
|
|
res = make(map[uint64]bool)
|
|
|
|
|
}
|
|
|
|
|
res[g.ID] = true
|
|
|
|
|
}
|
|
|
|
|
if len(res) == 0 && id != "" {
|
|
|
|
|
return nil, fmt.Errorf("failed to find matching goroutines for id: %s", id)
|
|
|
|
|
}
|
|
|
|
|
return res, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pprofIO generates IO pprof-like profile (time spent in IO wait,
|
|
|
|
|
// currently only network blocking event).
|
|
|
|
|
func pprofIO(w io.Writer, id string) error {
|
2015-01-30 13:31:43 +03:00
|
|
|
events, err := parseEvents()
|
|
|
|
|
if err != nil {
|
2016-05-24 12:50:38 +01:00
|
|
|
return err
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
goroutines, err := pprofMatchingGoroutines(id, events)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
if goroutines != nil && !goroutines[ev.G] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += ev.Link.Ts - ev.Ts
|
|
|
|
|
prof[ev.StkID] = rec
|
|
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 12:50:38 +01:00
|
|
|
// pprofBlock generates blocking pprof-like profile (time spent blocked on synchronization primitives).
|
2017-11-02 19:17:39 -04:00
|
|
|
func pprofBlock(w io.Writer, id string) error {
|
2015-01-30 13:31:43 +03:00
|
|
|
events, err := parseEvents()
|
|
|
|
|
if err != nil {
|
2016-05-24 12:50:38 +01:00
|
|
|
return err
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
goroutines, err := pprofMatchingGoroutines(id, events)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
if goroutines != nil && !goroutines[ev.G] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += ev.Link.Ts - ev.Ts
|
|
|
|
|
prof[ev.StkID] = rec
|
|
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 12:50:38 +01:00
|
|
|
// pprofSyscall generates syscall pprof-like profile (time spent blocked in syscalls).
|
2017-11-02 19:17:39 -04:00
|
|
|
func pprofSyscall(w io.Writer, id string) error {
|
|
|
|
|
|
2015-01-30 13:31:43 +03:00
|
|
|
events, err := parseEvents()
|
|
|
|
|
if err != nil {
|
2016-05-24 12:50:38 +01:00
|
|
|
return err
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
goroutines, err := pprofMatchingGoroutines(id, events)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
if goroutines != nil && !goroutines[ev.G] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += ev.Link.Ts - ev.Ts
|
|
|
|
|
prof[ev.StkID] = rec
|
|
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 12:50:38 +01:00
|
|
|
// pprofSched 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).
|
2017-11-02 19:17:39 -04:00
|
|
|
func pprofSched(w io.Writer, id string) error {
|
2015-01-30 13:31:43 +03:00
|
|
|
events, err := parseEvents()
|
|
|
|
|
if err != nil {
|
2016-05-24 12:50:38 +01:00
|
|
|
return err
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
goroutines, err := pprofMatchingGoroutines(id, events)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2017-11-02 19:17:39 -04:00
|
|
|
if goroutines != nil && !goroutines[ev.G] {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2015-01-30 13:31:43 +03:00
|
|
|
rec := prof[ev.StkID]
|
|
|
|
|
rec.stk = ev.Stk
|
|
|
|
|
rec.n++
|
|
|
|
|
rec.time += ev.Link.Ts - ev.Ts
|
|
|
|
|
prof[ev.StkID] = rec
|
|
|
|
|
}
|
2016-05-24 12:50:38 +01:00
|
|
|
return buildProfile(prof).Write(w)
|
2015-01-30 13:31:43 +03:00
|
|
|
}
|
|
|
|
|
|
2016-05-24 12:50:38 +01:00
|
|
|
// serveSVGProfile serves pprof-like profile generated by prof as svg.
|
2017-11-02 19:17:39 -04:00
|
|
|
func serveSVGProfile(prof func(w io.Writer, id string) 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")
|
|
|
|
|
if err := prof(w, r.FormValue("id")); err != nil {
|
|
|
|
|
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)
|
2017-11-02 19:17:39 -04:00
|
|
|
if err := prof(blockb, r.FormValue("id")); 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
|
|
|
|
|
}
|