Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server/debug: expose panicparse's UI at /debug/pprof/goroutineui #35148

Merged
merged 6 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ ignored = [
name = "github.com/docker/docker"
branch = "master"

[[constraint]]
name = "github.com/maruel/panicparse"
revision = "f20d4c4d746f810c9110e21928d4135e1f2a3efa"

# https://github.com/getsentry/raven-go/pull/139
[[constraint]]
name = "github.com/getsentry/raven-go"
Expand Down
100 changes: 100 additions & 0 deletions pkg/server/debug/goroutineui/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2019 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package goroutineui

import (
"bytes"
"io"
"io/ioutil"
"runtime"
"sort"
"strings"
"time"

"github.com/maruel/panicparse/stack"
)

// stacks is a wrapper for runtime.Stack that attempts to recover the data for all goroutines.
func stacks() []byte {
// We don't know how big the traces are, so grow a few times if they don't fit. Start large, though.
var trace []byte
for n := 1 << 20; /* 1mb */ n <= (1 << 29); /* 512mb */ n *= 2 {
trace = make([]byte, n)
nbytes := runtime.Stack(trace, true /* all */)
if nbytes < len(trace) {
return trace[:nbytes]
}
}
return trace
}

// A Dump wraps a goroutine dump with functionality to output through panicparse.
type Dump struct {
err error

now time.Time
buckets []*stack.Bucket
}

// NewDump grabs a goroutine dump and associates it with the supplied time.
func NewDump(now time.Time) Dump {
return NewDumpFromBytes(now, stacks())
}

// NewDumpFromBytes is like NewDump, but treats the supplied bytes as a goroutine
// dump.
func NewDumpFromBytes(now time.Time, b []byte) Dump {
c, err := stack.ParseDump(bytes.NewReader(b), ioutil.Discard, true /* guesspaths */)
if err != nil {
return Dump{err: err}
}
return Dump{now: now, buckets: stack.Aggregate(c.Goroutines, stack.AnyValue)}
}

// SortCountDesc rearranges the goroutine buckets such that higher multiplicities
// appear earlier.
func (d Dump) SortCountDesc() {
sort.Slice(d.buckets, func(i, j int) bool {
a, b := d.buckets[i], d.buckets[j]
return len(a.IDs) > len(b.IDs)
})
}

// SortWaitDesc rearranges the goroutine buckets such that goroutines that have
// longer wait times appear earlier.
func (d Dump) SortWaitDesc() {
sort.Slice(d.buckets, func(i, j int) bool {
a, b := d.buckets[i], d.buckets[j]
return a.SleepMax > b.SleepMax
})
}

// HTML writes the rendered output of panicparse into the supplied Writer.
func (d Dump) HTML(w io.Writer) error {
if d.err != nil {
return d.err
}
return writeToHTML(w, d.buckets, d.now)
}

// HTMLString is like HTML, but returns a string. If an error occurs, its string
// representation is returned.
func (d Dump) HTMLString() string {
var w strings.Builder
if err := d.HTML(&w); err != nil {
return err.Error()
}
return w.String()
}
98 changes: 98 additions & 0 deletions pkg/server/debug/goroutineui/dump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright 2019 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.

package goroutineui

import (
"io/ioutil"
"testing"
"time"

"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/stretchr/testify/assert"
)

func TestDumpHTML(t *testing.T) {
defer leaktest.AfterTest(t)()

now := time.Time{}
dump := NewDumpFromBytes(now, []byte(fixture))
dump.SortWaitDesc() // noop
dump.SortCountDesc() // noop
act := dump.HTMLString()

if false {
_ = ioutil.WriteFile("test.html", []byte(act), 0644)
}
assert.Equal(t, exp, act)
}

// This is the output of debug.PrintStack() on the go playground.
const fixture = `goroutine 1 [running]:
runtime/debug.Stack(0x434070, 0xddb11, 0x0, 0x40e0f8)
/usr/local/go/src/runtime/debug/stack.go:24 +0xc0
runtime/debug.PrintStack()
/usr/local/go/src/runtime/debug/stack.go:16 +0x20
main.main()
/tmp/sandbox157492124/main.go:6 +0x20`

const exp = `<!DOCTYPE html><meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PanicParse</title>
<link rel="shortcut icon" type="image/png" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIgAAACACAMAAADnN9ENAAAA5FBMVEVMaXFvTjQhISEhISEhISEhISEhISEhISEhISEhISEhISEhISHRbBTRbBTRbBQhISHRbBTRbBTRbBTRbBQ6OjrRbBTZcBTsehbzfhf1fxfidRVOTk75oiL7uCr90zL3kB3/6zv2hhlHMR5OTk5KSkpKSkpRUVFKSkpKSkpMTExBQUE2NjYpKSlOTk5EREQ8PDw4ODgtLS1RUVFQUFAmJiYkJCQ0NDRLS0tTU1MoKCgyMjJHR0dTU1NdXV0rKytXV1dVVVUvLy9eXl5aWlpcXFxcXFxeXl5fX186OjphYWE/Pz9KSkrdB5CTAAAATHRSTlMAEUZggJjf/6/vcL86e1nP2//vmSC7//////9E/////////2WPpYHp////////////////////z/////8w6P////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itqPtRKSYZDrTMyrTOuC/BIJbGRhmXKfLktmkk8QwphfQRkA6jz1zIy&#43;PAbsRbInQi8qgF6C4H9PvfQ2E8NXrR7z9tJvf&#43;Z1/ANt&#43;S&#43;GBXoDpAAAAAElFTkSuQmCC"/>
<style>
body {
background: black;
color: lightgray;
}
body, pre {
font-family: Menlo, monospace;
font-weight: bold;
}
.FuncStdLibExported {
color: #7CFC00;
}
.FuncStdLib {
color: #008000;
}
.FuncMain {
color: #C0C000;
}
.FuncOtherExported {
color: #FF0000;
}
.FuncOther {
color: #A00000;
}
.RoutineFirst {
}
.Routine {
}
</style>
<div id="legend">Generated on 0001-01-01 00:00:00 &#43;0000 UTC.
</div>
<div id="content">

<h1>Running Routine</h1>
<span class="Routine">1: <span class="state">running</span>

<h2>Stack</h2>

- stack.go:24 <span class="FuncStdLibExported">Stack</span>({[{4407408 } {908049 } {0 } {4251896 }] [] false})<br>
- stack.go:16 <span class="FuncStdLibExported">PrintStack</span>({[] [] false})<br>
- .:0 <span class="FuncMain">main</span>({[] [] false})<br>


</div>
`
Loading