6 signature MLTON_PROFILE =
12 val equals: t * t -> bool
15 val write: t * string -> unit
19 val withData: Data.t * (unit -> 'a) -> 'a
23 `MLton.Profile` provides <:Profiling:> control from within the
24 program, allowing you to profile individual portions of your
25 program. With `MLton.Profile`, you can create many units of profiling
26 data (essentially, mappings from functions to counts) during a run of
27 a program, switch between them while the program is running, and
28 output multiple `mlmon.out` files.
32 a compile-time constant that is false only when compiling `-profile no`.
36 the type of a unit of profiling data. In order to most efficiently
37 execute non-profiled programs, when compiling `-profile no` (the
38 default), `Data.t` is equivalent to `unit ref`.
40 * `Data.equals (x, y)`
42 returns true if the `x` and `y` are the same unit of profiling data.
46 frees the memory associated with the unit of profiling data `x`. It
47 is an error to free the current unit of profiling data or to free a
48 previously freed unit of profiling data. When compiling
49 `-profile no`, `Data.free x` is a no-op.
53 returns a new unit of profiling data. Each unit of profiling data is
54 allocated from the process address space (but is _not_ in the MLton
55 heap) and consumes memory proportional to the number of source
56 functions. When compiling `-profile no`, `Data.malloc ()` is
57 equivalent to allocating a new `unit ref`.
61 writes the accumulated ticks in the unit of profiling data `x` to file
62 `f`. It is an error to write a previously freed unit of profiling
63 data. When compiling `-profile no`, `write (x, f)` is a no-op. A
64 profiled program will always write the current unit of profiling data
65 at program exit to a file named `mlmon.out`.
69 runs `f` with `d` as the unit of profiling data, and returns the
70 result of `f` after restoring the current unit of profiling data.
71 When compiling `-profile no`, `withData (d, f)` is equivalent to
77 Here is an example, taken from the `examples/profiling` directory,
78 showing how to profile the executions of the `fib` and `tak` functions
79 separately. Suppose that `fib-tak.sml` contains the following.
82 structure Profile = MLton.Profile
84 val fibData = Profile.Data.malloc ()
85 val takData = Profile.Data.malloc ()
88 Profile.withData (d, fn () => f x)
93 | n => fib (n - 1) + fib (n - 2)
94 val fib = wrap (fib, fibData)
99 else tak (tak (x - 1, y, z),
102 val tak = wrap (tak, takData)
106 | n => (fib 38; f (n-1))
111 | n => (tak (18,12,6); g (n-1))
114 fun done (data, file) =
115 (Profile.Data.write (data, file)
116 ; Profile.Data.free data)
118 val _ = done (fibData, "mlmon.fib.out")
119 val _ = done (takData, "mlmon.tak.out")
122 Compile and run the program.
124 % mlton -profile time fib-tak.sml
128 Separately display the profiling data for `fib`
130 % mlprof fib-tak mlmon.fib.out
131 5.77 seconds of CPU time (0.00 seconds GC)
139 % mlprof fib-tak mlmon.tak.out
140 0.68 seconds of CPU time (0.00 seconds GC)
146 Combine the data for `fib` and `tak` by calling `mlprof`
147 with multiple `mlmon.out` files.
149 % mlprof fib-tak mlmon.fib.out mlmon.tak.out mlmon.out
150 6.45 seconds of CPU time (0.00 seconds GC)