Advanced OS Customizations & Bootloaders

Beyond the Basics: Advanced Ftrace Features for Android Power Users (Histograms, Filters, Function Tracing)

Google AdSense Native Placement - Horizontal Top-Post banner

Introduction: Unlocking Android Performance with Ftrace

Ftrace, the Linux kernel’s internal tracing mechanism, is an invaluable tool for debugging performance issues, understanding system behavior, and optimizing Android devices. While many power users are familiar with basic event tracing, its true power lies in advanced features like event filtering, histograms, and function tracing. This guide delves into these capabilities, empowering you to perform deeper, more precise performance analysis on your Android system.

Understanding the intricate dance between processes, threads, and kernel operations is crucial for identifying bottlenecks. Ftrace provides a high-resolution lens into this world, offering insights that traditional userspace tools often miss. By leveraging these advanced features, you can significantly reduce the noise in your trace data and focus on the specific events and functions that matter most for your debugging efforts.

Prerequisites: Accessing Ftrace on Android

To follow along, you’ll need root access on your Android device and `adb` set up on your host machine. Most Ftrace operations are performed via the `debugfs` filesystem, typically mounted at `/sys/kernel/tracing`.

adb shell
su
cd /sys/kernel/tracing

Before starting any advanced tracing, it’s good practice to clear the trace buffer and disable all events:

echo 0 > tracing_on
echo > trace
echo nop > current_tracer
for i in /sys/kernel/tracing/events/*/*/enable; do echo 0 > $i; done

Advanced Filtering: Isolating Relevant Events

One of the most common challenges in Ftrace is dealing with the sheer volume of trace data. Event filtering allows you to specify conditions under which events should be recorded, drastically reducing the trace size and improving readability.

Applying Event Filters

Filters are applied to specific event types using the `filter` file within the event’s directory. The syntax supports various conditions on event arguments.

# Example: Trace only sched_switch events where the previous process was 'surfaceflinger'
echo 'prev_comm == "surfaceflinger"' > events/sched/sched_switch/filter

# Example: Trace only ext4_sync_file events where 'dev' is a specific device ID
echo 'dev == 202,1' > events/ext4/ext4_sync_file/filter

# Example: Trace events for a specific PID (e.g., PID 1234)
echo 'common_pid == 1234' > events/sched/sched_wakeup/filter
echo 'common_pid == 1234' > events/sched/sched_switch/filter

# Enable the filtered events
echo 1 > events/sched/sched_switch/enable
echo 1 > events/sched/sched_wakeup/enable
echo 1 > tracing_on

After capturing your trace, remember to disable the filters and events:

echo 0 > events/sched/sched_switch/filter
echo 0 > events/sched/sched_wakeup/filter
echo 0 > events/ext4/ext4_sync_file/filter
echo 0 > events/sched/sched_switch/enable
echo 0 > events/sched/sched_wakeup/enable

You can combine multiple conditions using `&&` (AND) and `||` (OR) operators, and use comparison operators like `==`, `!=`, “, `=`. String comparisons are also supported.

Histograms: Aggregating Event Data

While raw trace data is useful, sometimes you need aggregated statistics to quickly grasp performance patterns. Ftrace histograms provide this by counting or calculating metrics (like average, sum, min, max, or latency) for events based on specified keys.

Configuring Event Histograms

Histograms are configured via the `hist` file within an event’s directory. The basic syntax involves `keys` to group by and `values` to calculate.

# Clear previous histogram settings
echo > events/sched/sched_switch/hist

# Example: Measure latency of sched_switch events grouped by previous and next process names
echo 'keys=prev_comm,next_comm:val=common_latency.max' > events/sched/sched_switch/hist

# Or, for a simple count:
echo 'keys=prev_comm' > events/sched/sched_switch/hist

# Enable the event and tracing
echo 1 > events/sched/sched_switch/enable
echo 1 > tracing_on

# Run your workload...

# Read the histogram output
cat events/sched/sched_switch/hist

# Disable and clear
echo 0 > tracing_on
echo 0 > events/sched/sched_switch/enable
echo > events/sched/sched_switch/hist

Common `val` options include `hitcount` (default), `common_latency.min/max/avg/sum`. The `common_latency` field is automatically available for events that report latency.

