Skip to content

Commit cd4e1ef

Browse files
captain5050acmel
authored andcommitted
perf pmus: Skip duplicate PMUs and don't print list suffix by default
Add a PMUs scan that ignores duplicates. When there are multiple PMUs that differ only by suffix, by default just list the first one and skip all others. The scan routine checks that the PMU names match but doesn't enforce that the numbers are consecutive as for some PMUs there are gaps. If "-v" is passed to "perf list" then list all PMUs. With the previous change duplicate PMUs are no longer printed but the suffix of the first is printed. When duplicate PMUs are being skipped avoid printing the suffix. Before: $ perf list ... uncore_imc_free_running_0/data_read/ [Kernel PMU event] uncore_imc_free_running_0/data_total/ [Kernel PMU event] uncore_imc_free_running_0/data_write/ [Kernel PMU event] uncore_imc_free_running_1/data_read/ [Kernel PMU event] uncore_imc_free_running_1/data_total/ [Kernel PMU event] uncore_imc_free_running_1/data_write/ [Kernel PMU event] After: $ perf list ... uncore_imc_free_running/data_read/ [Kernel PMU event] uncore_imc_free_running/data_total/ [Kernel PMU event] uncore_imc_free_running/data_write/ [Kernel PMU event] ... $ perf list -v uncore_imc_free_running_0/data_read/ [Kernel PMU event] uncore_imc_free_running_0/data_total/ [Kernel PMU event] uncore_imc_free_running_0/data_write/ [Kernel PMU event] uncore_imc_free_running_1/data_read/ [Kernel PMU event] uncore_imc_free_running_1/data_total/ [Kernel PMU event] uncore_imc_free_running_1/data_write/ [Kernel PMU event] ... Reviewed-by: Kan Liang <kan.liang@linux.intel.com> Signed-off-by: Ian Rogers <irogers@google.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Jiri Olsa <jolsa@kernel.org> Cc: John Garry <john.g.garry@oracle.com> Cc: Kajol Jain <kjain@linux.ibm.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ravi Bangoria <ravi.bangoria@amd.com> Link: https://lore.kernel.org/r/20230825135237.921058-3-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 8d9f514 commit cd4e1ef

File tree

7 files changed

+76
-11
lines changed

7 files changed

+76
-11
lines changed

tools/perf/builtin-list.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,13 @@ static void json_print_metric(void *ps __maybe_unused, const char *group,
434434
strbuf_release(&buf);
435435
}
436436

437+
static bool default_skip_duplicate_pmus(void *ps)
438+
{
439+
struct print_state *print_state = ps;
440+
441+
return !print_state->long_desc;
442+
}
443+
437444
int cmd_list(int argc, const char **argv)
438445
{
439446
int i, ret = 0;
@@ -445,6 +452,7 @@ int cmd_list(int argc, const char **argv)
445452
.print_end = default_print_end,
446453
.print_event = default_print_event,
447454
.print_metric = default_print_metric,
455+
.skip_duplicate_pmus = default_skip_duplicate_pmus,
448456
};
449457
const char *cputype = NULL;
450458
const char *unit_name = NULL;

tools/perf/util/pmu.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,7 +1578,9 @@ int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, p
15781578
.cb = cb,
15791579
};
15801580

1581-
return perf_pmu__for_each_event(pmu, &args, find_event_callback);
1581+
/* Sub-optimal, but function is only used by tests. */
1582+
return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false,
1583+
&args, find_event_callback);
15821584
}
15831585

15841586
static void perf_pmu__del_formats(struct list_head *formats)
@@ -1652,10 +1654,13 @@ static int sub_non_neg(int a, int b)
16521654
}
16531655

16541656
static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
1655-
const struct perf_pmu_alias *alias)
1657+
const struct perf_pmu_alias *alias, bool skip_duplicate_pmus)
16561658
{
16571659
struct parse_events_term *term;
1658-
int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name);
1660+
int pmu_name_len = skip_duplicate_pmus
1661+
? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL)
1662+
: (int)strlen(pmu->name);
1663+
int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name);
16591664

16601665
list_for_each_entry(term, &alias->terms, list) {
16611666
if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR)
@@ -1677,7 +1682,8 @@ static char *format_alias(char *buf, int len, const struct perf_pmu *pmu,
16771682
return buf;
16781683
}
16791684

1680-
int perf_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb)
1685+
int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
1686+
void *state, pmu_event_callback cb)
16811687
{
16821688
char buf[1024];
16831689
struct perf_pmu_alias *event;
@@ -1696,7 +1702,8 @@ int perf_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callba
16961702
info.name = event->name;
16971703
buf_used = 0;
16981704
} else {
1699-
info.name = format_alias(buf, sizeof(buf), pmu, event);
1705+
info.name = format_alias(buf, sizeof(buf), pmu, event,
1706+
skip_duplicate_pmus);
17001707
if (pmu->is_core) {
17011708
info.alias = info.name;
17021709
info.name = event->name;

tools/perf/util/pmu.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu);
212212
bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu);
213213
bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name);
214214
size_t perf_pmu__num_events(struct perf_pmu *pmu);
215-
int perf_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb);
215+
int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus,
216+
void *state, pmu_event_callback cb);
216217
bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name);
217218

