View Source mix profile.cprof (Mix v1.15.0)

Profiles the given file or expression using Erlang's cprof tool.

cprof can be useful when you want to discover the bottlenecks related to function calls.

Before running the code, it invokes the app.start task which compiles and loads your project. After that, the target expression is profiled together with all matching function calls, by setting breakpoints containing counters. These can only be set on BEAM code so BIFs cannot be call count traced.

To profile the code, you can use syntax similar to the mix run task:

$ mix profile.cprof -e Hello.world
$ mix profile.cprof -e "[1, 2, 3] |> Enum.reverse |> Enum.map(&Integer.to_string/1)"
$ mix profile.cprof my_script.exs arg1 arg2 arg3

This task is automatically re-enabled, so you can profile multiple times in the same Mix invocation.

command-line-options

Command line options

  • --matching - only profile calls matching the given Module.function/arity pattern
  • --limit - filters out any results with a call count less than the limit
  • --module - filters out any results not pertaining to the given module
  • --eval, -e - evaluate the given code
  • --require, -r - requires pattern before running the command
  • --parallel, -p - makes all requires parallel
  • --no-compile - does not compile even if files require compilation
  • --no-deps-check - does not check dependencies
  • --no-archives-check - does not check archives
  • --no-halt - does not halt the system after running the command
  • --no-start - does not start applications after compilation
  • --no-elixir-version-check - does not check the Elixir version from mix.exs

profile-output

Profile output

Example output:

                                                                     CNT
Total                                                                 15
Enum                                                                   6  <--
  Enum."-map/2-lists^map/1-0-"/2                                       4
  Enum.reverse/1                                                       1
  Enum.map/2                                                           1
:elixir_compiler                                                       4  <--
  anonymous fn/1 in :elixir_compiler.__FILE__/1                        3
  anonymous fn/0 in :elixir_compiler.__FILE__/1                        1
String.Chars.Integer                                                   3  <--
  String.Chars.Integer.to_string/1                                     3
:erlang                                                                2  <--
  :erlang.trace_pattern/3                                              2
Profile done over 20229 matching functions

The default output contains data gathered from all matching functions. The left column structures each module and its total call count trace is presented on the right. Each module has its count discriminated by function below. The <-- symbol is meant to help visualize where a new module call count begins.

The first row (Total) is the sum of all function calls. In the last row the number of matching functions that were considered for profiling is presented.

When --matching option is specified, call count tracing will be started only for the functions matching the given pattern:

String.Chars.Integer                                                   3  <--
  String.Chars.Integer.to_string/1                                     3
Profile done over 1 matching functions

The pattern can be a module name, such as String to count all calls to that module, a call without arity, such as String.split, to count all calls to that function regardless of arity, or a call with arity, such as String.split/3, to count all calls to that exact module, function and arity.

caveats

Caveats

You should be aware the profiler is stopped as soon as the code has finished running. This may need special attention, when: running asynchronous code as function calls which were called before the profiler stopped will not be counted; running synchronous code as long running computations and a profiler without a proper MFA trace pattern or filter may lead to a result set which is difficult to comprehend.

Other caveats are the impossibility to call count trace BIFs, since breakpoints can only be set on BEAM code; functions calls performed by :cprof are not traced; the maximum size of a call counter is equal to the host machine's word size (for example, 2147483647 in a 32-bit host).

Link to this section Summary

Functions

Allows to programmatically run the cprof profiler on expression in fun.

Link to this section Functions

Link to this function

profile(fun, opts \\ [])

View Source
@spec profile(
  (-> result),
  keyword()
) :: result
when result: any()

Allows to programmatically run the cprof profiler on expression in fun.

Returns the return value of fun.

options

Options

  • :matching - only profile calls matching the given pattern in form of {module, function, arity}, where each element may be replaced by :_ to allow any value
  • :limit - filters out any results with a call count less than the limit
  • :module - filters out any results not pertaining to the given module