Skip to content

Commit 137359b

Browse files
captain5050acmel
authored andcommitted
perf parse-events: Use wildcard processing to set an event to merge into
The merge stat code fails for uncore events if they are repeated twice, for example `perf stat -e clockticks,clockticks -I 1000` as the counts of the second set of uncore events will be merged into the first counter. Reimplement the logic to have a first_wildcard_match so that merged later events correctly merge into the first wildcard event that they will be aggregated into. Signed-off-by: Ian Rogers <irogers@google.com> Tested-by: Chun-Tse Shao <ctshao@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Dr. David Alan Gilbert <linux@treblig.org> Cc: Howard Chu <howardchu95@gmail.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: James Clark <james.clark@linaro.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Kan Liang <kan.liang@linux.intel.com> Cc: Levi Yun <yeoreum.yun@arm.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Weilin Wang <weilin.wang@intel.com> Link: https://lore.kernel.org/r/20250513215401.2315949-3-ctshao@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 7d45f40 commit 137359b

5 files changed

Lines changed: 88 additions & 72 deletions

File tree

tools/perf/util/evsel.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,11 +552,11 @@ struct evsel *evsel__clone(struct evsel *dest, struct evsel *orig)
552552

553553
evsel->exclude_GH = orig->exclude_GH;
554554
evsel->sample_read = orig->sample_read;
555-
evsel->auto_merge_stats = orig->auto_merge_stats;
556555
evsel->collect_stat = orig->collect_stat;
557556
evsel->weak_group = orig->weak_group;
558557
evsel->use_config_name = orig->use_config_name;
559558
evsel->pmu = orig->pmu;
559+
evsel->first_wildcard_match = orig->first_wildcard_match;
560560

561561
if (evsel__copy_config_terms(evsel, orig) < 0)
562562
goto out_err;
@@ -3964,11 +3964,6 @@ bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_con
39643964
return true;
39653965
}
39663966

