mono-profilers(1) General Commands Manual mono-profilers(1)

mono-profilers - Mono's bundled profiler modules

mono --profile=log[:option,...] program.exe [args]...

mono --profile=coverage[:option,...] program.exe [args]...

mono --profile=aot[:option,...] program.exe [args]...

Mono ships with a few profiler modules that enable most typical profiling scenarios. This page describes each of them in the sections below.

The log profiler is Mono's general-purpose performance profiler. It can collect a wide variety of data that can be analyzed by tools such as mprof-report(1) or the Xamarin Profiler.

By default, the log profiler writes its output to output.mlpd. Refer to the mono/profiler/log.h file in the Mono source tree for documentation on the log profiler's file format.

A default invocation of the log profiler gathers only basic data: Metadata load and unload events, thread start and stop events, performance counter samples, exception throws, etc. Most users will want to enable some of the heavier features such as GC allocation recording, statistical sampling, heap snapshotting (heapshots), or method entry and exit instrumentation. See the Options sub-section.

Note that, in most realistic scenarios, the log profiler will record a vast amount of data. This can lead to very large log files. (The zip and report options can help deal with this.)

The log profiler supports the following options:

Print usage instructions.
Write log data to file. The optional modifiers are:
+
The program PID is appended to the file name. For example, output=+out.mlpd outputs to out.mlpd.1234 if the PID is 1234.
#
file is parsed as a file descriptor number, which is opened with fdopen(3). This is mainly useful in embedding scenarios.
|
file is treated as a program name. It will be started with popen(3) and the log data will be piped to its standard input.
Generate a report directly instead of writing the log data to a file. If this option is used together with the output option, the report will be written to the specified file instead of the log data.
Compress the output file with gzip(1).
Use port to listen for command server connections. See the Command server sub-section.
Disables pre Mono 5.6 compatibility. In particular, this disables exception events and performance counter sampling by default. It also makes it so that GC move events won't be enabled by default when heapshots are enabled. To use this option, it must be the first option given to the log profiler.
This option will be the default in a future version of the log profiler.
[no]event
Enable or disable gathering data for event, which can be one of:
Exception throw and clause (catch, finally, etc) evaluation events. Enabled by default unless nodefaults is used.
Monitor lock contention, acquisition, and release events.
GC start, progress, stop, and resize events.
GC allocation events.
GC move events.
GC root report events. Generated on every collection if enabled, unless nodefaults is used, in which case, they're only generated on heapshots.
GC handle creation and deletion events.
Object finalization events.
Performance counter sample events. Enabled by default unless nodefaults is used.
JIT code buffer events.
Alias for gc, gcalloc, and gcmove.
Alias for exception, monitor, gc, gcmove, gcroot, gchandle, finalization, and counter.
Enable statistical sampling. The default is to sample at a frequency of 100 Hz, but freq can be used to override this.
By default, sampling uses process time (i.e., the more work a process does, the more samples are collected). The -real variant uses wall clock time instead. Wall clock time is better for programs that are I/O-bound.
Limit the number of reusable sample events to num allocations. A value of zero means no limit. By default, the value of this setting is based on the number of CPU cores. Some tinkering with this setting may be necessary for programs with an unusually high amount of threads.
Enable method entry and exit instrumentation. This is an alternative to statistical sampling when you need more precise information. Note that this mode is extremely heavy and can slow most programs to a crawl.
Limit method entry and exit instrumentation to methods matching the specified call spec. This uses the same syntax as the --trace option for mono(1), so refer to that page for more information.
Limit method entry and exit event collection to a call depth of num.
Limit backtraces in various events (including statistical samples) to num frames.
Enable heap snapshots. mode, if given, can be one of:
Only perform a heapshot when receiving a command via the command server or when triggered through the managed library.
Perform a heapshot on every num collections of the major generation.
Perform a heapshot on a major generation collection if num milliseconds have passed since the last heapshot.
If mode is not given, a heapshot will be performed on every collection of the major generation.
In addition to any other heapshot settings, also perform a heapshot on runtime shutdown.
Print detailed debugging information. Most users should not use this option.

The log profiler features a simple command server that currently is only used to trigger manual heapshots (typcally when using the on-demand mode, but also usable with the other modes). A random port will be used to listen for connections unless the port option is used. To trigger a heapshot, open a TCP connection to the command server and send the C string "heapshot\n".

The command server supports multiple simultaneous connections.

The log profiler comes with a managed library called Mono.Profiler.Log. This library allows easily reading log files in managed code (e.g., C#) as well as interacting with the profiler at run-time.

With the ability to easily read log files, users can write all sorts of interesting analyses that might not be provided by the standard tools (e.g., mprof-report(1)).

The LogProfiler class allows users to reconfigure profiler settings at run-time. For example, certain event types can be toggled on or off, the mode and frequency of heapshots and sampling can be changed, etc. Heapshots can also be triggered manually.

To use this library, simply pass -r:Mono.Profiler.Log when compiling your code.

Collect GC allocation and sampling data for a program, then generate a report:

mono --profile=log:alloc,sample program.exe
mprof-report output.mlpd

Perform a heapshot on every 5th collection and generate a report directly:

mono --profile=log:heapshot=5gc,report program.exe

The code coverage profiler collects information about how often code paths are executed. This is done by instrumenting JIT-compiled code at all sequence points. On program exit, the coverage profiler collects all execution count information and outputs it to an XML file. The main use case for the coverage profiler is unit testing: By running unit test suites with the coverage profiler, it is possible to determine whether the unit tests actually cover all the code that they should.

By default, the coverage profiler writes its output to coverage.xml. Refer to the mono/profiler/coverage.c file in the Mono source tree for documentation on the schema.

Please note that the coverage profiler currently does not support instrumenting AOT-compiled code. When collecting coverage data, one may wish to run Mono with the -O=-aot option to disable loading AOT-compiled code.

The coverage profiler supports the following options:

Print usage instructions.
Write coverage data to file. The optional modifiers are:
+
The program PID is appended to the file name. For example, output=+cov.xml outputs to cov.xml.1234 if the PID is 1234.
#
file is parsed as a file descriptor number, which is opened with fdopen(3). This is mainly useful in embedding scenarios.
|
file is treated as a program name. It will be started with popen(3) and the coverage data will be piped to its standard input.
Supply a coverage filter file. This option can be given multiple times. See the Filter files sub-section.

Filter files can be used to pick and choose which types should be considered for coverage instrumentation. A filter file consists of a series of lines of the form:

+|-[image_name]type_name_prefix

Here, image_name is something like mscorlib. type_name_prefix can be something like System.Int32 for a specific type or System.App to pick all types starting with App in the System namespace.

Lines starting with + indicate that a type should be instrumented for coverage, whereas lines starting with - indicate the opposite. Lines starting with + always override lines starting with - regardless of the order they appear in.

Lines not starting with either character are ignored. This can be used to write comments. For example, this is a valid file:

# Ignore coverage in network-related code, except HTTP client code.
-[MyProgram]MyProgram.Net
+[MyProgram]MyProgram.Net.Http.HttpClient

Coverage data for a program can be collected like this:

mono -O=-aot --profile=coverage:output=cov.xml program.exe

cov.xml will now contain the coverage data.

The AOT profiler will record which generic instantiations a program makes use of and save the information to a specified file. This data can then be used by the AOT compiler to compile those generic instantiations ahead of time to reduce program startup time.

By default, the AOT profiler writes its output to output.aotprofile. Refer to the mono/profiler/aot.h file in the Mono source tree for documentation on the AOT profiler's file format.

The AOT profiler supports the following options:

Profile only NUM seconds of runtime and then write the data.
Print usage instructions.
Write output data to file. The optional modifiers are:
+
The program PID is appended to the file name. For example, output=+out.aotprofile outputs to out.aotprofile.1234 if the PID is 1234.
#
file is parsed as a file descriptor number, which is opened with fdopen(3). This is mainly useful in embedding scenarios.
|
file is treated as a program name. It will be started with popen(3) and the log data will be piped to its standard input.
Print detailed debugging information. Most users should not use this option.

A profile can be collected and used like this:

mono --profile=aot:output=program.aotprofile program.exe
mono --aot=profile=program.aotprofile program.exe
mono program.exe

mono(1), mprof-report(1)