File: HowProfilingWorks

package info (click to toggle)
mlton 20061107-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,828 kB
  • ctags: 61,118
  • sloc: ansic: 11,446; makefile: 1,339; sh: 1,160; lisp: 900; pascal: 256; asm: 97
file content (108 lines) | stat: -rw-r--r-- 6,367 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="robots" content="index,nofollow">



<title>HowProfilingWorks - MLton Standard ML Compiler (SML Compiler)</title>
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="common.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="screen" href="screen.css">
<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="print" href="print.css">


<link rel="Start" href="Home">


</head>

<body lang="en" dir="ltr">

<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-833377-1";
urchinTracker();
</script>
<table bgcolor = lightblue cellspacing = 0 style = "border: 0px;" width = 100%>
  <tr>
    <td style = "
		border: 0px;
		color: darkblue; 
		font-size: 150%;
		text-align: left;">
      <a class = mltona href="Home">MLton 20061025</a>
    <td style = "
		border: 0px;
		font-size: 150%;
		text-align: center;
		width: 50%;">
      HowProfilingWorks
    <td style = "
		border: 0px;
		text-align: right;">
      <table cellspacing = 0 style = "border: 0px">
        <tr style = "vertical-align: middle;">
      </table>
  <tr style = "background-color: white;">
    <td colspan = 3
	style = "
		border: 0px;
		font-size:70%;
		text-align: right;">
      <a href = "Home">Home</a>
      &nbsp;<a href = "Index">Index</a>
      &nbsp;
</table>
<div id="content" lang="en" dir="ltr">
Here's how <a href="Profiling">Profiling</a> works.  If profiling is on, the front end (elaborator) inserts <tt>Enter</tt> and <tt>Leave</tt> statements into the source program for function entry and exit.   For example, 
<pre class=code>
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">f</FONT></I></B></FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B> <B><FONT COLOR="#A020F0">if</FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B> 0 <B><FONT COLOR="#A020F0">then</FONT></B> 0 <B><FONT COLOR="#A020F0">else</FONT></B> 1 <B><FONT COLOR="#5F9EA0">+</FONT></B> f (n <B><FONT COLOR="#5F9EA0">-</FONT></B> 1)
</PRE>
<p>
 becomes 
<pre class=code>
<B><FONT COLOR="#A020F0">fun </FONT></B><B><FONT COLOR="#0000FF"><B><I><FONT COLOR="#000000">f</FONT></I></B></FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B> 
   <B><FONT COLOR="#A020F0">let</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B> Enter <B><FONT COLOR="#BC8F8F">&quot;f&quot;</FONT></B>
      <B><FONT COLOR="#A020F0">val</FONT></B> res <B><FONT COLOR="#5F9EA0">=</FONT></B> (<B><FONT COLOR="#A020F0">if</FONT></B> n <B><FONT COLOR="#5F9EA0">=</FONT></B> 0 <B><FONT COLOR="#A020F0">then</FONT></B> 0 <B><FONT COLOR="#A020F0">else</FONT></B> 1 <B><FONT COLOR="#5F9EA0">+</FONT></B> f (n <B><FONT COLOR="#5F9EA0">-</FONT></B> 1))
                <B><FONT COLOR="#A020F0">handle</FONT></B> e <B><FONT COLOR="#5F9EA0">=</FONT></B><B><FONT COLOR="#5F9EA0">&gt;</FONT></B> (Leave <B><FONT COLOR="#BC8F8F">&quot;f&quot;</FONT></B>; <B><FONT COLOR="#A020F0">raise</FONT></B> e)
      <B><FONT COLOR="#A020F0">val</FONT></B> () <B><FONT COLOR="#5F9EA0">=</FONT></B> Leave <B><FONT COLOR="#BC8F8F">&quot;f&quot;</FONT></B>
   <B><FONT COLOR="#A020F0">in</FONT></B> 
      res
   <B><FONT COLOR="#A020F0">end</FONT></B>
</PRE>
 Actually there is a bit more information than just the source function name; there is also lexical nesting and file position.  
</p>
<p>
Most of the middle of the compiler ignores, but preserves, <tt>Enter</tt> and <tt>Leave</tt>.  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>
<p>
There is the <tt>checkProf</tt> function in 
<a href = "http://mlton.org/cgi-bin/viewsvn.cgi/mlton/tags/on-20061025-release/mlton/ssa/type-check.fun?view=markup"><img src="moin-www.png" alt="[WWW]" height="11" width="11">type-check.fun</a>
, which checks that the <tt>Enter</tt>/<tt>Leave</tt> statements match up. 
</p>
<p>
In the backend, just before translating to the <a href="Machine">Machine</a> IL, the profiler uses the <tt>Enter</tt>/<tt>Leave</tt> statements to infer the "local" portion of the control stack at each program point.  The profiler then removes the <tt>Enter</tt>s/<tt>Leave</tt>s and inserts different information depending on which kind of profiling is happening.  For time profiling, 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 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>
<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 <tt>mlmon.out</tt> file.  Then, <tt>mlprof</tt> uses source information stored in the executable to associate the counts in the <tt>mlmon.out</tt> file with source functions. 
</p>
<p>
For time profiling, the profiler catches the <tt>SIGPROF</tt> 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>
<h2 id="head-bcaa33a7ae44bd5042c37a9cdbea7f843b1cf7c8">Caveats</h2>
<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>
<p>
Profiling has not been tested with signals or threads.  In particular, stack profiling may behave strangely. 
</p>
</div>



<p>
<hr>
Last edited on 2005-12-01 04:35:20 by <span title="ppp-71-139-183-221.dsl.snfc21.pacbell.net"><a href="StephenWeeks">StephenWeeks</a></span>.
</body></html>