Histograms are incredibly powerful for identifying which processes are causing the most context switches, which files are being accessed most frequently, or which kernel functions are taking the longest.

Function Tracing: Pinpointing Kernel Function Execution

The `function` tracer allows you to monitor the execution of specific kernel functions. This is crucial for understanding control flow, identifying unexpected function calls, or measuring the duration of critical kernel routines.

Enabling Function Tracing

Unlike event tracing which uses `debugfs/events`, function tracing is controlled via the `current_tracer`, `set_ftrace_filter`, and `set_ftrace_notrace` files.

# Clear and reset
echo nop > current_tracer
echo > set_ftrace_filter
echo > set_ftrace_notrace

# Set the function tracer
echo function > current_tracer

# Filter to trace only specific functions (e.g., functions related to ext4 filesystem)
echo 'ext4_*' > set_ftrace_filter

# Exclude specific functions (e.g., ext4_journal_start, if it's too noisy)
echo 'ext4_journal_start' > set_ftrace_notrace

# Enable tracing
echo 1 > tracing_on

# Run your workload...

# Read the trace
cat trace

# Disable and clear
echo 0 > tracing_on
echo nop > current_tracer
echo > set_ftrace_filter
echo > set_ftrace_notrace
echo > trace

Wildcards (`*`) are supported in `set_ftrace_filter` and `set_ftrace_notrace`. Be cautious when enabling function tracing on a broad set of functions, as it can introduce significant overhead and generate massive trace files, potentially impacting system performance. Always start with a narrow filter.

Combining Advanced Features: A Practical Example

Let’s imagine you’re debugging a UI jank issue and suspect excessive file I/O operations from a specific process. You want to see which `ext4` functions are being called by `surfaceflinger` and how frequently.

# 1. Clear previous settings
cd /sys/kernel/tracing
echo 0 > tracing_on
echo > trace
echo nop > current_tracer
for i in /sys/kernel/tracing/events/*/*/enable; do echo 0 > $i; done
echo > set_ftrace_filter
echo > set_ftrace_notrace
echo > events/ext4/ext4_sync_file/hist

# 2. Set up function tracer for ext4 functions
echo function > current_tracer
echo 'ext4_*' > set_ftrace_filter

# 3. Enable sched_switch events and filter by surfaceflinger's PID
#    (Note: You'll need to find surfaceflinger's PID first: `pidof surfaceflinger`)
SURFACEFLINGER_PID=$(pidof surfaceflinger)
echo "common_pid == $SURFACEFLINGER_PID" > events/sched/sched_switch/filter
echo 1 > events/sched/sched_switch/enable

# 4. Set up a histogram for ext4_sync_file to count calls by common_pid
echo 'keys=common_pid' > events/ext4/ext4_sync_file/hist
echo 1 > events/ext4/ext4_sync_file/enable

# 5. Start tracing
echo 1 > tracing_on

# 6. Interact with the UI to reproduce jank...

# 7. Stop tracing
echo 0 > tracing_on

# 8. Examine results
cat trace | grep surfaceflinger # Look for function calls related to surfaceflinger
cat events/ext4/ext4_sync_file/hist # See I/O counts by PID

# 9. Clean up
echo nop > current_tracer
echo > set_ftrace_filter
echo > set_ftrace_notrace
echo 0 > events/sched/sched_switch/enable
echo 0 > events/ext4/ext4_sync_file/enable
echo > events/sched/sched_switch/filter
echo > events/ext4/ext4_sync_file/hist

This combined approach allows you to correlate specific kernel function calls with process activity and aggregate data for quick insights, providing a holistic view of the performance issue.

Conclusion

Mastering advanced Ftrace features like event filtering, histograms, and function tracing transforms your ability to diagnose complex performance problems on Android. These tools move you beyond simply observing events to actively analyzing, quantifying, and isolating the root causes of system inefficiencies. By practicing with these techniques, you’ll gain unparalleled insight into the kernel’s operation, becoming a true Android power user capable of deep-level performance debugging and optimization.

Android Mobile Specs & Compare Directory

Are you researching mobile hardware properties, processor SoCs, GPU chipsets, or RAM configurations? Access our complete specs catalog to compare up to 5 devices side-by-side!

Compare Devices Specs →
Google AdSense Inline Placement - Content Footer banner