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