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