/Erlang 21

2 cprof - The Call Count Profiler

cprof is a profiling tool that can be used to get a picture of how often different functions in the system are called.

cprof uses breakpoints similar to local call trace, but containing counters, to collect profiling data. Therfore there is no need for special compilation of any module to be profiled.

cprof presents all profiled modules in decreasing total call count order, and for each module presents all profiled functions also in decreasing call count order. A call count limit can be specified to filter out all functions below the limit.

Profiling is done in the following steps:

Starts profiling with zeroed call counters for specified functions by setting call count breakpoints on them.
Runs the code to be profiled.
Pauses the call counters for specified functions. This minimises the impact of code running in the background or in the shell that disturbs the profiling. Call counters are automatically paused when they "hit the ceiling" of the host machine word size. For a 32 bit host the maximum counter value is 2147483647.
Collects call counters and computes the result.
Restarts the call counters from zero for specified functions. Can be used to collect a new set of counters without having to stop and start call count profiling.
Stops profiling by removing call count breakpoints from specified functions.

Functions can be specified as either all in the system, all in one module, all arities of one function, one function, or all functions in all modules not yet loaded. As for now, BIFs cannot be call count traced.

The analysis result can either be for all modules, or for one module. In either case a call count limit can be given to filter out the functions with a call count below the limit. The all modules analysis does not contain the module cprof itself, it can only be analysed by specifying it as a single module to analyse.

Call count tracing is very lightweight compared to other forms of tracing since no trace message has to be generated. Some measurements indicates performance degradations in the vicinity of 10 percent.

The following sections show some examples of profiling with cprof. See also cprof(3).

2.1 Example: Background work

From the Erlang shell:

1> cprof:start(), cprof:pause(). % Stop counters just after start
2> cprof:analyse().
3> cprof:analyse(cprof).
4> cprof:stop().

The example showed the background work that the shell performs just to interpret the first command line. Most work is done by erl_eval and orddict.

What is captured in this example is the part of the work the shell does while interpreting the command line that occurs between the actual calls to cprof:start() and cprof:analyse().

2.2 Example: One module

From the Erlang shell:

1> cprof:start(),R=calendar:day_of_the_week(1896,4,27),cprof:pause(),R.
2> cprof:analyse(calendar).
3> cprof:stop().

The example tells us that "Aktiebolaget LM Ericsson & Co" was registered on a Monday (since the return value of the first command is 1), and that the calendar module needed 9 function calls to calculate that.

Using cprof:analyse() in this example also shows approximately the same background work as in the first example.

2.3 Example: In the code

Write a module:

do(N) ->
    do(N, []).
do(0, L) ->
    R = lists:sort(L),
do(N, L) ->
    do(N-1, [random:uniform(256)-1 | L]).

From the Erlang shell:

1> c(sort).
2> l(random).
3> sort:do(1000).
4> cprof:analyse().
5> cprof:stop().

The example shows some details of how lists:sort/1 works. It used 6047 function calls in the module lists_sort to complete the work.

This time, since the shell was not involved, no other work was done in the system during the profiling. If you retry the same example with a freshly started Erlang emulator, but omit the command l(random), the analysis will show a lot more function calls done by code_server and others to automatically load the module random.

© 2010–2017 Ericsson AB
Licensed under the Apache License, Version 2.0.