perf ftrace: Support inlined functions and display enhancements in function graph tracer
v9 cover perf ftrace: Support inlined functions and display enhancements in function graph tracer
The Linux kernel's function graph tracer operates at the machine instruction level via compiler instrumentation (-fpatchable-function-entry), recording entry and return events for physical function calls. Consequently, functions inlined by the compiler are invisible in the resulting call graph, as no discrete call or return instructions are emitted for them. While technically expected, this omission frequently obscures the logical execution flow when analysing kernel subsystems heavily reliant upon inlining (such as the scheduler, memory management, locking primitives, and RCU).
When the kernel is configured with CONFIG_FUNCTION_GRAPH_RETADDR=y, the funcgraph-retaddr trace option records the caller's return address on each function entry, manifested in the trace stream as a comment (e.g. /* <-wake_up_new_task+0x1d1/0x3e0 */). By interrogating DWARF debug information from the kernel image (vmlinux) utilising libdw, perf ftrace resolves this return address to its inlined callchain and synthesises the intermediate inlined frames directly into the streamed call graph.
This patch series addresses these challenges through six focused commits:
1. Stack frame optimisation in __cmd_ftrace()
Replaces the 4096-byte stack array in __cmd_ftrace() with dynamic heap allocation alongside struct strbuf streaming, reducing the stack frame footprint by over 96% (down to ~160 bytes) whilst ensuring complete line-delimited records, explicit NUL termination, and robust error unwind and EOF handling.
2. Return address comment filtering
Introduces ftrace_parse_retaddr() and ftrace_filter_retaddr() with --filter-retaddr and --graph-opts filter-retaddr to strip caller comments from trace output, accompanied by an initial unit test suite in tools/perf/tests/ftrace.c.
3. Preparatory vmlinux lookup helper
Introduces vmlinux_path__find() in tools/perf/util/symbol.c and symbol.h to search standard vmlinux paths and return the first accessible kernel image, providing a generic helper across tools/perf and avoiding hardcoded access() checks in tests.
4. Inlined function call graph reconstruction
Introduces --inline and -k/--vmlinux, dynamically synthesising inlined frames marked with /* (inline) */. Correctly matches bare closing braces when funcgraph-tail is disabled, clamps stack depth to prevent indentation drift, and expands the automated test suite to verify inline resolution, comment filtering, and stack saturation recovery utilising vmlinux_path__find().
5. Execution duration suppression
Introduces --graph-opts noduration (accepting noduration=[0|1]) to toggle the kernel's funcgraph-duration tracing option, omitting the duration column when the user is focused purely on call graph topology and inlined hierarchy. Rejects noduration when invoked under 'perf ftrace profile'.
6. Extraneous function filtering in function graph tracer
Introduces userspace call graph filtering when targeted tracing (-G or --graph-funcs) is active. Suppresses transient unrelated entries arriving before filter activation as well as detached callee trees (such as ring-buffer wakeups) originating outside the target hierarchy. Dynamically resises the synthetic call stack to eliminate saturation edge cases and accurately suppresses detached subtrees without orphan exit name collisions.
Synthesised inlined functions are rendered with an explicit /* (inline) */ annotation at both entry and exit:
# tracer: function_graph
#
# CPU FUNCTION CALLS
# | | | | |
0) enqueue_entity() {
0) update_cfs_group() { /* (inline) */
0) calc_concur_shares() {
0) tg_cpus() {
0) cpuset_num_cpus() {
0) class_rcu_constructor() { /* (inline) */
0) rcu_read_lock() { /* (inline) */
0) __rcu_read_lock();
0) } /* rcu_read_lock (inline) */
0) } /* class_rcu_constructor (inline) */
0) cgroup_e_css();
0) class_rcu_destructor() { /* (inline) */
0) rcu_read_unlock() { /* (inline) */
0) __rcu_read_unlock();
0) } /* rcu_read_unlock (inline) */
0) } /* class_rcu_destructor (inline) */
0) }
0) }
0) __calc_smp_shares();
0) reweight_entity();
0) } /* update_cfs_group (inline) */
0) }
Changes since v8:
- Preserved funcgraph-retval return value comments when filtering funcgraph-retaddr comments (/* <-caller ret=0x0 */ -> /* ret=0x0 */) in ftrace_filter_retaddr()
- Enhanced vmlinux_path__find() helper in tools/perf/util/symbol.c:
- Avoided redundant memory allocation in vmlinux_path__init() when vmlinux_path is already initialised
- Ensured symbol configuration is initialised via symbol__init() prior to path resolution
- Guaranteed vmlinux_path memory cleanup by invoking vmlinux_path__exit() unconditionally in symbol__exit()
- Tracked raw (pre-inline-expansion) indentation on synthetic stack frames, ensuring bare closing braces ("}") are accurately matched against corresponding non-inlined frames without indentation distortion
- Unwound stale frames on untracked caller entries (caller_idx < 0) whose raw indentation is greater than or equal to the new entry, and closed trailing open inlined frames resulting from dropped exit events
- Pre-bound single-PID tracing across all CPUs in perf_ftrace__setup_inlines(), ensuring consistent synthetic call stack tracking across CPU migration when kernel context-switch banners are suppressed (verif_pid prev_pid == -1)
- Strengthened error handling during DWARF/vmlinux initialisation in perf_ftrace__setup_inlines(), checking load return codes, emitting diagnostic warnings, and safely freeing ftrace->machine on failure
- Recognised interrupt/IRQ entry and exit markers ("=====>" and "<=====") alongside context-switch markers under --graph-opts noduration, preventing non-function lines from corrupting function column detection or popping active stack frames
- Handled leaf function lines with trailing comments
(e.g. funcgraph-retaddr or funcgraph-retval comments) in parse_func_duration() for 'perf ftrace profile', ensuring parentheses are stripped cleanly and leaf records match non-leaf entries in duration tracking
- Preserved tracked ancestor frames on cs->stack across dropped target root exits by verifying caller provenance
(caller_idx < cs->target_root_depth) before checking indentation bounds in ftrace_check_target_caller()
- Purged stale non-target frames below target root upon target root entry when caller_idx < 0, preventing unrelated ancestor frames from prematurely terminating subsequent target calls
- Prevented cs->orphan_depth lockouts when both an orphan exit and enclosing target root exit are dropped by resetting cs->orphan_depth on any line at or outside target root indentation (leading_spaces <= cs->target_root_indent), and allowing ancestor exits to fall through to match frames on cs->stack
- Preserved interrupt/IRQ markers inside active target callgraphs while suppressing those outside target filters under -G or --graph-funcs
- Fixed test_stack_saturation() to avoid premature frame unwinding, ensuring depth clamping and recovery are tested as intended
- Expanded automated test coverage in tools/perf/tests/ftrace.c:
- Added test_context_switch_handling() for CPU migration and task tracking
- Added test_column_layouts() covering standard, verbose, and noduration formats including IRQ and context-switch markers
- Added regression tests in test_graph_target_filter() covering double dropped exits, ancestor frame preservation, leaf duration parsing, and deep orphan recovery
- Link to v8: https://lore.kernel.org/lkml/[email protected]/
Changes since v7:
- Rebased onto latest perf-tools-next/perf-tools-next (commit 1c90f0a3c8a1 ("perf kvm: Cast tv_usec to long to fix build on MIPS 32-bits"))
- Link to v7: https://lore.kernel.org/all/[email protected]/
Changes since v6:
- Expanded the series from 5 to 6 patches by introducing a standalone preparatory commit ("perf symbol: Introduce vmlinux_path__find() helper") in tools/perf/util/symbol.c and symbol.h to locate accessible kernel images via configured vmlinux paths, replacing ad-hoc access("vmlinux", R_OK) checks in unit tests (Ian Rogers)
- Explicitly included stdio.h in tools/perf/util/ftrace.h to declare the FILE type for ftrace_process_fgraph_line(), ensuring clean compilation under musl libc and non-glibc environments
- Prevented name collisions between orphan callee exits and active target frames in ftrace_process_fgraph_line()
- Allowed dropped-orphan exit recovery on closing braces only when the exit explicitly names the target root function itself, ensuring that lost orphan exits do not swallow the target root's exit while protecting intermediate callee frames from collisions
- Replaced the fixed-size array in struct ftrace_task_state with a dynamically resized stack grown via realloc(), completely eliminating cs->overflow_depth and its associated failure modes:
- Fixed skipped filter teardown when a target function begins at the stack limit, ensuring standard teardown runs upon root exit and preventing subsequent sibling functions from leaking into filtered output
- Fixed orphan filter bypass during deep call stacks by preserving all caller frames on the stack, ensuring unknown callers (caller_idx < 0) are accurately identified as orphans and suppressed
- Link to v6: https://lore.kernel.org/all/[email protected]/
Changes since v5:
- Prevented call stack desynchronisation on dropped orphan exit events by clearing orphan_depth when a named exit matches a tracked frame or an entry descends from the target callgraph
- Fixed premature target filter termination on child exits by capturing cs->target_root_depth after unwinding stale frames above the caller
- Ensured synthesised caller inlines above cs->target_root_depth are cleanly closed upon target function exit before resetting filter state
- Prevented legitimate deep calls exceeding FTRACE_MAX_STACK from being falsely dropped as orphans by checking cs->overflow_depth
- Expanded unit tests to cover dropped orphan exits, target root depth synchronisation after stale siblings, and deep call stack overflows
- Provided support for glob pattern matching with -G or --graph-funcs
- Link to v5: https://lore.kernel.org/all/[email protected]/
Changes since v4:
- Handled non-fatal EAGAIN, EWOULDBLOCK, and EINTR in __cmd_ftrace() polling and draining loops to prevent premature session termination on non-blocking reads or signal interruptions
- Eliminated an unused local variable endptr in ftrace_parse_retaddr() by passing NULL to strtoull()
- Prevented stack buffer overflows in make_blank_prefix() by clamping the blanking loop bounds and end pointer to the destination buffer length when processing trace records with exceptionally long prefixes
- Tracked call stack saturation with overflow_depth to prevent untracked function exits with bare closing braces from prematurely popping tracked frames from the parser stack on deep call chains exceeding FTRACE_MAX_STACK
- Ensured stale non-inlined callee frames resulting from dropped function exits do not block inactive inlined frames from being closed upon subsequent entries by unwinding the stack down to caller_idx + 1 + common
- Fixed filter state not resetting for targeted leaf calls when --inline is active: synthesised inline frames inflated cs->depth beyond cs->target_root_depth, causing subsequent sibling functions to be falsely classified as detached orphans and bypassing inline unwinding; resolved by tracking target root entries (is_target_root), explicitly unwinding all synthesised inlines, and resetting filter state upon leaf completion
- Expanded unit test coverage to verify recovery across dropped exit events and bare closing braces, and to verify that targeted leaf calls with synthesised inlined frames properly close inlines and reset filter state
- Link to v4: https://lore.kernel.org/lkml/[email protected]/
Changes since v3:
- Explicitly rejected the noduration option when invoked under the profile subcommand, since profiling inherently requires function duration to compute timing statistics
- Fixed make_blank_prefix() clobbering the comm-pid column in verbose mode by searching backwards for the preceding "|" delimiter, and returning early when graph_noduration is active
- Fixed filter state not resetting for targeted leaf calls at non-zero call stack depths by checking cs->depth <= cs->target_root_depth and clearing cs->target_root_depth, preventing subsequent non-targeted sibling functions within the parent frame from leaking into trace output
- Expanded unit test coverage to verify comm-pid preservation under verbose and verbose-noduration inline modes, and to verify that targeted leaf calls at depth > 0 properly reset the target filter state.
- Link to v3: https://lore.kernel.org/lkml/[email protected]/
Changes since v2:
- Expanded the series from 4 to 5 patches by adding a dedicated commit ("perf ftrace: Filter extraneous functions in function graph tracer") to enforce strict target callgraph boundaries when -G or --graph-funcs is specified
- Suppressed unrelated top-level functions that leak through the transient tracefs filter initialisation window before RCU synchronisation completes
- Filtered detached/orphan calls from appearing as nested children within the target graph by validating caller return addresses against the active target stack
- Tracked nested orphan scope depth (cs->orphan_depth) to cleanly suppress child calls and closing delimiters of detached subtrees without desynchronising the target stack
- Added automated unit test coverage in tools/perf/tests/ftrace.c (test_graph_target_filter()) validating top-level suppression (both with and without --inline) and detached orphan wakeup suppression
- Link to v2: https://lore.kernel.org/lkml/[email protected]/
Changes since v1:
- Modularised the implementation into a 4-patch bisectable series rather than a monolithic commit
- Resolved potential infinite loop on poll() upon trace_pipe EOF
- Fixed stuck call stack frames when funcgraph-tail is disabled by supporting bare closing delimiters matching LIFO stack frames
- Prevented unbounded indentation growth on deep call trees by clamping inline expansion and synchronising cs->inlined_depth to frames successfully pushed onto cs->stack
- Added dedicated unit tests covering bare closing braces and stack saturation recovery
- Refined option documentation in perf-ftrace.txt
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
Aaron Tomlin (6):
perf ftrace: Optimise __cmd_ftrace() stack frame with strbuf streaming perf ftrace: Support filtering return address comments perf symbol: Introduce vmlinux_path__find() helper
perf ftrace: Support display of inlined functions in function graph
tracer
perf ftrace: Support omitting execution duration in function graph
tracer
perf ftrace: Filter extraneous functions in function graph tracer
tools/perf/Documentation/perf-ftrace.txt | 20 + tools/perf/builtin-ftrace.c | 212 +- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/ftrace.c | 3271 ++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/util/Build | 1 + tools/perf/util/ftrace.c | 1634 +++++++++++ tools/perf/util/ftrace.h | 28 + tools/perf/util/symbol.c | 38 +- tools/perf/util/symbol.h | 2 + 11 files changed, 5180 insertions(+), 29 deletions(-) create mode 100644 tools/perf/tests/ftrace.c create mode 100644 tools/perf/util/ftrace.c
Signature · 1 line
2.55.0
PATCH v9 1/6 perf ftrace: Optimise __cmd_ftrace() stack frame with strbuf streaming
In __cmd_ftrace(), incoming trace data from trace_pipe was historically buffered in a fixed 4096-byte array allocated directly on the stack frame. Furthermore, reads were written directly to stdout via fwrite() in arbitrary chunk sizes, rather than as discrete, well-delimited trace lines.
Refactor __cmd_ftrace() to employ dynamic heap allocation for the read buffer alongside a dynamic struct strbuf to accumulate streamed characters into whole lines. Pre-allocating the strbuf ensures that line processing incurs zero heap reallocations during steady-state polling, while the stack frame of __cmd_ftrace() is reduced by over 96% (down to ~160 bytes). Robust error and EOF detection on stdout and the trace stream ensures polling terminates cleanly upon pipeline or pager exit, while retrying on non-fatal EAGAIN and EINTR conditions prevents premature termination on non-blocking reads or signal interruptions.
--- tools/perf/builtin-ftrace.c | 79 +++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 13 deletions(-)
tools/perf/builtin-ftrace.c +66 -13
@@ -14,6 +14,7 @@#include <poll.h> #include <semaphore.h> #include <signal.h> +#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <linux/capability.h>
@@ -41,7 +42,10 @@#include "util/units.h" #include "util/parse-sublevel-options.h" +#include "util/strbuf.h" +#define DEFAULT_TRACER "function_graph" +#define TRACE_BUF_SIZE 4096static volatile sig_atomic_t workload_exec_errno;static volatile sig_atomic_t done;
@@ -738,7 +742,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) {char *trace_file;int trace_fd; -char buf[4096 ]; +char *buf =NULL ; +struct strbuf linebuf =STRBUF_INIT ;struct pollfd pollfd = { .events =POLLIN , };
@@ -767,10 +772,18 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)setup_pager (); + buf =malloc (TRACE_BUF_SIZE ); +if (!buf) { +pr_err ("failed to allocate trace buffer\n" ); + goto out_reset; + } +strbuf_init (&linebuf,512 ); +strbuf_setlen (&linebuf,0 ); + trace_file =get_tracing_instance_file ("trace_pipe" );if (!trace_file) {pr_err ("failed to open trace_pipe\n" ); - goto out_reset; + goto out_free; } trace_fd =open (trace_file,O_RDONLY );
@@ -779,7 +792,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)if (trace_fd <0 ) {pr_err ("failed to open trace_pipe\n" ); - goto out_reset; + goto out_free; }fcntl (trace_fd,F_SETFL ,O_NONBLOCK );
@@ -806,24 +819,42 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) }while (!done) { -if (poll (&pollfd,1 ,-1 ) <0 ) +if (poll (&pollfd,1 ,-1 ) <0 ) { +if (errno ==EINTR ) +continue ;break ; + }if (pollfd.revents &POLLIN ) { -int n =read (trace_fd, buf,sizeof (buf)); -if (n <0 ) +int n =read (trace_fd, buf,TRACE_BUF_SIZE ); + +if (n <0 ) { +if (errno ==EAGAIN || errno ==EWOULDBLOCK || errno ==EINTR ) +continue ;break ; -if (fwrite (buf, n,1 , stdout) !=1 ) + } +if (n ==0 )break ; +for (int i =0 ; i < n; i++) { +if (buf[i] =='\n' ) { +if (fprintf (stdout,"%s\n" , linebuf.buf) <0 ) +break ; +strbuf_setlen (&linebuf,0 ); + }else { +if (strbuf_addch (&linebuf, buf[i]) <0 ) + goto out_close_fd; + } + }/* flush output since stdout is in full buffering mode due to pager */ -fflush (stdout); +if (fflush (stdout) <0 ||ferror (stdout)) +break ; } }write_tracing_file ("tracing_on" ,"0" );if (workload_exec_errno) { -const char *emsg =str_error_r (workload_exec_errno, buf,sizeof (buf)); +const char *emsg =str_error_r (workload_exec_errno, buf,TRACE_BUF_SIZE );/* flush stdout first so below error msg appears at the end. */ fflush (stdout);pr_err ("workload failed: %s\n" , emsg);
@@ -831,16 +862,38 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) }/* read remaining buffer contents */ -while (true) { -int n =read (trace_fd, buf,sizeof (buf)); -if (n <=0 ) +while (!ferror (stdout)) { +int n =read (trace_fd, buf,TRACE_BUF_SIZE ); + +if (n <0 ) { +if (errno ==EINTR ) +continue ;break ; -if (fwrite (buf, n,1 , stdout) !=1 ) + } +if (n ==0 )break ; +for (int i =0 ; i < n; i++) { +if (buf[i] =='\n' ) { +if (fprintf (stdout,"%s\n" , linebuf.buf) <0 ) +break ; +strbuf_setlen (&linebuf,0 ); + }else { +if (strbuf_addch (&linebuf, buf[i]) <0 ) + goto out_close_fd; + } + } + } + +if (!ferror (stdout) && linebuf.len >0 ) { +fprintf (stdout,"%s\n" , linebuf.buf); +strbuf_setlen (&linebuf,0 ); } out_close_fd:close (trace_fd); +out_free: +free (buf); +strbuf_release (&linebuf); out_reset:exit_tracing_instance (); out:
Signature · 1 line
2.55.0
PATCH v9 2/6 perf ftrace: Support filtering return address comments
In reply to Aaron TomlinWhen tracing with the Linux kernel's function graph tracer, enabling funcgraph-retaddr records the return address of each function entry as a comment in the trace stream (e.g. /* <-load_elf_phdrs+0x6c/0xb0 */). While valuable for tracing execution origins, these trailing comments can add considerable visual clutter to the call graph, particularly when examining deeply nested call trees or when synthesising higher-level abstractions.
Introduce support for filtering out return address comments from function graph trace output. Provide a standalone --filter-retaddr flag.
Key aspects of this implementation:
1. Return address parsing and filtering
Implement ftrace_filter_retaddr() in tools/perf/util/ftrace.c to locate return address comments and strip them in-place with memmove(3).
Additionally, introduce ftrace_parse_retaddr() to parse caller symbol names and offsets across diverse output styles (standard format, return values, and bracketed addresses), establishing the parsing foundation for subsequent inlined callchain resolution.
2. Command-line interface
Expose --filter-retaddr in tools/perf/builtin-ftrace.c, updating the documentation in tools/perf/Documentation/perf-ftrace.txt accordingly.
3. Automated verification
Add a dedicated unit test suite ("Ftrace return address processing") in tools/perf/tests/ftrace.c to validate comment parsing and string filtering across all supported formats.
--- tools/perf/Documentation/perf-ftrace.txt | 4 + tools/perf/builtin-ftrace.c | 8 ++ tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/ftrace.c | 81 +++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/util/Build | 1 + tools/perf/util/ftrace.c | 93 ++++++++++++++++++++++++ tools/perf/util/ftrace.h | 5 ++ 9 files changed, 195 insertions(+) create mode 100644 tools/perf/tests/ftrace.c create mode 100644 tools/perf/util/ftrace.c
tools/perf/Documentation/perf-ftrace.txt +4 -0
@@ -134,6 +134,10 @@ OPTIONS for 'perf ftrace trace' - depth=<n> - Set max depth for function graph tracer to follow. - tail - Print function name at the end. +--filter-retaddr:: + Filter out function return address comments (`/* <-caller+offset */`) + from function_graph output. + OPTIONS for 'perf ftrace latency' ---------------------------------
tools/perf/builtin-ftrace.c +8 -0
@@ -837,6 +837,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)break ;for (int i =0 ; i < n; i++) {if (buf[i] =='\n' ) { +if (ftrace->filter_retaddr) +ftrace_filter_retaddr (linebuf.buf);if (fprintf (stdout,"%s\n" , linebuf.buf) <0 )break ;strbuf_setlen (&linebuf,0 );
@@ -874,6 +876,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)break ;for (int i =0 ; i < n; i++) {if (buf[i] =='\n' ) { +if (ftrace->filter_retaddr) +ftrace_filter_retaddr (linebuf.buf);if (fprintf (stdout,"%s\n" , linebuf.buf) <0 )break ;strbuf_setlen (&linebuf,0 );
@@ -885,6 +889,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) }if (!ferror (stdout) && linebuf.len >0 ) { +if (ftrace->filter_retaddr) +ftrace_filter_retaddr (linebuf.buf);fprintf (stdout,"%s\n" , linebuf.buf);strbuf_setlen (&linebuf,0 ); }
@@ -1850,6 +1856,8 @@ int cmd_ftrace(int argc, const char **argv) "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),OPT_BOOLEAN (0 ,"inherit" , &ftrace.inherit,"Trace children processes" ), +OPT_BOOLEAN_SET (0 ,"filter-retaddr" , &ftrace.filter_retaddr, &ftrace.filter_retaddr_set, +"Filter out funcgraph return address comments" ),OPT_INTEGER ('D' ,"delay" , &ftrace.target.initial_delay,"Number of milliseconds to wait before starting tracing after program start" ), OPT_PARENT(common_options),
tools/perf/tests/Build +1 -0
@@ -72,6 +72,7 @@ perf-test-y += hwmon_pmu.o perf-test-y += tool_pmu.o perf-test-y += subcmd-help.o perf-test-y += kallsyms-split.o +perf-test-y += ftrace.o ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc riscv)) perf-test-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
tools/perf/tests/builtin-test.c +1 -0
@@ -156,6 +156,7 @@ static struct test_suite *generic_tests[] = { &suite__perf_hooks, &suite__unit_number__scnprint, &suite__mem2node, + &suite__ftrace, &suite__time_utils, &suite__jit_write_elf, &suite__pfm,
tools/perf/tests/ftrace.c new file +81 -0
81 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -180,6 +180,7 @@ DECLARE_SUITE(perf_hooks);DECLARE_SUITE (unit_number__scnprint);DECLARE_SUITE (mem2node);DECLARE_SUITE (maps); +DECLARE_SUITE (ftrace);DECLARE_SUITE (time_utils);DECLARE_SUITE (jit_write_elf);DECLARE_SUITE (api_io);
tools/perf/util/Build +1 -0
@@ -169,6 +169,7 @@ perf-util-y += list_sort.o perf-util-y += mutex.o perf-util-y += sharded_mutex.o perf-util-y += intel-tpebs.o +perf-util-y += ftrace.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter_cgroup.o
tools/perf/util/ftrace.c new file +93 -0
93 lines not on this page · raw message
tools/perf/util/ftrace.h +5 -0
@@ -39,6 +39,8 @@ struct perf_ftrace {int graph_verbose;int graph_thresh;int graph_tail; +bool filter_retaddr; +bool filter_retaddr_set; };struct filter_entry {
@@ -93,4 +95,7 @@ perf_ftrace__latency_cleanup_bpf(struct perf_ftrace *ftrace __maybe_unused)#endif /* HAVE_BPF_SKEL */ +bool ftrace_parse_retaddr (const char *str,char *sym_name,size_t sym_len,u64 *offset); +void ftrace_filter_retaddr (char *str); +#endif /* __PERF_FTRACE_H__ */
Signature · 1 line
2.55.0
PATCH v9 3/6 perf symbol: Introduce vmlinux_path__find() helper
In reply to Aaron TomlinThe vmlinux_path array maintains standard paths used by perf to locate the kernel image (e.g. "vmlinux", "/boot/vmlinux", "/lib/modules/%s/build/vmlinux", "/usr/lib/debug/..."). While vmlinux_path__init() populates these candidates and dso__load_vmlinux_path() loads symbols into a DSO/map, callers (such as test suites or probing tools) often need to verify the existence of an accessible vmlinux on disk before attempting machine setup or test execution.
Introduce vmlinux_path__find() to search through the configured vmlinux_path entries (or symbol_conf.vmlinux_name if explicitly set) and return the path to the first readable vmlinux file, or NULL if none is accessible.
--- tools/perf/util/symbol.c | 38 +++++++++++++++++++++++++++++++++++++- tools/perf/util/symbol.h | 2 ++ 2 files changed, 39 insertions(+), 1 deletion(-)
tools/perf/util/symbol.c +37 -1
@@ -2337,6 +2337,9 @@ static int vmlinux_path__init(struct perf_env *env)const char *kernel_version;unsigned int i; +if (vmlinux_path) +return 0 ; + vmlinux_path =malloc (sizeof (char *) * (ARRAY_SIZE (vmlinux_paths) +ARRAY_SIZE (vmlinux_paths_upd)));if (vmlinux_path ==NULL )
@@ -2372,6 +2375,38 @@ static int vmlinux_path__init(struct perf_env *env)return -1 ; } +/** +* vmlinux_path__find - Locate the first accessible vmlinux file +* +* Check symbol_conf.vmlinux_name first if set, then search through the +* initialized paths in vmlinux_path. +* +* Return: A pointer to the path of the first readable vmlinux file, or NULL if +* none is accessible. +*/ +const char *vmlinux_path__find (void ) +{ +int i; + +if (symbol_conf.vmlinux_name &&access (symbol_conf.vmlinux_name,R_OK ) ==0 ) +return symbol_conf.vmlinux_name; + +if (symbol__init (NULL ) <0 ) +return NULL ; + +if (!vmlinux_path) { +if (vmlinux_path__init (NULL ) <0 ) +return NULL ; + } + +for (i =0 ; i < vmlinux_path__nr_entries; ++i) { +if (access (vmlinux_path[i],R_OK ) ==0 ) +return vmlinux_path[i]; + } + +return NULL ; +} +int setup_list (struct strlist **list,const char *list_str,const char *list_name) {
@@ -2598,6 +2633,8 @@ int symbol__init(struct perf_env *env)void symbol__exit (void ) { +vmlinux_path__exit (); +if (!symbol_conf.initialized)return ;
@@ -2608,7 +2645,6 @@ void symbol__exit(void)intlist__delete (symbol_conf.tid_list);intlist__delete (symbol_conf.pid_list);intlist__delete (symbol_conf.addr_list); -vmlinux_path__exit (); symbol_conf.sym_list = symbol_conf.dso_list = symbol_conf.comm_list =NULL ; symbol_conf.bt_stop_list =NULL ; symbol_conf.initialized = false;
tools/perf/util/symbol.h +2 -0
@@ -184,6 +184,8 @@ static inline int __symbol__join_symfs(char *bf, size_t size, const char *path)extern int vmlinux_path__nr_entries;extern char **vmlinux_path; +const char *vmlinux_path__find (void ); +static inline void *symbol__priv (struct symbol *sym) {return ((void *)sym) - symbol_conf.priv_size;
Signature · 1 line
2.55.0
PATCH v9 4/6 perf ftrace: Support display of inlined functions in function graph tracer
In reply to Aaron TomlinThe Linux kernel's function graph tracer operates at the machine instruction level via compiler instrumentation (-fpatchable-function-entry), recording entry and return events for physical function calls. Consequently, functions inlined by the compiler are invisible in the resulting call graph, as no discrete call or return instructions are emitted for them. While technically expected, this omission frequently obscures the logical execution flow when analysing kernel subsystems heavily reliant upon inlining.
Address this by introducing the --inline option to perf ftrace for the function_graph tracer.
When the kernel is configured with CONFIG_FUNCTION_GRAPH_RETADDR=y, the funcgraph-retaddr trace option records the caller's return address on each function entry, manifested in the trace stream as a comment (e.g. /* <-irq_work_queue_on+0x81/0xa0 */). By interrogating DWARF debug information from the kernel image (vmlinux) utilising libdw, perf ftrace resolves this return address to its inlined callchain and synthesises the intermediate inlined frames directly into the streamed call graph.
Key aspects of this implementation:
1. Inlined frame presentation
Synthesised inlined functions are rendered with an explicit /* (inline) */ annotation at both entry and exit:
# tracer: function_graph
#
# CPU FUNCTION CALLS
# | | | | |
6) activate_task() {
6) enqueue_task() {
6) enqueue_task_fair() {
6) update_curr_eevdf() { /* (inline) */
6) update_curr() {
6) update_se() {
6) cgroup_account_cputime() { /* (inline) */
6) cpuacct_charge();
6) } /* cgroup_account_cputime (inline) */
6) }
6) dl_server_update() {
6) update_curr_dl_se();
6) }
6) } /* update_curr_eevdf (inline) */
6) enqueue_hierarchy() { /* (inline) */
6) update_curr() {
6) update_se();
6) }
6) enqueue_entity() {
6) account_entity_enqueue() { /* (inline) */
6) account_llc_enqueue();
6) } /* account_entity_enqueue (inline) */
6) }
6) } /* enqueue_hierarchy (inline) */
6) place_entity() {
6) avg_vruntime();
6) }
6) __enqueue_entity();
6) dl_server_start();
6) hrtick_update() { /* (inline) */
6) hrtick_active() { /* (inline) */
6) hrtimer_active();
6) } /* hrtick_active (inline) */
6) } /* hrtick_update (inline) */
6) hrtick_start_fair();
6) psi_enqueue() { /* (inline) */
6) psi_task_change() {
6) psi_flags_change();
6) psi_group_change() {
6) record_times();
6) }
6) }
6) } /* psi_enqueue (inline) */
6) }
6) }
This affords immediate visual clarity to the reader upon entering an inlined scope, without necessitating a search for the closing delimiter.
2. Call graph reconstruction and per-task stack tracking
Maintains a per-task synthetic call stack tracking real and inlined frames across context switches, synthesising entry and exit delimiters to accurately reconstruct the execution hierarchy. Tasks are tracked in a hash table keyed by PID, with per-CPU active bindings updated upon context-switch banners or verbose line records. Inlined frames are structured strictly root-to-leaf and validated against the active stack to prevent un-traced callers from attaching orphan inlines. Bare closing braces without trailing function names (the default when funcgraph-tail is disabled) are correctly matched against preceding frames on the stack. Context column alignment, including task comm-pid delimiters in verbose mode, is preserved when generating prefixes for synthesised inline frames. Furthermore, stack depth saturation guards ensure that inlined frame depth tracking remains clamped, indentation cannot drift even under deep call trees exceeding FTRACE_MAX_STACK, and overflow depth is automatically recovered upon caller detection.
3. Option handling and diagnostics
Adds -k or --vmlinux to designate an explicit debug image, and provides clear diagnostic guidance should --inline be invoked on a kernel lacking CONFIG_FUNCTION_GRAPH_RETADDR. Automatically enables return address comment filtering unless explicitly requested.
4. Automated verification
Expands the unit test suite in tools/perf/tests/ftrace.c to validate DWARF inline symbol resolution, comment filtering toggles, bare closing braces handling, task context-switch isolation and migration, and stack saturation/overflow recovery.
--- tools/perf/Documentation/perf-ftrace.txt | 17 +- tools/perf/builtin-ftrace.c | 76 +- tools/perf/tests/ftrace.c | 914 ++++++++++++++++++- tools/perf/util/ftrace.c | 1059 +++++++++++++++++++++- tools/perf/util/ftrace.h | 14 + 5 files changed, 2059 insertions(+), 21 deletions(-)
tools/perf/Documentation/perf-ftrace.txt +16 -1
@@ -134,9 +134,24 @@ OPTIONS for 'perf ftrace trace' - depth=<n> - Set max depth for function graph tracer to follow. - tail - Print function name at the end. +--inline:: + Show inlined functions in function_graph tracer. This requires + kernel support for the funcgraph-retaddr option and a vmlinux with + debug symbols. Inlined functions are displayed in the call graph + hierarchy, and their inlined status is indicated with `/* (inline) */` + on entry and exit, e.g. `func() { /* (inline) */` and `} /* func (inline) */`. + By default, raw return address comments (`/* <-caller+offset */`) + are hidden in the output; specify `--graph-opts retaddr` to show them. + --filter-retaddr:: Filter out function return address comments (`/* <-caller+offset */`) - from function_graph output. + from function_graph output. This is enabled by default when `--inline` + is used; use `--no-filter-retaddr` to disable it. + +-k:: +--vmlinux=:: + Path to the vmlinux file containing debug symbols for resolving + inlined functions. OPTIONS for 'perf ftrace latency'
tools/perf/builtin-ftrace.c +60 -16
140 lines not on this page · raw message
tools/perf/tests/ftrace.c +913 -1
933 lines not on this page · raw message
tools/perf/util/ftrace.c +1056 -3
1,071 lines not on this page · raw message
tools/perf/util/ftrace.h +14 -0
@@ -1,6 +1,7 @@#ifndef __PERF_FTRACE_H__#define __PERF_FTRACE_H__ +#include <stdio.h> #include <linux/list.h> #include "target.h"
@@ -41,6 +42,8 @@ struct perf_ftrace {int graph_tail;bool filter_retaddr;bool filter_retaddr_set; +bool use_inline; +struct machine *machine; };struct filter_entry {
@@ -95,7 +98,18 @@ perf_ftrace__latency_cleanup_bpf(struct perf_ftrace *ftrace __maybe_unused)#endif /* HAVE_BPF_SKEL */ +struct machine ; + +#define FTRACE_MAX_INLINES 32 +bool ftrace_parse_retaddr (const char *str,char *sym_name,size_t sym_len,u64 *offset);void ftrace_filter_retaddr (char *str); +int ftrace_resolve_inlines (struct machine *machine, +const char *sym_name,u64 offset, +const char **inlined_names,int max_inlines); +int ftrace_process_fgraph_line (struct perf_ftrace *ftrace,const char *line,FILE *out); +void ftrace_cpu_states__clear (void ); +int perf_ftrace__setup_inlines (struct perf_ftrace *ftrace); +void perf_ftrace__cleanup_inlines (struct perf_ftrace *ftrace);#endif /* __PERF_FTRACE_H__ */
Signature · 1 line
2.55.0
PATCH v9 5/6 perf ftrace: Support omitting execution duration in function graph tracer
In reply to Aaron TomlinThe Linux kernel's function graph tracer displays the execution duration of completed functions by default via the funcgraph-duration trace option. Whilst informative, in scenarios where the user is solely interested in the call graph topology, call flow sequence, or inlined hierarchy, the duration column can add visual clutter and widen each trace line.
Introduce support for omitting function duration by exposing the noduration sub-option under --graph-opts. Consistent with the existing nosleep-time and noirqs sub-options, it accepts an optional value, so noduration=0 explicitly retains the duration column; when specified more than once, the last occurrence takes effect. When enabled, perf ftrace configures the kernel's tracing instance option funcgraph-duration to 0, and restores it to 1 upon resetting tracing options.
Because the profile subcommand fundamentally requires function execution duration to compute timing statistics and call counts, combining noduration with perf ftrace profile is rejected with an explicit error.
--- tools/perf/Documentation/perf-ftrace.txt | 1 + tools/perf/builtin-ftrace.c | 34 ++++++- tools/perf/tests/ftrace.c | 107 ++++++++++++++++++++++- tools/perf/util/ftrace.c | 25 ++++-- tools/perf/util/ftrace.h | 1 + 5 files changed, 156 insertions(+), 12 deletions(-)
tools/perf/Documentation/perf-ftrace.txt +1 -0
@@ -129,6 +129,7 @@ OPTIONS for 'perf ftrace trace' - retaddr - Show function return address. - nosleep-time - Measure on-CPU time only for function_graph tracer. - noirqs - Ignore functions that happen inside interrupt. + - noduration - Do not print function duration. - verbose - Show process names, PIDs, timestamps, etc. - thresh=<n> - Setup trace duration threshold in microseconds. - depth=<n> - Set max depth for function graph tracer to follow.
tools/perf/builtin-ftrace.c +33 -1
@@ -300,6 +300,7 @@ static void reset_tracing_options(struct perf_ftrace *ftrace __maybe_unused)write_tracing_option_file ("func_stack_trace" ,"0" );write_tracing_option_file ("sleep-time" ,"1" );write_tracing_option_file ("funcgraph-irqs" ,"1" ); +write_tracing_option_file ("funcgraph-duration" ,"1" );write_tracing_option_file ("funcgraph-proc" ,"0" );write_tracing_option_file ("funcgraph-abstime" ,"0" );write_tracing_option_file ("funcgraph-tail" ,"0" );
@@ -599,6 +600,26 @@ static int set_tracing_funcgraph_irqs(struct perf_ftrace *ftrace)return 0 ; } +/** +* set_tracing_funcgraph_duration - Set funcgraph-duration tracing option +* @ftrace: Perf ftrace context +* +* Configure the kernel's funcgraph-duration tracing option based on the +* ftrace->graph_noduration flag. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static int set_tracing_funcgraph_duration (struct perf_ftrace *ftrace) +{ +if (!ftrace->graph_noduration) +return 0 ; + +if (write_tracing_option_file ("funcgraph-duration" ,"0" ) <0 ) +return -1 ; + +return 0 ; +} +static int set_tracing_funcgraph_verbose (struct perf_ftrace *ftrace) {if (!ftrace->graph_verbose)
@@ -708,6 +729,11 @@ static int set_tracing_options(struct perf_ftrace *ftrace)return -1 ; } +if (set_tracing_funcgraph_duration (ftrace) <0 ) { +pr_err ("failed to set tracing option funcgraph-duration\n" ); +return -1 ; + } +if (set_tracing_funcgraph_verbose (ftrace) <0 ) {pr_err ("failed to set tracing option funcgraph-proc/funcgraph-abstime\n" );return -1 ;
@@ -1785,6 +1811,7 @@ static int parse_graph_tracer_opts(const struct option *opt, { .name = "retaddr", .value_ptr = &ftrace->graph_retaddr }, { .name = "nosleep-time", .value_ptr = &ftrace->graph_nosleep_time }, { .name = "noirqs", .value_ptr = &ftrace->graph_noirqs }, + { .name = "noduration", .value_ptr = &ftrace->graph_noduration }, { .name = "verbose", .value_ptr = &ftrace->graph_verbose }, { .name = "thresh", .value_ptr = &ftrace->graph_thresh }, { .name = "depth", .value_ptr = &ftrace->graph_depth },
@@ -1874,7 +1901,7 @@ int cmd_ftrace(int argc, const char **argv)OPT_CALLBACK ('g' ,"nograph-funcs" , &ftrace.nograph_funcs,"func" ,"Set nograph filter on given functions" , parse_filter_func),OPT_CALLBACK (0 ,"graph-opts" , &ftrace,"options" , -"Graph tracer options, available options: args,retval,retval-hex,retaddr,nosleep-time,noirqs,verbose,thresh=<n>,depth=<n>" , +"Graph tracer options, available options: args,retval,retval-hex,retaddr,nosleep-time,noirqs,noduration,verbose,thresh=<n>,depth=<n>,tail" , parse_graph_tracer_opts),OPT_CALLBACK ('m' ,"buffer-size" , &ftrace.percpu_buffer_size,"size" , "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),
@@ -2082,6 +2109,11 @@ int cmd_ftrace(int argc, const char **argv) cmd_func = __cmd_latency;break ;case PERF_FTRACE_PROFILE : +if (ftrace.graph_noduration) { +pr_err ("Error: 'noduration' option cannot be used with profile\n" ); + ret = -EINVAL ; + goto out_delete_filters; + } cmd_func = __cmd_profile;break ;case PERF_FTRACE_NONE :
tools/perf/tests/ftrace.c +103 -4
148 lines not on this page · raw message
tools/perf/util/ftrace.c +18 -7
@@ -573,6 +573,9 @@ static void make_blank_prefix(const struct perf_ftrace *ftrace, const char *linememcpy (buf, line, len); buf[len] ='\0' ; +if (ftrace->graph_noduration) +return ; +if (*pipe_char =='|' ) {if (ftrace->graph_verbose) {/* In verbose mode, duration starts after the comm-pid '|' */
@@ -611,10 +614,12 @@ static void make_blank_prefix(const struct perf_ftrace *ftrace, const char *line* [ abstime |] CPU) [ comm-pid |] [ duration |] func * * The abstime and comm-pid columns are only present with --graph-opts -* verbose. +* verbose, whereas the duration column, and thus its '|' delimiter, is +* omitted with --graph-opts noduration. * * Return: A pointer to the last character of the context columns (the final -* '|' delimiter). Returns NULL if @line is not a trace record. +* '|' delimiter, or the CPU ')' when no delimiter follows it). Returns NULL +* if @line is not a trace record (e.g. a header or a context switch marker). */ static const char *find_func_column (const struct perf_ftrace *ftrace,const char *line,int *cpu)
@@ -668,9 +673,14 @@ static const char *find_func_column(const struct perf_ftrace *ftrace,return NULL ; } - p =strchr (p +1 ,'|' ); -if (!p) +if (!ftrace->graph_noduration) { + p =strchr (p +1 ,'|' ); +if (!p) +return NULL ; + }else if (strstr (p," => " ) ||strstr (p,"=====>" ) ||strstr (p,"<=====" )) { +/* Context switch or interrupt marker */ return NULL ; + } *cpu = (int )val;return p;
@@ -765,11 +775,12 @@ int ftrace_process_fgraph_line(struct perf_ftrace *ftrace, const char *line, FIL func_start =skip_spaces (after_pipe); leading_spaces = func_start - after_pipe; -if (*func_start =='\0' ) -return print_line (ftrace, line, out); - is_exit = (*func_start =='}' ); +if (*func_start =='\0' || + (!isalpha (*func_start) && *func_start !='_' && !is_exit)) +return print_line (ftrace, line, out); +if (is_exit) {char exit_name[128 ];
tools/perf/util/ftrace.h +1 -0
@@ -37,6 +37,7 @@ struct perf_ftrace {int graph_retaddr;int graph_nosleep_time;int graph_noirqs; +int graph_noduration;int graph_verbose;int graph_thresh;int graph_tail;
Signature · 1 line
2.55.0
PATCH v9 6/6 perf ftrace: Filter extraneous functions in function graph tracer
In reply to Aaron TomlinWhen tracing with the function graph tracer using a targeted filter (-G or --graph-funcs), users occasionally observe unrelated functions in the trace output:
1. Top-level functions (e.g. most notably finish_task_switch.isra.0()) appearing before or alongside the requested target function. This occurs because clearing and rewriting set_graph_function creates a transient window where remote CPUs still observe an empty filter (tracing all functions) before RCU synchronisation completes, coupled with perf ftrace sleeping in poll() on trace_pipe right as tracing starts.
2. Detached/orphan calls inside the target callgraph (e.g. housekeeping_any_cpu() and arch_irq_work_raise()) appearing as child frames under the target function. These routines are invoked by rb_wakeups() to notify perf's poll() on trace_pipe whenever an event is committed, and execute while the target function is on the CPU.
Resolve these issues deterministically in userspace:
1. Target callgraph state tracking
Track active target callgraph state per-CPU in tools/perf/util/ftrace.c. Any entry not matching the requested -G targets is suppressed along with its nested children until its scope exits. When a target function is matched (at root or nested depths), record its root depth; for leaf target calls, immediately reset the filter state upon completion to ensure subsequent sibling functions remain suppressed. Support glob wildcard matching (-G 'prefix_*' and compiler clone suffixes).
2. Caller provenance and orphan suppression
Within an active target callgraph, validate that child entries descend from an active frame on the target stack using their return addresses (CONFIG_FUNCTION_GRAPH_RETADDR). Any detached calls originating outside the target hierarchy (such as ring-buffer wakeups or asynchronous interrupts) are suppressed along with their subtrees (cs->orphan_depth).
Exits within an orphan subtree are suppressed without querying the target stack, preventing common helpers (such as _raw_spin_unlock) from colliding with or prematurely unspooling active target frames. Dropped-orphan recovery safely resets orphan depth whenever an entry or exit occurs at or outside the target root indentation.
3. Dynamic call stack allocation
Maintain the synthetic call stack as a dynamically reallocated array, growing on demand via realloc(). This supports arbitrarily deep call trees without fixed-size truncation, ensures unknown callers are unambiguously identified as orphans, and guarantees that standard filter teardown executes cleanly regardless of stack depth upon target function entry.
4. Automated verification
Add comprehensive regression tests in tools/perf/tests/ftrace.c (test_graph_target_filter()) validating top-level suppression, detached orphan suppression, glob matching, deep stack traversal, dropped-orphan recovery, and orphan exit collision prevention.
--- tools/perf/builtin-ftrace.c | 45 +- tools/perf/tests/ftrace.c | 2179 +++++++++++++++++++++++++++++++++++ tools/perf/util/ftrace.c | 767 +++++++++--- tools/perf/util/ftrace.h | 8 + 4 files changed, 2841 insertions(+), 158 deletions(-)
tools/perf/builtin-ftrace.c +32 -13
@@ -576,13 +576,19 @@ static int set_tracing_funcgraph_retval(struct perf_ftrace *ftrace)static int set_tracing_funcgraph_retaddr (struct perf_ftrace *ftrace) { -if (ftrace->graph_retaddr || ftrace->use_inline) { +bool need_retaddr = ftrace->graph_retaddr || ftrace->use_inline || + (ftrace->subcmd ==PERF_FTRACE_TRACE && + !list_empty (&ftrace->graph_funcs)); + +if (need_retaddr) {if (write_tracing_option_file ("funcgraph-retaddr" ,"1" ) <0 ) {if (ftrace->use_inline) {pr_err ("failed to set funcgraph-retaddr option in tracing\n" );pr_err ("Hint: ensure kernel is compiled with CONFIG_FUNCTION_GRAPH_RETADDR=y\n" ); +return -1 ; } -return -1 ; +if (ftrace->graph_retaddr) +return -1 ; } }
@@ -1381,6 +1387,8 @@ static int parse_func_duration(struct perf_ftrace *ftrace, char *line, size_t le {char *p;char *func; +char *semi; +char *ret_str;double duration;/* skip CPU */
@@ -1423,12 +1431,21 @@ static int parse_func_duration(struct perf_ftrace *ftrace, char *line, size_t le func =skip_spaces (p);/* skip the closing bracket and the start of comment */ -if (*func =='}' ) +if (*func =='}' ) { func +=5 ; + ret_str =strstr (func," ret=" ); +if (ret_str) + *ret_str ='\0' ; + p = func +strlen (func) -1 ; + }else { + semi =strchr (func,';' ); +if (semi) + *semi ='\0' ; + p = func +strlen (func) -1 ; + } -/* remove semi-colon or end of comment at the end */ - p = line + len -1 ; -while (!isalnum (*p) && *p !=']' ) { +/* remove parens, semi-colon or end of comment at the end */ +while (p > func && !isalnum (*p) && *p !=']' ) { *p ='\0' ; --p; }
@@ -1853,12 +1870,6 @@ static int parse_sort_key(const struct option *opt, const char *str, int unset)return 0 ; } -enum perf_ftrace_subcommand { -PERF_FTRACE_NONE , -PERF_FTRACE_TRACE , -PERF_FTRACE_LATENCY , -PERF_FTRACE_PROFILE , -};int cmd_ftrace (int argc,const char **argv) {
@@ -2016,6 +2027,8 @@ int cmd_ftrace(int argc, const char **argv)if (subcmd ==PERF_FTRACE_NONE ) subcmd =PERF_FTRACE_TRACE ; + ftrace.subcmd = subcmd; + argc =parse_options (argc, argv, options, ftrace_usage,PARSE_OPT_STOP_AT_NON_OPTION );if (argc <0 ) {
@@ -2037,9 +2050,15 @@ int cmd_ftrace(int argc, const char **argv) ret = -EINVAL ; goto out_delete_filters; } +if (ftrace.graph_thresh) { +pr_err ("Error: --inline cannot be combined with --graph-opts thresh\n" ); + ret = -EINVAL ; + goto out_delete_filters; + } }if (!ftrace.filter_retaddr_set) { -if (ftrace.use_inline && !ftrace.graph_retaddr) +if ((ftrace.use_inline || !list_empty (&ftrace.graph_funcs)) && + !ftrace.graph_retaddr) ftrace.filter_retaddr = true;else ftrace.filter_retaddr = false;
tools/perf/tests/ftrace.c +2179 -0
2,191 lines not on this page · raw message
tools/perf/util/ftrace.c +622 -145
719 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.55.0
Part of the diff is not shown here. View the raw message.
[email protected]
AI reviewRe: [PATCH perf-tools-next v9 5/6] perf ftrace: Support omitting execution duration in function graph tracer
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 16 lines
The Linux kernel's function graph tracer displays the execution duration of completed functions by default via the funcgraph-duration trace option. Whilst informative, in scenarios where the user is solely interested in the call graph topology, call flow sequence, or inlined hierarchy, the duration column can add visual clutter and widen each trace line.
Introduce support for omitting function duration by exposing the noduration sub-option under --graph-opts. Consistent with the existing nosleep-time and noirqs sub-options, it accepts an optional value, so noduration=0 explicitly retains the duration column; when specified more than once, the last occurrence takes effect. When enabled, perf ftrace configures the kernel's tracing instance option funcgraph-duration to 0, and restores it to 1 upon resetting tracing options.
Quoted 5 lines
Because the profile subcommand fundamentally requires function execution duration to compute timing statistics and call counts, combining noduration with perf ftrace profile is rejected with an explicit error.
Signed-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH perf-tools-next v9 2/6] perf ftrace: Support filtering return address comments
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 14 lines
When tracing with the Linux kernel's function graph tracer, enabling funcgraph-retaddr records the return address of each function entry as a comment in the trace stream (e.g. /* <-load_elf_phdrs+0x6c/0xb0 */). While valuable for tracing execution origins, these trailing comments can add considerable visual clutter to the call graph, particularly when examining deeply nested call trees or when synthesising higher-level abstractions.
Introduce support for filtering out return address comments from function graph trace output. Provide a standalone --filter-retaddr flag.
Key aspects of this implementation:
Quoted 7 lines
1. Return address parsing and filtering
[ ... ]
processing") intools/perf/tests/ftrace.cto validate comment parsing and string filtering across all supported formats.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH perf-tools-next v9 4/6] perf ftrace: Support display of inlined functions in function graph tracer
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 19 lines
The Linux kernel's function graph tracer operates at the machine instruction level via compiler instrumentation (-fpatchable-function-entry), recording entry and return events for physical function calls. Consequently, functions inlined by the compiler are invisible in the resulting call graph, as no discrete call or return instructions are emitted for them. While technically expected, this omission frequently obscures the logical execution flow when analysing kernel subsystems heavily reliant upon inlining.
Address this by introducing the --inline option to perf ftrace for the
function_graphtracer.When the kernel is configured with
CONFIG_FUNCTION_GRAPH_RETADDR=y, the funcgraph-retaddr trace option records the caller's return address on each function entry, manifested in the trace stream as a comment [ ... ]
toggles, bare closing braces handling, task context-switch isolation and migration, and stack saturation/overflow recovery.
Quoted 2 lines
Assisted-by Antigravity:gemini-3.1-proSigned-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
[email protected]
AI reviewRe: [PATCH perf-tools-next v9 6/6] perf ftrace: Filter extraneous functions in function graph tracer
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 13 lines
When tracing with the function graph tracer using a targeted filter (-G or --graph-funcs), users occasionally observe unrelated functions in the trace output:
1. Top-level functions (e.g. most notably finish_task_switch.isra.0()) appearing before or alongside the requested target function. This occurs because clearing and rewriting
set_graph_functioncreates a transient window where remote CPUs still observe an empty filter (tracing all functions) before RCU synchronisation completes, coupled with perf ftrace sleeping inpoll()ontrace_piperight as tracing starts.
Quoted 8 lines
2. Detached/orphan calls inside the target callgraph (e.g.
housekeeping_any_cpu()andarch_irq_work_raise())
[ ... ]
detached orphan suppression, glob matching, deep stack traversal, dropped-orphan recovery, and orphan exit collision prevention.Signed-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH perf-tools-next v9 3/6] perf symbol: Introduce vmlinux_path__find() helper
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 8 lines
The
vmlinux_patharray maintains standard paths used by perf to locate the kernel image (e.g. "vmlinux", "/boot/vmlinux", "/lib/modules/%s/build/vmlinux", "/usr/lib/debug/..."). Whilevmlinux_path__init()populates these candidates anddso__load_vmlinux_path()loads symbols into a DSO/map, callers (such as test suites or probing tools) often need to verify the existence of an accessible vmlinux on disk before attempting machine setup or test execution.
Quoted 5 lines
Introduce
vmlinux_path__find()to search through the configuredvmlinux_pathentries (or symbol_conf.vmlinux_name if explicitly set) and return the path to the first readable vmlinux file, or NULL if none is accessible.Signed-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
[email protected]
AI reviewRe: [PATCH perf-tools-next v9 1/6] perf ftrace: Optimise __cmd_ftrace() stack frame with strbuf streaming
In reply to Aaron TomlinSashiko has reviewed this patch and found no issues.
Quoted 16 lines
In
__cmd_ftrace(), incoming trace data fromtrace_pipewas historically buffered in a fixed 4096-byte array allocated directly on the stack frame. Furthermore, reads were written directly to stdout viafwrite()in arbitrary chunk sizes, rather than as discrete, well-delimited trace lines.Refactor
__cmd_ftrace()to employ dynamic heap allocation for the read buffer alongside a dynamic struct strbuf to accumulate streamed characters into whole lines. Pre-allocating the strbuf ensures that line processing incurs zero heap reallocations during steady-state polling, while the stack frame of__cmd_ftrace()is reduced by over 96% (down to ~160 bytes). Robust error and EOF detection on stdout and the trace stream ensures polling terminates cleanly upon pipeline or pager exit, while retrying on non-fatal EAGAIN and EINTR conditions prevents premature termination on non-blocking reads or signal interruptions.
Quoted 1 line
Signed-off-by Aaron Tomlin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 93 lines · Aaron Tomlin
The Linux kernel's function graph tracer operates at the machine instruction level via compiler instrumentation (-fpatchable-function-entry), recording entry and return events for physical function calls. Consequently, functions inlined by the compiler are invisible in the resulting call graph, as no discrete call or return instructions are emitted for them. While technically expected, this omission frequently obscures the logical execution flow when analysing kernel subsystems heavily reliant upon inlining (such as the scheduler, memory management, locking primitives, and RCU).
When the kernel is configured with
CONFIG_FUNCTION_GRAPH_RETADDR=y, the funcgraph-retaddr trace option records the caller's return address on each function entry, manifested in the trace stream as a comment (e.g. /* <-wake_up_new_task+0x1d1/0x3e0 */). By interrogating DWARF debug information from the kernel image (vmlinux) utilising libdw, perf ftrace resolves this return address to its inlined callchain and synthesises the intermediate inlined frames directly into the streamed call graph.This patch series addresses these challenges through six focused commits:
1. Stack frame optimisation in
__cmd_ftrace()Replaces the 4096-byte stack array in
__cmd_ftrace()with dynamic heap allocation alongside struct strbuf streaming, reducing the stack frame footprint by over 96% (down to ~160 bytes) whilst ensuring complete line-delimited records, explicit NUL termination, and robust error unwind and EOF handling.2. Return address comment filtering
Introduces
ftrace_parse_retaddr()andftrace_filter_retaddr()with --filter-retaddr and --graph-opts filter-retaddr to strip caller comments from trace output, accompanied by an initial unit test suite intools/perf/tests/ftrace.c.3. Preparatory vmlinux lookup helper
Introduces
vmlinux_path__find()intools/perf/util/symbol.candsymbol.hto search standard vmlinux paths and return the first accessible kernel image, providing a generic helper acrosstools/perfand avoiding hardcodedaccess()checks in tests.4. Inlined function call graph reconstruction
Introduces --inline and -k/--vmlinux, dynamically synthesising inlined frames marked with /* (inline) */. Correctly matches bare closing braces when funcgraph-tail is disabled, clamps stack depth to prevent indentation drift, and expands the automated test suite to verify inline resolution, comment filtering, and stack saturation recovery utilising
vmlinux_path__find().5. Execution duration suppression
Introduces --graph-opts noduration (accepting noduration=[0|1]) to toggle the kernel's funcgraph-duration tracing option, omitting the duration column when the user is focused purely on call graph topology and inlined hierarchy. Rejects noduration when invoked under 'perf ftrace profile'.
6. Extraneous function filtering in function graph tracer
Introduces userspace call graph filtering when targeted tracing (-G or --graph-funcs) is active. Suppresses transient unrelated entries arriving before filter activation as well as detached callee trees (such as ring-buffer wakeups) originating outside the target hierarchy. Dynamically resises the synthetic call stack to eliminate saturation edge cases and accurately suppresses detached subtrees without orphan exit name collisions.
Synthesised inlined functions are rendered with an explicit /* (inline) */ annotation at both entry and exit:
# tracer:
function_graph
#
# CPU FUNCTION CALLS
# | | | | |
0)enqueue_entity(){
0)update_cfs_group(){ /* (inline) */
0)calc_concur_shares(){
0)tg_cpus(){
0)cpuset_num_cpus(){
0)class_rcu_constructor(){ /* (inline) */
0)rcu_read_lock(){ /* (inline) */
0)__rcu_read_lock();
0) } /*rcu_read_lock(inline) */
0) } /*class_rcu_constructor(inline) */
0)cgroup_e_css();
0)class_rcu_destructor(){ /* (inline) */
0)rcu_read_unlock(){ /* (inline) */
0)__rcu_read_unlock();
Quoted 8 lines
0) } /*
rcu_read_unlock(inline) */
0) } /*class_rcu_destructor(inline) */
0) }
0) }
0)__calc_smp_shares();
0)reweight_entity();
0) } /*update_cfs_group(inline) */
0) }
Lots of tests, Sashiko is happy and a useful feature! I'm not sure "--filter-retaddr" is the best name for an option to disable comments on return addresses, but naming things is hard.
Thanks!
Ian
Quoted 271 lines
Changes since v8:
- Preserved funcgraph-retval return value comments when filtering funcgraph-retaddr comments (/* <-caller ret=0x0 */ -> /* ret=0x0 */) in
ftrace_filter_retaddr()- Enhanced
vmlinux_path__find()helper intools/perf/util/symbol.c:
- Avoided redundant memory allocation invmlinux_path__init()whenvmlinux_pathis already initialised
- Ensured symbol configuration is initialised viasymbol__init()prior to path resolution
- Guaranteedvmlinux_pathmemory cleanup by invokingvmlinux_path__exit()unconditionally insymbol__exit()- Tracked raw (pre-inline-expansion) indentation on synthetic stack frames, ensuring bare closing braces ("}") are accurately matched against corresponding non-inlined frames without indentation distortion
- Unwound stale frames on untracked caller entries (
caller_idx< 0) whose raw indentation is greater than or equal to the new entry, and closed trailing open inlined frames resulting from dropped exit events- Pre-bound single-PID tracing across all CPUs in
perf_ftrace__setup_inlines(), ensuring consistent synthetic call stack tracking across CPU migration when kernel context-switch banners are suppressed (verif_pidprev_pid== -1)- Strengthened error handling during DWARF/vmlinux initialisation in
perf_ftrace__setup_inlines(), checking load return codes, emitting diagnostic warnings, and safely freeingftrace->machineon failure- Recognised interrupt/IRQ entry and exit markers ("=====>" and "<=====") alongside context-switch markers under --graph-opts noduration, preventing non-function lines from corrupting function column detection or popping active stack frames
- Handled leaf function lines with trailing comments
(e.g. funcgraph-retaddr or funcgraph-retval comments) inparse_func_duration()for 'perf ftrace profile', ensuring parentheses are stripped cleanly and leaf records match non-leaf entries in duration tracking- Preserved tracked ancestor frames on
cs->stackacross dropped target root exits by verifying caller provenance
(caller_idx<cs->target_root_depth) before checking indentation bounds inftrace_check_target_caller()- Purged stale non-target frames below target root upon target root entry when
caller_idx< 0, preventing unrelated ancestor frames from prematurely terminating subsequent target calls- Prevented
cs->orphan_depthlockouts when both an orphan exit and enclosing target root exit are dropped by resettingcs->orphan_depthon any line at or outside target root indentation (leading_spaces<=cs->target_root_indent), and allowing ancestor exits to fall through to match frames oncs->stack- Preserved interrupt/IRQ markers inside active target callgraphs while suppressing those outside target filters under -G or --graph-funcs
- Fixed
test_stack_saturation()to avoid premature frame unwinding, ensuring depth clamping and recovery are tested as intended- Expanded automated test coverage in
tools/perf/tests/ftrace.c:
- Addedtest_context_switch_handling()for CPU migration and task tracking
- Addedtest_column_layouts()covering standard, verbose, and noduration formats including IRQ and context-switch markers
- Added regression tests intest_graph_target_filter()covering double dropped exits, ancestor frame preservation, leaf duration parsing, and deep orphan recovery- Link to v8: https://lore.kernel.org/lkml/[email protected]/
Changes since v7:
- Rebased onto latest perf-tools-next/perf-tools-next (commit
1c90f0a3c8a1("perf kvm: Casttv_usecto long to fix build on MIPS 32-bits"))- Link to v7: https://lore.kernel.org/all/[email protected]/
Changes since v6:
- Expanded the series from 5 to 6 patches by introducing a standalone preparatory commit ("perf symbol: Introduce
vmlinux_path__find()helper") intools/perf/util/symbol.candsymbol.hto locate accessible kernel images via configured vmlinux paths, replacing ad-hocaccess("vmlinux", R_OK)checks in unit tests (Ian Rogers)- Explicitly included
stdio.hintools/perf/util/ftrace.hto declare the FILE type forftrace_process_fgraph_line(), ensuring clean compilation under musl libc and non-glibc environments- Prevented name collisions between orphan callee exits and active target frames in
ftrace_process_fgraph_line()- Allowed dropped-orphan exit recovery on closing braces only when the exit explicitly names the target root function itself, ensuring that lost orphan exits do not swallow the target root's exit while protecting intermediate callee frames from collisions
- Replaced the fixed-size array in struct
ftrace_task_statewith a dynamically resized stack grown viarealloc(), completely eliminatingcs->overflow_depthand its associated failure modes:
- Fixed skipped filter teardown when a target function begins at the stack limit, ensuring standard teardown runs upon root exit and preventing subsequent sibling functions from leaking into filtered output- Fixed orphan filter bypass during deep call stacks by preserving all caller frames on the stack, ensuring unknown callers (
caller_idx< 0) are accurately identified as orphans and suppressed- Link to v6: https://lore.kernel.org/all/[email protected]/
Changes since v5:
- Prevented call stack desynchronisation on dropped orphan exit events by clearing
orphan_depthwhen a named exit matches a tracked frame or an entry descends from the target callgraph- Fixed premature target filter termination on child exits by capturing
cs->target_root_depthafter unwinding stale frames above the caller- Ensured synthesised caller inlines above
cs->target_root_depthare cleanly closed upon target function exit before resetting filter state- Prevented legitimate deep calls exceeding
FTRACE_MAX_STACKfrom being falsely dropped as orphans by checkingcs->overflow_depth- Expanded unit tests to cover dropped orphan exits, target root depth synchronisation after stale siblings, and deep call stack overflows
- Provided support for glob pattern matching with -G or --graph-funcs
- Link to v5: https://lore.kernel.org/all/[email protected]/
Changes since v4:
- Handled non-fatal EAGAIN, EWOULDBLOCK, and EINTR in
__cmd_ftrace()polling and draining loops to prevent premature session termination on non-blocking reads or signal interruptions- Eliminated an unused local variable endptr in
ftrace_parse_retaddr()by passing NULL tostrtoull()- Prevented stack buffer overflows in
make_blank_prefix()by clamping the blanking loop bounds and end pointer to the destination buffer length when processing trace records with exceptionally long prefixes- Tracked call stack saturation with
overflow_depthto prevent untracked function exits with bare closing braces from prematurely popping tracked frames from the parser stack on deep call chains exceedingFTRACE_MAX_STACK- Ensured stale non-inlined callee frames resulting from dropped function exits do not block inactive inlined frames from being closed upon subsequent entries by unwinding the stack down to
caller_idx+ 1 + common- Fixed filter state not resetting for targeted leaf calls when --inline is active: synthesised inline frames inflated
cs->depthbeyondcs->target_root_depth, causing subsequent sibling functions to be falsely classified as detached orphans and bypassing inline unwinding; resolved by tracking target root entries (is_target_root), explicitly unwinding all synthesised inlines, and resetting filter state upon leaf completion- Expanded unit test coverage to verify recovery across dropped exit events and bare closing braces, and to verify that targeted leaf calls with synthesised inlined frames properly close inlines and reset filter state
- Link to v4: https://lore.kernel.org/lkml/[email protected]/
Changes since v3:
- Explicitly rejected the noduration option when invoked under the profile subcommand, since profiling inherently requires function duration to compute timing statistics
- Fixed
make_blank_prefix()clobbering the comm-pid column in verbose mode by searching backwards for the preceding "|" delimiter, and returning early whengraph_nodurationis active- Fixed filter state not resetting for targeted leaf calls at non-zero call stack depths by checking
cs->depth<=cs->target_root_depthand clearingcs->target_root_depth, preventing subsequent non-targeted sibling functions within the parent frame from leaking into trace output- Expanded unit test coverage to verify comm-pid preservation under verbose and verbose-noduration inline modes, and to verify that targeted leaf calls at depth > 0 properly reset the target filter state.
- Link to v3: https://lore.kernel.org/lkml/[email protected]/
Changes since v2:
- Expanded the series from 4 to 5 patches by adding a dedicated commit ("perf ftrace: Filter extraneous functions in function graph tracer") to enforce strict target callgraph boundaries when -G or --graph-funcs is specified
- Suppressed unrelated top-level functions that leak through the transient tracefs filter initialisation window before RCU synchronisation completes
- Filtered detached/orphan calls from appearing as nested children within the target graph by validating caller return addresses against the active target stack
- Tracked nested orphan scope depth (
cs->orphan_depth) to cleanly suppress child calls and closing delimiters of detached subtrees without desynchronising the target stack- Added automated unit test coverage in
tools/perf/tests/ftrace.c(test_graph_target_filter()) validating top-level suppression (both with and without --inline) and detached orphan wakeup suppression- Link to v2: https://lore.kernel.org/lkml/[email protected]/
Changes since v1:
- Modularised the implementation into a 4-patch bisectable series rather than a monolithic commit
- Resolved potential infinite loop on
poll()upontrace_pipeEOF- Fixed stuck call stack frames when funcgraph-tail is disabled by supporting bare closing delimiters matching LIFO stack frames
- Prevented unbounded indentation growth on deep call trees by clamping inline expansion and synchronising
cs->inlined_depthto frames successfully pushed ontocs->stack- Added dedicated unit tests covering bare closing braces and stack saturation recovery
- Refined option documentation in perf-ftrace.txt
- Link to v1: https://lore.kernel.org/lkml/[email protected]/
Aaron Tomlin (6):
perf ftrace: Optimise__cmd_ftrace()stack frame with strbuf streaming perf ftrace: Support filtering return address comments perf symbol: Introducevmlinux_path__find()helper
perf ftrace: Support display of inlined functions in function graph
tracer
perf ftrace: Support omitting execution duration in function graph
tracer
perf ftrace: Filter extraneous functions in function graph tracertools/perf/Documentation/perf-ftrace.txt | 20 + tools/perf/builtin-ftrace.c | 212 +- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/ftrace.c | 3271 ++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/util/Build | 1 + tools/perf/util/ftrace.c | 1634 +++++++++++ tools/perf/util/ftrace.h | 28 + tools/perf/util/symbol.c | 38 +- tools/perf/util/symbol.h | 2 + 11 files changed, 5180 insertions(+), 29 deletions(-) create mode 100644 tools/perf/tests/ftrace.c create mode 100644 tools/perf/util/ftrace.c--
2.55.0
Quoted 107 lines · Ian Rogers
Quoted 93 lines · Aaron Tomlin
The Linux kernel's function graph tracer operates at the machine instruction level via compiler instrumentation (-fpatchable-function-entry), recording entry and return events for physical function calls. Consequently, functions inlined by the compiler are invisible in the resulting call graph, as no discrete call or return instructions are emitted for them. While technically expected, this omission frequently obscures the logical execution flow when analysing kernel subsystems heavily reliant upon inlining (such as the scheduler, memory management, locking primitives, and RCU).
When the kernel is configured with
CONFIG_FUNCTION_GRAPH_RETADDR=y, the funcgraph-retaddr trace option records the caller's return address on each function entry, manifested in the trace stream as a comment (e.g. /* <-wake_up_new_task+0x1d1/0x3e0 */). By interrogating DWARF debug information from the kernel image (vmlinux) utilising libdw, perf ftrace resolves this return address to its inlined callchain and synthesises the intermediate inlined frames directly into the streamed call graph.This patch series addresses these challenges through six focused commits:
1. Stack frame optimisation in
__cmd_ftrace()Replaces the 4096-byte stack array in
__cmd_ftrace()with dynamic heap allocation alongside struct strbuf streaming, reducing the stack frame footprint by over 96% (down to ~160 bytes) whilst ensuring complete line-delimited records, explicit NUL termination, and robust error unwind and EOF handling.2. Return address comment filtering
Introduces
ftrace_parse_retaddr()andftrace_filter_retaddr()with --filter-retaddr and --graph-opts filter-retaddr to strip caller comments from trace output, accompanied by an initial unit test suite intools/perf/tests/ftrace.c.3. Preparatory vmlinux lookup helper
Introduces
vmlinux_path__find()intools/perf/util/symbol.candsymbol.hto search standard vmlinux paths and return the first accessible kernel image, providing a generic helper acrosstools/perfand avoiding hardcodedaccess()checks in tests.4. Inlined function call graph reconstruction
Introduces --inline and -k/--vmlinux, dynamically synthesising inlined frames marked with /* (inline) */. Correctly matches bare closing braces when funcgraph-tail is disabled, clamps stack depth to prevent indentation drift, and expands the automated test suite to verify inline resolution, comment filtering, and stack saturation recovery utilising
vmlinux_path__find().5. Execution duration suppression
Introduces --graph-opts noduration (accepting noduration=[0|1]) to toggle the kernel's funcgraph-duration tracing option, omitting the duration column when the user is focused purely on call graph topology and inlined hierarchy. Rejects noduration when invoked under 'perf ftrace profile'.
6. Extraneous function filtering in function graph tracer
Introduces userspace call graph filtering when targeted tracing (-G or --graph-funcs) is active. Suppresses transient unrelated entries arriving before filter activation as well as detached callee trees (such as ring-buffer wakeups) originating outside the target hierarchy. Dynamically resises the synthetic call stack to eliminate saturation edge cases and accurately suppresses detached subtrees without orphan exit name collisions.
Synthesised inlined functions are rendered with an explicit /* (inline) */ annotation at both entry and exit:
# tracer:
function_graph
#
# CPU FUNCTION CALLS
# | | | | |
0)enqueue_entity(){
0)update_cfs_group(){ /* (inline) */
0)calc_concur_shares(){
0)tg_cpus(){
0)cpuset_num_cpus(){
0)class_rcu_constructor(){ /* (inline) */
0)rcu_read_lock(){ /* (inline) */
0)__rcu_read_lock();
0) } /*rcu_read_lock(inline) */
0) } /*class_rcu_constructor(inline) */
0)cgroup_e_css();
0)class_rcu_destructor(){ /* (inline) */
0)rcu_read_unlock(){ /* (inline) */
0)__rcu_read_unlock();Quoted 8 lines
0) } /*
rcu_read_unlock(inline) */
0) } /*class_rcu_destructor(inline) */
0) }
0) }
0)__calc_smp_shares();
0)reweight_entity();
0) } /*update_cfs_group(inline) */
0) }Lots of tests, Sashiko is happy and a useful feature! I'm not sure "--filter-retaddr" is the best name for an option to disable comments on return addresses, but naming things is hard.
Quoted 5 lines
Tested-by Ian Rogers <[email protected]>Reviewed-by Ian Rogers <[email protected]>Thanks!
Ian
Indeed the name is not the best :)
Thank you Ian!
Signature · 1 line
Aaron Tomlin
Namhyung Kim
maintainerRe: [PATCH perf-tools-next v9 1/6] perf ftrace: Optimise __cmd_ftrace() stack frame with strbuf streaming
In reply to Aaron TomlinQuoted 6 lines · Aaron Tomlin
In
__cmd_ftrace(), incoming trace data fromtrace_pipewas historically buffered in a fixed 4096-byte array allocated directly on the stack frame. Furthermore, reads were written directly to stdout viafwrite()in arbitrary chunk sizes, rather than as discrete, well-delimited trace lines.
Quoted 9 lines
Refactor
__cmd_ftrace()to employ dynamic heap allocation for the read buffer alongside a dynamic struct strbuf to accumulate streamed characters into whole lines. Pre-allocating the strbuf ensures that line processing incurs zero heap reallocations during steady-state polling, while the stack frame of__cmd_ftrace()is reduced by over 96% (down to ~160 bytes). Robust error and EOF detection on stdout and the trace stream ensures polling terminates cleanly upon pipeline or pager exit, while retrying on non-fatal EAGAIN and EINTR conditions prevents premature termination on non-blocking reads or signal interruptions.
I think it's better to split the error handling into its own commit maybe with a Fixes tag. But I'm curious if it's possible to get EAGAIN on read() after poll().
Thanks,
Namhyung
Quoted 163 lines
Signed-off-by Aaron Tomlin <[email protected]>--- tools/perf/builtin-ftrace.c | 79 +++++++++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 13 deletions(-)tools/perf/builtin-ftrace.c +66
-13@@ -14,6 +14,7 @@#include <poll.h> #include <semaphore.h> #include <signal.h> +#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <linux/capability.h> @@ -41,7 +42,10 @@#include "util/units.h" #include "util/parse-sublevel-options.h" +#include "util/strbuf.h" +#define DEFAULT_TRACER "function_graph" +#define TRACE_BUF_SIZE 4096static volatile sig_atomic_t workload_exec_errno;static volatile sig_atomic_t done;@@ -738,7 +742,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) {char *trace_file;int trace_fd; -char buf[4096 ]; +char *buf =NULL ; +struct strbuf linebuf =STRBUF_INIT ;struct pollfd pollfd = { .events =POLLIN , };@@ -767,10 +772,18 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)setup_pager (); + buf =malloc (TRACE_BUF_SIZE ); +if (!buf) { +pr_err ("failed to allocate trace buffer\n" ); + goto out_reset; + } +strbuf_init (&linebuf,512 ); +strbuf_setlen (&linebuf,0 ); + trace_file =get_tracing_instance_file ("trace_pipe" );if (!trace_file) {pr_err ("failed to open trace_pipe\n" ); - goto out_reset; + goto out_free; } trace_fd =open (trace_file,O_RDONLY );@@ -779,7 +792,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)if (trace_fd <0 ) {pr_err ("failed to open trace_pipe\n" ); - goto out_reset; + goto out_free; }fcntl (trace_fd,F_SETFL ,O_NONBLOCK );@@ -806,24 +819,42 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) }while (!done) { -if (poll (&pollfd,1 ,-1 ) <0 ) +if (poll (&pollfd,1 ,-1 ) <0 ) { +if (errno ==EINTR ) +continue ;break ; + }if (pollfd.revents &POLLIN ) { -int n =read (trace_fd, buf,sizeof (buf)); -if (n <0 ) +int n =read (trace_fd, buf,TRACE_BUF_SIZE ); + +if (n <0 ) { +if (errno ==EAGAIN || errno ==EWOULDBLOCK || errno ==EINTR ) +continue ;break ; -if (fwrite (buf, n,1 , stdout) !=1 ) + } +if (n ==0 )break ; +for (int i =0 ; i < n; i++) { +if (buf[i] =='\n' ) { +if (fprintf (stdout,"%s\n" , linebuf.buf) <0 ) +break ; +strbuf_setlen (&linebuf,0 ); + }else { +if (strbuf_addch (&linebuf, buf[i]) <0 ) + goto out_close_fd; + } + }/* flush output since stdout is in full buffering mode due to pager */ -fflush (stdout); +if (fflush (stdout) <0 ||ferror (stdout)) +break ; } }write_tracing_file ("tracing_on" ,"0" );if (workload_exec_errno) { -const char *emsg =str_error_r (workload_exec_errno, buf,sizeof (buf)); +const char *emsg =str_error_r (workload_exec_errno, buf,TRACE_BUF_SIZE );/* flush stdout first so below error msg appears at the end. */ fflush (stdout);pr_err ("workload failed: %s\n" , emsg);@@ -831,16 +862,38 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) }/* read remaining buffer contents */ -while (true) { -int n =read (trace_fd, buf,sizeof (buf)); -if (n <=0 ) +while (!ferror (stdout)) { +int n =read (trace_fd, buf,TRACE_BUF_SIZE ); + +if (n <0 ) { +if (errno ==EINTR ) +continue ;break ; -if (fwrite (buf, n,1 , stdout) !=1 ) + } +if (n ==0 )break ; +for (int i =0 ; i < n; i++) { +if (buf[i] =='\n' ) { +if (fprintf (stdout,"%s\n" , linebuf.buf) <0 ) +break ; +strbuf_setlen (&linebuf,0 ); + }else { +if (strbuf_addch (&linebuf, buf[i]) <0 ) + goto out_close_fd; + } + } + } + +if (!ferror (stdout) && linebuf.len >0 ) { +fprintf (stdout,"%s\n" , linebuf.buf); +strbuf_setlen (&linebuf,0 ); } out_close_fd:close (trace_fd); +out_free: +free (buf); +strbuf_release (&linebuf); out_reset:exit_tracing_instance (); out:Signature · 1 line
2.55.0
Re: [PATCH perf-tools-next v9 1/6] perf ftrace: Optimise __cmd_ftrace() stack frame with strbuf streaming
Quoted 17 lines · Namhyung Kim
Quoted 6 lines · Aaron Tomlin
In
__cmd_ftrace(), incoming trace data fromtrace_pipewas historically buffered in a fixed 4096-byte array allocated directly on the stack frame. Furthermore, reads were written directly to stdout viafwrite()in arbitrary chunk sizes, rather than as discrete, well-delimited trace lines.Quoted 9 lines
Refactor
__cmd_ftrace()to employ dynamic heap allocation for the read buffer alongside a dynamic struct strbuf to accumulate streamed characters into whole lines. Pre-allocating the strbuf ensures that line processing incurs zero heap reallocations during steady-state polling, while the stack frame of__cmd_ftrace()is reduced by over 96% (down to ~160 bytes). Robust error and EOF detection on stdout and the trace stream ensures polling terminates cleanly upon pipeline or pager exit, while retrying on non-fatal EAGAIN and EINTR conditions prevents premature termination on non-blocking reads or signal interruptions.
Quoted 6 lines
I think it's better to split the error handling into its own commit maybe with a Fixes tag. But I'm curious if it's possible to get EAGAIN on
read()afterpoll().Thanks,
Namhyung
Hi Namhyung
I agree with splitting the error handling and signal resilience into a dedicated commit ahead of the stack frame and strbuf streaming change. The error handling and non-blocking retry logic is an independent bugfix applicable to stable kernels.
Regarding your question about whether read() can return EAGAIN after poll(), yes, it can. With trace_pipe configured for non-blocking I/O in __cmd_ftrace():
fcntl(trace_fd, F_SETFL, O_NONBLOCK);
There are indeed several [not limited to the following] scenarios in the kernel where read() returns -EAGAIN after poll() reports POLLIN:
1. The kernel implementation of non-blocking read on trace_pipe:
When read() is invoked on trace_pipe, tracing_read_pipe() calls
tracing_wait_pipe() in kernel/trace/trace.c:
static int tracing_wait_pipe(struct file *filp)
{
struct trace_iterator *iter = filp->private_data;
while (trace_empty(iter)) {
if ((filp->f_flags & O_NONBLOCK)) {
return -EAGAIN;
}
...
}
return 1;
}
Whenever trace_empty(iter) evaluates to true on a non-blocking
descriptor, the kernel explicitly returns -EAGAIN.
2. TRACE_ITER(BLOCK) trace option:
See trace_poll() in kernel/trace/trace.c:
static __poll_t
trace_poll(struct trace_iterator *iter, struct file *filp, poll_table *poll_table)
{
struct trace_array *tr = iter->tr;
if (trace_buffer_iter(iter, iter->cpu_file))
return EPOLLIN | EPOLLRDNORM;
if (tr->trace_flags & TRACE_ITER(BLOCK))
/* Always select as readable when in blocking mode */
return EPOLLIN | EPOLLRDNORM;
else
return ring_buffer_poll_wait(iter->array_buffer->buffer, iter->cpu_file,
filp, poll_table, iter->tr->buffer_percent);
}
If block mode is set on the trace instance (e.g. via
trace_options), trace_poll() unconditionally returns EPOLLIN |
EPOLLRDNORM regardless of whether the ring buffer contains any
data. The subsequent non-blocking read() immediately hits the
trace_empty() check in tracing_wait_pipe() and returns -EAGAIN.I will split this into two commits for v10.
Kind regards,
Signature · 1 line
Aaron Tomlin
Namhyung Kim
maintainerRe: [PATCH perf-tools-next v9 2/6] perf ftrace: Support filtering return address comments
In reply to Aaron TomlinHello,
Quoted 11 lines · Aaron Tomlin
When tracing with the Linux kernel's function graph tracer, enabling funcgraph-retaddr records the return address of each function entry as a comment in the trace stream (e.g. /* <-load_elf_phdrs+0x6c/0xb0 */). While valuable for tracing execution origins, these trailing comments can add considerable visual clutter to the call graph, particularly when examining deeply nested call trees or when synthesising higher-level abstractions.
Introduce support for filtering out return address comments from function graph trace output. Provide a standalone --filter-retaddr flag.
I think we agreed not to have this option in the previous discussion, no? The retaddr will appear only if user passed an option, so you can unconditionally filter out the comment if it's not given, right?
Thanks,
Namhyung
Quoted 356 lines
Key aspects of this implementation:
1. Return address parsing and filtering
Implement
ftrace_filter_retaddr()intools/perf/util/ftrace.cto locate return address comments and strip them in-place with memmove(3).Additionally, introduce
ftrace_parse_retaddr()to parse caller symbol names and offsets across diverse output styles (standard format, return values, and bracketed addresses), establishing the parsing foundation for subsequent inlined callchain resolution.2. Command-line interface
Expose --filter-retaddr in
tools/perf/builtin-ftrace.c, updating the documentation intools/perf/Documentation/perf-ftrace.txtaccordingly.3. Automated verification
Add a dedicated unit test suite ("Ftrace return address processing") in
tools/perf/tests/ftrace.cto validate comment parsing and string filtering across all supported formats.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Aaron Tomlin <[email protected]>--- tools/perf/Documentation/perf-ftrace.txt | 4 + tools/perf/builtin-ftrace.c | 8 ++ tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/ftrace.c | 81 +++++++++++++++++++++ tools/perf/tests/tests.h | 1 + tools/perf/util/Build | 1 + tools/perf/util/ftrace.c | 93 ++++++++++++++++++++++++ tools/perf/util/ftrace.h | 5 ++ 9 files changed, 195 insertions(+) create mode 100644 tools/perf/tests/ftrace.c create mode 100644 tools/perf/util/ftrace.ctools/perf/Documentation/perf-ftrace.txt +4
-0@@ -134,6 +134,10 @@ OPTIONS for 'perf ftrace trace' - depth=<n> - Set max depth for function graph tracer to follow. - tail - Print function name at the end. +--filter-retaddr:: + Filter out function return address comments (`/* <-caller+offset */`) + from function_graph output. + OPTIONS for 'perf ftrace latency' ---------------------------------tools/perf/builtin-ftrace.c +8
-0@@ -837,6 +837,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)break ;for (int i =0 ; i < n; i++) {if (buf[i] =='\n' ) { +if (ftrace->filter_retaddr) +ftrace_filter_retaddr (linebuf.buf);if (fprintf (stdout,"%s\n" , linebuf.buf) <0 )break ;strbuf_setlen (&linebuf,0 );@@ -874,6 +876,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)break ;for (int i =0 ; i < n; i++) {if (buf[i] =='\n' ) { +if (ftrace->filter_retaddr) +ftrace_filter_retaddr (linebuf.buf);if (fprintf (stdout,"%s\n" , linebuf.buf) <0 )break ;strbuf_setlen (&linebuf,0 );@@ -885,6 +889,8 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) }if (!ferror (stdout) && linebuf.len >0 ) { +if (ftrace->filter_retaddr) +ftrace_filter_retaddr (linebuf.buf);fprintf (stdout,"%s\n" , linebuf.buf);strbuf_setlen (&linebuf,0 ); }@@ -1850,6 +1856,8 @@ int cmd_ftrace(int argc, const char **argv) "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),OPT_BOOLEAN (0 ,"inherit" , &ftrace.inherit,"Trace children processes" ), +OPT_BOOLEAN_SET (0 ,"filter-retaddr" , &ftrace.filter_retaddr, &ftrace.filter_retaddr_set, +"Filter out funcgraph return address comments" ),OPT_INTEGER ('D' ,"delay" , &ftrace.target.initial_delay,"Number of milliseconds to wait before starting tracing after program start" ), OPT_PARENT(common_options),tools/perf/tests/Build +1
-0@@ -72,6 +72,7 @@ perf-test-y += hwmon_pmu.o perf-test-y += tool_pmu.o perf-test-y += subcmd-help.o perf-test-y += kallsyms-split.o +perf-test-y += ftrace.o ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc riscv)) perf-test-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.otools/perf/tests/builtin-test.c +1
-0@@ -156,6 +156,7 @@ static struct test_suite *generic_tests[] = { &suite__perf_hooks, &suite__unit_number__scnprint, &suite__mem2node, + &suite__ftrace, &suite__time_utils, &suite__jit_write_elf, &suite__pfm,tools/perf/tests/ftrace.c new file +81
-0@@ -0,0 +1,81 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/compiler.h> +#include <linux/kernel.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include "tests.h" +#include "debug.h" +#include "util/ftrace.h" + +static int test_parse_retaddr (void ) +{ +char sym[128 ]; +u64 offset =0 ; + +/* Standard format */ +TEST_ASSERT_VAL ("parse standard retaddr" , +ftrace_parse_retaddr ("kernel_read() { /* <-load_elf_phdrs+0x6c/0xb0 */" , + sym,sizeof (sym), &offset)); +TEST_ASSERT_EQUAL ("sym name" ,strcmp (sym,"load_elf_phdrs" ),0 ); +TEST_ASSERT_VAL ("offset" , offset ==0x6c ); + +/* With return value */ +TEST_ASSERT_VAL ("parse retaddr with ret" , +ftrace_parse_retaddr ( +"__cond_resched(); /* <-load_elf_phdrs+0x6c/0xb0 ret=0x0 */" , + sym,sizeof (sym), &offset)); +TEST_ASSERT_EQUAL ("sym name" ,strcmp (sym,"load_elf_phdrs" ),0 ); +TEST_ASSERT_VAL ("offset" , offset ==0x6c ); + +/* Bracketed address */ +TEST_ASSERT_VAL ("parse bracketed retaddr" , +ftrace_parse_retaddr ("foo() { /* <-[0xffffffff818534c8] bar+0x20/0x40 */" , + sym,sizeof (sym), &offset)); +TEST_ASSERT_EQUAL ("sym name" ,strcmp (sym,"bar" ),0 ); +TEST_ASSERT_VAL ("offset" , offset ==0x20 ); + +/* No return address */ +TEST_ASSERT_VAL ("no retaddr" , + !ftrace_parse_retaddr ("do_filp_open();" , sym,sizeof (sym), &offset)); + +return TEST_OK ; +} + +static int test_filter_retaddr (void ) +{ +char line1[] ="kernel_read() { /* <-load_elf_phdrs+0x6c/0xb0 */" ; +char line2[] ="__cond_resched(); /* <-load_elf_phdrs+0x6c/0xb0 ret=0x0 */" ; +char line3[] ="do_filp_open();" ; + +ftrace_filter_retaddr (line1); +TEST_ASSERT_EQUAL ("filter standard retaddr" ,strcmp (line1,"kernel_read() {" ),0 ); + +ftrace_filter_retaddr (line2); +TEST_ASSERT_EQUAL ("filter retaddr with ret" , +strcmp (line2,"__cond_resched(); /* ret=0x0 */" ),0 ); + +ftrace_filter_retaddr (line3); +TEST_ASSERT_EQUAL ("filter line without retaddr unchanged" , +strcmp (line3,"do_filp_open();" ),0 ); + +return TEST_OK ; +} + +static int test__ftrace (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) +{ +int ret; + + ret =test_parse_retaddr (); +if (ret !=TEST_OK ) +return ret; + + ret =test_filter_retaddr (); +if (ret !=TEST_OK ) +return ret; + +return TEST_OK ; +} + +DEFINE_SUITE ("Ftrace return address processing" , ftrace);tools/perf/tests/tests.h +1
-0@@ -180,6 +180,7 @@ DECLARE_SUITE(perf_hooks);DECLARE_SUITE (unit_number__scnprint);DECLARE_SUITE (mem2node);DECLARE_SUITE (maps); +DECLARE_SUITE (ftrace);DECLARE_SUITE (time_utils);DECLARE_SUITE (jit_write_elf);DECLARE_SUITE (api_io);tools/perf/util/Build +1
-0@@ -169,6 +169,7 @@ perf-util-y += list_sort.o perf-util-y += mutex.o perf-util-y += sharded_mutex.o perf-util-y += intel-tpebs.o +perf-util-y += ftrace.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter_cgroup.otools/perf/util/ftrace.c new file +93
-0@@ -0,0 +1,93 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/compiler.h> +#include <linux/kernel.h> +#include <linux/string.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <ctype.h> +#include "util/ftrace.h" + +/** +* ftrace_parse_retaddr - Parse return address comment from trace line +* @str: The trace line containing the comment +* @sym_name: Buffer to store the parsed symbol name +* @sym_len: Length of the @sym_name buffer +* @offset: Pointer to store the parsed offset +* +* Extract the function name and offset from a funcgraph-retaddr comment +* (e.g. "<-load_elf_phdrs+0x6c/0xb0"). +* +* Return: true if successfully parsed, false otherwise. +*/ +bool ftrace_parse_retaddr (const char *str,char *sym_name,size_t sym_len,u64 *offset) +{ +const char *p =strstr (str,"<-" ); +const char *plus; +const char *bracket; +size_t name_len; + +if (!p) +return false; + + p +=2 ; +while (*p ==' ' ) + p++; + +if (*p =='[' ) { + bracket =strchr (p,']' ); +if (bracket) + p = bracket +1 ; +while (*p ==' ' ) + p++; + } + + plus =strchr (p,'+' ); +if (!plus) +return false; + + name_len = plus - p; +if (name_len ==0 || name_len >= sym_len) +return false; + +memcpy (sym_name, p, name_len); + sym_name[name_len] ='\0' ; + + *offset =strtoull (plus +1 ,NULL ,16 ); +return true; +} + +/** +* ftrace_filter_retaddr - Remove return address comment from trace line +* @str: The trace line to modify +* +* Find and strip the funcgraph-retaddr comment from the trace line +* in-place, removing trailing spaces where applicable. +*/ +void ftrace_filter_retaddr (char *str) +{ +char *start =strstr (str,"/* <-" ); +char *end; +char *ret_ptr; + +if (!start) +return ; + + end =strstr (start,"*/" ); +if (!end) +return ; + + ret_ptr =strstr (start," ret=" ); +if (ret_ptr && ret_ptr < end) { +memmove (start +3 , ret_ptr +1 ,strlen (ret_ptr +1 ) +1 ); +return ; + } + + end +=2 ;/* skip end-of-comment delimiter */ + +/* Also backtrack any spaces before comment */ +while (start > str && *(start -1 ) ==' ' ) + start--; + +memmove (start, end,strlen (end) +1 ); +}tools/perf/util/ftrace.h +5
-0@@ -39,6 +39,8 @@ struct perf_ftrace {int graph_verbose;int graph_thresh;int graph_tail; +bool filter_retaddr; +bool filter_retaddr_set; };struct filter_entry {@@ -93,4 +95,7 @@ perf_ftrace__latency_cleanup_bpf(struct perf_ftrace *ftrace __maybe_unused)#endif /* HAVE_BPF_SKEL */ +bool ftrace_parse_retaddr (const char *str,char *sym_name,size_t sym_len,u64 *offset); +void ftrace_filter_retaddr (char *str); +#endif /* __PERF_FTRACE_H__ */ Signature · 1 line
2.55.0
Namhyung Kim
maintainerRe: [PATCH perf-tools-next v9 4/6] perf ftrace: Support display of inlined functions in function graph tracer
In reply to Aaron TomlinQuoted 118 lines · Aaron Tomlin
The Linux kernel's function graph tracer operates at the machine instruction level via compiler instrumentation (-fpatchable-function-entry), recording entry and return events for physical function calls. Consequently, functions inlined by the compiler are invisible in the resulting call graph, as no discrete call or return instructions are emitted for them. While technically expected, this omission frequently obscures the logical execution flow when analysing kernel subsystems heavily reliant upon inlining.
Address this by introducing the --inline option to perf ftrace for the
function_graphtracer.When the kernel is configured with
CONFIG_FUNCTION_GRAPH_RETADDR=y, the funcgraph-retaddr trace option records the caller's return address on each function entry, manifested in the trace stream as a comment (e.g. /* <-irq_work_queue_on+0x81/0xa0 */). By interrogating DWARF debug information from the kernel image (vmlinux) utilising libdw, perf ftrace resolves this return address to its inlined callchain and synthesises the intermediate inlined frames directly into the streamed call graph.Key aspects of this implementation:
1. Inlined frame presentation
Synthesised inlined functions are rendered with an explicit /* (inline) */ annotation at both entry and exit:
# tracer:
function_graph
#
# CPU FUNCTION CALLS
# | | | | |
6)activate_task(){
6)enqueue_task(){
6)enqueue_task_fair(){
6)update_curr_eevdf(){ /* (inline) */
6)update_curr(){
6)update_se(){
6)cgroup_account_cputime(){ /* (inline) */
6)cpuacct_charge();
6) } /*cgroup_account_cputime(inline) */
6) }
6)dl_server_update(){
6)update_curr_dl_se();
6) }
6) } /*update_curr_eevdf(inline) */
6)enqueue_hierarchy(){ /* (inline) */
6)update_curr(){
6)update_se();
6) }
6)enqueue_entity(){
6)account_entity_enqueue(){ /* (inline) */
6)account_llc_enqueue();
6) } /*account_entity_enqueue(inline) */
6) }
6) } /*enqueue_hierarchy(inline) */
6)place_entity(){
6)avg_vruntime();
6) }
6)__enqueue_entity();
6)dl_server_start();
6)hrtick_update(){ /* (inline) */
6)hrtick_active(){ /* (inline) */
6)hrtimer_active();
6) } /*hrtick_active(inline) */
6) } /*hrtick_update(inline) */
6)hrtick_start_fair();
6)psi_enqueue(){ /* (inline) */
6)psi_task_change(){
6)psi_flags_change();
6)psi_group_change(){
6)record_times();
6) }
6) }
6) } /*psi_enqueue(inline) */
6) }
6) }This affords immediate visual clarity to the reader upon entering an inlined scope, without necessitating a search for the closing delimiter.
2. Call graph reconstruction and per-task stack tracking
Maintains a per-task synthetic call stack tracking real and inlined frames across context switches, synthesising entry and exit delimiters to accurately reconstruct the execution hierarchy. Tasks are tracked in a hash table keyed by PID, with per-CPU active bindings updated upon context-switch banners or verbose line records. Inlined frames are structured strictly root-to-leaf and validated against the active stack to prevent un-traced callers from attaching orphan inlines. Bare closing braces without trailing function names (the default when funcgraph-tail is disabled) are correctly matched against preceding frames on the stack. Context column alignment, including task comm-pid delimiters in verbose mode, is preserved when generating prefixes for synthesised inline frames. Furthermore, stack depth saturation guards ensure that inlined frame depth tracking remains clamped, indentation cannot drift even under deep call trees exceeding
FTRACE_MAX_STACK, and overflow depth is automatically recovered upon caller detection.3. Option handling and diagnostics
Adds -k or --vmlinux to designate an explicit debug image, and provides clear diagnostic guidance should --inline be invoked on a kernel lacking
CONFIG_FUNCTION_GRAPH_RETADDR. Automatically enables return address comment filtering unless explicitly requested.4. Automated verification
Expands the unit test suite in
tools/perf/tests/ftrace.cto validate DWARF inline symbol resolution, comment filtering toggles, bare closing braces handling, task context-switch isolation and migration, and stack saturation/overflow recovery.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Aaron Tomlin <[email protected]>
Quoted 7 lines
--- tools/perf/Documentation/perf-ftrace.txt | 17 +- tools/perf/builtin-ftrace.c | 76 +- tools/perf/tests/ftrace.c | 914 ++++++++++++++++++- tools/perf/util/ftrace.c | 1059 +++++++++++++++++++++- tools/perf/util/ftrace.h | 14 + 5 files changed, 2059 insertions(+), 21 deletions(-)
I think it's too big and you can split into pieces like:
1. add the basic task stack management code
2. support context switch
3. support inline functions
4. add test code
Thanks,
Namhyung
Quoted 2,243 lines
tools/perf/Documentation/perf-ftrace.txt +16
-1@@ -134,9 +134,24 @@ OPTIONS for 'perf ftrace trace' - depth=<n> - Set max depth for function graph tracer to follow. - tail - Print function name at the end. +--inline:: + Show inlined functions in function_graph tracer. This requires + kernel support for the funcgraph-retaddr option and a vmlinux with + debug symbols. Inlined functions are displayed in the call graph + hierarchy, and their inlined status is indicated with `/* (inline) */` + on entry and exit, e.g. `func() { /* (inline) */` and `} /* func (inline) */`. + By default, raw return address comments (`/* <-caller+offset */`) + are hidden in the output; specify `--graph-opts retaddr` to show them. + --filter-retaddr:: Filter out function return address comments (`/* <-caller+offset */`) - from function_graph output. + from function_graph output. This is enabled by default when `--inline` + is used; use `--no-filter-retaddr` to disable it. + +-k:: +--vmlinux=:: + Path to the vmlinux file containing debug symbols for resolving + inlined functions. OPTIONS for 'perf ftrace latency'tools/perf/builtin-ftrace.c +60
-16@@ -575,9 +575,14 @@ static int set_tracing_funcgraph_retval(struct perf_ftrace *ftrace)static int set_tracing_funcgraph_retaddr (struct perf_ftrace *ftrace) { -if (ftrace->graph_retaddr) { -if (write_tracing_option_file ("funcgraph-retaddr" ,"1" ) <0 ) +if (ftrace->graph_retaddr || ftrace->use_inline) { +if (write_tracing_option_file ("funcgraph-retaddr" ,"1" ) <0 ) { +if (ftrace->use_inline) { +pr_err ("failed to set funcgraph-retaddr option in tracing\n" ); +pr_err ("Hint: ensure kernel is compiled with CONFIG_FUNCTION_GRAPH_RETADDR=y\n" ); + }return -1 ; + } }return 0 ;@@ -744,6 +749,7 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)int trace_fd;char *buf =NULL ;struct strbuf linebuf =STRBUF_INIT ; +int ret =0 ;struct pollfd pollfd = { .events =POLLIN , };@@ -770,6 +776,9 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace) goto out_reset; } +if (perf_ftrace__setup_inlines (ftrace) <0 ) + goto out_reset; +setup_pager (); buf =malloc (TRACE_BUF_SIZE );@@ -837,14 +846,20 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)break ;for (int i =0 ; i < n; i++) {if (buf[i] =='\n' ) { -if (ftrace->filter_retaddr) -ftrace_filter_retaddr (linebuf.buf); -if (fprintf (stdout,"%s\n" , linebuf.buf) <0 ) +if (ftrace_process_fgraph_line (ftrace, + linebuf.buf, + stdout) <0 ) { + ret =-1 ; + goto out_close_fd; + } +if (ferror (stdout))break ;strbuf_setlen (&linebuf,0 ); }else { -if (strbuf_addch (&linebuf, buf[i]) <0 ) +if (strbuf_addch (&linebuf, buf[i]) <0 ) { + ret =-1 ; goto out_close_fd; + } } }/* flush output since stdout is in full buffering mode due to pager */ @@ -876,22 +891,30 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)break ;for (int i =0 ; i < n; i++) {if (buf[i] =='\n' ) { -if (ftrace->filter_retaddr) -ftrace_filter_retaddr (linebuf.buf); -if (fprintf (stdout,"%s\n" , linebuf.buf) <0 ) +if (ftrace_process_fgraph_line (ftrace, + linebuf.buf, + stdout) <0 ) { + ret =-1 ; + goto out_close_fd; + } +if (ferror (stdout))break ;strbuf_setlen (&linebuf,0 ); }else { -if (strbuf_addch (&linebuf, buf[i]) <0 ) +if (strbuf_addch (&linebuf, buf[i]) <0 ) { + ret =-1 ; goto out_close_fd; + } } } }if (!ferror (stdout) && linebuf.len >0 ) { -if (ftrace->filter_retaddr) -ftrace_filter_retaddr (linebuf.buf); -fprintf (stdout,"%s\n" , linebuf.buf); +if (ftrace_process_fgraph_line (ftrace, linebuf.buf, + stdout) <0 ) { + ret =-1 ; + goto out_close_fd; + }strbuf_setlen (&linebuf,0 ); }@@ -901,9 +924,10 @@ static int __cmd_ftrace(struct perf_ftrace *ftrace)free (buf);strbuf_release (&linebuf); out_reset: +perf_ftrace__cleanup_inlines (ftrace);exit_tracing_instance (); out: -return (done && !workload_exec_errno) ?0 :-1 ; +return (ret ==0 && done && !workload_exec_errno) ?0 :-1 ; } static void make_histogram(struct perf_ftrace *ftrace, int buckets[],@@ -1856,8 +1880,13 @@ int cmd_ftrace(int argc, const char **argv) "Size of per cpu buffer, needs to use a B, K, M or G suffix.", parse_buffer_size),OPT_BOOLEAN (0 ,"inherit" , &ftrace.inherit,"Trace children processes" ), -OPT_BOOLEAN_SET (0 ,"filter-retaddr" , &ftrace.filter_retaddr, &ftrace.filter_retaddr_set, -"Filter out funcgraph return address comments" ), +OPT_BOOLEAN (0 ,"inline" , &ftrace.use_inline, +"Show inlined functions in function_graph tracer" ), +OPT_BOOLEAN_SET (0 ,"filter-retaddr" , &ftrace.filter_retaddr, + &ftrace.filter_retaddr_set, +"Filter out funcgraph return address comments (default with --inline)" ), +OPT_STRING ('k' ,"vmlinux" , &symbol_conf.vmlinux_name,"file" , +"vmlinux pathname" ),OPT_INTEGER ('D' ,"delay" , &ftrace.target.initial_delay,"Number of milliseconds to wait before starting tracing after program start" ), OPT_PARENT(common_options),@@ -1973,6 +2002,21 @@ int cmd_ftrace(int argc, const char **argv)switch (subcmd) {case PERF_FTRACE_TRACE : +select_tracer (&ftrace); + +if (ftrace.use_inline) { +if (strcmp (ftrace.tracer,"function_graph" ) !=0 ) { +pr_err ("Error: --inline option is only supported with function_graph tracer\n" ); + ret = -EINVAL ; + goto out_delete_filters; + } + } +if (!ftrace.filter_retaddr_set) { +if (ftrace.use_inline && !ftrace.graph_retaddr) + ftrace.filter_retaddr = true; +else + ftrace.filter_retaddr = false; + } cmd_func = __cmd_ftrace;break ;case PERF_FTRACE_LATENCY :tools/perf/tests/ftrace.c +913
-1@@ -7,7 +7,13 @@#include <unistd.h> #include "tests.h" #include "debug.h" +#include "dso.h" +#include "machine.h" +#include "map.h" +#include "symbol.h" #include "util/ftrace.h" +#include "evlist.h" +#include "thread_map.h" static int test_parse_retaddr (void ) {@@ -63,6 +69,896 @@ static int test_filter_retaddr(void)return TEST_OK ; } +#define TEST_CHECK (text, cond) \ +do { \ + if (!(cond)) { \ + pr_debug("FAILED %s:%d %s\n", __FILE__, __LINE__, text); \ + ret = TEST_FAIL; \ + goto out_cleanup; \ + } \ +} while (0) + +static int test_inline_processing (void ) +{ +static const char line_entry[] =" 0) | wake_up_new_task() {" ; +static const char line_enqueue[] = +" 0) | enqueue_task() { /* <-wake_up_new_task+0x1d1/0x3e0 */" ; +static const char line_exit_enqueue[] =" 0) + 10.000 us | } /* enqueue_task */" ; +static const char line_exit_task[] =" 0) + 20.000 us | } /* wake_up_new_task */" ; +static const char line_bare_enqueue[] =" 0) + 10.000 us | }" ; +static const char line_bare_task[] =" 0) + 20.000 us | }" ; +static const char line_unmatched[] =" 0) + 5.000 us | } /* unrelated_func */" ; +static const char line_verbose_entry[] = +" 1234.567890 | 0) bash-1234 | .... | | wake_up_new_task() {" ; +static const char line_verbose_enqueue[] = +" 1234.567890 | 0) bash-1234 | .... | | enqueue_task() { /* <-wake_up_new_task+0x1d1/0x3e0 */" ; +static const char line_verbose_exit_enqueue[] = +" 1234.567890 | 0) bash-1234 | .... | 1.234 us | } /* enqueue_task */" ; +static const char line_verbose_exit_task[] = +" 1234.567890 | 0) bash-1234 | .... | 2.567 us | } /* wake_up_new_task */" ; +static const char line_verbose_inline_entry[] = +" 1234.567890 | 0) bash-1234 | .... | | activate_task() { /* (inline) */" ; +static const char line_verbose_inline_exit[] = +" 1234.567890 | 0) bash-1234 | .... | | } /* activate_task (inline) */" ; +struct perf_ftrace ftrace; +char *out_buf =NULL ; +size_t out_len =0 ; +FILE *out; +const char *unmatched_pos; +const char *inline_exit_pos; +const char *vmlinux; +const char *saved_vmlinux = symbol_conf.vmlinux_name; +bool saved_ignore_buildid = symbol_conf.ignore_vmlinux_buildid; +int ret =TEST_OK ; + +memset (&ftrace,0 ,sizeof (ftrace)); + ftrace.use_inline = true; + ftrace.filter_retaddr =1 ;/* Default when --graph-opts retaddr is not specified */ + ftrace.tracer ="function_graph" ; + + vmlinux =vmlinux_path__find (); + +/* If vmlinux is present, test resolving symbols */ +if (vmlinux) { +const char *inlined_names[FTRACE_MAX_INLINES ]; +int num_inlines; +int j; + + symbol_conf.vmlinux_name = vmlinux; + symbol_conf.ignore_vmlinux_buildid = true; + ret =perf_ftrace__setup_inlines (&ftrace); +if (ret <0 || !ftrace.machine) { + ret =TEST_SKIP ; + goto out_cleanup; + } + + num_inlines =ftrace_resolve_inlines (ftrace.machine, +"wake_up_new_task" ,0x1d1 , + inlined_names, +FTRACE_MAX_INLINES ); +if (num_inlines <=0 ||strcmp (inlined_names[0 ],"activate_task" ) !=0 ) { +pr_debug ("vmlinux does not inline activate_task at wake_up_new_task+0x1d1; skipping inline test\n" ); +for (j =0 ; j < num_inlines; j++) +free ((void *)inlined_names[j]); + ret =TEST_SKIP ; + goto out_cleanup; + } +for (j =0 ; j < num_inlines; j++) +free ((void *)inlined_names[j]); + +if (ftrace_resolve_inlines (ftrace.machine,"wake_up_new_task" ,0 , + inlined_names,FTRACE_MAX_INLINES ) !=0 ) { +pr_debug ("ftrace_resolve_inlines with offset 0 should return 0\n" ); + ret =TEST_FAIL ; + goto out_cleanup; + } + + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +/* Feed simulated function graph trace lines */ +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_exit_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_exit_task, out); + +fclose (out); +ftrace_cpu_states__clear (); + +pr_debug ("Processed output:\n%s\n" , out_buf); + +/* Verify activate_task entry with inline hint */ +TEST_CHECK ("contains activate_task entry with inline hint" , +strstr (out_buf,"activate_task() { /* (inline) */" ) !=NULL ); + +/* Verify activate_task exit with inline hint */ +TEST_CHECK ("contains activate_task exit with inline hint" , +strstr (out_buf,"} /* activate_task (inline) */" ) !=NULL ); + +/* Verify return address comment was filtered out by default */ +TEST_CHECK ("retaddr comment filtered out" , +strstr (out_buf,"/* <-wake_up_new_task" ) ==NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Now test with filter_retaddr = 0 (user specified --graph-opts retaddr) */ + ftrace.filter_retaddr =0 ; + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_exit_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_exit_task, out); + +fclose (out); +ftrace_cpu_states__clear (); + +/* Verify return address comment IS preserved when filter_retaddr = 0 */ +TEST_CHECK ("retaddr comment preserved when requested" , +strstr (out_buf,"/* <-wake_up_new_task" ) !=NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test with bare closing braces (funcgraph-tail disabled by default) */ + ftrace.filter_retaddr =1 ; + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_bare_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_bare_task, out); + +fclose (out); + +TEST_CHECK ("bare braces inline entry" , +strstr (out_buf,"activate_task() { /* (inline) */" ) !=NULL ); +TEST_CHECK ("bare braces inline exit" , +strstr (out_buf,"} /* activate_task (inline) */" ) !=NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test bare closing brace matches outer frame when inner callee exit was dropped */ + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +/* line_bare_enqueue is dropped! */ +ftrace_process_fgraph_line (&ftrace, line_bare_task, out); + +fclose (out); + +TEST_CHECK ("dropped callee exit: inlined frame closed" , +strstr (out_buf,"} /* activate_task (inline) */" ) !=NULL ); +TEST_CHECK ("dropped callee exit: outer bare brace emitted" , +strstr (out_buf," }" ) !=NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test that unmatched exit events do not pop active inlined frames */ + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_exit_enqueue, out); +/* Unmatched exit event while activate_task is still active */ +ftrace_process_fgraph_line (&ftrace, line_unmatched, out); +ftrace_process_fgraph_line (&ftrace, line_exit_task, out); + +fclose (out); + + unmatched_pos =strstr (out_buf,"} /* unrelated_func */" ); + inline_exit_pos =strstr (out_buf,"} /* activate_task (inline) */" ); + +TEST_CHECK ("unmatched exit present" , unmatched_pos !=NULL ); +TEST_CHECK ("inline exit present" , inline_exit_pos !=NULL ); +TEST_CHECK ("inline exit not prematurely popped before unmatched exit" , + inline_exit_pos > unmatched_pos); + +free (out_buf); + out_buf =NULL ; + +/* Test recovery when an intermediate real function exit event is dropped */ + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +/* line_exit_enqueue is dropped; outer wake_up_new_task exits directly */ +ftrace_process_fgraph_line (&ftrace, line_exit_task, out); + +fclose (out); + +TEST_CHECK ("inline exit synthesized despite dropped callee exit" , +strstr (out_buf,"} /* activate_task (inline) */" ) !=NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test recovery when intermediate callee exit is dropped before sibling call */ + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +/* line_exit_enqueue dropped; caller makes sibling call without inlines */ +ftrace_process_fgraph_line (&ftrace, +" 0) | check_preempt_curr(); /* <-wake_up_new_task+0x0/0x3e0 */" , + out); +ftrace_process_fgraph_line (&ftrace, line_exit_task, out); + +fclose (out); + +TEST_CHECK ("inline exit synthesized when callee exit dropped before sibling call" , +strstr (out_buf,"} /* activate_task (inline) */" ) !=NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test that callers not on active stack do not synthesise inlines */ + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +/* housekeeping_any_cpu called by untraced ring_buffer_unlock_commit */ +ftrace_process_fgraph_line (&ftrace, +" 0) | housekeeping_any_cpu(); /* <-ring_buffer_unlock_commit+0x1a0/0x1f0 */" , + out); +ftrace_process_fgraph_line (&ftrace, line_exit_task, out); + +fclose (out); + +TEST_CHECK ("untraced caller inlines not synthesised" , +strstr (out_buf,"rb_wakeups" ) ==NULL && +strstr (out_buf,"rb_irq_work_queue" ) ==NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test trailing inlined frame closed on untraced entry (caller_idx < 0) */ + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +/* line_enqueue has inlined activate_task() open; next entry has caller_idx < 0 */ +ftrace_process_fgraph_line (&ftrace, +" 0) | irq_handler() {" , + out); +ftrace_process_fgraph_line (&ftrace, +" 0) | } /* irq_handler */" , + out); +ftrace_process_fgraph_line (&ftrace, line_exit_task, out); + +fclose (out); + +TEST_CHECK ("trailing inline frame closed before untraced call" , +strstr (out_buf,"} /* activate_task (inline) */" ) !=NULL ); +TEST_CHECK ("untraced call not indented inside trailing inline frame" , +strstr (out_buf," irq_handler() {" ) !=NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test dropped exits unwound when new function starts at shallower indent */ + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_entry, out); +ftrace_process_fgraph_line (&ftrace, line_enqueue, out); +/* Exits dropped! New root entry arrives at indent 2 */ +ftrace_process_fgraph_line (&ftrace, +" 0) | schedule() {" , + out); +ftrace_process_fgraph_line (&ftrace, +" 0) | } /* schedule */" , + out); + +fclose (out); + +TEST_CHECK ("stale inlines closed when new root arrives" , +strstr (out_buf,"} /* activate_task (inline) */" ) !=NULL ); +TEST_CHECK ("new root entry printed without stale indentation" , +strstr (out_buf," schedule() {" ) !=NULL ); + +free (out_buf); + out_buf =NULL ; + +/* Test verbose mode prefix formatting for synthesised inlines */ + ftrace.graph_verbose = true; + out =open_memstream (&out_buf, &out_len); +if (!out) { + ret =TEST_FAIL ; + goto out_cleanup; + } + +ftrace_process_fgraph_line (&ftrace, line_verbose_entry, out); +ftrace_process_fgraph_line (&ftrace, line_verbose_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_verbose_exit_enqueue, out); +ftrace_process_fgraph_line (&ftrace, line_verbose_exit_task, out); + +fclose (out); +ftrace_cpu_states__clear (); + +TEST_CHECK ("verbose inline entry preserves comm-pid" , +strstr (out_buf, line_verbose_inline_entry) !=NULL ); +TEST_CHECK ("verbose inline exit preserves comm-pid" , +strstr (out_buf, line_verbose_inline_exit) !=NULL ); + +free (out_buf); + out_buf =NULL ; + ftrace.graph_verbose = false; + } + +out_cleanup: +free (out_buf); +perf_ftrace__cleanup_inlines (&ftrace); + symbol_conf.vmlinux_name = saved_vmlinux; + symbol_conf.ignore_vmlinux_buildid = saved_ignore_buildid; +return ret; +} +#undef TEST_CHECK + +static void feed_fgraph_entry (struct perf_ftrace *ftrace,int cpu,int depth, +const char *name,const char *retaddr,FILE *out) +{ +char line[1024 ]; +int indent =2 +2 * depth; + +if (retaddr) { +snprintf (line,sizeof (line)," %d) |%*s%s() { /* <-%s */" , + cpu, indent,"" , name, retaddr); + }else { +snprintf (line,sizeof (line)," %d) |%*s%s() {" , + cpu, indent,"" , name); + } +ftrace_process_fgraph_line (ftrace, line, out); +} + +static void feed_fgraph_exit (struct perf_ftrace *ftrace,int cpu,int depth, +const char *name,FILE *out) +{ +char line[1024 ]; +int indent =2 +2 * depth; + +if (name) { +snprintf (line,sizeof (line)," %d) 1.000 us |%*s} /* %s */" , + cpu, indent,"" , name); + }else { +snprintf (line,sizeof (line)," %d) 1.000 us |%*s}" , + cpu, indent,"" ); + } +ftrace_process_fgraph_line (ftrace, line, out); +} + +static int test_stack_saturation (void ) +{ +struct perf_ftrace ftrace; +char *out_buf =NULL ; +size_t out_len =0 ; +FILE *out; +const char *vmlinux; +int i; + +memset (&ftrace,0 ,sizeof (ftrace)); + ftrace.use_inline = true; +ftrace_cpu_states__clear (); + + out =open_memstream (&out_buf, &out_len); +if (!out) +return TEST_FAIL ; + +/* Push root frame at depth 0 */ +feed_fgraph_entry (&ftrace,0 ,0 ,"root_func" ,NULL , out); + +/* Push 255 more frames to fill FTRACE_MAX_STACK (256) */ +for (i =1 ; i <256 ; i++) +feed_fgraph_entry (&ftrace,0 , i,"sub_func" ,NULL , out); + +/* Push 44 overflow frames beyond FTRACE_MAX_STACK */ +for (i =0 ; i <44 ; i++) +feed_fgraph_entry (&ftrace,0 ,256 + i,"overflow_func" ,NULL , out); + +/* Pop the 44 overflow frames with bare closing braces */ +for (i =43 ; i >=0 ; i--) +feed_fgraph_exit (&ftrace,0 ,256 + i,NULL , out); + +/* Pop 255 tracked sub_func frames */ +for (i =255 ; i >=1 ; i--) +feed_fgraph_exit (&ftrace,0 , i,NULL , out); + +/* Pop root frame - must cleanly match root_func on stack, not popped prematurely */ +feed_fgraph_exit (&ftrace,0 ,0 ,"root_func" , out); + +/* Feed one more function entry - it must have base indentation (no indentation drift) */ +feed_fgraph_entry (&ftrace,0 ,0 ,"base_func" ,NULL , out); + +fclose (out); +ftrace_cpu_states__clear (); + +/* Verify base_func starts at standard 2 spaces indentation without drift */ +if (!strstr (out_buf,"| base_func() {" )) { +pr_debug ("FAILED %s:%d no indentation drift after stack saturation\n" , + __FILE__, __LINE__); +free (out_buf); +return TEST_FAIL ; + } + +free (out_buf); + out_buf =NULL ; + +/* Test overflow recovery when events are dropped */ + out =open_memstream (&out_buf, &out_len); +if (!out) +return TEST_FAIL ; + +feed_fgraph_entry (&ftrace,0 ,0 ,"root_func" ,NULL , out); +for (i =1 ; i <256 ; i++) +feed_fgraph_entry (&ftrace,0 , i,"sub_func" ,NULL , out); +for (i =0 ; i <44 ; i++) +feed_fgraph_entry (&ftrace,0 ,256 + i,"overflow_func" ,NULL , out); + +/* Dropped events: root_func directly calls recovery_func */ +feed_fgraph_entry (&ftrace,0 ,1 ,"recovery_func" ,"root_func+0x20" , out); +feed_fgraph_exit (&ftrace,0 ,1 ,NULL , out); +feed_fgraph_exit (&ftrace,0 ,0 ,"root_func" , out); + +fclose (out); +ftrace_cpu_states__clear (); + +if (!strstr (out_buf,"recovery_func() {" )) { +pr_debug ("FAILED %s:%d recovery_func missing after overflow recovery\n" , + __FILE__, __LINE__); +free (out_buf); +return TEST_FAIL ; + } +if (!strstr (out_buf,"} /* root_func */" )) { +pr_debug ("FAILED %s:%d root_func exit missing after overflow recovery\n" , + __FILE__, __LINE__); +free (out_buf); +return TEST_FAIL ; + } + +free (out_buf); + out_buf =NULL ; + +/* Test that bare braces during overflow do not prematurely pop tracked frames */ + out =open_memstream (&out_buf, &out_len); +if (!out) +return TEST_FAIL ; + +feed_fgraph_entry (&ftrace,0 ,0 ,"root_func" ,NULL , out); +for (i =1 ; i <256 ; i++) +feed_fgraph_entry (&ftrace,0 , i,"sub_func" ,NULL , out); +for (i =0 ; i <10 ; i++) +feed_fgraph_entry (&ftrace,0 ,256 + i,"overflow_func" ,NULL , out); + +/* Pop 10 overflow frames with bare braces */ +for (i =9 ; i >=0 ; i--) +feed_fgraph_exit (&ftrace,0 ,256 + i,NULL , out); + +/* Pop 255 sub_func frames with bare braces */ +for (i =255 ; i >=1 ; i--) +feed_fgraph_exit (&ftrace,0 , i,NULL , out); + +/* Root frame must still be on the stack to match caller */ +feed_fgraph_entry (&ftrace,0 ,1 ,"child_func" ,"root_func+0x10" , out); +feed_fgraph_exit (&ftrace,0 ,1 ,NULL , out); +feed_fgraph_exit (&ftrace,0 ,0 ,"root_func" , out); + +fclose (out); +ftrace_cpu_states__clear (); + +if (!strstr (out_buf,"child_func() {" )) { +pr_debug ("FAILED %s:%d child_func missing after bare braces in overflow\n" , + __FILE__, __LINE__); +free (out_buf); +return TEST_FAIL ; + } +if (!strstr (out_buf,"} /* root_func */" )) { +pr_debug ("FAILED %s:%d root_func exit missing after bare braces in overflow\n" , + __FILE__, __LINE__); +free (out_buf); +return TEST_FAIL ; + } + +free (out_buf); + out_buf =NULL ; + +/* Test recursive caller lookup skips stale inner invocation */ + out =open_memstream (&out_buf, &out_len); +if (!out) +return TEST_FAIL ; + +/* Outer recursive invocation at indent 2 */ +ftrace_process_fgraph_line (&ftrace," 0) | rec_func() {" , out); +/* Inner recursive invocation at indent 4 */ +ftrace_process_fgraph_line (&ftrace, +" 0) | rec_func() { /* <-rec_func+0x10 */" , + out); +/* Inner invocation exit is dropped by ring buffer */ +/* Outer invocation calls sibling_func at indent 4 */ +ftrace_process_fgraph_line (&ftrace, +" 0) 1.000 us | sibling_func(); /* <-rec_func+0x20 */" , + out); +ftrace_process_fgraph_line (&ftrace," 0) 2.000 us | } /* rec_func */" , out); + +fclose (out); +ftrace_cpu_states__clear (); + +if (!strstr (out_buf,"sibling_func();" )) { +pr_debug ("FAILED %s:%d sibling_func missing under recursive caller\n" , + __FILE__, __LINE__); +free (out_buf); +return TEST_FAIL ; + } +if (!strstr (out_buf,"} /* rec_func */" )) { +pr_debug ("FAILED %s:%d rec_func exit missing after dropped inner exit\n" , + __FILE__, __LINE__); +free (out_buf); +return TEST_FAIL ; + } +free (out_buf); + out_buf =NULL ; + +/* Test top inlined frame when cs->overflow_depth > 0 does not misclassify exit */ + vmlinux =vmlinux_path__find (); +if (vmlinux) { +const char *saved_vmlinux = symbol_conf.vmlinux_name; +bool saved_ignore_buildid = symbol_conf.ignore_vmlinux_buildid; +const char *inlined_names[FTRACE_MAX_INLINES ]; +int num_inlines; +int j; + + symbol_conf.vmlinux_name = vmlinux; + symbol_conf.ignore_vmlinux_buildid = true; +if (perf_ftrace__setup_inlines (&ftrace) ==0 && ftrace.machine) { + num_inlines =ftrace_resolve_inlines (ftrace.machine, +"wake_up_new_task" ,0x1d1 , + inlined_names, +FTRACE_MAX_INLINES ); +if (num_inlines >0 && !strcmp (inlined_names[0 ],"activate_task" )) { + out =open_memstream (&out_buf, &out_len); +if (!out) { +for (j =0 ; j < num_inlines; j++) +free ((void *)inlined_names[j]); +perf_ftrace__cleanup_inlines (&ftrace); + symbol_conf.vmlinux_name = saved_vmlinux; + symbol_conf.ignore_vmlinux_buildid = saved_ignore_buildid; +return TEST_FAIL ; + } + +/* Fill stack up to 254 with tracked frames */ +feed_fgraph_entry (&ftrace,0 ,0 ,"root_func" ,NULL , out); +for (i =1 ; i <254 ; i++) +feed_fgraph_entry (&ftrace,0 , i,"sub_func" ,NULL , out); + +/* Frame 254 is wake_up_new_task */ +feed_fgraph_entry (&ftrace,0 ,254 ,"wake_up_new_task" ,NULL , out); + +/* +* Frame 255: enqueue_task resolves inline activate_task. +* Inlined frame activate_task is pushed at stack[255] +* (cs->depth = 256). Real frame enqueue_task overflows +* into cs->overflow_depth = 1. +*/ +feed_fgraph_entry (&ftrace,0 ,255 ,"enqueue_task" , +"wake_up_new_task+0x1d1/0x3e0" , out); + +/* +* Nested child call inside overflow: does NOT pop the +* inlined frame! +*/ +feed_fgraph_entry (&ftrace,0 ,256 ,"child_overflow" , +"enqueue_task+0x10" , out); + +/* Exit child_overflow (overflow_depth decrements to 1) */ +feed_fgraph_exit (&ftrace,0 ,256 ,NULL , out); + +/* Exit enqueue_task (overflow_depth decrements to 0) */ +feed_fgraph_exit (&ftrace,0 ,255 ,NULL , out); + +/* Exit wake_up_new_task: synthesises inline exit */ +feed_fgraph_exit (&ftrace,0 ,254 ,NULL , out); + +/* Pop remaining sub_func frames and root_func */ +for (i =253 ; i >=1 ; i--) +feed_fgraph_exit (&ftrace,0 , i,NULL , out); +feed_fgraph_exit (&ftrace,0 ,0 ,"root_func" , out); + +/* Feed base frame to check for indentation drift */ +feed_fgraph_entry (&ftrace,0 ,0 ,"drift_check" ,NULL , out); + +fclose (out); +ftrace_cpu_states__clear (); + +if (!strstr (out_buf,"activate_task() { /* (inline) */" ) || + !strstr (out_buf,"} /* activate_task (inline) */" ) || + !strstr (out_buf,"| drift_check() {" )) { +pr_debug ("FAILED %s:%d top inlined frame saturation failed\n" , + __FILE__, __LINE__); +free (out_buf); +for (j =0 ; j < num_inlines; j++) +free ((void *)inlined_names[j]); +perf_ftrace__cleanup_inlines (&ftrace); + symbol_conf.vmlinux_name = saved_vmlinux; + symbol_conf.ignore_vmlinux_buildid = saved_ignore_buildid; +return TEST_FAIL ; + } + +free (out_buf); + out_buf =NULL ; + } +for (j =0 ; j < num_inlines; j++) +free ((void *)inlined_names[j]); +perf_ftrace__cleanup_inlines (&ftrace); + } + symbol_conf.vmlinux_name = saved_vmlinux; + symbol_conf.ignore_vmlinux_buildid = saved_ignore_buildid; + } + +free (out_buf); +return TEST_OK ; +} + +static int test_column_layouts (void ) +{ +static const struct { +const char *desc; +bool verbose; +const char *in; +const char *expected; + } cases[] = { + { +"default layout" , false, +" 7) | wake_up_new_task() { /* <-kernel_clone+0x160/0x510 */" , +" 7) | wake_up_new_task() {\n" , + }, + { +"verbose layout" , true, +" 1234.567890 | 7) bash-1234 | .... | | wake_up_new_task() { /* <-kernel_clone+0x160/0x510 */" , +" 1234.567890 | 7) bash-1234 | .... | | wake_up_new_task() {\n" , + }, + }; +struct perf_ftrace ftrace; +char *out_buf =NULL ; +size_t out_len =0 ; +FILE *out; +size_t i; + +for (i =0 ; i <ARRAY_SIZE (cases); i++) { +memset (&ftrace,0 ,sizeof (ftrace)); + ftrace.use_inline = true; + ftrace.filter_retaddr = true; + ftrace.graph_verbose = cases[i].verbose; + + out =open_memstream (&out_buf, &out_len); +if (!out) +return TEST_FAIL ; + +ftrace_process_fgraph_line (&ftrace, cases[i].in, out); +fclose (out); +ftrace_cpu_states__clear (); + +if (strcmp (out_buf, cases[i].expected)) { +pr_debug ("%s: expected '%s', got '%s'\n" , + cases[i].desc, cases[i].expected, out_buf); +free (out_buf); +return TEST_FAIL ; + } +free (out_buf); + out_buf =NULL ; + } + +return TEST_OK ; +} + +static int test_context_switch_handling (void ) +{ +struct perf_ftrace ftrace; +struct perf_thread_map *threads; +char *out_buf =NULL ; +size_t out_len =0 ; +FILE *out; +int i; + +memset (&ftrace,0 ,sizeof (ftrace)); + ftrace.use_inline = true; +ftrace_cpu_states__clear (); + + out =open_memstream (&out_buf, &out_len); +if (!out) +return TEST_FAIL ; + +/* +* Initial Task 1 (PID 100) starts running on CPU 0 before any banner +* has been emitted on that CPU (default kernel ftrace behaviour). +*/ +ftrace_process_fgraph_line (&ftrace," 0) | func1() {" , out); + +/* First context switch on CPU 0: Task 1 switches to <idle>-0 */ +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 0) task1-100 => <idle>-0 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); + +/* Idle task executes on CPU 0 - should NOT inherit func1's stack */ +ftrace_process_fgraph_line (&ftrace," 0) | default_idle() {" , out); +ftrace_process_fgraph_line (&ftrace," 0) | } /* default_idle */" , out); + +/* Context switch to Task 2 (PID 200) on CPU 0 */ +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 0) <idle>-0 => task2-200 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); + +/* Task 2 executes func2 - should NOT inherit func1's indentation */ +ftrace_process_fgraph_line (&ftrace," 0) | func2() {" , out); +ftrace_process_fgraph_line (&ftrace," 0) | } /* func2 */" , out); + +/* Task 1 migrates and resumes on CPU 1 */ +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 1) <idle>-0 => task1-100 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); + +/* Task 1 exits func1 on CPU 1 - should cleanly match func1 from its preserved stack */ +ftrace_process_fgraph_line (&ftrace," 1) | } /* func1 */" , out); + +/* +* Test delayed context switch banner migration where a task state +* allocated on CPU 3 is rekeyed from CPU 2. +*/ +ftrace_process_fgraph_line (&ftrace," 2) | func3() {" , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 3) <idle>-0 => task3-300 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 2) task3-300 => <idle>-0 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 3) | func4() {" , out); +ftrace_process_fgraph_line (&ftrace," 3) | } /* func4 */" , out); +ftrace_process_fgraph_line (&ftrace," 3) | } /* func3 */" , out); + +/* +* Test delayed context switch banner migration where CPU 3 enters func6() +* BEFORE CPU 2 emits its delayed context switch banner. ts from CPU 2 +* must be merged underneath existing on CPU 3. +*/ +ftrace_process_fgraph_line (&ftrace," 2) | func5() {" , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 3) <idle>-0 => task4-400 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 3) | func6() {" , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 2) task4-400 => <idle>-0 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 3) | } /* func6 */" , out); +ftrace_process_fgraph_line (&ftrace," 3) | } /* func5 */" , out); + +/* +* Test single-task profiling across CPU migration where context switch +* banners are suppressed by the kernel (verif_pid prev_pid == -1). +*/ + ftrace.evlist =evlist__new (); +if (ftrace.evlist) { + threads =thread_map__new_by_tid (500 ); +perf_evlist__set_maps (evlist__core (ftrace.evlist),NULL , threads); +perf_thread_map__put (threads); + } + +ftrace_cpu_states__clear (); +ftrace_process_fgraph_line (&ftrace," 0) | func_a() {" , out); +ftrace_process_fgraph_line (&ftrace," 1) | func_b() {" , out); +ftrace_process_fgraph_line (&ftrace," 1) | } /* func_b */" , out); +ftrace_process_fgraph_line (&ftrace," 0) | } /* func_a */" , out); + +evlist__put (ftrace.evlist); + ftrace.evlist =NULL ; + +fclose (out); +ftrace_cpu_states__clear (); + +/* Verify default_idle in Idle task started at base indentation */ +TEST_ASSERT_VAL ("idle task does not inherit initial task call stack" , +strstr (out_buf,"| default_idle() {" ) !=NULL ); + +/* Verify func2 in Task 2 started at base indentation "| func2() {" */ +TEST_ASSERT_VAL ("task2 does not inherit task1 call stack" , +strstr (out_buf,"| func2() {" ) !=NULL ); + +/* Verify task1 exit on CPU 1 matches cleanly */ +TEST_ASSERT_VAL ("task1 preserved stack across migration" , +strstr (out_buf,"1) | } /* func1 */" ) !=NULL ); + +/* Verify task3 preserved stack across delayed context switch banner */ +TEST_ASSERT_VAL ("task3 preserved stack across delayed banner" , +strstr (out_buf,"3) | } /* func3 */" ) !=NULL ); + +/* Verify task4 merged outer stack when entered before delayed banner */ +TEST_ASSERT_VAL ("task4 merged outer stack when entered before delayed banner" , +strstr (out_buf,"3) | } /* func5 */" ) !=NULL ); + +/* Verify single-task stack tracked across CPUs without banners */ +TEST_ASSERT_VAL ("single-task stack tracked across CPUs without banners" , +strstr (out_buf,"0) | } /* func_a */" ) !=NULL ); + +/* +* Test migration where merged stack depth exceeds FTRACE_MAX_STACK +* does not cause indentation drift or corruption. +* CPU 2 starts without a context switch banner, accumulating under +* FTRACE_UNKNOWN_PID(2). +*/ +free (out_buf); + out_buf =NULL ; + out =open_memstream (&out_buf, &out_len); +if (!out) +return TEST_FAIL ; + +for (i =0 ; i <200 ; i++) +feed_fgraph_entry (&ftrace,2 , i,"deep_func" ,NULL , out); + +/* Migrate to CPU 3, enter more frames */ +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 3) <idle>-0 => task5-500 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +for (i =0 ; i <100 ; i++) +feed_fgraph_entry (&ftrace,3 ,200 + i,"more_func" ,NULL , out); + +/* Delayed context switch banner from CPU 2 rekeys and merges stacks (> 256) */ +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); +ftrace_process_fgraph_line (&ftrace," 2) task5-500 => <idle>-0 " , out); +ftrace_process_fgraph_line (&ftrace," ------------------------------------------" , out); + +/* Unwind all frames on CPU 3 (44 in overflow_depth + 256 on stack = 300) */ +for (i =299 ; i >=0 ; i--) +feed_fgraph_exit (&ftrace,3 , i,NULL , out); +feed_fgraph_entry (&ftrace,3 ,0 ,"after_merge" ,NULL , out); + +fclose (out); +ftrace_cpu_states__clear (); + +TEST_ASSERT_VAL ("no indentation drift after saturated stack rekey" , +strstr (out_buf,"| after_merge() {" ) !=NULL ); + +free (out_buf); +return TEST_OK ; +} +static int test__ftrace (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) {int ret;@@ -75,7 +971,23 @@ static int test__ftrace(struct test_suite *test __maybe_unused, int subtest __maif (ret !=TEST_OK )return ret; + ret =test_stack_saturation (); +if (ret !=TEST_OK ) +return ret; + + ret =test_context_switch_handling (); +if (ret !=TEST_OK ) +return ret; + + ret =test_column_layouts (); +if (ret !=TEST_OK ) +return ret; + + ret =test_inline_processing (); +if (ret !=TEST_OK ) +return ret; +return TEST_OK ; } -DEFINE_SUITE ("Ftrace return address processing" , ftrace); +DEFINE_SUITE ("Ftrace inline processing" , ftrace);tools/perf/util/ftrace.c +1056
-3@@ -1,12 +1,346 @@// SPDX-License-Identifier: GPL-2.0 -#include <linux/compiler.h> -#include <linux/kernel.h> -#include <linux/string.h> +/* +* ftrace.c - Ftrace utilities and inlined function processing +* +* Copyright (C) 2026 Aaron Tomlin <[email protected]> +*/ #include <stdio.h> #include <stdlib.h> +#include <errno.h> #include <string.h> #include <ctype.h> +#include <stdbool.h> +#include <linux/kernel.h> +#include <linux/zalloc.h> +#include <linux/hash.h> +#include <linux/list.h> +#include <linux/string.h> +#include "debug.h" +#include "dso.h" +#include "evlist.h" +#include "machine.h" +#include "map.h" +#include "srcline.h" +#include "symbol.h" #include "util/ftrace.h" +#include <perf/threadmap.h> + +#define FTRACE_MAX_CPUS 4096 +#define FTRACE_MAX_STACK 256 +#define FTRACE_PID_HASH_BITS 10 +#define FTRACE_PID_HASH_SIZE (1 << FTRACE_PID_HASH_BITS) + +#define FTRACE_IDLE_PID (cpu) ((pid_t)(-1 - (cpu))) +#define FTRACE_UNKNOWN_PID (cpu) ((pid_t)(-1 - FTRACE_MAX_CPUS - (cpu))) + +struct ftrace_stack_frame { +char *name; +bool inlined; +int indent; +int raw_indent; +}; + +struct ftrace_task_state { +struct hlist_node node; +pid_t pid; +struct ftrace_stack_frame stack[FTRACE_MAX_STACK ]; +int depth; +int inlined_depth; +int overflow_depth; +}; + +#define ftrace_cpu_state ftrace_task_state + +static struct hlist_head ftrace_task_hash[FTRACE_PID_HASH_SIZE ]; +static struct ftrace_task_state *ftrace_cpu_tasks[FTRACE_MAX_CPUS ]; + +/** +* ftrace_cpu_states__clear - Free allocated CPU and task stack states +* +* Free all memory associated with the per-task stack tracking states +* used for inlined function resolution, including the strings +* allocated for stack frames. +*/ +void ftrace_cpu_states__clear (void ) +{ +struct ftrace_task_state *ts; +struct hlist_node *tmp; +int i, j; + +for (i =0 ; i <FTRACE_PID_HASH_SIZE ; i++) { +hlist_for_each_entry_safe (ts, tmp, &ftrace_task_hash[i], node) { +for (j =0 ; j < ts->depth; j++) +free (ts->stack[j].name); +hlist_del_init (&ts->node); +free (ts); + } + } + +memset (ftrace_cpu_tasks,0 ,sizeof (ftrace_cpu_tasks)); +} + +/** +* find_task_state - Look up call stack state for a task by PID +* @pid: Process ID of the task +* +* Return: A pointer to the task's stack state, or NULL if not found. +*/ +static struct ftrace_task_state *find_task_state (pid_t pid) +{ +struct ftrace_task_state *ts; +struct hlist_head *head; +u32 key =hash_32 ((u32 )pid,FTRACE_PID_HASH_BITS ); + + head = &ftrace_task_hash[key]; +hlist_for_each_entry (ts, head, node) { +if (ts->pid == pid) +return ts; + } + +return NULL ; +} + +/** +* get_task_state - Get or allocate stack state for a given task PID +* @pid: Process ID of the task +* +* Return: A pointer to the task's stack state, or NULL on allocation failure. +*/ +static struct ftrace_task_state *get_task_state (pid_t pid) +{ +struct ftrace_task_state *ts =find_task_state (pid); +struct hlist_head *head; +u32 key; + +if (ts) +return ts; + + ts =zalloc (sizeof (*ts)); +if (!ts) +return NULL ; + + ts->pid = pid; + key =hash_32 ((u32 )pid,FTRACE_PID_HASH_BITS ); + head = &ftrace_task_hash[key]; +hlist_add_head (&ts->node, head); +return ts; +} + +/** +* ftrace_task_state__rekey - Reassign a task state to a new PID +* @ts: The task state to re-key +* @new_pid: The new process ID to associate with the state +* +* Rehash @ts under @new_pid. If a task state for @new_pid already exists, +* merge or replace it and update active CPU references accordingly. +* +* Return: A pointer to the active task state associated with @new_pid. +*/ +static struct ftrace_task_state *ftrace_task_state__rekey (struct ftrace_task_state *ts, +pid_t new_pid) +{ +struct ftrace_task_state *existing; +u32 key; +int keep_existing; +int j, c; + +if (ts->pid == new_pid) +return ts; + +hlist_del_init (&ts->node); + + existing =find_task_state (new_pid); +if (existing) { +if (existing->depth ==0 ) { +for (c =0 ; c <FTRACE_MAX_CPUS ; c++) { +if (ftrace_cpu_tasks[c] == existing) + ftrace_cpu_tasks[c] = ts; + } +hlist_del_init (&existing->node); +free (existing); + }else { +for (c =0 ; c <FTRACE_MAX_CPUS ; c++) { +if (ftrace_cpu_tasks[c] == ts) + ftrace_cpu_tasks[c] = existing; + } + +if (ts->depth >=FTRACE_MAX_STACK ) { +for (j =0 ; j < existing->depth; j++) { +if (!existing->stack[j].inlined) + existing->overflow_depth++; +free (existing->stack[j].name); + } + existing->inlined_depth =0 ; +for (j =0 ; j <FTRACE_MAX_STACK ; j++) { +if (ts->stack[j].inlined) + existing->inlined_depth++; + } +memcpy (&existing->stack[0 ], &ts->stack[0 ], +FTRACE_MAX_STACK *sizeof (existing->stack[0 ])); + existing->depth =FTRACE_MAX_STACK ; + existing->overflow_depth += ts->overflow_depth; + }else if (existing->depth + ts->depth >FTRACE_MAX_STACK ) { + keep_existing =FTRACE_MAX_STACK - ts->depth; + +for (j = keep_existing; j < existing->depth; j++) { +if (!existing->stack[j].inlined) + existing->overflow_depth++; +else + existing->inlined_depth--; +free (existing->stack[j].name); + } +memmove (&existing->stack[ts->depth], &existing->stack[0 ], + keep_existing *sizeof (existing->stack[0 ])); +memcpy (&existing->stack[0 ], &ts->stack[0 ], + ts->depth *sizeof (existing->stack[0 ])); + existing->depth =FTRACE_MAX_STACK ; + existing->inlined_depth += ts->inlined_depth; + existing->overflow_depth += ts->overflow_depth; + }else { +memmove (&existing->stack[ts->depth], &existing->stack[0 ], + existing->depth *sizeof (existing->stack[0 ])); +memcpy (&existing->stack[0 ], &ts->stack[0 ], + ts->depth *sizeof (existing->stack[0 ])); + existing->depth += ts->depth; + existing->inlined_depth += ts->inlined_depth; + existing->overflow_depth += ts->overflow_depth; + } + +free (ts); +return existing; + } + } + + ts->pid = new_pid; + key =hash_32 ((u32 )new_pid,FTRACE_PID_HASH_BITS ); +hlist_add_head (&ts->node, &ftrace_task_hash[key]); +return ts; +} + +static pid_t ftrace__get_single_pid (struct perf_ftrace *ftrace) +{ +struct perf_thread_map *threads; +pid_t pid; + +if (!ftrace || !ftrace->evlist || ftrace->inherit) +return -1 ; + + threads =evlist__core (ftrace->evlist)->threads; +if (!threads ||perf_thread_map__nr (threads) !=1 ) +return -1 ; + + pid =perf_thread_map__pid (threads,0 ); +return (pid >0 ) ? pid :-1 ; +} + +/** +* get_cpu_state - Get or allocate active task stack state for a given CPU +* @ftrace: Perf ftrace context +* @cpu: The CPU number +* +* Return: A pointer to the CPU's active task stack state, or NULL on error or +* out-of-bounds CPU index. +*/ +static struct ftrace_task_state *get_cpu_state (struct perf_ftrace *ftrace,int cpu) +{ +pid_t single_pid; + +if (cpu <0 || cpu >=FTRACE_MAX_CPUS ) +return NULL ; + +if (!ftrace_cpu_tasks[cpu]) { + single_pid =ftrace__get_single_pid (ftrace); +if (single_pid >0 ) + ftrace_cpu_tasks[cpu] =get_task_state (single_pid); +else + ftrace_cpu_tasks[cpu] =get_task_state (FTRACE_UNKNOWN_PID (cpu)); + } + +return ftrace_cpu_tasks[cpu]; +} + +/** +* parse_context_switch - Parse context switch banner and update CPU's task +* @line: The trace line containing the banner +* +* Check if @line is a context switch banner (e.g. " 0) <idle>-0 => migrati-19"). +* If so, extract the CPU number and both the outgoing and incoming task PIDs, +* re-key any initial unknown task state on this CPU to the outgoing PID, and +* update the active task mapping for that CPU to the incoming PID. +* +* Return: true if @line was a context switch banner, false otherwise. +*/ +static bool parse_context_switch (const char *line) +{ +const char *arrow =strstr (line," => " ); +const char *p; +const char *next; +const char *hyphen; +const char *h; +char *endptr; +char *end_pid; +long cpu_val; +long prev_pid; +long next_pid; +pid_t real_prev; +pid_t real_next; + +if (!arrow) +return false; + +/* Find CPU number before ')' */ + p =skip_spaces (line); + cpu_val =strtol (p, &endptr,10 ); +if (endptr == p || *endptr !=')' ) +return false; + +if (cpu_val <0 || cpu_val >=FTRACE_MAX_CPUS ) +return false; + +/* +* Parse outgoing task PID before " => " (" CPU) comm-pid => ..."). +* Scan backwards from arrow to find the hyphen before the numeric PID. +*/ + h = arrow -1 ; +while (h > endptr && *h ==' ' ) + h--; +while (h > endptr && *h !='-' ) + h--; +if (h <= endptr || *h !='-' ) +return false; + + prev_pid =strtol (h +1 , &end_pid,10 ); +if (end_pid == h +1 ) +return false; + +/* Parse target task PID after " => " (" => comm-pid") */ + next =skip_spaces (arrow +4 ); + hyphen =strrchr (next,'-' ); +if (!hyphen) +return false; + + next_pid =strtol (hyphen +1 , &end_pid,10 ); +if (end_pid == hyphen +1 ) +return false; + + real_prev = (prev_pid ==0 ) ?FTRACE_IDLE_PID (cpu_val) : (pid_t )prev_pid; + real_next = (next_pid ==0 ) ?FTRACE_IDLE_PID (cpu_val) : (pid_t )next_pid; + +/* +* If no context switch was seen previously on this CPU, its initial +* pre-banner trace records were accumulated under an unknown synthetic +* PID. Re-key that state to real_prev so the initial task's stack and +* inlined frames are preserved when it schedules back in. +*/ +if (ftrace_cpu_tasks[cpu_val] && + ftrace_cpu_tasks[cpu_val]->pid ==FTRACE_UNKNOWN_PID (cpu_val)) { + ftrace_cpu_tasks[cpu_val] = +ftrace_task_state__rekey (ftrace_cpu_tasks[cpu_val], real_prev); + } + + ftrace_cpu_tasks[cpu_val] =get_task_state (real_next); +return true; +}/** * ftrace_parse_retaddr - Parse return address comment from trace line @@ -91,3 +425,722 @@ void ftrace_filter_retaddr(char *str)memmove (start, end,strlen (end) +1 ); } + +/** +* ftrace_resolve_inlines - Resolve inlined functions for a return address +* @machine: Perf machine context +* @sym_name: Name of the caller function +* @offset: Instruction offset within the caller function +* @inlined_names: Array to store the resolved inline function names +* @max_inlines: Maximum number of inline frames to resolve +* +* Use DWARF debug information from the kernel image to resolve a caller's +* return address to its inlined callchain. +* +* Return: The number of inline functions successfully resolved. +*/ +int ftrace_resolve_inlines (struct machine *machine, +const char *sym_name,u64 offset, +const char **inlined_names,int max_inlines) +{ +struct map *map =NULL ; +struct symbol *sym; +struct dso *dso; +struct inline_node *node; +struct inline_list *ilist; +u64 ip, addr; +int count =0 ; +int j; + +if (!machine || !offset) +return 0 ; + + sym =machine__find_kernel_symbol_by_name (machine, sym_name, &map); +if (!sym || !map) +return 0 ; + + dso =map__dso (map); +if (!dso) { +map__put (map); +return 0 ; + } + + ip = sym->start + offset -1 ; + addr =map__rip_2objdump (map, ip); + + node =inlines__tree_find (dso__inlined_nodes (dso), addr); +if (!node) { + node =dso__parse_addr_inlines (dso, addr, sym); +if (node) +inlines__tree_insert (dso__inlined_nodes (dso), node); + } + +if (!node) { +map__put (map); +return 0 ; + } + +list_for_each_entry_reverse (ilist, &node->val, list) { +if (ilist->symbol &&symbol__inlined (ilist->symbol)) { +if (count < max_inlines) { +char *name =strdup (ilist->symbol->name); + +if (!name) { +for (j =0 ; j < count; j++) +free ((void *)inlined_names[j]); +map__put (map); +return -ENOMEM ; + } + inlined_names[count++] = name; + } + } + } + +map__put (map); +return count; +} + +/** +* extract_entry_func_name - Extract the function name from an entry record +* @str: The trace line's function column string +* @buf: Buffer to store the extracted name +* @size: Size of the @buf +* +* Parse the function name out of an entry trace record by stripping away +* parentheses or braces. +*/ +static void extract_entry_func_name (const char *str,char *buf,size_t size) +{ +size_t i =0 ; + +while (*str && *str !='(' && *str !='{' && !isspace (*str) && i < size -1 ) + buf[i++] = *str++; + buf[i] ='\0' ; +} + +/** +* extract_exit_func_name - Extract the function name from an exit record +* @str: The trace line's function column string +* @buf: Buffer to store the extracted name +* @size: Size of the @buf +* +* Parse the original function name out of the trailing comment in an +* exit trace record. +*/ +static void extract_exit_func_name (const char *str,char *buf,size_t size) +{ +const char *p =strchr (str,'}' ); +size_t i =0 ; + + buf[0 ] ='\0' ; +if (!p) +return ; + + p =strstr (p,"/*" ); +if (!p) +return ; + + p =skip_spaces (p +2 ); +while (*p && !isspace (*p) && *p !='*' && *p !='/' && i < size -1 ) + buf[i++] = *p++; + buf[i] ='\0' ; +} + +/** +* make_blank_prefix - Create a blank prefix mirroring a trace record's context +* @ftrace: Perf ftrace context +* @line: The original trace record +* @pipe_char: Pointer to the final delimiter character of the context columns +* @buf: Buffer to store the blank prefix +* @size: Size of the @buf +* +* Generate a prefix matching the context columns of @line (e.g. CPU, duration) +* but with the duration field replaced by spaces. +*/ +static void make_blank_prefix (const struct perf_ftrace *ftrace,const char *line, +const char *pipe_char,char *buf,size_t size) +{ +size_t len = pipe_char - line +1 ; +const char *start_delim =NULL ; +char *c; + +if (!size) +return ; + +if (len >= size) + len = size -1 ; + +memcpy (buf, line, len); + buf[len] ='\0' ; + +if (*pipe_char =='|' ) { +if (ftrace->graph_verbose) { +/* In verbose mode, duration starts after the comm-pid '|' */ +for (const char *p = pipe_char -1 ; p >= line; p--) { +if (*p =='|' ) { + start_delim = p; +break ; + } + } + }else { +/* In default mode, duration starts after CPU ')' */ + start_delim =strchr (line,')' ); + } + +if (start_delim && start_delim < pipe_char && start_delim < line + len) { +char *end = buf + (pipe_char - line); + +if (end > buf + len) + end = buf + len; +for (c = buf + (start_delim - line) +1 ; c < end; c++) + *c =' ' ; + } + } +} + +/** +* find_func_column - Locate the function call column in a trace record +* @ftrace: Perf ftrace context +* @line: The trace record to parse +* @cpu: Pointer to store the CPU number +* +* Locate the context columns that precede the function call column of a +* function_graph trace record. Depending on the funcgraph-* options set +* up by perf, a record looks like: +* +* [ abstime |] CPU) [ comm-pid |] [ duration |] func +* +* The abstime and comm-pid columns are only present with --graph-opts +* verbose. +* +* Return: A pointer to the last character of the context columns (the final +* '|' delimiter). Returns NULL if @line is not a trace record. +*/ +static const char *find_func_column (const struct perf_ftrace *ftrace, +const char *line,int *cpu) +{ +const char *p = line; +char *endptr; +long val; + +if (ftrace->graph_verbose) { + p =strchr (p,'|' ); +if (!p) +return NULL ; + p++; + } + + p =skip_spaces (p); + val =strtol (p, &endptr,10 ); +if (endptr == p || *endptr !=')' ) +return NULL ; + p = endptr; + +if (ftrace->graph_verbose) { +const char *proc_start = p; + + p =strchr (p,'|' ); +if (!p) +return NULL ; + +if (val >=0 && val <FTRACE_MAX_CPUS ) { +const char *h = p -1 ; + +while (h >= proc_start && *h !='-' ) + h--; +if (h >= proc_start && *h =='-' ) { +long pid_val =strtol (h +1 ,NULL ,10 ); +pid_t task_pid = (pid_val ==0 ) ? +FTRACE_IDLE_PID (val) : + (pid_t )pid_val; + + ftrace_cpu_tasks[(int )val] =get_task_state (task_pid); + } + } + +/* +* In verbose mode, latency-format is also enabled by +* set_tracing_funcgraph_verbose(), emitting a "<lat-fmt> |" +* column after the comm-pid column. +*/ + p =strchr (p +1 ,'|' ); +if (!p) +return NULL ; + } + + p =strchr (p +1 ,'|' ); +if (!p) +return NULL ; + + *cpu = (int )val; +return p; +} + +/** +* print_line - Output a trace line verbatim, filtering return addresses if needed +* @ftrace: Perf ftrace context +* @line: The trace line to output +* @out: File stream to output to +* +* Output @line to @out. If ftrace->filter_retaddr is set, return address +* comments are filtered out first. +* +* Return: 0 on success, or a negative error code (e.g. -ENOMEM) on failure. +*/ +static int print_line (struct perf_ftrace *ftrace,const char *line,FILE *out) +{ +char *filtered; + +if (!ftrace->filter_retaddr) { +fprintf (out,"%s\n" , line); +return 0 ; + } + + filtered =strdup (line); +if (!filtered) { +pr_err ("Not enough memory\n" ); +return -ENOMEM ; + } + +ftrace_filter_retaddr (filtered); +fprintf (out,"%s\n" , filtered); +free (filtered); +return 0 ; +} + +/** +* ftrace_process_fgraph_line - Process and output a function graph trace line +* @ftrace: Perf ftrace context +* @line: The trace line to process +* @out: File stream to output to +* +* Process a trace line from the function_graph tracer. If --inline is set, +* this function parses the return address comment, resolves it to its +* inlined callchain, and synthesises intermediate frames into the streamed +* call graph. +* +* Return: 0 on success, or a negative error code on failure. +*/ +int ftrace_process_fgraph_line (struct perf_ftrace *ftrace,const char *line,FILE *out) +{ +char caller_sym[128 ]; +char blank_prefix[128 ]; +const char *inlined_names[FTRACE_MAX_INLINES ]; +const char *pipe_char; +const char *after_pipe; +const char *func_start; +struct ftrace_cpu_state *cs; +u64 caller_offset =0 ; +int num_inlines =0 ; +int cpu =-1 ; +int leading_spaces; +int extra_indent; +int caller_idx; +int active_inlines; +int common; +int match_idx; +int top_raw_indent; +int i, j; +bool is_exit; +bool has_open_brace; +bool has_semicolon; +bool has_retaddr = false; + +if (!ftrace->use_inline) +return print_line (ftrace, line, out); + + pipe_char =find_func_column (ftrace, line, &cpu); +if (!pipe_char) { +parse_context_switch (line); +return print_line (ftrace, line, out); + } + + cs =get_cpu_state (ftrace, cpu); +if (!cs) +return print_line (ftrace, line, out); + +make_blank_prefix (ftrace, line, pipe_char, blank_prefix,sizeof (blank_prefix)); + + after_pipe = pipe_char +1 ; + func_start =skip_spaces (after_pipe); + leading_spaces = func_start - after_pipe; + +if (*func_start =='\0' ) +return print_line (ftrace, line, out); + + is_exit = (*func_start =='}' ); + +if (is_exit) { +char exit_name[128 ]; + +extract_exit_func_name (func_start, exit_name,sizeof (exit_name)); + + top_raw_indent =-1 ; +for (i = cs->depth -1 ; i >=0 ; i--) { +if (!cs->stack[i].inlined) { + top_raw_indent = cs->stack[i].raw_indent; +break ; + } + } + +/* +* If currently overflowed beyond FTRACE_MAX_STACK, any exit whose +* indentation is deeper than the top tracked frame belongs to the +* overflow region. +*/ +if (cs->overflow_depth >0 && + (top_raw_indent <0 || leading_spaces > top_raw_indent)) { + cs->overflow_depth--; + extra_indent = cs->inlined_depth *2 ; +fprintf (out,"%.*s%*s%s\n" , + (int )(pipe_char - line +1 ), line, + leading_spaces + extra_indent,"" , func_start); +return 0 ; + } + +/* Find matching frame on stack */ + match_idx =-1 ; +if (exit_name[0 ] !='\0' ) { +for (i = cs->depth -1 ; i >=0 ; i--) { +if (!cs->stack[i].inlined && cs->stack[i].name && +strcmp (cs->stack[i].name, exit_name) ==0 ) { + match_idx = i; +break ; + } + } + } + +/* +* If bare brace ('}'), match the topmost non-inlined frame +* with matching indentation. +*/ +if (match_idx <0 && exit_name[0 ] =='\0' ) { +for (i = cs->depth -1 ; i >=0 ; i--) { +if (!cs->stack[i].inlined && + cs->stack[i].raw_indent == leading_spaces) { + match_idx = i; +break ; + } + } + } + +if (match_idx >=0 ) { + cs->overflow_depth =0 ; + }else if (cs->overflow_depth >0 ) { + cs->overflow_depth--; + extra_indent = cs->inlined_depth *2 ; +fprintf (out,"%.*s%*s%s\n" , + (int )(pipe_char - line +1 ), line, + leading_spaces + extra_indent,"" , func_start); +return 0 ; + } + +if (match_idx >=0 ) { +/* Close all inlined and orphaned frames above match_idx */ +while (cs->depth >0 && cs->depth -1 > match_idx) { +struct ftrace_stack_frame *top = &cs->stack[cs->depth -1 ]; + +if (top->inlined) { +int exit_indent = top->indent; + +if (cs->inlined_depth >0 ) + cs->inlined_depth--; +fprintf (out,"%s%*s} /* %s (inline) */\n" , + blank_prefix, exit_indent,"" , top->name); + } +free (top->name); + cs->depth--; + } + +/* Pop the matched real frame */ +if (cs->depth >0 && match_idx == cs->depth -1 && + !cs->stack[cs->depth -1 ].inlined) { +free (cs->stack[cs->depth -1 ].name); + cs->depth--; + } + + extra_indent = cs->inlined_depth *2 ; + }else { + extra_indent =0 ; + } + +fprintf (out,"%.*s%*s%s\n" , + (int )(pipe_char - line +1 ), line, + leading_spaces + extra_indent,"" , func_start); +return 0 ; + } + +/* It's an entry or a leaf call */ + has_open_brace = (strchr (func_start,'{' ) !=NULL ); + has_semicolon = (strchr (func_start,';' ) !=NULL ); + + top_raw_indent =-1 ; +for (i = cs->depth -1 ; i >=0 ; i--) { +if (!cs->stack[i].inlined) { + top_raw_indent = cs->stack[i].raw_indent; +break ; + } + } + +if (cs->overflow_depth >0 && top_raw_indent >=0 && + leading_spaces <= top_raw_indent) + cs->overflow_depth =0 ; + +if (ftrace_parse_retaddr (func_start, caller_sym,sizeof (caller_sym), &caller_offset)) { + has_retaddr = true; + num_inlines =ftrace_resolve_inlines (ftrace->machine, caller_sym, + caller_offset, inlined_names, +FTRACE_MAX_INLINES ); +if (num_inlines <0 ) +return num_inlines; + } + +/* Determine which inlined frames are currently open above the caller */ + caller_idx =-1 ; +if (has_retaddr && cs->overflow_depth ==0 ) { +for (i = cs->depth -1 ; i >=0 ; i--) { +if (!cs->stack[i].inlined && cs->stack[i].name && + cs->stack[i].raw_indent < leading_spaces && +strcmp (cs->stack[i].name, caller_sym) ==0 ) { + caller_idx = i; +break ; + } + } + } + +/* If caller is not on the tracked stack, do not synthesise inlines */ +if (has_retaddr && caller_idx <0 ) { +for (i =0 ; i < num_inlines; i++) +free ((void *)inlined_names[i]); + num_inlines =0 ; + } + + active_inlines =0 ; +if (caller_idx >=0 ) { +for (i = caller_idx +1 ; i < cs->depth; i++) { +if (cs->stack[i].inlined) + active_inlines++; +else +break ; + } + } + +/* Find common prefix of inlined functions */ + common =0 ; +if (caller_idx >=0 ) { +while (common < active_inlines && common < num_inlines && + cs->stack[caller_idx +1 + common].name && + !strcmp (cs->stack[caller_idx +1 + common].name, + inlined_names[common])) { + common++; + } + } + +/* Close stale callee frames and inactive inlined frames above caller */ +if (caller_idx >=0 ) + cs->overflow_depth =0 ; + +while (caller_idx >=0 && cs->depth > caller_idx +1 + common) { +struct ftrace_stack_frame *top = &cs->stack[cs->depth -1 ]; + +if (top->inlined) { +int exit_indent = top->indent; + +if (cs->inlined_depth >0 ) + cs->inlined_depth--; +fprintf (out,"%s%*s} /* %s (inline) */\n" , + blank_prefix, exit_indent,"" , top->name); + } +free (top->name); + cs->depth--; + } + +if (caller_idx <0 && cs->overflow_depth ==0 ) { +/* +* If caller is not on the tracked stack, unwind any frames whose +* raw indentation is greater than or equal to the new entry's +* indentation (their exit events were dropped), and close any +* trailing inlined frames that were left open from prior calls. +*/ +while (cs->depth >0 ) { +struct ftrace_stack_frame *top = &cs->stack[cs->depth -1 ]; + +if (top->inlined) { +int exit_indent = top->indent; + +if (cs->inlined_depth >0 ) + cs->inlined_depth--; +fprintf (out,"%s%*s} /* %s (inline) */\n" , + blank_prefix, exit_indent,"" , top->name); +free (top->name); + cs->depth--; + }else if (top->raw_indent >= leading_spaces) { +free (top->name); + cs->depth--; + }else { +break ; + } + } + } + +/* Open new inlined frames */ +for (i = common; i < num_inlines; i++) { +int entry_indent = leading_spaces + cs->inlined_depth *2 ; + +if (!inlined_names[i]) { +pr_err ("Not enough memory\n" ); +for (j =0 ; j < num_inlines; j++) +free ((void *)inlined_names[j]); +return -ENOMEM ; + } +if (cs->depth >=FTRACE_MAX_STACK ) +break ; + +fprintf (out,"%s%*s%s() { /* (inline) */\n" , + blank_prefix, entry_indent,"" , inlined_names[i]); + cs->stack[cs->depth].name =strdup (inlined_names[i]); +if (!cs->stack[cs->depth].name) { +pr_err ("Not enough memory\n" ); +for (j =0 ; j < num_inlines; j++) +free ((void *)inlined_names[j]); +return -ENOMEM ; + } + cs->stack[cs->depth].inlined = true; + cs->stack[cs->depth].indent = entry_indent; + cs->stack[cs->depth].raw_indent =-1 ; + cs->depth++; + cs->inlined_depth++; + } + +/* Print current line with updated indentation */ + extra_indent = cs->inlined_depth *2 ; + +if (ftrace->filter_retaddr) { +char *filtered =strdup (func_start); + +if (filtered) { +ftrace_filter_retaddr (filtered); +fprintf (out,"%.*s%*s%s\n" , + (int )(pipe_char - line +1 ), line, + leading_spaces + extra_indent,"" , filtered); +free (filtered); + }else { +pr_err ("Not enough memory\n" ); +for (i =0 ; i < num_inlines; i++) +free ((void *)inlined_names[i]); +return -ENOMEM ; + } + }else { +fprintf (out,"%.*s%*s%s\n" , + (int )(pipe_char - line +1 ), line, + leading_spaces + extra_indent,"" , func_start); + } + +/* If non-leaf entry, push onto stack */ +if (has_open_brace && !has_semicolon) { +char entry_name[128 ]; + +extract_entry_func_name (func_start, entry_name,sizeof (entry_name)); +if (cs->depth <FTRACE_MAX_STACK ) { + cs->stack[cs->depth].name =strdup (entry_name); +if (!cs->stack[cs->depth].name) { +pr_err ("Not enough memory\n" ); +for (i =0 ; i < num_inlines; i++) +free ((void *)inlined_names[i]); +return -ENOMEM ; + } + cs->stack[cs->depth].inlined = false; + cs->stack[cs->depth].indent = leading_spaces + extra_indent; + cs->stack[cs->depth].raw_indent = leading_spaces; + cs->depth++; + }else { + cs->overflow_depth++; + } + } + +for (i =0 ; i < num_inlines; i++) +free ((void *)inlined_names[i]); +return 0 ; +} + +/** +* perf_ftrace__setup_inlines - Initialise symbol resolution for inlines +* @ftrace: Perf ftrace context +* +* Initialise symbol tracking and load the kernel image (vmlinux) for +* DWARF debug information parsing. +* +* Return: 0 on success, or a negative error code on failure. +*/ +int perf_ftrace__setup_inlines (struct perf_ftrace *ftrace) +{ +struct map *kmap; +struct ftrace_task_state *ts; +pid_t single_pid; +int c; + +if (!ftrace->use_inline) +return 0 ; + + single_pid =ftrace__get_single_pid (ftrace); +if (single_pid >0 ) { + ts =get_task_state (single_pid); +if (ts) { +for (c =0 ; c <FTRACE_MAX_CPUS ; c++) + ftrace_cpu_tasks[c] = ts; + } + } + + symbol_conf.try_vmlinux_path = (symbol_conf.vmlinux_name ==NULL ); + symbol_conf.inline_name = true; +if (symbol_conf.vmlinux_name) + symbol_conf.ignore_vmlinux_buildid = true; + +if (symbol__init (NULL ) <0 ) { +pr_warning ("Failed to initialize symbols for inlined functions\n" ); +return -1 ; + } + + ftrace->machine =machine__new_host (NULL ); +if (!ftrace->machine) { +pr_warning ("Failed to create host machine for symbols\n" ); +return -1 ; + } + +if (symbol_conf.vmlinux_name) { + kmap =machine__kernel_map (ftrace->machine); +if (!kmap || +dso__load_vmlinux (map__dso (kmap), kmap, + symbol_conf.vmlinux_name, false) <=0 ) { +pr_warning ("Failed to load kernel image '%s'\n" , symbol_conf.vmlinux_name); + goto out_delete_machine; + } + }else { +if (machine__load_vmlinux_path (ftrace->machine) <=0 ) { +pr_warning ("Failed to load vmlinux from search path\n" ); + goto out_delete_machine; + } + } + +return 0 ; + +out_delete_machine: +machine__delete (ftrace->machine); + ftrace->machine =NULL ; +return -1 ; +} + +/** +* perf_ftrace__cleanup_inlines - Clean up resources used for inline resolution +* @ftrace: Perf ftrace context +* +* Free the machine context and the CPU stack tracking states. +*/ +void perf_ftrace__cleanup_inlines (struct perf_ftrace *ftrace) +{ +if (ftrace->machine) { +machine__delete (ftrace->machine); + ftrace->machine =NULL ; + } +ftrace_cpu_states__clear (); +}tools/perf/util/ftrace.h +14
-0@@ -1,6 +1,7 @@#ifndef __PERF_FTRACE_H__#define __PERF_FTRACE_H__ +#include <stdio.h> #include <linux/list.h> #include "target.h" @@ -41,6 +42,8 @@ struct perf_ftrace {int graph_tail;bool filter_retaddr;bool filter_retaddr_set; +bool use_inline; +struct machine *machine; };struct filter_entry {@@ -95,7 +98,18 @@ perf_ftrace__latency_cleanup_bpf(struct perf_ftrace *ftrace __maybe_unused)#endif /* HAVE_BPF_SKEL */ +struct machine ; + +#define FTRACE_MAX_INLINES 32 +bool ftrace_parse_retaddr (const char *str,char *sym_name,size_t sym_len,u64 *offset);void ftrace_filter_retaddr (char *str); +int ftrace_resolve_inlines (struct machine *machine, +const char *sym_name,u64 offset, +const char **inlined_names,int max_inlines); +int ftrace_process_fgraph_line (struct perf_ftrace *ftrace,const char *line,FILE *out); +void ftrace_cpu_states__clear (void ); +int perf_ftrace__setup_inlines (struct perf_ftrace *ftrace); +void perf_ftrace__cleanup_inlines (struct perf_ftrace *ftrace);#endif /* __PERF_FTRACE_H__ */ Signature · 1 line
2.55.0
Re: [PATCH perf-tools-next v9 2/6] perf ftrace: Support filtering return address comments
In reply to Namhyung KimQuoted 15 lines · Namhyung Kim
Hello,
Quoted 11 lines · Aaron Tomlin
When tracing with the Linux kernel's function graph tracer, enabling funcgraph-retaddr records the return address of each function entry as a comment in the trace stream (e.g. /* <-load_elf_phdrs+0x6c/0xb0 */). While valuable for tracing execution origins, these trailing comments can add considerable visual clutter to the call graph, particularly when examining deeply nested call trees or when synthesising higher-level abstractions.
Introduce support for filtering out return address comments from function graph trace output. Provide a standalone --filter-retaddr flag.
Quoted 6 lines
I think we agreed not to have this option in the previous discussion, no? The retaddr will appear only if user passed an option, so you can unconditionally filter out the comment if it's not given, right?
Thanks,
Namhyung
Hi Namhyung,
Yes, we did. Since --graph-opts retaddr already defines whether the user wants to see return address comments, there is no need for a standalone --filter-retaddr option.
Whenever perf ftrace enables funcgraph-retaddr internally (for --inline or -G/--graph-funcs), we can simply strip the /* <-caller */ comments unconditionally if !ftrace->graph_retaddr. If the user explicitly passed --graph-opts retaddr, we leave them in place.
I will drop the --filter-retaddr option and its documentation, and wire the filtering directly to !ftrace->graph_retaddr.
Kind regards,
Signature · 1 line
Aaron Tomlin
Re: [PATCH perf-tools-next v9 4/6] perf ftrace: Support display of inlined functions in function graph tracer
In reply to Namhyung KimQuoted 129 lines · Namhyung Kim
Quoted 118 lines · Aaron Tomlin
The Linux kernel's function graph tracer operates at the machine instruction level via compiler instrumentation (-fpatchable-function-entry), recording entry and return events for physical function calls. Consequently, functions inlined by the compiler are invisible in the resulting call graph, as no discrete call or return instructions are emitted for them. While technically expected, this omission frequently obscures the logical execution flow when analysing kernel subsystems heavily reliant upon inlining.
Address this by introducing the --inline option to perf ftrace for the
function_graphtracer.When the kernel is configured with
CONFIG_FUNCTION_GRAPH_RETADDR=y, the funcgraph-retaddr trace option records the caller's return address on each function entry, manifested in the trace stream as a comment (e.g. /* <-irq_work_queue_on+0x81/0xa0 */). By interrogating DWARF debug information from the kernel image (vmlinux) utilising libdw, perf ftrace resolves this return address to its inlined callchain and synthesises the intermediate inlined frames directly into the streamed call graph.Key aspects of this implementation:
1. Inlined frame presentation
Synthesised inlined functions are rendered with an explicit /* (inline) */ annotation at both entry and exit:
# tracer:
function_graph
#
# CPU FUNCTION CALLS
# | | | | |
6)activate_task(){
6)enqueue_task(){
6)enqueue_task_fair(){
6)update_curr_eevdf(){ /* (inline) */
6)update_curr(){
6)update_se(){
6)cgroup_account_cputime(){ /* (inline) */
6)cpuacct_charge();
6) } /*cgroup_account_cputime(inline) */
6) }
6)dl_server_update(){
6)update_curr_dl_se();
6) }
6) } /*update_curr_eevdf(inline) */
6)enqueue_hierarchy(){ /* (inline) */
6)update_curr(){
6)update_se();
6) }
6)enqueue_entity(){
6)account_entity_enqueue(){ /* (inline) */
6)account_llc_enqueue();
6) } /*account_entity_enqueue(inline) */
6) }
6) } /*enqueue_hierarchy(inline) */
6)place_entity(){
6)avg_vruntime();
6) }
6)__enqueue_entity();
6)dl_server_start();
6)hrtick_update(){ /* (inline) */
6)hrtick_active(){ /* (inline) */
6)hrtimer_active();
6) } /*hrtick_active(inline) */
6) } /*hrtick_update(inline) */
6)hrtick_start_fair();
6)psi_enqueue(){ /* (inline) */
6)psi_task_change(){
6)psi_flags_change();
6)psi_group_change(){
6)record_times();
6) }
6) }
6) } /*psi_enqueue(inline) */
6) }
6) }This affords immediate visual clarity to the reader upon entering an inlined scope, without necessitating a search for the closing delimiter.
2. Call graph reconstruction and per-task stack tracking
Maintains a per-task synthetic call stack tracking real and inlined frames across context switches, synthesising entry and exit delimiters to accurately reconstruct the execution hierarchy. Tasks are tracked in a hash table keyed by PID, with per-CPU active bindings updated upon context-switch banners or verbose line records. Inlined frames are structured strictly root-to-leaf and validated against the active stack to prevent un-traced callers from attaching orphan inlines. Bare closing braces without trailing function names (the default when funcgraph-tail is disabled) are correctly matched against preceding frames on the stack. Context column alignment, including task comm-pid delimiters in verbose mode, is preserved when generating prefixes for synthesised inline frames. Furthermore, stack depth saturation guards ensure that inlined frame depth tracking remains clamped, indentation cannot drift even under deep call trees exceeding
FTRACE_MAX_STACK, and overflow depth is automatically recovered upon caller detection.3. Option handling and diagnostics
Adds -k or --vmlinux to designate an explicit debug image, and provides clear diagnostic guidance should --inline be invoked on a kernel lacking
CONFIG_FUNCTION_GRAPH_RETADDR. Automatically enables return address comment filtering unless explicitly requested.4. Automated verification
Expands the unit test suite in
tools/perf/tests/ftrace.cto validate DWARF inline symbol resolution, comment filtering toggles, bare closing braces handling, task context-switch isolation and migration, and stack saturation/overflow recovery.Assisted-by Antigravity:gemini-3.1-proSigned-off-by Aaron Tomlin <[email protected]>Quoted 7 lines
--- tools/perf/Documentation/perf-ftrace.txt | 17 +- tools/perf/builtin-ftrace.c | 76 +- tools/perf/tests/ftrace.c | 914 ++++++++++++++++++- tools/perf/util/ftrace.c | 1059 +++++++++++++++++++++- tools/perf/util/ftrace.h | 14 + 5 files changed, 2059 insertions(+), 21 deletions(-)I think it's too big and you can split into pieces like:
Quoted 7 lines
1. add the basic task stack management code
2. support context switch
3. support inline functions
4. add test codeThanks,
Namhyung
Hi Namhyung,
Acknowledged.
Kind regards,
Signature · 1 line
Aaron Tomlin