estp.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <!-- This file is generated by Nim. -->
  4. <html xmlns="https://www.w3.org/1999/xhtml" xml:lang="en" lang="en" data-theme="auto">
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Embedded Stack Trace Profiler (ESTP) User Guide</title>
  9. <!-- Google fonts -->
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,600,900' rel='stylesheet' type='text/css'/>
  11. <link href='https://fonts.googleapis.com/css?family=Source+Code+Pro:400,500,600' rel='stylesheet' type='text/css'/>
  12. <!-- Favicon -->
  13. <link rel="shortcut icon" href="data:image/x-icon;base64,AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAUAAAAF////AP///wD///8A////AP///wD///8A////AP///wD///8A////AAAAAAIAAABbAAAAlQAAAKIAAACbAAAAmwAAAKIAAACVAAAAWwAAAAL///8A////AP///wD///8A////AAAAABQAAADAAAAAYwAAAA3///8A////AP///wD///8AAAAADQAAAGMAAADAAAAAFP///wD///8A////AP///wAAAACdAAAAOv///wD///8A////AP///wD///8A////AP///wD///8AAAAAOgAAAJ3///8A////AP///wAAAAAnAAAAcP///wAAAAAoAAAASv///wD///8A////AP///wAAAABKAAAAKP///wAAAABwAAAAJ////wD///8AAAAAgQAAABwAAACIAAAAkAAAAJMAAACtAAAAFQAAABUAAACtAAAAkwAAAJAAAACIAAAAHAAAAIH///8A////AAAAAKQAAACrAAAAaP///wD///8AAAAARQAAANIAAADSAAAARf///wD///8AAAAAaAAAAKsAAACk////AAAAADMAAACcAAAAnQAAABj///8A////AP///wAAAAAYAAAAGP///wD///8A////AAAAABgAAACdAAAAnAAAADMAAAB1AAAAwwAAAP8AAADpAAAAsQAAAE4AAAAb////AP///wAAAAAbAAAATgAAALEAAADpAAAA/wAAAMMAAAB1AAAAtwAAAOkAAAD/AAAA/wAAAP8AAADvAAAA3gAAAN4AAADeAAAA3gAAAO8AAAD/AAAA/wAAAP8AAADpAAAAtwAAAGUAAAA/AAAA3wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAAD/AAAA/wAAAP8AAADfAAAAPwAAAGX///8A////AAAAAEgAAADtAAAAvwAAAL0AAADGAAAA7wAAAO8AAADGAAAAvQAAAL8AAADtAAAASP///wD///8A////AP///wD///8AAAAAO////wD///8A////AAAAAIcAAACH////AP///wD///8AAAAAO////wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A////AP///wD///8A//8AAP//AAD4HwAA7/cAAN/7AAD//wAAoYUAAJ55AACf+QAAh+EAAAAAAADAAwAA4AcAAP5/AAD//wAA//8AAA=="/>
  14. <link rel="icon" type="image/png" sizes="32x32" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA3XAAAN1wFCKJt4AAAAB3RJTUUH4QQQEwksSS9ZWwAAAk1JREFUWMPtll2ITVEUx39nn/O7Y5qR8f05wtCUUr6ZIS++8pEnkZInPImneaCQ5METNdOkeFBKUhMPRIkHKfEuUZSUlGlKPN2TrgfncpvmnntnmlEyq1Z7t89/rf9a6+y99oZxGZf/XeIq61EdtgKXgdXA0xrYAvBjOIF1AI9zvjcC74BSpndrJPkBWDScTF8Aa4E3wDlgHbASaANmVqlcCnwHvgDvgVfAJ+AikAAvgfVZwLnSVZHZaOuKoQi3ZOMi4NkYkpe1p4J7A8BpYAD49hfIy/oqG0+hLomiKP2L5L+1ubn5115S+3OAn4EnwBlgMzCjyt6ZAnQCJ4A7wOs88iRJHvw50HoujuPBoCKwHWiosy8MdfZnAdcHk8dxXFJ3VQbQlCTJvRBCGdRbD4M6uc5glpY3eAihpN5S5w12diSEcCCEcKUO4ljdr15T76ur1FDDLIQQ3qv71EdDOe3Kxj3leRXyk+pxdWnFWod6Wt2bY3de3aSuUHcPBVimHs7mK9WrmeOF6lR1o9qnzskh2ar2qm1qizpfXaPeVGdlmGN5pb09qMxz1Xb1kLqgzn1RyH7JUXW52lr5e/Kqi9qpto7V1atuUzfnARrV7jEib1T76gG2qxdGmXyiekkt1GswPTtek0aBfJp6YySGBfWg2tPQ0FAYgf1stUfdmdcjarbYJEniKIq6gY/Aw+zWHAC+p2labGpqiorFYgGYCEzN7oQdQClN07O1/EfDyGgC0ALMBdYAi4FyK+4H3gLPsxfR1zRNi+NP7nH5J+QntnXe5B5mpfQAAAAASUVORK5CYII=">
  15. <!-- CSS -->
  16. <link rel="stylesheet" type="text/css" href="nimdoc.out.css?v=2.3.1">
  17. <!-- JS -->
  18. <script type="text/javascript" src="dochack.js?v=2.3.1"></script>
  19. </head>
  20. <body>
  21. <div class="document" id="documentId">
  22. <div class="container">
  23. <h1 class="title">Embedded Stack Trace Profiler (ESTP) User Guide</h1>
  24. <table class="docinfo" frame="void" rules="none"><col class="docinfo-name" /><col class="docinfo-content" /><tbody valign="top"><tr><th class="docinfo-name">Author:</th><td>Andreas Rumpf</td></tr>
  25. <tr><th class="docinfo-name">Version:</th><td>2.3.1</td></tr>
  26. </tbody></table><p>Nim comes with a platform independent profiler - the Embedded Stack Trace Profiler (ESTP). The profiler is <em>embedded</em> into your executable. To activate the profiler you need to do:</p>
  27. <ul class="simple"><li>compile your program with the <tt class="docutils literal"><span class="pre option">--profiler:on --stackTrace:on</span></tt> command line options</li>
  28. <li>import the <tt class="docutils literal"><span class="pre"><span class="Identifier">nimprof</span></span></tt> module</li>
  29. <li>run your program as usual.</li>
  30. </ul>
  31. <p>You can in fact look at <tt class="docutils literal"><span class="pre"><span class="Identifier">nimprof</span></span></tt>'s source code to see how to implement your own profiler.</p>
  32. <p>The setting <tt class="docutils literal"><span class="pre option">--profiler:on</span></tt> defines the conditional symbol <tt class="docutils literal"><span class="pre"><span class="Identifier">profiler</span></span></tt>. You can use <tt class="docutils literal"><span class="pre"><span class="Keyword">when</span> <span class="Identifier">compileOption</span><span class="Punctuation">(</span><span class="StringLit">&quot;profiler&quot;</span><span class="Punctuation">)</span></span></tt> to make the switch seamless. If <tt class="docutils literal"><span class="pre option">profiler</span></tt> is <tt class="docutils literal"><span class="pre option">off</span></tt>, your program runs normally. Otherwise your program is profiled.</p>
  33. <p><pre class="listing"><span class="Keyword">when</span> <span class="Identifier">compileOption</span><span class="Punctuation">(</span><span class="StringLit">&quot;profiler&quot;</span><span class="Punctuation">)</span><span class="Punctuation">:</span>
  34. <span class="Keyword">import</span> <span class="Identifier">std</span><span class="Operator">/</span><span class="Identifier">nimprof</span></pre></p>
  35. <p>After your program has finished the profiler will create a file <tt class="docutils literal"><span class="pre">profile_results.txt</span></tt> containing the profiling results.</p>
  36. <p>Since the profiler works by examining stack traces, it's essential that the option <tt class="docutils literal"><span class="pre option">--stackTrace:on</span></tt> is active! Unfortunately this means that a profiling build is much slower than a release build.</p>
  37. <h1 id="memory-profiler">Memory profiler</h1><p>You can also use ESTP as a memory profiler to see which stack traces allocate the most memory and thus create the most GC pressure. It may also help to find memory leaks. To activate the memory profiler you need to do:</p>
  38. <ul class="simple"><li>compile your program with the <tt class="docutils literal"><span class="pre option">--profiler:off --stackTrace:on -d:memProfiler</span></tt> command line options. Yes it's <tt class="docutils literal"><span class="pre option">--profiler:off</span></tt>.</li>
  39. <li>import the <tt class="docutils literal"><span class="pre"><span class="Identifier">nimprof</span></span></tt> module</li>
  40. <li>run your program as usual.</li>
  41. </ul>
  42. <p>Define the symbol <tt class="docutils literal"><span class="pre"><span class="Identifier">ignoreAllocationSize</span></span></tt> so that only the number of allocations is counted and the sizes of the memory allocations do not matter.</p>
  43. <h1 id="example-results-file">Example results file</h1><p>The results file lists stack traces ordered by significance.</p>
  44. <p>The following example file has been generated by profiling the Nim compiler itself: It shows that in total 5.4% of the runtime has been spent in <tt class="docutils literal"><span class="pre"><span class="Identifier">crcFromRope</span></span></tt> or its children.</p>
  45. <p>In general the stack traces show you immediately where the problem is because the trace acts like an explanation; in traditional profilers you can only find expensive leaf functions easily but the <em>reason</em> why they are invoked often remains mysterious.</p>
  46. <pre>total executions of each stack trace:
  47. Entry: 0/3391 Calls: 84/4160 = 2.0% [sum: 84; 84/4160 = 2.0%]
  48. newCrcFromRopeAux
  49. crcFromRope
  50. writeRopeIfNotEqual
  51. shouldRecompile
  52. writeModule
  53. myClose
  54. closePasses
  55. processModule
  56. CompileModule
  57. CompileProject
  58. CommandCompileToC
  59. MainCommand
  60. HandleCmdLine
  61. nim
  62. Entry: 1/3391 Calls: 46/4160 = 1.1% [sum: 130; 130/4160 = 3.1%]
  63. updateCrc32
  64. newCrcFromRopeAux
  65. crcFromRope
  66. writeRopeIfNotEqual
  67. shouldRecompile
  68. writeModule
  69. myClose
  70. closePasses
  71. processModule
  72. CompileModule
  73. CompileProject
  74. CommandCompileToC
  75. MainCommand
  76. HandleCmdLine
  77. nim
  78. Entry: 2/3391 Calls: 41/4160 = 0.99% [sum: 171; 171/4160 = 4.1%]
  79. updateCrc32
  80. updateCrc32
  81. newCrcFromRopeAux
  82. crcFromRope
  83. writeRopeIfNotEqual
  84. shouldRecompile
  85. writeModule
  86. myClose
  87. closePasses
  88. processModule
  89. CompileModule
  90. CompileProject
  91. CommandCompileToC
  92. MainCommand
  93. HandleCmdLine
  94. nim
  95. Entry: 3/3391 Calls: 41/4160 = 0.99% [sum: 212; 212/4160 = 5.1%]
  96. crcFromFile
  97. writeRopeIfNotEqual
  98. shouldRecompile
  99. writeModule
  100. myClose
  101. closePasses
  102. processModule
  103. CompileModule
  104. CompileProject
  105. CommandCompileToC
  106. MainCommand
  107. HandleCmdLine
  108. nim
  109. Entry: 4/3391 Calls: 41/4160 = 0.99% [sum: 253; 253/4160 = 6.1%]
  110. updateCrc32
  111. crcFromFile
  112. writeRopeIfNotEqual
  113. shouldRecompile
  114. writeModule
  115. myClose
  116. closePasses
  117. processModule
  118. CompileModule
  119. CompileProject
  120. CommandCompileToC
  121. MainCommand
  122. HandleCmdLine
  123. nim
  124. Entry: 5/3391 Calls: 32/4160 = 0.77% [sum: 285; 285/4160 = 6.9%]
  125. pop
  126. newCrcFromRopeAux
  127. crcFromRope
  128. writeRopeIfNotEqual
  129. shouldRecompile
  130. writeModule
  131. myClose
  132. closePasses
  133. processModule
  134. CompileModule
  135. CompileProject
  136. CommandCompileToC
  137. MainCommand
  138. HandleCmdLine
  139. nim
  140. Entry: 6/3391 Calls: 17/4160 = 0.41% [sum: 302; 302/4160 = 7.3%]
  141. doOperation
  142. forAllChildrenAux
  143. pop
  144. newCrcFromRopeAux
  145. crcFromRope
  146. writeRopeIfNotEqual
  147. shouldRecompile
  148. writeModule
  149. myClose
  150. closePasses
  151. processModule
  152. CompileModule
  153. CompileProject
  154. CommandCompileToC
  155. MainCommand
  156. HandleCmdLine
  157. ...
  158. nim
  159. Entry: 7/3391 Calls: 14/4160 = 0.34% [sum: 316; 316/4160 = 7.6%]
  160. Contains
  161. isAccessible
  162. interiorAllocatedPtr
  163. gcMark
  164. markStackAndRegisters
  165. collectCTBody
  166. collectCT
  167. rawNewObj
  168. newObj
  169. newNode
  170. copyTree
  171. matchesAux
  172. matches
  173. resolveOverloads
  174. semOverloadedCall
  175. semOverloadedCallAnalyseEffects
  176. ...
  177. CommandCompileToC
  178. MainCommand
  179. HandleCmdLine</pre>
  180. <div class="twelve-columns footer">
  181. <span class="nim-sprite"></span>
  182. <br>
  183. <small style="color: var(--hint);">Made with Nim. Generated: 2025-03-15 14:12:49 UTC</small>
  184. </div>
  185. </div>
  186. </div>
  187. <script defer data-domain="nim-lang.org" src="https://plausible.io/js/plausible.js"></script>
  188. </body>
  189. </html>