2019-12-19 16:41:55 +00:00
|
|
|
// Package profile is meant to be a near identical implemenation of
|
|
|
|
// https://golang.org/src/net/http/pprof/pprof.go
|
|
|
|
// It's purpose is to provide a way to accommodate the RPC endpoint style
|
|
|
|
// we use instead of traditional http handlers.
|
|
|
|
|
|
|
|
package pprof
|
2019-12-04 13:36:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-12-19 16:41:55 +00:00
|
|
|
"runtime"
|
2019-12-04 13:36:12 +00:00
|
|
|
"runtime/pprof"
|
|
|
|
"runtime/trace"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ReqType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
CmdReq ReqType = "cmdline"
|
|
|
|
CPUReq ReqType = "cpu"
|
|
|
|
TraceReq ReqType = "trace"
|
2019-12-19 16:41:55 +00:00
|
|
|
LookupReq ReqType = "lookup"
|
2019-12-09 15:55:43 +00:00
|
|
|
|
2019-12-12 21:52:13 +00:00
|
|
|
ErrProfileNotFoundPrefix = "Pprof profile not found profile:"
|
2019-12-04 13:36:12 +00:00
|
|
|
)
|
|
|
|
|
2019-12-09 15:55:43 +00:00
|
|
|
// NewErrProfileNotFound returns a new error caused by a pprof.Lookup
|
|
|
|
// profile not being found
|
|
|
|
func NewErrProfileNotFound(profile string) error {
|
|
|
|
return fmt.Errorf("%s %s", ErrProfileNotFoundPrefix, profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrProfileNotFound returns whether the error is due to a pprof profile
|
|
|
|
// being invalid
|
|
|
|
func IsErrProfileNotFound(err error) bool {
|
|
|
|
return err != nil && strings.Contains(err.Error(), ErrProfileNotFoundPrefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cmdline responds with the running program's
|
|
|
|
// command line, with arguments separated by NUL bytes.
|
2019-12-11 19:58:41 +00:00
|
|
|
func Cmdline() ([]byte, map[string]string, error) {
|
2019-12-09 15:55:43 +00:00
|
|
|
var buf bytes.Buffer
|
2020-12-10 15:29:18 +00:00
|
|
|
fmt.Fprint(&buf, strings.Join(os.Args, "\x00"))
|
2019-12-11 19:58:41 +00:00
|
|
|
|
|
|
|
return buf.Bytes(),
|
|
|
|
map[string]string{
|
|
|
|
"X-Content-Type-Options": "nosniff",
|
|
|
|
"Content-Type": "text/plain; charset=utf-8",
|
|
|
|
}, nil
|
2019-12-09 15:55:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Profile generates a pprof.Profile report for the given profile name
|
|
|
|
// see runtime/pprof/pprof.go for available profiles.
|
2019-12-19 16:41:55 +00:00
|
|
|
func Profile(profile string, debug, gc int) ([]byte, map[string]string, error) {
|
2019-12-04 13:36:12 +00:00
|
|
|
p := pprof.Lookup(profile)
|
|
|
|
if p == nil {
|
2019-12-11 19:58:41 +00:00
|
|
|
return nil, nil, NewErrProfileNotFound(profile)
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 16:41:55 +00:00
|
|
|
if profile == "heap" && gc > 0 {
|
|
|
|
runtime.GC()
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:36:12 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := p.WriteTo(&buf, debug); err != nil {
|
2019-12-11 19:58:41 +00:00
|
|
|
return nil, nil, err
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
headers := map[string]string{
|
|
|
|
"X-Content-Type-Options": "nosniff",
|
|
|
|
}
|
|
|
|
if debug != 0 {
|
|
|
|
headers["Content-Type"] = "text/plain; charset=utf-8"
|
|
|
|
} else {
|
|
|
|
headers["Content-Type"] = "application/octet-stream"
|
|
|
|
headers["Content-Disposition"] = fmt.Sprintf(`attachment; filename="%s"`, profile)
|
|
|
|
}
|
|
|
|
return buf.Bytes(), headers, nil
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 15:55:43 +00:00
|
|
|
// CPUProfile generates a CPU Profile for a given duration
|
2019-12-11 19:58:41 +00:00
|
|
|
func CPUProfile(ctx context.Context, sec int) ([]byte, map[string]string, error) {
|
2019-12-04 13:36:12 +00:00
|
|
|
if sec <= 0 {
|
|
|
|
sec = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := pprof.StartCPUProfile(&buf); err != nil {
|
2019-12-11 19:58:41 +00:00
|
|
|
return nil, nil, err
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 15:55:43 +00:00
|
|
|
sleep(ctx, time.Duration(sec)*time.Second)
|
2019-12-04 13:36:12 +00:00
|
|
|
|
|
|
|
pprof.StopCPUProfile()
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
return buf.Bytes(),
|
|
|
|
map[string]string{
|
|
|
|
"X-Content-Type-Options": "nosniff",
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
"Content-Disposition": `attachment; filename="profile"`,
|
|
|
|
}, nil
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 15:55:43 +00:00
|
|
|
// Trace runs a trace profile for a given duration
|
2019-12-11 19:58:41 +00:00
|
|
|
func Trace(ctx context.Context, sec int) ([]byte, map[string]string, error) {
|
2019-12-04 13:36:12 +00:00
|
|
|
if sec <= 0 {
|
|
|
|
sec = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := trace.Start(&buf); err != nil {
|
2019-12-11 19:58:41 +00:00
|
|
|
return nil, nil, err
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
sleep(ctx, time.Duration(sec)*time.Second)
|
2019-12-04 13:36:12 +00:00
|
|
|
|
|
|
|
trace.Stop()
|
|
|
|
|
2019-12-11 19:58:41 +00:00
|
|
|
return buf.Bytes(),
|
|
|
|
map[string]string{
|
|
|
|
"X-Content-Type-Options": "nosniff",
|
|
|
|
"Content-Type": "application/octet-stream",
|
|
|
|
"Content-Disposition": `attachment; filename="trace"`,
|
|
|
|
}, nil
|
2019-12-04 13:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func sleep(ctx context.Context, d time.Duration) {
|
2019-12-09 15:55:43 +00:00
|
|
|
// Sleep until duration is met or ctx is cancelled
|
2019-12-04 13:36:12 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(d):
|
|
|
|
case <-ctx.Done():
|
|
|
|
}
|
|
|
|
}
|