Skip to content

Commit 754baf4

Browse files
captain5050acmel
authored andcommitted
perf pmu: Change aliases from list to hashmap
Finding an alias for things like perf_pmu__have_event() would need to search the aliases list, whilst this happens relatively infrequently it can be a significant overhead in testing. Switch to using a hashmap. Move common initialization code to perf_pmu__init(). Refactor the test 'struct perf_pmu_test_pmu' to not have perf pmu within it to better support the perf_pmu__init() function. Before: ``` $ time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m13.287s user 0m13.026s sys 0m0.532s ``` After: ``` $ time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m13.011s user 0m12.885s sys 0m0.485s ``` Committer testing: root@number:~# grep -m1 'model name' /proc/cpuinfo model name : AMD Ryzen 9 9950X3D 16-Core Processor root@number:~# Before: root@number:~# time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m9.296s user 0m9.361s sys 0m0.063s root@number:~# After: root@number:~# time perf test "Parsing of PMU event table metrics" 10.3: Parsing of PMU event table metrics : Ok 10.4: Parsing of PMU event table metrics with fake PMUs : Ok real 0m9.286s user 0m9.354s sys 0m0.062s root@number:~# Signed-off-by: Ian Rogers <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Tested-by: Namhyung Kim <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: James Clark <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Ravi Bangoria <[email protected]> Cc: Thomas Richter <[email protected]> Cc: Xu Yang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 375368a commit 754baf4

File tree

5 files changed

+199
-161
lines changed

5 files changed

+199
-161
lines changed

tools/perf/tests/pmu-events.c

Lines changed: 62 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ struct perf_pmu_test_event {
3838
};
3939

4040
struct perf_pmu_test_pmu {
41-
struct perf_pmu pmu;
41+
const char *pmu_name;
42+
bool pmu_is_uncore;
43+
const char *pmu_id;
4244
struct perf_pmu_test_event const *aliases[10];
4345
};
4446

@@ -553,11 +555,10 @@ static int __test_core_pmu_event_aliases(const char *pmu_name, int *count)
553555
if (!pmu)
554556
return -1;
555557

556-
INIT_LIST_HEAD(&pmu->format);
557-
INIT_LIST_HEAD(&pmu->aliases);
558-
INIT_LIST_HEAD(&pmu->caps);
559-
INIT_LIST_HEAD(&pmu->list);
560-
pmu->name = strdup(pmu_name);
558+
if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, pmu_name) != 0) {
559+
perf_pmu__delete(pmu);
560+
return -1;
561+
}
561562
pmu->is_core = true;
562563

563564
pmu->events_table = table;
@@ -594,14 +595,30 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
594595
{
595596
int alias_count = 0, to_match_count = 0, matched_count = 0;
596597
struct perf_pmu_test_event const **table;
597-
struct perf_pmu *pmu = &test_pmu->pmu;
598-
const char *pmu_name = pmu->name;
598+
struct perf_pmu *pmu;
599599
const struct pmu_events_table *events_table;
600600
int res = 0;
601601

602602
events_table = find_core_events_table("testarch", "testcpu");
603603
if (!events_table)
604604
return -1;
605+
606+
pmu = zalloc(sizeof(*pmu));
607+
if (!pmu)
608+
return -1;
609+
610+
if (perf_pmu__init(pmu, PERF_PMU_TYPE_FAKE, test_pmu->pmu_name) != 0) {
611+
perf_pmu__delete(pmu);
612+
return -1;
613+
}
614+
pmu->is_uncore = test_pmu->pmu_is_uncore;
615+
if (test_pmu->pmu_id) {
616+
pmu->id = strdup(test_pmu->pmu_id);
617+
if (!pmu->id) {
618+
perf_pmu__delete(pmu);
619+
return -1;
620+
}
621+
}
605622
pmu->events_table = events_table;
606623
pmu_add_cpu_aliases_table(pmu, events_table);
607624
pmu->cpu_aliases_added = true;
@@ -617,7 +634,8 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
617634

618635
if (alias_count != to_match_count) {
619636
pr_debug("testing aliases uncore PMU %s: mismatch expected aliases (%d) vs found (%d)\n",
620-
pmu_name, to_match_count, alias_count);
637+
pmu->name, to_match_count, alias_count);
638+
perf_pmu__delete(pmu);
621639
return -1;
622640
}
623641

@@ -630,9 +648,10 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
630648
.count = &matched_count,
631649
};
632650

