Skip to content

Commit

Permalink
Slightly safer linux counters
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Apr 21, 2023
1 parent 3c313b8 commit abf3d7d
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions benchmark/linux/linux-perf-events.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
// https://github.com/WojciechMula/toys/blob/master/000helpers/linux-perf-events.h
#pragma once
#ifdef __linux__
#ifdef __has_include
#if __has_include(<asm/unistd.h>)
#include <asm/unistd.h> // for __NR_perf_event_open
#else
#warning "Header asm/unistd.h cannot be found though it is a linux system. Are linux headers missing?"
#endif
#else // no __has_include
// Please insure that linux headers have been installed.

#include <asm/unistd.h> // for __NR_perf_event_open
#endif
#include <linux/perf_event.h> // for perf event constants
#include <sys/ioctl.h> // for ioctl
#include <unistd.h> // for syscall

#include <cerrno> // for errno
#include <cstring> // for std::memset
#include <cstring> // for memset
#include <stdexcept>

#include <iostream>
Expand All @@ -28,13 +19,11 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
perf_event_attr attribs{};
size_t num_events{};
std::vector<uint64_t> temp_result_vec{};
std::vector<uint64_t> result{};
std::vector<int> fds{};
bool quiet;
std::vector<uint64_t> ids{};

public:
explicit LinuxEvents(std::vector<int> config_vec, bool _quiet=false) : fd(0), working(true), quiet{_quiet} {
std::memset(&attribs, 0, sizeof(attribs));
explicit LinuxEvents(std::vector<int> config_vec) : fd(0), working(true) {
memset(&attribs, 0, sizeof(attribs));
attribs.type = TYPE;
attribs.size = sizeof(attribs);
attribs.disabled = 1;
Expand All @@ -49,30 +38,25 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {

int group = -1; // no group
num_events = config_vec.size();
result.resize(config_vec.size());
ids.resize(config_vec.size());
uint32_t i = 0;
for (auto config : config_vec) {
attribs.config = config;
int _fd = static_cast<int>(syscall(__NR_perf_event_open, &attribs, pid, cpu, group, flags));
if (_fd == -1) {
report_error("perf_event_open");
}
fd = _fd; // fd tracks the last _fd value.
fds.push_back(fd);
ioctl(fd, PERF_EVENT_IOC_ID, &result[i++]);
ioctl(_fd, PERF_EVENT_IOC_ID, &ids[i++]);
if (group == -1) {
group = fd;
group = _fd;
fd = _fd;
}
}

temp_result_vec.resize(num_events * 2 + 1);
}

~LinuxEvents() {
for (auto tfd : fds) {
if (tfd != -1) { close(tfd); }
}
}
~LinuxEvents() { if (fd != -1) { close(fd); } }

inline void start() {
if (fd != -1) {
Expand All @@ -97,24 +81,25 @@ template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents {
}
}
// our actual results are in slots 1,3,5, ... of this structure
// we really should be checking our result 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 &context) {
if (!quiet) {
if (working) {
std::cerr << (context + ": " + std::string(strerror(errno))) << std::endl;
}
}
void report_error(const std::string &) {
working = false;
}
};
#endif
#endif

0 comments on commit abf3d7d

Please sign in to comment.