ext_profiler.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title>Profiler</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <meta name="Author" content="Mike Pall">
  7. <meta name="Copyright" content="Copyright (C) 2005-2017, Mike Pall">
  8. <meta name="Language" content="en">
  9. <link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
  10. <link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
  11. </head>
  12. <body>
  13. <div id="site">
  14. <a href="http://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
  15. </div>
  16. <div id="head">
  17. <h1>Profiler</h1>
  18. </div>
  19. <div id="nav">
  20. <ul><li>
  21. <a href="luajit.html">LuaJIT</a>
  22. <ul><li>
  23. <a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
  24. </li><li>
  25. <a href="install.html">Installation</a>
  26. </li><li>
  27. <a href="running.html">Running</a>
  28. </li></ul>
  29. </li><li>
  30. <a href="extensions.html">Extensions</a>
  31. <ul><li>
  32. <a href="ext_ffi.html">FFI Library</a>
  33. <ul><li>
  34. <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
  35. </li><li>
  36. <a href="ext_ffi_api.html">ffi.* API</a>
  37. </li><li>
  38. <a href="ext_ffi_semantics.html">FFI Semantics</a>
  39. </li></ul>
  40. </li><li>
  41. <a href="ext_jit.html">jit.* Library</a>
  42. </li><li>
  43. <a href="ext_c_api.html">Lua/C API</a>
  44. </li><li>
  45. <a class="current" href="ext_profiler.html">Profiler</a>
  46. </li></ul>
  47. </li><li>
  48. <a href="status.html">Status</a>
  49. <ul><li>
  50. <a href="changes.html">Changes</a>
  51. </li></ul>
  52. </li><li>
  53. <a href="faq.html">FAQ</a>
  54. </li><li>
  55. <a href="http://luajit.org/performance.html">Performance <span class="ext">&raquo;</span></a>
  56. </li><li>
  57. <a href="http://wiki.luajit.org/">Wiki <span class="ext">&raquo;</span></a>
  58. </li><li>
  59. <a href="http://luajit.org/list.html">Mailing List <span class="ext">&raquo;</span></a>
  60. </li></ul>
  61. </div>
  62. <div id="main">
  63. <p>
  64. LuaJIT has an integrated statistical profiler with very low overhead. It
  65. allows sampling the currently executing stack and other parameters in
  66. regular intervals.
  67. </p>
  68. <p>
  69. The integrated profiler can be accessed from three levels:
  70. </p>
  71. <ul>
  72. <li>The <a href="#hl_profiler">bundled high-level profiler</a>, invoked by the
  73. <a href="#j_p"><tt>-jp</tt></a> command line option.</li>
  74. <li>A <a href="#ll_lua_api">low-level Lua API</a> to control the profiler.</li>
  75. <li>A <a href="#ll_c_api">low-level C API</a> to control the profiler.</li>
  76. </ul>
  77. <h2 id="hl_profiler">High-Level Profiler</h2>
  78. <p>
  79. The bundled high-level profiler offers basic profiling functionality. It
  80. generates simple textual summaries or source code annotations. It can be
  81. accessed with the <a href="#j_p"><tt>-jp</tt></a> command line option
  82. or from Lua code by loading the underlying <tt>jit.p</tt> module.
  83. </p>
  84. <p>
  85. To cut to the chase &mdash; run this to get a CPU usage profile by
  86. function name:
  87. </p>
  88. <pre class="code">
  89. luajit -jp myapp.lua
  90. </pre>
  91. <p>
  92. It's <em>not</em> a stated goal of the bundled profiler to add every
  93. possible option or to cater for special profiling needs. The low-level
  94. profiler APIs are documented below. They may be used by third-party
  95. authors to implement advanced functionality, e.g. IDE integration or
  96. graphical profilers.
  97. </p>
  98. <p>
  99. Note: Sampling works for both interpreted and JIT-compiled code. The
  100. results for JIT-compiled code may sometimes be surprising. LuaJIT
  101. heavily optimizes and inlines Lua code &mdash; there's no simple
  102. one-to-one correspondence between source code lines and the sampled
  103. machine code.
  104. </p>
  105. <h3 id="j_p"><tt>-jp=[options[,output]]</tt></h3>
  106. <p>
  107. The <tt>-jp</tt> command line option starts the high-level profiler.
  108. When the application run by the command line terminates, the profiler
  109. stops and writes the results to <tt>stdout</tt> or to the specified
  110. <tt>output</tt> file.
  111. </p>
  112. <p>
  113. The <tt>options</tt> argument specifies how the profiling is to be
  114. performed:
  115. </p>
  116. <ul>
  117. <li><tt>f</tt> &mdash; Stack dump: function name, otherwise module:line.
  118. This is the default mode.</li>
  119. <li><tt>F</tt> &mdash; Stack dump: ditto, but dump module:name.</li>
  120. <li><tt>l</tt> &mdash; Stack dump: module:line.</li>
  121. <li><tt>&lt;number&gt;</tt> &mdash; stack dump depth (callee &larr;
  122. caller). Default: 1.</li>
  123. <li><tt>-&lt;number&gt;</tt> &mdash; Inverse stack dump depth (caller
  124. &rarr; callee).</li>
  125. <li><tt>s</tt> &mdash; Split stack dump after first stack level. Implies
  126. depth&nbsp;&ge;&nbsp;2 or depth&nbsp;&le;&nbsp;-2.</li>
  127. <li><tt>p</tt> &mdash; Show full path for module names.</li>
  128. <li><tt>v</tt> &mdash; Show VM states.</li>
  129. <li><tt>z</tt> &mdash; Show <a href="#jit_zone">zones</a>.</li>
  130. <li><tt>r</tt> &mdash; Show raw sample counts. Default: show percentages.</li>
  131. <li><tt>a</tt> &mdash; Annotate excerpts from source code files.</li>
  132. <li><tt>A</tt> &mdash; Annotate complete source code files.</li>
  133. <li><tt>G</tt> &mdash; Produce raw output suitable for graphical tools.</li>
  134. <li><tt>m&lt;number&gt;</tt> &mdash; Minimum sample percentage to be shown.
  135. Default: 3%.</li>
  136. <li><tt>i&lt;number&gt;</tt> &mdash; Sampling interval in milliseconds.
  137. Default: 10ms.<br>
  138. Note: The actual sampling precision is OS-dependent.</li>
  139. </ul>
  140. <p>
  141. The default output for <tt>-jp</tt> is a list of the most CPU consuming
  142. spots in the application. Increasing the stack dump depth with (say)
  143. <tt>-jp=2</tt> may help to point out the main callers or callees of
  144. hotspots. But sample aggregation is still flat per unique stack dump.
  145. </p>
  146. <p>
  147. To get a two-level view (split view) of callers/callees, use
  148. <tt>-jp=s</tt> or <tt>-jp=-s</tt>. The percentages shown for the second
  149. level are relative to the first level.
  150. </p>
  151. <p>
  152. To see how much time is spent in each line relative to a function, use
  153. <tt>-jp=fl</tt>.
  154. </p>
  155. <p>
  156. To see how much time is spent in different VM states or
  157. <a href="#jit_zone">zones</a>, use <tt>-jp=v</tt> or <tt>-jp=z</tt>.
  158. </p>
  159. <p>
  160. Combinations of <tt>v/z</tt> with <tt>f/F/l</tt> produce two-level
  161. views, e.g. <tt>-jp=vf</tt> or <tt>-jp=fv</tt>. This shows the time
  162. spent in a VM state or zone vs. hotspots. This can be used to answer
  163. questions like "Which time consuming functions are only interpreted?" or
  164. "What's the garbage collector overhead for a specific function?".
  165. </p>
  166. <p>
  167. Multiple options can be combined &mdash; but not all combinations make
  168. sense, see above. E.g. <tt>-jp=3si4m1</tt> samples three stack levels
  169. deep in 4ms intervals and shows a split view of the CPU consuming
  170. functions and their callers with a 1% threshold.
  171. </p>
  172. <p>
  173. Source code annotations produced by <tt>-jp=a</tt> or <tt>-jp=A</tt> are
  174. always flat and at the line level. Obviously, the source code files need
  175. to be readable by the profiler script.
  176. </p>
  177. <p>
  178. The high-level profiler can also be started and stopped from Lua code with:
  179. </p>
  180. <pre class="code">
  181. require("jit.p").start(options, output)
  182. ...
  183. require("jit.p").stop()
  184. </pre>
  185. <h3 id="jit_zone"><tt>jit.zone</tt> &mdash; Zones</h3>
  186. <p>
  187. Zones can be used to provide information about different parts of an
  188. application to the high-level profiler. E.g. a game could make use of an
  189. <tt>"AI"</tt> zone, a <tt>"PHYS"</tt> zone, etc. Zones are hierarchical,
  190. organized as a stack.
  191. </p>
  192. <p>
  193. The <tt>jit.zone</tt> module needs to be loaded explicitly:
  194. </p>
  195. <pre class="code">
  196. local zone = require("jit.zone")
  197. </pre>
  198. <ul>
  199. <li><tt>zone("name")</tt> pushes a named zone to the zone stack.</li>
  200. <li><tt>zone()</tt> pops the current zone from the zone stack and
  201. returns its name.</li>
  202. <li><tt>zone:get()</tt> returns the current zone name or <tt>nil</tt>.</li>
  203. <li><tt>zone:flush()</tt> flushes the zone stack.</li>
  204. </ul>
  205. <p>
  206. To show the time spent in each zone use <tt>-jp=z</tt>. To show the time
  207. spent relative to hotspots use e.g. <tt>-jp=zf</tt> or <tt>-jp=fz</tt>.
  208. </p>
  209. <h2 id="ll_lua_api">Low-level Lua API</h2>
  210. <p>
  211. The <tt>jit.profile</tt> module gives access to the low-level API of the
  212. profiler from Lua code. This module needs to be loaded explicitly:
  213. <pre class="code">
  214. local profile = require("jit.profile")
  215. </pre>
  216. <p>
  217. This module can be used to implement your own higher-level profiler.
  218. A typical profiling run starts the profiler, captures stack dumps in
  219. the profiler callback, adds them to a hash table to aggregate the number
  220. of samples, stops the profiler and then analyzes all of the captured
  221. stack dumps. Other parameters can be sampled in the profiler callback,
  222. too. But it's important not to spend too much time in the callback,
  223. since this may skew the statistics.
  224. </p>
  225. <h3 id="profile_start"><tt>profile.start(mode, cb)</tt>
  226. &mdash; Start profiler</h3>
  227. <p>
  228. This function starts the profiler. The <tt>mode</tt> argument is a
  229. string holding options:
  230. </p>
  231. <ul>
  232. <li><tt>f</tt> &mdash; Profile with precision down to the function level.</li>
  233. <li><tt>l</tt> &mdash; Profile with precision down to the line level.</li>
  234. <li><tt>i&lt;number&gt;</tt> &mdash; Sampling interval in milliseconds (default
  235. 10ms).</br>
  236. Note: The actual sampling precision is OS-dependent.
  237. </li>
  238. </ul>
  239. <p>
  240. The <tt>cb</tt> argument is a callback function which is called with
  241. three arguments: <tt>(thread, samples, vmstate)</tt>. The callback is
  242. called on a separate coroutine, the <tt>thread</tt> argument is the
  243. state that holds the stack to sample for profiling. Note: do
  244. <em>not</em> modify the stack of that state or call functions on it.
  245. </p>
  246. <p>
  247. <tt>samples</tt> gives the number of accumulated samples since the last
  248. callback (usually 1).
  249. </p>
  250. <p>
  251. <tt>vmstate</tt> holds the VM state at the time the profiling timer
  252. triggered. This may or may not correspond to the state of the VM when
  253. the profiling callback is called. The state is either <tt>'N'</tt>
  254. native (compiled) code, <tt>'I'</tt> interpreted code, <tt>'C'</tt>
  255. C&nbsp;code, <tt>'G'</tt> the garbage collector, or <tt>'J'</tt> the JIT
  256. compiler.
  257. </p>
  258. <h3 id="profile_stop"><tt>profile.stop()</tt>
  259. &mdash; Stop profiler</h3>
  260. <p>
  261. This function stops the profiler.
  262. </p>
  263. <h3 id="profile_dump"><tt>dump = profile.dumpstack([thread,] fmt, depth)</tt>
  264. &mdash; Dump stack </h3>
  265. <p>
  266. This function allows taking stack dumps in an efficient manner. It
  267. returns a string with a stack dump for the <tt>thread</tt> (coroutine),
  268. formatted according to the <tt>fmt</tt> argument:
  269. </p>
  270. <ul>
  271. <li><tt>p</tt> &mdash; Preserve the full path for module names. Otherwise
  272. only the file name is used.</li>
  273. <li><tt>f</tt> &mdash; Dump the function name if it can be derived. Otherwise
  274. use module:line.</li>
  275. <li><tt>F</tt> &mdash; Ditto, but dump module:name.</li>
  276. <li><tt>l</tt> &mdash; Dump module:line.</li>
  277. <li><tt>Z</tt> &mdash; Zap the following characters for the last dumped
  278. frame.</li>
  279. <li>All other characters are added verbatim to the output string.</li>
  280. </ul>
  281. <p>
  282. The <tt>depth</tt> argument gives the number of frames to dump, starting
  283. at the topmost frame of the thread. A negative number dumps the frames in
  284. inverse order.
  285. </p>
  286. <p>
  287. The first example prints a list of the current module names and line
  288. numbers of up to 10 frames in separate lines. The second example prints
  289. semicolon-separated function names for all frames (up to 100) in inverse
  290. order:
  291. </p>
  292. <pre class="code">
  293. print(profile.dumpstack(thread, "l\n", 10))
  294. print(profile.dumpstack(thread, "lZ;", -100))
  295. </pre>
  296. <h2 id="ll_c_api">Low-level C API</h2>
  297. <p>
  298. The profiler can be controlled directly from C&nbsp;code, e.g. for
  299. use by IDEs. The declarations are in <tt>"luajit.h"</tt> (see
  300. <a href="ext_c_api.html">Lua/C API</a> extensions).
  301. </p>
  302. <h3 id="luaJIT_profile_start"><tt>luaJIT_profile_start(L, mode, cb, data)</tt>
  303. &mdash; Start profiler</h3>
  304. <p>
  305. This function starts the profiler. <a href="#profile_start">See
  306. above</a> for a description of the <tt>mode</tt> argument.
  307. </p>
  308. <p>
  309. The <tt>cb</tt> argument is a callback function with the following
  310. declaration:
  311. </p>
  312. <pre class="code">
  313. typedef void (*luaJIT_profile_callback)(void *data, lua_State *L,
  314. int samples, int vmstate);
  315. </pre>
  316. <p>
  317. <tt>data</tt> is available for use by the callback. <tt>L</tt> is the
  318. state that holds the stack to sample for profiling. Note: do
  319. <em>not</em> modify this stack or call functions on this stack &mdash;
  320. use a separate coroutine for this purpose. <a href="#profile_start">See
  321. above</a> for a description of <tt>samples</tt> and <tt>vmstate</tt>.
  322. </p>
  323. <h3 id="luaJIT_profile_stop"><tt>luaJIT_profile_stop(L)</tt>
  324. &mdash; Stop profiler</h3>
  325. <p>
  326. This function stops the profiler.
  327. </p>
  328. <h3 id="luaJIT_profile_dumpstack"><tt>p = luaJIT_profile_dumpstack(L, fmt, depth, len)</tt>
  329. &mdash; Dump stack </h3>
  330. <p>
  331. This function allows taking stack dumps in an efficient manner.
  332. <a href="#profile_dump">See above</a> for a description of <tt>fmt</tt>
  333. and <tt>depth</tt>.
  334. </p>
  335. <p>
  336. This function returns a <tt>const&nbsp;char&nbsp;*</tt> pointing to a
  337. private string buffer of the profiler. The <tt>int&nbsp;*len</tt>
  338. argument returns the length of the output string. The buffer is
  339. overwritten on the next call and deallocated when the profiler stops.
  340. You either need to consume the content immediately or copy it for later
  341. use.
  342. </p>
  343. <br class="flush">
  344. </div>
  345. <div id="foot">
  346. <hr class="hide">
  347. Copyright &copy; 2005-2017 Mike Pall
  348. <span class="noprint">
  349. &middot;
  350. <a href="contact.html">Contact</a>
  351. </span>
  352. </div>
  353. </body>
  354. </html>