633-
if (strcmp(pmu_name, test_event.matching_pmu)) {
651+
if (strcmp(pmu->name, test_event.matching_pmu)) {
634652
pr_debug("testing aliases uncore PMU %s: mismatched matching_pmu, %s vs %s\n",
635-
pmu_name, test_event.matching_pmu, pmu_name);
653+
pmu->name, test_event.matching_pmu, pmu->name);
654+
perf_pmu__delete(pmu);
636655
return -1;
637656
}
638657

@@ -641,123 +660,103 @@ static int __test_uncore_pmu_event_aliases(struct perf_pmu_test_pmu *test_pmu)
641660
if (err) {
642661
res = err;
643662
pr_debug("testing aliases uncore PMU %s: could not match alias %s\n",
644-
pmu_name, event->name);
663+
pmu->name, event->name);
664+
perf_pmu__delete(pmu);
645665
return -1;
646666
}
647667
}
648668

649669
if (alias_count != matched_count) {
650670
pr_debug("testing aliases uncore PMU %s: mismatch found aliases (%d) vs matched (%d)\n",
651-
pmu_name, matched_count, alias_count);
671+
pmu->name, matched_count, alias_count);
652672
res = -1;
653673
}
674+
perf_pmu__delete(pmu);
654675
return res;
655676
}
656677

