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