summaryrefslogtreecommitdiff
path: root/devdocs/go/net%2Fhttp%2Fpprof%2Findex.html
blob: 552a61c34d3b2afe39e39f4e000d6482920e04e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<h1> Package pprof  </h1>     <ul id="short-nav">
<li><code>import "net/http/pprof"</code></li>
<li><a href="#pkg-overview" class="overviewLink">Overview</a></li>
<li><a href="#pkg-index" class="indexLink">Index</a></li>
</ul>     <h2 id="pkg-overview">Overview </h2> <p>Package pprof serves via its HTTP server runtime profiling data in the format expected by the pprof visualization tool. </p>
<p>The package is typically only imported for the side effect of registering its HTTP handlers. The handled paths all begin with /debug/pprof/. </p>
<p>To use pprof, link this package into your program: </p>
<pre data-language="go">import _ "net/http/pprof"
</pre> <p>If your application is not already running an http server, you need to start one. Add "net/http" and "log" to your imports and the following code to your main function: </p>
<pre data-language="go">go func() {
	log.Println(http.ListenAndServe("localhost:6060", nil))
}()
</pre> <p>By default, all the profiles listed in <span>runtime/pprof.Profile</span> are available (via <a href="#Handler">Handler</a>), in addition to the <a href="#Cmdline">Cmdline</a>, <a href="#Profile">Profile</a>, <a href="#Symbol">Symbol</a>, and <a href="#Trace">Trace</a> profiles defined in this package. If you are not using DefaultServeMux, you will have to register handlers with the mux you are using. </p>
<h3 id="hdr-Parameters">Parameters</h3> <p>Parameters can be passed via GET query params: </p>
<ul> <li>debug=N (all profiles): response format: N = 0: binary (default), N &gt; 0: plaintext </li>
<li>gc=N (heap profile): N &gt; 0: run a garbage collection cycle before profiling </li>
<li>seconds=N (allocs, block, goroutine, heap, mutex, threadcreate profiles): return a delta profile </li>
<li>seconds=N (cpu (profile), trace profiles): profile for the given duration </li>
</ul> <h3 id="hdr-Usage_examples">Usage examples</h3> <p>Use the pprof tool to look at the heap profile: </p>
<pre data-language="go">go tool pprof http://localhost:6060/debug/pprof/heap
</pre> <p>Or to look at a 30-second CPU profile: </p>
<pre data-language="go">go tool pprof http://localhost:6060/debug/pprof/profile?seconds=30
</pre> <p>Or to look at the goroutine blocking profile, after calling <span>runtime.SetBlockProfileRate</span> in your program: </p>
<pre data-language="go">go tool pprof http://localhost:6060/debug/pprof/block
</pre> <p>Or to look at the holders of contended mutexes, after calling <span>runtime.SetMutexProfileFraction</span> in your program: </p>
<pre data-language="go">go tool pprof http://localhost:6060/debug/pprof/mutex
</pre> <p>The package also exports a handler that serves execution trace data for the "go tool trace" command. To collect a 5-second execution trace: </p>
<pre data-language="go">curl -o trace.out http://localhost:6060/debug/pprof/trace?seconds=5
go tool trace trace.out
</pre> <p>To view all available profiles, open <span>http://localhost:6060/debug/pprof/</span> in your browser. </p>
<p>For a study of the facility in action, visit <a href="https://blog.golang.org/2011/06/profiling-go-programs.html">https://blog.golang.org/2011/06/profiling-go-programs.html</a>. </p>     <h2 id="pkg-index">Index </h2>  <ul id="manual-nav">
<li><a href="#Cmdline">func Cmdline(w http.ResponseWriter, r *http.Request)</a></li>
<li><a href="#Handler">func Handler(name string) http.Handler</a></li>
<li><a href="#Index">func Index(w http.ResponseWriter, r *http.Request)</a></li>
<li><a href="#Profile">func Profile(w http.ResponseWriter, r *http.Request)</a></li>
<li><a href="#Symbol">func Symbol(w http.ResponseWriter, r *http.Request)</a></li>
<li><a href="#Trace">func Trace(w http.ResponseWriter, r *http.Request)</a></li>
</ul> <h3>Package files</h3> <p>  <span>pprof.go</span>  </p>   <h2 id="Cmdline">func <span>Cmdline</span>  </h2> <pre data-language="go">func Cmdline(w http.ResponseWriter, r *http.Request)</pre> <p>Cmdline responds with the running program's command line, with arguments separated by NUL bytes. The package initialization registers it as /debug/pprof/cmdline. </p>
<h2 id="Handler">func <span>Handler</span>  </h2> <pre data-language="go">func Handler(name string) http.Handler</pre> <p>Handler returns an HTTP handler that serves the named profile. Available profiles can be found in <span>runtime/pprof.Profile</span>. </p>
<h2 id="Index">func <span>Index</span>  </h2> <pre data-language="go">func Index(w http.ResponseWriter, r *http.Request)</pre> <p>Index responds with the pprof-formatted profile named by the request. For example, "/debug/pprof/heap" serves the "heap" profile. Index responds to a request for "/debug/pprof/" with an HTML page listing the available profiles. </p>
<h2 id="Profile">func <span>Profile</span>  </h2> <pre data-language="go">func Profile(w http.ResponseWriter, r *http.Request)</pre> <p>Profile responds with the pprof-formatted cpu profile. Profiling lasts for duration specified in seconds GET parameter, or for 30 seconds if not specified. The package initialization registers it as /debug/pprof/profile. </p>
<h2 id="Symbol">func <span>Symbol</span>  </h2> <pre data-language="go">func Symbol(w http.ResponseWriter, r *http.Request)</pre> <p>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. </p>
<h2 id="Trace">func <span>Trace</span>  <span title="Added in Go 1.5">1.5</span> </h2> <pre data-language="go">func Trace(w http.ResponseWriter, r *http.Request)</pre> <p>Trace responds with the execution trace in binary form. Tracing lasts for duration specified in seconds GET parameter, or for 1 second if not specified. The package initialization registers it as /debug/pprof/trace. </p><div class="_attribution">
  <p class="_attribution-p">
    &copy; Google, Inc.<br>Licensed under the Creative Commons Attribution License 3.0.<br>
    <a href="http://golang.org/pkg/net/http/pprof/" class="_attribution-link">http://golang.org/pkg/net/http/pprof/</a>
  </p>
</div>