pprof_remote_servers.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <HTML>
  2. <HEAD>
  3. <title>pprof and Remote Servers</title>
  4. </HEAD>
  5. <BODY>
  6. <h1><code>pprof</code> and Remote Servers</h1>
  7. <p>In mid-2006, we added an experimental facility to <A
  8. HREF="cpu_profiler.html">pprof</A>, the tool that analyzes CPU and
  9. heap profiles. This facility allows you to collect profile
  10. information from running applications. It makes it easy to collect
  11. profile information without having to stop the program first, and
  12. without having to log into the machine where the application is
  13. running. This is meant to be used on webservers, but will work on any
  14. application that can be modified to accept TCP connections on a port
  15. of its choosing, and to respond to HTTP requests on that port.</p>
  16. <p>We do not currently have infrastructure, such as apache modules,
  17. that you can pop into a webserver or other application to get the
  18. necessary functionality "for free." However, it's easy to generate
  19. the necessary data, which should allow the interested developer to add
  20. the necessary support into his or her applications.</p>
  21. <p>To use <code>pprof</code> in this experimental "server" mode, you
  22. give the script a host and port it should query, replacing the normal
  23. commandline arguments of application + profile file:</p>
  24. <pre>
  25. % pprof internalweb.mycompany.com:80
  26. </pre>
  27. <p>The host must be listening on that port, and be able to accept HTTP/1.0
  28. requests -- sent via <code>wget</code> and <code>curl</code> -- for
  29. several urls. The following sections list the urls that
  30. <code>pprof</code> can send, and the responses it expects in
  31. return.</p>
  32. <p>Here are examples that pprof will recognize, when you give them
  33. on the commandline, are urls. In general, you
  34. specify the host and a port (the port-number is required), and put
  35. the service-name at the end of the url.:</p>
  36. <blockquote><pre>
  37. http://myhost:80/pprof/heap # retrieves a heap profile
  38. http://myhost:8008/pprof/profile # retrieves a CPU profile
  39. http://myhost:80 # retrieves a CPU profile (the default)
  40. http://myhost:8080/ # retrieves a CPU profile (the default)
  41. myhost:8088/pprof/growth # "http://" is optional, but port is not
  42. http://myhost:80/myservice/pprof/heap # /pprof/heap just has to come at the end
  43. http://myhost:80/pprof/pmuprofile # CPU profile using performance counters
  44. </pre></blockquote>
  45. <h2> <code><b>/pprof/heap</b></code> </h2>
  46. <p><code>pprof</code> asks for the url <code>/pprof/heap</code> to
  47. get heap information. The actual url is controlled via the variable
  48. <code>HEAP_PAGE</code> in the <code>pprof</code> script, so you
  49. can change it if you'd like.</p>
  50. <p>There are two ways to get this data. The first is to call</p>
  51. <pre>
  52. MallocExtension::instance()->GetHeapSample(&output);
  53. </pre>
  54. <p>and have the server send <code>output</code> back as an HTTP
  55. response to <code>pprof</code>. <code>MallocExtension</code> is
  56. defined in the header file <code>gperftools/malloc_extension.h</code>.</p>
  57. <p>Note this will only only work if the binary is being run with
  58. sampling turned on (which is not the default). To do this, set the
  59. environment variable <code>TCMALLOC_SAMPLE_PARAMETER</code> to a
  60. positive value, such as 524288, before running.</p>
  61. <p>The other way is to call <code>HeapProfileStart(filename)</code>
  62. (from <code>heap-profiler.h</code>), continue to do work, and then,
  63. some number of seconds later, call <code>GetHeapProfile()</code>
  64. (followed by <code>HeapProfilerStop()</code>). The server can send
  65. the output of <code>GetHeapProfile</code> back as the HTTP response to
  66. pprof. (Note you must <code>free()</code> this data after using it.)
  67. This is similar to how <A HREF="#profile">profile requests</A> are
  68. handled, below. This technique does not require the application to
  69. run with sampling turned on.</p>
  70. <p>Here's an example of what the output should look like:</p>
  71. <pre>
  72. heap profile: 1923: 127923432 [ 1923: 127923432] @ heap_v2/524288
  73. 1: 312 [ 1: 312] @ 0x2aaaabaf5ccc 0x2aaaaba4cd2c 0x2aaaac08c09a
  74. 928: 122586016 [ 928: 122586016] @ 0x2aaaabaf682c 0x400680 0x400bdd 0x2aaaab1c368a 0x2aaaab1c8f77 0x2aaaab1c0396 0x2aaaab1c86ed 0x4007ff 0x2aaaaca62afa
  75. 1: 16 [ 1: 16] @ 0x2aaaabaf5ccc 0x2aaaabb04bac 0x2aaaabc1b262 0x2aaaabc21496 0x2aaaabc214bb
  76. [...]
  77. </pre>
  78. <p> Older code may produce "version 1" heap profiles which look like this:<p/>
  79. <pre>
  80. heap profile: 14933: 791700132 [ 14933: 791700132] @ heap
  81. 1: 848688 [ 1: 848688] @ 0xa4b142 0x7f5bfc 0x87065e 0x4056e9 0x4125f8 0x42b4f1 0x45b1ba 0x463248 0x460871 0x45cb7c 0x5f1744 0x607cee 0x5f4a5e 0x40080f 0x2aaaabad7afa
  82. 1: 1048576 [ 1: 1048576] @ 0xa4a9b2 0x7fd025 0x4ca6d8 0x4ca814 0x4caa88 0x2aaaab104cf0 0x404e20 0x4125f8 0x42b4f1 0x45b1ba 0x463248 0x460871 0x45cb7c 0x5f1744 0x607cee 0x5f4a5e 0x40080f 0x2aaaabad7afa
  83. 2942: 388629374 [ 2942: 388629374] @ 0xa4b142 0x4006a0 0x400bed 0x5f0cfa 0x5f1744 0x607cee 0x5f4a5e 0x40080f 0x2aaaabad7afa
  84. [...]
  85. </pre>
  86. <p>pprof accepts both old and new heap profiles and automatically
  87. detects which one you are using.</p>
  88. <h2> <code><b>/pprof/growth</b></code> </h2>
  89. <p><code>pprof</code> asks for the url <code>/pprof/growth</code> to
  90. get heap-profiling delta (growth) information. The actual url is
  91. controlled via the variable <code>GROWTH_PAGE</code> in the
  92. <code>pprof</code> script, so you can change it if you'd like.</p>
  93. <p>The server should respond by calling</p>
  94. <pre>
  95. MallocExtension::instance()->GetHeapGrowthStacks(&output);
  96. </pre>
  97. <p>and sending <code>output</code> back as an HTTP response to
  98. <code>pprof</code>. <code>MallocExtension</code> is defined in the
  99. header file <code>gperftools/malloc_extension.h</code>.</p>
  100. <p>Here's an example, from an actual Google webserver, of what the
  101. output should look like:</p>
  102. <pre>
  103. heap profile: 741: 812122112 [ 741: 812122112] @ growth
  104. 1: 1572864 [ 1: 1572864] @ 0x87da564 0x87db8a3 0x84787a4 0x846e851 0x836d12f 0x834cd1c 0x8349ba5 0x10a3177 0x8349961
  105. 1: 1048576 [ 1: 1048576] @ 0x87d92e8 0x87d9213 0x87d9178 0x87d94d3 0x87da9da 0x8a364ff 0x8a437e7 0x8ab7d23 0x8ab7da9 0x8ac7454 0x8348465 0x10a3161 0x8349961
  106. [...]
  107. </pre>
  108. <h2> <A NAME="profile"><code><b>/pprof/profile</b></code></A> </h2>
  109. <p><code>pprof</code> asks for the url
  110. <code>/pprof/profile?seconds=XX</code> to get cpu-profiling
  111. information. The actual url is controlled via the variable
  112. <code>PROFILE_PAGE</code> in the <code>pprof</code> script, so you can
  113. change it if you'd like.</p>
  114. <p>The server should respond by calling
  115. <code>ProfilerStart(filename)</code>, continuing to do its work, and
  116. then, XX seconds later, calling <code>ProfilerStop()</code>. (These
  117. functions are declared in <code>gperftools/profiler.h</code>.) The
  118. application is responsible for picking a unique filename for
  119. <code>ProfilerStart()</code>. After calling
  120. <code>ProfilerStop()</code>, the server should read the contents of
  121. <code>filename</code> and send them back as an HTTP response to
  122. <code>pprof</code>.</p>
  123. <p>Obviously, to get useful profile information the application must
  124. continue to run in the XX seconds that the profiler is running. Thus,
  125. the profile start-stop calls should be done in a separate thread, or
  126. be otherwise non-blocking.</p>
  127. <p>The profiler output file is binary, but near the end of it, it
  128. should have lines of text somewhat like this:</p>
  129. <pre>
  130. 01016000-01017000 rw-p 00015000 03:01 59314 /lib/ld-2.2.2.so
  131. </pre>
  132. <h2> <code><b>/pprof/pmuprofile</b></code> </h2>
  133. <code>pprof</code> asks for a url of the form
  134. <code>/pprof/pmuprofile?event=hw_event:unit_mask&period=nnn&seconds=xxx</code>
  135. to get cpu-profiling information. The actual url is controlled via the variable
  136. <code>PMUPROFILE_PAGE</code> in the <code>pprof</code> script, so you can
  137. change it if you'd like.</p>
  138. <p>
  139. This is similar to pprof, but is meant to be used with your CPU's hardware
  140. performance counters. The server could be implemented on top of a library
  141. such as <a href="http://perfmon2.sourceforge.net/">
  142. <code>libpfm</code></a>. It should collect a sample every nnn occurrences
  143. of the event and stop the sampling after xxx seconds. Much of the code
  144. for <code>/pprof/profile</code> can be reused for this purpose.
  145. </p>
  146. <p>The server side routines (the equivalent of
  147. ProfilerStart/ProfilerStart) are not available as part of perftools,
  148. so this URL is unlikely to be that useful.</p>
  149. <h2> <code><b>/pprof/contention</b></code> </h2>
  150. <p>This is intended to be able to profile (thread) lock contention in
  151. addition to CPU and memory use. It's not yet usable.</p>
  152. <h2> <code><b>/pprof/cmdline</b></code> </h2>
  153. <p><code>pprof</code> asks for the url <code>/pprof/cmdline</code> to
  154. figure out what application it's profiling. The actual url is
  155. controlled via the variable <code>PROGRAM_NAME_PAGE</code> in the
  156. <code>pprof</code> script, so you can change it if you'd like.</p>
  157. <p>The server should respond by reading the contents of
  158. <code>/proc/self/cmdline</code>, converting all internal NUL (\0)
  159. characters to newlines, and sending the result back as an HTTP
  160. response to <code>pprof</code>.</p>
  161. <p>Here's an example return value:<p>
  162. <pre>
  163. /root/server/custom_webserver
  164. 80
  165. --configfile=/root/server/ws.config
  166. </pre>
  167. <h2> <code><b>/pprof/symbol</b></code> </h2>
  168. <p><code>pprof</code> asks for the url <code>/pprof/symbol</code> to
  169. map from hex addresses to variable names. The actual url is
  170. controlled via the variable <code>SYMBOL_PAGE</code> in the
  171. <code>pprof</code> script, so you can change it if you'd like.</p>
  172. <p>When the server receives a GET request for
  173. <code>/pprof/symbol</code>, it should return a line formatted like
  174. so:</p>
  175. <pre>
  176. num_symbols: ###
  177. </pre>
  178. <p>where <code>###</code> is the number of symbols found in the
  179. binary. (For now, the only important distinction is whether the value
  180. is 0, which it is for executables that lack debug information, or
  181. not-0).</p>
  182. <p>This is perhaps the hardest request to write code for, because in
  183. addition to the GET request for this url, the server must accept POST
  184. requests. This means that after the HTTP headers, pprof will pass in
  185. a list of hex addresses connected by <code>+</code>, like so:</p>
  186. <pre>
  187. curl -d '0x0824d061+0x0824d1cf' http://remote_host:80/pprof/symbol
  188. </pre>
  189. <p>The server should read the POST data, which will be in one line,
  190. and for each hex value, should write one line of output to the output
  191. stream, like so:</p>
  192. <pre>
  193. &lt;hex address&gt;&lt;tab&gt;&lt;function name&gt;
  194. </pre>
  195. <p>For instance:</p>
  196. <pre>
  197. 0x08b2dabd _Update
  198. </pre>
  199. <p>The other reason this is the most difficult request to implement,
  200. is that the application will have to figure out for itself how to map
  201. from address to function name. One possibility is to run <code>nm -C
  202. -n &lt;program name&gt;</code> to get the mappings at
  203. program-compile-time. Another, at least on Linux, is to call out to
  204. addr2line for every <code>pprof/symbol</code> call, for instance
  205. <code>addr2line -Cfse /proc/<getpid>/exe 0x12345678 0x876543210</code>
  206. (presumably with some caching!)</p>
  207. <p><code>pprof</code> itself does just this for local profiles (not
  208. ones that talk to remote servers); look at the subroutine
  209. <code>GetProcedureBoundaries</code>.</p>
  210. <hr>
  211. Last modified: Mon Jun 12 21:30:14 PDT 2006
  212. </body>
  213. </html>