Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / tests / parse-metric.c
blobfc0838a7abc22f3839dffb2e05910eed35b39258
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>
17 static struct pmu_event pme_test[] = {
19 .metric_expr = "inst_retired.any / cpu_clk_unhalted.thread",
20 .metric_name = "IPC",
21 .metric_group = "group1",
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",
29 .metric_expr = "l1d\\-loads\\-misses / inst_retired.any",
30 .metric_name = "dcache_miss_cpi",
33 .metric_expr = "l1i\\-loads\\-misses / inst_retired.any",
34 .metric_name = "icache_miss_cycles",
37 .metric_expr = "(dcache_miss_cpi + icache_miss_cycles)",
38 .metric_name = "cache_miss_cycles",
39 .metric_group = "group1",
42 .metric_expr = "l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hit",
43 .metric_name = "DCache_L2_All_Hits",
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",
51 .metric_expr = "dcache_l2_all_hits + dcache_l2_all_miss",
52 .metric_name = "DCache_L2_All",
55 .metric_expr = "d_ratio(dcache_l2_all_hits, dcache_l2_all)",
56 .metric_name = "DCache_L2_Hits",
59 .metric_expr = "d_ratio(dcache_l2_all_miss, dcache_l2_all)",
60 .metric_name = "DCache_L2_Misses",
63 .metric_expr = "ipc + m2",
64 .metric_name = "M1",
67 .metric_expr = "ipc + m1",
68 .metric_name = "M2",
71 .metric_expr = "1/m3",
72 .metric_name = "M3",
76 static struct pmu_events_map map = {
77 .cpuid = "test",
78 .version = "1",
79 .type = "core",
80 .table = pme_test,
83 struct value {
84 const char *event;
85 u64 val;
88 static u64 find_value(const char *name, struct value *values)
90 struct value *v = values;
92 while (v->event) {
93 if (!strcmp(name, v->event))
94 return v->val;
95 v++;
97 return 0;
100 static void load_runtime_stat(struct runtime_stat *st, struct evlist *evlist,
101 struct value *vals)
103 struct evsel *evsel;
104 u64 count;
106 evlist__for_each_entry(evlist, evsel) {
107 count = find_value(evsel->name, vals);
108 perf_stat__update_shadow_stats(evsel, count, 0, st);
112 static double compute_single(struct rblist *metric_events, struct evlist *evlist,
113 struct runtime_stat *st, const char *name)
115 struct metric_expr *mexp;
116 struct metric_event *me;
117 struct evsel *evsel;
119 evlist__for_each_entry(evlist, evsel) {
120 me = metricgroup__lookup(metric_events, evsel, false);
121 if (me != NULL) {
122 list_for_each_entry (mexp, &me->head, nd) {
123 if (strcmp(mexp->metric_name, name))
124 continue;
125 return test_generic_metric(mexp, 0, st);
129 return 0.;
132 static int __compute_metric(const char *name, struct value *vals,
133 const char *name1, double *ratio1,
134 const char *name2, double *ratio2)
136 struct rblist metric_events = {
137 .nr_entries = 0,
139 struct perf_cpu_map *cpus;
140 struct runtime_stat st;
141 struct evlist *evlist;
142 int err;
145 * We need to prepare evlist for stat mode running on CPU 0
146 * because that's where all the stats are going to be created.
148 evlist = evlist__new();
149 if (!evlist)
150 return -ENOMEM;
152 cpus = perf_cpu_map__new("0");
153 if (!cpus)
154 return -ENOMEM;
156 perf_evlist__set_maps(&evlist->core, cpus, NULL);
158 /* Parse the metric into metric_events list. */
159 err = metricgroup__parse_groups_test(evlist, &map, name,
160 false, false,
161 &metric_events);
162 if (err)
163 return err;
165 if (perf_evlist__alloc_stats(evlist, false))
166 return -1;
168 /* Load the runtime stats with given numbers for events. */
169 runtime_stat__init(&st);
170 load_runtime_stat(&st, evlist, vals);
172 /* And execute the metric */
173 if (name1 && ratio1)
174 *ratio1 = compute_single(&metric_events, evlist, &st, name1);
175 if (name2 && ratio2)
176 *ratio2 = compute_single(&metric_events, evlist, &st, name2);
178 /* ... clenup. */
179 metricgroup__rblist_exit(&metric_events);
180 runtime_stat__exit(&st);
181 perf_evlist__free_stats(evlist);
182 perf_cpu_map__put(cpus);
183 evlist__delete(evlist);
184 return 0;
187 static int compute_metric(const char *name, struct value *vals, double *ratio)
189 return __compute_metric(name, vals, name, ratio, NULL, NULL);
192 static int compute_metric_group(const char *name, struct value *vals,
193 const char *name1, double *ratio1,
194 const char *name2, double *ratio2)
196 return __compute_metric(name, vals, name1, ratio1, name2, ratio2);
199 static int test_ipc(void)
201 double ratio;
202 struct value vals[] = {
203 { .event = "inst_retired.any", .val = 300 },
204 { .event = "cpu_clk_unhalted.thread", .val = 200 },
205 { .event = NULL, },
208 TEST_ASSERT_VAL("failed to compute metric",
209 compute_metric("IPC", vals, &ratio) == 0);
211 TEST_ASSERT_VAL("IPC failed, wrong ratio",
212 ratio == 1.5);
213 return 0;
216 static int test_frontend(void)
218 double ratio;
219 struct value vals[] = {
220 { .event = "idq_uops_not_delivered.core", .val = 300 },
221 { .event = "cpu_clk_unhalted.thread", .val = 200 },
222 { .event = "cpu_clk_unhalted.one_thread_active", .val = 400 },
223 { .event = "cpu_clk_unhalted.ref_xclk", .val = 600 },
224 { .event = NULL, },
227 TEST_ASSERT_VAL("failed to compute metric",
228 compute_metric("Frontend_Bound_SMT", vals, &ratio) == 0);
230 TEST_ASSERT_VAL("Frontend_Bound_SMT failed, wrong ratio",
231 ratio == 0.45);
232 return 0;
235 static int test_cache_miss_cycles(void)
237 double ratio;
238 struct value vals[] = {
239 { .event = "l1d-loads-misses", .val = 300 },
240 { .event = "l1i-loads-misses", .val = 200 },
241 { .event = "inst_retired.any", .val = 400 },
242 { .event = NULL, },
245 TEST_ASSERT_VAL("failed to compute metric",
246 compute_metric("cache_miss_cycles", vals, &ratio) == 0);
248 TEST_ASSERT_VAL("cache_miss_cycles failed, wrong ratio",
249 ratio == 1.25);
250 return 0;
255 * DCache_L2_All_Hits = l2_rqsts.demand_data_rd_hit + l2_rqsts.pf_hit + l2_rqsts.rfo_hi
256 * DCache_L2_All_Miss = max(l2_rqsts.all_demand_data_rd - l2_rqsts.demand_data_rd_hit, 0) +
257 * l2_rqsts.pf_miss + l2_rqsts.rfo_miss
258 * DCache_L2_All = dcache_l2_all_hits + dcache_l2_all_miss
259 * DCache_L2_Hits = d_ratio(dcache_l2_all_hits, dcache_l2_all)
260 * DCache_L2_Misses = d_ratio(dcache_l2_all_miss, dcache_l2_all)
262 * l2_rqsts.demand_data_rd_hit = 100
263 * l2_rqsts.pf_hit = 200
264 * l2_rqsts.rfo_hi = 300
265 * l2_rqsts.all_demand_data_rd = 400
266 * l2_rqsts.pf_miss = 500
267 * l2_rqsts.rfo_miss = 600
269 * DCache_L2_All_Hits = 600
270 * DCache_L2_All_Miss = MAX(400 - 100, 0) + 500 + 600 = 1400
271 * DCache_L2_All = 600 + 1400 = 2000
272 * DCache_L2_Hits = 600 / 2000 = 0.3
273 * DCache_L2_Misses = 1400 / 2000 = 0.7
275 static int test_dcache_l2(void)
277 double ratio;
278 struct value vals[] = {
279 { .event = "l2_rqsts.demand_data_rd_hit", .val = 100 },
280 { .event = "l2_rqsts.pf_hit", .val = 200 },
281 { .event = "l2_rqsts.rfo_hit", .val = 300 },
282 { .event = "l2_rqsts.all_demand_data_rd", .val = 400 },
283 { .event = "l2_rqsts.pf_miss", .val = 500 },
284 { .event = "l2_rqsts.rfo_miss", .val = 600 },
285 { .event = NULL, },
288 TEST_ASSERT_VAL("failed to compute metric",
289 compute_metric("DCache_L2_Hits", vals, &ratio) == 0);
291 TEST_ASSERT_VAL("DCache_L2_Hits failed, wrong ratio",
292 ratio == 0.3);
294 TEST_ASSERT_VAL("failed to compute metric",
295 compute_metric("DCache_L2_Misses", vals, &ratio) == 0);
297 TEST_ASSERT_VAL("DCache_L2_Misses failed, wrong ratio",
298 ratio == 0.7);
299 return 0;
302 static int test_recursion_fail(void)
304 double ratio;
305 struct value vals[] = {
306 { .event = "inst_retired.any", .val = 300 },
307 { .event = "cpu_clk_unhalted.thread", .val = 200 },
308 { .event = NULL, },
311 TEST_ASSERT_VAL("failed to find recursion",
312 compute_metric("M1", vals, &ratio) == -1);
314 TEST_ASSERT_VAL("failed to find recursion",
315 compute_metric("M3", vals, &ratio) == -1);
316 return 0;
319 static int test_metric_group(void)
321 double ratio1, ratio2;
322 struct value vals[] = {
323 { .event = "cpu_clk_unhalted.thread", .val = 200 },
324 { .event = "l1d-loads-misses", .val = 300 },
325 { .event = "l1i-loads-misses", .val = 200 },
326 { .event = "inst_retired.any", .val = 400 },
327 { .event = NULL, },
330 TEST_ASSERT_VAL("failed to find recursion",
331 compute_metric_group("group1", vals,
332 "IPC", &ratio1,
333 "cache_miss_cycles", &ratio2) == 0);
335 TEST_ASSERT_VAL("group IPC failed, wrong ratio",
336 ratio1 == 2.0);
338 TEST_ASSERT_VAL("group cache_miss_cycles failed, wrong ratio",
339 ratio2 == 1.25);
340 return 0;
343 int test__parse_metric(struct test *test __maybe_unused, int subtest __maybe_unused)
345 TEST_ASSERT_VAL("IPC failed", test_ipc() == 0);
346 TEST_ASSERT_VAL("frontend failed", test_frontend() == 0);
347 TEST_ASSERT_VAL("cache_miss_cycles failed", test_cache_miss_cycles() == 0);
348 TEST_ASSERT_VAL("DCache_L2 failed", test_dcache_l2() == 0);
349 TEST_ASSERT_VAL("recursion fail failed", test_recursion_fail() == 0);
350 TEST_ASSERT_VAL("test metric group", test_metric_group() == 0);
351 return 0;