| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <cerrno> |
| 10 | #include <cstdint> |
| 11 | #include <cstring> |
| 12 | #include <iostream> |
| 13 | |
| 14 | namespace kai::benchmark { |
| 15 | |
| 16 | #if defined(__APPLE__) && defined(KAI_BENCHMARK_KPERF) && KAI_BENCHMARK_KPERF |
| 17 | |
| 18 | extern "C" { |
| 19 | int kpc_force_all_ctrs_set(int enable); |
| 20 | int kpc_force_all_ctrs_get(int* enable); |
| 21 | int kpc_set_config(uint32_t classes, uint64_t* config); |
| 22 | int kpc_set_counting(uint32_t classes); |
| 23 | int kpc_set_thread_counting(uint32_t classes); |
| 24 | uint32_t kpc_get_counter_count(uint32_t classes); |
| 25 | int kpc_get_thread_counters(uint32_t tid, uint32_t buf_count, uint64_t* buf); |
| 26 | } |
| 27 | |
| 28 | inline void log_kpc_error(const char* op) { |
| 29 | std::cerr << "KPC call failed: " << op << " (errno=" << errno << ": " << std::strerror(errno) << ")\n"; |
| 30 | } |
| 31 | #endif |
| 32 | |
| 33 | struct CycleCounterState { |
| 34 | bool available = false; |
| 35 | bool initialized = false; |
| 36 | #if defined(__APPLE__) && defined(KAI_BENCHMARK_KPERF) && KAI_BENCHMARK_KPERF |
| 37 | uint32_t classes = 0; |
| 38 | uint32_t counter_count = 0; |
| 39 | #endif |
| 40 | }; |
| 41 | |
| 42 | inline CycleCounterState& cycle_counter_state() { |
| 43 | static CycleCounterState state{}; |
| 44 | return state; |
| 45 | } |
| 46 | |
| 47 | inline bool cycle_counter_init() { |
| 48 | CycleCounterState& state = cycle_counter_state(); |
| 49 | if (state.initialized) { |
| 50 | return state.available; |
| 51 | } |
| 52 | state.initialized = true; |
| 53 | |
| 54 | #if defined(__APPLE__) && defined(KAI_BENCHMARK_KPERF) && KAI_BENCHMARK_KPERF |
| 55 | const auto init_kperf = [&]() -> bool { |
| 56 | int force_counters = 0; |
| 57 | errno = 0; |
| 58 | int rc = kpc_force_all_ctrs_get(&force_counters); |
| 59 | if (rc != 0) { |
| 60 | log_kpc_error("kpc_force_all_ctrs_get"); |
| 61 | return false; |
| 62 | } |
| 63 | (void)force_counters; |
| 64 | static constexpr uint32_t KPC_CLASS_FIXED = 0; |
| 65 | static constexpr uint32_t KPC_CLASS_FIXED_MASK = 1u << KPC_CLASS_FIXED; |
| 66 | |
| 67 | state.classes = KPC_CLASS_FIXED_MASK; |
| 68 | |
| 69 | errno = 0; |
| 70 | rc = kpc_set_counting(state.classes); |
| 71 | if (rc != 0) { |
| 72 | log_kpc_error("kpc_set_counting"); |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | errno = 0; |
| 77 | rc = kpc_set_thread_counting(state.classes); |
| 78 | if (rc != 0) { |
| 79 | log_kpc_error("kpc_set_thread_counting"); |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | errno = 0; |
| 84 | const uint32_t nctrs = kpc_get_counter_count(state.classes); |
| 85 | if (nctrs == 0 || nctrs > 64) { |
| 86 | std::cerr << "KPC counter count invalid: " << nctrs << "\n"; |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | state.counter_count = nctrs; |
| 91 | state.available = true; |
| 92 | return true; |
| 93 | }; |
| 94 | |
| 95 | (void)init_kperf(); |
| 96 | if (!state.available) { |
| 97 | std::cerr << "Unable to enable KPC counters; CPU cycle counter data will be omitted.\n"; |
| 98 | } |
| 99 | #endif |
| 100 | |
| 101 | return state.available; |
| 102 | } |
| 103 | |
| 104 | inline void cycle_counter_start() { |
| 105 | #if defined(__APPLE__) && defined(KAI_BENCHMARK_KPERF) && KAI_BENCHMARK_KPERF |
| 106 | CycleCounterState& state = cycle_counter_state(); |
| 107 | if (state.available) { |
| 108 | if (kpc_force_all_ctrs_set(1) != 0 || kpc_set_counting(state.classes) != 0 || |
| 109 | kpc_set_thread_counting(state.classes) != 0) { |
| 110 | state.available = false; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | inline void cycle_counter_stop() { |
| 118 | #if defined(__APPLE__) && defined(KAI_BENCHMARK_KPERF) && KAI_BENCHMARK_KPERF |
| 119 | kpc_set_thread_counting(0); |
| 120 | kpc_set_counting(0); |
| 121 | kpc_force_all_ctrs_set(0); |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | inline bool cycle_counter_read(uint64_t& value) { |
| 126 | #if defined(__APPLE__) && defined(KAI_BENCHMARK_KPERF) && KAI_BENCHMARK_KPERF |
| 127 | CycleCounterState& state = cycle_counter_state(); |
| 128 | if (!state.available) { |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | uint64_t counters[64] = {}; |
| 133 | errno = 0; |
| 134 | if (kpc_get_thread_counters(0, state.counter_count, counters) != 0) { |
| 135 | log_kpc_error("kpc_get_thread_counters"); |
| 136 | state.available = false; |
| 137 | return false; |
| 138 | } |
| 139 | value = counters[0]; |
| 140 | return true; |
| 141 | #else |
| 142 | (void)value; |
| 143 | return false; |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | inline void cycle_counter_shutdown() { |
| 148 | CycleCounterState& state = cycle_counter_state(); |
| 149 | state.available = false; |
| 150 | state.initialized = false; |
| 151 | } |
| 152 | |
| 153 | } // namespace kai::benchmark |