perf pmu-events: Hide pmu_events_map
[linux-2.6-microblaze.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 int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
276 {
277         if (!is_same(e1->name, e2->name)) {
278                 pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
279                           e1->name, e1->name, e2->name);
280                 return -1;
281         }
282
283         if (!is_same(e1->compat, e2->compat)) {
284                 pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
285                           e1->name, e1->compat, e2->compat);
286                 return -1;
287         }
288
289         if (!is_same(e1->event, e2->event)) {
290                 pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
291                           e1->name, e1->event, e2->event);
292                 return -1;
293         }
294
295         if (!is_same(e1->desc, e2->desc)) {
296                 pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
297                           e1->name, e1->desc, e2->desc);
298                 return -1;
299         }
300
301         if (!is_same(e1->topic, e2->topic)) {
302                 pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
303                           e1->name, e1->topic, e2->topic);
304                 return -1;
305         }
306
307         if (!is_same(e1->long_desc, e2->long_desc)) {
308                 pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
309                           e1->name, e1->long_desc, e2->long_desc);
310                 return -1;
311         }
312
313         if (!is_same(e1->pmu, e2->pmu)) {
314                 pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
315                           e1->name, e1->pmu, e2->pmu);
316                 return -1;
317         }
318
319         if (!is_same(e1->unit, e2->unit)) {
320                 pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
321                           e1->name, e1->unit, e2->unit);
322                 return -1;
323         }
324
325         if (!is_same(e1->perpkg, e2->perpkg)) {
326                 pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
327                           e1->name, e1->perpkg, e2->perpkg);
328                 return -1;
329         }
330
331         if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
332                 pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
333                           e1->name, e1->aggr_mode, e2->aggr_mode);
334                 return -1;
335         }
336
337         if (!is_same(e1->metric_expr, e2->metric_expr)) {
338                 pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
339                           e1->name, e1->metric_expr, e2->metric_expr);
340                 return -1;
341         }
342
343         if (!is_same(e1->metric_name, e2->metric_name)) {
344                 pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
345                           e1->name,     e1->metric_name, e2->metric_name);
346                 return -1;
347         }
348
349         if (!is_same(e1->metric_group, e2->metric_group)) {
350                 pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
351                           e1->name, e1->metric_group, e2->metric_group);
352                 return -1;
353         }
354
355         if (!is_same(e1->deprecated, e2->deprecated)) {
356                 pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
357                           e1->name, e1->deprecated, e2->deprecated);
358                 return -1;
359         }
360
361         if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
362                 pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
363                           e1->name, e1->metric_constraint, e2->metric_constraint);
364                 return -1;
365         }
366
367         return 0;
368 }
369
370 static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
371                                 struct perf_pmu_test_event const *test_event,
372                                 char const *pmu_name)
373 {
374         struct pmu_event const *event = &test_event->event;
375
376         /* An alias was found, ensure everything is in order */
377         if (!is_same(alias->name, event->name)) {
378                 pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
379                           pmu_name, alias->name, event->name);
380                 return -1;
381         }
382
383         if (!is_same(alias->desc, event->desc)) {
384                 pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
385                           pmu_name, alias->desc, event->desc);
386                 return -1;
387         }
388
389         if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
390                 pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
391                           pmu_name, alias->long_desc,
392                           test_event->alias_long_desc);
393                 return -1;
394         }
395
396         if (!is_same(alias->topic, event->topic)) {
397                 pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
398                           pmu_name, alias->topic, event->topic);
399                 return -1;
400         }
401
402         if (!is_same(alias->str, test_event->alias_str)) {
403                 pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
404                           pmu_name, alias->str, test_event->alias_str);
405                 return -1;
406         }
407
408         if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
409                 pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
410                           pmu_name, alias->str, test_event->alias_long_desc);
411                 return -1;
412         }
413
414
415         if (!is_same(alias->pmu_name, test_event->event.pmu)) {
416                 pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
417                           pmu_name, alias->pmu_name, test_event->event.pmu);
418                 return -1;
419         }
420
421         return 0;
422 }
423
424 /* Verify generated events from pmu-events.c are as expected */
425 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
426                                  int subtest __maybe_unused)
427 {
428         const struct pmu_event *sys_event_tables = find_sys_events_table("pme_test_soc_sys");
429         const struct pmu_event *table = find_core_events_table("testarch", "testcpu");
430         int map_events = 0, expected_events;
431
432         /* ignore 3x sentinels */
433         expected_events = ARRAY_SIZE(core_events) +
434                           ARRAY_SIZE(uncore_events) +
435                           ARRAY_SIZE(sys_events) - 3;
436
437         if (!table || !sys_event_tables)
438                 return -1;
439
440         for (; table->name; table++) {
441                 struct perf_pmu_test_event const **test_event_table;
442                 bool found = false;
443
444                 if (table->pmu)
445                         test_event_table = &uncore_events[0];
446                 else
447                         test_event_table = &core_events[0];
448
449                 for (; *test_event_table; test_event_table++) {
450                         struct perf_pmu_test_event const *test_event = *test_event_table;
451                         struct pmu_event const *event = &test_event->event;
452
453                         if (strcmp(table->name, event->name))
454                                 continue;
455                         found = true;
456                         map_events++;
457
458                         if (compare_pmu_events(table, event))
459                                 return -1;
460
461                         pr_debug("testing event table %s: pass\n", table->name);
462                 }
463
464                 if (!found) {
465                         pr_err("testing event table: could not find event %s\n",
466                                table->name);
467                         return -1;
468                 }
469         }
470
471         for (table = sys_event_tables; table->name; table++) {
472                 struct perf_pmu_test_event const **test_event_table;
473                 bool found = false;
474
475                 test_event_table = &sys_events[0];
476
477                 for (; *test_event_table; test_event_table++) {
478                         struct perf_pmu_test_event const *test_event = *test_event_table;
479                         struct pmu_event const *event = &test_event->event;
480
481                         if (strcmp(table->name, event->name))
482                                 continue;
483                         found = true;
484                         map_events++;
485
486                         if (compare_pmu_events(table, event))
487                                 return -1;
488
489                         pr_debug("testing sys event table %s: pass\n", table->name);
490                 }
491                 if (!found) {
492                         pr_debug("testing event table: could not find event %s\n",
493                                    table->name);
494                         return -1;
495                 }
496         }
497
498         if (map_events != expected_events) {
499                 pr_err("testing event table: found %d, but expected %d\n",
500                        map_events, expected_events);
501                 return -1;
502         }
503
504         return 0;
505 }
506
507 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
508 {
509         struct perf_pmu_alias *alias;
510
511         list_for_each_entry(alias, aliases, list)
512                 if (!strcmp(test_event, alias->name))
513                         return alias;
514
515         return NULL;
516 }
517
518 /* Verify aliases are as expected */
519 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
520 {
521         struct perf_pmu_test_event const **test_event_table;
522         struct perf_pmu *pmu;
523         LIST_HEAD(aliases);
524         int res = 0;
525         const struct pmu_event *table = find_core_events_table("testarch", "testcpu");
526         struct perf_pmu_alias *a, *tmp;
527
528         if (!table)
529                 return -1;
530
531         test_event_table = &core_events[0];
532
533         pmu = zalloc(sizeof(*pmu));
534         if (!pmu)
535                 return -1;
536
537         pmu->name = pmu_name;
538
539         pmu_add_cpu_aliases_table(&aliases, pmu, table);
540
541         for (; *test_event_table; test_event_table++) {
542                 struct perf_pmu_test_event const *test_event = *test_event_table;
543                 struct pmu_event const *event = &test_event->event;
544                 struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
545
546                 if (!alias) {
547                         pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
548                                   pmu_name, event->name);
549                         res = -1;
550                         break;
551                 }
552
553                 if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
554                         res = -1;
555                         break;
556                 }
557
558                 (*count)++;
559                 pr_debug2("testing aliases core PMU %s: matched event %s\n",
560                           pmu_name, alias->name);
561         }
562
563         list_for_each_entry_safe(a, tmp, &aliases, list) {
564                 list_del(&a->list);
565                 perf_pmu_free_alias(a);
566         }
567         free(pmu);
568         return res;
569 }
570
571 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
572 {
573         int alias_count = 0, to_match_count = 0, matched_count = 0;
574         struct perf_pmu_test_event const **table;
575         struct perf_pmu *pmu = &test_pmu->pmu;
576         const char *pmu_name = pmu->name;
577         struct perf_pmu_alias *a, *tmp, *alias;
578         const struct pmu_event *events_table;
579         LIST_HEAD(aliases);
580         int res = 0;
581
582         events_table = find_core_events_table("testarch", "testcpu");
583         if (!events_table)
584                 return -1;
585         pmu_add_cpu_aliases_table(&aliases, pmu, events_table);
586         pmu_add_sys_aliases(&aliases, pmu);
587
588         /* Count how many aliases we generated */
589         list_for_each_entry(alias, &aliases, list)
590                 alias_count++;
591
592         /* Count how many aliases we expect from the known table */
593         for (table = &test_pmu->aliases[0]; *table; table++)
594                 to_match_count++;
595
596         if (alias_count != to_match_count) {
597                 pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
598                          pmu_name, to_match_count, alias_count);
599                 res = -1;
600                 goto out;
601         }
602
603         list_for_each_entry(alias, &aliases, list) {
604                 bool matched = false;
605
606                 for (table = &test_pmu->aliases[0]; *table; table++) {
607                         struct perf_pmu_test_event const *test_event = *table;
608                         struct pmu_event const *event = &test_event->event;
609
610                         if (!strcmp(event->name, alias->name)) {
611                                 if (compare_alias_to_test_event(alias,
612                                                         test_event,
613                                                         pmu_name)) {
614                                         continue;
615                                 }
616                                 matched = true;
617                                 matched_count++;
618                         }
619                 }
620
621                 if (matched == false) {
622                         pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
623                                  pmu_name, alias->name);
624                         res = -1;
625                         goto out;
626                 }
627         }
628
629         if (alias_count != matched_count) {
630                 pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
631                          pmu_name, matched_count, alias_count);
632                 res = -1;
633         }
634
635 out:
636         list_for_each_entry_safe(a, tmp, &aliases, list) {
637                 list_del(&a->list);
638                 perf_pmu_free_alias(a);
639         }
640         return res;
641 }
642
643 static struct perf_pmu_test_pmu test_pmus[] = {
644         {
645                 .pmu = {
646                         .name = (char *)"hisi_sccl1_ddrc2",
647                         .is_uncore = 1,
648                 },
649                 .aliases = {
650                         &uncore_hisi_ddrc_flux_wcmd,
651                 },
652         },
653         {
654                 .pmu = {
655                         .name = (char *)"uncore_cbox_0",
656                         .is_uncore = 1,
657                 },
658                 .aliases = {
659                         &unc_cbo_xsnp_response_miss_eviction,
660                         &uncore_hyphen,
661                         &uncore_two_hyph,
662                 },
663         },
664         {
665                 .pmu = {
666                         .name = (char *)"hisi_sccl3_l3c7",
667                         .is_uncore = 1,
668                 },
669                 .aliases = {
670                         &uncore_hisi_l3c_rd_hit_cpipe,
671                 },
672         },
673         {
674                 .pmu = {
675                         .name = (char *)"uncore_imc_free_running_0",
676                         .is_uncore = 1,
677                 },
678                 .aliases = {
679                         &uncore_imc_free_running_cache_miss,
680                 },
681         },
682         {
683                 .pmu = {
684                         .name = (char *)"uncore_imc_0",
685                         .is_uncore = 1,
686                 },
687                 .aliases = {
688                         &uncore_imc_cache_hits,
689                 },
690         },
691         {
692                 .pmu = {
693                         .name = (char *)"uncore_sys_ddr_pmu0",
694                         .is_uncore = 1,
695                         .id = (char *)"v8",
696                 },
697                 .aliases = {
698                         &sys_ddr_pmu_write_cycles,
699                 },
700         },
701         {
702                 .pmu = {
703                         .name = (char *)"uncore_sys_ccn_pmu4",
704                         .is_uncore = 1,
705                         .id = (char *)"0x01",
706                 },
707                 .aliases = {
708                         &sys_ccn_pmu_read_cycles,
709                 },
710         },
711 };
712
713 /* Test that aliases generated are as expected */
714 static int test__aliases(struct test_suite *test __maybe_unused,
715                         int subtest __maybe_unused)
716 {
717         struct perf_pmu *pmu = NULL;
718         unsigned long i;
719
720         while ((pmu = perf_pmu__scan(pmu)) != NULL) {
721                 int count = 0;
722
723                 if (!is_pmu_core(pmu->name))
724                         continue;
725
726                 if (list_empty(&pmu->format)) {
727                         pr_debug2("skipping testing core PMU %s\n", pmu->name);
728                         continue;
729                 }
730
731                 if (__test_core_pmu_event_aliases(pmu->name, &count)) {
732                         pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
733                         return -1;
734                 }
735
736                 if (count == 0) {
737                         pr_debug("testing core PMU %s aliases: no events to match\n",
738                                   pmu->name);
739                         return -1;
740                 }
741
742                 pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
743         }
744
745         for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
746                 int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
747
748                 if (res)
749                         return res;
750         }
751
752         return 0;
753 }
754
755 static bool is_number(const char *str)
756 {
757         char *end_ptr;
758         double v;
759
760         errno = 0;
761         v = strtod(str, &end_ptr);
762         (void)v; // We're not interested in this value, only if it is valid
763         return errno == 0 && end_ptr != str;
764 }
765
766 static int check_parse_id(const char *id, struct parse_events_error *error,
767                           struct perf_pmu *fake_pmu)
768 {
769         struct evlist *evlist;
770         int ret;
771         char *dup, *cur;
772
773         /* Numbers are always valid. */
774         if (is_number(id))
775                 return 0;
776
777         evlist = evlist__new();
778         if (!evlist)
779                 return -ENOMEM;
780
781         dup = strdup(id);
782         if (!dup)
783                 return -ENOMEM;
784
785         for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
786                 *cur = '/';
787
788         if (fake_pmu) {
789                 /*
790                  * Every call to __parse_events will try to initialize the PMU
791                  * state from sysfs and then clean it up at the end. Reset the
792                  * PMU events to the test state so that we don't pick up
793                  * erroneous prefixes and suffixes.
794                  */
795                 perf_pmu__test_parse_init();
796         }
797         ret = __parse_events(evlist, dup, error, fake_pmu);
798         free(dup);
799
800         evlist__delete(evlist);
801         return ret;
802 }
803
804 static int check_parse_cpu(const char *id, bool same_cpu, const struct pmu_event *pe)
805 {
806         struct parse_events_error error;
807         int ret;
808
809         parse_events_error__init(&error);
810         ret = check_parse_id(id, &error, NULL);
811         if (ret && same_cpu) {
812                 pr_warning("Parse event failed metric '%s' id '%s' expr '%s'\n",
813                         pe->metric_name, id, pe->metric_expr);
814                 pr_warning("Error string '%s' help '%s'\n", error.str,
815                         error.help);
816         } else if (ret) {
817                 pr_debug3("Parse event failed, but for an event that may not be supported by this CPU.\nid '%s' metric '%s' expr '%s'\n",
818                           id, pe->metric_name, pe->metric_expr);
819                 ret = 0;
820         }
821         parse_events_error__exit(&error);
822         return ret;
823 }
824
825 static int check_parse_fake(const char *id)
826 {
827         struct parse_events_error error;
828         int ret;
829
830         parse_events_error__init(&error);
831         ret = check_parse_id(id, &error, &perf_pmu__fake);
832         parse_events_error__exit(&error);
833         return ret;
834 }
835
836 struct metric {
837         struct list_head list;
838         struct metric_ref metric_ref;
839 };
840
841 static int resolve_metric_simple(struct expr_parse_ctx *pctx,
842                                  struct list_head *compound_list,
843                                  const struct pmu_event *map,
844                                  const char *metric_name)
845 {
846         struct hashmap_entry *cur, *cur_tmp;
847         struct metric *metric, *tmp;
848         size_t bkt;
849         bool all;
850         int rc;
851
852         do {
853                 all = true;
854                 hashmap__for_each_entry_safe(pctx->ids, cur, cur_tmp, bkt) {
855                         struct metric_ref *ref;
856                         const struct pmu_event *pe;
857
858                         pe = metricgroup__find_metric(cur->key, map);
859                         if (!pe)
860                                 continue;
861
862                         if (!strcmp(metric_name, (char *)cur->key)) {
863                                 pr_warning("Recursion detected for metric %s\n", metric_name);
864                                 rc = -1;
865                                 goto out_err;
866                         }
867
868                         all = false;
869
870                         /* The metric key itself needs to go out.. */
871                         expr__del_id(pctx, cur->key);
872
873                         metric = malloc(sizeof(*metric));
874                         if (!metric) {
875                                 rc = -ENOMEM;
876                                 goto out_err;
877                         }
878
879                         ref = &metric->metric_ref;
880                         ref->metric_name = pe->metric_name;
881                         ref->metric_expr = pe->metric_expr;
882                         list_add_tail(&metric->list, compound_list);
883
884                         rc = expr__find_ids(pe->metric_expr, NULL, pctx);
885                         if (rc)
886                                 goto out_err;
887                         break; /* The hashmap has been modified, so restart */
888                 }
889         } while (!all);
890
891         return 0;
892
893 out_err:
894         list_for_each_entry_safe(metric, tmp, compound_list, list)
895                 free(metric);
896
897         return rc;
898
899 }
900
901 static void expr_failure(const char *msg, const struct pmu_event *pe)
902 {
903         pr_debug("%s\nOn metric %s\nOn expression %s\n", msg, pe->metric_name, pe->metric_expr);
904 }
905
906
907 struct test__parsing_data {
908         const struct pmu_event *cpus_table;
909         struct expr_parse_ctx *ctx;
910         int failures;
911 };
912
913 static int test__parsing_callback(const struct pmu_event *pe, const struct pmu_event *table,
914                                   void *vdata)
915 {
916         struct test__parsing_data *data = vdata;
917         struct metric *metric, *tmp;
918         struct hashmap_entry *cur;
919         LIST_HEAD(compound_list);
920         size_t bkt;
921         int k;
922         double result;
923
924         if (!pe->metric_expr)
925                 return 0;
926
927         pr_debug("Found metric '%s'\n", pe->metric_name);
928
929         expr__ctx_clear(data->ctx);
930         if (expr__find_ids(pe->metric_expr, NULL, data->ctx) < 0) {
931                 expr_failure("Parse find ids failed", pe);
932                 data->failures++;
933                 return 0;
934         }
935
936         if (resolve_metric_simple(data->ctx, &compound_list, table,
937                                   pe->metric_name)) {
938                 expr_failure("Could not resolve metrics", pe);
939                 data->failures++;
940                 return TEST_FAIL; /* Don't tolerate errors due to severity */
941         }
942
943         /*
944          * Add all ids with a made up value. The value may trigger divide by
945          * zero when subtracted and so try to make them unique.
946          */
947         k = 1;
948         hashmap__for_each_entry(data->ctx->ids, cur, bkt)
949                 expr__add_id_val(data->ctx, strdup(cur->key), k++);
950
951         hashmap__for_each_entry(data->ctx->ids, cur, bkt) {
952                 if (check_parse_cpu(cur->key, table == data->cpus_table, pe))
953                         data->failures++;
954         }
955
956         list_for_each_entry_safe(metric, tmp, &compound_list, list) {
957                 expr__add_ref(data->ctx, &metric->metric_ref);
958                 free(metric);
959         }
960
961         if (expr__parse(&result, data->ctx, pe->metric_expr)) {
962                 /*
963                  * Parsing failed, make numbers go from large to small which can
964                  * resolve divide by zero issues.
965                  */
966                 k = 1024;
967                 hashmap__for_each_entry(data->ctx->ids, cur, bkt)
968                         expr__add_id_val(data->ctx, strdup(cur->key), k--);
969                 if (expr__parse(&result, data->ctx, pe->metric_expr)) {
970                         expr_failure("Parse failed", pe);
971                         data->failures++;
972                 }
973         }
974         return 0;
975 }
976
977 static int test__parsing(struct test_suite *test __maybe_unused,
978                          int subtest __maybe_unused)
979 {
980         struct test__parsing_data data = {
981                 .cpus_table = pmu_events_table__find(),
982                 .ctx = expr__ctx_new(),
983                 .failures = 0,
984         };
985
986         if (!data.ctx) {
987                 pr_debug("expr__ctx_new failed");
988                 return TEST_FAIL;
989         }
990         pmu_for_each_core_event(test__parsing_callback, &data);
991         pmu_for_each_sys_event(test__parsing_callback, &data);
992
993         expr__ctx_free(data.ctx);
994         return data.failures == 0 ? TEST_OK : TEST_FAIL;
995 }
996
997 struct test_metric {
998         const char *str;
999 };
1000
1001 static struct test_metric metrics[] = {
1002         { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
1003         { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
1004         { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
1005         { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
1006         { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
1007 };
1008
1009 static int metric_parse_fake(const char *str)
1010 {
1011         struct expr_parse_ctx *ctx;
1012         struct hashmap_entry *cur;
1013         double result;
1014         int ret = -1;
1015         size_t bkt;
1016         int i;
1017
1018         pr_debug("parsing '%s'\n", str);
1019
1020         ctx = expr__ctx_new();
1021         if (!ctx) {
1022                 pr_debug("expr__ctx_new failed");
1023                 return TEST_FAIL;
1024         }
1025         if (expr__find_ids(str, NULL, ctx) < 0) {
1026                 pr_err("expr__find_ids failed\n");
1027                 return -1;
1028         }
1029
1030         /*
1031          * Add all ids with a made up value. The value may
1032          * trigger divide by zero when subtracted and so try to
1033          * make them unique.
1034          */
1035         i = 1;
1036         hashmap__for_each_entry(ctx->ids, cur, bkt)
1037                 expr__add_id_val(ctx, strdup(cur->key), i++);
1038
1039         hashmap__for_each_entry(ctx->ids, cur, bkt) {
1040                 if (check_parse_fake(cur->key)) {
1041                         pr_err("check_parse_fake failed\n");
1042                         goto out;
1043                 }
1044         }
1045
1046         ret = 0;
1047         if (expr__parse(&result, ctx, str)) {
1048                 /*
1049                  * Parsing failed, make numbers go from large to small which can
1050                  * resolve divide by zero issues.
1051                  */
1052                 i = 1024;
1053                 hashmap__for_each_entry(ctx->ids, cur, bkt)
1054                         expr__add_id_val(ctx, strdup(cur->key), i--);
1055                 if (expr__parse(&result, ctx, str)) {
1056                         pr_err("expr__parse failed\n");
1057                         ret = -1;
1058                 }
1059         }
1060
1061 out:
1062         expr__ctx_free(ctx);
1063         return ret;
1064 }
1065
1066 static int test__parsing_fake_callback(const struct pmu_event *pe,
1067                                        const struct pmu_event *table __maybe_unused,
1068                                        void *data __maybe_unused)
1069 {
1070         if (!pe->metric_expr)
1071                 return 0;
1072
1073         return metric_parse_fake(pe->metric_expr);
1074 }
1075
1076 /*
1077  * Parse all the metrics for current architecture,
1078  * or all defined cpus via the 'fake_pmu'
1079  * in parse_events.
1080  */
1081 static int test__parsing_fake(struct test_suite *test __maybe_unused,
1082                               int subtest __maybe_unused)
1083 {
1084         int err = 0;
1085
1086         for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
1087                 err = metric_parse_fake(metrics[i].str);
1088                 if (err)
1089                         return err;
1090         }
1091
1092         err = pmu_for_each_core_event(test__parsing_fake_callback, NULL);
1093         if (err)
1094                 return err;
1095
1096         return pmu_for_each_sys_event(test__parsing_fake_callback, NULL);
1097 }
1098
1099 static struct test_case pmu_events_tests[] = {
1100         TEST_CASE("PMU event table sanity", pmu_event_table),
1101         TEST_CASE("PMU event map aliases", aliases),
1102         TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1103                          "some metrics failed"),
1104         TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1105         { .name = NULL, }
1106 };
1107
1108 struct test_suite suite__pmu_events = {
1109         .desc = "PMU events",
1110         .test_cases = pmu_events_tests,
1111 };