This is the second article in the “Systrace Thread CPU State Analysis Tips” series. It analyzes the causes of the “Running” state in Systrace and provides optimization strategies for when Running segments are excessively long.
The goal of this series is to use Systrace to view the Android system from a different perspective and to learn the Framework through visualization. While reading Framework source code can be difficult to remember, seeing the flow in Systrace can lead to deeper understanding. You can find the complete Systrace Basics and Action Series here.
Table of Contents
- Systrace Thread CPU State Analysis Tips - Runnable
- Systrace Thread CPU State Analysis Tips - Running
- Systrace Thread CPU State Analysis Tips - Sleep and Uninterruptible Sleep
Long Running Duration
Visualization
In Trace, a green segment indicates the thread is in the Running state.
Cause 1: High Code Complexity and Execution Time
This is the most common cause. While rare, platform bugs can occur if manufacturers add heavy logic to high-frequency core functions in libc or system calls, prolonging execution time.
Optimization Suggestions: Optimize logic and algorithms to reduce complexity. To pinpoint which specific function is time-consuming, use the AS CPU Profiler, simpleperf, or add custom trace points via the Trace.begin/end() API.
Cause 2: Interpreted Code Execution
The keyword “Compiling” in a Trace might indicate interpreted execution. This frequently occurs with newly installed Apps that haven’t undergone odex optimization.
Optimization Suggestions: Test with a dex2oat optimized version. Low performance under interpreted execution can only be improved by running dex2oat or optimizing the code itself.
Additionally, certain language features like frequent JNI calls or repetitive reflection can slow down execution. Beyond experience, use CPU Profiler or simpleperf for diagnosis.
Cause 3: Thread Running on Small Cores
For CPU-bound operations, Small Cores may not meet performance needs as they are designed for non-UX-critical threads. However, Android cannot always guarantee optimal scheduling, and tasks may be assigned to Small Cores.
Optimization Suggestions: Use Thread Affinity (Core Binding) or SchedBoost to ensure threads run on cores with higher compute power, like Big Cores. If migrating cores doesn’t help, check if the frequency is being scaled properly (see Cause 4).
Cause 4: Big Cores Running at Low Frequencies
Optimization Suggestions:
- Optimize code logic to reduce load, allowing smooth execution even at lower frequencies.
- Adjust scheduler frequency-scaling parameters for more aggressive scaling based on load.
- Use platform-provided APIs to lock the CPU at a specific frequency (commonly known as “locking frequency”).
Cause 5: Thermal Throttling: Core Shutdown and Frequency Capping
Optimization Suggestions:
Due to structural heat dissipation limits or aggressive thermal parameters, almost all manufacturers will cap CPU frequency or shut down cores to prevent overheating and burns. The first step is identifying the heat trigger.
Distinguish between external and internal factors. External factors include ambient heat (sunlight, heaters), which is worse in summer.
Internal factors are primarily driven by the CPU, Modem, camera module, or other power-intensive components. For the CPU, if a background thread is saturating a core, resolve it first. If a foreground app’s high load causes high current draw, reduce that load. For other components, check for unnecessary operation, then optimize business logic.
If thermal parameters are overly aggressive, compare with competitors to tune the parameters for a better balance.
Cause 6: Weak CPU Compute Power
Optimization Suggestions:
ARM processors with the same frequency can vary significantly in performance due to microarchitecture configurations. L1/L2 Cache capacity also heavily impacts MIPS (Million Instructions Per Second).
Two optimization paths:
- Compiler Parameters: Difficult for most App developers. System vendors (like Huawei with the Ark Compiler) optimize JNI and code efficiency to boost performance without changing App source code.
- Code Logic Optimization: Use tools like
simpleperfto find hot spots or observe CPU behavior:- High Cache Miss Rate: Optimize memory access patterns.
- Excessive Complex Instructions: Refactor code to reduce complexity.
- Caching: Design robust business caches to improve hit rates and avoid “churn” (repeated allocation/deallocation).
About Me && Blog
Below is my personal intro and related links. I look forward to exchanging ideas with fellow professionals. “When three walk together, one can always be my teacher!”
- Blogger Intro: Includes personal WeChat and WeChat group links.
- Blog Content Navigation: A guide for my blog content.
- Curated Excellent Blog Articles - Android Performance Optimization Must-Knows: Welcome to recommend projects/articles.
- Android Performance Optimization Knowledge Planet: Welcome to join and thank you for your support~
One walks faster alone, but a group walks further together.
