aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAustin Clements <austin@google.com>2016-07-25 13:41:07 -0400
committerAustin Clements <austin@google.com>2016-07-26 22:16:55 +0000
commitb11fff3886705bd98aec4923f43216ed8e13cb1a (patch)
treee83c684f3d38533c23ab0c19c5ddb65f04871e43
parentff60da6962f71871fac3dd6a5406686ea92de8dc (diff)
downloadgo-b11fff3886705bd98aec4923f43216ed8e13cb1a.tar.gz
go-b11fff3886705bd98aec4923f43216ed8e13cb1a.zip
runtime/pprof: document use of pprof package
Currently the pprof package gives almost no guidance for how to use it and, despite the standard boilerplate used to create CPU and memory profiles, this boilerplate appears nowhere in the pprof documentation. Update the pprof package documentation to give the standard boilerplate in a form people can copy, paste, and tweak. This boilerplate is based on rsc's 2011 blog post on profiling Go programs at https://blog.golang.org/profiling-go-programs, which is where I always go when I need to copy-paste the boilerplate. Change-Id: I74021e494ea4dcc6b56d6fb5e59829ad4bb7b0be Reviewed-on: https://go-review.googlesource.com/25182 Reviewed-by: Rick Hudson <rlh@golang.org>
-rw-r--r--src/runtime/pprof/pprof.go63
1 files changed, 62 insertions, 1 deletions
diff --git a/src/runtime/pprof/pprof.go b/src/runtime/pprof/pprof.go
index b7c41f13de..25f7ed6eb1 100644
--- a/src/runtime/pprof/pprof.go
+++ b/src/runtime/pprof/pprof.go
@@ -4,8 +4,69 @@
// Package pprof writes runtime profiling data in the format expected
// by the pprof visualization tool.
+//
+// Profiling a Go program
+//
+// The first step to profiling a Go program is to enable profiling.
+// Support for profiling benchmarks built with the standard testing
+// package is built into go test. For example, the following command
+// runs benchmarks in the current directory and writes the CPU and
+// memory profiles to cpu.prof and mem.prof:
+//
+// go test -cpuprofile cpu.prof -memprofile mem.prof -bench .
+//
+// To add equivalent profiling support to a standalone program, add
+// code like the following to your main function:
+//
+// var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")
+// var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
+//
+// func main() {
+// flag.Parse()
+// if *cpuprofile != "" {
+// f, err := os.Create(*cpuprofile)
+// if err != nil {
+// log.Fatal("could not create CPU profile: ", err)
+// }
+// if err := pprof.StartCPUProfile(f); err != nil {
+// log.Fatal("could not start CPU profile: ", err)
+// }
+// defer pprof.StopCPUProfile()
+// }
+// ...
+// if *memprofile != "" {
+// f, err := os.Create(*memprofile)
+// if err != nil {
+// log.Fatal("could not create memory profile: ", err)
+// }
+// runtime.GC() // get up-to-date statistics
+// if err := pprof.WriteHeapProfile(f); err != nil {
+// log.Fatal("could not write memory profile: ", err)
+// }
+// f.Close()
+// }
+// }
+//
+// There is also a standard HTTP interface to profiling data. Adding
+// the following line will install handlers under the /debug/pprof/
+// URL to download live profiles:
+//
+// import _ "net/http/pprof"
+//
+// See the net/http/pprof package for more details.
+//
+// Profiles can then be visualized with the pprof tool:
+//
+// go tool pprof cpu.prof
+//
+// There are many commands available from the pprof command line.
+// Commonly used commands include "top", which prints a summary of the
+// top program hot-spots, and "web", which opens an interactive graph
+// of hot-spots and their call graphs. Use "help" for information on
+// all pprof commands.
+//
// For more information about pprof, see
-// http://github.com/google/pprof/.
+// https://github.com/google/pprof/blob/master/doc/pprof.md.
package pprof
import (