estp.rst 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ===================================================
  2. Embedded Stack Trace Profiler (ESTP) User Guide
  3. ===================================================
  4. :Author: Andreas Rumpf
  5. :Version: |nimversion|
  6. Nim comes with a platform independent profiler -
  7. the Embedded Stack Trace Profiler (ESTP). The profiler
  8. is *embedded* into your executable. To activate the profiler you need to do:
  9. * compile your program with the ``--profiler:on --stackTrace:on`` command
  10. line options
  11. * import the ``nimprof`` module
  12. * run your program as usual.
  13. You can in fact look at ``nimprof``'s source code to see how to implement
  14. your own profiler.
  15. The setting ``--profiler:on`` defines the conditional symbol ``profiler``.
  16. After your program has finished the profiler will create a
  17. file ``profile_results.txt`` containing the profiling results.
  18. Since the profiler works by examining stack traces, it's essential that
  19. the option ``--stackTrace:on`` is active! Unfortunately this means that a
  20. profiling build is much slower than a release build.
  21. Memory profiler
  22. ===============
  23. You can also use ESTP as a memory profiler to see which stack traces allocate
  24. the most memory and thus create the most GC pressure. It may also help to
  25. find memory leaks. To activate the memory profiler you need to do:
  26. * compile your program with the ``--profiler:off --stackTrace:on -d:memProfiler``
  27. command line options. Yes it's ``--profiler:off``.
  28. * import the ``nimprof`` module
  29. * run your program as usual.
  30. Define the symbol ``ignoreAllocationSize`` so that only the number of
  31. allocations is counted and the sizes of the memory allocations do not matter.
  32. Example results file
  33. ====================
  34. The results file lists stack traces ordered by significance.
  35. The following example file has been generated by profiling the Nim compiler
  36. itself: It shows that in total 5.4% of the runtime has been spent
  37. in ``crcFromRope`` or its children.
  38. In general the stack traces show you immediately where the problem is because
  39. the trace acts like an explanation; in traditional profilers you can only find
  40. expensive leaf functions easily but the *reason* why they are invoked
  41. often remains mysterious.
  42. ::
  43. total executions of each stack trace:
  44. Entry: 0/3391 Calls: 84/4160 = 2.0% [sum: 84; 84/4160 = 2.0%]
  45. newCrcFromRopeAux
  46. crcFromRope
  47. writeRopeIfNotEqual
  48. shouldRecompile
  49. writeModule
  50. myClose
  51. closePasses
  52. processModule
  53. CompileModule
  54. CompileProject
  55. CommandCompileToC
  56. MainCommand
  57. HandleCmdLine
  58. nim
  59. Entry: 1/3391 Calls: 46/4160 = 1.1% [sum: 130; 130/4160 = 3.1%]
  60. updateCrc32
  61. newCrcFromRopeAux
  62. crcFromRope
  63. writeRopeIfNotEqual
  64. shouldRecompile
  65. writeModule
  66. myClose
  67. closePasses
  68. processModule
  69. CompileModule
  70. CompileProject
  71. CommandCompileToC
  72. MainCommand
  73. HandleCmdLine
  74. nim
  75. Entry: 2/3391 Calls: 41/4160 = 0.99% [sum: 171; 171/4160 = 4.1%]
  76. updateCrc32
  77. updateCrc32
  78. newCrcFromRopeAux
  79. crcFromRope
  80. writeRopeIfNotEqual
  81. shouldRecompile
  82. writeModule
  83. myClose
  84. closePasses
  85. processModule
  86. CompileModule
  87. CompileProject
  88. CommandCompileToC
  89. MainCommand
  90. HandleCmdLine
  91. nim
  92. Entry: 3/3391 Calls: 41/4160 = 0.99% [sum: 212; 212/4160 = 5.1%]
  93. crcFromFile
  94. writeRopeIfNotEqual
  95. shouldRecompile
  96. writeModule
  97. myClose
  98. closePasses
  99. processModule
  100. CompileModule
  101. CompileProject
  102. CommandCompileToC
  103. MainCommand
  104. HandleCmdLine
  105. nim
  106. Entry: 4/3391 Calls: 41/4160 = 0.99% [sum: 253; 253/4160 = 6.1%]
  107. updateCrc32
  108. crcFromFile
  109. writeRopeIfNotEqual
  110. shouldRecompile
  111. writeModule
  112. myClose
  113. closePasses
  114. processModule
  115. CompileModule
  116. CompileProject
  117. CommandCompileToC
  118. MainCommand
  119. HandleCmdLine
  120. nim
  121. Entry: 5/3391 Calls: 32/4160 = 0.77% [sum: 285; 285/4160 = 6.9%]
  122. pop
  123. newCrcFromRopeAux
  124. crcFromRope
  125. writeRopeIfNotEqual
  126. shouldRecompile
  127. writeModule
  128. myClose
  129. closePasses
  130. processModule
  131. CompileModule
  132. CompileProject
  133. CommandCompileToC
  134. MainCommand
  135. HandleCmdLine
  136. nim
  137. Entry: 6/3391 Calls: 17/4160 = 0.41% [sum: 302; 302/4160 = 7.3%]
  138. doOperation
  139. forAllChildrenAux
  140. pop
  141. newCrcFromRopeAux
  142. crcFromRope
  143. writeRopeIfNotEqual
  144. shouldRecompile
  145. writeModule
  146. myClose
  147. closePasses
  148. processModule
  149. CompileModule
  150. CompileProject
  151. CommandCompileToC
  152. MainCommand
  153. HandleCmdLine
  154. ...
  155. nim
  156. Entry: 7/3391 Calls: 14/4160 = 0.34% [sum: 316; 316/4160 = 7.6%]
  157. Contains
  158. isAccessible
  159. interiorAllocatedPtr
  160. gcMark
  161. markStackAndRegisters
  162. collectCTBody
  163. collectCT
  164. rawNewObj
  165. newObj
  166. newNode
  167. copyTree
  168. matchesAux
  169. matches
  170. resolveOverloads
  171. semOverloadedCall
  172. semOverloadedCallAnalyseEffects
  173. ...
  174. CommandCompileToC
  175. MainCommand
  176. HandleCmdLine