16.1 Introduction to the Function Tracer Subsystem
The Function Tracer Subsystem (ftrace) is a built-in tracing infrastructure embedded within the Linux kernel. It is capable of monitoring the execution call chains of almost all kernel functions with minimal operational overhead. The peak efficiency of the function tracer stems from its dynamic instrumentation technique (Dynamic Ftrace), which implements live tracing through runtime instruction replacement. The execution of runtime instruction patching requires coordinated synchronization across two distinct implementation phases: compilation and boot initialization.
During kernel compilation, if the configuration flag CONFIG_FUNCTION_TRACER is enabled (achieved in legacy Linux versions via the GCC -pg compiler flag), the compiler toolchain automatically plants a profiling call hook referencing mcount (or __fentry__) at the leading boundary entry point of every single kernel function. When the system bootstraps the function tracer during early initialization (ftrace_init), the initialization routine scans and registers all these execution patch sites, and completely overwrites their call instructions with hardware NOP (no-operation) placeholders. Following this transformation, the performance penalty imposed on the operating system remains virtually zero. When a specific function later needs to be actively tracked, the kernel hot-patches the matching code site, substituting the NOP placeholder with a concrete branch instruction pointing directly to a tracing redirection trampoline. This redirection relies on the exact same microarchitectural code manipulation mechanics detailed within Chapter 12 covering jump label modification.
The metrics and control paths monitored by the function tracer subsystem encompass:
-
Core kernel function invocations alongside their corresponding parent caller functions.
-
Hierarchical function execution relationships arranged in a tree topology, including precise runtime execution duration metrics.
-
Auditing and logging the maximum duration for which hardware interrupts are continuously disabled, tracking the exact code locations responsible for the blockages to troubleshoot systemic latency anomalies.
-
Tracking the precise time windows during which thread preemption is strictly disabled.
Under standard operational baseline criteria, the function tracer subsystem resides in an inactive state. Activating active tracing passes requires dispatching explicit configuration commands from user space via the interaction entries exposed within the tracefs file system. Modern Linux distributions automatically mount the tracefs infrastructure by default at the virtual file system directory path /sys/kernel/tracing.
The generic diagnostic execution workflow for initializing a function tracing pass is generally executed as follows:
- Mounting the tracefs infrastructure (automatically handled across modern distributions):
mount -t tracefs nodev /sys/kernel/tracing
- Configuring the target tracer plugin type:
echo function_graph > /sys/kernel/tracing/current_tracer
- Specifying the target execution filtering constraints:
echo 'vfs_read*' > /sys/kernel/tracing/set_ftrace_filter
- Energizing the tracing engine:
echo 1 > /sys/kernel/tracing/tracing_on
The finalized tracking telemetry payloads are collected and exposed via the output file /sys/kernel/tracing/trace.