perf trace: Introduce --show-cpu option to display cpu id
PATCH v3 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 v2 [1]:
- 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 [2]:
- 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]/
--- 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
@@ -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
[email protected]
AI reviewSashiko AI review found 1 potential issue(s): - [Medium] The `--show-cpu` option fails to print the CPU ID for interrupted system call entries, resulting in lost information and broken columnar…
Sashiko AI review found 1 potential issue(s):
- [Medium] The --show-cpu option fails to print the CPU ID for interrupted system call entries, resulting in lost information and broken columnar alignment. --
commit f837c4ea902d9b8f1e88b211cf0a2320cd3c7a26
Author: Aaron Tomlin <[email protected]>
perf trace: Introduce --show-cpu option to display cpu id
This commit introduces the --show-cpu option for perf trace. It extracts the CPU from the perf sample and prints it alongside the timestamp to better trace system-wide workloads.
Quoted 4 lines
tools/perf/builtin-trace.c +0
-0
[ ... ]
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, (u32)-1 , + trace->output);
When a system call entry is processed in trace__sys_enter(), it is often buffered (ttrace->entry_pending = true) so it can be combined with its exit event on a single line. Does hardcoding (u32)-1 here cause the CPU ID to be lost when a pending entry is interrupted?
Since trace__fprintf_entry_head() skips printing the CPU ID entirely when the cpu is (u32)-1, it appears this will omit the CPU column for interrupted entries (which print with " ..."). Could this break the columnar alignment of the output?
Would it be better to extend struct thread_trace to store the cpu ID from the sample during trace__sys_enter(), so it can be accurately printed when the entry is flushed here?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1