Skip to contentSkip to conversation

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

PATCH In review Aaron Tomlin 2 participants 3 messages Apr 24 – May 18 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v4 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 v3 [1]:
- Extended struct thread_trace to track the entry_cpu. This resolves a visual alignment bug and prevents the loss of CPU information when printing pending/interrupted system call entries

Changes since v2 [2]:
- Fixed a tautological check in trace__fprintf_cpu(). It now correctly guards against missing CPU data by checking against (u32)-1

- Corrected the format specifier in trace__fprintf_cpu()

- Replaced the hardcoded CPU ID of 0 with (u32)-1 in trace__printf_interrupted_entry() to prevent false CPU attribution

- Refactored trace__event_handler() to utilise the trace__fprintf_cpu() helper, removing duplicated formatting logic

Changes since v1 [3]:
- 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]/
[2]: https://lore.kernel.org/linux-perf-users/[email protected]/
[3]: https://lore.kernel.org/linux-perf-users/[email protected]/

---
 tools/perf/Documentation/perf-trace.txt |  3 ++
 tools/perf/builtin-trace.c              | 51 ++++++++++++++++++++++---
 2 files changed, 49 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 +46 -5

128 lines not on this page · raw message

Signature · 1 line
2.51.0
#1 lore raw
Quoted 16 lines · Aaron Tomlin

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)
Quoted 3 lines

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.

Hi Peter, Ingo, Arnaldo, Namhyung,

Just a gentle ping on this patch. It has been a few weeks since v4 was submitted [1].

Are there any further comments or revisions needed, or is this in a good state to be picked up?

[1]: https://lore.kernel.org/lkml/[email protected]/

Kind regards,

Signature · 1 line
Aaron Tomlin
maintainer Acked-by
#2 lore raw

Hello,

Quoted 23 lines · Aaron Tomlin
Quoted 16 lines · Aaron Tomlin

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)
Quoted 3 lines

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.

Hi Peter, Ingo, Arnaldo, Namhyung,

Quoted 7 lines

Just a gentle ping on this patch. It has been a few weeks since v4 was submitted [1].

Are there any further comments or revisions needed, or is this in a good state to be picked up?

[1]: https://lore.kernel.org/lkml/[email protected]/

Thanks,
Namhyung