Skip to contentSkip to conversation

perf list: Print matching PMU events for --unit

PATCH Superseded Namhyung Kim 2 participants 6 messages Nov 18, 2025 – Nov 19, 2025 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/3 perf list: Print matching PMU events for --unit

When --unit option is used, pmu_glob is set to the argument. It should match with event PMU and display the matching ones only. But it also shows raw events and metrics after that.

  $ perf list --unit tool
  List of pre-defined events (to be used in -e or -M):

tool:
core_wide
[1 if not SMT,if SMT are events being gathered on all SMT threads 1 otherwise 0. Unit: tool]
duration_time
[Wall clock interval time in nanoseconds. Unit: tool]
has_pmem
[1 if persistent memory installed otherwise 0. Unit: tool]
num_cores
[Number of cores. A core consists of 1 or more thread,with each thread being associated with a logical Linux CPU. Unit: tool]
num_cpus
[Number of logical Linux CPUs. There may be multiple such CPUs on a core. Unit: tool]
...
rNNN [Raw event descriptor]
cpu/event=0..255,pc,edge,.../modifier [Raw event descriptor]
[(see 'man perf-list' or 'man perf-record' on how to encode it)]
breakpoint//modifier [Raw event descriptor]
cstate_core/event=0..0xffffffffffffffff/modifier [Raw event descriptor]
cstate_pkg/event=0..0xffffffffffffffff/modifier [Raw event descriptor]
drm_i915//modifier [Raw event descriptor]
hwmon_acpitz//modifier [Raw event descriptor]
hwmon_ac//modifier [Raw event descriptor]
hwmon_bat0//modifier [Raw event descriptor]
hwmon_coretemp//modifier [Raw event descriptor]
...

Metric Groups:

Backend: [Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet]
tma_core_bound
[This metric represents fraction of slots where Core non-memory issues were of a bottleneck]
tma_info_core_ilp
[Instruction-Level-Parallelism (average number of uops executed when there is execution) per thread (logical-processor)]
tma_info_memory_l2mpki
[L2 cache true misses per kilo instruction for retired demand loads]
...

This change makes it print the tool PMU events only.

---
 tools/perf/builtin-list.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-list.c +5 -3
