summaryrefslogtreecommitdiff
path: root/src/pkg/http
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2010-03-24 09:40:09 -0700
committerRuss Cox <rsc@golang.org>2010-03-24 09:40:09 -0700
commit0b6984f741e9ddb628b5ad718682e58da48c707d (patch)
tree881a840ee1f85e3047ae2629fbe9db5f448d3411 /src/pkg/http
parent10fabceda7fd8fab6adf4aa8db16d24ba5d3a730 (diff)
downloadgolang-0b6984f741e9ddb628b5ad718682e58da48c707d.tar.gz
runtime: malloc sampling, pprof interface
R=r CC=golang-dev http://codereview.appspot.com/719041
Diffstat (limited to 'src/pkg/http')
-rw-r--r--src/pkg/http/pprof/Makefile11
-rw-r--r--src/pkg/http/pprof/pprof.go92
2 files changed, 103 insertions, 0 deletions
diff --git a/src/pkg/http/pprof/Makefile b/src/pkg/http/pprof/Makefile
new file mode 100644
index 000000000..e0315112f
--- /dev/null
+++ b/src/pkg/http/pprof/Makefile
@@ -0,0 +1,11 @@
+# Copyright 2010 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+include ../../../Make.$(GOARCH)
+
+TARG=http/pprof
+GOFILES=\
+ pprof.go\
+
+include ../../../Make.pkg
diff --git a/src/pkg/http/pprof/pprof.go b/src/pkg/http/pprof/pprof.go
new file mode 100644
index 000000000..38d91afbf
--- /dev/null
+++ b/src/pkg/http/pprof/pprof.go
@@ -0,0 +1,92 @@
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package pprof serves via its HTTP server runtime profiling data
+// in the format expected by the pprof visualization tool.
+// For more information about pprof, see
+// http://code.google.com/p/google-perftools/.
+//
+// The package is typically only imported for the side effect of
+// registering its HTTP handlers.
+// The handled paths all begin with /debug/pprof/.
+//
+// To use pprof, link this package into your program:
+// import _ "http/pprof"
+//
+// Then use the pprof tool to look at the heap profile:
+//
+// pprof http://localhost:6060/debug/pprof/heap
+//
+package pprof
+
+import (
+ "bufio"
+ "fmt"
+ "http"
+ "os"
+ "runtime"
+ "runtime/pprof"
+ "strconv"
+ "strings"
+)
+
+func init() {
+ http.Handle("/debug/pprof/cmdline", http.HandlerFunc(Cmdline))
+ http.Handle("/debug/pprof/heap", http.HandlerFunc(Heap))
+ http.Handle("/debug/pprof/symbol", http.HandlerFunc(Symbol))
+}
+
+// Cmdline responds with the running program's
+// command line, with arguments separated by NUL bytes.
+// The package initialization registers it as /debug/pprof/cmdline.
+func Cmdline(c *http.Conn, r *http.Request) {
+ c.SetHeader("content-type", "text/plain; charset=utf-8")
+ fmt.Fprintf(c, strings.Join(os.Args, "\x00"))
+}
+
+// Heap responds with the pprof-formatted heap profile.
+// The package initialization registers it as /debug/pprof/heap.
+func Heap(c *http.Conn, r *http.Request) {
+ c.SetHeader("content-type", "text/plain; charset=utf-8")
+ pprof.WriteHeapProfile(c)
+}
+
+// Symbol looks up the program counters listed in the request,
+// responding with a table mapping program counters to function names.
+// The package initialization registers it as /debug/pprof/symbol.
+func Symbol(c *http.Conn, r *http.Request) {
+ c.SetHeader("content-type", "text/plain; charset=utf-8")
+
+ // We don't know how many symbols we have, but we
+ // do have symbol information. Pprof only cares whether
+ // this number is 0 (no symbols available) or > 0.
+ fmt.Fprintf(c, "num_symbols: 1\n")
+
+ var b *bufio.Reader
+ if r.Method == "POST" {
+ b = bufio.NewReader(r.Body)
+ } else {
+ b = bufio.NewReader(strings.NewReader(r.URL.RawQuery))
+ }
+
+ for {
+ w, err := b.ReadSlice('+')
+ if err == nil {
+ w = w[0 : len(w)-1] // trim +
+ }
+ pc, _ := strconv.Btoui64(string(w), 0)
+ if pc != 0 {
+ f := runtime.FuncForPC(uintptr(pc))
+ if f != nil {
+ fmt.Fprintf(c, "%#x %s\n", pc, f.Name())
+ }
+ }
+
+ // Wait until here to check for err; the last
+ // symbol will have an err because it doesn't end in +.
+ if err != nil {
+ break
+ }
+ }
+}