Skip to content

Commit ef24fd7

Browse files
agnivadehyangah
authored andcommitted
net/http/pprof: update the /debug/pprof endpoint
- Documented the duration parameter in Profile() to match with Trace(). - Properly handling the error from strconv.ParseInt to match with Trace(). - Updated the profiles tables to include additional handlers exposed from net/http/pprof. Added a separate section at the bottom to explain what the profiles are and how to use them. Fixes #24380 Change-Id: I8b7e100d6826a4feec81f29f918e7a7f7ccc71a0 Reviewed-on: https://go-review.googlesource.com/112495 Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent e4172e5 commit ef24fd7

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

src/net/http/pprof/pprof.go

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
//
2727
// Or to look at a 30-second CPU profile:
2828
//
29-
// go tool pprof http://localhost:6060/debug/pprof/profile
29+
// go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
3030
//
3131
// Or to look at the goroutine blocking profile, after calling
3232
// runtime.SetBlockProfileRate in your program:
@@ -63,6 +63,7 @@ import (
6363
"runtime"
6464
"runtime/pprof"
6565
"runtime/trace"
66+
"sort"
6667
"strconv"
6768
"strings"
6869
"time"
@@ -110,11 +111,12 @@ func serveError(w http.ResponseWriter, status int, txt string) {
110111
}
111112

112113
// Profile responds with the pprof-formatted cpu profile.
114+
// Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified.
113115
// The package initialization registers it as /debug/pprof/profile.
114116
func Profile(w http.ResponseWriter, r *http.Request) {
115117
w.Header().Set("X-Content-Type-Options", "nosniff")
116-
sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
117-
if sec == 0 {
118+
sec, err := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
119+
if sec <= 0 || err != nil {
118120
sec = 30
119121
}
120122

@@ -243,6 +245,18 @@ func (name handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
243245
p.WriteTo(w, debug)
244246
}
245247

248+
var profileDescriptions = map[string]string{
249+
"allocs": "A sampling of all past memory allocations",
250+
"block": "Stack traces that led to blocking on synchronization primitives",
251+
"cmdline": "The command line invocation of the current program",
252+
"goroutine": "Stack traces of all current goroutines",
253+
"heap": "A sampling of memory allocations of live objects. You can specify the gc GET parameter to run GC before taking the heap sample.",
254+
"mutex": "Stack traces of holders of contended mutexes",
255+
"profile": "CPU profile. You can specify the duration in the seconds GET parameter. After you get the profile file, use the go tool pprof command to investigate the profile.",
256+
"threadcreate": "Stack traces that led to the creation of new OS threads",
257+
"trace": "A trace of execution of the current program. You can specify the duration in the seconds GET parameter. After you get the trace file, use the go tool trace command to investigate the trace.",
258+
}
259+
246260
// Index responds with the pprof-formatted profile named by the request.
247261
// For example, "/debug/pprof/heap" serves the "heap" profile.
248262
// Index responds to a request for "/debug/pprof/" with an HTML page
@@ -256,7 +270,35 @@ func Index(w http.ResponseWriter, r *http.Request) {
256270
}
257271
}
258272

259-
profiles := pprof.Profiles()
273+
type profile struct {
274+
Name string
275+
Href string
276+
Desc string
277+
Count int
278+
}
279+
var profiles []profile
280+
for _, p := range pprof.Profiles() {
281+
profiles = append(profiles, profile{
282+
Name: p.Name(),
283+
Href: p.Name() + "?debug=1",
284+
Desc: profileDescriptions[p.Name()],
285+
Count: p.Count(),
286+
})
287+
}
288+
289+
// Adding other profiles exposed from within this package
290+
for _, p := range []string{"cmdline", "profile", "trace"} {
291+
profiles = append(profiles, profile{
292+
Name: p,
293+
Href: p,
294+
Desc: profileDescriptions[p],
295+
})
296+
}
297+
298+
sort.Slice(profiles, func(i, j int) bool {
299+
return profiles[i].Name < profiles[j].Name
300+
})
301+
260302
if err := indexTmpl.Execute(w, profiles); err != nil {
261303
log.Print(err)
262304
}
@@ -265,18 +307,35 @@ func Index(w http.ResponseWriter, r *http.Request) {
265307
var indexTmpl = template.Must(template.New("index").Parse(`<html>
266308
<head>
267309
<title>/debug/pprof/</title>
310+
<style>
311+
.profile-name{
312+
display:inline-block;
313+
width:6rem;
314+
}
315+
</style>
268316
</head>
269317
<body>
270318
/debug/pprof/<br>
271319
<br>
272-
profiles:<br>
320+
Types of profiles available:
273321
<table>
322+
<thread><td>Count</td><td>Profile</td></thead>
274323
{{range .}}
275-
<tr><td align=right>{{.Count}}<td><a href="{{.Name}}?debug=1">{{.Name}}</a>
324+
<tr>
325+
<td>{{.Count}}</td><td><a href={{.Href}}>{{.Name}}</a></td>
326+
</tr>
276327
{{end}}
277328
</table>
278-
<br>
279-
<a href="goroutine?debug=2">full goroutine stack dump</a><br>
329+
<a href="goroutine?debug=2">full goroutine stack dump</a>
330+
<br/>
331+
<p>
332+
Profile Descriptions:
333+
<ul>
334+
{{range .}}
335+
<li><div class=profile-name>{{.Name}}:</div> {{.Desc}}</li>
336+
{{end}}
337+
</ul>
338+
</p>
280339
</body>
281340
</html>
282341
`))

src/net/http/pprof/pprof_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@ import (
99
"io/ioutil"
1010
"net/http"
1111
"net/http/httptest"
12+
"runtime/pprof"
1213
"testing"
1314
)
1415

16+
// TestDescriptions checks that the profile names under runtime/pprof package
17+
// have a key in the description map.
18+
func TestDescriptions(t *testing.T) {
19+
for _, p := range pprof.Profiles() {
20+
_, ok := profileDescriptions[p.Name()]
21+
if ok != true {
22+
t.Errorf("%s does not exist in profileDescriptions map\n", p.Name())
23+
}
24+
}
25+
}
26+
1527
func TestHandlers(t *testing.T) {
1628
testCases := []struct {
1729
path string

0 commit comments

Comments
 (0)