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

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

Merged
merged 2 commits into from
Mar 29, 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
16 changes: 12 additions & 4 deletions plugin/pprof/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ This plugin can only be used once per Server Block.
## 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

Expand All @@ -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
. {
pprof :6060
pprof :6060 {
block
}
}
~~~

Expand Down
10 changes: 7 additions & 3 deletions plugin/pprof/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
"net"
"net/http"
pp "net/http/pprof"
"runtime"
)

type handler struct {
addr string
ln net.Listener
mux *http.ServeMux
addr string
rateBloc int
ln net.Listener
mux *http.ServeMux
}

func (h *handler) Startup() error {
Expand All @@ -30,6 +32,8 @@ func (h *handler) Startup() error {
h.mux.HandleFunc(path+"/symbol", pp.Symbol)
h.mux.HandleFunc(path+"/trace", pp.Trace)

runtime.SetBlockProfileRate(h.rateBloc)

go func() {
http.Serve(h.ln, h.mux)
}()
Expand Down
26 changes: 23 additions & 3 deletions plugin/pprof/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pprof

import (
"net"
"strconv"
"sync"

"github.com/coredns/coredns/plugin"
Expand Down Expand Up @@ -36,15 +37,34 @@ func setup(c *caddy.Controller) error {
h.addr = args[0]
_, _, e := net.SplitHostPort(h.addr)
if e != nil {
return e
return plugin.Error("pprof", c.Errf("%v", e))
}
}

if len(args) > 1 {
return plugin.Error("pprof", c.ArgErr())
}
if c.NextBlock() {
return plugin.Error("pprof", c.ArgErr())

for c.NextBlock() {
switch c.Val() {
case "block":
args := c.RemainingArgs()
if len(args) > 1 {
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() {
Expand Down
12 changes: 11 additions & 1 deletion plugin/pprof/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,21 @@ func TestPProf(t *testing.T) {
{`pprof`, false},
{`pprof 1.2.3.4:1234`, false},
{`pprof :1234`, false},
{`pprof {}`, true},
{`pprof :1234 -1`, true},
{`pprof {
}`, false},
{`pprof /foo`, true},
{`pprof {
a b
}`, true},
{`pprof { block
}`, false},
{`pprof :1234 {
block 20
}`, false},
{`pprof {
block 20 30
}`, true},
{`pprof
pprof`, true},
}
Expand Down