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