Skip to contentSkip to conversation

perf trace: Introduce --show-cpu option to display cpu id

PATCH Superseded Aaron Tomlin 2 participants 2 messages Apr 23 perf events

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf trace: Introduce --show-cpu option to display cpu id

When tracing system-wide workloads or specific events, it is highly valuable to know exactly which CPU executed a specific event. Currently, perf trace output defaults to omitting CPU information.

Introduce a new "--show-cpu" command-line option. When provided, this flag extracts the CPU from the perf sample and prints it in a "[000]" format immediately following the timestamp. This mirrors the behaviour of other tracing tools like ftrace and perf script. For example:

  # perf trace -e sched:sched_switch --max-events 5 --show-cpu
       0.000 [002] :0/0 sched:sched_switch(prev_comm: "swapper/2", prev_prio: 120, next_comm: "rcu_preempt", next_pid: 16 (rcu_preempt), next_prio: 120)
       0.009 [002] rcu_preempt/16 sched:sched_switch(prev_comm: "rcu_preempt", prev_pid: 16 (rcu_preempt), prev_prio: 120, prev_state: 128, next_comm: "swapper/2", next_prio: 120)
       0.033 [002] :0/0 sched:sched_switch(prev_comm: "swapper/2", prev_prio: 120, next_comm: "kworker/u32:48", next_pid: 35840 (kworker/u32:48-), next_prio: 120)
       0.041 [002] kworker/u32:48/35840 sched:sched_switch(prev_comm: "kworker/u32:48", prev_pid: 35840 (kworker/u32:48-), prev_prio: 120, prev_state: 128, next_comm: "swapper/2", next_prio: 120)
       0.045 [002] :0/0 sched:sched_switch(prev_comm: "swapper/2", prev_prio: 120, next_comm: "kworker/u32:48", next_pid: 35840 (kworker/u32:48-), next_prio: 120)

The feature is implemented strictly as an opt-in toggle to prevent cluttering the standard output and to preserve backwards compatibility for scripts parsing the default output format.

---
Changes since v1 [1]:
- Fixed a silent failure where core trace events (e.g., system calls and page faults) ignored the --show-cpu flag. All primary workload events correctly display the CPU ID when required

- Updated all core event handlers (i.e., trace__sys_enter, trace__sys_exit, trace__pgfault and trace__printf_interrupted_entry) to extract sample->cpu and pass it down into the entry head formatter

- Abstracted the CPU formatting logic into a dedicated, documented helper function trace__fprintf_cpu()

- Added a safety guard to verify the CPU data is actually present (cpu != (u32)-1) before attempting to print it, preventing dummy values from polluting the output when sample data is missing

[1]: https://lore.kernel.org/linux-perf-users/[email protected]/

---
 tools/perf/Documentation/perf-trace.txt |  3 ++
 tools/perf/builtin-trace.c              | 49 ++++++++++++++++++++++---
 2 files changed, 47 insertions(+), 5 deletions(-)
tools/perf/Documentation/perf-trace.txt +3 -0
index 892c82a9bf40..d0b6c771a1b9 100644
@@ -199,6 +199,9 @@ the thread executes on the designated CPUs. Default is to monitor all CPUs.
 --show-on-off-events::
 	Show the --switch-on/off events too.
 
+--show-cpu::
+	Show cpu id.
+
 --max-stack::
         Set the stack depth limit when parsing the callchain, anything
         beyond the specified depth will be ignored. Note that at this point
tools/perf/builtin-trace.c +44 -5

114 lines not on this page · raw message

Signature · 1 line
2.51.0
#1 lore raw
Sashiko AI review found 2 potential issue(s): - [High] Hardcoded CPU 0 misattributes interrupted syscalls. - [Medium] Tautological check on unsigned integer and format string mismatch. --

Sashiko AI review found 2 potential issue(s):
- [High] Hardcoded CPU 0 misattributes interrupted syscalls.
- [Medium] Tautological check on unsigned integer and format string mismatch. --

commit 1c192dba8ca61a6516bab340989a37ca856bfece
Author: Aaron Tomlin <[email protected]>

perf trace: Introduce --show-cpu option to display cpu id

This commit adds a new --show-cpu command-line option to perf trace. When enabled, it extracts the CPU ID from the perf sample and includes it in the trace output immediately following the timestamp.

Quoted 4 lines
tools/perf/builtin-trace.c +0 -0
index 311d9da9896a41..264e4762ea28d2 100644

[ ... ]

tools/perf/builtin-trace.c static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) · line 1912

Quoted 5 lines
tools/perf/builtin-trace.c +5 -0
+static size_t trace__fprintf_cpu(u32 cpu, FILE *fp)
+{
+	size_t printed = 0;
+
+	if (cpu >= 0)

Since cpu is an unsigned 32-bit integer (u32), is this comparison always true? This might trigger a compiler warning for tautological comparisons.

tools/perf/builtin-trace.c static size_t trace__fprintf_tstamp(struct trace *trace, u64 tstamp, FILE *fp) · line 1913

Quoted 1 line
tools/perf/builtin-trace.c +1 -0
+		printed += fprintf(fp, "[%03d] ", cpu);

Given that cpu is unsigned, should this use the %03u format specifier instead of %03d to avoid a format signedness mismatch?

Quoted 3 lines
tools/perf/builtin-trace.c +3 -0
+
+	return printed;
+}

[ ... ]

tools/perf/builtin-trace.c static int trace__printf_interrupted_entry(struct trace *trace) · line 2734

Quoted 8 lines
tools/perf/builtin-trace.c +3 -1
@@ -2704,7 +2729,9 @@ static int trace__printf_interrupted_entry(struct trace *trace)
 	if (!ttrace->entry_pending)
 		return 0;
 
-	printed  = trace__fprintf_entry_head(trace, trace->current, 0, false, ttrace->entry_time, trace->output);
+	printed = trace__fprintf_entry_head(trace, trace->current, 0, false,
+					    ttrace->entry_time, 0,
+					    trace->output);

Does passing a hardcoded 0 for the cpu argument cause all interrupted events to be incorrectly attributed to CPU 0 when the --show-cpu option is enabled?

If the actual entry CPU isn't saved in struct thread_trace, would it be better to pass (u32)-1 here so the CPU printout is properly omitted, matching the check inside trace__fprintf_entry_head()?

[ ... ]

tools/perf/builtin-trace.c static int trace__event_handler(struct trace *trace, struct evsel *evsel, · line 3312

Quoted 6 lines
tools/perf/builtin-trace.c +2 -0
@@ -3277,6 +3308,9 @@ static int trace__event_handler(struct trace *trace, struct evsel *evsel,
 	trace__printf_interrupted_entry(trace);
 	trace__fprintf_tstamp(trace, sample->time, trace->output);
 
+	if (trace->show_cpu && sample->cpu != (u32)-1)
+		fprintf(trace->output, "[%03d] ", sample->cpu);

Similar to the function above, should this use %03u since sample->cpu is an unsigned integer?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1