Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[llvm-exegesis] Refactor Counter to CounterGroup #77887

Conversation

boomanaiden154
Copy link
Contributor

This refactoring gets things ready for validation counters where the plan is to reuse the existing Counter infrastructure to contain event groups that consist of a single event that is being measured along with validation counters.

This refactoring gets things ready for validation counters where the
plan is to reuse the existing Counter infrastructure to contain event
groups that consist of a single event that is being measured along with
validation counters.
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 12, 2024

@llvm/pr-subscribers-tools-llvm-exegesis

Author: Aiden Grossman (boomanaiden154)

Changes

This refactoring gets things ready for validation counters where the plan is to reuse the existing Counter infrastructure to contain event groups that consist of a single event that is being measured along with validation counters.


Full diff: https://github.com/llvm/llvm-project/pull/77887.diff

8 Files Affected:

  • (modified) llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp (+2-2)
  • (modified) llvm/tools/llvm-exegesis/lib/PerfHelper.cpp (+14-13)
  • (modified) llvm/tools/llvm-exegesis/lib/PerfHelper.h (+9-7)
  • (modified) llvm/tools/llvm-exegesis/lib/Target.cpp (+2-2)
  • (modified) llvm/tools/llvm-exegesis/lib/Target.h (+1-1)
  • (modified) llvm/tools/llvm-exegesis/lib/X86/Target.cpp (+1-1)
  • (modified) llvm/tools/llvm-exegesis/lib/X86/X86Counter.cpp (+1-1)
  • (modified) llvm/tools/llvm-exegesis/lib/X86/X86Counter.h (+1-1)
diff --git a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
index dee7af5fd520a4..28d578ce5bea01 100644
--- a/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
+++ b/llvm/tools/llvm-exegesis/lib/BenchmarkRunner.cpp
@@ -128,7 +128,7 @@ class InProcessFunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
     if (!CounterOrError)
       return CounterOrError.takeError();
 
