Skip to content

Commit e3e47a1

Browse files
[llvm-exegesis] Refactor Counter to CounterGroup
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.
1 parent 8fb8ad6 commit e3e47a1

File tree

8 files changed

+31
-28
lines changed

8 files changed

+31
-28
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class InProcessFunctionExecutorImpl : public BenchmarkRunner::FunctionExecutor {
128128
if (!CounterOrError)
129129
return CounterOrError.takeError();
130130

131-
pfm::Counter *Counter = CounterOrError.get().get();
131+
pfm::CounterGroup *Counter = CounterOrError.get().get();
132132
Scratch->clear();
133133
{
134134
auto PS = ET.withSavedState();
@@ -311,7 +311,7 @@ class SubProcessFunctionExecutorImpl
311311
if (!CounterOrError)
312312
return CounterOrError.takeError();
313313

314-
pfm::Counter *Counter = CounterOrError.get().get();
314+
pfm::CounterGroup *Counter = CounterOrError.get().get();
315315

316316
close(PipeFiles[0]);
317317

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,16 @@ StringRef PerfEvent::getPfmEventString() const {
107107
return FullQualifiedEventString;
108108
}
109109

110-
Counter::Counter(PerfEvent &&E, pid_t ProcessID) : Event(std::move(E)) {
110+
CounterGroup::CounterGroup(PerfEvent &&E, pid_t ProcessID)
111+
: Event(std::move(E)) {
111112
assert(Event.valid());
112113
IsDummyEvent = Event.name() == PerfEvent::DummyEventString;
113114
if (!IsDummyEvent)
114115
initRealEvent(E, ProcessID);
115116
}
116117

117118
#ifdef HAVE_LIBPFM
118-
void Counter::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
119+
void CounterGroup::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
119120
const int Cpu = -1; // measure any processor.
120121
const int GroupFd = -1; // no grouping of counters.
121122
const uint32_t Flags = 0;
@@ -133,23 +134,23 @@ void Counter::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
133134
assert(FileDescriptor != -1 && "Unable to open event");
134135
}
135136

136-
Counter::~Counter() {
137+
CounterGroup::~CounterGroup() {
137138
if (!IsDummyEvent)
138139
close(FileDescriptor);
139140
}
140141

141-
void Counter::start() {
142+
void CounterGroup::start() {
142143
if (!IsDummyEvent)
143144
ioctl(FileDescriptor, PERF_EVENT_IOC_RESET, 0);
144145
}
145146

146-
void Counter::stop() {
147+
void CounterGroup::stop() {
147148
if (!IsDummyEvent)
148149
ioctl(FileDescriptor, PERF_EVENT_IOC_DISABLE, 0);
149150
}
150151

151152
llvm::Expected<llvm::SmallVector<int64_t, 4>>
152-
Counter::readOrError(StringRef /*unused*/) const {
153+
CounterGroup::readOrError(StringRef /*unused*/) const {
153154
int64_t Count = 0;
154155
if (!IsDummyEvent) {
155156
ssize_t ReadSize = ::read(FileDescriptor, &Count, sizeof(Count));
@@ -165,19 +166,19 @@ Counter::readOrError(StringRef /*unused*/) const {
165166
return Result;
166167
}
167168

168-
int Counter::numValues() const { return 1; }
169+
int CounterGroup::numValues() const { return 1; }
169170
#else
170171

171-
void Counter::initRealEvent(const PerfEvent &, pid_t ProcessID) {}
172+
void CounterGroup::initRealEvent(const PerfEvent &, pid_t ProcessID) {}
172173

173-
Counter::~Counter() = default;
174+
CounterGroup::~CounterGroup() = default;
174175

175-
void Counter::start() {}
176+
void CounterGroup::start() {}
176177

177-
void Counter::stop() {}
178+
void CounterGroup::stop() {}
178179

179180
llvm::Expected<llvm::SmallVector<int64_t, 4>>
180-
Counter::readOrError(StringRef /*unused*/) const {
181+
CounterGroup::readOrError(StringRef /*unused*/) const {
181182
if (IsDummyEvent) {
182183
llvm::SmallVector<int64_t, 4> Result;
183184
Result.push_back(42);
@@ -187,7 +188,7 @@ Counter::readOrError(StringRef /*unused*/) const {
187188
llvm::errc::io_error);
188189
}
189190

190-
int Counter::numValues() const { return 1; }
191+
int CounterGroup::numValues() const { return 1; }
191192

192193
#endif
193194

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,19 @@ class PerfEvent {
7777
void initRealEvent(StringRef PfmEventString);
7878
};
7979

80-
// Uses a valid PerfEvent to configure the Kernel so we can measure the
81-
// underlying event.
82-
class Counter {
80+
// Consists of a counter measuring a specific event and associated validation
81+
// counters measuring execution conditions. All counters in a group are part
82+
// of a single event group and are thus scheduled on and off the CPU as a single
83+
// unit.
84+
class CounterGroup {
8385
public:
8486
// event: the PerfEvent to measure.
85-
explicit Counter(PerfEvent &&event, pid_t ProcessID = 0);
87+
explicit CounterGroup(PerfEvent &&event, pid_t ProcessID = 0);
8688

87-
Counter(const Counter &) = delete;
88-
Counter(Counter &&other) = default;
89+
CounterGroup(const CounterGroup &) = delete;
90+
CounterGroup(CounterGroup &&other) = default;
8991

90-
virtual ~Counter();
92+
virtual ~CounterGroup();
9193

9294
/// Starts the measurement of the event.
9395
virtual void start();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const ExegesisTarget *ExegesisTarget::lookup(Triple TT) {
3535
return nullptr;
3636
}
3737

38-
Expected<std::unique_ptr<pfm::Counter>>
38+
Expected<std::unique_ptr<pfm::CounterGroup>>
3939
ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &,
4040
const pid_t ProcessID) const {
4141
pfm::PerfEvent Event(CounterName);
@@ -45,7 +45,7 @@ ExegesisTarget::createCounter(StringRef CounterName, const LLVMState &,
4545
.concat(CounterName)
4646
.concat("'"));
4747

48-
return std::make_unique<pfm::Counter>(std::move(Event), ProcessID);
48+
return std::make_unique<pfm::CounterGroup>(std::move(Event), ProcessID);
4949
}
5050

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class ExegesisTarget {
8484
: CpuPfmCounters(CpuPfmCounters), IsOpcodeAvailable(IsOpcodeAvailable) {}
8585

8686
// Targets can use this to create target-specific perf counters.
87-
virtual Expected<std::unique_ptr<pfm::Counter>>
87+
virtual Expected<std::unique_ptr<pfm::CounterGroup>>
8888
createCounter(StringRef CounterName, const LLVMState &State,
8989
const pid_t ProcessID = 0) const;
9090

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ class ExegesisX86Target : public ExegesisTarget {
679679
ExegesisX86Target()
680680
: ExegesisTarget(X86CpuPfmCounters, X86_MC::isOpcodeAvailable) {}
681681

682-
Expected<std::unique_ptr<pfm::Counter>>
682+
Expected<std::unique_ptr<pfm::CounterGroup>>
683683
createCounter(StringRef CounterName, const LLVMState &State,
684684
const pid_t ProcessID) const override {
685685
// If LbrSamplingPeriod was provided, then ignore the

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ X86LbrPerfEvent::X86LbrPerfEvent(unsigned SamplingPeriod) {
141141
}
142142

143143
X86LbrCounter::X86LbrCounter(pfm::PerfEvent &&NewEvent)
144-
: Counter(std::move(NewEvent)) {
144+
: CounterGroup(std::move(NewEvent)) {
145145
MMappedBuffer = mmap(nullptr, kMappedBufferSize, PROT_READ | PROT_WRITE,
146146
MAP_SHARED, FileDescriptor, 0);
147147
if (MMappedBuffer == MAP_FAILED)

llvm/tools/llvm-exegesis/lib/X86/X86Counter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class X86LbrPerfEvent : public pfm::PerfEvent {
3131
X86LbrPerfEvent(unsigned SamplingPeriod);
3232
};
3333

34-
class X86LbrCounter : public pfm::Counter {
34+
class X86LbrCounter : public pfm::CounterGroup {
3535
public:
3636
static llvm::Error checkLbrSupport();
3737

0 commit comments

Comments
 (0)