index 16400366f8276a7a..28bf1fc7f5eeff8f 100644
@@ -130,7 +130,7 @@ static void default_print_event(void *ps, const char *topic,
 	if (deprecated && !print_state->deprecated)
 		return;
 
-	if (print_state->pmu_glob && pmu_name && !strglobmatch(pmu_name, print_state->pmu_glob))
+	if (print_state->pmu_glob && (!pmu_name || !strglobmatch(pmu_name, print_state->pmu_glob)))
 		return;
 
 	if (print_state->exclude_abi && pmu_type < PERF_TYPE_MAX && pmu_type != PERF_TYPE_RAW)
@@ -612,8 +612,10 @@ int cmd_list(int argc, const char **argv)
 	print_cb.print_start(ps);
 
 	if (argc == 0) {
-		default_ps.metrics = true;
-		default_ps.metricgroups = true;
+		if (!unit_name) {
+			default_ps.metrics = true;
+			default_ps.metricgroups = true;
+		}
 		print_events(&print_cb, ps);
 		goto out;
 	}
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
author maintainer
#1 lore raw

PATCH 2/3 perf list: Get rid of json_print_state

The JSON print state has only one different field (need_sep). Let's move it to the default print state and use it. It would waste a little bit of more spaces but who cares? :)

---
 tools/perf/builtin-list.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
tools/perf/builtin-list.c +5 -7
index 28bf1fc7f5eeff8f..1ab969ffe371c5cb 100644
@@ -60,6 +60,8 @@ struct print_state {
 	bool metricgroups;
 	/** @exclude_abi: Exclude PMUs with types less than PERF_TYPE_MAX except PERF_TYPE_RAW. */
 	bool exclude_abi;
+	/** @need_sep: Should a separator be printed prior to the next item? (for JSON) */
+	bool need_sep;
 	/** @last_topic: The last printed event topic. */
 	char *last_topic;
 	/** @last_metricgroups: The last printed metric group. */
@@ -368,7 +370,7 @@ static void json_print_event(void *ps, const char *topic,
 			     const char *desc, const char *long_desc,
 			     const char *encoding_desc)
 {
-	struct json_print_state *print_state = ps;
+	struct print_state *print_state = ps;
 	bool need_sep = false;
 	FILE *fp = print_state->fp;
 	struct strbuf buf;
@@ -444,7 +446,7 @@ static void json_print_metric(void *ps __maybe_unused, const char *group,
 			      const char *threshold, const char *unit,
 			      const char *pmu_name)
 {
-	struct json_print_state *print_state = ps;
+	struct print_state *print_state = ps;
 	bool need_sep = false;
 	FILE *fp = print_state->fp;
 	struct strbuf buf;
@@ -521,9 +523,6 @@ int cmd_list(int argc, const char **argv)
 		.fp = stdout,
 		.desc = true,
 	};
-	struct print_state json_ps = {
-		.fp = stdout,
-	};
 	void *ps = &default_ps;
 	struct print_callbacks print_cb = {
 		.print_start = default_print_start,
@@ -574,7 +573,6 @@ int cmd_list(int argc, const char **argv)
 
 	if (output_path) {
 		default_ps.fp = fopen(output_path, "w");
-		json_ps.fp = default_ps.fp;
 	}
 
 	setup_pager();
@@ -590,7 +588,7 @@ int cmd_list(int argc, const char **argv)
 			.print_metric = json_print_metric,
 			.skip_duplicate_pmus = json_skip_duplicate_pmus,
 		};
-		ps = &json_ps;
+		ps = &default_ps;
 	} else {
 		default_ps.last_topic = strdup("");
 		assert(default_ps.last_topic);
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
reviewer
#2 lore raw

Re: [PATCH 2/3] perf list: Get rid of json_print_state

Quoted 15 lines · Namhyung Kim

The JSON print state has only one different field (need_sep). Let's move it to the default print state and use it. It would waste a little bit of more spaces but who cares? :)

---
 tools/perf/builtin-list.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
tools/perf/builtin-list.c +0 -0
index 28bf1fc7f5eeff8f..1ab969ffe371c5cb 100644

tools/perf/builtin-list.c struct print_state { · line 64

Quoted 5 lines
tools/perf/builtin-list.c +2 -0
@@ -60,6 +60,8 @@ struct print_state {
        bool metricgroups;
        /** @exclude_abi: Exclude PMUs with types less than PERF_TYPE_MAX except PERF_TYPE_RAW. */
        bool exclude_abi;
+       /** @need_sep: Should a separator be printed prior to the next item? (for JSON) */
+       bool need_sep;

I'm concerned we might be reinventing something like the stat-display code :-)

Perhaps this would be cleaner as:

```
struct json_print_state {
  /** State for regular filtering, etc. */
  struct print_state ps;
  /** Should a json separator ',' be printed prior to the next item? */
  bool need_sep;
}
```

So we have the regular print state stuff in the json print_state but don't expose json-ness things to other print_state users - and hopefully avoid convoluted shared state, function pointers, etc.

Thanks,
Ian

Quoted 51 lines
tools/perf/builtin-list.c +3 -8
        /** @last_topic: The last printed event topic. */
        char *last_topic;
        /** @last_metricgroups: The last printed metric group. */
@@ -368,7 +370,7 @@ static void json_print_event(void *ps, const char *topic,
                             const char *desc, const char *long_desc,
                             const char *encoding_desc)
 {
-       struct json_print_state *print_state = ps;
+       struct print_state *print_state = ps;
        bool need_sep = false;
        FILE *fp = print_state->fp;
        struct strbuf buf;
@@ -444,7 +446,7 @@ static void json_print_metric(void *ps __maybe_unused, const char *group,
                              const char *threshold, const char *unit,
                              const char *pmu_name)
 {
-       struct json_print_state *print_state = ps;
+       struct print_state *print_state = ps;
        bool need_sep = false;
        FILE *fp = print_state->fp;
        struct strbuf buf;
@@ -521,9 +523,6 @@ int cmd_list(int argc, const char **argv)
                .fp = stdout,
                .desc = true,
        };
-       struct print_state json_ps = {
-               .fp = stdout,
-       };
        void *ps = &default_ps;
        struct print_callbacks print_cb = {
                .print_start = default_print_start,
@@ -574,7 +573,6 @@ int cmd_list(int argc, const char **argv)
 
        if (output_path) {
                default_ps.fp = fopen(output_path, "w");
-               json_ps.fp = default_ps.fp;
        }
 
        setup_pager();
@@ -590,7 +588,7 @@ int cmd_list(int argc, const char **argv)
                        .print_metric = json_print_metric,
                        .skip_duplicate_pmus = json_skip_duplicate_pmus,
                };
-               ps = &json_ps;
+               ps = &default_ps;
        } else {
                default_ps.last_topic = strdup("");
                assert(default_ps.last_topic);
--

2.52.0.rc1.455.g30608eb744-goog

author maintainer
#3 lore raw

PATCH 3/3 perf list: Support filtering in JSON output

In reply to Namhyung Kim

Like regular output mode, it should honor command line arguments to limit to a certain type of PMUs or events.

  $ perf list -j hw
  [
  {
          "Unit": "cpu",
          "Topic": "legacy hardware",
          "EventName": "branch-instructions",
          "EventType": "Kernel PMU event",
          "BriefDescription": "Retired branch instructions [This event is an alias of branches]",
          "Encoding": "cpu/event=0xc4\n/"
  },
  {
          "Unit": "cpu",
          "Topic": "legacy hardware",
          "EventName": "branch-misses",
          "EventType": "Kernel PMU event",
          "BriefDescription": "Mispredicted branch instructions",
          "Encoding": "cpu/event=0xc5\n/"
  },
  ...
---
 tools/perf/builtin-list.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tools/perf/builtin-list.c +20 -0
index 1ab969ffe371c5cb..48c799007414bf6e 100644
@@ -375,6 +375,21 @@ static void json_print_event(void *ps, const char *topic,
 	FILE *fp = print_state->fp;
 	struct strbuf buf;
 
+	if (deprecated && !print_state->deprecated)
+		return;
+
+	if (print_state->pmu_glob && (!pmu_name || !strglobmatch(pmu_name, print_state->pmu_glob)))
+		return;
+
+	if (print_state->exclude_abi && pmu_type < PERF_TYPE_MAX && pmu_type != PERF_TYPE_RAW)
+		return;
+
+	if (print_state->event_glob &&
+	    (!event_name || !strglobmatch(event_name, print_state->event_glob)) &&
+	    (!event_alias || !strglobmatch(event_alias, print_state->event_glob)) &&
+	    (!topic || !strglobmatch_nocase(topic, print_state->event_glob)))
+		return;
+
 	strbuf_init(&buf, 0);
 	fprintf(fp, "%s{\n", print_state->need_sep ? ",\n" : "");
 	print_state->need_sep = true;
@@ -451,6 +466,11 @@ static void json_print_metric(void *ps __maybe_unused, const char *group,
 	FILE *fp = print_state->fp;
 	struct strbuf buf;
 
+	if (print_state->event_glob &&
+	    (!print_state->metrics || !name || !strglobmatch(name, print_state->event_glob)) &&
+	    (!print_state->metricgroups || !group || !strglobmatch(group, print_state->event_glob)))
+		return;
+
 	strbuf_init(&buf, 0);
 	fprintf(fp, "%s{\n", print_state->need_sep ? ",\n" : "");
 	print_state->need_sep = true;
Signature · 1 line
2.52.0.rc1.455.g30608eb744-goog
reviewer
#4 lore raw

Re: [PATCH 3/3] perf list: Support filtering in JSON output

Quoted 4 lines · Namhyung Kim

Like regular output mode, it should honor command line arguments to limit to a certain type of PMUs or events.

Quoted 9 lines
  $ perf list -j hw
  [
  {
          "Unit": "cpu",
          "Topic": "legacy hardware",
          "EventName": "branch-instructions",
          "EventType": "Kernel PMU event",
          "BriefDescription": "Retired branch instructions [This event is an alias of branches]",
          "Encoding": "cpu/event=0xc4\n/"

The new-line '\n' here looks like another bug, the bug is coming from reading the event from sysfs but not stripping any '\n'.

This change lgtm but we need to figure out the struct json_print_state in patch 2.

Thanks,
Ian

Quoted 57 lines
  },
  {
          "Unit": "cpu",
          "Topic": "legacy hardware",
          "EventName": "branch-misses",
          "EventType": "Kernel PMU event",
          "BriefDescription": "Mispredicted branch instructions",
          "Encoding": "cpu/event=0xc5\n/"
  },
  ...
---
 tools/perf/builtin-list.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
tools/perf/builtin-list.c +20 -1
index 1ab969ffe371c5cb..48c799007414bf6e 100644
@@ -375,6 +375,21 @@ static void json_print_event(void *ps, const char *topic,
        FILE *fp = print_state->fp;
        struct strbuf buf;
 
+       if (deprecated && !print_state->deprecated)
+               return;
+
+       if (print_state->pmu_glob && (!pmu_name || !strglobmatch(pmu_name, print_state->pmu_glob)))
+               return;
+
+       if (print_state->exclude_abi && pmu_type < PERF_TYPE_MAX && pmu_type != PERF_TYPE_RAW)
+               return;
+
+       if (print_state->event_glob &&
+           (!event_name || !strglobmatch(event_name, print_state->event_glob)) &&
+           (!event_alias || !strglobmatch(event_alias, print_state->event_glob)) &&
+           (!topic || !strglobmatch_nocase(topic, print_state->event_glob)))
+               return;
+
        strbuf_init(&buf, 0);
        fprintf(fp, "%s{\n", print_state->need_sep ? ",\n" : "");
        print_state->need_sep = true;
@@ -451,6 +466,11 @@ static void json_print_metric(void *ps __maybe_unused, const char *group,
        FILE *fp = print_state->fp;
        struct strbuf buf;
 
+       if (print_state->event_glob &&
+           (!print_state->metrics || !name || !strglobmatch(name, print_state->event_glob)) &&
+           (!print_state->metricgroups || !group || !strglobmatch(group, print_state->event_glob)))
+               return;
+
        strbuf_init(&buf, 0);
        fprintf(fp, "%s{\n", print_state->need_sep ? ",\n" : "");
        print_state->need_sep = true;
--

2.52.0.rc1.455.g30608eb744-goog

reviewer Reviewed-by on v1 1/3
#5 lore raw
In reply to Namhyung Kim
Quoted 44 lines · Namhyung Kim

When --unit option is used, pmu_glob is set to the argument. It should match with event PMU and display the matching ones only. But it also shows raw events and metrics after that.

  $ perf list --unit tool
  List of pre-defined events (to be used in -e or -M):

tool:
core_wide
[1 if not SMT,if SMT are events being gathered on all SMT threads 1 otherwise 0. Unit: tool]
duration_time
[Wall clock interval time in nanoseconds. Unit: tool]
has_pmem
[1 if persistent memory installed otherwise 0. Unit: tool]
num_cores
[Number of cores. A core consists of 1 or more thread,with each thread being associated with a logical Linux CPU. Unit: tool]
num_cpus
[Number of logical Linux CPUs. There may be multiple such CPUs on a core. Unit: tool]
...
rNNN [Raw event descriptor]
cpu/event=0..255,pc,edge,.../modifier [Raw event descriptor]
[(see 'man perf-list' or 'man perf-record' on how to encode it)]
breakpoint//modifier [Raw event descriptor]
cstate_core/event=0..0xffffffffffffffff/modifier [Raw event descriptor]
cstate_pkg/event=0..0xffffffffffffffff/modifier [Raw event descriptor]
drm_i915//modifier [Raw event descriptor]
hwmon_acpitz//modifier [Raw event descriptor]
hwmon_ac//modifier [Raw event descriptor]
hwmon_bat0//modifier [Raw event descriptor]
hwmon_coretemp//modifier [Raw event descriptor]
...

Metric Groups:

Backend: [Grouping from Top-down Microarchitecture Analysis Metrics spreadsheet]
tma_core_bound
[This metric represents fraction of slots where Core non-memory issues were of a bottleneck]
tma_info_core_ilp
[Instruction-Level-Parallelism (average number of uops executed when there is execution) per thread (logical-processor)]
tma_info_memory_l2mpki
[L2 cache true misses per kilo instruction for retired demand loads]
...

Quoted 1 line

This change makes it print the tool PMU events only.

Perhaps this can be used to simplify tests like:
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian

Quoted 34 lines
---
 tools/perf/builtin-list.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-list.c +5 -4
index 16400366f8276a7a..28bf1fc7f5eeff8f 100644
@@ -130,7 +130,7 @@ static void default_print_event(void *ps, const char *topic,
        if (deprecated && !print_state->deprecated)
                return;
 
-       if (print_state->pmu_glob && pmu_name && !strglobmatch(pmu_name, print_state->pmu_glob))
+       if (print_state->pmu_glob && (!pmu_name || !strglobmatch(pmu_name, print_state->pmu_glob)))
                return;
 
        if (print_state->exclude_abi && pmu_type < PERF_TYPE_MAX && pmu_type != PERF_TYPE_RAW)
@@ -612,8 +612,10 @@ int cmd_list(int argc, const char **argv)
        print_cb.print_start(ps);
 
        if (argc == 0) {
-               default_ps.metrics = true;
-               default_ps.metricgroups = true;
+               if (!unit_name) {
+                       default_ps.metrics = true;
+                       default_ps.metricgroups = true;
+               }
                print_events(&print_cb, ps);
                goto out;
        }
--

2.52.0.rc1.455.g30608eb744-goog