<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.9">
<title>HowProfilingWorks</title>
<link rel="stylesheet" href="./asciidoc.css" type="text/css">
<link rel="stylesheet" href="./pygments.css" type="text/css">


<script type="text/javascript" src="./asciidoc.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
asciidoc.install();
/*]]>*/
</script>
<link rel="stylesheet" href="./mlton.css" type="text/css">
</head>
<body class="article">
<div id="banner">
<div id="banner-home">
<a href="./Home">MLton 20180207</a>
</div>
</div>
<div id="header">
<h1>HowProfilingWorks</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="paragraph"><p>Here&#8217;s how <a href="Profiling">Profiling</a> works.  If profiling is on, the front end
(elaborator) inserts <span class="monospaced">Enter</span> and <span class="monospaced">Leave</span> statements into the source
program for function entry and exit.  For example,</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">1</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>becomes</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span class="k">fun</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">let</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Enter</span><span class="w"> </span><span class="s">&quot;f&quot;</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">then</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="n">+</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">(</span><span class="n">n</span><span class="w"> </span><span class="n">-</span><span class="w"> </span><span class="mi">1</span><span class="p">))</span><span class="w"></span>
<span class="w">                </span><span class="k">handle</span><span class="w"> </span><span class="n">e</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">Leave</span><span class="w"> </span><span class="s">&quot;f&quot;</span><span class="p">;</span><span class="w"> </span><span class="k">raise</span><span class="w"> </span><span class="n">e</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Leave</span><span class="w"> </span><span class="s">&quot;f&quot;</span><span class="w"></span>
<span class="w">   </span><span class="k">in</span><span class="w"></span>
<span class="w">      </span><span class="n">res</span><span class="w"></span>
<span class="w">   </span><span class="k">end</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Actually there is a bit more information than just the source function
name; there is also lexical nesting and file position.</p></div>
<div class="paragraph"><p>Most of the middle of the compiler ignores, but preserves, <span class="monospaced">Enter</span> and
<span class="monospaced">Leave</span>.  However, so that profiling preserves tail calls, the
<a href="Shrink">SSA shrinker</a> has an optimization that notices when the only
operations that cause a call to be a nontail call are profiling
operations, and if so, moves them before the call, turning it into a
tail call. If you observe a program that has a tail call that appears
to be turned into a nontail when compiled with profiling, please
<a href="Bug">report a bug</a>.</p></div>
<div class="paragraph"><p>There is the <span class="monospaced">checkProf</span> function in
<a href="https://github.com/MLton/mlton/blob/master/mlton/ssa/type-check.fun"><span class="monospaced">type-check.fun</span></a>, which checks that
the <span class="monospaced">Enter</span>/<span class="monospaced">Leave</span> statements match up.</p></div>
<div class="paragraph"><p>In the backend, just before translating to the <a href="Machine"> Machine IL</a>,
the profiler uses the <span class="monospaced">Enter</span>/<span class="monospaced">Leave</span> statements to infer the "local"
portion of the control stack at each program point.  The profiler then
removes the <span class="monospaced">Enter</span>s/<span class="monospaced">Leave</span>s and inserts different information
depending on which kind of profiling is happening.  For time profiling
(with the <a href="AMD64Codegen">AMD64Codegen</a> and <a href="X86Codegen">X86Codegen</a>), the profiler inserts labels that cover the
code (i.e. each statement has a unique label in its basic block that
prefixes it) and associates each label with the local control stack.
For time profiling (with the <a href="CCodegen">CCodegen</a> and <a href="LLVMCodegen">LLVMCodegen</a>), the profiler
inserts code that sets a global field that records the local control
stack.  For allocation profiling, the profiler inserts calls to a C
function that will maintain byte counts.  With stack profiling, the
profiler also inserts a call to a C function at each nontail call in
order to maintain information at runtime about what SML functions are
on the stack.</p></div>
<div class="paragraph"><p>At run time, the profiler associates counters (either clock ticks or
byte counts) with source functions.  When the program finishes, the
profiler writes the counts out to the <span class="monospaced">mlmon.out</span> file.  Then,
<span class="monospaced">mlprof</span> uses source information stored in the executable to
associate the counts in the <span class="monospaced">mlmon.out</span> file with source
functions.</p></div>
<div class="paragraph"><p>For time profiling, the profiler catches the <span class="monospaced">SIGPROF</span> signal 100
times per second and increments the appropriate counter, determined by
looking at the label prefixing the current program counter and mapping
that to the current source function.</p></div>
</div>
</div>
<div class="sect1">
<h2 id="_caveats">Caveats</h2>
<div class="sectionbody">
<div class="paragraph"><p>There may be a few missed clock ticks or bytes allocated at the very
end of the program after the data is written.</p></div>
<div class="paragraph"><p>Profiling has not been tested with signals or threads.  In particular,
stack profiling may behave strangely.</p></div>
</div>
</div>
</div>
<div id="footnotes"><hr></div>
<div id="footer">
<div id="footer-text">
</div>
<div id="footer-badges">
</div>
</div>
</body>
</html>
