| 1 |
= CallGraph |
| 2 |
|
| 3 |
For easier visualization of <<Profiling#,profiling>> data, `mlprof` can |
| 4 |
create a call graph of the program in dot format, from which you can |
| 5 |
use the http://www.research.att.com/sw/tools/graphviz/[graphviz] |
| 6 |
software package to create a PostScript or PNG graph. For example, |
| 7 |
---- |
| 8 |
mlprof -call-graph foo.dot foo mlmon.out |
| 9 |
---- |
| 10 |
will create `foo.dot` with a complete call graph. For each source |
| 11 |
function, there will be one node in the graph that contains the |
| 12 |
function name (and source position with `-show-line true`), as |
| 13 |
well as the percentage of ticks. If you want to create a call graph |
| 14 |
for your program without any profiling data, you can simply call |
| 15 |
`mlprof` without any `mlmon.out` files, as in |
| 16 |
---- |
| 17 |
mlprof -call-graph foo.dot foo |
| 18 |
---- |
| 19 |
|
| 20 |
Because SML has higher-order functions, the call graph is is dependent |
| 21 |
on MLton's analysis of which functions call each other. This analysis |
| 22 |
depends on many implementation details and might display spurious |
| 23 |
edges that a human could conclude are impossible. However, in |
| 24 |
practice, the call graphs tend to be very accurate. |
| 25 |
|
| 26 |
Because call graphs can get big, `mlprof` provides the `-keep` option |
| 27 |
to specify the nodes that you would like to see. This option also |
| 28 |
controls which functions appear in the table that `mlprof` prints. |
| 29 |
The argument to `-keep` is an expression describing a set of source |
| 30 |
functions (i.e. graph nodes). The expression _e_ should be of the |
| 31 |
following form. |
| 32 |
|
| 33 |
* ``all`` |
| 34 |
* ``"__s__"`` |
| 35 |
* ``(and __e ...__)`` |
| 36 |
* ``(from __e__)`` |
| 37 |
* ``(not __e__)`` |
| 38 |
* ``(or __e__)`` |
| 39 |
* ``(pred __e__)`` |
| 40 |
* ``(succ __e__)`` |
| 41 |
* ``(thresh __x__)`` |
| 42 |
* ``(thresh-gc __x__)`` |
| 43 |
* ``(thresh-stack __x__)`` |
| 44 |
* ``(to __e__)`` |
| 45 |
|
| 46 |
In the grammar, ``all`` denotes the set of all nodes. ``"__s__"`` is |
| 47 |
a regular expression denoting the set of functions whose name |
| 48 |
(followed by a space and the source position) has a prefix matching |
| 49 |
the regexp. The `and`, `not`, and `or` expressions denote |
| 50 |
intersection, complement, and union, respectively. The `pred` and |
| 51 |
`succ` expressions add the set of immediate predecessors or successors |
| 52 |
to their argument, respectively. The `from` and `to` expressions |
| 53 |
denote the set of nodes that have paths from or to the set of nodes |
| 54 |
denoted by their arguments, respectively. Finally, `thresh`, |
| 55 |
`thresh-gc`, and `thresh-stack` denote the set of nodes whose |
| 56 |
percentage of ticks, gc ticks, or stack ticks, respectively, is |
| 57 |
greater than or equal to the real number _x_. |
| 58 |
|
| 59 |
For example, if you want to see the entire call graph for a program, |
| 60 |
you can use `-keep all` (this is the default). If you want to see |
| 61 |
all nodes reachable from function `foo` in your program, you would |
| 62 |
use `-keep '(from "foo")'`. Or, if you want to see all the |
| 63 |
functions defined in subdirectory `bar` of your project that used |
| 64 |
at least 1% of the ticks, you would use |
| 65 |
---- |
| 66 |
-keep '(and ".*/bar/" (thresh 1.0))' |
| 67 |
---- |
| 68 |
To see all functions with ticks above a threshold, you can also use |
| 69 |
`-thresh x`, which is an abbreviation for `-keep '(thresh x)'`. You |
| 70 |
can not use multiple `-keep` arguments or both `-keep` and `-thresh`. |
| 71 |
When you use `-keep` to display a subset of the functions, `mlprof` |
| 72 |
will add dashed edges to the call graph to indicate a path in the |
| 73 |
original call graph from one function to another. |
| 74 |
|
| 75 |
When compiling with `-profile-stack true`, you can use `mlprof -gray |
| 76 |
true` to make the nodes darker or lighter depending on whether their |
| 77 |
stack percentage is higher or lower. |
| 78 |
|
| 79 |
MLton's optimizer may duplicate source functions for any of a number |
| 80 |
of reasons (functor duplication, monomorphisation, polyvariance, |
| 81 |
inlining). By default, all duplicates of a function are treated as |
| 82 |
one. If you would like to treat the duplicates separately, you can |
| 83 |
use ``mlprof -split __regexp__``, which will cause all duplicates of |
| 84 |
functions whose name has a prefix matching the regular expression to |
| 85 |
be treated separately. This can be especially useful for higher-order |
| 86 |
utility functions like `General.o`. |
| 87 |
|
| 88 |
== Caveats |
| 89 |
|
| 90 |
Technically speaking, `mlprof` produces a call-stack graph rather than |
| 91 |
a call graph, because it describes the set of possible call stacks. |
| 92 |
The difference is in how tail calls are displayed. For example if `f` |
| 93 |
nontail calls `g` and `g` tail calls `h`, then the call-stack graph |
| 94 |
has edges from `f` to `g` and `f` to `h`, while the call graph has |
| 95 |
edges from `f` to `g` and `g` to `h`. That is, a tail call from `g` |
| 96 |
to `h` removes `g` from the call stack and replaces it with `h`. |