657678
static struct perf_pmu_test_pmu test_pmus[] = {
658679
{
659-
.pmu = {
660-
.name = "hisi_sccl1_ddrc2",
661-
.is_uncore = 1,
662-
},
680+
.pmu_name = "hisi_sccl1_ddrc2",
681+
.pmu_is_uncore = 1,
663682
.aliases = {
664683
&uncore_hisi_ddrc_flux_wcmd,
665684
},
666685
},
667686
{
668-
.pmu = {
669-
.name = "uncore_cbox_0",
670-
.is_uncore = 1,
671-
},
687+
.pmu_name = "uncore_cbox_0",
688+
.pmu_is_uncore = 1,
672689
.aliases = {
673690
&unc_cbo_xsnp_response_miss_eviction,
674691
&uncore_hyphen,
675692
&uncore_two_hyph,
676693
},
677694
},
678695
{
679-
.pmu = {
680-
.name = "hisi_sccl3_l3c7",
681-
.is_uncore = 1,
682-
},
696+
.pmu_name = "hisi_sccl3_l3c7",
697+
.pmu_is_uncore = 1,
683698
.aliases = {
684699
&uncore_hisi_l3c_rd_hit_cpipe,
685700
},
686701
},
687702
{
688-
.pmu = {
689-
.name = "uncore_imc_free_running_0",
690-
.is_uncore = 1,
691-
},
703+
.pmu_name = "uncore_imc_free_running_0",
704+
.pmu_is_uncore = 1,
692705
.aliases = {
693706
&uncore_imc_free_running_cache_miss,
694707
},
695708
},
696709
{
697-
.pmu = {
698-
.name = "uncore_imc_0",
699-
.is_uncore = 1,
700-
},
710+
.pmu_name = "uncore_imc_0",
711+
.pmu_is_uncore = 1,
701712
.aliases = {
702713
&uncore_imc_cache_hits,
703714
},
704715
},
705716
{
706-
.pmu = {
707-
.name = "uncore_sys_ddr_pmu0",
708-
.is_uncore = 1,
709-
.id = "v8",
710-
},
717+
.pmu_name = "uncore_sys_ddr_pmu0",
718+
.pmu_is_uncore = 1,
719+
.pmu_id = "v8",
711720
.aliases = {
712721
&sys_ddr_pmu_write_cycles,
713722
},
714723
},
715724
{
716-
.pmu = {
717-
.name = "uncore_sys_ccn_pmu4",
718-
.is_uncore = 1,
719-
.id = "0x01",
720-
},
725+
.pmu_name = "uncore_sys_ccn_pmu4",
726+
.pmu_is_uncore = 1,
727+
.pmu_id = "0x01",
721728
.aliases = {
722729
&sys_ccn_pmu_read_cycles,
723730
},
724731
},
725732
{
726-
.pmu = {
727-
.name = (char *)"uncore_sys_cmn_pmu0",
728-
.is_uncore = 1,
729-
.id = (char *)"43401",
730-
},
733+
.pmu_name = "uncore_sys_cmn_pmu0",
734+
.pmu_is_uncore = 1,
735+
.pmu_id = "43401",
731736
.aliases = {
732737
&sys_cmn_pmu_hnf_cache_miss,
733738
},
734739
},
735740
{
736-
.pmu = {
737-
.name = (char *)"uncore_sys_cmn_pmu0",
738-
.is_uncore = 1,
739-
.id = (char *)"43602",
740-
},
741+
.pmu_name = "uncore_sys_cmn_pmu0",
742+
.pmu_is_uncore = 1,
743+
.pmu_id = "43602",
741744
.aliases = {
742745
&sys_cmn_pmu_hnf_cache_miss,
743746
},
744747
},
745748
{
746-
.pmu = {
747-
.name = (char *)"uncore_sys_cmn_pmu0",
748-
.is_uncore = 1,
749-
.id = (char *)"43c03",
750-
},
749+
.pmu_name = "uncore_sys_cmn_pmu0",
750+
.pmu_is_uncore = 1,
751+
.pmu_id = "43c03",
751752
.aliases = {
752753
&sys_cmn_pmu_hnf_cache_miss,
753754
},
754755
},
755756
{
756-
.pmu = {
757-
.name = (char *)"uncore_sys_cmn_pmu0",
758-
.is_uncore = 1,
759-
.id = (char *)"43a01",
760-
},
757+
.pmu_name = "uncore_sys_cmn_pmu0",
758+
.pmu_is_uncore = 1,
759+
.pmu_id = "43a01",
761760
.aliases = {
762761
&sys_cmn_pmu_hnf_cache_miss,
763762
},
@@ -796,10 +795,6 @@ static int test__aliases(struct test_suite *test __maybe_unused,
796795
for (i = 0; i < ARRAY_SIZE(test_pmus); i++) {
797796
int res;
798797

799-
INIT_LIST_HEAD(&test_pmus[i].pmu.format);
800-
INIT_LIST_HEAD(&test_pmus[i].pmu.aliases);
801-
INIT_LIST_HEAD(&test_pmus[i].pmu.caps);
802-
803798
res = __test_uncore_pmu_event_aliases(&test_pmus[i]);
804799
if (res)
805800
return res;

tools/perf/util/hwmon_pmu.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -346,42 +346,43 @@ struct perf_pmu *hwmon_pmu__new(struct list_head *pmus, int hwmon_dir, const cha
346346
{
347347
char buf[32];
348348
struct hwmon_pmu *hwm;
349+
__u32 type = PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NULL, 10);
350+
351+
if (type > PERF_PMU_TYPE_HWMON_END) {
352+
pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_name);
353+
return NULL;
354+
}
355+
356+
snprintf(buf, sizeof(buf), "hwmon_%s", name);
357+
fix_name(buf + 6);
349358

350359
hwm = zalloc(sizeof(*hwm));
351360
if (!hwm)
352361
return NULL;
353362

354-
hwm->hwmon_dir_fd = hwmon_dir;
355-
hwm->pmu.type = PERF_PMU_TYPE_HWMON_START + strtoul(sysfs_name + 5, NULL, 10);
356-
if (hwm->pmu.type > PERF_PMU_TYPE_HWMON_END) {
357-
pr_err("Unable to encode hwmon type from %s in valid PMU type\n", sysfs_name);
358-
goto err_out;
363+
if (perf_pmu__init(&hwm->pmu, type, buf) != 0) {
364+
perf_pmu__delete(&hwm->pmu);
365+
return NULL;
359366
}
360-
snprintf(buf, sizeof(buf), "hwmon_%s", name);
361-
fix_name(buf + 6);
362-
hwm->pmu.name = strdup(buf);
363-
if (!hwm->pmu.name)
364-
goto err_out;
367+
368+
hwm->hwmon_dir_fd = hwmon_dir;
365369
hwm->pmu.alias_name = strdup(sysfs_name);
366-
if (!hwm->pmu.alias_name)
367-
goto err_out;
370+
if (!hwm->pmu.alias_name) {
371+
perf_pmu__delete(&hwm->pmu);
372+
return NULL;
373+
}
368374
hwm->pmu.cpus = perf_cpu_map__new("0");
369-
if (!hwm->pmu.cpus)
370-
goto err_out;
375+
if (!hwm->pmu.cpus) {
376+
perf_pmu__delete(&hwm->pmu);
377+
return NULL;
378+
}
371379
INIT_LIST_HEAD(&hwm->pmu.format);
372-
INIT_LIST_HEAD(&hwm->pmu.aliases);
373380
INIT_LIST_HEAD(&hwm->pmu.caps);
374381
hashmap__init(&hwm->events, hwmon_pmu__event_hashmap_hash,
375382
hwmon_pmu__event_hashmap_equal, /*ctx=*/NULL);
376383

377384
list_add_tail(&hwm->pmu.list, pmus);
378385
return &hwm->pmu;
379-
err_out:
380-
free((char *)hwm->pmu.name);
381-
free(hwm->pmu.alias_name);
382-
free(hwm);
383-
close(hwmon_dir);
384-
return NULL;
385386
}
386387

387388
void hwmon_pmu__exit(struct perf_pmu *pmu)

0 commit comments

Comments
 (0)