263cbb67c861a8fb9d87e6caac005398454ba17e
[platform/kernel/linux-starfive.git] / tools / perf / tests / pmu-events.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "math.h"
3 #include "parse-events.h"
4 #include "pmu.h"
5 #include "tests.h"
6 #include <errno.h>
7 #include <stdio.h>
8 #include <linux/kernel.h>
9 #include <linux/zalloc.h>
10 #include "debug.h"
11 #include "../pmu-events/pmu-events.h"
12 #include "util/evlist.h"
13 #include "util/expr.h"
14 #include "util/parse-events.h"
15 #include "metricgroup.h"
16
17 struct perf_pmu_test_event {
18         /* used for matching against events from generated pmu-events.c */
19         struct pmu_event event;
20
21         /* used for matching against event aliases */
22         /* extra events for aliases */
23         const char *alias_str;
24
25         /*
26          * Note: For when PublicDescription does not exist in the JSON, we
27          * will have no long_desc in pmu_event.long_desc, but long_desc may
28          * be set in the alias.
29          */
30         const char *alias_long_desc;
31
32         /* PMU which we should match against */
33         const char *matching_pmu;
34 };
35
36 struct perf_pmu_test_pmu {
37         struct perf_pmu pmu;
38         struct perf_pmu_test_event const *aliases[10];
39 };
40
41 static const struct perf_pmu_test_event bp_l1_btb_correct = {
42         .event = {
43                 .name = "bp_l1_btb_correct",
44                 .event = "event=0x8a",
45                 .desc = "L1 BTB Correction",
46                 .topic = "branch",
47         },
48         .alias_str = "event=0x8a",
49         .alias_long_desc = "L1 BTB Correction",
50 };
51
52 static const struct perf_pmu_test_event bp_l2_btb_correct = {
53         .event = {
54                 .name = "bp_l2_btb_correct",
55                 .event = "event=0x8b",
56                 .desc = "L2 BTB Correction",
57                 .topic = "branch",
58         },
59         .alias_str = "event=0x8b",
60         .alias_long_desc = "L2 BTB Correction",
61 };
62
63 static const struct perf_pmu_test_event segment_reg_loads_any = {
64         .event = {
65                 .name = "segment_reg_loads.any",
66                 .event = "event=0x6,period=200000,umask=0x80",
67                 .desc = "Number of segment register loads",
68                 .topic = "other",
69         },
70         .alias_str = "event=0x6,period=0x30d40,umask=0x80",
71         .alias_long_desc = "Number of segment register loads",
72 };
73
74 static const struct perf_pmu_test_event dispatch_blocked_any = {
75         .event = {
76                 .name = "dispatch_blocked.any",
77                 .event = "event=0x9,period=200000,umask=0x20",
78                 .desc = "Memory cluster signals to block micro-op dispatch for any reason",
79                 .topic = "other",
80         },
81         .alias_str = "event=0x9,period=0x30d40,umask=0x20",
82         .alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
83 };
84
85 static const struct perf_pmu_test_event eist_trans = {
86         .event = {
87                 .name = "eist_trans",
88                 .event = "event=0x3a,period=200000,umask=0x0",
89                 .desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
90                 .topic = "other",
91         },
92         .alias_str = "event=0x3a,period=0x30d40,umask=0",
93         .alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
94 };
95
96 static const struct perf_pmu_test_event l3_cache_rd = {
97         .event = {
98                 .name = "l3_cache_rd",
99                 .event = "event=0x40",
100                 .desc = "L3 cache access, read",
101                 .long_desc = "Attributable Level 3 cache access, read",
102                 .topic = "cache",
103         },
104         .alias_str = "event=0x40",
105         .alias_long_desc = "Attributable Level 3 cache access, read",
106 };
107
108 static const struct perf_pmu_test_event *core_events[] = {
109         &bp_l1_btb_correct,
110         &bp_l2_btb_correct,
111         &segment_reg_loads_any,
112         &dispatch_blocked_any,
113         &eist_trans,
114         &l3_cache_rd,
115         NULL
116 };
117
118 static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
119         .event = {
120                 .name = "uncore_hisi_ddrc.flux_wcmd",
121                 .event = "event=0x2",
122                 .desc = "DDRC write commands. Unit: hisi_sccl,ddrc ",
123                 .topic = "uncore",
124                 .long_desc = "DDRC write commands",
125                 .pmu = "hisi_sccl,ddrc",
126         },
127         .alias_str = "event=0x2",
128         .alias_long_desc = "DDRC write commands",
129         .matching_pmu = "hisi_sccl1_ddrc2",
130 };
131
132 static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
133         .event = {
134                 .name = "unc_cbo_xsnp_response.miss_eviction",
135                 .event = "event=0x22,umask=0x81",
136                 .desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core. Unit: uncore_cbox ",
137                 .topic = "uncore",
138                 .long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
139                 .pmu = "uncore_cbox",
140         },
141         .alias_str = "event=0x22,umask=0x81",
142         .alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
143         .matching_pmu = "uncore_cbox_0",
144 };
145
146 static const struct perf_pmu_test_event uncore_hyphen = {
147         .event = {
148                 .name = "event-hyphen",
149                 .event = "event=0xe0,umask=0x00",
150                 .desc = "UNC_CBO_HYPHEN. Unit: uncore_cbox ",
151                 .topic = "uncore",
152                 .long_desc = "UNC_CBO_HYPHEN",
153                 .pmu = "uncore_cbox",
154         },
155         .alias_str = "event=0xe0,umask=0",
156         .alias_long_desc = "UNC_CBO_HYPHEN",
157         .matching_pmu = "uncore_cbox_0",
158 };
159
160 static const struct perf_pmu_test_event uncore_two_hyph = {
161         .event = {
162                 .name = "event-two-hyph",
163                 .event = "event=0xc0,umask=0x00",
164                 .desc = "UNC_CBO_TWO_HYPH. Unit: uncore_cbox ",
165                 .topic = "uncore",
166                 .long_desc = "UNC_CBO_TWO_HYPH",
167                 .pmu = "uncore_cbox",
168         },
169         .alias_str = "event=0xc0,umask=0",
170         .alias_long_desc = "UNC_CBO_TWO_HYPH",
171         .matching_pmu = "uncore_cbox_0",
172 };
173
174 static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
175         .event = {
176                 .name = "uncore_hisi_l3c.rd_hit_cpipe",
177                 .event = "event=0x7",
178                 .desc = "Total read hits. Unit: hisi_sccl,l3c ",
179                 .topic = "uncore",
180                 .long_desc = "Total read hits",
181                 .pmu = "hisi_sccl,l3c",
182         },
183         .alias_str = "event=0x7",
184         .alias_long_desc = "Total read hits",
185         .matching_pmu = "hisi_sccl3_l3c7",
186 };
187
188 static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
189         .event = {
190                 .name = "uncore_imc_free_running.cache_miss",
191                 .event = "event=0x12",
192                 .desc = "Total cache misses. Unit: uncore_imc_free_running ",
193                 .topic = "uncore",
194                 .long_desc = "Total cache misses",
195                 .pmu = "uncore_imc_free_running",
196         },
197         .alias_str = "event=0x12",
198         .alias_long_desc = "Total cache misses",
199         .matching_pmu = "uncore_imc_free_running_0",
200 };
201
202 static const struct perf_pmu_test_event uncore_imc_cache_hits = {
203         .event = {
204                 .name = "uncore_imc.cache_hits",
205                 .event = "event=0x34",
206                 .desc = "Total cache hits. Unit: uncore_imc ",
207                 .topic = "uncore",
208                 .long_desc = "Total cache hits",
209                 .pmu = "uncore_imc",
210         },
211         .alias_str = "event=0x34",
212         .alias_long_desc = "Total cache hits",
213         .matching_pmu = "uncore_imc_0",
214 };
215
216 static const struct perf_pmu_test_event *uncore_events[] = {
217         &uncore_hisi_ddrc_flux_wcmd,
218         &unc_cbo_xsnp_response_miss_eviction,
219         &uncore_hyphen,
220         &uncore_two_hyph,
221         &uncore_hisi_l3c_rd_hit_cpipe,
222         &uncore_imc_free_running_cache_miss,
223         &uncore_imc_cache_hits,
224         NULL
225 };
226
227 static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
228         .event = {
229                 .name = "sys_ddr_pmu.write_cycles",
230                 .event = "event=0x2b",
231                 .desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
232                 .topic = "uncore",
233                 .pmu = "uncore_sys_ddr_pmu",
234                 .compat = "v8",
235         },
236         .alias_str = "event=0x2b",
237         .alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
238         .matching_pmu = "uncore_sys_ddr_pmu",
239 };
240
241 static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
242         .event = {
243                 .name = "sys_ccn_pmu.read_cycles",
244                 .event = "config=0x2c",
245                 .desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
246                 .topic = "uncore",
247                 .pmu = "uncore_sys_ccn_pmu",
248                 .compat = "0x01",
249         },
250         .alias_str = "config=0x2c",
251         .alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
252         .matching_pmu = "uncore_sys_ccn_pmu",
253 };
254
255 static const struct perf_pmu_test_event *sys_events[] = {
256         &sys_ddr_pmu_write_cycles,
257         &sys_ccn_pmu_read_cycles,
258         NULL
259 };
260
261 static bool is_same(const char *reference, const char *test)
262 {
263         if (!reference && !test)
264                 return true;
265
266         if (reference && !test)
267                 return false;
268
269         if (!reference && test)
270                 return false;
271
272         return !strcmp(reference, test);
273 }
274
275 static const struct pmu_events_map *__test_pmu_get_events_map(void)
276 {
277         const struct pmu_events_map *map;
278
279         for (map = &pmu_events_map[0]; map->cpuid; map++) {
280                 if (!strcmp(map->cpuid, "testcpu"))
281                         return map;
282         }
283
284         pr_err("could not find test events map\n");
285
286         return NULL;
287 }
288
289 static const struct pmu_event *__test_pmu_get_sys_events_table(void)
290 {
291         const struct pmu_sys_events *tables = &pmu_sys_event_tables[0];
292
293         for ( ; tables->name; tables++) {
294                 if (!strcmp("pme_test_soc_sys", tables->name))
295                         return tables->table;
296         }
297
298         return NULL;
299 }
300
301 static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
302 {
303         if (!is_same(e1->name, e2->name)) {
304                 pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
305                           e1->name, e1->name, e2->name);
306                 return -1;
307         }
308
309         if (!is_same(e1->compat, e2->compat)) {
310                 pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
311                           e1->name, e1->compat, e2->compat);
312                 return -1;
313         }
314
315         if (!is_same(e1->event, e2->event)) {
316                 pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
317                           e1->name, e1->event, e2->event);
318                 return -1;
319         }
320
321         if (!is_same(e1->desc, e2->desc)) {
322                 pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
323                           e1->name, e1->desc, e2->desc);
324                 return -1;
325         }
326
327         if (!is_same(e1->topic, e2->topic)) {
328                 pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
329                           e1->name, e1->topic, e2->topic);
330                 return -1;
331         }
332
333         if (!is_same(e1->long_desc, e2->long_desc)) {
334                 pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
335                           e1->name, e1->long_desc, e2->long_desc);
336                 return -1;
337         }
338
339         if (!is_same(e1->pmu, e2->pmu)) {
340                 pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
341                           e1->name, e1->pmu, e2->pmu);
342                 return -1;
343         }
344
345         if (!is_same(e1->unit, e2->unit)) {
346                 pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
347                           e1->name, e1->unit, e2->unit);
348                 return -1;
349         }
350
351         if (!is_same(e1->perpkg, e2->perpkg)) {
352                 pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
353                           e1->name, e1->perpkg, e2->perpkg);
354                 return -1;
355         }
356
357         if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
358                 pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
359                           e1->name, e1->aggr_mode, e2->aggr_mode);
360                 return -1;
361         }
362
363         if (!is_same(e1->metric_expr, e2->metric_expr)) {
364                 pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
365                           e1->name, e1->metric_expr, e2->metric_expr);
366                 return -1;
367         }
368
369         if (!is_same(e1->metric_name, e2->metric_name)) {
370                 pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
371                           e1->name,     e1->metric_name, e2->metric_name);
372                 return -1;
373         }
374
375         if (!is_same(e1->metric_group, e2->metric_group)) {
376                 pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
377                           e1->name, e1->metric_group, e2->metric_group);
378                 return -1;
379         }
380
381         if (!is_same(e1->deprecated, e2->deprecated)) {
382                 pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
383                           e1->name, e1->deprecated, e2->deprecated);
384                 return -1;
385         }
386
387         if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
388                 pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
389                           e1->name, e1->metric_constraint, e2->metric_constraint);
390                 return -1;
391         }
392
393         return 0;
394 }
395
396 static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
397                                 struct perf_pmu_test_event const *test_event,
398                                 char const *pmu_name)
399 {
400         struct pmu_event const *event = &test_event->event;
401
402         /* An alias was found, ensure everything is in order */
403         if (!is_same(alias->name, event->name)) {
404                 pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
405                           pmu_name, alias->name, event->name);
406                 return -1;
407         }
408
409         if (!is_same(alias->desc, event->desc)) {
410                 pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
411                           pmu_name, alias->desc, event->desc);
412                 return -1;
413         }
414
415         if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
416                 pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
417                           pmu_name, alias->long_desc,
418                           test_event->alias_long_desc);
419                 return -1;
420         }
421
422         if (!is_same(alias->topic, event->topic)) {
423                 pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
424                           pmu_name, alias->topic, event->topic);
425                 return -1;
426         }
427
428         if (!is_same(alias->str, test_event->alias_str)) {
429                 pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
430                           pmu_name, alias->str, test_event->alias_str);
431                 return -1;
432         }
433
434         if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
435                 pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
436                           pmu_name, alias->str, test_event->alias_long_desc);
437                 return -1;
438         }
439
440
441         if (!is_same(alias->pmu_name, test_event->event.pmu)) {
442                 pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
443                           pmu_name, alias->pmu_name, test_event->event.pmu);
444                 return -1;
445         }
446
447         return 0;
448 }
449
450 /* Verify generated events from pmu-events.c are as expected */
451 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
452                                  int subtest __maybe_unused)
453 {
454         const struct pmu_event *sys_event_tables = __test_pmu_get_sys_events_table();
455         const struct pmu_events_map *map = __test_pmu_get_events_map();
456         const struct pmu_event *table;
457         int map_events = 0, expected_events;
458
459         /* ignore 3x sentinels */
460         expected_events = ARRAY_SIZE(core_events) +
461                           ARRAY_SIZE(uncore_events) +
462                           ARRAY_SIZE(sys_events) - 3;
463
464         if (!map || !sys_event_tables)
465                 return -1;
466
467         for (table = map->table; table->name; table++) {
468                 struct perf_pmu_test_event const **test_event_table;
469                 bool found = false;
470
471                 if (table->pmu)
472                         test_event_table = &uncore_events[0];
473                 else
474                         test_event_table = &core_events[0];
475
476                 for (; *test_event_table; test_event_table++) {
477                         struct perf_pmu_test_event const *test_event = *test_event_table;
478                         struct pmu_event const *event = &test_event->event;
479
480                         if (strcmp(table->name, event->name))
481                                 continue;
482                         found = true;
483                         map_events++;
484
485                         if (compare_pmu_events(table, event))
486                                 return -1;
487
488                         pr_debug("testing event table %s: pass\n", table->name);
489                 }
490
491                 if (!found) {
492                         pr_err("testing event table: could not find event %s\n",
493                                table->name);
494                         return -1;
495                 }
496         }
497
498         for (table = sys_event_tables; table->name; table++) {
499                 struct perf_pmu_test_event const **test_event_table;
500                 bool found = false;
501
502                 test_event_table = &sys_events[0];
503
504                 for (; *test_event_table; test_event_table++) {
505                         struct perf_pmu_test_event const *test_event = *test_event_table;
506                         struct pmu_event const *event = &test_event->event;
507
508                         if (strcmp(table->name, event->name))
509                                 continue;
510                         found = true;
511                         map_events++;
512
513                         if (compare_pmu_events(table, event))
514                                 return -1;
515
516                         pr_debug("testing sys event table %s: pass\n", table->name);
517                 }
518                 if (!found) {
519                         pr_debug("testing event table: could not find event %s\n",
520                                    table->name);
521                         return -1;
522                 }
523         }
524
525         if (map_events != expected_events) {
526                 pr_err("testing event table: found %d, but expected %d\n",
527                        map_events, expected_events);
528                 return -1;
529         }
530
531         return 0;
532 }
533
534 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
535 {
536         struct perf_pmu_alias *alias;
537
538         list_for_each_entry(alias, aliases, list)
539                 if (!strcmp(test_event, alias->name))
540                         return alias;
541
542         return NULL;
543 }
544
545 /* Verify aliases are as expected */
546 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
547 {
548         struct perf_pmu_test_event const **test_event_table;
549         struct perf_pmu *pmu;
550         LIST_HEAD(aliases);
551         int res = 0;
552         const struct pmu_events_map *map = __test_pmu_get_events_map();
553         struct perf_pmu_alias *a, *tmp;
554
555         if (!map)
556                 return -1;
557
558         test_event_table = &core_events[0];
559
560         pmu = zalloc(sizeof(*pmu));
561         if (!pmu)
562                 return -1;
563
564         pmu->name = pmu_name;
565
566         pmu_add_cpu_aliases_map(&aliases, pmu, map);
567
568         for (; *test_event_table; test_event_table++) {
569                 struct perf_pmu_test_event const *test_event = *test_event_table;
570                 struct pmu_event const *event = &test_event->event;
571                 struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
572
573                 if (!alias) {
574                         pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
575                                   pmu_name, event->name);
576                         res = -1;
577                         break;
578                 }
579
580                 if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
581                         res = -1;
582                         break;
583                 }
584
585                 (*count)++;
586                 pr_debug2("testing aliases core PMU %s: matched event %s\n",
587                           pmu_name, alias->name);
588         }
589
590         list_for_each_entry_safe(a, tmp, &aliases, list) {
591                 list_del(&a->list);
592                 perf_pmu_free_alias(a);
593         }
594         free(pmu);
595         return res;
596 }
597
598 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
599 {
600         int alias_count = 0, to_match_count = 0, matched_count = 0;
601         struct perf_pmu_test_event const **table;
602         struct perf_pmu *pmu = &test_pmu->pmu;
603         const char *pmu_name = pmu->name;
604         struct perf_pmu_alias *a, *tmp, *alias;
605         const struct pmu_events_map *map;
606         LIST_HEAD(aliases);
607         int res = 0;
608
609         map = __test_pmu_get_events_map();
610         if (!map)
611                 return -1;
612         pmu_add_cpu_aliases_map(&aliases, pmu, map);
613         pmu_add_sys_aliases(&aliases, pmu);
614
615         /* Count how many aliases we generated */
616         list_for_each_entry(alias, &aliases, list)
617                 alias_count++;
618
619         /* Count how many aliases we expect from the known table */
620         for (table = &test_pmu->aliases[0]; *table; table++)
621                 to_match_count++;
622
623         if (alias_count != to_match_count) {
624                 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
625                          pmu_name, to_match_count, alias_count);
626                 res = -1;
627                 goto out;
628         }
629
630         list_for_each_entry(alias, &aliases, list) {
631                 bool matched = false;
632
633                 for (table = &test_pmu->aliases[0]; *table; table++) {
634                         struct perf_pmu_test_event const *test_event = *table;
635                         struct pmu_event const *event = &test_event->event;
636
637                         if (!strcmp(event->name, alias->name)) {
638                                 if (compare_alias_to_test_event(alias,
639                                                         test_event,
640                                                         pmu_name)) {
641                                         continue;
642                                 }
643                                 matched = true;
644                                 matched_count++;
645                         }
646                 }
647
648                 if (matched == false) {
649                         pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
650                                  pmu_name, alias->name);
651                         res = -1;
652                         goto out;
653                 }
654         }
655
656         if (alias_count != matched_count) {
657                 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
658                          pmu_name, matched_count, alias_count);
659                 res = -1;
660         }
661
662 out:
663         list_for_each_entry_safe(a, tmp, &aliases, list) {
664                 list_del(&a->list);
665                 perf_pmu_free_alias(a);
666         }
667         return res;
668 }
669
670 static struct perf_pmu_test_pmu test_pmus[] = {
671         {
672                 .pmu = {
673                         .name = (char *)"hisi_sccl1_ddrc2",
674                         .is_uncore = 1,
675                 },
676                 .aliases = {
677                         &uncore_hisi_ddrc_flux_wcmd,
678                 },
679         },
680         {
681                 .pmu = {
682                         .name = (char *)"uncore_cbox_0",
683                         .is_uncore = 1,
684                 },
685                 .aliases = {
686                         &unc_cbo_xsnp_response_miss_eviction,
687                         &uncore_hyphen,
688                         &uncore_two_hyph,
689                 },
690         },
691         {
692                 .pmu = {
693                         .name = (char *)"hisi_sccl3_l3c7",
694                         .is_uncore = 1,
695                 },
696                 .aliases = {
697                         &uncore_hisi_l3c_rd_hit_cpipe,
698                 },
699         },
700         {
701                 .pmu = {
702                         .name = (char *)"uncore_imc_free_running_0",
703                         .is_uncore = 1,
704                 },
705                 .aliases = {
706                         &uncore_imc_free_running_cache_miss,
707                 },
708         },
709         {
710                 .pmu = {
711                         .name = (char *)"uncore_imc_0",
712                         .is_uncore = 1,
713                 },
714                 .aliases = {
715                         &uncore_imc_cache_hits,
716                 },
717         },
718         {
719                 .pmu = {
720                         .name = (char *)"uncore_sys_ddr_pmu0",
721                         .is_uncore = 1,
722                         .id = (char *)"v8",
723                 },
724                 .aliases = {
725                         &sys_ddr_pmu_write_cycles,
726                 },
727         },
728         {
729                 .pmu = {
730                         .name = (char *)"uncore_sys_ccn_pmu4",
731                         .is_uncore = 1,
732                         .id = (char *)"0x01",
733                 },
734                 .aliases = {
735                         &sys_ccn_pmu_read_cycles,
736                 },
737         },
738 };
739
740 /* Test that aliases generated are as expected */
741 static int test__aliases(struct test_suite *test __maybe_unused,
742                         int subtest __maybe_unused)
743 {
744         struct perf_pmu *pmu = NULL;
745         unsigned long i;
746
747         while ((pmu = perf_pmu__scan(pmu)) != NULL) {
748                 int count = 0;
749
750                 if (!is_pmu_core(pmu->name))
751                         continue;
752
753                 if (list_empty(&pmu->format)) {
754                         pr_debug2("skipping testing core PMU %s\n", pmu->name);
755                         continue;
756                 }
757
758                 if (__test_core_pmu_event_aliases(pmu->name, &count)) {
759                         pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
760                         return -1;
761                 }
762
763                 if (count == 0) {
764                         pr_debug("testing core PMU %s aliases: no events to match\n",
765                                   pmu->name);
766                         return -1;
767                 }
768
769                 pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
770         }
771
772         for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
773                 int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
774
775                 if (res)
776                         return res;
777         }
778
779         return 0;
780 }
781
782 static bool is_number(const char *str)
783 {
784         char *end_ptr;
785         double v;
786
787         errno = 0;
788         v = strtod(str, &end_ptr);
789         (void)v; // We're not interested in this value, only if it is valid
790         return errno == 0 && end_ptr != str;
791 }
792
793 static int check_parse_id(const char *id, struct parse_events_error *error,
794                           struct perf_pmu *fake_pmu)
795 {
796         struct evlist *evlist;
797         int ret;
798         char *dup, *cur;
799
800         /* Numbers are always valid. */
801         if (is_number(id))
802                 return 0;
803
804         evlist = evlist__new();
805         if (!evlist)
806                 return -ENOMEM;
807
808         dup = strdup(id);
809         if (!dup)
810                 return -ENOMEM;
811
812         for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
813                 *cur = '/';
814
815         if (fake_pmu) {
816                 /*
817                  * Every call to __parse_events will try to initialize the PMU
818                  * state from sysfs and then clean it up at the end. Reset the
819                  * PMU events to the test state so that we don't pick up
820                  * erroneous prefixes and suffixes.
821                  */
822                 perf_pmu__test_parse_init();
823         }
824         ret = __parse_events(evlist, dup, error, fake_pmu);
825         free(dup);
826
827         evlist__delete(evlist);
828         return ret;
829 }
830
831 static int check_parse_cpu(const char *id, bool same_cpu, const struct pmu_event *pe)
832 {
833         struct parse_events_error error;
834         int ret;
835
836         parse_events_error__init(&error);
837         ret = check_parse_id(id, &error, NULL);
838         if (ret && same_cpu) {
839                 pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
840                         pe->metric_name, id, pe->metric_expr);
841                 pr_warning("Error string '%s' help '%s'\n", error.str,
842                         error.help);
843         } else if (ret) {
844                 pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
845                           id, pe->metric_name, pe->metric_expr);
846                 ret = 0;
847         }
848         parse_events_error__exit(&error);
849         return ret;
850 }
851
852 static int check_parse_fake(const char *id)
853 {
854         struct parse_events_error error;
855         int ret;
856
857         parse_events_error__init(&error);
858         ret = check_parse_id(id, &error, &perf_pmu__fake);
859         parse_events_error__exit(&error);
860         return ret;
861 }
862
863 static void expr_failure(const char *msg,
864                          const struct pmu_events_map *map,
865                          const struct pmu_event *pe)
866 {
867         pr_debug("%s for map %s %s %s\n",
868                 msg, map->cpuid, map->version, map->type);
869         pr_debug("On metric %s\n", pe->metric_name);
870         pr_debug("On expression %s\n", pe->metric_expr);
871 }
872
873 struct metric {
874         struct list_head list;
875         struct metric_ref metric_ref;
876 };
877
878 static int resolve_metric_simple(struct expr_parse_ctx *pctx,
879                                  struct list_head *compound_list,
880                                  const struct pmu_events_map *map,
881                                  const char *metric_name)
882 {
883         struct hashmap_entry *cur, *cur_tmp;
884         struct metric *metric, *tmp;
885         size_t bkt;
886         bool all;
887         int rc;
888
889         do {
890                 all = true;
891                 hashmap__for_each_entry_safe(pctx->ids, cur, cur_tmp, bkt) {
892                         struct metric_ref *ref;
893                         const struct pmu_event *pe;
894
895                         pe = metricgroup__find_metric(cur->key, map);
896                         if (!pe)
897                                 continue;
898
899                         if (!strcmp(metric_name, (char *)cur->key)) {
900                                 pr_warning("Recursion detected for metric %s\n", metric_name);
901                                 rc = -1;
902                                 goto out_err;
903                         }
904
905                         all = false;
906
907                         /* The metric key itself needs to go out.. */
908                         expr__del_id(pctx, cur->key);
909
910                         metric = malloc(sizeof(*metric));
911                         if (!metric) {
912                                 rc = -ENOMEM;
913                                 goto out_err;
914                         }
915
916                         ref = &metric->metric_ref;
917                         ref->metric_name = pe->metric_name;
918                         ref->metric_expr = pe->metric_expr;
919                         list_add_tail(&metric->list, compound_list);
920
921                         rc = expr__find_ids(pe->metric_expr, NULL, pctx);
922                         if (rc)
923                                 goto out_err;
924                         break; /* The hashmap has been modified, so restart */
925                 }
926         } while (!all);
927
928         return 0;
929
930 out_err:
931         list_for_each_entry_safe(metric, tmp, compound_list, list)
932                 free(metric);
933
934         return rc;
935
936 }
937
938 static int test__parsing(struct test_suite *test __maybe_unused,
939                          int subtest __maybe_unused)
940 {
941         const struct pmu_events_map *cpus_map = pmu_events_map__find();
942         const struct pmu_events_map *map;
943         const struct pmu_event *pe;
944         int i, j, k;
945         int ret = 0;
946         struct expr_parse_ctx *ctx;
947         double result;
948
949         ctx = expr__ctx_new();
950         if (!ctx) {
951                 pr_debug("expr__ctx_new failed");
952                 return TEST_FAIL;
953         }
954         i = 0;
955         for (;;) {
956                 map = &pmu_events_map[i++];
957                 if (!map->table)
958                         break;
959                 j = 0;
960                 for (;;) {
961                         struct metric *metric, *tmp;
962                         struct hashmap_entry *cur;
963                         LIST_HEAD(compound_list);
964                         size_t bkt;
965
966                         pe = &map->table[j++];
967                         if (!pe->name && !pe->metric_group && !pe->metric_name)
968                                 break;
969                         if (!pe->metric_expr)
970                                 continue;
971                         expr__ctx_clear(ctx);
972                         if (expr__find_ids(pe->metric_expr, NULL, ctx) < 0) {
973                                 expr_failure("Parse find ids failed", map, pe);
974                                 ret++;
975                                 continue;
976                         }
977
978                         if (resolve_metric_simple(ctx, &compound_list, map,
979                                                   pe->metric_name)) {
980                                 expr_failure("Could not resolve metrics", map, pe);
981                                 ret++;
982                                 goto exit; /* Don't tolerate errors due to severity */
983                         }
984
985                         /*
986                          * Add all ids with a made up value. The value may
987                          * trigger divide by zero when subtracted and so try to
988                          * make them unique.
989                          */
990                         k = 1;
991                         hashmap__for_each_entry(ctx->ids, cur, bkt)
992                                 expr__add_id_val(ctx, strdup(cur->key), k++);
993
994                         hashmap__for_each_entry(ctx->ids, cur, bkt) {
995                                 if (check_parse_cpu(cur->key, map == cpus_map,
996                                                    pe))
997                                         ret++;
998                         }
999
1000                         list_for_each_entry_safe(metric, tmp, &compound_list, list) {
1001                                 expr__add_ref(ctx, &metric->metric_ref);
1002                                 free(metric);
1003                         }
1004
1005                         if (expr__parse(&result, ctx, pe->metric_expr)) {
1006                                 /*
1007                                  * Parsing failed, make numbers go from large to
1008                                  * small which can resolve divide by zero
1009                                  * issues.
1010                                  */
1011                                 k = 1024;
1012                                 hashmap__for_each_entry(ctx->ids, cur, bkt)
1013                                         expr__add_id_val(ctx, strdup(cur->key), k--);
1014                                 if (expr__parse(&result, ctx, pe->metric_expr)) {
1015                                         expr_failure("Parse failed", map, pe);
1016                                         ret++;
1017                                 }
1018                         }
1019                 }
1020         }
1021         expr__ctx_free(ctx);
1022         /* TODO: fail when not ok */
1023 exit:
1024         return ret == 0 ? TEST_OK : TEST_SKIP;
1025 }
1026
1027 struct test_metric {
1028         const char *str;
1029 };
1030
1031 static struct test_metric metrics[] = {
1032         { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
1033         { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
1034         { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
1035         { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
1036         { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
1037 };
1038
1039 static int metric_parse_fake(const char *str)
1040 {
1041         struct expr_parse_ctx *ctx;
1042         struct hashmap_entry *cur;
1043         double result;
1044         int ret = -1;
1045         size_t bkt;
1046         int i;
1047
1048         pr_debug("parsing '%s'\n", str);
1049
1050         ctx = expr__ctx_new();
1051         if (!ctx) {
1052                 pr_debug("expr__ctx_new failed");
1053                 return TEST_FAIL;
1054         }
1055         if (expr__find_ids(str, NULL, ctx) < 0) {
1056                 pr_err("expr__find_ids failed\n");
1057                 return -1;
1058         }
1059
1060         /*
1061          * Add all ids with a made up value. The value may
1062          * trigger divide by zero when subtracted and so try to
1063          * make them unique.
1064          */
1065         i = 1;
1066         hashmap__for_each_entry(ctx->ids, cur, bkt)
1067                 expr__add_id_val(ctx, strdup(cur->key), i++);
1068
1069         hashmap__for_each_entry(ctx->ids, cur, bkt) {
1070                 if (check_parse_fake(cur->key)) {
1071                         pr_err("check_parse_fake failed\n");
1072                         goto out;
1073                 }
1074         }
1075
1076         ret = 0;
1077         if (expr__parse(&result, ctx, str)) {
1078                 /*
1079                  * Parsing failed, make numbers go from large to small which can
1080                  * resolve divide by zero issues.
1081                  */
1082                 i = 1024;
1083                 hashmap__for_each_entry(ctx->ids, cur, bkt)
1084                         expr__add_id_val(ctx, strdup(cur->key), i--);
1085                 if (expr__parse(&result, ctx, str)) {
1086                         pr_err("expr__parse failed\n");
1087                         ret = -1;
1088                 }
1089         }
1090
1091 out:
1092         expr__ctx_free(ctx);
1093         return ret;
1094 }
1095
1096 /*
1097  * Parse all the metrics for current architecture,
1098  * or all defined cpus via the 'fake_pmu'
1099  * in parse_events.
1100  */
1101 static int test__parsing_fake(struct test_suite *test __maybe_unused,
1102                               int subtest __maybe_unused)
1103 {
1104         const struct pmu_events_map *map;
1105         const struct pmu_event *pe;
1106         unsigned int i, j;
1107         int err = 0;
1108
1109         for (i = 0; i < ARRAY_SIZE(metrics); i++) {
1110                 err = metric_parse_fake(metrics[i].str);
1111                 if (err)
1112                         return err;
1113         }
1114
1115         i = 0;
1116         for (;;) {
1117                 map = &pmu_events_map[i++];
1118                 if (!map->table)
1119                         break;
1120                 j = 0;
1121                 for (;;) {
1122                         pe = &map->table[j++];
1123                         if (!pe->name && !pe->metric_group && !pe->metric_name)
1124                                 break;
1125                         if (!pe->metric_expr)
1126                                 continue;
1127                         pr_debug("Found metric '%s' for '%s'\n", pe->metric_name, map->cpuid);
1128                         err = metric_parse_fake(pe->metric_expr);
1129                         if (err)
1130                                 return err;
1131                 }
1132         }
1133
1134         return 0;
1135 }
1136
1137 static struct test_case pmu_events_tests[] = {
1138         TEST_CASE("PMU event table sanity", pmu_event_table),
1139         TEST_CASE("PMU event map aliases", aliases),
1140         TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1141                          "some metrics failed"),
1142         TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1143         { .name = NULL, }
1144 };
1145
1146 struct test_suite suite__pmu_events = {
1147         .desc = "PMU events",
1148         .test_cases = pmu_events_tests,
1149 };