From 1f97534ae02cfff606bdc71fd52e75835e3e588f Mon Sep 17 00:00:00 2001 From: Clement Courbet Date: Wed, 8 Apr 2020 14:37:38 +0200 Subject: [PATCH] [llvm-exegesis][NFC] Let the pfm::Counter own the PerfHelper. A perf helper is always only ever cretaed to be checked for validity then passed as Counter ctor argument, never to be touched again. Its lifetime should outlive that of the counter, and there is never any reason to have two different counters of top of the perf helper. Make sure these assumptions always hold by making the Counter consume the PerfHelper. --- tools/llvm-exegesis/lib/BenchmarkRunner.cpp | 2 +- tools/llvm-exegesis/lib/PerfHelper.cpp | 2 +- tools/llvm-exegesis/lib/PerfHelper.h | 3 ++- unittests/tools/llvm-exegesis/PerfHelperTest.cpp | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/tools/llvm-exegesis/lib/BenchmarkRunner.cpp index be778f29f52..9592fd8faa0 100644 --- a/tools/llvm-exegesis/lib/BenchmarkRunner.cpp +++ b/tools/llvm-exegesis/lib/BenchmarkRunner.cpp @@ -55,7 +55,7 @@ private: if (!PerfEvent.valid()) return make_error( Twine("invalid perf event '").concat(CounterName).concat("'")); - pfm::Counter Counter(PerfEvent); + pfm::Counter Counter(std::move(PerfEvent)); Scratch->clear(); { CrashRecoveryContext CRC; diff --git a/tools/llvm-exegesis/lib/PerfHelper.cpp b/tools/llvm-exegesis/lib/PerfHelper.cpp index a4e5c17129f..59c66ab2374 100644 --- a/tools/llvm-exegesis/lib/PerfHelper.cpp +++ b/tools/llvm-exegesis/lib/PerfHelper.cpp @@ -88,7 +88,7 @@ StringRef PerfEvent::getPfmEventString() const { } #ifdef HAVE_LIBPFM -Counter::Counter(const PerfEvent &Event) { +Counter::Counter(PerfEvent &&E) : Event(std::move(E)){ assert(Event.valid()); const pid_t Pid = 0; // measure current process/thread. const int Cpu = -1; // measure any processor. diff --git a/tools/llvm-exegesis/lib/PerfHelper.h b/tools/llvm-exegesis/lib/PerfHelper.h index a6a2fc9b9d2..99c555587c5 100644 --- a/tools/llvm-exegesis/lib/PerfHelper.h +++ b/tools/llvm-exegesis/lib/PerfHelper.h @@ -65,7 +65,7 @@ private: // underlying event. struct Counter { // event: the PerfEvent to measure. - explicit Counter(const PerfEvent &event); + explicit Counter(PerfEvent &&event); Counter(const Counter &) = delete; Counter(Counter &&other) = default; @@ -77,6 +77,7 @@ struct Counter { int64_t read() const; // Return the current value of the counter. private: + PerfEvent Event; #ifdef HAVE_LIBPFM int FileDescriptor = -1; #endif diff --git a/unittests/tools/llvm-exegesis/PerfHelperTest.cpp b/unittests/tools/llvm-exegesis/PerfHelperTest.cpp index fa6f05c900b..f4a07e8ebf5 100644 --- a/unittests/tools/llvm-exegesis/PerfHelperTest.cpp +++ b/unittests/tools/llvm-exegesis/PerfHelperTest.cpp @@ -22,11 +22,11 @@ using ::testing::Not; TEST(PerfHelperTest, FunctionalTest) { #ifdef HAVE_LIBPFM ASSERT_FALSE(pfmInitialize()); - const PerfEvent Event("CYCLES:u"); + PerfEvent Event("CYCLES:u"); ASSERT_TRUE(Event.valid()); EXPECT_EQ(Event.name(), "CYCLES:u"); EXPECT_THAT(Event.getPfmEventString(), Not(IsEmpty())); - Counter Cnt(Event); + Counter Cnt(std::move(Event)); Cnt.start(); Cnt.stop(); Cnt.read();