README 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. gperftools
  2. ----------
  3. (originally Google Performance Tools)
  4. The fastest malloc we’ve seen; works particularly well with threads
  5. and STL. Also: thread-friendly heap-checker, heap-profiler, and
  6. cpu-profiler.
  7. OVERVIEW
  8. ---------
  9. gperftools is a collection of a high-performance multi-threaded
  10. malloc() implementation, plus some pretty nifty performance analysis
  11. tools.
  12. gperftools is distributed under the terms of the BSD License. Join our
  13. mailing list at gperftools@googlegroups.com for updates:
  14. https://groups.google.com/forum/#!forum/gperftools
  15. gperftools was original home for pprof program. But do note that
  16. original pprof (which is still included with gperftools) is now
  17. deprecated in favor of golang version at https://github.com/google/pprof
  18. TCMALLOC
  19. --------
  20. Just link in -ltcmalloc or -ltcmalloc_minimal to get the advantages of
  21. tcmalloc -- a replacement for malloc and new. See below for some
  22. environment variables you can use with tcmalloc, as well.
  23. tcmalloc functionality is available on all systems we've tested; see
  24. INSTALL for more details. See README_windows.txt for instructions on
  25. using tcmalloc on Windows.
  26. when compiling. gcc makes some optimizations assuming it is using its
  27. own, built-in malloc; that assumption obviously isn't true with
  28. tcmalloc. In practice, we haven't seen any problems with this, but
  29. the expected risk is highest for users who register their own malloc
  30. hooks with tcmalloc (using gperftools/malloc_hook.h). The risk is
  31. lowest for folks who use tcmalloc_minimal (or, of course, who pass in
  32. the above flags :-) ).
  33. HEAP PROFILER
  34. -------------
  35. See docs/heapprofile.html for information about how to use tcmalloc's
  36. heap profiler and analyze its output.
  37. As a quick-start, do the following after installing this package:
  38. 1) Link your executable with -ltcmalloc
  39. 2) Run your executable with the HEAPPROFILE environment var set:
  40. $ HEAPPROFILE=/tmp/heapprof <path/to/binary> [binary args]
  41. 3) Run pprof to analyze the heap usage
  42. $ pprof <path/to/binary> /tmp/heapprof.0045.heap # run 'ls' to see options
  43. $ pprof --gv <path/to/binary> /tmp/heapprof.0045.heap
  44. You can also use LD_PRELOAD to heap-profile an executable that you
  45. didn't compile.
  46. There are other environment variables, besides HEAPPROFILE, you can
  47. set to adjust the heap-profiler behavior; c.f. "ENVIRONMENT VARIABLES"
  48. below.
  49. The heap profiler is available on all unix-based systems we've tested;
  50. see INSTALL for more details. It is not currently available on Windows.
  51. HEAP CHECKER
  52. ------------
  53. See docs/heap_checker.html for information about how to use tcmalloc's
  54. heap checker.
  55. In order to catch all heap leaks, tcmalloc must be linked *last* into
  56. your executable. The heap checker may mischaracterize some memory
  57. accesses in libraries listed after it on the link line. For instance,
  58. it may report these libraries as leaking memory when they're not.
  59. (See the source code for more details.)
  60. Here's a quick-start for how to use:
  61. As a quick-start, do the following after installing this package:
  62. 1) Link your executable with -ltcmalloc
  63. 2) Run your executable with the HEAPCHECK environment var set:
  64. $ HEAPCHECK=1 <path/to/binary> [binary args]
  65. Other values for HEAPCHECK: normal (equivalent to "1"), strict, draconian
  66. You can also use LD_PRELOAD to heap-check an executable that you
  67. didn't compile.
  68. The heap checker is only available on Linux at this time; see INSTALL
  69. for more details.
  70. CPU PROFILER
  71. ------------
  72. See docs/cpuprofile.html for information about how to use the CPU
  73. profiler and analyze its output.
  74. As a quick-start, do the following after installing this package:
  75. 1) Link your executable with -lprofiler
  76. 2) Run your executable with the CPUPROFILE environment var set:
  77. $ CPUPROFILE=/tmp/prof.out <path/to/binary> [binary args]
  78. 3) Run pprof to analyze the CPU usage
  79. $ pprof <path/to/binary> /tmp/prof.out # -pg-like text output
  80. $ pprof --gv <path/to/binary> /tmp/prof.out # really cool graphical output
  81. There are other environment variables, besides CPUPROFILE, you can set
  82. to adjust the cpu-profiler behavior; cf "ENVIRONMENT VARIABLES" below.
  83. The CPU profiler is available on all unix-based systems we've tested;
  84. see INSTALL for more details. It is not currently available on Windows.
  85. NOTE: CPU profiling doesn't work after fork (unless you immediately
  86. do an exec()-like call afterwards). Furthermore, if you do
  87. fork, and the child calls exit(), it may corrupt the profile
  88. data. You can use _exit() to work around this. We hope to have
  89. a fix for both problems in the next release of perftools
  90. (hopefully perftools 1.2).
  91. EVERYTHING IN ONE
  92. -----------------
  93. If you want the CPU profiler, heap profiler, and heap leak-checker to
  94. all be available for your application, you can do:
  95. gcc -o myapp ... -lprofiler -ltcmalloc
  96. However, if you have a reason to use the static versions of the
  97. library, this two-library linking won't work:
  98. gcc -o myapp ... /usr/lib/libprofiler.a /usr/lib/libtcmalloc.a # errors!
  99. Instead, use the special libtcmalloc_and_profiler library, which we
  100. make for just this purpose:
  101. gcc -o myapp ... /usr/lib/libtcmalloc_and_profiler.a
  102. CONFIGURATION OPTIONS
  103. ---------------------
  104. For advanced users, there are several flags you can pass to
  105. './configure' that tweak tcmalloc performance. (These are in addition
  106. to the environment variables you can set at runtime to affect
  107. tcmalloc, described below.) See the INSTALL file for details.
  108. ENVIRONMENT VARIABLES
  109. ---------------------
  110. The cpu profiler, heap checker, and heap profiler will lie dormant,
  111. using no memory or CPU, until you turn them on. (Thus, there's no
  112. harm in linking -lprofiler into every application, and also -ltcmalloc
  113. assuming you're ok using the non-libc malloc library.)
  114. The easiest way to turn them on is by setting the appropriate
  115. environment variables. We have several variables that let you
  116. enable/disable features as well as tweak parameters.
  117. Here are some of the most important variables:
  118. HEAPPROFILE=<pre> -- turns on heap profiling and dumps data using this prefix
  119. HEAPCHECK=<type> -- turns on heap checking with strictness 'type'
  120. CPUPROFILE=<file> -- turns on cpu profiling and dumps data to this file.
  121. PROFILESELECTED=1 -- if set, cpu-profiler will only profile regions of code
  122. surrounded with ProfilerEnable()/ProfilerDisable().
  123. CPUPROFILE_FREQUENCY=x-- how many interrupts/second the cpu-profiler samples.
  124. PERFTOOLS_VERBOSE=<level> -- the higher level, the more messages malloc emits
  125. MALLOCSTATS=<level> -- prints memory-use stats at program-exit
  126. For a full list of variables, see the documentation pages:
  127. docs/cpuprofile.html
  128. docs/heapprofile.html
  129. docs/heap_checker.html
  130. COMPILING ON NON-LINUX SYSTEMS
  131. ------------------------------
  132. Perftools was developed and tested on x86 Linux systems, and it works
  133. in its full generality only on those systems. However, we've
  134. successfully ported much of the tcmalloc library to FreeBSD, Solaris
  135. x86, and Darwin (Mac OS X) x86 and ppc; and we've ported the basic
  136. functionality in tcmalloc_minimal to Windows. See INSTALL for details.
  137. See README_windows.txt for details on the Windows port.
  138. PERFORMANCE
  139. -----------
  140. If you're interested in some third-party comparisons of tcmalloc to
  141. other malloc libraries, here are a few web pages that have been
  142. brought to our attention. The first discusses the effect of using
  143. various malloc libraries on OpenLDAP. The second compares tcmalloc to
  144. win32's malloc.
  145. http://www.highlandsun.com/hyc/malloc/
  146. http://gaiacrtn.free.fr/articles/win32perftools.html
  147. It's possible to build tcmalloc in a way that trades off faster
  148. performance (particularly for deletes) at the cost of more memory
  149. fragmentation (that is, more unusable memory on your system). See the
  150. INSTALL file for details.
  151. OLD SYSTEM ISSUES
  152. -----------------
  153. When compiling perftools on some old systems, like RedHat 8, you may
  154. get an error like this:
  155. ___tls_get_addr: symbol not found
  156. This means that you have a system where some parts are updated enough
  157. to support Thread Local Storage, but others are not. The perftools
  158. configure script can't always detect this kind of case, leading to
  159. that error. To fix it, just comment out (or delete) the line
  160. #define HAVE_TLS 1
  161. in your config.h file before building.
  162. 64-BIT ISSUES
  163. -------------
  164. There are two issues that can cause program hangs or crashes on x86_64
  165. 64-bit systems, which use the libunwind library to get stack-traces.
  166. Neither issue should affect the core tcmalloc library; they both
  167. affect the perftools tools such as cpu-profiler, heap-checker, and
  168. heap-profiler.
  169. 1) Some libc's -- at least glibc 2.4 on x86_64 -- have a bug where the
  170. libc function dl_iterate_phdr() acquires its locks in the wrong
  171. order. This bug should not affect tcmalloc, but may cause occasional
  172. deadlock with the cpu-profiler, heap-profiler, and heap-checker.
  173. Its likeliness increases the more dlopen() commands an executable has.
  174. Most executables don't have any, though several library routines like
  175. getgrgid() call dlopen() behind the scenes.
  176. 2) On x86-64 64-bit systems, while tcmalloc itself works fine, the
  177. cpu-profiler tool is unreliable: it will sometimes work, but sometimes
  178. cause a segfault. I'll explain the problem first, and then some
  179. workarounds.
  180. Note that this only affects the cpu-profiler, which is a
  181. gperftools feature you must turn on manually by setting the
  182. CPUPROFILE environment variable. If you do not turn on cpu-profiling,
  183. you shouldn't see any crashes due to perftools.
  184. The gory details: The underlying problem is in the backtrace()
  185. function, which is a built-in function in libc.
  186. Backtracing is fairly straightforward in the normal case, but can run
  187. into problems when having to backtrace across a signal frame.
  188. Unfortunately, the cpu-profiler uses signals in order to register a
  189. profiling event, so every backtrace that the profiler does crosses a
  190. signal frame.
  191. In our experience, the only time there is trouble is when the signal
  192. fires in the middle of pthread_mutex_lock. pthread_mutex_lock is
  193. called quite a bit from system libraries, particularly at program
  194. startup and when creating a new thread.
  195. The solution: The dwarf debugging format has support for 'cfi
  196. annotations', which make it easy to recognize a signal frame. Some OS
  197. distributions, such as Fedora and gentoo 2007.0, already have added
  198. cfi annotations to their libc. A future version of libunwind should
  199. recognize these annotations; these systems should not see any
  200. crashes.
  201. Workarounds: If you see problems with crashes when running the
  202. cpu-profiler, consider inserting ProfilerStart()/ProfilerStop() into
  203. your code, rather than setting CPUPROFILE. This will profile only
  204. those sections of the codebase. Though we haven't done much testing,
  205. in theory this should reduce the chance of crashes by limiting the
  206. signal generation to only a small part of the codebase. Ideally, you
  207. would not use ProfilerStart()/ProfilerStop() around code that spawns
  208. new threads, or is otherwise likely to cause a call to
  209. pthread_mutex_lock!
  210. ---
  211. 17 May 2011