Previous Next Contents

Profiling (ocamlprof)

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, ...

Compiling for profiling

Before profiling an execution, the program must be compiled in profiling mode, using the ocamlcp front-end to the ocamlc compiler (see chapter 7). 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.

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 -pfilm 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.

Profiling an execution

Running a bytecode executable file that has been compiled with ocamlcp records the execution counts for the specified parts of the program and saves them in a file called ocamlprof.dump in the current directory.

The ocamlprof.dump file is written only if the program terminates normally (by calling exit or by falling through). It is not written if the program terminates with an uncaught exception.

If a compatible dump file already exists in the current directory, then the profiling information is accumulated in this dump file. This allows, for instance, the profiling of several executions of a program on different inputs.

Printing profiling information

The ocamlprof command produces a source listing of the program modules where execution counts have been inserted as comments. For instance,

        ocamlprof foo.ml
prints the source code for the foo module, with comments indicating how many times the functions in this module have been called. Naturally, this information is accurate only if the source file has not been modified since the profiling execution took place.

The following options are recognized by ocamlprof:

-f dumpfile
Specifies an alternate dump file of profiling information
-F string
Specifies an additional string to be output with profiling information. By default, ocamlprof will annotate programs with comments of the form (* n *) where n is the counter value for a profiling point. With option -F s, the annotation will be (* s n *).

Time profiling

Profiling with ocamlprof only records execution counts, not the actual time spent into each function. There is currently no way to perform time profiling on bytecode programs generated by ocamlc. On native-code programs generated by ocamlopt, the standard Unix profiler prof can be used; just add the -ccopt -p option when linking the program:

        ocamlopt -o myprog -ccopt -p other-options files
        ./myprog
        prof myprog
Function names in the output of prof have the following format:
        Module-name_function-name_unique-number

Previous Next Contents