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