benchmark/docs/perf_counters.md
Raghu Raja 398a8ac2e8
[bazel] Build libpfm as a dependency to allow collection of perf counters (#1408)
* Build libpfm as a dependency to allow collection of perf counters

This commit builds libpfm using rules_foreign_cc and lets the default
build of the benchmark library support perf counter collection without
needing additional work from users.

Tested with a custom target:

```
bazel run \
        --override_repository=com_github_google_benchmark=/home/raghu/benchmark \
        -c opt :test-bench -- "--benchmark_perf_counters=INSTRUCTIONS,CYCLES"
Using profile: local

<snip>

----------------------------------------------------------------------
Benchmark            Time             CPU   Iterations UserCounters...
----------------------------------------------------------------------
BM_Test      0.279 ns        0.279 ns   1000000000 CYCLES=1.00888 INSTRUCTIONS=2

```

Signed-off-by: Raghu Raja <raghu@enfabrica.net>

* Adding myself to the CONTRIBUTORS file per CLA guidance

Enfabrica has already signed a corporate CLA.

Signed-off-by: Raghu Raja <raghu@enfabrica.net>

Signed-off-by: Raghu Raja <raghu@enfabrica.net>
2022-10-31 11:03:59 +00:00

1.5 KiB

User-Requested Performance Counters

When running benchmarks, the user may choose to request collection of performance counters. This may be useful in investigation scenarios - narrowing down the cause of a regression; or verifying that the underlying cause of a performance improvement matches expectations.

This feature is available if:

The feature does not require modifying benchmark code. Counter collection is handled at the boundaries where timer collection is also handled.

To opt-in:

  • If using a Bazel build, add --define pfm=1 to your buid flags
  • If using CMake:
    • Install libpfm4-dev, e.g. apt-get install libpfm4-dev.
    • Enable the CMake flag BENCHMARK_ENABLE_LIBPFM in CMakeLists.txt.

To use, pass a comma-separated list of counter names through the --benchmark_perf_counters flag. The names are decoded through libpfm - meaning, they are platform specific, but some (e.g. CYCLES or INSTRUCTIONS) are mapped by libpfm to platform-specifics - see libpfm documentation for more details.

The counter values are reported back through the User Counters mechanism, meaning, they are available in all the formats (e.g. JSON) supported by User Counters.