xref: /linux/tools/perf/tests/parse-metric.c (revision 473f6c8f437b049f8ec015d57cd59bb983b1d85c)
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 
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 
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 
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(evlist), 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 
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__put(evlist);
88 		return -ENOMEM;
89 	}
90 
91 	perf_evlist__set_maps(evlist__core(evlist), 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 					     /*cputype_filter=*/false);
97 	if (err)
98 		goto out;
99 
100 	err = evlist__alloc_stats(/*config=*/NULL, evlist, /*alloc_raw=*/false);
101 	if (err)
102 		goto out;
103 
104 	/* Load the runtime stats with given numbers for events. */
105 	load_runtime_stat(evlist, vals);
106 
107 	/* And execute the metric */
108 	if (name1 && ratio1)
109 		*ratio1 = compute_single(evlist, name1);
110 	if (name2 && ratio2)
111 		*ratio2 = compute_single(evlist, name2);
112 
113 out:
114 	/* ... cleanup. */
115 	evlist__free_stats(evlist);
116 	perf_cpu_map__put(cpus);
117 	evlist__put(evlist);
118 	return err;
119 }
120 
121 static int compute_metric(const char *name, struct value *vals, double *ratio)
122 {
123 	return __compute_metric(name, vals, name, ratio, NULL, NULL);
124 }
125 
126 static int compute_metric_group(const char *name, struct value *vals,
127 				const char *name1, double *ratio1,
128 				const char *name2, double *ratio2)
129 {
130 	return __compute_metric(name, vals, name1, ratio1, name2, ratio2);
131 }
132 
133 static int test_ipc(void)
134 {
135 	double ratio;
136 	struct value vals[] = {
137 		{ .event = "inst_retired.any",        .val = 300 },
138 		{ .event = "cpu_clk_unhalted.thread", .val = 200 },
139 		{ .event = NULL, },
140 	};
141 
142 	TEST_ASSERT_VAL("failed to compute metric",
143 			compute_metric("IPC", vals, &ratio) == 0);
144 
145 	TEST_ASSERT_VAL("IPC failed, wrong ratio",
146 			ratio == 1.5);
147 	return 0;
148 }
149 
150 static int test_frontend(void)
151 {
152 	double ratio;
153 	struct value vals[] = {
154 		{ .event = "idq_uops_not_delivered.core",        .val = 300 },
155 		{ .event = "cpu_clk_unhalted.thread",            .val = 200 },
156 		{ .event = "cpu_clk_unhalted.one_thread_active", .val = 400 },
157 		{ .event = "cpu_clk_unhalted.ref_xclk",          .val = 600 },
158 		{ .event = NULL, },
159 	};
160 
161 	TEST_ASSERT_VAL("failed to compute metric",
162 			compute_metric("Frontend_Bound_SMT", vals, &ratio) == 0);
163 
164 	TEST_ASSERT_VAL("Frontend_Bound_SMT failed, wrong ratio",
165 			ratio == 0.45);
166 	return 0;
167 }
168 
169 static int test_cache_miss_cycles(void)
170 {
171 	double ratio;
172 	struct value vals[] = {
173 		{ .event = "l1d-loads-misses",  .val = 300 },
174 		{ .event = "l1i-loads-misses",  .val = 200 },
175 		{ .event = "inst_retired.any",  .val = 400 },
176 		{ .event = NULL, },
177 	};
178 
179 	TEST_ASSERT_VAL("failed to compute metric",
180 			compute_metric("cache_miss_cycles", vals, &ratio) == 0);
181 
182 	TEST_ASSERT_VAL("cache_miss_cycles failed, wrong ratio",
183 			ratio == 1.25);
184 	return 0;
185 }
186 
187 
188 /*
189  * DCache_L2_All_Hits = l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hi
190  * DCache_L2_All_Miss = max(l2_rqsts.all_demand_data_rd - l2_rqsts.demand_data_rd_hit, 0) +
191  *                      l2_rqsts.pf_miss + l2_rqsts.rfo_miss
192  * DCache_L2_All      = dcache_l2_all_hits + dcache_l2_all_miss
193  * DCache_L2_Hits     = d_ratio(dcache_l2_all_hits, dcache_l2_all)
194  * DCache_L2_Misses   = d_ratio(dcache_l2_all_miss, dcache_l2_all)
195  *
196  * l2_rqsts.demand_data_rd_hit = 100
197  * l2_rqsts.pf_hit             = 200
198  * l2_rqsts.rfo_hi             = 300
199  * l2_rqsts.all_demand_data_rd = 400
200  * l2_rqsts.pf_miss            = 500
201  * l2_rqsts.rfo_miss           = 600
202  *
203  * DCache_L2_All_Hits = 600
204  * DCache_L2_All_Miss = MAX(400 - 100, 0) + 500 + 600 = 1400
205  * DCache_L2_All      = 600 + 1400  = 2000
206  * DCache_L2_Hits     = 600 / 2000  = 0.3
207  * DCache_L2_Misses   = 1400 / 2000 = 0.7
208  */
209 static int test_dcache_l2(void)
210 {
211 	double ratio;
212 	struct value vals[] = {
213 		{ .event = "l2_rqsts.demand_data_rd_hit", .val = 100 },
214 		{ .event = "l2_rqsts.pf_hit",             .val = 200 },
215 		{ .event = "l2_rqsts.rfo_hit",            .val = 300 },
216 		{ .event = "l2_rqsts.all_demand_data_rd", .val = 400 },
217 		{ .event = "l2_rqsts.pf_miss",            .val = 500 },
218 		{ .event = "l2_rqsts.rfo_miss",           .val = 600 },
219 		{ .event = NULL, },
220 	};
221 
222 	TEST_ASSERT_VAL("failed to compute metric",
223 			compute_metric("DCache_L2_Hits", vals, &ratio) == 0);
224 
225 	TEST_ASSERT_VAL("DCache_L2_Hits failed, wrong ratio",
226 			ratio == 0.3);
227 
228 	TEST_ASSERT_VAL("failed to compute metric",
229 			compute_metric("DCache_L2_Misses", vals, &ratio) == 0);
230 
231 	TEST_ASSERT_VAL("DCache_L2_Misses failed, wrong ratio",
232 			ratio == 0.7);
233 	return 0;
234 }
235 
236 static int test_recursion_fail(void)
237 {
238 	double ratio;
239 	struct value vals[] = {
240 		{ .event = "inst_retired.any",        .val = 300 },
241 		{ .event = "cpu_clk_unhalted.thread", .val = 200 },
242 		{ .event = NULL, },
243 	};
244 
245 	TEST_ASSERT_VAL("failed to find recursion",
246 			compute_metric("M1", vals, &ratio) == -1);
247 
248 	TEST_ASSERT_VAL("failed to find recursion",
249 			compute_metric("M3", vals, &ratio) == -1);
250 	return 0;
251 }
252 
253 static int test_memory_bandwidth(void)
254 {
255 	double ratio;
256 	struct value vals[] = {
257 		{ .event = "l1d.replacement", .val = 4000000 },
258 		{ .event = "duration_time",  .val = 200000000 },
259 		{ .event = NULL, },
260 	};
261 
262 	TEST_ASSERT_VAL("failed to compute metric",
263 			compute_metric("L1D_Cache_Fill_BW", vals, &ratio) == 0);
264 	TEST_ASSERT_VAL("L1D_Cache_Fill_BW, wrong ratio",
265 			1.28 == ratio);
266 
267 	return 0;
268 }
269 
270 static int test_metric_group(void)
271 {
272 	double ratio1, ratio2;
273 	struct value vals[] = {
274 		{ .event = "cpu_clk_unhalted.thread", .val = 200 },
275 		{ .event = "l1d-loads-misses",        .val = 300 },
276 		{ .event = "l1i-loads-misses",        .val = 200 },
277 		{ .event = "inst_retired.any",        .val = 400 },
278 		{ .event = NULL, },
279 	};
280 
281 	TEST_ASSERT_VAL("failed to find recursion",
282 			compute_metric_group("group1", vals,
283 					     "IPC", &ratio1,
284 					     "cache_miss_cycles", &ratio2) == 0);
285 
286 	TEST_ASSERT_VAL("group IPC failed, wrong ratio",
287 			ratio1 == 2.0);
288 
289 	TEST_ASSERT_VAL("group cache_miss_cycles failed, wrong ratio",
290 			ratio2 == 1.25);
291 	return 0;
292 }
293 
294 static int test__parse_metric(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
295 {
296 	TEST_ASSERT_VAL("IPC failed", test_ipc() == 0);
297 	TEST_ASSERT_VAL("frontend failed", test_frontend() == 0);
298 	TEST_ASSERT_VAL("DCache_L2 failed", test_dcache_l2() == 0);
299 	TEST_ASSERT_VAL("recursion fail failed", test_recursion_fail() == 0);
300 	TEST_ASSERT_VAL("Memory bandwidth", test_memory_bandwidth() == 0);
301 	TEST_ASSERT_VAL("cache_miss_cycles failed", test_cache_miss_cycles() == 0);
302 	TEST_ASSERT_VAL("test metric group", test_metric_group() == 0);
303 	return 0;
304 }
305 
306 DEFINE_SUITE("Parse and process metrics", parse_metric);
307