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