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