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 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:
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.
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.
The ocamlprof command produces a source listing of the program modules where execution counts have been inserted as comments. For instance,
ocamlprof foo.mlprints 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:
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 myprogFunction names in the output of prof have the following format: