Skip to content

Skip perf counter tests when counters are unavailable - #2204

Merged
LebedevRI merged 10 commits into
google:mainfrom
Haihan-Jiang:skip-unavailable-perf-counters
May 27, 2026
Merged

LebedevRI merged 10 commits into
google:mainfrom
Haihan-Jiang:skip-unavailable-perf-counters

Conversation

@Haihan-Jiang

Copy link
Copy Markdown
Contributor

Fixes #2174

This handles platforms where benchmark is built with libpfm, but the specific counters used by these tests cannot be opened. In that case PerfCounters::kSupported is true, while PerfCounters::Create() returns no matching counters.

Changes:

  • skip the gtest perf counter cases when the requested counter names are not available
  • make perf_counters_test exit early unless both CYCLES and INSTRUCTIONS can be created
  • keep the invalid-counter checks in NegativeTest intact

Tested:

  • docker run --rm -v "$PWD":/src -w /src ubuntu:22.04 ... ctest --test-dir build-linux -R "perf_counters_(gtest|test)$" --output-on-failure
Comment thread test/perf_counters_test.cc Outdated
if (!benchmark::internal::PerfCounters::kSupported) {
return false;
}
benchmark::internal::PerfCounters::Initialize();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does PerfCounters::Create() not do this internally?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, Create() already calls Initialize() when the counter list is non-empty. I removed the extra call in 315b6db and left the helper as just an availability check.

Comment thread test/perf_counters_gtest.cc Outdated
}

static bool HasUniqueCounterNames(const PerfCounters& pc,
const std::set<std::string>& names) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file should do the same as the other one,
the current fix looks a bit too unrefined i think.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I replaced the repeated per-test checks with a shared HasRequiredPerfCounters helper in de12d46, so this file now uses the same preflight pattern as perf_counters_test.cc.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure there is a point in keeping HasUniqueCounterNames()?
Also, HasRequiredPerfCounters() should probably be shared,
at least via a header file.

Comment thread test/perf_counters_gtest.cc Outdated

TEST(PerfCountersTest, OneCounter) {
if (!HasRequiredPerfCounters({kGenericPerfEvent1})) {
GTEST_SKIP() << "Requested performance counters are not available.\n";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think GTEST_SKIP needs a trailing newline.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b4c1ba3. I removed the trailing newlines from the GTEST_SKIP messages in this file.

Comment thread test/perf_counters_gtest.cc Resolved
@LebedevRI

Copy link
Copy Markdown
Collaborator

Ok, this is going to be annoying. Let's not bother with sharing the function then :)

@Haihan-Jiang

Copy link
Copy Markdown
Contributor Author

Done, I removed the shared helper header in 23b83d0 and kept the availability checks local to each perf counter test file. That should avoid the Bazel include issue as well.

Local validation:

  • cmake --build build --target perf_counters_gtest perf_counters_test
  • ./build/test/perf_counters_test
  • ./build/test/perf_counters_gtest
  • git diff --check

@LebedevRI LebedevRI left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable-ish i guess.

@LebedevRI
LebedevRI merged commit 345756f into google:main May 27, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants