From 5a132799a479efaca3a6217495b50d65b15b562a Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Thu, 15 Aug 2024 15:13:00 -0700 Subject: [PATCH] Add cycles_total --- ServerBench.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/ServerBench.cpp b/ServerBench.cpp index 4323b53..5912d4b 100644 --- a/ServerBench.cpp +++ b/ServerBench.cpp @@ -174,14 +174,14 @@ double toSeconds(timeval t) { #include #ifdef __linux__ -struct InstructionCount { - InstructionCount() { +struct PerfCounter { + explicit PerfCounter(int event) { struct perf_event_attr pe; memset(&pe, 0, sizeof(pe)); pe.type = PERF_TYPE_HARDWARE; pe.size = sizeof(pe); - pe.config = PERF_COUNT_HW_INSTRUCTIONS; + pe.config = event; pe.inherit = 1; pe.exclude_kernel = 1; pe.exclude_hv = 1; @@ -202,7 +202,7 @@ struct InstructionCount { return count; } - ~InstructionCount() { close(fd); } + ~PerfCounter() { close(fd); } private: int fd; @@ -215,7 +215,8 @@ private: } }; #else -struct InstructionCount { +struct PerfCounter { + explicit PerPerfCounter(int) {} int64_t total() { return 0; } }; #endif @@ -232,7 +233,8 @@ int main(int argc, char **argv) { int metricsCount; cs.getMetricsV1(&metrics, &metricsCount); - InstructionCount instructions; + PerfCounter instructions{PERF_COUNT_HW_INSTRUCTIONS}; + PerfCounter cycles{PERF_COUNT_HW_CPU_CYCLES}; auto w = std::thread{workload, &cs}; for (;;) { @@ -265,6 +267,11 @@ int main(int argc, char **argv) { "instructions_total "; body += std::to_string(instructions.total()); body += "\n"; + body += "# HELP cycles_total Total number of cycles\n" + "# TYPE cycles_total counter\n" + "cycles_total "; + body += std::to_string(cycles.total()); + body += "\n"; for (int i = 0; i < metricsCount; ++i) { body += "# HELP ";