[llvm-exegesis][NFC] Let the pfm::Counter own the PerfHelper.
authorClement Courbet <courbet@google.com>
Wed, 8 Apr 2020 12:37:38 +0000 (14:37 +0200)
committerClement Courbet <courbet@google.com>
Wed, 8 Apr 2020 13:37:30 +0000 (15:37 +0200)
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.

llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
llvm/tools/llvm-exegesis/lib/PerfHelper.h
llvm/unittests/tools/llvm-exegesis/PerfHelperTest.cpp

index be778f2..9592fd8 100644 (file)
@@ -55,7 +55,7 @@ private:
       if (!PerfEvent.valid())
         return make_error<Failure>(
             Twine("invalid perf event '").concat(CounterName).concat("'"));
-      pfm::Counter Counter(PerfEvent);
+      pfm::Counter Counter(std::move(PerfEvent));
       Scratch->clear();
       {
         CrashRecoveryContext CRC;
index a4e5c17..59c66ab 100644 (file)
@@ -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.
index a6a2fc9..99c5555 100644 (file)
@@ -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
index fa6f05c..f4a07e8 100644 (file)
@@ -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();