218219
/**

tools/perf/util/pmus.c

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static LIST_HEAD(other_pmus);
3636
static bool read_sysfs_core_pmus;
3737
static bool read_sysfs_all_pmus;
3838

39-
static int pmu_name_len_no_suffix(const char *str, unsigned long *num)
39+
int pmu_name_len_no_suffix(const char *str, unsigned long *num)
4040
{
4141
int orig_len, len;
4242

@@ -276,6 +276,43 @@ struct perf_pmu *perf_pmus__scan_core(struct perf_pmu *pmu)
276276
return NULL;
277277
}
278278

279+
static struct perf_pmu *perf_pmus__scan_skip_duplicates(struct perf_pmu *pmu)
280+
{
281+
bool use_core_pmus = !pmu || pmu->is_core;
282+
int last_pmu_name_len = 0;
283+
const char *last_pmu_name = (pmu && pmu->name) ? pmu->name : "";
284+
285+
if (!pmu) {
286+
pmu_read_sysfs(/*core_only=*/false);
287+
pmu = list_prepare_entry(pmu, &core_pmus, list);
288+
} else
289+
last_pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", NULL);
290+
291+
if (use_core_pmus) {
292+
list_for_each_entry_continue(pmu, &core_pmus, list) {
293+
int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
294+
295+
if (last_pmu_name_len == pmu_name_len &&
296+
!strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
297+
continue;
298+
299+
return pmu;
300+
}
301+
pmu = NULL;
302+
pmu = list_prepare_entry(pmu, &other_pmus, list);
303+
}
304+
list_for_each_entry_continue(pmu, &other_pmus, list) {
305+
int pmu_name_len = pmu_name_len_no_suffix(pmu->name ?: "", /*num=*/NULL);
306+
307+
if (last_pmu_name_len == pmu_name_len &&
308+
!strncmp(last_pmu_name, pmu->name ?: "", pmu_name_len))
309+
continue;
310+
311+
return pmu;
312+
}
313+
return NULL;
314+
}
315+
279316
const struct perf_pmu *perf_pmus__pmu_for_pmu_filter(const char *str)
280317
{
281318
struct perf_pmu *pmu = NULL;
@@ -401,10 +438,17 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *p
401438
int len;
402439
struct sevent *aliases;
403440
struct events_callback_state state;
441+
bool skip_duplicate_pmus = print_cb->skip_duplicate_pmus(print_state);
442+
struct perf_pmu *(*scan_fn)(struct perf_pmu *);
443+
444+
if (skip_duplicate_pmus)
445+
scan_fn = perf_pmus__scan_skip_duplicates;
446+
else
447+
scan_fn = perf_pmus__scan;
404448

405449
pmu = NULL;
406450
len = 0;
407-
while ((pmu = perf_pmus__scan(pmu)) != NULL)
451+
while ((pmu = scan_fn(pmu)) != NULL)
408452
len += perf_pmu__num_events(pmu);
409453

410454
aliases = zalloc(sizeof(struct sevent) * len);
@@ -418,8 +462,9 @@ void perf_pmus__print_pmu_events(const struct print_callbacks *print_cb, void *p
418462
.aliases_len = len,
419463
.index = 0,
420464
};
421-
while ((pmu = perf_pmus__scan(pmu)) != NULL) {
422-
perf_pmu__for_each_event(pmu, &state, perf_pmus__print_pmu_events__callback);
465+
while ((pmu = scan_fn(pmu)) != NULL) {
466+
perf_pmu__for_each_event(pmu, skip_duplicate_pmus, &state,
467+
perf_pmus__print_pmu_events__callback);
423468
}
424469
qsort(aliases, len, sizeof(struct sevent), cmp_sevent);
425470
for (int j = 0; j < len; j++) {

tools/perf/util/pmus.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
struct perf_pmu;
66
struct print_callbacks;
77

8+
int pmu_name_len_no_suffix(const char *str, unsigned long *num);
9+
810
void perf_pmus__destroy(void);
911

1012
struct perf_pmu *perf_pmus__find(const char *name);

tools/perf/util/print-events.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct print_callbacks {
2626
const char *expr,
2727
const char *threshold,
2828
const char *unit);
29+
bool (*skip_duplicate_pmus)(void *print_state);
2930
};
3031

3132
/** Print all events, the default when no options are specified. */

tools/perf/util/s390-sample-raw.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ static char *get_counter_name(int set, int nr, struct perf_pmu *pmu)
171171
if (!pmu)
172172
return NULL;
173173

174-
perf_pmu__for_each_event(pmu, &data, get_counter_name_callback);
174+
perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ true,
175+
&data, get_counter_name_callback);
175176
return data.result;
176177
}
177178

0 commit comments

Comments
 (0)