Debian Codegraph @

doc/guide/src/HowProfilingWorks.adoc

raw
1 = HowProfilingWorks
2
3 Here's how <<Profiling#>> works. If profiling is on, the front end
4 (elaborator) inserts `Enter` and `Leave` statements into the source
5 program for function entry and exit. For example,
6 [source,sml]
7 ----
8 fun f n = if n = 0 then 0 else 1 + f (n - 1)
9 ----
10 becomes
11 [source,sml]
12 ----
13 fun f n =
14 let
15 val () = Enter "f"
16 val res = (if n = 0 then 0 else 1 + f (n - 1))
17 handle e => (Leave "f"; raise e)
18 val () = Leave "f"
19 in
20 res
21 end
22 ----
23
24 Actually there is a bit more information than just the source function
25 name; there is also lexical nesting and file position.
26
27 Most of the middle of the compiler ignores, but preserves, `Enter` and
28 `Leave`. However, so that profiling preserves tail calls, the
29 <<Shrink#,SSA shrinker>> has an optimization that notices when the only
30 operations that cause a call to be a nontail call are profiling
31 operations, and if so, moves them before the call, turning it into a
32 tail call. If you observe a program that has a tail call that appears
33 to be turned into a nontail when compiled with profiling, please
34 <<Bug#,report a bug>>.
35
36 There is the `checkProf` function in
37 https://github.com/MLton/mlton/blob/master/mlton/ssa/type-check.fun[`type-check.fun`], which checks that
38 the `Enter`/`Leave` statements match up.
39
40 In the backend, just before translating to the <<Machine#,Machine IL>>,
41 the profiler uses the `Enter`/`Leave` statements to infer the "local"
42 portion of the control stack at each program point. The profiler then
43 removes the ``Enter``s/``Leave``s and inserts different information
44 depending on which kind of profiling is happening. For time
45 profiling, the profiler inserts code that sets a global field that
46 records the local control stack. For allocation profiling, the
47 profiler inserts calls to a C function that will maintain byte counts.
48 With stack profiling, the profiler also inserts a call to a C function
49 at each nontail call in order to maintain information at runtime about
50 what SML functions are on the stack.
51
52 At run time, the profiler associates counters (either clock ticks or
53 byte counts) with source functions. When the program finishes, the
54 profiler writes the counts out to the `mlmon.out` file. Then,
55 `mlprof` uses source information stored in the executable to
56 associate the counts in the `mlmon.out` file with source
57 functions.
58
59 For time profiling, the profiler catches the `SIGPROF` signal 100
60 times per second and increments the appropriate counter, determined by
61 looking at the global field that records the local control stack and
62 mapping that to the current source function.
63
64 == Caveats
65
66 There may be a few missed clock ticks or bytes allocated at the very
67 end of the program after the data is written.
68
69 Profiling has not been tested with signals or threads. In particular,
70 stack profiling may behave strangely.