<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="generator" content="AsciiDoc 8.6.10">
<title>MLtonProfile</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 20201002</a>
</div>
</div>
<div id="header">
<h1>MLtonProfile</h1>
</div>
<div id="content">
<div id="preamble">
<div class="sectionbody">
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">signature</span><span class="w"> </span><span class="n">MLTON_PROFILE</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">sig</span><span class="w"></span>
<span class="w">      </span><span class="k">structure</span><span class="w"> </span><span class="n">Data</span><span class="p">:</span><span class="w"></span>
<span class="w">         </span><span class="k">sig</span><span class="w"></span>
<span class="w">            </span><span class="k">type</span><span class="w"> </span><span class="n">t</span><span class="w"></span>

<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">equals</span><span class="p">:</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">bool</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">free</span><span class="p">:</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">malloc</span><span class="p">:</span><span class="w"> </span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">t</span><span class="w"></span>
<span class="w">            </span><span class="k">val</span><span class="w"> </span><span class="n">write</span><span class="p">:</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="n">string</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">unit</span><span class="w"></span>
<span class="w">         </span><span class="k">end</span><span class="w"></span>

<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">isOn</span><span class="p">:</span><span class="w"> </span><span class="n">bool</span><span class="w"></span>
<span class="w">      </span><span class="k">val</span><span class="w"> </span><span class="n">withData</span><span class="p">:</span><span class="w"> </span><span class="n">Data</span><span class="p">.</span><span class="n">t</span><span class="w"> </span><span class="n">*</span><span class="w"> </span><span class="p">(</span><span class="n">unit</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</span><span class="p">)</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="n">&#39;a</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><span class="monospaced">MLton.Profile</span> provides <a href="Profiling">Profiling</a> control from within the
program, allowing you to profile individual portions of your
program. With <span class="monospaced">MLton.Profile</span>, you can create many units of profiling
data (essentially, mappings from functions to counts) during a run of
a program, switch between them while the program is running, and
output multiple <span class="monospaced">mlmon.out</span> files.</p></div>
<div class="ulist"><ul>
<li>
<p>
<span class="monospaced">isOn</span>
</p>
<div class="paragraph"><p>a compile-time constant that is false only when compiling <span class="monospaced">-profile no</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">type Data.t</span>
</p>
<div class="paragraph"><p>the type of a unit of profiling data.  In order to most efficiently
execute non-profiled programs, when compiling <span class="monospaced">-profile no</span> (the
default), <span class="monospaced">Data.t</span> is equivalent to <span class="monospaced">unit ref</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">Data.equals (x, y)</span>
</p>
<div class="paragraph"><p>returns true if the <span class="monospaced">x</span> and <span class="monospaced">y</span> are the same unit of profiling data.</p></div>
</li>
<li>
<p>
<span class="monospaced">Data.free x</span>
</p>
<div class="paragraph"><p>frees the memory associated with the unit of profiling data <span class="monospaced">x</span>.  It
is an error to free the current unit of profiling data or to free a
previously freed unit of profiling data.  When compiling
<span class="monospaced">-profile no</span>, <span class="monospaced">Data.free x</span> is a no-op.</p></div>
</li>
<li>
<p>
<span class="monospaced">Data.malloc ()</span>
</p>
<div class="paragraph"><p>returns a new unit of profiling data.  Each unit of profiling data is
allocated from the process address space (but is <em>not</em> in the MLton
heap) and consumes memory proportional to the number of source
functions.  When compiling <span class="monospaced">-profile no</span>, <span class="monospaced">Data.malloc ()</span> is
equivalent to allocating a new <span class="monospaced">unit ref</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">write (x, f)</span>
</p>
<div class="paragraph"><p>writes the accumulated ticks in the unit of profiling data <span class="monospaced">x</span> to file
<span class="monospaced">f</span>.  It is an error to write a previously freed unit of profiling
data.  When compiling <span class="monospaced">-profile no</span>, <span class="monospaced">write (x, f)</span> is a no-op.  A
profiled program will always write the current unit of profiling data
at program exit to a file named <span class="monospaced">mlmon.out</span>.</p></div>
</li>
<li>
<p>
<span class="monospaced">withData (d, f)</span>
</p>
<div class="paragraph"><p>runs <span class="monospaced">f</span> with <span class="monospaced">d</span> as the unit of profiling data, and returns the
result of <span class="monospaced">f</span> after restoring the current unit of profiling data.
When compiling <span class="monospaced">-profile no</span>, <span class="monospaced">withData (d, f)</span> is equivalent to
<span class="monospaced">f ()</span>.</p></div>
</li>
</ul></div>
</div>
</div>
<div class="sect1">
<h2 id="_example">Example</h2>
<div class="sectionbody">
<div class="paragraph"><p>Here is an example, taken from the <span class="monospaced">examples/profiling</span> directory,
showing how to profile the executions of the <span class="monospaced">fib</span> and <span class="monospaced">tak</span> functions
separately.  Suppose that <span class="monospaced">fib-tak.sml</span> contains the following.</p></div>
<div class="listingblock">
<div class="content"><div class="highlight"><pre><span></span><span class="k">structure</span><span class="w"> </span><span class="n">Profile</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">MLton</span><span class="p">.</span><span class="n">Profile</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="n">fibData</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Profile</span><span class="p">.</span><span class="n">Data</span><span class="p">.</span><span class="n">malloc</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">takData</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">Profile</span><span class="p">.</span><span class="n">Data</span><span class="p">.</span><span class="n">malloc</span><span class="w"> </span><span class="p">()</span><span class="w"></span>

