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"
15 static struct pmu_event pme_test
[] = {
17 .metric_expr
= "inst_retired.any / cpu_clk_unhalted.thread",
19 .metric_group
= "group1",
22 .metric_expr
= "idq_uops_not_delivered.core / (4 * (( ( cpu_clk_unhalted.thread / 2 ) * "
23 "( 1 + cpu_clk_unhalted.one_thread_active / cpu_clk_unhalted.ref_xclk ) )))",
24 .metric_name
= "Frontend_Bound_SMT",
27 .metric_expr
= "l1d\\-loads\\-misses / inst_retired.any",
28 .metric_name
= "dcache_miss_cpi",
31 .metric_expr
= "l1i\\-loads\\-misses / inst_retired.any",
32 .metric_name
= "icache_miss_cycles",
35 .metric_expr
= "(dcache_miss_cpi + icache_miss_cycles)",
36 .metric_name
= "cache_miss_cycles",
37 .metric_group
= "group1",
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",
77 static struct pmu_events_map map
= {
89 static u64
find_value(const char *name
, struct value
*values
)
91 struct value
*v
= values
;
94 if (!strcmp(name
, v
->event
))
101 static void load_runtime_stat(struct runtime_stat
*st
, struct evlist
*evlist
,
107 evlist__for_each_entry(evlist
, evsel
) {
108 count
= find_value(evsel
->name
, vals
);
109 perf_stat__update_shadow_stats(evsel
, count
, 0, st
);
113 static double compute_single(struct rblist
*metric_events
, struct evlist
*evlist
,
114 struct runtime_stat
*st
, const char *name
)
116 struct metric_expr
*mexp
;
117 struct metric_event
*me
;
120 evlist__for_each_entry(evlist
, evsel
) {
121 me
= metricgroup__lookup(metric_events
, evsel
, false);
123 list_for_each_entry (mexp
, &me
->head
, nd
) {
124 if (strcmp(mexp
->metric_name
, name
))
126 return test_generic_metric(mexp
, 0, st
);
133 static int __compute_metric(const char *name
, struct value
*vals
,
134 const char *name1
, double *ratio1
,
135 const char *name2
, double *ratio2
)
137 struct rblist metric_events
= {
140 struct perf_cpu_map
*cpus
;
141 struct runtime_stat st
;
142 struct evlist
*evlist
;
146 * We need to prepare evlist for stat mode running on CPU 0
147 * because that's where all the stats are going to be created.
149 evlist
= evlist__new();
153 cpus
= perf_cpu_map__new("0");
155 evlist__delete(evlist
);
159 perf_evlist__set_maps(&evlist
->core
, cpus
, NULL
);
160 runtime_stat__init(&st
);
162 /* Parse the metric into metric_events list. */
163 err
= metricgroup__parse_groups_test(evlist
, &map
, name
,
169 err
= evlist__alloc_stats(evlist
, false);
173 /* Load the runtime stats with given numbers for events. */
174 load_runtime_stat(&st
, evlist
, vals
);
176 /* And execute the metric */
178 *ratio1
= compute_single(&metric_events
, evlist
, &st
, name1
);
180 *ratio2
= compute_single(&metric_events
, evlist
, &st
, name2
);
184 metricgroup__rblist_exit(&metric_events
);
185 runtime_stat__exit(&st
);
186 evlist__free_stats(evlist
);
187 perf_cpu_map__put(cpus
);
188 evlist__delete(evlist
);
192 static int compute_metric(const char *name
, struct value
*vals
, double *ratio
)
194 return __compute_metric(name
, vals
, name
, ratio
, NULL
, NULL
);
197 static int compute_metric_group(const char *name
, struct value
*vals
,
198 const char *name1
, double *ratio1
,
199 const char *name2
, double *ratio2
)
201 return __compute_metric(name
, vals
, name1
, ratio1
, name2
, ratio2
);
204 static int test_ipc(void)
207 struct value vals
[] = {
208 { .event
= "inst_retired.any", .val
= 300 },
209 { .event
= "cpu_clk_unhalted.thread", .val
= 200 },
213 TEST_ASSERT_VAL("failed to compute metric",
214 compute_metric("IPC", vals
, &ratio
) == 0);
216 TEST_ASSERT_VAL("IPC failed, wrong ratio",
221 static int test_frontend(void)
224 struct value vals
[] = {
225 { .event
= "idq_uops_not_delivered.core", .val
= 300 },
226 { .event
= "cpu_clk_unhalted.thread", .val
= 200 },
227 { .event
= "cpu_clk_unhalted.one_thread_active", .val
= 400 },
228 { .event
= "cpu_clk_unhalted.ref_xclk", .val
= 600 },
232 TEST_ASSERT_VAL("failed to compute metric",
233 compute_metric("Frontend_Bound_SMT", vals
, &ratio
) == 0);
235 TEST_ASSERT_VAL("Frontend_Bound_SMT failed, wrong ratio",
240 static int test_cache_miss_cycles(void)
243 struct value vals
[] = {
244 { .event
= "l1d-loads-misses", .val
= 300 },
245 { .event
= "l1i-loads-misses", .val
= 200 },
246 { .event
= "inst_retired.any", .val
= 400 },
250 TEST_ASSERT_VAL("failed to compute metric",
251 compute_metric("cache_miss_cycles", vals
, &ratio
) == 0);
253 TEST_ASSERT_VAL("cache_miss_cycles failed, wrong ratio",
260 * DCache_L2_All_Hits = l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hi
261 * DCache_L2_All_Miss = max(l2_rqsts.all_demand_data_rd - l2_rqsts.demand_data_rd_hit, 0) +
262 * l2_rqsts.pf_miss + l2_rqsts.rfo_miss
263 * DCache_L2_All = dcache_l2_all_hits + dcache_l2_all_miss
264 * DCache_L2_Hits = d_ratio(dcache_l2_all_hits, dcache_l2_all)
265 * DCache_L2_Misses = d_ratio(dcache_l2_all_miss, dcache_l2_all)
267 * l2_rqsts.demand_data_rd_hit = 100
268 * l2_rqsts.pf_hit = 200
269 * l2_rqsts.rfo_hi = 300
270 * l2_rqsts.all_demand_data_rd = 400
271 * l2_rqsts.pf_miss = 500
272 * l2_rqsts.rfo_miss = 600
274 * DCache_L2_All_Hits = 600
275 * DCache_L2_All_Miss = MAX(400 - 100, 0) + 500 + 600 = 1400
276 * DCache_L2_All = 600 + 1400 = 2000
277 * DCache_L2_Hits = 600 / 2000 = 0.3
278 * DCache_L2_Misses = 1400 / 2000 = 0.7
280 static int test_dcache_l2(void)
283 struct value vals
[] = {
284 { .event
= "l2_rqsts.demand_data_rd_hit", .val
= 100 },
285 { .event
= "l2_rqsts.pf_hit", .val
= 200 },
286 { .event
= "l2_rqsts.rfo_hit", .val
= 300 },
287 { .event
= "l2_rqsts.all_demand_data_rd", .val
= 400 },
288 { .event
= "l2_rqsts.pf_miss", .val
= 500 },
289 { .event
= "l2_rqsts.rfo_miss", .val
= 600 },
293 TEST_ASSERT_VAL("failed to compute metric",
294 compute_metric("DCache_L2_Hits", vals
, &ratio
) == 0);
296 TEST_ASSERT_VAL("DCache_L2_Hits failed, wrong ratio",
299 TEST_ASSERT_VAL("failed to compute metric",
300 compute_metric("DCache_L2_Misses", vals
, &ratio
) == 0);
302 TEST_ASSERT_VAL("DCache_L2_Misses failed, wrong ratio",
307 static int test_recursion_fail(void)
310 struct value vals
[] = {
311 { .event
= "inst_retired.any", .val
= 300 },
312 { .event
= "cpu_clk_unhalted.thread", .val
= 200 },
316 TEST_ASSERT_VAL("failed to find recursion",
317 compute_metric("M1", vals
, &ratio
) == -1);
319 TEST_ASSERT_VAL("failed to find recursion",
320 compute_metric("M3", vals
, &ratio
) == -1);
324 static int test_metric_group(void)
326 double ratio1
, ratio2
;
327 struct value vals
[] = {
328 { .event
= "cpu_clk_unhalted.thread", .val
= 200 },
329 { .event
= "l1d-loads-misses", .val
= 300 },
330 { .event
= "l1i-loads-misses", .val
= 200 },
331 { .event
= "inst_retired.any", .val
= 400 },
335 TEST_ASSERT_VAL("failed to find recursion",
336 compute_metric_group("group1", vals
,
338 "cache_miss_cycles", &ratio2
) == 0);
340 TEST_ASSERT_VAL("group IPC failed, wrong ratio",
343 TEST_ASSERT_VAL("group cache_miss_cycles failed, wrong ratio",
348 int test__parse_metric(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
350 TEST_ASSERT_VAL("IPC failed", test_ipc() == 0);
351 TEST_ASSERT_VAL("frontend failed", test_frontend() == 0);
352 TEST_ASSERT_VAL("cache_miss_cycles failed", test_cache_miss_cycles() == 0);
353 TEST_ASSERT_VAL("DCache_L2 failed", test_dcache_l2() == 0);
354 TEST_ASSERT_VAL("recursion fail failed", test_recursion_fail() == 0);
355 TEST_ASSERT_VAL("test metric group", test_metric_group() == 0);