Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "math.h"
0003 #include "parse-events.h"
0004 #include "pmu.h"
0005 #include "tests.h"
0006 #include <errno.h>
0007 #include <stdio.h>
0008 #include <linux/kernel.h>
0009 #include <linux/zalloc.h>
0010 #include "debug.h"
0011 #include "../pmu-events/pmu-events.h"
0012 #include <perf/evlist.h>
0013 #include "util/evlist.h"
0014 #include "util/expr.h"
0015 #include "util/parse-events.h"
0016 #include "metricgroup.h"
0017 #include "stat.h"
0018 
0019 struct perf_pmu_test_event {
0020     /* used for matching against events from generated pmu-events.c */
0021     struct pmu_event event;
0022 
0023     /* used for matching against event aliases */
0024     /* extra events for aliases */
0025     const char *alias_str;
0026 
0027     /*
0028      * Note: For when PublicDescription does not exist in the JSON, we
0029      * will have no long_desc in pmu_event.long_desc, but long_desc may
0030      * be set in the alias.
0031      */
0032     const char *alias_long_desc;
0033 
0034     /* PMU which we should match against */
0035     const char *matching_pmu;
0036 };
0037 
0038 struct perf_pmu_test_pmu {
0039     struct perf_pmu pmu;
0040     struct perf_pmu_test_event const *aliases[10];
0041 };
0042 
0043 static const struct perf_pmu_test_event bp_l1_btb_correct = {
0044     .event = {
0045         .name = "bp_l1_btb_correct",
0046         .event = "event=0x8a",
0047         .desc = "L1 BTB Correction",
0048         .topic = "branch",
0049     },
0050     .alias_str = "event=0x8a",
0051     .alias_long_desc = "L1 BTB Correction",
0052 };
0053 
0054 static const struct perf_pmu_test_event bp_l2_btb_correct = {
0055     .event = {
0056         .name = "bp_l2_btb_correct",
0057         .event = "event=0x8b",
0058         .desc = "L2 BTB Correction",
0059         .topic = "branch",
0060     },
0061     .alias_str = "event=0x8b",
0062     .alias_long_desc = "L2 BTB Correction",
0063 };
0064 
0065 static const struct perf_pmu_test_event segment_reg_loads_any = {
0066     .event = {
0067         .name = "segment_reg_loads.any",
0068         .event = "event=0x6,period=200000,umask=0x80",
0069         .desc = "Number of segment register loads",
0070         .topic = "other",
0071     },
0072     .alias_str = "event=0x6,period=0x30d40,umask=0x80",
0073     .alias_long_desc = "Number of segment register loads",
0074 };
0075 
0076 static const struct perf_pmu_test_event dispatch_blocked_any = {
0077     .event = {
0078         .name = "dispatch_blocked.any",
0079         .event = "event=0x9,period=200000,umask=0x20",
0080         .desc = "Memory cluster signals to block micro-op dispatch for any reason",
0081         .topic = "other",
0082     },
0083     .alias_str = "event=0x9,period=0x30d40,umask=0x20",
0084     .alias_long_desc = "Memory cluster signals to block micro-op dispatch for any reason",
0085 };
0086 
0087 static const struct perf_pmu_test_event eist_trans = {
0088     .event = {
0089         .name = "eist_trans",
0090         .event = "event=0x3a,period=200000,umask=0x0",
0091         .desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
0092         .topic = "other",
0093     },
0094     .alias_str = "event=0x3a,period=0x30d40,umask=0",
0095     .alias_long_desc = "Number of Enhanced Intel SpeedStep(R) Technology (EIST) transitions",
0096 };
0097 
0098 static const struct perf_pmu_test_event l3_cache_rd = {
0099     .event = {
0100         .name = "l3_cache_rd",
0101         .event = "event=0x40",
0102         .desc = "L3 cache access, read",
0103         .long_desc = "Attributable Level 3 cache access, read",
0104         .topic = "cache",
0105     },
0106     .alias_str = "event=0x40",
0107     .alias_long_desc = "Attributable Level 3 cache access, read",
0108 };
0109 
0110 static const struct perf_pmu_test_event *core_events[] = {
0111     &bp_l1_btb_correct,
0112     &bp_l2_btb_correct,
0113     &segment_reg_loads_any,
0114     &dispatch_blocked_any,
0115     &eist_trans,
0116     &l3_cache_rd,
0117     NULL
0118 };
0119 
0120 static const struct perf_pmu_test_event uncore_hisi_ddrc_flux_wcmd = {
0121     .event = {
0122         .name = "uncore_hisi_ddrc.flux_wcmd",
0123         .event = "event=0x2",
0124         .desc = "DDRC write commands. Unit: hisi_sccl,ddrc ",
0125         .topic = "uncore",
0126         .long_desc = "DDRC write commands",
0127         .pmu = "hisi_sccl,ddrc",
0128     },
0129     .alias_str = "event=0x2",
0130     .alias_long_desc = "DDRC write commands",
0131     .matching_pmu = "hisi_sccl1_ddrc2",
0132 };
0133 
0134 static const struct perf_pmu_test_event unc_cbo_xsnp_response_miss_eviction = {
0135     .event = {
0136         .name = "unc_cbo_xsnp_response.miss_eviction",
0137         .event = "event=0x22,umask=0x81",
0138         .desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core. Unit: uncore_cbox ",
0139         .topic = "uncore",
0140         .long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
0141         .pmu = "uncore_cbox",
0142     },
0143     .alias_str = "event=0x22,umask=0x81",
0144     .alias_long_desc = "A cross-core snoop resulted from L3 Eviction which misses in some processor core",
0145     .matching_pmu = "uncore_cbox_0",
0146 };
0147 
0148 static const struct perf_pmu_test_event uncore_hyphen = {
0149     .event = {
0150         .name = "event-hyphen",
0151         .event = "event=0xe0,umask=0x00",
0152         .desc = "UNC_CBO_HYPHEN. Unit: uncore_cbox ",
0153         .topic = "uncore",
0154         .long_desc = "UNC_CBO_HYPHEN",
0155         .pmu = "uncore_cbox",
0156     },
0157     .alias_str = "event=0xe0,umask=0",
0158     .alias_long_desc = "UNC_CBO_HYPHEN",
0159     .matching_pmu = "uncore_cbox_0",
0160 };
0161 
0162 static const struct perf_pmu_test_event uncore_two_hyph = {
0163     .event = {
0164         .name = "event-two-hyph",
0165         .event = "event=0xc0,umask=0x00",
0166         .desc = "UNC_CBO_TWO_HYPH. Unit: uncore_cbox ",
0167         .topic = "uncore",
0168         .long_desc = "UNC_CBO_TWO_HYPH",
0169         .pmu = "uncore_cbox",
0170     },
0171     .alias_str = "event=0xc0,umask=0",
0172     .alias_long_desc = "UNC_CBO_TWO_HYPH",
0173     .matching_pmu = "uncore_cbox_0",
0174 };
0175 
0176 static const struct perf_pmu_test_event uncore_hisi_l3c_rd_hit_cpipe = {
0177     .event = {
0178         .name = "uncore_hisi_l3c.rd_hit_cpipe",
0179         .event = "event=0x7",
0180         .desc = "Total read hits. Unit: hisi_sccl,l3c ",
0181         .topic = "uncore",
0182         .long_desc = "Total read hits",
0183         .pmu = "hisi_sccl,l3c",
0184     },
0185     .alias_str = "event=0x7",
0186     .alias_long_desc = "Total read hits",
0187     .matching_pmu = "hisi_sccl3_l3c7",
0188 };
0189 
0190 static const struct perf_pmu_test_event uncore_imc_free_running_cache_miss = {
0191     .event = {
0192         .name = "uncore_imc_free_running.cache_miss",
0193         .event = "event=0x12",
0194         .desc = "Total cache misses. Unit: uncore_imc_free_running ",
0195         .topic = "uncore",
0196         .long_desc = "Total cache misses",
0197         .pmu = "uncore_imc_free_running",
0198     },
0199     .alias_str = "event=0x12",
0200     .alias_long_desc = "Total cache misses",
0201     .matching_pmu = "uncore_imc_free_running_0",
0202 };
0203 
0204 static const struct perf_pmu_test_event uncore_imc_cache_hits = {
0205     .event = {
0206         .name = "uncore_imc.cache_hits",
0207         .event = "event=0x34",
0208         .desc = "Total cache hits. Unit: uncore_imc ",
0209         .topic = "uncore",
0210         .long_desc = "Total cache hits",
0211         .pmu = "uncore_imc",
0212     },
0213     .alias_str = "event=0x34",
0214     .alias_long_desc = "Total cache hits",
0215     .matching_pmu = "uncore_imc_0",
0216 };
0217 
0218 static const struct perf_pmu_test_event *uncore_events[] = {
0219     &uncore_hisi_ddrc_flux_wcmd,
0220     &unc_cbo_xsnp_response_miss_eviction,
0221     &uncore_hyphen,
0222     &uncore_two_hyph,
0223     &uncore_hisi_l3c_rd_hit_cpipe,
0224     &uncore_imc_free_running_cache_miss,
0225     &uncore_imc_cache_hits,
0226     NULL
0227 };
0228 
0229 static const struct perf_pmu_test_event sys_ddr_pmu_write_cycles = {
0230     .event = {
0231         .name = "sys_ddr_pmu.write_cycles",
0232         .event = "event=0x2b",
0233         .desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
0234         .topic = "uncore",
0235         .pmu = "uncore_sys_ddr_pmu",
0236         .compat = "v8",
0237     },
0238     .alias_str = "event=0x2b",
0239     .alias_long_desc = "ddr write-cycles event. Unit: uncore_sys_ddr_pmu ",
0240     .matching_pmu = "uncore_sys_ddr_pmu",
0241 };
0242 
0243 static const struct perf_pmu_test_event sys_ccn_pmu_read_cycles = {
0244     .event = {
0245         .name = "sys_ccn_pmu.read_cycles",
0246         .event = "config=0x2c",
0247         .desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
0248         .topic = "uncore",
0249         .pmu = "uncore_sys_ccn_pmu",
0250         .compat = "0x01",
0251     },
0252     .alias_str = "config=0x2c",
0253     .alias_long_desc = "ccn read-cycles event. Unit: uncore_sys_ccn_pmu ",
0254     .matching_pmu = "uncore_sys_ccn_pmu",
0255 };
0256 
0257 static const struct perf_pmu_test_event *sys_events[] = {
0258     &sys_ddr_pmu_write_cycles,
0259     &sys_ccn_pmu_read_cycles,
0260     NULL
0261 };
0262 
0263 static bool is_same(const char *reference, const char *test)
0264 {
0265     if (!reference && !test)
0266         return true;
0267 
0268     if (reference && !test)
0269         return false;
0270 
0271     if (!reference && test)
0272         return false;
0273 
0274     return !strcmp(reference, test);
0275 }
0276 
0277 static int compare_pmu_events(const struct pmu_event *e1, const struct pmu_event *e2)
0278 {
0279     if (!is_same(e1->name, e2->name)) {
0280         pr_debug2("testing event e1 %s: mismatched name string, %s vs %s\n",
0281               e1->name, e1->name, e2->name);
0282         return -1;
0283     }
0284 
0285     if (!is_same(e1->compat, e2->compat)) {
0286         pr_debug2("testing event e1 %s: mismatched compat string, %s vs %s\n",
0287               e1->name, e1->compat, e2->compat);
0288         return -1;
0289     }
0290 
0291     if (!is_same(e1->event, e2->event)) {
0292         pr_debug2("testing event e1 %s: mismatched event, %s vs %s\n",
0293               e1->name, e1->event, e2->event);
0294         return -1;
0295     }
0296 
0297     if (!is_same(e1->desc, e2->desc)) {
0298         pr_debug2("testing event e1 %s: mismatched desc, %s vs %s\n",
0299               e1->name, e1->desc, e2->desc);
0300         return -1;
0301     }
0302 
0303     if (!is_same(e1->topic, e2->topic)) {
0304         pr_debug2("testing event e1 %s: mismatched topic, %s vs %s\n",
0305               e1->name, e1->topic, e2->topic);
0306         return -1;
0307     }
0308 
0309     if (!is_same(e1->long_desc, e2->long_desc)) {
0310         pr_debug2("testing event e1 %s: mismatched long_desc, %s vs %s\n",
0311               e1->name, e1->long_desc, e2->long_desc);
0312         return -1;
0313     }
0314 
0315     if (!is_same(e1->pmu, e2->pmu)) {
0316         pr_debug2("testing event e1 %s: mismatched pmu string, %s vs %s\n",
0317               e1->name, e1->pmu, e2->pmu);
0318         return -1;
0319     }
0320 
0321     if (!is_same(e1->unit, e2->unit)) {
0322         pr_debug2("testing event e1 %s: mismatched unit, %s vs %s\n",
0323               e1->name, e1->unit, e2->unit);
0324         return -1;
0325     }
0326 
0327     if (!is_same(e1->perpkg, e2->perpkg)) {
0328         pr_debug2("testing event e1 %s: mismatched perpkg, %s vs %s\n",
0329               e1->name, e1->perpkg, e2->perpkg);
0330         return -1;
0331     }
0332 
0333     if (!is_same(e1->aggr_mode, e2->aggr_mode)) {
0334         pr_debug2("testing event e1 %s: mismatched aggr_mode, %s vs %s\n",
0335               e1->name, e1->aggr_mode, e2->aggr_mode);
0336         return -1;
0337     }
0338 
0339     if (!is_same(e1->metric_expr, e2->metric_expr)) {
0340         pr_debug2("testing event e1 %s: mismatched metric_expr, %s vs %s\n",
0341               e1->name, e1->metric_expr, e2->metric_expr);
0342         return -1;
0343     }
0344 
0345     if (!is_same(e1->metric_name, e2->metric_name)) {
0346         pr_debug2("testing event e1 %s: mismatched metric_name, %s vs %s\n",
0347               e1->name, e1->metric_name, e2->metric_name);
0348         return -1;
0349     }
0350 
0351     if (!is_same(e1->metric_group, e2->metric_group)) {
0352         pr_debug2("testing event e1 %s: mismatched metric_group, %s vs %s\n",
0353               e1->name, e1->metric_group, e2->metric_group);
0354         return -1;
0355     }
0356 
0357     if (!is_same(e1->deprecated, e2->deprecated)) {
0358         pr_debug2("testing event e1 %s: mismatched deprecated, %s vs %s\n",
0359               e1->name, e1->deprecated, e2->deprecated);
0360         return -1;
0361     }
0362 
0363     if (!is_same(e1->metric_constraint, e2->metric_constraint)) {
0364         pr_debug2("testing event e1 %s: mismatched metric_constant, %s vs %s\n",
0365               e1->name, e1->metric_constraint, e2->metric_constraint);
0366         return -1;
0367     }
0368 
0369     return 0;
0370 }
0371 
0372 static int compare_alias_to_test_event(struct perf_pmu_alias *alias,
0373                 struct perf_pmu_test_event const *test_event,
0374                 char const *pmu_name)
0375 {
0376     struct pmu_event const *event = &test_event->event;
0377 
0378     /* An alias was found, ensure everything is in order */
0379     if (!is_same(alias->name, event->name)) {
0380         pr_debug("testing aliases PMU %s: mismatched name, %s vs %s\n",
0381               pmu_name, alias->name, event->name);
0382         return -1;
0383     }
0384 
0385     if (!is_same(alias->desc, event->desc)) {
0386         pr_debug("testing aliases PMU %s: mismatched desc, %s vs %s\n",
0387               pmu_name, alias->desc, event->desc);
0388         return -1;
0389     }
0390 
0391     if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
0392         pr_debug("testing aliases PMU %s: mismatched long_desc, %s vs %s\n",
0393               pmu_name, alias->long_desc,
0394               test_event->alias_long_desc);
0395         return -1;
0396     }
0397 
0398     if (!is_same(alias->topic, event->topic)) {
0399         pr_debug("testing aliases PMU %s: mismatched topic, %s vs %s\n",
0400               pmu_name, alias->topic, event->topic);
0401         return -1;
0402     }
0403 
0404     if (!is_same(alias->str, test_event->alias_str)) {
0405         pr_debug("testing aliases PMU %s: mismatched str, %s vs %s\n",
0406               pmu_name, alias->str, test_event->alias_str);
0407         return -1;
0408     }
0409 
0410     if (!is_same(alias->long_desc, test_event->alias_long_desc)) {
0411         pr_debug("testing aliases PMU %s: mismatched long desc, %s vs %s\n",
0412               pmu_name, alias->str, test_event->alias_long_desc);
0413         return -1;
0414     }
0415 
0416 
0417     if (!is_same(alias->pmu_name, test_event->event.pmu)) {
0418         pr_debug("testing aliases PMU %s: mismatched pmu_name, %s vs %s\n",
0419               pmu_name, alias->pmu_name, test_event->event.pmu);
0420         return -1;
0421     }
0422 
0423     return 0;
0424 }
0425 
0426 static int test__pmu_event_table_core_callback(const struct pmu_event *pe,
0427                            const struct pmu_events_table *table __maybe_unused,
0428                            void *data)
0429 {
0430     int *map_events = data;
0431     struct perf_pmu_test_event const **test_event_table;
0432     bool found = false;
0433 
0434     if (!pe->name)
0435         return 0;
0436 
0437     if (pe->pmu)
0438         test_event_table = &uncore_events[0];
0439     else
0440         test_event_table = &core_events[0];
0441 
0442     for (; *test_event_table; test_event_table++) {
0443         struct perf_pmu_test_event const *test_event = *test_event_table;
0444         struct pmu_event const *event = &test_event->event;
0445 
0446         if (strcmp(pe->name, event->name))
0447             continue;
0448         found = true;
0449         (*map_events)++;
0450 
0451         if (compare_pmu_events(pe, event))
0452             return -1;
0453 
0454         pr_debug("testing event table %s: pass\n", pe->name);
0455     }
0456     if (!found) {
0457         pr_err("testing event table: could not find event %s\n", pe->name);
0458         return -1;
0459     }
0460     return 0;
0461 }
0462 
0463 static int test__pmu_event_table_sys_callback(const struct pmu_event *pe,
0464                           const struct pmu_events_table *table __maybe_unused,
0465                           void *data)
0466 {
0467     int *map_events = data;
0468     struct perf_pmu_test_event const **test_event_table;
0469     bool found = false;
0470 
0471     test_event_table = &sys_events[0];
0472 
0473     for (; *test_event_table; test_event_table++) {
0474         struct perf_pmu_test_event const *test_event = *test_event_table;
0475         struct pmu_event const *event = &test_event->event;
0476 
0477         if (strcmp(pe->name, event->name))
0478             continue;
0479         found = true;
0480         (*map_events)++;
0481 
0482         if (compare_pmu_events(pe, event))
0483             return TEST_FAIL;
0484 
0485         pr_debug("testing sys event table %s: pass\n", pe->name);
0486     }
0487     if (!found) {
0488         pr_debug("testing sys event table: could not find event %s\n", pe->name);
0489         return TEST_FAIL;
0490     }
0491     return TEST_OK;
0492 }
0493 
0494 /* Verify generated events from pmu-events.c are as expected */
0495 static int test__pmu_event_table(struct test_suite *test __maybe_unused,
0496                  int subtest __maybe_unused)
0497 {
0498     const struct pmu_events_table *sys_event_table = find_sys_events_table("pme_test_soc_sys");
0499     const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
0500     int map_events = 0, expected_events, err;
0501 
0502     /* ignore 3x sentinels */
0503     expected_events = ARRAY_SIZE(core_events) +
0504               ARRAY_SIZE(uncore_events) +
0505               ARRAY_SIZE(sys_events) - 3;
0506 
0507     if (!table || !sys_event_table)
0508         return -1;
0509 
0510     err = pmu_events_table_for_each_event(table, test__pmu_event_table_core_callback,
0511                           &map_events);
0512     if (err)
0513         return err;
0514 
0515     err = pmu_events_table_for_each_event(sys_event_table, test__pmu_event_table_sys_callback,
0516                           &map_events);
0517     if (err)
0518         return err;
0519 
0520     if (map_events != expected_events) {
0521         pr_err("testing event table: found %d, but expected %d\n",
0522                map_events, expected_events);
0523         return TEST_FAIL;
0524     }
0525 
0526     return 0;
0527 }
0528 
0529 static struct perf_pmu_alias *find_alias(const char *test_event, struct list_head *aliases)
0530 {
0531     struct perf_pmu_alias *alias;
0532 
0533     list_for_each_entry(alias, aliases, list)
0534         if (!strcmp(test_event, alias->name))
0535             return alias;
0536 
0537     return NULL;
0538 }
0539 
0540 /* Verify aliases are as expected */
0541 static int __test_core_pmu_event_aliases(char *pmu_name, int *count)
0542 {
0543     struct perf_pmu_test_event const **test_event_table;
0544     struct perf_pmu *pmu;
0545     LIST_HEAD(aliases);
0546     int res = 0;
0547     const struct pmu_events_table *table = find_core_events_table("testarch", "testcpu");
0548     struct perf_pmu_alias *a, *tmp;
0549 
0550     if (!table)
0551         return -1;
0552 
0553     test_event_table = &core_events[0];
0554 
0555     pmu = zalloc(sizeof(*pmu));
0556     if (!pmu)
0557         return -1;
0558 
0559     pmu->name = pmu_name;
0560 
0561     pmu_add_cpu_aliases_table(&aliases, pmu, table);
0562 
0563     for (; *test_event_table; test_event_table++) {
0564         struct perf_pmu_test_event const *test_event = *test_event_table;
0565         struct pmu_event const *event = &test_event->event;
0566         struct perf_pmu_alias *alias = find_alias(event->name, &aliases);
0567 
0568         if (!alias) {
0569             pr_debug("testing aliases core PMU %s: no alias, alias_table->name=%s\n",
0570                   pmu_name, event->name);
0571             res = -1;
0572             break;
0573         }
0574 
0575         if (compare_alias_to_test_event(alias, test_event, pmu_name)) {
0576             res = -1;
0577             break;
0578         }
0579 
0580         (*count)++;
0581         pr_debug2("testing aliases core PMU %s: matched event %s\n",
0582               pmu_name, alias->name);
0583     }
0584 
0585     list_for_each_entry_safe(a, tmp, &aliases, list) {
0586         list_del(&a->list);
0587         perf_pmu_free_alias(a);
0588     }
0589     free(pmu);
0590     return res;
0591 }
0592 
0593 static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
0594 {
0595     int alias_count = 0, to_match_count = 0, matched_count = 0;
0596     struct perf_pmu_test_event const **table;
0597     struct perf_pmu *pmu = &test_pmu->pmu;
0598     const char *pmu_name = pmu->name;
0599     struct perf_pmu_alias *a, *tmp, *alias;
0600     const struct pmu_events_table *events_table;
0601     LIST_HEAD(aliases);
0602     int res = 0;
0603 
0604     events_table = find_core_events_table("testarch", "testcpu");
0605     if (!events_table)
0606         return -1;
0607     pmu_add_cpu_aliases_table(&aliases, pmu, events_table);
0608     pmu_add_sys_aliases(&aliases, pmu);
0609 
0610     /* Count how many aliases we generated */
0611     list_for_each_entry(alias, &aliases, list)
0612         alias_count++;
0613 
0614     /* Count how many aliases we expect from the known table */
0615     for (table = &test_pmu->aliases[0]; *table; table++)
0616         to_match_count++;
0617 
0618     if (alias_count != to_match_count) {
0619         pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
0620              pmu_name, to_match_count, alias_count);
0621         res = -1;
0622         goto out;
0623     }
0624 
0625     list_for_each_entry(alias, &aliases, list) {
0626         bool matched = false;
0627 
0628         for (table = &test_pmu->aliases[0]; *table; table++) {
0629             struct perf_pmu_test_event const *test_event = *table;
0630             struct pmu_event const *event = &test_event->event;
0631 
0632             if (!strcmp(event->name, alias->name)) {
0633                 if (compare_alias_to_test_event(alias,
0634                             test_event,
0635                             pmu_name)) {
0636                     continue;
0637                 }
0638                 matched = true;
0639                 matched_count++;
0640             }
0641         }
0642 
0643         if (matched == false) {
0644             pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
0645                  pmu_name, alias->name);
0646             res = -1;
0647             goto out;
0648         }
0649     }
0650 
0651     if (alias_count != matched_count) {
0652         pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
0653              pmu_name, matched_count, alias_count);
0654         res = -1;
0655     }
0656 
0657 out:
0658     list_for_each_entry_safe(a, tmp, &aliases, list) {
0659         list_del(&a->list);
0660         perf_pmu_free_alias(a);
0661     }
0662     return res;
0663 }
0664 
0665 static struct perf_pmu_test_pmu test_pmus[] = {
0666     {
0667         .pmu = {
0668             .name = (char *)"hisi_sccl1_ddrc2",
0669             .is_uncore = 1,
0670         },
0671         .aliases = {
0672             &uncore_hisi_ddrc_flux_wcmd,
0673         },
0674     },
0675     {
0676         .pmu = {
0677             .name = (char *)"uncore_cbox_0",
0678             .is_uncore = 1,
0679         },
0680         .aliases = {
0681             &unc_cbo_xsnp_response_miss_eviction,
0682             &uncore_hyphen,
0683             &uncore_two_hyph,
0684         },
0685     },
0686     {
0687         .pmu = {
0688             .name = (char *)"hisi_sccl3_l3c7",
0689             .is_uncore = 1,
0690         },
0691         .aliases = {
0692             &uncore_hisi_l3c_rd_hit_cpipe,
0693         },
0694     },
0695     {
0696         .pmu = {
0697             .name = (char *)"uncore_imc_free_running_0",
0698             .is_uncore = 1,
0699         },
0700         .aliases = {
0701             &uncore_imc_free_running_cache_miss,
0702         },
0703     },
0704     {
0705         .pmu = {
0706             .name = (char *)"uncore_imc_0",
0707             .is_uncore = 1,
0708         },
0709         .aliases = {
0710             &uncore_imc_cache_hits,
0711         },
0712     },
0713     {
0714         .pmu = {
0715             .name = (char *)"uncore_sys_ddr_pmu0",
0716             .is_uncore = 1,
0717             .id = (char *)"v8",
0718         },
0719         .aliases = {
0720             &sys_ddr_pmu_write_cycles,
0721         },
0722     },
0723     {
0724         .pmu = {
0725             .name = (char *)"uncore_sys_ccn_pmu4",
0726             .is_uncore = 1,
0727             .id = (char *)"0x01",
0728         },
0729         .aliases = {
0730             &sys_ccn_pmu_read_cycles,
0731         },
0732     },
0733 };
0734 
0735 /* Test that aliases generated are as expected */
0736 static int test__aliases(struct test_suite *test __maybe_unused,
0737             int subtest __maybe_unused)
0738 {
0739     struct perf_pmu *pmu = NULL;
0740     unsigned long i;
0741 
0742     while ((pmu = perf_pmu__scan(pmu)) != NULL) {
0743         int count = 0;
0744 
0745         if (!is_pmu_core(pmu->name))
0746             continue;
0747 
0748         if (list_empty(&pmu->format)) {
0749             pr_debug2("skipping testing core PMU %s\n", pmu->name);
0750             continue;
0751         }
0752 
0753         if (__test_core_pmu_event_aliases(pmu->name, &count)) {
0754             pr_debug("testing core PMU %s aliases: failed\n", pmu->name);
0755             return -1;
0756         }
0757 
0758         if (count == 0) {
0759             pr_debug("testing core PMU %s aliases: no events to match\n",
0760                   pmu->name);
0761             return -1;
0762         }
0763 
0764         pr_debug("testing core PMU %s aliases: pass\n", pmu->name);
0765     }
0766 
0767     for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
0768         int res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
0769 
0770         if (res)
0771             return res;
0772     }
0773 
0774     return 0;
0775 }
0776 
0777 static bool is_number(const char *str)
0778 {
0779     char *end_ptr;
0780     double v;
0781 
0782     errno = 0;
0783     v = strtod(str, &end_ptr);
0784     (void)v; // We're not interested in this value, only if it is valid
0785     return errno == 0 && end_ptr != str;
0786 }
0787 
0788 static int check_parse_id(const char *id, struct parse_events_error *error,
0789               struct perf_pmu *fake_pmu)
0790 {
0791     struct evlist *evlist;
0792     int ret;
0793     char *dup, *cur;
0794 
0795     /* Numbers are always valid. */
0796     if (is_number(id))
0797         return 0;
0798 
0799     evlist = evlist__new();
0800     if (!evlist)
0801         return -ENOMEM;
0802 
0803     dup = strdup(id);
0804     if (!dup)
0805         return -ENOMEM;
0806 
0807     for (cur = strchr(dup, '@') ; cur; cur = strchr(++cur, '@'))
0808         *cur = '/';
0809 
0810     if (fake_pmu) {
0811         /*
0812          * Every call to __parse_events will try to initialize the PMU
0813          * state from sysfs and then clean it up at the end. Reset the
0814          * PMU events to the test state so that we don't pick up
0815          * erroneous prefixes and suffixes.
0816          */
0817         perf_pmu__test_parse_init();
0818     }
0819     ret = __parse_events(evlist, dup, error, fake_pmu);
0820     free(dup);
0821 
0822     evlist__delete(evlist);
0823     return ret;
0824 }
0825 
0826 static int check_parse_fake(const char *id)
0827 {
0828     struct parse_events_error error;
0829     int ret;
0830 
0831     parse_events_error__init(&error);
0832     ret = check_parse_id(id, &error, &perf_pmu__fake);
0833     parse_events_error__exit(&error);
0834     return ret;
0835 }
0836 
0837 struct metric {
0838     struct list_head list;
0839     struct metric_ref metric_ref;
0840 };
0841 
0842 static int test__parsing_callback(const struct pmu_event *pe, const struct pmu_events_table *table,
0843                   void *data)
0844 {
0845     int *failures = data;
0846     int k;
0847     struct evlist *evlist;
0848     struct perf_cpu_map *cpus;
0849     struct runtime_stat st;
0850     struct evsel *evsel;
0851     struct rblist metric_events = {
0852         .nr_entries = 0,
0853     };
0854     int err = 0;
0855 
0856     if (!pe->metric_expr)
0857         return 0;
0858 
0859     pr_debug("Found metric '%s'\n", pe->metric_name);
0860     (*failures)++;
0861 
0862     /*
0863      * We need to prepare evlist for stat mode running on CPU 0
0864      * because that's where all the stats are going to be created.
0865      */
0866     evlist = evlist__new();
0867     if (!evlist)
0868         return -ENOMEM;
0869 
0870     cpus = perf_cpu_map__new("0");
0871     if (!cpus) {
0872         evlist__delete(evlist);
0873         return -ENOMEM;
0874     }
0875 
0876     perf_evlist__set_maps(&evlist->core, cpus, NULL);
0877     runtime_stat__init(&st);
0878 
0879     err = metricgroup__parse_groups_test(evlist, table, pe->metric_name,
0880                          false, false,
0881                          &metric_events);
0882     if (err) {
0883         if (!strcmp(pe->metric_name, "M1") || !strcmp(pe->metric_name, "M2") ||
0884             !strcmp(pe->metric_name, "M3")) {
0885             (*failures)--;
0886             pr_debug("Expected broken metric %s skipping\n", pe->metric_name);
0887             err = 0;
0888         }
0889         goto out_err;
0890     }
0891 
0892     err = evlist__alloc_stats(evlist, false);
0893     if (err)
0894         goto out_err;
0895     /*
0896      * Add all ids with a made up value. The value may trigger divide by
0897      * zero when subtracted and so try to make them unique.
0898      */
0899     k = 1;
0900     perf_stat__reset_shadow_stats();
0901     evlist__for_each_entry(evlist, evsel) {
0902         perf_stat__update_shadow_stats(evsel, k, 0, &st);
0903         if (!strcmp(evsel->name, "duration_time"))
0904             update_stats(&walltime_nsecs_stats, k);
0905         k++;
0906     }
0907     evlist__for_each_entry(evlist, evsel) {
0908         struct metric_event *me = metricgroup__lookup(&metric_events, evsel, false);
0909 
0910         if (me != NULL) {
0911             struct metric_expr *mexp;
0912 
0913             list_for_each_entry (mexp, &me->head, nd) {
0914                 if (strcmp(mexp->metric_name, pe->metric_name))
0915                     continue;
0916                 pr_debug("Result %f\n", test_generic_metric(mexp, 0, &st));
0917                 err = 0;
0918                 (*failures)--;
0919                 goto out_err;
0920             }
0921         }
0922     }
0923     pr_debug("Didn't find parsed metric %s", pe->metric_name);
0924     err = 1;
0925 out_err:
0926     if (err)
0927         pr_debug("Broken metric %s\n", pe->metric_name);
0928 
0929     /* ... cleanup. */
0930     metricgroup__rblist_exit(&metric_events);
0931     runtime_stat__exit(&st);
0932     evlist__free_stats(evlist);
0933     perf_cpu_map__put(cpus);
0934     evlist__delete(evlist);
0935     return err;
0936 }
0937 
0938 static int test__parsing(struct test_suite *test __maybe_unused,
0939              int subtest __maybe_unused)
0940 {
0941     int failures = 0;
0942 
0943     pmu_for_each_core_event(test__parsing_callback, &failures);
0944     pmu_for_each_sys_event(test__parsing_callback, &failures);
0945 
0946     return failures == 0 ? TEST_OK : TEST_FAIL;
0947 }
0948 
0949 struct test_metric {
0950     const char *str;
0951 };
0952 
0953 static struct test_metric metrics[] = {
0954     { "(unc_p_power_state_occupancy.cores_c0 / unc_p_clockticks) * 100." },
0955     { "imx8_ddr0@read\\-cycles@ * 4 * 4", },
0956     { "imx8_ddr0@axid\\-read\\,axi_mask\\=0xffff\\,axi_id\\=0x0000@ * 4", },
0957     { "(cstate_pkg@c2\\-residency@ / msr@tsc@) * 100", },
0958     { "(imx8_ddr0@read\\-cycles@ + imx8_ddr0@write\\-cycles@)", },
0959 };
0960 
0961 static int metric_parse_fake(const char *str)
0962 {
0963     struct expr_parse_ctx *ctx;
0964     struct hashmap_entry *cur;
0965     double result;
0966     int ret = -1;
0967     size_t bkt;
0968     int i;
0969 
0970     pr_debug("parsing '%s'\n", str);
0971 
0972     ctx = expr__ctx_new();
0973     if (!ctx) {
0974         pr_debug("expr__ctx_new failed");
0975         return TEST_FAIL;
0976     }
0977     if (expr__find_ids(str, NULL, ctx) < 0) {
0978         pr_err("expr__find_ids failed\n");
0979         return -1;
0980     }
0981 
0982     /*
0983      * Add all ids with a made up value. The value may
0984      * trigger divide by zero when subtracted and so try to
0985      * make them unique.
0986      */
0987     i = 1;
0988     hashmap__for_each_entry(ctx->ids, cur, bkt)
0989         expr__add_id_val(ctx, strdup(cur->key), i++);
0990 
0991     hashmap__for_each_entry(ctx->ids, cur, bkt) {
0992         if (check_parse_fake(cur->key)) {
0993             pr_err("check_parse_fake failed\n");
0994             goto out;
0995         }
0996     }
0997 
0998     ret = 0;
0999     if (expr__parse(&result, ctx, str)) {
1000         /*
1001          * Parsing failed, make numbers go from large to small which can
1002          * resolve divide by zero issues.
1003          */
1004         i = 1024;
1005         hashmap__for_each_entry(ctx->ids, cur, bkt)
1006             expr__add_id_val(ctx, strdup(cur->key), i--);
1007         if (expr__parse(&result, ctx, str)) {
1008             pr_err("expr__parse failed\n");
1009             ret = -1;
1010         }
1011     }
1012 
1013 out:
1014     expr__ctx_free(ctx);
1015     return ret;
1016 }
1017 
1018 static int test__parsing_fake_callback(const struct pmu_event *pe,
1019                        const struct pmu_events_table *table __maybe_unused,
1020                        void *data __maybe_unused)
1021 {
1022     if (!pe->metric_expr)
1023         return 0;
1024 
1025     return metric_parse_fake(pe->metric_expr);
1026 }
1027 
1028 /*
1029  * Parse all the metrics for current architecture,
1030  * or all defined cpus via the 'fake_pmu'
1031  * in parse_events.
1032  */
1033 static int test__parsing_fake(struct test_suite *test __maybe_unused,
1034                   int subtest __maybe_unused)
1035 {
1036     int err = 0;
1037 
1038     for (size_t i = 0; i < ARRAY_SIZE(metrics); i++) {
1039         err = metric_parse_fake(metrics[i].str);
1040         if (err)
1041             return err;
1042     }
1043 
1044     err = pmu_for_each_core_event(test__parsing_fake_callback, NULL);
1045     if (err)
1046         return err;
1047 
1048     return pmu_for_each_sys_event(test__parsing_fake_callback, NULL);
1049 }
1050 
1051 static struct test_case pmu_events_tests[] = {
1052     TEST_CASE("PMU event table sanity", pmu_event_table),
1053     TEST_CASE("PMU event map aliases", aliases),
1054     TEST_CASE_REASON("Parsing of PMU event table metrics", parsing,
1055              "some metrics failed"),
1056     TEST_CASE("Parsing of PMU event table metrics with fake PMUs", parsing_fake),
1057     { .name = NULL, }
1058 };
1059 
1060 struct test_suite suite__pmu_events = {
1061     .desc = "PMU events",
1062     .test_cases = pmu_events_tests,
1063 };