<span class="k">fun</span><span class="w"> </span><span class="n">wrap</span><span class="w"> </span><span class="p">(</span><span class="n">f</span><span class="p">,</span><span class="w"> </span><span class="n">d</span><span class="p">)</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="n">Profile</span><span class="p">.</span><span class="n">withData</span><span class="w"> </span><span class="p">(</span><span class="n">d</span><span class="p">,</span><span class="w"> </span><span class="k">fn</span><span class="w"> </span><span class="p">()</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">fib</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="mi">0</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="mi">1</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="n">fib</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="n">+</span><span class="w"> </span><span class="n">fib</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">2</span><span class="p">)</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">fib</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">wrap</span><span class="w"> </span><span class="p">(</span><span class="n">fib</span><span class="p">,</span><span class="w"> </span><span class="n">fibData</span><span class="p">)</span><span class="w"></span>

<span class="k">fun</span><span class="w"> </span><span class="n">tak</span><span class="w"> </span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">if</span><span class="w"> </span><span class="n">not</span><span class="w"> </span><span class="p">(</span><span class="n">y</span><span class="w"> </span><span class="n">&lt;</span><span class="w"> </span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span><span class="k">then</span><span class="w"> </span><span class="n">z</span><span class="w"></span>
<span class="w">   </span><span class="k">else</span><span class="w"> </span><span class="n">tak</span><span class="w"> </span><span class="p">(</span><span class="n">tak</span><span class="w"> </span><span class="p">(</span><span class="n">x</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="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">z</span><span class="p">),</span><span class="w"></span>
<span class="w">             </span><span class="n">tak</span><span class="w"> </span><span class="p">(</span><span class="n">y</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="n">z</span><span class="p">,</span><span class="w"> </span><span class="n">x</span><span class="p">),</span><span class="w"></span>
<span class="w">             </span><span class="n">tak</span><span class="w"> </span><span class="p">(</span><span class="n">z</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="n">x</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">))</span><span class="w"></span>
<span class="k">val</span><span class="w"> </span><span class="n">tak</span><span class="w"> </span><span class="p">=</span><span class="w"> </span><span class="n">wrap</span><span class="w"> </span><span class="p">(</span><span class="n">tak</span><span class="p">,</span><span class="w"> </span><span class="n">takData</span><span class="p">)</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">f</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">fib</span><span class="w"> </span><span class="mi">38</span><span class="p">;</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="mi">1</span><span class="p">))</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">f</span><span class="w"> </span><span class="mi">2</span><span class="w"></span>

<span class="k">val</span><span class="w"> </span><span class="k">rec</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="k">fn</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="w">    </span><span class="p">|</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="p">=&gt;</span><span class="w"> </span><span class="p">(</span><span class="n">tak</span><span class="w"> </span><span class="p">(</span><span class="mi">18</span><span class="p">,</span><span class="mi">12</span><span class="p">,</span><span class="mi">6</span><span class="p">);</span><span class="w"> </span><span class="n">g</span><span class="w"> </span><span class="p">(</span><span class="n">n-</span><span class="mi">1</span><span class="p">))</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">g</span><span class="w"> </span><span class="mi">500</span><span class="w"></span>

<span class="k">fun</span><span class="w"> </span><span class="n">done</span><span class="w"> </span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">)</span><span class="w"> </span><span class="p">=</span><span class="w"></span>
<span class="w">   </span><span class="p">(</span><span class="n">Profile</span><span class="p">.</span><span class="n">Data</span><span class="p">.</span><span class="n">write</span><span class="w"> </span><span class="p">(</span><span class="n">data</span><span class="p">,</span><span class="w"> </span><span class="n">file</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">;</span><span class="w"> </span><span class="n">Profile</span><span class="p">.</span><span class="n">Data</span><span class="p">.</span><span class="n">free</span><span class="w"> </span><span class="n">data</span><span class="p">)</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">done</span><span class="w"> </span><span class="p">(</span><span class="n">fibData</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;mlmon.fib.out&quot;</span><span class="p">)</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">done</span><span class="w"> </span><span class="p">(</span><span class="n">takData</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;mlmon.tak.out&quot;</span><span class="p">)</span><span class="w"></span>
</pre></div></div></div>
<div class="paragraph"><p>Compile and run the program.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlton -profile time fib-tak.sml
% ./fib-tak</pre>
</div></div>
<div class="paragraph"><p>Separately display the profiling data for <span class="monospaced">fib</span></p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlprof fib-tak mlmon.fib.out
5.77 seconds of CPU time (0.00 seconds GC)
function   cur
--------- -----
fib       96.9%
&lt;unknown&gt;  3.1%</pre>
</div></div>
<div class="paragraph"><p>and for <span class="monospaced">tak</span></p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlprof fib-tak mlmon.tak.out
0.68 seconds of CPU time (0.00 seconds GC)
function  cur
-------- ------
tak      100.0%</pre>
</div></div>
<div class="paragraph"><p>Combine the data for <span class="monospaced">fib</span> and <span class="monospaced">tak</span> by calling <span class="monospaced">mlprof</span>
with multiple <span class="monospaced">mlmon.out</span> files.</p></div>
<div class="listingblock">
<div class="content monospaced">
<pre>% mlprof fib-tak mlmon.fib.out mlmon.tak.out mlmon.out
6.45 seconds of CPU time (0.00 seconds GC)
function   cur
--------- -----
fib       86.7%
tak       10.5%
&lt;unknown&gt;  2.8%</pre>
</div></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>
