This chapter describes how the execution of Objective Caml
programs can be profiled, by recording how many times functions are
called, branches of conditionals are taken, ...
Before profiling an execution, the program must be compiled in
profiling mode, using the ocamlcp front-end to the ocamlc compiler
(see chapter 8). When compiling modules separately,
ocamlcp must be used when compiling the modules (production
of .cmo files), and can also be used (though this is not strictly
necessary) when linking them together.
Note
If a module (.ml file) doesn't have a corresponding
interface (.mli file), then compiling it with ocamlcp will produce
object files (.cmi and .cmo) that are not compatible with the ones
produced by ocamlc, which may lead to problems (if the .cmi or
.cmo is still around) when switching between profiling and
non-profiling compilations. To avoid this problem, you should always
have a .mli file for each .ml file.
Note
To make sure your programs can be compiled in
profiling mode, avoid using any identifier that begins with
__ocaml_prof.
The amount of profiling information can be controlled through the -p
option to ocamlcp, followed by one or several letters indicating which
parts of the program should be profiled:
a
all options
f
function calls : a count point is set at the beginning of
function bodies
i
if ...then ...else ... : count points are set in
both then branch and else branch
l
while, for loops: a count point is set at the beginning of
the loop body
m
match branches: a count point is set at the beginning of the
body of each branch
t
try ...with ... branches: a count point is set at the
beginning of the body of each branch
For instance, compiling with ocamlcp -p film profiles function calls,
if...then...else..., loops and pattern matching.
Calling ocamlcp without the -p option defaults to -p fm, meaning
that only function calls and pattern matching are profiled.
Note: Due to the implementation of streams and stream patterns as
syntactic sugar, it is hard to predict what parts of stream expressions
and patterns will be profiled by a given flag. To profile a program with
streams, we recommend using ocamlcp -p a.