[llvm] 48f0c80 - [llvm-exegesis] Add ability to assign perf counters to specific PID

Aiden Grossman via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 25 17:46:11 PDT 2023


Author: Aiden Grossman
Date: 2023-06-26T00:45:53Z
New Revision: 48f0c80277d15bef574c10f247b25329dfe4941d

URL: https://github.com/llvm/llvm-project/commit/48f0c80277d15bef574c10f247b25329dfe4941d
DIFF: https://github.com/llvm/llvm-project/commit/48f0c80277d15bef574c10f247b25329dfe4941d.diff

LOG: [llvm-exegesis] Add ability to assign perf counters to specific PID

This patch gives the ability to assign performance counters within
llvm-exegesis to a specific process by passing its PID. This is needed
later on for implementing a subprocess executor. Defaults to zero, the
current process, for the InProcessFunctionExecutorImpl.

Reviewed By: courbet

Differential Revision: https://reviews.llvm.org/D151020

Added: 
    

Modified: 
    llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
    llvm/tools/llvm-exegesis/lib/PerfHelper.h
    llvm/tools/llvm-exegesis/lib/Target.cpp
    llvm/tools/llvm-exegesis/lib/Target.h
    llvm/tools/llvm-exegesis/lib/X86/Target.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp b/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
index 4bf748523cf27..3ff1745e9e062 100644
--- a/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
+++ b/llvm/tools/llvm-exegesis/lib/PerfHelper.cpp
@@ -107,21 +107,20 @@ StringRef PerfEvent::getPfmEventString() const {
   return FullQualifiedEventString;
 }
 
-Counter::Counter(PerfEvent &&E) : Event(std::move(E)){
+Counter::Counter(PerfEvent &&E, pid_t ProcessID) : Event(std::move(E)) {
   assert(Event.valid());
   IsDummyEvent = Event.name() == PerfEvent::DummyEventString;
   if (!IsDummyEvent)
-    initRealEvent(E);
+    initRealEvent(E, ProcessID);
 }
 
 #ifdef HAVE_LIBPFM
-void Counter::initRealEvent(const PerfEvent &E) {
-  const pid_t Pid = 0;    // measure current process/thread.
+void Counter::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;
   perf_event_attr AttrCopy = *Event.attribute();
-  FileDescriptor = perf_event_open(&AttrCopy, Pid, Cpu, GroupFd, Flags);
+  FileDescriptor = perf_event_open(&AttrCopy, ProcessID, Cpu, GroupFd, Flags);
   if (FileDescriptor == -1) {
     errs() << "Unable to open event. ERRNO: " << strerror(errno)
            << ". Make sure your kernel allows user "
@@ -180,7 +179,7 @@ Counter::readOrError(StringRef /*unused*/) const {
 int Counter::numValues() const { return 1; }
 #else
 
-void Counter::initRealEvent(const PerfEvent &) {}
+void Counter::initRealEvent(const PerfEvent &, pid_t ProcessID) {}
 
 Counter::~Counter() = default;
 

diff  --git a/llvm/tools/llvm-exegesis/lib/PerfHelper.h b/llvm/tools/llvm-exegesis/lib/PerfHelper.h
index f3e2b1b6b9522..0796c264b6229 100644
--- a/llvm/tools/llvm-exegesis/lib/PerfHelper.h
+++ b/llvm/tools/llvm-exegesis/lib/PerfHelper.h
@@ -23,6 +23,12 @@
 #include <functional>
 #include <memory>
 
+#ifdef _MSC_VER
+typedef int pid_t;
+#else
+#include <sys/types.h>
+#endif // HAVE_LIBPFM
+
 struct perf_event_attr;
 
 namespace llvm {
@@ -76,7 +82,7 @@ class PerfEvent {
 class Counter {
 public:
   // event: the PerfEvent to measure.
-  explicit Counter(PerfEvent &&event);
+  explicit Counter(PerfEvent &&event, pid_t ProcessID = 0);
 
   Counter(const Counter &) = delete;
   Counter(Counter &&other) = default;
@@ -103,15 +109,15 @@ class Counter {
 
   virtual int numValues() const;
 
+  int getFileDescriptor() const { return FileDescriptor; }
+
 protected:
   PerfEvent Event;
-#ifdef HAVE_LIBPFM
   int FileDescriptor = -1;
-#endif
   bool IsDummyEvent;
 
 private:
-  void initRealEvent(const PerfEvent &E);
+  void initRealEvent(const PerfEvent &E, pid_t ProcessID);
 };
 
 } // namespace pfm

diff  --git a/llvm/tools/llvm-exegesis/lib/Target.cpp b/llvm/tools/llvm-exegesis/lib/Target.cpp
index d99638f2679d3..1e5f688cf61bd 100644
--- a/llvm/tools/llvm-exegesis/lib/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/Target.cpp
@@ -35,7 +35,8 @@ const ExegesisTarget *ExegesisTarget::lookup(Triple TT) {
 }
 
 Expected<std::unique_ptr<pfm::Counter>>
-ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &) const {
+ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &,
+                              const pid_t ProcessID) const {
   pfm::PerfEvent Event(CounterName);
   if (!Event.valid())
     return llvm::make_error<Failure>(
@@ -43,7 +44,7 @@ ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &) const {
             .concat(CounterName)
             .concat("'"));
 
-  return std::make_unique<pfm::Counter>(std::move(Event));
+  return std::make_unique<pfm::Counter>(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 b8fd4abcbbe4a..51bd5f3aff55a 100644
--- a/llvm/tools/llvm-exegesis/lib/Target.h
+++ b/llvm/tools/llvm-exegesis/lib/Target.h
@@ -75,7 +75,8 @@ class ExegesisTarget {
 
   // Targets can use this to create target-specific perf counters.
   virtual Expected<std::unique_ptr<pfm::Counter>>
-  createCounter(StringRef CounterName, const LLVMState &State) const;
+  createCounter(StringRef CounterName, const LLVMState &State,
+                const pid_t ProcessID = 0) const;
 
   // Targets can use this to add target-specific passes in assembleToStream();
   virtual void addTargetSpecificPasses(PassManagerBase &PM) const {}

diff  --git a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
index 833ba01e55467..12e80a7ca06b3 100644
--- a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
+++ b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp
@@ -665,7 +665,8 @@ class ExegesisX86Target : public ExegesisTarget {
   ExegesisX86Target() : ExegesisTarget(X86CpuPfmCounters) {}
 
   Expected<std::unique_ptr<pfm::Counter>>
-  createCounter(StringRef CounterName, const LLVMState &State) const override {
+  createCounter(StringRef CounterName, const LLVMState &State,
+                const pid_t ProcessID) const override {
     // If LbrSamplingPeriod was provided, then ignore the
     // CounterName because we only have one for LBR.
     if (LbrSamplingPeriod > 0) {
@@ -682,7 +683,7 @@ class ExegesisX86Target : public ExegesisTarget {
           llvm::errc::invalid_argument);
 #endif
     }
-    return ExegesisTarget::createCounter(CounterName, State);
+    return ExegesisTarget::createCounter(CounterName, State, ProcessID);
   }
 
 private:


        


More information about the llvm-commits mailing list