Skip to content

Commit

Permalink
Slightly safer linux perf counters
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Apr 21, 2023
1 parent 64463d4 commit 99f3f4a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions benchmarks/performancecounters/linux-perf-events.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// https://github.com/WojciechMula/toys/blob/master/000helpers/linux-perf-events.h
#pragma once
#ifdef __linux__

Expand Down Expand Up @@ -44,14 +43,15 @@ class LinuxEvents {
uint32_t i = 0;
for (auto config : config_vec) {
attribs.config = config;
fd = static_cast<int>(
int _fd = static_cast<int>(
syscall(__NR_perf_event_open, &attribs, pid, cpu, group, flags));
if (fd == -1) {
if (_fd == -1) {
report_error("perf_event_open");
}
ioctl(fd, PERF_EVENT_IOC_ID, &ids[i++]);
ioctl(_fd, PERF_EVENT_IOC_ID, &ids[i++]);
if (group == -1) {
group = fd;
group = _fd;
fd = _fd;
}
}

Expand Down Expand Up @@ -87,15 +87,19 @@ class LinuxEvents {
}
}
// our actual results are in slots 1,3,5, ... of this structure
// we really should be checking our ids obtained earlier to be safe
for (uint32_t i = 1; i < temp_result_vec.size(); i += 2) {
results[i / 2] = temp_result_vec[i];
}
for (uint32_t i = 2; i < temp_result_vec.size(); i += 2) {
if (ids[i / 2 - 1] != temp_result_vec[i]) {
report_error("event mismatch");
}
}
}

bool is_working() { return working; }

private:
void report_error(const std::string &) { working = false; }
};
#endif
#endif

0 comments on commit 99f3f4a

Please sign in to comment.