1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <perf/cpumap.h>
6 #include <perf/evlist.h>
7 #include "metricgroup.h"
8 #include "tests.h"
9 #include "pmu-events/pmu-events.h"
10 #include "evlist.h"
11 #include "rblist.h"
12 #include "debug.h"
13 #include "expr.h"
14 #include "stat.h"
15 #include "pmus.h"
16
17 struct value {
18 const char *event;
19 u64 val;
20 };
21
find_value(const char * name,struct value * values)22 static u64 find_value(const char *name, struct value *values)
23 {
24 struct value *v = values;
25
26 while (v->event) {
27 if (!strcmp(name, v->event))
28 return v->val;
29 v++;
30 }
31 return 0;
32 }
33
load_runtime_stat(struct evlist * evlist,struct value * vals)34 static void load_runtime_stat(struct evlist *evlist, struct value *vals)
35 {
36 struct evsel *evsel;
37 u64 count;
38
39 evlist__alloc_aggr_stats(evlist, 1);
40 evlist__for_each_entry(evlist, evsel) {
41 count = find_value(evsel->name, vals);
42 evsel->supported = true;
43 evsel->stats->aggr->counts.val = count;
44 evsel->stats->aggr->counts.ena = 1;
45 evsel->stats->aggr->counts.run = 1;
46 }
47 }
48
compute_single(struct evlist * evlist,const char * name)49 static double compute_single(struct evlist *evlist, const char *name)
50 {
51 struct metric_expr *mexp;
52 struct metric_event *me;
53 struct evsel *evsel;
54
55 evlist__for_each_entry(evlist, evsel) {
56 me = metricgroup__lookup(&evlist->metric_events, evsel, false);
57 if (me != NULL) {
58 list_for_each_entry (mexp, &me->head, nd) {
59 if (strcmp(mexp->metric_name, name))
60 continue;
61 return test_generic_metric(mexp, 0);
62 }
63 }
64 }
65 return 0.;
66 }
67
__compute_metric(const char * name,struct value * vals,const char * name1,double * ratio1,const char * name2,double * ratio2)68 static int __compute_metric(const char *name, struct value *vals,
69 const char *name1, double *ratio1,
70 const char *name2, double *ratio2)
71 {
72 const struct pmu_metrics_table *pme_test;
73 struct perf_cpu_map *cpus;
74 struct evlist *evlist;
75 int err;
76
77 /*
78 * We need to prepare evlist for stat mode running on CPU 0
79 * because that's where all the stats are going to be created.
80 */
81 evlist = evlist__new();
82 if (!evlist)
83 return -ENOMEM;
84
85 cpus = perf_cpu_map__new("0");
86 if (!cpus) {
87 evlist__delete(evlist);
88 return -ENOMEM;
89 }
90
91 perf_evlist__set_maps(&evlist->core, cpus, NULL);
92
93 /* Parse the metric into metric_events list. */
94 pme_test = find_core_metrics_table("testarch", "testcpu");
95 err = metricgroup__parse_groups_test(evlist, pme_test, name);
96 if (err)
97 goto out;
98
99 err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
100 if (err)
101 goto out;
102
103 /* Load the runtime stats with given numbers for events. */
104 load_runtime_stat(evlist, vals);
105
106 /* And execute the metric */
107 if (name1 && ratio1)
108 *ratio1 = compute_single(evlist, name1);
109 if (name2 && ratio2)
110 *ratio2 = compute_single(evlist, name2);
111
112 out:
113 /* ... cleanup. */
114 evlist__free_stats(evlist);
115 perf_cpu_map__put(cpus);
116 evlist__delete(evlist);
117 return err;
118 }
119
compute_metric(const char * name,struct value * vals,double * ratio)120 static int compute_metric(const char *name, struct value *vals, double *ratio)
121 {
122 return __compute_metric(name, vals, name, ratio, NULL, NULL);
123 }
124
compute_metric_group(const char * name,struct value * vals,const char * name1,double * ratio1,const char * name2,double * ratio2)125 static int compute_metric_group(const char *name, struct value *vals,
126 const char *name1, double *ratio1,
127 const char *name2, double *ratio2)
128 {
129 return __compute_metric(name, vals, name1, ratio1, name2, ratio2);
130 }
131
test_ipc(void)132 static int test_ipc(void)
133 {
134 double ratio;
135 struct value vals[] = {
136 { .event = "inst_retired.any", .val = 300 },
137 { .event = "cpu_clk_unhalted.thread", .val = 200 },
138 { .event = NULL, },
139 };
140
141 TEST_ASSERT_VAL("failed to compute metric",
142 compute_metric("IPC", vals, &ratio) == 0);
143
144 TEST_ASSERT_VAL("IPC failed, wrong ratio",
145 ratio == 1.5);
146 return 0;
147 }
148
test_frontend(void)149 static int test_frontend(void)
150 {
151 double ratio;
152 struct value vals[] = {
153 { .event = "idq_uops_not_delivered.core", .val = 300 },
154 { .event = "cpu_clk_unhalted.thread", .val = 200 },
155 { .event = "cpu_clk_unhalted.one_thread_active", .val = 400 },
156 { .event = "cpu_clk_unhalted.ref_xclk", .val = 600 },
157 { .event = NULL, },
158 };
159
160 TEST_ASSERT_VAL("failed to compute metric",
161 compute_metric("Frontend_Bound_SMT", vals, &ratio) == 0);
162
163 TEST_ASSERT_VAL("Frontend_Bound_SMT failed, wrong ratio",
164 ratio == 0.45);
165 return 0;
166 }
167
test_cache_miss_cycles(void)168 static int test_cache_miss_cycles(void)
169 {
170 double ratio;
171 struct value vals[] = {
172 { .event = "l1d-loads-misses", .val = 300 },
173 { .event = "l1i-loads-misses", .val = 200 },
174 { .event = "inst_retired.any", .val = 400 },
175 { .event = NULL, },
176 };
177
178 TEST_ASSERT_VAL("failed to compute metric",
179 compute_metric("cache_miss_cycles", vals, &ratio) == 0);
180
181 TEST_ASSERT_VAL("cache_miss_cycles failed, wrong ratio",
182 ratio == 1.25);
183 return 0;
184 }
185
186
187 /*
188 * DCache_L2_All_Hits = l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hi
189 * DCache_L2_All_Miss = max(l2_rqsts.all_demand_data_rd - l2_rqsts.demand_data_rd_hit, 0) +
190 * l2_rqsts.pf_miss + l2_rqsts.rfo_miss
191 * DCache_L2_All = dcache_l2_all_hits + dcache_l2_all_miss
192 * DCache_L2_Hits = d_ratio(dcache_l2_all_hits, dcache_l2_all)
193 * DCache_L2_Misses = d_ratio(dcache_l2_all_miss, dcache_l2_all)
194 *
195 * l2_rqsts.demand_data_rd_hit = 100
196 * l2_rqsts.pf_hit = 200
197 * l2_rqsts.rfo_hi = 300
198 * l2_rqsts.all_demand_data_rd = 400
199 * l2_rqsts.pf_miss = 500
200 * l2_rqsts.rfo_miss = 600
201 *
202 * DCache_L2_All_Hits = 600
203 * DCache_L2_All_Miss = MAX(400 - 100, 0) + 500 + 600 = 1400
204 * DCache_L2_All = 600 + 1400 = 2000
205 * DCache_L2_Hits = 600 / 2000 = 0.3
206 * DCache_L2_Misses = 1400 / 2000 = 0.7
207 */
test_dcache_l2(void)208 static int test_dcache_l2(void)
209 {
210 double ratio;
211 struct value vals[] = {
212 { .event = "l2_rqsts.demand_data_rd_hit", .val = 100 },
213 { .event = "l2_rqsts.pf_hit", .val = 200 },
214 { .event = "l2_rqsts.rfo_hit", .val = 300 },
215 { .event = "l2_rqsts.all_demand_data_rd", .val = 400 },
216 { .event = "l2_rqsts.pf_miss", .val = 500 },
217 { .event = "l2_rqsts.rfo_miss", .val = 600 },
218 { .event = NULL, },
219 };
220
221 TEST_ASSERT_VAL("failed to compute metric",
222 compute_metric("DCache_L2_Hits", vals, &ratio) == 0);
223
224 TEST_ASSERT_VAL("DCache_L2_Hits failed, wrong ratio",
225 ratio == 0.3);
226
227 TEST_ASSERT_VAL("failed to compute metric",
228 compute_metric("DCache_L2_Misses", vals, &ratio) == 0);
229
230 TEST_ASSERT_VAL("DCache_L2_Misses failed, wrong ratio",
231 ratio == 0.7);
232 return 0;
233 }
234
test_recursion_fail(void)235 static int test_recursion_fail(void)
236 {
237 double ratio;
238 struct value vals[] = {
239 { .event = "inst_retired.any", .val = 300 },
240 { .event = "cpu_clk_unhalted.thread", .val = 200 },
241 { .event = NULL, },
242 };
243
244 TEST_ASSERT_VAL("failed to find recursion",
245 compute_metric("M1", vals, &ratio) == -1);
246
247 TEST_ASSERT_VAL("failed to find recursion",
248 compute_metric("M3", vals, &ratio) == -1);
249 return 0;
250 }
251
test_memory_bandwidth(void)252 static int test_memory_bandwidth(void)
253 {
254 double ratio;
255 struct value vals[] = {
256 { .event = "l1d.replacement", .val = 4000000 },
257 { .event = "duration_time", .val = 200000000 },
258 { .event = NULL, },
259 };
260
261 TEST_ASSERT_VAL("failed to compute metric",
262 compute_metric("L1D_Cache_Fill_BW", vals, &ratio) == 0);
263 TEST_ASSERT_VAL("L1D_Cache_Fill_BW, wrong ratio",
264 1.28 == ratio);
265
266 return 0;
267 }
268
test_metric_group(void)269 static int test_metric_group(void)
270 {
271 double ratio1, ratio2;
272 struct value vals[] = {
273 { .event = "cpu_clk_unhalted.thread", .val = 200 },
274 { .event = "l1d-loads-misses", .val = 300 },
275 { .event = "l1i-loads-misses", .val = 200 },
276 { .event = "inst_retired.any", .val = 400 },
277 { .event = NULL, },
278 };
279
280 TEST_ASSERT_VAL("failed to find recursion",
281 compute_metric_group("group1", vals,
282 "IPC", &ratio1,
283 "cache_miss_cycles", &ratio2) == 0);
284
285 TEST_ASSERT_VAL("group IPC failed, wrong ratio",
286 ratio1 == 2.0);
287
288 TEST_ASSERT_VAL("group cache_miss_cycles failed, wrong ratio",
289 ratio2 == 1.25);
290 return 0;
291 }
292
test__parse_metric(struct test_suite * test __maybe_unused,int subtest __maybe_unused)293 static int test__parse_metric(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
294 {
295 TEST_ASSERT_VAL("IPC failed", test_ipc() == 0);
296 TEST_ASSERT_VAL("frontend failed", test_frontend() == 0);
297 TEST_ASSERT_VAL("DCache_L2 failed", test_dcache_l2() == 0);
298 TEST_ASSERT_VAL("recursion fail failed", test_recursion_fail() == 0);
299 TEST_ASSERT_VAL("Memory bandwidth", test_memory_bandwidth() == 0);
300 TEST_ASSERT_VAL("cache_miss_cycles failed", test_cache_miss_cycles() == 0);
301 TEST_ASSERT_VAL("test metric group", test_metric_group() == 0);
302 return 0;
303 }
304
305 DEFINE_SUITE("Parse and process metrics", parse_metric);
306