Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / ProfilingTheStack.adoc
1 ProfilingTheStack
2 =================
3
4 For all forms of <:Profiling:>, you can gather counts for all
5 functions on the stack, not just the currently executing function. To
6 do so, compile your program with `-profile-stack true`. For example,
7 suppose that `list-rev.sml` contains the following.
8
9 [source,sml]
10 ----
11 sys::[./bin/InclGitFile.py mlton master doc/examples/profiling/list-rev.sml]
12 ----
13
14 Compile with stack profiling and then run the program.
15 ----
16 % mlton -profile alloc -profile-stack true list-rev.sml
17 % ./list-rev
18 ----
19
20 Display the profiling data.
21 ----
22 % mlprof -show-line true list-rev mlmon.out
23 6,030,136 bytes allocated (108,336 bytes by GC)
24 function cur stack GC
25 ----------------------- ----- ----- ----
26 append list-rev.sml: 1 97.6% 97.6% 1.4%
27 <gc> 1.8% 0.0% 1.8%
28 <main> 0.4% 98.2% 1.8%
29 rev list-rev.sml: 6 0.2% 97.6% 1.8%
30 ----
31
32 In the above table, we see that `rev`, defined on line 6 of
33 `list-rev.sml`, is only responsible for 0.2% of the allocation, but is
34 on the stack while 97.6% of the allocation is done by the user program
35 and while 1.8% of the allocation is done by the garbage collector.
36
37 The run-time performance impact of `-profile-stack true` can be
38 noticeable since there is some extra bookkeeping at every nontail call
39 and return.