| 1 |
= MLtonProfile |
| 2 |
|
| 3 |
[source,sml] |
| 4 |
---- |
| 5 |
signature MLTON_PROFILE = |
| 6 |
sig |
| 7 |
structure Data: |
| 8 |
sig |
| 9 |
type t |
| 10 |
|
| 11 |
val equals: t * t -> bool |
| 12 |
val free: t -> unit |
| 13 |
val malloc: unit -> t |
| 14 |
val write: t * string -> unit |
| 15 |
end |
| 16 |
|
| 17 |
val isOn: bool |
| 18 |
val withData: Data.t * (unit -> 'a) -> 'a |
| 19 |
end |
| 20 |
---- |
| 21 |
|
| 22 |
`MLton.Profile` provides <<Profiling#>> control from within the |
| 23 |
program, allowing you to profile individual portions of your |
| 24 |
program. With `MLton.Profile`, you can create many units of profiling |
| 25 |
data (essentially, mappings from functions to counts) during a run of |
| 26 |
a program, switch between them while the program is running, and |
| 27 |
output multiple `mlmon.out` files. |
| 28 |
|
| 29 |
* `isOn` |
| 30 |
+ |
| 31 |
a compile-time constant that is false only when compiling `-profile no`. |
| 32 |
|
| 33 |
* `type Data.t` |
| 34 |
+ |
| 35 |
the type of a unit of profiling data. In order to most efficiently |
| 36 |
execute non-profiled programs, when compiling `-profile no` (the |
| 37 |
default), `Data.t` is equivalent to `unit ref`. |
| 38 |
|
| 39 |
* `Data.equals (x, y)` |
| 40 |
+ |
| 41 |
returns true if the `x` and `y` are the same unit of profiling data. |
| 42 |
|
| 43 |
* `Data.free x` |
| 44 |
+ |
| 45 |
frees the memory associated with the unit of profiling data `x`. It |
| 46 |
is an error to free the current unit of profiling data or to free a |
| 47 |
previously freed unit of profiling data. When compiling |
| 48 |
`-profile no`, `Data.free x` is a no-op. |
| 49 |
|
| 50 |
* `Data.malloc ()` |
| 51 |
+ |
| 52 |
returns a new unit of profiling data. Each unit of profiling data is |
| 53 |
allocated from the process address space (but is _not_ in the MLton |
| 54 |
heap) and consumes memory proportional to the number of source |
| 55 |
functions. When compiling `-profile no`, `Data.malloc ()` is |
| 56 |
equivalent to allocating a new `unit ref`. |
| 57 |
|
| 58 |
* `write (x, f)` |
| 59 |
+ |
| 60 |
writes the accumulated ticks in the unit of profiling data `x` to file |
| 61 |
`f`. It is an error to write a previously freed unit of profiling |
| 62 |
data. When compiling `-profile no`, `write (x, f)` is a no-op. A |
| 63 |
profiled program will always write the current unit of profiling data |
| 64 |
at program exit to a file named `mlmon.out`. |
| 65 |
|
| 66 |
* `withData (d, f)` |
| 67 |
+ |
| 68 |
runs `f` with `d` as the unit of profiling data, and returns the |
| 69 |
result of `f` after restoring the current unit of profiling data. |
| 70 |
When compiling `-profile no`, `withData (d, f)` is equivalent to |
| 71 |
`f ()`. |
| 72 |
|
| 73 |
|
| 74 |
== Example |
| 75 |
|
| 76 |
Here is an example, taken from the `examples/profiling` directory, |
| 77 |
showing how to profile the executions of the `fib` and `tak` functions |
| 78 |
separately. Suppose that `fib-tak.sml` contains the following. |
| 79 |
[source,sml] |
| 80 |
---- |
| 81 |
structure Profile = MLton.Profile |
| 82 |
|
| 83 |
val fibData = Profile.Data.malloc () |
| 84 |
val takData = Profile.Data.malloc () |
| 85 |
|
| 86 |
fun wrap (f, d) x = |
| 87 |
Profile.withData (d, fn () => f x) |
| 88 |
|
| 89 |
val rec fib = |
| 90 |
fn 0 => 0 |
| 91 |
| 1 => 1 |
| 92 |
| n => fib (n - 1) + fib (n - 2) |
| 93 |
val fib = wrap (fib, fibData) |
| 94 |
|
| 95 |
fun tak (x,y,z) = |
| 96 |
if not (y < x) |
| 97 |
then z |
| 98 |
else tak (tak (x - 1, y, z), |
| 99 |
tak (y - 1, z, x), |
| 100 |
tak (z - 1, x, y)) |
| 101 |
val tak = wrap (tak, takData) |
| 102 |
|
| 103 |
val rec f = |
| 104 |
fn 0 => () |
| 105 |
| n => (fib 38; f (n-1)) |
| 106 |
val _ = f 2 |
| 107 |
|
| 108 |
val rec g = |
| 109 |
fn 0 => () |
| 110 |
| n => (tak (18,12,6); g (n-1)) |
| 111 |
val _ = g 500 |
| 112 |
|
| 113 |
fun done (data, file) = |
| 114 |
(Profile.Data.write (data, file) |
| 115 |
; Profile.Data.free data) |
| 116 |
|
| 117 |
val _ = done (fibData, "mlmon.fib.out") |
| 118 |
val _ = done (takData, "mlmon.tak.out") |
| 119 |
---- |
| 120 |
|
| 121 |
Compile and run the program. |
| 122 |
---- |
| 123 |
% mlton -profile time fib-tak.sml |
| 124 |
% ./fib-tak |
| 125 |
---- |
| 126 |
|
| 127 |
Separately display the profiling data for `fib` |
| 128 |
---- |
| 129 |
% mlprof fib-tak mlmon.fib.out |
| 130 |
5.77 seconds of CPU time (0.00 seconds GC) |
| 131 |
function cur |
| 132 |
--------- ----- |
| 133 |
fib 96.9% |
| 134 |
<unknown> 3.1% |
| 135 |
---- |
| 136 |
and for `tak` |
| 137 |
---- |
| 138 |
% mlprof fib-tak mlmon.tak.out |
| 139 |
0.68 seconds of CPU time (0.00 seconds GC) |
| 140 |
function cur |
| 141 |
-------- ------ |
| 142 |
tak 100.0% |
| 143 |
---- |
| 144 |
|
| 145 |
Combine the data for `fib` and `tak` by calling `mlprof` |
| 146 |
with multiple `mlmon.out` files. |
| 147 |
---- |
| 148 |
% mlprof fib-tak mlmon.fib.out mlmon.tak.out mlmon.out |
| 149 |
6.45 seconds of CPU time (0.00 seconds GC) |
| 150 |
function cur |
| 151 |
--------- ----- |
| 152 |
fib 86.7% |
| 153 |
tak 10.5% |
| 154 |
<unknown> 2.8% |
| 155 |
---- |