| 1 | // |
| 2 | // SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com> |
| 3 | // |
| 4 | // SPDX-License-Identifier: Apache-2.0 |
| 5 | // |
| 6 | |
| 7 | // Class to collect cache statistics using perf events |
| 8 | // |
| 9 | // It would be possible to create a bare metal version of this that hits the PMU registers directly, but for now |
| 10 | // this is Linux only, using the perf interface. |
| 11 | |
| 12 | #include <cinttypes> |
| 13 | #include <cstdint> |
| 14 | #include <cstdio> |
| 15 | #include <cstring> |
| 16 | #include <vector> |
| 17 | |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #define NUM_EVENTS 6 |
| 21 | |
| 22 | // PMU event IDs for L1D fill, L1D access, L2 fill, L2 access, LLC fill, LLC access. |
| 23 | // This relationship is relied on in the statistics output function below. |
| 24 | static const uint32_t event_ids[] = { 0x3, 0x4, 0x17, 0x16, 0x37, 0x36 }; |
| 25 | |
| 26 | #ifdef __linux__ |
| 27 | |
| 28 | #include <linux/perf_event.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <syscall.h> |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | static long |
| 35 | perf_event_open(struct perf_event_attr *hw_event, pid_t pid, |
| 36 | int cpu, int group_fd, unsigned long flags) |
| 37 | { |
| 38 | int ret; |
| 39 | |
| 40 | ret = syscall(__NR_perf_event_open, hw_event, pid, cpu, |
| 41 | group_fd, flags); |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | } // anonymous namespace |
| 46 | |
| 47 | #endif |
| 48 | |
| 49 | class cache_stats { |
| 50 | // Cache statistics need to be gathered per thread - one of these needs to be created per active thread (this |
| 51 | // construction can occur anywhere), then init() is called to set up the counter (this must be done on the |
| 52 | // thread in question), then start_count() and stop_count() (which probably work from anywhere but at present |
| 53 | // are also called on the thread in question). |
| 54 | // |
| 55 | // stop_count() collects the stats into the m_counters[] array for each thread, then "report_summary()" in the |
| 56 | // main class (below) aggregates across the threads. |
| 57 | class cache_stats_thread { |
| 58 | uint64_t m_counters[NUM_EVENTS]; |
| 59 | uint64_t m_ids[NUM_EVENTS]; |
| 60 | int m_fds[NUM_EVENTS]; |
| 61 | bool m_valid; |
| 62 | |
| 63 | struct read_format { |
| 64 | uint64_t nr; |
| 65 | uint64_t time_enabled; |
| 66 | uint64_t time_running; |
| 67 | struct { |
| 68 | uint64_t value; |
| 69 | uint64_t id; |
| 70 | } values[NUM_EVENTS]; |
| 71 | }; |
| 72 | |
| 73 | public: |
| 74 | cache_stats_thread() : m_valid(false) { |
| 75 | // Initialize the 'fds' array so we can tidy up properly later. |
| 76 | for (unsigned int i=0; i<NUM_EVENTS; i++) { |
| 77 | m_fds[i] = -1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | ~cache_stats_thread() { |
| 82 | for (unsigned int i=0; i<NUM_EVENTS; i++) { |
| 83 | if (m_fds[i] >= 0) { |
| 84 | close(m_fds[i]); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Initialise counters: this is not included in the constructor to allow construction en-masse and |
| 90 | // initialization per-thread later. |
| 91 | void init() { |
| 92 | #ifdef __linux__ |
| 93 | // Bare metal code would need to do this directly. |
| 94 | |
| 95 | for (unsigned int i=0; i<NUM_EVENTS; i++) { |
| 96 | struct perf_event_attr pea; |
| 97 | |
| 98 | memset(&pea, 0, sizeof(struct perf_event_attr)); |
| 99 | pea.type = PERF_TYPE_RAW; |
| 100 | pea.size = sizeof(struct perf_event_attr); |
| 101 | pea.config = event_ids[i]; |
| 102 | pea.disabled = 1; |
| 103 | pea.exclude_kernel = 1; |
| 104 | pea.exclude_hv = 1; |
| 105 | pea.read_format = PERF_FORMAT_GROUP | PERF_FORMAT_ID | PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING; |
| 106 | |
| 107 | m_fds[i] = perf_event_open(&pea, 0, -1, (i == 0) ? -1 : m_fds[0], 0); |
| 108 | if (m_fds[i] == -1) { |
| 109 | printf("Can't set up perf event %u!\n", i); |
| 110 | perror("perf_event_open"); |
| 111 | return; |
| 112 | } |
| 113 | ioctl(m_fds[i], PERF_EVENT_IOC_ID, &m_ids[i]); |
| 114 | } |
| 115 | |
| 116 | m_valid = true; |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | void start_count() { |
| 121 | if (!m_valid) { |
| 122 | return; |
| 123 | } |
| 124 | #ifdef __linux__ |
| 125 | ioctl(m_fds[0], PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP); |
| 126 | ioctl(m_fds[0], PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP); |
| 127 | #endif |
| 128 | } |
| 129 | |
| 130 | void stop_count() { |
| 131 | if (!m_valid) { |
| 132 | for (unsigned int j=0; j<NUM_EVENTS; j++) { |
| 133 | m_counters[j] = 0; |
| 134 | } |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | #ifdef __linux__ |
| 139 | read_format res; |
| 140 | |
| 141 | ioctl(m_fds[0], PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP); |
| 142 | |
| 143 | ssize_t r = read(m_fds[0], &res, sizeof(res)); |
| 144 | |
| 145 | if (r != sizeof(res)) { |
| 146 | printf("perf: bad read length %zd, expected %zu\n", r, sizeof(res)); |
| 147 | } |
| 148 | |
| 149 | if (res.nr != NUM_EVENTS) { |
| 150 | printf("perf: number of events mismatch!\n"); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | for (unsigned int i=0; i<NUM_EVENTS; i++) { |
| 155 | for (unsigned int j=0; j<NUM_EVENTS; j++) { |
| 156 | if (res.values[i].id == m_ids[j]) { |
| 157 | m_counters[j] = res.values[i].value; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | #endif |
| 163 | } |
| 164 | |
| 165 | const uint64_t *get_counters() const { |
| 166 | return m_counters; |
| 167 | } |
| 168 | |
| 169 | bool is_valid() const { |
| 170 | return m_valid; |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | std::vector<cache_stats_thread> m_thread_data; |
| 175 | |
| 176 | public: |
| 177 | cache_stats(unsigned int num_threads) : m_thread_data(num_threads) { } |
| 178 | |
| 179 | cache_stats_thread *get_thread_entry(unsigned int threadid) { |
| 180 | return &(m_thread_data[threadid]); |
| 181 | } |
| 182 | |
| 183 | void report_summary() { |
| 184 | uint64_t totals[NUM_EVENTS]; |
| 185 | |
| 186 | for (unsigned int j=0; j<NUM_EVENTS; j++) { |
| 187 | totals[j] = 0; |
| 188 | } |
| 189 | |
| 190 | for (unsigned int i=0; i<m_thread_data.size(); i++) { |
| 191 | if (!m_thread_data[i].is_valid()) { |
| 192 | printf("At least some performance counters failed to initialise - not reporting cache data.\n"); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | const uint64_t *thread_counters = m_thread_data[i].get_counters(); |
| 197 | |
| 198 | for (unsigned int j=0; j<NUM_EVENTS; j++) { |
| 199 | totals[j] += thread_counters[j]; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | const char *descs[] = { "L1", "L2", "LL" }; |
| 204 | |
| 205 | for (unsigned int j=0; j<NUM_EVENTS/2; j++) { |
| 206 | uint64_t accesses = totals[j*2+1]; |
| 207 | uint64_t fills = totals[j*2]; |
| 208 | |
| 209 | printf("%s cache: %" PRIu64 " accesses, %" PRIu64 " fills, %.1f%% miss rate.\n", descs[j], accesses, fills, (float)fills*100/accesses); |
| 210 | } |
| 211 | } |
| 212 | }; |