google/benchmark

[BUG] --benchmark_perf_counters only counts user events

Open

#1,601 创建于 2023年5月19日

在 GitHub 查看
 (0 评论) (1 反应) (0 负责人)C++ (7,968 star) (1,539 fork)batch import
bughelp wanted

描述

Describe the bug Perf counter events specified using --benchmark_perf_counters only count user events. Event modifiers, like "event:k" to count kernel-only events, or without a modifier, like "event" to count user+kernel, only count user events, i.e., as if it's always "event:u".

System Which OS, compiler, and compiler version are you using:

  • OS: Ubuntu 22.04.2 LTS
  • Compiler and version: gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)

To reproduce Steps to reproduce the behavior:

  1. Run a benchmark with --benchmark_perf_counters=INSTRUCTIONS:u,INSTRUCTIONS:k,INSTRUCTIONS
  2. See that the event counts are the same for all three, even though the event modifiers should have it count only user events, kernel events, or total events, respectively, and should most likely be different. At the very least, INSTRUCTIONS:u + INSTRUCTIONS:k should total to INSTRUCTIONS.

Expected behavior The :u modifier should count only user events. The :k modifier should count only kernel events. The event without a specified modifier should count both user and kernel events.

Additional context The reason is that PerfCounters::Create() overrides the perf_event_attr attr to exclude kernel and hv events and thus only counts user events.

https://github.com/google/benchmark/blob/v1.8.0/src/perf_counters.cc#L141

    attr.exclude_kernel = true;
    attr.exclude_user = false;
    attr.exclude_hv = true;

If these three lines are commented out, event modifiers appear to work as expected.

However, giving just the event name without any modifiers still counts only user events because the default mode is user mode only.

https://github.com/google/benchmark/blob/v1.8.0/src/perf_counters.cc#L85 const int kCounterMode = PFM_PLM3; // user mode only

Nonetheless, if both user and kernel modifiers are given, i.e. "INSTRUCTIONS:k:u", then both will be counted.

贡献者指南