3967-
if (counter->merged_stat) {
3968-
/* Counter won't be shown. */
3969-
return false;
3970-
}
3971-
39723967
if (counter->use_config_name || counter->is_libpfm_event) {
39733968
/* Original name will be used. */
39743969
return false;
@@ -3997,12 +3992,21 @@ bool evsel__set_needs_uniquify(struct evsel *counter, const struct perf_stat_con
39973992
return true;
39983993
}
39993994

3995+
if (counter->first_wildcard_match != NULL) {
3996+
/*
3997+
* If stats are merged then only the first_wildcard_match is
3998+
* displayed, there is no need to uniquify this evsel as the
3999+
* name won't be shown.
4000+
*/
4001+
return false;
4002+
}
4003+
40004004
/*
40014005
* Do other non-merged events in the evlist have the same name? If so
40024006
* uniquify is necessary.
40034007
*/
40044008
evlist__for_each_entry(counter->evlist, evsel) {
4005-
if (evsel == counter || evsel->merged_stat || evsel->pmu == counter->pmu)
4009+
if (evsel == counter || evsel->first_wildcard_match || evsel->pmu == counter->pmu)
40064010
continue;
40074011

40084012
if (evsel__name_is(counter, evsel__name(evsel))) {

tools/perf/util/evsel.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ struct evsel {
7070
const char *unit;
7171
struct cgroup *cgrp;
7272
const char *metric_id;
73+
/*
74+
* This point to the first evsel with the same name, intended to store the
75+
* aggregated counts in aggregation mode.
76+
*/
77+
struct evsel *first_wildcard_match;
7378
/* parse modifier helper */
7479
int exclude_GH;
7580
int sample_read;
@@ -78,7 +83,6 @@ struct evsel {
7883
bool percore;
7984
bool precise_max;
8085
bool is_libpfm_event;
81-
bool auto_merge_stats;
8286
bool collect_stat;
8387
bool weak_group;
8488
bool bpf_counter;
@@ -115,7 +119,6 @@ struct evsel {
115119
bool ignore_missing_thread;
116120
bool forced_leader;
117121
bool cmdline_group_boundary;
118-
bool merged_stat;
119122
bool reset_group;
120123
bool errored;
121124
bool needs_auxtrace_mmap;

tools/perf/util/parse-events.c

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,13 +250,34 @@ __add_event(struct list_head *list, int *idx,
250250
struct perf_event_attr *attr,
251251
bool init_attr,
252252
const char *name, const char *metric_id, struct perf_pmu *pmu,
253-
struct list_head *config_terms, bool auto_merge_stats,
253+
struct list_head *config_terms, struct evsel *first_wildcard_match,
254254
struct perf_cpu_map *cpu_list, u64 alternate_hw_config)
255255
{
256256
struct evsel *evsel;
257257
bool is_pmu_core;
258258
struct perf_cpu_map *cpus;
259259

260+
/*
261+
* Ensure the first_wildcard_match's PMU matches that of the new event
262+
* being added. Otherwise try to match with another event further down
263+
* the evlist.
264+
*/
265+
if (first_wildcard_match) {
266+
struct evsel *pos = list_prev_entry(first_wildcard_match, core.node);
267+
268+
first_wildcard_match = NULL;
269+
list_for_each_entry_continue(pos, list, core.node) {
270+
if (perf_pmu__name_no_suffix_match(pos->pmu, pmu->name)) {
271+
first_wildcard_match = pos;
272+
break;
273+
}
274+
if (pos->pmu->is_core && (!pmu || pmu->is_core)) {
275+
first_wildcard_match = pos;
276+
break;
277+
}
278+
}
279+
}
280+
260281
if (pmu) {
261282
is_pmu_core = pmu->is_core;
262283
cpus = perf_cpu_map__get(perf_cpu_map__is_empty(cpu_list) ? pmu->cpus : cpu_list);
@@ -293,9 +314,9 @@ __add_event(struct list_head *list, int *idx,
293314
evsel->core.own_cpus = perf_cpu_map__get(cpus);
294315
evsel->core.requires_cpu = pmu ? pmu->is_uncore : false;
295316
evsel->core.is_pmu_core = is_pmu_core;
296-
evsel->auto_merge_stats = auto_merge_stats;
297317
evsel->pmu = pmu;
298318
evsel->alternate_hw_config = alternate_hw_config;
319+
evsel->first_wildcard_match = first_wildcard_match;
299320

300321
if (name)
301322
evsel->name = strdup(name);
@@ -318,7 +339,7 @@ struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr,
318339
{
319340
return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name,
320341
metric_id, pmu, /*config_terms=*/NULL,
321-
/*auto_merge_stats=*/false, /*cpu_list=*/NULL,
342+
/*first_wildcard_match=*/NULL, /*cpu_list=*/NULL,
322343
/*alternate_hw_config=*/PERF_COUNT_HW_MAX);
323344
}
324345

@@ -329,7 +350,7 @@ static int add_event(struct list_head *list, int *idx,
329350
{
330351
return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id,
331352
/*pmu=*/NULL, config_terms,
332-
/*auto_merge_stats=*/false, /*cpu_list=*/NULL,
353+
/*first_wildcard_match=*/NULL, /*cpu_list=*/NULL,
333354
alternate_hw_config) ? 0 : -ENOMEM;
334355
}
335356

@@ -454,7 +475,7 @@ bool parse_events__filter_pmu(const struct parse_events_state *parse_state,
454475
static int parse_events_add_pmu(struct parse_events_state *parse_state,
455476
struct list_head *list, struct perf_pmu *pmu,
456477
const struct parse_events_terms *const_parsed_terms,
457-
bool auto_merge_stats, u64 alternate_hw_config);
478+
struct evsel *first_wildcard_match, u64 alternate_hw_config);
458479

459480
int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
460481
struct parse_events_state *parse_state,
@@ -466,6 +487,7 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
466487
const char *metric_id = get_config_metric_id(parsed_terms);
467488
struct perf_cpu_map *cpus = get_config_cpu(parsed_terms);
468489
int ret = 0;
490+
struct evsel *first_wildcard_match = NULL;
469491

470492
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
471493
LIST_HEAD(config_terms);
@@ -481,10 +503,13 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
481503
*/
482504
ret = parse_events_add_pmu(parse_state, list, pmu,
483505
parsed_terms,
484-
perf_pmu__auto_merge_stats(pmu),
506+
first_wildcard_match,
485507
/*alternate_hw_config=*/PERF_COUNT_HW_MAX);
486508
if (ret)
487509
goto out_err;
510+
if (first_wildcard_match == NULL)
511+
first_wildcard_match =
512+
container_of(list->prev, struct evsel, core.node);
488513
continue;
489514
}
490515

@@ -515,10 +540,12 @@ int parse_events_add_cache(struct list_head *list, int *idx, const char *name,
515540
}
516541

517542
if (__add_event(list, idx, &attr, /*init_attr*/true, config_name ?: name,
518-
metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
543+
metric_id, pmu, &config_terms, first_wildcard_match,
519544
cpus, /*alternate_hw_config=*/PERF_COUNT_HW_MAX) == NULL)
520545
ret = -ENOMEM;
521546

547+
if (first_wildcard_match == NULL)
548+
first_wildcard_match = container_of(list->prev, struct evsel, core.node);
522549
free_config_terms(&config_terms);
523550
if (ret)
524551
goto out_err;
@@ -1387,7 +1414,8 @@ int parse_events_add_tracepoint(struct parse_events_state *parse_state,
13871414
static int __parse_events_add_numeric(struct parse_events_state *parse_state,
13881415
struct list_head *list,
13891416
struct perf_pmu *pmu, u32 type, u32 extended_type,
1390-
u64 config, const struct parse_events_terms *head_config)
1417+
u64 config, const struct parse_events_terms *head_config,
1418+
struct evsel *first_wildcard_match)
13911419
{
13921420
struct perf_event_attr attr;
13931421
LIST_HEAD(config_terms);
@@ -1416,7 +1444,7 @@ static int __parse_events_add_numeric(struct parse_events_state *parse_state,
14161444
metric_id = get_config_metric_id(head_config);
14171445
cpus = get_config_cpu(head_config);
14181446
ret = __add_event(list, &parse_state->idx, &attr, /*init_attr*/true, name,
1419-
metric_id, pmu, &config_terms, /*auto_merge_stats=*/false,
1447+
metric_id, pmu, &config_terms, first_wildcard_match,
14201448
cpus, /*alternate_hw_config=*/PERF_COUNT_HW_MAX) ? 0 : -ENOMEM;
14211449
perf_cpu_map__put(cpus);
14221450
free_config_terms(&config_terms);
@@ -1434,6 +1462,7 @@ int parse_events_add_numeric(struct parse_events_state *parse_state,
14341462

14351463
/* Wildcards on numeric values are only supported by core PMUs. */
14361464
if (wildcard && perf_pmus__supports_extended_type()) {
1465+
struct evsel *first_wildcard_match = NULL;
14371466
while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
14381467
int ret;
14391468

@@ -1443,15 +1472,20 @@ int parse_events_add_numeric(struct parse_events_state *parse_state,
14431472

14441473
ret = __parse_events_add_numeric(parse_state, list, pmu,
14451474
type, pmu->type,
1446-
config, head_config);
1475+
config, head_config,
1476+
first_wildcard_match);
14471477
if (ret)
14481478
return ret;
1479+
if (first_wildcard_match == NULL)
1480+
first_wildcard_match =
1481+
container_of(list->prev, struct evsel, core.node);
14491482
}
14501483
if (found_supported)
14511484
return 0;
14521485
}
14531486
return __parse_events_add_numeric(parse_state, list, perf_pmus__find_by_type(type),
1454-
type, /*extended_type=*/0, config, head_config);
1487+
type, /*extended_type=*/0, config, head_config,
1488+
/*first_wildcard_match=*/NULL);
14551489
}
14561490

14571491
static bool config_term_percore(struct list_head *config_terms)
@@ -1469,7 +1503,7 @@ static bool config_term_percore(struct list_head *config_terms)
14691503
static int parse_events_add_pmu(struct parse_events_state *parse_state,
14701504
struct list_head *list, struct perf_pmu *pmu,
14711505
const struct parse_events_terms *const_parsed_terms,
1472-
bool auto_merge_stats, u64 alternate_hw_config)
1506+
struct evsel *first_wildcard_match, u64 alternate_hw_config)
14731507
{
14741508
struct perf_event_attr attr;
14751509
struct perf_pmu_info info;
@@ -1506,7 +1540,7 @@ static int parse_events_add_pmu(struct parse_events_state *parse_state,
15061540
evsel = __add_event(list, &parse_state->idx, &attr,
15071541
/*init_attr=*/true, /*name=*/NULL,
15081542
/*metric_id=*/NULL, pmu,
1509-
/*config_terms=*/NULL, auto_merge_stats,
1543+
/*config_terms=*/NULL, first_wildcard_match,
15101544
/*cpu_list=*/NULL, alternate_hw_config);
15111545
return evsel ? 0 : -ENOMEM;
15121546
}
@@ -1577,7 +1611,7 @@ static int parse_events_add_pmu(struct parse_events_state *parse_state,
15771611
evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true,
15781612
get_config_name(&parsed_terms),
15791613
get_config_metric_id(&parsed_terms), pmu,
1580-
&config_terms, auto_merge_stats, term_cpu, alternate_hw_config);
1614+
&config_terms, first_wildcard_match, term_cpu, alternate_hw_config);
15811615
perf_cpu_map__put(term_cpu);
15821616
if (!evsel) {
15831617
parse_events_terms__exit(&parsed_terms);
@@ -1614,6 +1648,7 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
16141648
int ok = 0;
16151649
const char *config;
16161650
struct parse_events_terms parsed_terms;
1651+
struct evsel *first_wildcard_match = NULL;
16171652

16181653
*listp = NULL;
16191654

@@ -1646,17 +1681,14 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
16461681
INIT_LIST_HEAD(list);
16471682

16481683
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
1649-
bool auto_merge_stats;
1650-
16511684
if (parse_events__filter_pmu(parse_state, pmu))
16521685
continue;
16531686

16541687
if (!perf_pmu__have_event(pmu, event_name))
16551688
continue;
16561689

1657-
auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
16581690
if (!parse_events_add_pmu(parse_state, list, pmu,
1659-
&parsed_terms, auto_merge_stats, hw_config)) {
1691+
&parsed_terms, first_wildcard_match, hw_config)) {
16601692
struct strbuf sb;
16611693

16621694
strbuf_init(&sb, /*hint=*/ 0);
@@ -1665,11 +1697,13 @@ int parse_events_multi_pmu_add(struct parse_events_state *parse_state,
16651697
strbuf_release(&sb);
16661698
ok++;
16671699
}
1700+
if (first_wildcard_match == NULL)
1701+
first_wildcard_match = container_of(list->prev, struct evsel, core.node);
16681702
}
16691703

16701704
if (parse_state->fake_pmu) {
16711705
if (!parse_events_add_pmu(parse_state, list, perf_pmus__fake_pmu(), &parsed_terms,
1672-
/*auto_merge_stats=*/true, hw_config)) {
1706+
first_wildcard_match, hw_config)) {
16731707
struct strbuf sb;
16741708

16751709
strbuf_init(&sb, /*hint=*/ 0);
@@ -1700,6 +1734,7 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
17001734
struct perf_pmu *pmu;
17011735
int ok = 0;
17021736
char *help;
1737+
struct evsel *first_wildcard_match = NULL;
17031738

17041739
*listp = malloc(sizeof(**listp));
17051740
if (!*listp)
@@ -1710,14 +1745,14 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
17101745
/* Attempt to add to list assuming event_or_pmu is a PMU name. */
17111746
pmu = perf_pmus__find(event_or_pmu);
17121747
if (pmu && !parse_events_add_pmu(parse_state, *listp, pmu, const_parsed_terms,
1713-
/*auto_merge_stats=*/false,
1748+
first_wildcard_match,
17141749
/*alternate_hw_config=*/PERF_COUNT_HW_MAX))
17151750
return 0;
17161751

17171752
if (parse_state->fake_pmu) {
17181753
if (!parse_events_add_pmu(parse_state, *listp, perf_pmus__fake_pmu(),
17191754
const_parsed_terms,
1720-
/*auto_merge_stats=*/false,
1755+
first_wildcard_match,
17211756
/*alternate_hw_config=*/PERF_COUNT_HW_MAX))
17221757
return 0;
17231758
}
@@ -1727,15 +1762,16 @@ int parse_events_multi_pmu_add_or_add_pmu(struct parse_events_state *parse_state
17271762
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
17281763
if (!parse_events__filter_pmu(parse_state, pmu) &&
17291764
perf_pmu__wildcard_match(pmu, event_or_pmu)) {
1730-
bool auto_merge_stats = perf_pmu__auto_merge_stats(pmu);
1731-
17321765
if (!parse_events_add_pmu(parse_state, *listp, pmu,
17331766
const_parsed_terms,
1734-
auto_merge_stats,
1767+
first_wildcard_match,
17351768
/*alternate_hw_config=*/PERF_COUNT_HW_MAX)) {
17361769
ok++;
17371770
parse_state->wild_card_pmus = true;
17381771
}
1772+
if (first_wildcard_match == NULL)
1773+
first_wildcard_match =
1774+
container_of((*listp)->prev, struct evsel, core.node);
17391775
}
17401776
}
17411777
if (ok)

tools/perf/util/stat-display.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,8 +1002,15 @@ static void print_counter_aggrdata(struct perf_stat_config *config,
10021002
os->evsel = counter;
10031003

10041004
/* Skip already merged uncore/hybrid events */
1005-
if (counter->merged_stat)
1006-
return;
1005+
if (config->aggr_mode != AGGR_NONE) {
1006+
if (evsel__is_hybrid(counter)) {
1007+
if (config->hybrid_merge && counter->first_wildcard_match != NULL)
1008+
return;
1009+
} else {
1010+
if (counter->first_wildcard_match != NULL)
1011+
return;
1012+
}
1013+
}
10071014

10081015
val = aggr->counts.val;
10091016
ena = aggr->counts.ena;

0 commit comments

Comments
 (0)