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