@@ -107,21 +107,18 @@ StringRef PerfEvent::getPfmEventString() const {
107
107
return FullQualifiedEventString;
108
108
}
109
109
110
- CounterGroup::CounterGroup (PerfEvent &&E, pid_t ProcessID )
111
- : Event(std::move(E )) {
110
+ ConfiguredEvent::ConfiguredEvent (PerfEvent &&EventToConfigure )
111
+ : Event(std::move(EventToConfigure )) {
112
112
assert (Event.valid ());
113
- IsDummyEvent = Event.name () == PerfEvent::DummyEventString;
114
- if (!IsDummyEvent)
115
- initRealEvent (E, ProcessID);
116
113
}
117
114
118
115
#ifdef HAVE_LIBPFM
119
- void CounterGroup ::initRealEvent (const PerfEvent &E, pid_t ProcessID) {
120
- const int Cpu = -1 ; // measure any processor.
121
- const int GroupFd = -1 ; // no grouping of counters.
116
+ void ConfiguredEvent ::initRealEvent (const pid_t ProcessID) {
117
+ const int CPU = -1 ;
118
+ const int GroupFD = -1 ;
122
119
const uint32_t Flags = 0 ;
123
120
perf_event_attr AttrCopy = *Event.attribute ();
124
- FileDescriptor = perf_event_open (&AttrCopy, ProcessID, Cpu, GroupFd , Flags);
121
+ FileDescriptor = perf_event_open (&AttrCopy, ProcessID, CPU, GroupFD , Flags);
125
122
if (FileDescriptor == -1 ) {
126
123
errs () << " Unable to open event. ERRNO: " << strerror (errno)
127
124
<< " . Make sure your kernel allows user "
@@ -134,44 +131,67 @@ void CounterGroup::initRealEvent(const PerfEvent &E, pid_t ProcessID) {
134
131
assert (FileDescriptor != -1 && " Unable to open event" );
135
132
}
136
133
137
- CounterGroup::~CounterGroup () {
134
+ Expected<SmallVector<int64_t >>
135
+ ConfiguredEvent::readOrError (StringRef /* unused*/ ) const {
136
+ int64_t Count = 0 ;
137
+ ssize_t ReadSize = ::read (FileDescriptor, &Count, sizeof (Count));
138
+
139
+ if (ReadSize != sizeof (Count))
140
+ return llvm::make_error<llvm::StringError>(" Failed to read event counter" ,
141
+ llvm::errc::io_error);
142
+
143
+ SmallVector<int64_t , 1 > Result;
144
+ Result.push_back (Count);
145
+ return Result;
146
+ }
147
+
148
+ ConfiguredEvent::~ConfiguredEvent () { close (FileDescriptor); }
149
+ #else
150
+ void ConfiguredEvent::initRealEvent (pid_t ProcessID) {}
151
+
152
+ Expected<SmallVector<int64_t >>
153
+ ConfiguredEvent::readOrError (StringRef /* unused*/ ) const {
154
+ return make_error<StringError>(" Not implemented" ,
155
+ errc::function_not_supported);
156
+ }
157
+
158
+ ConfiguredEvent::~ConfiguredEvent () = default ;
159
+ #endif // HAVE_LIBPFM
160
+
161
+ CounterGroup::CounterGroup (PerfEvent &&E, pid_t ProcessID)
162
+ : EventCounter(std::move(E)) {
163
+ IsDummyEvent = EventCounter.isDummyEvent ();
138
164
if (!IsDummyEvent)
139
- close (FileDescriptor);
165
+ initRealEvent (ProcessID);
166
+ }
167
+
168
+ #ifdef HAVE_LIBPFM
169
+ void CounterGroup::initRealEvent (pid_t ProcessID) {
170
+ EventCounter.initRealEvent (ProcessID);
140
171
}
141
172
142
173
void CounterGroup::start () {
143
174
if (!IsDummyEvent)
144
- ioctl (FileDescriptor , PERF_EVENT_IOC_RESET, 0 );
175
+ ioctl (getFileDescriptor () , PERF_EVENT_IOC_RESET, 0 );
145
176
}
146
177
147
178
void CounterGroup::stop () {
148
179
if (!IsDummyEvent)
149
- ioctl (FileDescriptor , PERF_EVENT_IOC_DISABLE, 0 );
180
+ ioctl (getFileDescriptor () , PERF_EVENT_IOC_DISABLE, 0 );
150
181
}
151
182
152
183
llvm::Expected<llvm::SmallVector<int64_t , 4 >>
153
- CounterGroup::readOrError (StringRef /* unused*/ ) const {
154
- int64_t Count = 0 ;
155
- if (!IsDummyEvent) {
156
- ssize_t ReadSize = ::read (FileDescriptor, &Count, sizeof (Count));
157
- if (ReadSize != sizeof (Count))
158
- return llvm::make_error<llvm::StringError>(" Failed to read event counter" ,
159
- llvm::errc::io_error);
160
- } else {
161
- Count = 42 ;
162
- }
163
-
164
- llvm::SmallVector<int64_t , 4 > Result;
165
- Result.push_back (Count);
166
- return Result;
184
+ CounterGroup::readOrError (StringRef FunctionBytes) const {
185
+ if (!IsDummyEvent)
186
+ return EventCounter.readOrError (FunctionBytes);
187
+ else
188
+ return SmallVector<int64_t , 1 >(1 , 42 );
167
189
}
168
190
169
191
int CounterGroup::numValues () const { return 1 ; }
170
192
#else
171
193
172
- void CounterGroup::initRealEvent (const PerfEvent &, pid_t ProcessID) {}
173
-
174
- CounterGroup::~CounterGroup () = default ;
194
+ void CounterGroup::initRealEvent (pid_t ProcessID) {}
175
195
176
196
void CounterGroup::start () {}
177
197
0 commit comments