diff options
author | Liqiang TAO <taolq@outlook.com> | 2022-01-25 19:14:20 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-25 10:14:20 +0000 |
commit | d0fbf8ac238d3bc3a111334ab3553173812d78e7 (patch) | |
tree | 589bdc6d72203e72080ad9b3e128f8d4ce9580b0 /test | |
parent | 57b2bfa33bbc4f171a7fc2807de71583b1e2fd2a (diff) | |
download | google-benchmark-d0fbf8ac238d3bc3a111334ab3553173812d78e7.tar.gz |
Cache PerfCounters instance in PerfCountersMeasurement (#1308)
This patch fixes #1306, by reducing the pinned instances of
PerfCounters.
The issue is caused by creating multiple pinned events in the
same thread, doing so results in the Snapshot(PerfCounterValues* values)
failing, and that's now discoverable.
Creating multile pinned events is an unsupported behavior currently.
The error would be detected at read() time, not
perf_event_open() / iotcl() time.
The unsupported benavior above is confirmed by Stephane Eranian @seranian,
and he also pointed the dectection method.
Finished this patch under the guidance of Mircea Trofin @mtrofin.
Diffstat (limited to 'test')
-rw-r--r-- | test/perf_counters_gtest.cc | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/test/perf_counters_gtest.cc b/test/perf_counters_gtest.cc index 3eac624..f9e6a6f 100644 --- a/test/perf_counters_gtest.cc +++ b/test/perf_counters_gtest.cc @@ -11,6 +11,7 @@ struct MsgHandler { #endif using benchmark::internal::PerfCounters; +using benchmark::internal::PerfCountersMeasurement; using benchmark::internal::PerfCounterValues; namespace { @@ -95,6 +96,53 @@ TEST(PerfCountersTest, Read2Counters) { EXPECT_GT(values2[1], 0); } +TEST(PerfCountersTest, ReopenExistingCounters) { + // The test works (i.e. causes read to fail) for the assumptions + // about hardware capabilities (i.e. small number (3-4) hardware + // counters) at this date. + if (!PerfCounters::kSupported) { + GTEST_SKIP() << "Test skipped because libpfm is not supported.\n"; + } + EXPECT_TRUE(PerfCounters::Initialize()); + std::vector<PerfCounters> counters; + counters.reserve(6); + for (int i = 0; i < 6; i++) + counters.push_back(PerfCounters::Create({kGenericPerfEvent1})); + PerfCounterValues values(1); + EXPECT_TRUE(counters[0].Snapshot(&values)); + EXPECT_FALSE(counters[4].Snapshot(&values)); + EXPECT_FALSE(counters[5].Snapshot(&values)); +} + +TEST(PerfCountersTest, CreateExistingMeasurements) { + // The test works (i.e. causes read to fail) for the assumptions + // about hardware capabilities (i.e. small number (3-4) hardware + // counters) at this date, + // the same as previous test ReopenExistingCounters. + if (!PerfCounters::kSupported) { + GTEST_SKIP() << "Test skipped because libpfm is not supported.\n"; + } + EXPECT_TRUE(PerfCounters::Initialize()); + std::vector<PerfCountersMeasurement> perf_counter_measurements; + std::vector<std::pair<std::string, double>> measurements; + + perf_counter_measurements.reserve(10); + for (int i = 0; i < 10; i++) + perf_counter_measurements.emplace_back( + std::vector<std::string>{kGenericPerfEvent1}); + + perf_counter_measurements[0].Start(); + EXPECT_TRUE(perf_counter_measurements[0].Stop(measurements)); + + measurements.clear(); + perf_counter_measurements[8].Start(); + EXPECT_FALSE(perf_counter_measurements[8].Stop(measurements)); + + measurements.clear(); + perf_counter_measurements[9].Start(); + EXPECT_FALSE(perf_counter_measurements[9].Stop(measurements)); +} + size_t do_work() { size_t res = 0; for (size_t i = 0; i < 100000000; ++i) res += i * i; |