xref: /aosp_15_r20/external/google-benchmark/docs/perf_counters.md (revision dbb99499c3810fa1611fa2242a2fc446be01a57c)
1<a name="perf-counters" />
2
3# User-Requested Performance Counters
4
5When running benchmarks, the user may choose to request collection of
6performance counters. This may be useful in investigation scenarios - narrowing
7down the cause of a regression; or verifying that the underlying cause of a
8performance improvement matches expectations.
9
10This feature is available if:
11
12* The benchmark is run on an architecture featuring a Performance Monitoring
13  Unit (PMU),
14* The benchmark is compiled with support for collecting counters. Currently,
15  this requires [libpfm](http://perfmon2.sourceforge.net/), which is built as a
16  dependency via Bazel.
17
18The feature does not require modifying benchmark code. Counter collection is
19handled at the boundaries where timer collection is also handled.
20
21To opt-in:
22* If using a Bazel build, add `--define pfm=1` to your build flags
23* If using CMake:
24  * Install `libpfm4-dev`, e.g. `apt-get install libpfm4-dev`.
25  * Enable the CMake flag `BENCHMARK_ENABLE_LIBPFM` in `CMakeLists.txt`.
26
27To use, pass a comma-separated list of counter names through the
28`--benchmark_perf_counters` flag. The names are decoded through libpfm - meaning,
29they are platform specific, but some (e.g. `CYCLES` or `INSTRUCTIONS`) are
30mapped by libpfm to platform-specifics - see libpfm
31[documentation](http://perfmon2.sourceforge.net/docs.html) for more details.
32
33The counter values are reported back through the [User Counters](../README.md#custom-counters)
34mechanism, meaning, they are available in all the formats (e.g. JSON) supported
35by User Counters.
36