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