15 #ifndef BENCHMARK_PERF_COUNTERS_H
16 #define BENCHMARK_PERF_COUNTERS_H
22 #include "benchmark/benchmark.h"
26 #ifndef BENCHMARK_OS_WINDOWS
45 BM_CHECK_LE(nr_counters_, kMaxCounters);
48 uint64_t operator[](
size_t pos)
const {
return values_[kPadding + pos]; }
50 static constexpr
size_t kMaxCounters = 3;
56 std::pair<char*, size_t> get_data_buffer() {
57 return {
reinterpret_cast<char*
>(values_.data()),
58 sizeof(uint64_t) * (kPadding + nr_counters_)};
61 static constexpr
size_t kPadding = 1;
62 std::array<uint64_t, kPadding + kMaxCounters> values_;
63 const size_t nr_counters_;
72 static const bool kSupported;
74 bool IsValid()
const {
return is_valid_; }
83 static bool Initialize();
90 static PerfCounters Create(
const std::vector<std::string>& counter_names);
96 #ifndef BENCHMARK_OS_WINDOWS
97 assert(values !=
nullptr);
99 auto buffer = values->get_data_buffer();
100 auto read_bytes = ::read(counter_ids_[0], buffer.first, buffer.second);
101 return static_cast<size_t>(read_bytes) == buffer.second;
108 const std::vector<std::string>& names()
const {
return counter_names_; }
109 size_t num_counters()
const {
return counter_names_.size(); }
112 PerfCounters(
const std::vector<std::string>& counter_names,
113 std::vector<int>&& counter_ids)
114 : counter_ids_(std::move(counter_ids)),
115 counter_names_(counter_names),
119 std::vector<int> counter_ids_;
120 const std::vector<std::string> counter_names_;
121 const bool is_valid_;
128 : counters_(std::move(c)),
129 start_values_(counters_.IsValid() ? counters_.names().size() : 0),
130 end_values_(counters_.IsValid() ? counters_.names().size() : 0) {}
132 bool IsValid()
const {
return counters_.IsValid(); }
134 BENCHMARK_ALWAYS_INLINE
void Start() {
139 counters_.Snapshot(&start_values_);
143 BENCHMARK_ALWAYS_INLINE std::vector<std::pair<std::string, double>>
144 StopAndGetMeasurements() {
149 counters_.Snapshot(&end_values_);
152 std::vector<std::pair<std::string, double>> ret;
153 for (
size_t i = 0; i < counters_.names().size(); ++i) {
154 double measurement =
static_cast<double>(end_values_[i]) -
155 static_cast<double>(start_values_[i]);
156 ret.push_back({counters_.names()[i], measurement});
167 BENCHMARK_UNUSED
static bool perf_init_anchor = PerfCounters::Initialize();
Definition: perf_counters.h:42
Definition: perf_counters.h:125
Definition: perf_counters.h:69