From 159f2eef748c2d498bbccff7539c2a39dcda420d Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Fri, 16 Aug 2024 15:35:35 -0700 Subject: [PATCH] Use group leader fd Events in the same group should be associated with the same set of instructions --- ServerBench.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/ServerBench.cpp b/ServerBench.cpp index 49cf546..891f9f6 100644 --- a/ServerBench.cpp +++ b/ServerBench.cpp @@ -171,7 +171,8 @@ double toSeconds(timeval t) { #ifdef __linux__ #include struct PerfCounter { - PerfCounter(int type, int config, const std::string &labels = {}) + PerfCounter(int type, int config, const std::string &labels = {}, + int groupLeaderFd = -1) : labels(labels) { struct perf_event_attr pe; @@ -183,7 +184,7 @@ struct PerfCounter { pe.exclude_kernel = 1; pe.exclude_hv = 1; - fd = perf_event_open(&pe, 0, -1, -1, 0); + fd = perf_event_open(&pe, 0, -1, groupLeaderFd, 0); if (fd < 0 && errno != ENOENT && errno != EINVAL) { perror(labels.c_str()); } @@ -214,6 +215,7 @@ struct PerfCounter { bool ok() const { return fd >= 0; } const std::string &getLabels() const { return labels; } + int getFd() const { return fd; } private: int fd; @@ -246,7 +248,8 @@ int main(int argc, char **argv) { cs.getMetricsV1(&metrics, &metricsCount); PerfCounter instructions{PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS}; - PerfCounter cycles{PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES}; + PerfCounter cycles{PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES, "", + instructions.getFd()}; std::vector cacheCounters; for (auto [id, idStr] : std::initializer_list>{ @@ -265,6 +268,7 @@ int main(int argc, char **argv) { {PERF_COUNT_HW_CACHE_OP_WRITE, "write"}, {PERF_COUNT_HW_CACHE_OP_PREFETCH, "prefetch"}, }) { + int groupLeaderFd = -1; for (auto [result, resultStr] : std::initializer_list>{ {PERF_COUNT_HW_CACHE_RESULT_MISS, "miss"}, @@ -273,9 +277,14 @@ int main(int argc, char **argv) { auto labels = "{id=\"" + idStr + "\", op=\"" + opStr + "\", result=\"" + resultStr + "\"}"; cacheCounters.emplace_back(PERF_TYPE_HW_CACHE, - id | (op << 8) | (result << 16), labels); + id | (op << 8) | (result << 16), labels, + groupLeaderFd); if (!cacheCounters.back().ok()) { cacheCounters.pop_back(); + } else { + if (groupLeaderFd == -1) { + groupLeaderFd = cacheCounters.back().getFd(); + } } } }