plugin/pprof - add option to enable block profiling (#2729)

* - add an option for block profiling to plugin pprof

* - move option block into nested block
This commit is contained in:
Francois Tur 2019-03-29 02:37:17 -04:00 committed by Miek Gieben
parent f6eb2a4c14
commit c144da2524
4 changed files with 53 additions and 11 deletions

View file

@ -17,10 +17,16 @@ This plugin can only be used once per Server Block.
## Syntax ## Syntax
~~~ ~~~
pprof [ADDRESS] pprof [ADDRESS] {
block [RATE]
}
~~~ ~~~
If not specified, ADDRESS defaults to localhost:6053. - If not specified, **ADDRESS** defaults to localhost:6053.
- `block` option allow to enable the `block` profiling. see [Diagnostics, chapter profiling](https://golang.org/doc/diagnostics.html).
if you need to use `block` profile, set a positive value to **RATE**. See [runtime.SetBlockProfileRate](https://golang.org/pkg/runtime/#SetBlockProfileRate).
if not specified, **RATE** default's to 1. if `block` option is not specified the `block` profiling is disabled.
## Examples ## Examples
@ -42,11 +48,13 @@ Listen on an alternate address:
} }
~~~ ~~~
Listen on an all addresses on port 6060: Listen on an all addresses on port 6060: and enable block profiling
~~~ txt ~~~ txt
. { . {
pprof :6060 pprof :6060 {
block
}
} }
~~~ ~~~

View file

@ -6,10 +6,12 @@ import (
"net" "net"
"net/http" "net/http"
pp "net/http/pprof" pp "net/http/pprof"
"runtime"
) )
type handler struct { type handler struct {
addr string addr string
rateBloc int
ln net.Listener ln net.Listener
mux *http.ServeMux mux *http.ServeMux
} }
@ -30,6 +32,8 @@ func (h *handler) Startup() error {
h.mux.HandleFunc(path+"/symbol", pp.Symbol) h.mux.HandleFunc(path+"/symbol", pp.Symbol)
h.mux.HandleFunc(path+"/trace", pp.Trace) h.mux.HandleFunc(path+"/trace", pp.Trace)
runtime.SetBlockProfileRate(h.rateBloc)
go func() { go func() {
http.Serve(h.ln, h.mux) http.Serve(h.ln, h.mux)
}() }()

View file

@ -2,6 +2,7 @@ package pprof
import ( import (
"net" "net"
"strconv"
"sync" "sync"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
@ -36,15 +37,34 @@ func setup(c *caddy.Controller) error {
h.addr = args[0] h.addr = args[0]
_, _, e := net.SplitHostPort(h.addr) _, _, e := net.SplitHostPort(h.addr)
if e != nil { if e != nil {
return e return plugin.Error("pprof", c.Errf("%v", e))
} }
} }
if len(args) > 1 { if len(args) > 1 {
return plugin.Error("pprof", c.ArgErr()) return plugin.Error("pprof", c.ArgErr())
} }
if c.NextBlock() {
for c.NextBlock() {
switch c.Val() {
case "block":
args := c.RemainingArgs()
if len(args) > 1 {
return plugin.Error("pprof", c.ArgErr()) return plugin.Error("pprof", c.ArgErr())
} }
h.rateBloc = 1
if len(args) > 0 {
t, err := strconv.Atoi(args[0])
if err != nil {
return plugin.Error("pprof", c.Errf("property '%s' invalid integer value '%v'", "block", args[0]))
}
h.rateBloc = t
}
default:
return plugin.Error("pprof", c.Errf("unknown property '%s'", c.Val()))
}
}
} }
pprofOnce.Do(func() { pprofOnce.Do(func() {

View file

@ -14,10 +14,20 @@ func TestPProf(t *testing.T) {
{`pprof`, false}, {`pprof`, false},
{`pprof 1.2.3.4:1234`, false}, {`pprof 1.2.3.4:1234`, false},
{`pprof :1234`, false}, {`pprof :1234`, false},
{`pprof {}`, true}, {`pprof :1234 -1`, true},
{`pprof {
}`, false},
{`pprof /foo`, true}, {`pprof /foo`, true},
{`pprof { {`pprof {
a b a b
}`, true},
{`pprof { block
}`, false},
{`pprof :1234 {
block 20
}`, false},
{`pprof {
block 20 30
}`, true}, }`, true},
{`pprof {`pprof
pprof`, true}, pprof`, true},