Skip to content

Commit 5e9173c

Browse files
[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
1 parent 0150493 commit 5e9173c

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

llvm/tools/llvm-exegesis/lib/PerfHelper.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,20 @@ StringRef PerfEvent::getPfmEventString() const {
107107
return FullQualifiedEventString;
108108
}
109109

110-
Counter::Counter(PerfEvent &&E) : Event(std::move(E)){
110+
Counter::Counter(PerfEvent &&E, pid_t ProcessID) : Event(std::move(E)) {
111111
assert(Event.valid());
112112
IsDummyEvent = Event.name() == PerfEvent::DummyEventString;
113113
if (!IsDummyEvent)
114-
initRealEvent(E);
114+
initRealEvent(E, ProcessID);
115115
}
116116

117117
#ifdef HAVE_LIBPFM
118-
void Counter::initRealEvent(const PerfEvent &E) {
119-
const pid_t Pid = 0; // measure current process/thread.
118+
void Counter::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
120119
const int Cpu = -1; // measure any processor.
121120
const int GroupFd = -1; // no grouping of counters.
122121
const uint32_t Flags = 0;
123122
perf_event_attr AttrCopy = *Event.attribute();
124-
FileDescriptor = perf_event_open(&AttrCopy, Pid, Cpu, GroupFd, Flags);
123+
FileDescriptor = perf_event_open(&AttrCopy, ProcessID, Cpu, GroupFd, Flags);
125124
if (FileDescriptor == -1) {
126125
errs() << "Unable to open event. ERRNO: " << strerror(errno)
127126
<< ". Make sure your kernel allows user "
@@ -180,7 +179,7 @@ Counter::readOrError(StringRef /*unused*/) const {
180179
int Counter::numValues() const { return 1; }
181180
#else
182181

183-
void Counter::initRealEvent(const PerfEvent &) {}
182+
void Counter::initRealEvent(const PerfEvent &, pid_t ProcessID) {}
184183

185184
Counter::~Counter() = default;
186185

llvm/tools/llvm-exegesis/lib/PerfHelper.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include <functional>
2424
#include <memory>
2525

26+
#ifndef HAVE_LIBPFM
27+
typedef int pid_t;
28+
#endif // HAVE_LIBPFM
29+
2630
struct perf_event_attr;
2731

2832
namespace llvm {
@@ -76,7 +80,7 @@ class PerfEvent {
7680
class Counter {
7781
public:
7882
// event: the PerfEvent to measure.
79-
explicit Counter(PerfEvent &&event);
83+
explicit Counter(PerfEvent &&event, pid_t ProcessID = 0);
8084

8185
Counter(const Counter &) = delete;
8286
Counter(Counter &&other) = default;
@@ -103,15 +107,15 @@ class Counter {
103107

104108
virtual int numValues() const;
105109

110+
int getFileDescriptor() const { return FileDescriptor; }
111+
106112
protected:
107113
PerfEvent Event;
108-
#ifdef HAVE_LIBPFM
109114
int FileDescriptor = -1;
110-
#endif
111115
bool IsDummyEvent;
112116

113117
private:
114-
void initRealEvent(const PerfEvent &E);
118+
void initRealEvent(const PerfEvent &E, pid_t ProcessID);
115119
};
116120

117121
} // namespace pfm

llvm/tools/llvm-exegesis/lib/Target.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ const ExegesisTarget *ExegesisTarget::lookup(Triple TT) {
3535
}
3636

3737
Expected<std::unique_ptr<pfm::Counter>>
38-
ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &) const {
38+
ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &,
39+
const pid_t ProcessID) const {
3940
pfm::PerfEvent Event(CounterName);
4041
if (!Event.valid())
4142
return llvm::make_error<Failure>(
4243
llvm::Twine("Unable to create counter with name '")
4344
.concat(CounterName)
4445
.concat("'"));
4546

46-
return std::make_unique<pfm::Counter>(std::move(Event));
47+
return std::make_unique<pfm::Counter>(std::move(Event), ProcessID);
4748
}
4849

4950
void ExegesisTarget::registerTarget(ExegesisTarget *Target) {

llvm/tools/llvm-exegesis/lib/Target.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class ExegesisTarget {
7575

7676
// Targets can use this to create target-specific perf counters.
7777
virtual Expected<std::unique_ptr<pfm::Counter>>
78-
createCounter(StringRef CounterName, const LLVMState &State) const;
78+
createCounter(StringRef CounterName, const LLVMState &State,
79+
const pid_t ProcessID = 0) const;
7980

8081
// Targets can use this to add target-specific passes in assembleToStream();
8182
virtual void addTargetSpecificPasses(PassManagerBase &PM) const {}

llvm/tools/llvm-exegesis/lib/X86/Target.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,8 @@ class ExegesisX86Target : public ExegesisTarget {
665665
ExegesisX86Target() : ExegesisTarget(X86CpuPfmCounters) {}
666666

667667
Expected<std::unique_ptr<pfm::Counter>>
668-
createCounter(StringRef CounterName, const LLVMState &State) const override {
668+
createCounter(StringRef CounterName, const LLVMState &State,
669+
const pid_t ProcessID) const override {
669670
// If LbrSamplingPeriod was provided, then ignore the
670671
// CounterName because we only have one for LBR.
671672
if (LbrSamplingPeriod > 0) {
@@ -682,7 +683,7 @@ class ExegesisX86Target : public ExegesisTarget {
682683
llvm::errc::invalid_argument);
683684
#endif
684685
}
685-
return ExegesisTarget::createCounter(CounterName, State);
686+
return ExegesisTarget::createCounter(CounterName, State, ProcessID);
686687
}
687688

688689
private:

0 commit comments

Comments
 (0)