-    pfm::Counter *Counter = CounterOrError.get().get();
+    pfm::CounterGroup *Counter = CounterOrError.get().get();
     Scratch->clear();
     {
       auto PS = ET.withSavedState();
@@ -311,7 +311,7 @@ class SubProcessFunctionExecutorImpl
     if (!CounterOrError)
       return CounterOrError.takeError();
 
-    pfm::Counter *Counter = CounterOrError.get().get();
+    pfm::CounterGroup *Counter = CounterOrError.get().get();
 
     close(PipeFiles[0]);
 
diff --git a/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp b/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
index 314de1ec32366f..f6e091bdff9aec 100644
--- a/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
+++ b/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
@@ -107,7 +107,8 @@ StringRef PerfEvent::getPfmEventString() const {
   return FullQualifiedEventString;
 }
 
-Counter::Counter(PerfEvent &&E, pid_t ProcessID) : Event(std::move(E)) {
+CounterGroup::CounterGroup(PerfEvent &&E, pid_t ProcessID)
+    : Event(std::move(E)) {
   assert(Event.valid());
   IsDummyEvent = Event.name() == PerfEvent::DummyEventString;
   if (!IsDummyEvent)
@@ -115,7 +116,7 @@ Counter::Counter(PerfEvent &&E, pid_t ProcessID) : Event(std::move(E)) {
 }
 
 #ifdef HAVE_LIBPFM
-void Counter::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
+void CounterGroup::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
   const int Cpu = -1;     // measure any processor.
   const int GroupFd = -1; // no grouping of counters.
   const uint32_t Flags = 0;
@@ -133,23 +134,23 @@ void Counter::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
   assert(FileDescriptor != -1 && "Unable to open event");
 }
 
-Counter::~Counter() {
+CounterGroup::~CounterGroup() {
   if (!IsDummyEvent)
     close(FileDescriptor);
 }
 
-void Counter::start() {
+void CounterGroup::start() {
   if (!IsDummyEvent)
     ioctl(FileDescriptor, PERF_EVENT_IOC_RESET, 0);
 }
 
-void Counter::stop() {
+void CounterGroup::stop() {
   if (!IsDummyEvent)
     ioctl(FileDescriptor, PERF_EVENT_IOC_DISABLE, 0);
 }
 
 llvm::Expected<llvm::SmallVector<int64_t, 4>>
-Counter::readOrError(StringRef /*unused*/) const {
+CounterGroup::readOrError(StringRef /*unused*/) const {
   int64_t Count = 0;
   if (!IsDummyEvent) {
     ssize_t ReadSize = ::read(FileDescriptor, &Count, sizeof(Count));
@@ -165,19 +166,19 @@ Counter::readOrError(StringRef /*unused*/) const {
   return Result;
 }
 
-int Counter::numValues() const { return 1; }
+int CounterGroup::numValues() const { return 1; }
 #else
 
-void Counter::initRealEvent(const PerfEvent &, pid_t ProcessID) {}
+void CounterGroup::initRealEvent(const PerfEvent &, pid_t ProcessID) {}
 
-Counter::~Counter() = default;
+CounterGroup::~CounterGroup() = default;
 
-void Counter::start() {}
+void CounterGroup::start() {}
 
-void Counter::stop() {}
+void CounterGroup::stop() {}
 
 llvm::Expected<llvm::SmallVector<int64_t, 4>>
-Counter::readOrError(StringRef /*unused*/) const {
+CounterGroup::readOrError(StringRef /*unused*/) const {
   if (IsDummyEvent) {
     llvm::SmallVector<int64_t, 4> Result;
     Result.push_back(42);
@@ -187,7 +188,7 @@ Counter::readOrError(StringRef /*unused*/) const {
                                              llvm::errc::io_error);
 }
 
-int Counter::numValues() const { return 1; }
+int CounterGroup::numValues() const { return 1; }
 
 #endif
 
diff --git a/llvm/tools/llvm-exegesis/lib/PerfHelper.h b/llvm/tools/llvm-exegesis/lib/PerfHelper.h
index 894aac1f197ed1..daf2fbd0e3abc8 100644
--- a/llvm/tools/llvm-exegesis/lib/PerfHelper.h
+++ b/llvm/tools/llvm-exegesis/lib/PerfHelper.h
@@ -77,17 +77,19 @@ class PerfEvent {
   void initRealEvent(StringRef PfmEventString);
 };
 
-// Uses a valid PerfEvent to configure the Kernel so we can measure the
-// underlying event.
-class Counter {
+// Consists of a counter measuring a specific event and associated validation
+// counters measuring execution conditions. All counters in a group are part
+// of a single event group and are thus scheduled on and off the CPU as a single
+// unit.
+class CounterGroup {
 public:
   // event: the PerfEvent to measure.
-  explicit Counter(PerfEvent &&event, pid_t ProcessID = 0);
+  explicit CounterGroup(PerfEvent &&event, pid_t ProcessID = 0);
 
-  Counter(const Counter &) = delete;
-  Counter(Counter &&other) = default;
+  CounterGroup(const CounterGroup &) = delete;
+  CounterGroup(CounterGroup &&other) = default;
 
-  virtual ~Counter();
+  virtual ~CounterGroup();
 
   /// Starts the measurement of the event.
   virtual void start();
diff --git a/llvm/tools/llvm-exegesis/lib/Target.cpp b/llvm/tools/llvm-exegesis/lib/Target.cpp
index fe1eded63dc51a..8f1c5a157eea39 100644
--- a/llvm/tools/llvm-exegesis/lib/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/Target.cpp
@@ -35,7 +35,7 @@ const ExegesisTarget *ExegesisTarget::lookup(Triple TT) {
   return nullptr;
 }
 
-Expected<std::unique_ptr<pfm::Counter>>
+Expected<std::unique_ptr<pfm::CounterGroup>>
 ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &,
                               const pid_t ProcessID) const {
   pfm::PerfEvent Event(CounterName);
@@ -45,7 +45,7 @@ ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &,
             .concat(CounterName)
             .concat("'"));
 
-  return std::make_unique<pfm::Counter>(std::move(Event), ProcessID);
+  return std::make_unique<pfm::CounterGroup>(std::move(Event), ProcessID);
 }
 
 void ExegesisTarget::registerTarget(ExegesisTarget *Target) {
diff --git a/llvm/tools/llvm-exegesis/lib/Target.h b/llvm/tools/llvm-exegesis/lib/Target.h
index a9e21c782b4dfe..da6d44611eca7c 100644
--- a/llvm/tools/llvm-exegesis/lib/Target.h
+++ b/llvm/tools/llvm-exegesis/lib/Target.h
@@ -84,7 +84,7 @@ class ExegesisTarget {
       : CpuPfmCounters(CpuPfmCounters), IsOpcodeAvailable(IsOpcodeAvailable) {}
 
   // Targets can use this to create target-specific perf counters.
-  virtual Expected<std::unique_ptr<pfm::Counter>>
+  virtual Expected<std::unique_ptr<pfm::CounterGroup>>
   createCounter(StringRef CounterName, const LLVMState &State,
                 const pid_t ProcessID = 0) const;
 
diff --git a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
index 0ab74b8e00da11..25df89fb0cf31d 100644
--- a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
@@ -679,7 +679,7 @@ class ExegesisX86Target : public ExegesisTarget {
   ExegesisX86Target()
       : ExegesisTarget(X86CpuPfmCounters, X86_MC::isOpcodeAvailable) {}
 
-  Expected<std::unique_ptr<pfm::Counter>>
+  Expected<std::unique_ptr<pfm::CounterGroup>>
   createCounter(StringRef CounterName, const LLVMState &State,
                 const pid_t ProcessID) const override {
     // If LbrSamplingPeriod was provided, then ignore the
diff --git a/llvm/tools/llvm-exegesis/lib/X86/X86Counter.cpp b/llvm/tools/llvm-exegesis/lib/X86/X86Counter.cpp
index 423c45e22bf8c0..55ca2d7146ceb7 100644
--- a/llvm/tools/llvm-exegesis/lib/X86/X86Counter.cpp
+++ b/llvm/tools/llvm-exegesis/lib/X86/X86Counter.cpp
@@ -141,7 +141,7 @@ X86LbrPerfEvent::X86LbrPerfEvent(unsigned SamplingPeriod) {
 }
 
 X86LbrCounter::X86LbrCounter(pfm::PerfEvent &&NewEvent)
-    : Counter(std::move(NewEvent)) {
+    : CounterGroup(std::move(NewEvent)) {
   MMappedBuffer = mmap(nullptr, kMappedBufferSize, PROT_READ | PROT_WRITE,
                        MAP_SHARED, FileDescriptor, 0);
   if (MMappedBuffer == MAP_FAILED)
diff --git a/llvm/tools/llvm-exegesis/lib/X86/X86Counter.h b/llvm/tools/llvm-exegesis/lib/X86/X86Counter.h
index 73e4dc5b990a0f..bc2fced9acfbf4 100644
--- a/llvm/tools/llvm-exegesis/lib/X86/X86Counter.h
+++ b/llvm/tools/llvm-exegesis/lib/X86/X86Counter.h
@@ -31,7 +31,7 @@ class X86LbrPerfEvent : public pfm::PerfEvent {
   X86LbrPerfEvent(unsigned SamplingPeriod);
 };
 
-class X86LbrCounter : public pfm::Counter {
+class X86LbrCounter : public pfm::CounterGroup {
 public:
   static llvm::Error checkLbrSupport();
 

@boomanaiden154 boomanaiden154 merged commit e366e04 into main Jan 16, 2024
4 of 5 checks passed
@boomanaiden154 boomanaiden154 deleted the users/boomanaiden154/exegesis-validation-counters-countergroup branch January 16, 2024 09:24
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
This refactoring gets things ready for validation counters where the
plan is to reuse the existing Counter infrastructure to contain event
groups that consist of a single event that is being measured along with
validation counters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants