| 1 |
747 |
jeremybenn |
// Copyright 2010 The Go Authors. All rights reserved.
|
| 2 |
|
|
// Use of this source code is governed by a BSD-style
|
| 3 |
|
|
// license that can be found in the LICENSE file.
|
| 4 |
|
|
|
| 5 |
|
|
// Package pprof serves via its HTTP server runtime profiling data
|
| 6 |
|
|
// in the format expected by the pprof visualization tool.
|
| 7 |
|
|
// For more information about pprof, see
|
| 8 |
|
|
// http://code.google.com/p/google-perftools/.
|
| 9 |
|
|
//
|
| 10 |
|
|
// The package is typically only imported for the side effect of
|
| 11 |
|
|
// registering its HTTP handlers.
|
| 12 |
|
|
// The handled paths all begin with /debug/pprof/.
|
| 13 |
|
|
//
|
| 14 |
|
|
// To use pprof, link this package into your program:
|
| 15 |
|
|
// import _ "http/pprof"
|
| 16 |
|
|
//
|
| 17 |
|
|
// Then use the pprof tool to look at the heap profile:
|
| 18 |
|
|
//
|
| 19 |
|
|
// pprof http://localhost:6060/debug/pprof/heap
|
| 20 |
|
|
//
|
| 21 |
|
|
// Or to look at a 30-second CPU profile:
|
| 22 |
|
|
//
|
| 23 |
|
|
// pprof http://localhost:6060/debug/pprof/profile
|
| 24 |
|
|
//
|
| 25 |
|
|
package pprof
|
| 26 |
|
|
|
| 27 |
|
|
import (
|
| 28 |
|
|
"bufio"
|
| 29 |
|
|
"bytes"
|
| 30 |
|
|
"fmt"
|
| 31 |
|
|
"io"
|
| 32 |
|
|
"net/http"
|
| 33 |
|
|
"os"
|
| 34 |
|
|
"runtime"
|
| 35 |
|
|
"runtime/pprof"
|
| 36 |
|
|
"strconv"
|
| 37 |
|
|
"strings"
|
| 38 |
|
|
"time"
|
| 39 |
|
|
)
|
| 40 |
|
|
|
| 41 |
|
|
func init() {
|
| 42 |
|
|
http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
|
| 43 |
|
|
http.Handle("/debug/pprof/profile", http.HandlerFunc(Profile))
|
| 44 |
|
|
http.Handle("/debug/pprof/heap", http.HandlerFunc(Heap))
|
| 45 |
|
|
http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
|
| 46 |
|
|
}
|
| 47 |
|
|
|
| 48 |
|
|
// Cmdline responds with the running program's
|
| 49 |
|
|
// command line, with arguments separated by NUL bytes.
|
| 50 |
|
|
// The package initialization registers it as /debug/pprof/cmdline.
|
| 51 |
|
|
func Cmdline(w http.ResponseWriter, r *http.Request) {
|
| 52 |
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
| 53 |
|
|
fmt.Fprintf(w, strings.Join(os.Args, "\x00"))
|
| 54 |
|
|
}
|
| 55 |
|
|
|
| 56 |
|
|
// Heap responds with the pprof-formatted heap profile.
|
| 57 |
|
|
// The package initialization registers it as /debug/pprof/heap.
|
| 58 |
|
|
func Heap(w http.ResponseWriter, r *http.Request) {
|
| 59 |
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
| 60 |
|
|
pprof.WriteHeapProfile(w)
|
| 61 |
|
|
}
|
| 62 |
|
|
|
| 63 |
|
|
// Profile responds with the pprof-formatted cpu profile.
|
| 64 |
|
|
// The package initialization registers it as /debug/pprof/profile.
|
| 65 |
|
|
func Profile(w http.ResponseWriter, r *http.Request) {
|
| 66 |
|
|
sec, _ := strconv.ParseInt(r.FormValue("seconds"), 10, 64)
|
| 67 |
|
|
if sec == 0 {
|
| 68 |
|
|
sec = 30
|
| 69 |
|
|
}
|
| 70 |
|
|
|
| 71 |
|
|
// Set Content Type assuming StartCPUProfile will work,
|
| 72 |
|
|
// because if it does it starts writing.
|
| 73 |
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
| 74 |
|
|
if err := pprof.StartCPUProfile(w); err != nil {
|
| 75 |
|
|
// StartCPUProfile failed, so no writes yet.
|
| 76 |
|
|
// Can change header back to text content
|
| 77 |
|
|
// and send error code.
|
| 78 |
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
| 79 |
|
|
w.WriteHeader(http.StatusInternalServerError)
|
| 80 |
|
|
fmt.Fprintf(w, "Could not enable CPU profiling: %s\n", err)
|
| 81 |
|
|
return
|
| 82 |
|
|
}
|
| 83 |
|
|
time.Sleep(time.Duration(sec) * time.Second)
|
| 84 |
|
|
pprof.StopCPUProfile()
|
| 85 |
|
|
}
|
| 86 |
|
|
|
| 87 |
|
|
// Symbol looks up the program counters listed in the request,
|
| 88 |
|
|
// responding with a table mapping program counters to function names.
|
| 89 |
|
|
// The package initialization registers it as /debug/pprof/symbol.
|
| 90 |
|
|
func Symbol(w http.ResponseWriter, r *http.Request) {
|
| 91 |
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
| 92 |
|
|
|
| 93 |
|
|
// We have to read the whole POST body before
|
| 94 |
|
|
// writing any output. Buffer the output here.
|
| 95 |
|
|
var buf bytes.Buffer
|
| 96 |
|
|
|
| 97 |
|
|
// We don't know how many symbols we have, but we
|
| 98 |
|
|
// do have symbol information. Pprof only cares whether
|
| 99 |
|
|
// this number is 0 (no symbols available) or > 0.
|
| 100 |
|
|
fmt.Fprintf(&buf, "num_symbols: 1\n")
|
| 101 |
|
|
|
| 102 |
|
|
var b *bufio.Reader
|
| 103 |
|
|
if r.Method == "POST" {
|
| 104 |
|
|
b = bufio.NewReader(r.Body)
|
| 105 |
|
|
} else {
|
| 106 |
|
|
b = bufio.NewReader(strings.NewReader(r.URL.RawQuery))
|
| 107 |
|
|
}
|
| 108 |
|
|
|
| 109 |
|
|
for {
|
| 110 |
|
|
word, err := b.ReadSlice('+')
|
| 111 |
|
|
if err == nil {
|
| 112 |
|
|
word = word[0 : len(word)-1] // trim +
|
| 113 |
|
|
}
|
| 114 |
|
|
pc, _ := strconv.ParseUint(string(word), 0, 64)
|
| 115 |
|
|
if pc != 0 {
|
| 116 |
|
|
f := runtime.FuncForPC(uintptr(pc))
|
| 117 |
|
|
if f != nil {
|
| 118 |
|
|
fmt.Fprintf(&buf, "%#x %s\n", pc, f.Name())
|
| 119 |
|
|
}
|
| 120 |
|
|
}
|
| 121 |
|
|
|
| 122 |
|
|
// Wait until here to check for err; the last
|
| 123 |
|
|
// symbol will have an err because it doesn't end in +.
|
| 124 |
|
|
if err != nil {
|
| 125 |
|
|
if err != io.EOF {
|
| 126 |
|
|
fmt.Fprintf(&buf, "reading request: %v\n", err)
|
| 127 |
|
|
}
|
| 128 |
|
|
break
|
| 129 |
|
|
}
|
| 130 |
|
|
}
|
| 131 |
|
|
|
| 132 |
|
|
w.Write(buf.Bytes())
|
| 133 |
|
|
}
|