1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
4 #include <perf/cpumap.h>
5 #include <perf/evlist.h>
6 #include "metricgroup.h"
8 #include "pmu-events/pmu-events.h"
14 #include <perf/cpumap.h>
15 #include <perf/evlist.h>
17 static struct pmu_event pme_test[] = {
19 .metric_expr = "inst_retired.any / cpu_clk_unhalted.thread",
23 .metric_expr = "idq_uops_not_delivered.core / (4 * (( ( cpu_clk_unhalted.thread / 2 ) * "
24 "( 1 + cpu_clk_unhalted.one_thread_active / cpu_clk_unhalted.ref_xclk ) )))",
25 .metric_name = "Frontend_Bound_SMT",
28 .metric_expr = "l1d\\-loads\\-misses / inst_retired.any",
29 .metric_name = "dcache_miss_cpi",
32 .metric_expr = "l1i\\-loads\\-misses / inst_retired.any",
33 .metric_name = "icache_miss_cycles",
36 .metric_expr = "(dcache_miss_cpi + icache_miss_cycles)",
37 .metric_name = "cache_miss_cycles",
40 .metric_expr = "l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hit",
41 .metric_name = "DCache_L2_All_Hits",
44 .metric_expr = "max(l2_rqsts.all_demand_data_rd - l2_rqsts.demand_data_rd_hit, 0) + "
45 "l2_rqsts.pf_miss + l2_rqsts.rfo_miss",
46 .metric_name = "DCache_L2_All_Miss",
49 .metric_expr = "dcache_l2_all_hits + dcache_l2_all_miss",
50 .metric_name = "DCache_L2_All",
53 .metric_expr = "d_ratio(dcache_l2_all_hits, dcache_l2_all)",
54 .metric_name = "DCache_L2_Hits",
57 .metric_expr = "d_ratio(dcache_l2_all_miss, dcache_l2_all)",
58 .metric_name = "DCache_L2_Misses",
61 .metric_expr = "ipc + m2",
65 .metric_expr = "ipc + m1",
69 .metric_expr = "1/m3",
74 static struct pmu_events_map map = {
86 static u64 find_value(const char *name, struct value *values)
88 struct value *v = values;
91 if (!strcmp(name, v->event))
98 static void load_runtime_stat(struct runtime_stat *st, struct evlist *evlist,
104 evlist__for_each_entry(evlist, evsel) {
105 count = find_value(evsel->name, vals);
106 perf_stat__update_shadow_stats(evsel, count, 0, st);
110 static double compute_single(struct rblist *metric_events, struct evlist *evlist,
111 struct runtime_stat *st)
113 struct evsel *evsel = evlist__first(evlist);
114 struct metric_event *me;
116 me = metricgroup__lookup(metric_events, evsel, false);
118 struct metric_expr *mexp;
120 mexp = list_first_entry(&me->head, struct metric_expr, nd);
121 return test_generic_metric(mexp, 0, st);
126 static int compute_metric(const char *name, struct value *vals, double *ratio)
128 struct rblist metric_events = {
131 struct perf_cpu_map *cpus;
132 struct runtime_stat st;
133 struct evlist *evlist;
137 * We need to prepare evlist for stat mode running on CPU 0
138 * because that's where all the stats are going to be created.
140 evlist = evlist__new();
144 cpus = perf_cpu_map__new("0");
148 perf_evlist__set_maps(&evlist->core, cpus, NULL);
150 /* Parse the metric into metric_events list. */
151 err = metricgroup__parse_groups_test(evlist, &map, name,
157 if (perf_evlist__alloc_stats(evlist, false))
160 /* Load the runtime stats with given numbers for events. */
161 runtime_stat__init(&st);
162 load_runtime_stat(&st, evlist, vals);
164 /* And execute the metric */
165 *ratio = compute_single(&metric_events, evlist, &st);
168 metricgroup__rblist_exit(&metric_events);
169 runtime_stat__exit(&st);
170 perf_evlist__free_stats(evlist);
171 perf_cpu_map__put(cpus);
172 evlist__delete(evlist);
176 static int test_ipc(void)
179 struct value vals[] = {
180 { .event = "inst_retired.any", .val = 300 },
181 { .event = "cpu_clk_unhalted.thread", .val = 200 },
185 TEST_ASSERT_VAL("failed to compute metric",
186 compute_metric("IPC", vals, &ratio) == 0);
188 TEST_ASSERT_VAL("IPC failed, wrong ratio",
193 static int test_frontend(void)
196 struct value vals[] = {
197 { .event = "idq_uops_not_delivered.core", .val = 300 },
198 { .event = "cpu_clk_unhalted.thread", .val = 200 },
199 { .event = "cpu_clk_unhalted.one_thread_active", .val = 400 },
200 { .event = "cpu_clk_unhalted.ref_xclk", .val = 600 },
204 TEST_ASSERT_VAL("failed to compute metric",
205 compute_metric("Frontend_Bound_SMT", vals, &ratio) == 0);
207 TEST_ASSERT_VAL("Frontend_Bound_SMT failed, wrong ratio",
212 static int test_cache_miss_cycles(void)
215 struct value vals[] = {
216 { .event = "l1d-loads-misses", .val = 300 },
217 { .event = "l1i-loads-misses", .val = 200 },
218 { .event = "inst_retired.any", .val = 400 },
222 TEST_ASSERT_VAL("failed to compute metric",
223 compute_metric("cache_miss_cycles", vals, &ratio) == 0);
225 TEST_ASSERT_VAL("cache_miss_cycles failed, wrong ratio",
232 * DCache_L2_All_Hits = l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hi
233 * DCache_L2_All_Miss = max(l2_rqsts.all_demand_data_rd - l2_rqsts.demand_data_rd_hit, 0) +
234 * l2_rqsts.pf_miss + l2_rqsts.rfo_miss
235 * DCache_L2_All = dcache_l2_all_hits + dcache_l2_all_miss
236 * DCache_L2_Hits = d_ratio(dcache_l2_all_hits, dcache_l2_all)
237 * DCache_L2_Misses = d_ratio(dcache_l2_all_miss, dcache_l2_all)
239 * l2_rqsts.demand_data_rd_hit = 100
240 * l2_rqsts.pf_hit = 200
241 * l2_rqsts.rfo_hi = 300
242 * l2_rqsts.all_demand_data_rd = 400
243 * l2_rqsts.pf_miss = 500
244 * l2_rqsts.rfo_miss = 600
246 * DCache_L2_All_Hits = 600
247 * DCache_L2_All_Miss = MAX(400 - 100, 0) + 500 + 600 = 1400
248 * DCache_L2_All = 600 + 1400 = 2000
249 * DCache_L2_Hits = 600 / 2000 = 0.3
250 * DCache_L2_Misses = 1400 / 2000 = 0.7
252 static int test_dcache_l2(void)
255 struct value vals[] = {
256 { .event = "l2_rqsts.demand_data_rd_hit", .val = 100 },
257 { .event = "l2_rqsts.pf_hit", .val = 200 },
258 { .event = "l2_rqsts.rfo_hit", .val = 300 },
259 { .event = "l2_rqsts.all_demand_data_rd", .val = 400 },
260 { .event = "l2_rqsts.pf_miss", .val = 500 },
261 { .event = "l2_rqsts.rfo_miss", .val = 600 },
265 TEST_ASSERT_VAL("failed to compute metric",
266 compute_metric("DCache_L2_Hits", vals, &ratio) == 0);
268 TEST_ASSERT_VAL("DCache_L2_Hits failed, wrong ratio",
271 TEST_ASSERT_VAL("failed to compute metric",
272 compute_metric("DCache_L2_Misses", vals, &ratio) == 0);
274 TEST_ASSERT_VAL("DCache_L2_Misses failed, wrong ratio",
279 static int test_recursion_fail(void)
282 struct value vals[] = {
283 { .event = "inst_retired.any", .val = 300 },
284 { .event = "cpu_clk_unhalted.thread", .val = 200 },
288 TEST_ASSERT_VAL("failed to find recursion",
289 compute_metric("M1", vals, &ratio) == -1);
291 TEST_ASSERT_VAL("failed to find recursion",
292 compute_metric("M3", vals, &ratio) == -1);
296 int test__parse_metric(struct test *test __maybe_unused, int subtest __maybe_unused)
298 TEST_ASSERT_VAL("IPC failed", test_ipc() == 0);
299 TEST_ASSERT_VAL("frontend failed", test_frontend() == 0);
300 TEST_ASSERT_VAL("cache_miss_cycles failed", test_cache_miss_cycles() == 0);
301 TEST_ASSERT_VAL("DCache_L2 failed", test_dcache_l2() == 0);
302 TEST_ASSERT_VAL("recursion fail failed", test_recursion_fail() == 0);