|
| 1 | +// https://github.com/WojciechMula/toys/blob/master/000helpers/linux-perf-events.h |
| 2 | +#pragma once |
| 3 | +#ifdef __linux__ |
| 4 | + |
| 5 | +#include <asm/unistd.h> // for __NR_perf_event_open |
| 6 | +#include <linux/perf_event.h> // for perf event constants |
| 7 | +#include <sys/ioctl.h> // for ioctl |
| 8 | +#include <unistd.h> // for syscall |
| 9 | + |
| 10 | +#include <cerrno> // for errno |
| 11 | +#include <cstring> // for memset |
| 12 | +#include <stdexcept> |
| 13 | + |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +template <int TYPE = PERF_TYPE_HARDWARE> class LinuxEvents { |
| 17 | + int fd; |
| 18 | + perf_event_attr attribs; |
| 19 | + int num_events; |
| 20 | + std::vector<uint64_t> temp_result_vec; |
| 21 | + std::vector<uint64_t> ids; |
| 22 | + |
| 23 | +public: |
| 24 | + LinuxEvents(std::vector<int> config_vec) : fd(0) { |
| 25 | + memset(&attribs, 0, sizeof(attribs)); |
| 26 | + attribs.type = TYPE; |
| 27 | + attribs.size = sizeof(attribs); |
| 28 | + attribs.disabled = 1; |
| 29 | + attribs.exclude_kernel = 1; |
| 30 | + attribs.exclude_hv = 1; |
| 31 | + |
| 32 | + attribs.sample_period = 0; |
| 33 | + attribs.read_format = PERF_FORMAT_GROUP | PERF_FORMAT_ID; |
| 34 | + const int pid = 0; // the current process |
| 35 | + const int cpu = -1; // all CPUs |
| 36 | + const unsigned long flags = 0; |
| 37 | + |
| 38 | + int group = -1; // no group |
| 39 | + num_events = config_vec.size(); |
| 40 | + uint32_t i = 0; |
| 41 | + for (auto config : config_vec) { |
| 42 | + attribs.config = config; |
| 43 | + fd = syscall(__NR_perf_event_open, &attribs, pid, cpu, group, flags); |
| 44 | + if (fd == -1) { |
| 45 | + report_error("perf_event_open"); |
| 46 | + } |
| 47 | + ioctl(fd, PERF_EVENT_IOC_ID, &ids[i++]); |
| 48 | + if (group == -1) { |
| 49 | + group = fd; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + temp_result_vec.resize(num_events * 2 + 1); |
| 54 | + } |
| 55 | + |
| 56 | + ~LinuxEvents() { close(fd); } |
| 57 | + |
| 58 | + inline void start() { |
| 59 | + if (ioctl(fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) { |
| 60 | + report_error("ioctl(PERF_EVENT_IOC_RESET)"); |
| 61 | + } |
| 62 | + |
| 63 | + if (ioctl(fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) { |
| 64 | + report_error("ioctl(PERF_EVENT_IOC_ENABLE)"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + inline void end(std::vector<unsigned long long> &results) { |
| 69 | + if (ioctl(fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == -1) { |
| 70 | + report_error("ioctl(PERF_EVENT_IOC_DISABLE)"); |
| 71 | + } |
| 72 | + |
| 73 | + if (read(fd, &temp_result_vec[0], temp_result_vec.size() * 8) == -1) { |
| 74 | + report_error("read"); |
| 75 | + } |
| 76 | + // our actual results are in slots 1,3,5, ... of this structure |
| 77 | + // we really should be checking our ids obtained earlier to be safe |
| 78 | + for (uint32_t i = 1; i < temp_result_vec.size(); i += 2) { |
| 79 | + results[i / 2] = temp_result_vec[i]; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + void report_error(const std::string &context) { |
| 85 | + throw std::runtime_error(context + ": " + std::string(strerror(errno))); |
| 86 | + } |
| 87 | +}; |
| 88 | +#endif |
0 commit comments