This is the seventh day of my participation in the First Challenge 2022.

Caton analyzes the idea

The whole idea, philosophically speaking, is pretty much the same:

  • Found the problem
  • Location problem
  • quantification
  • To fix the problem

The most complex part of the problem is locating and repairing the problem. Systrace is a tool to simplify problem locating. It organizes the events on the link in a timeline and provides visual icon display.

Caton problem analysis process

  • Problem detection – visual lag, online monitoring, GPU drawing bar charts, etc. can help us find page lag
  • StrictMode, Systrace, BlockCanary, etc.
  • Quantization problem – Local detailed analysis can use TraceView or Hugo to print the time of each method, and compare with the caton standard. For example, when normal human eyes collect animation less than 20fps, they will think that there is caton.
  • Fix problems – For specific problems, such as Io operations, mainline network requests, synchronous waiting, frequent GC, etc., fix specific problems.

Systrace introduction

  • Systrace takes advantage of fTrace’s ability to use probes to add staking code to key nodes of the system and start threads to collect the information we need.
  • The android SDK provides a start systrace script systrace. Py, located in the android SDK/platform – the tools/systrace/systrace. Py
  • Add trace.beginSection (“TTag”) and trace.endSection to the specific code to add the TTags you want to collect, but you need to specify which tags to collect.

Systrace use

Systrace command

  • Systrace. Py is provided by the android system tools, located in the android SDK/platform – the tools/systrace/systrace. Py
  • The systrace analysis command is as follows:
python systrace.py [options] [category ...]  python systrace.py -o ~/mytrace.html -a com.hch.ioc gfx view wm am res syncCopy the code

The options that

Option is optional. The important parameters are as follows:

  • -a <package_name> : This option enables the Trace function of the custom Trace Label in the specified package name App. That is, if you use it in your codeTrace.beginSection("tag").Trace.endSection; By default, your code does not take effect until you specify the package name.
  • -t N: specifies the time when Trace is run. If no value is entered, you need to manually enter the Enter command to end the Trace.
  • -l: This lists the Trace modules supported by the phone system you are analyzing. That’s the command above[category]The part; Different versions of the system support different modules,

-o FILE: specifies the output path of the trace data FILE. If the output path is not specified, the output path is trace.html of the current directory.

The category that

Category specifies the module to be monitored, such as AM, PM,webview,power, etc., which can be obtained using -l:

Py -l # GFX -graphics input-input view-view System webview -webview wm-window Manager am -  Activity Manager sm - Sync Manager audio - Audio video - Video camera - Camera hal - Hardware Modules res - Resource Loading dalvik - Dalvik VM rs - RenderScript bionic - Bionic C Library power - Power Management pm - Package Manager ss - System Server database - Database network - Network adb - ADB vibrator - Vibrator aidl - AIDL calls nnapi - NNAPI rro - Runtime Resource Overlay pdx - PDX services sched - CPU Scheduling freq - CPU Frequency idle - CPU Idle disk - Disk I/O sync - Synchronization memreclaim - Kernel Memory Reclaim binder_driver - Binder Kernel driver binder_lock - Binder global lock trace gfx - Graphics (HAL) ion - ION allocation (HAL)Copy the code

Common modules are described as follows:

  • sched: CPU scheduling information, very important; You can see what threads the CPU is running at each time; Thread scheduling, such as lock information.
  • gfx: Graphic information, including SerfaceFlinger, VSYNC messages, Texture, RenderThread, etc. The analysis of Catton relies heavily on this.
  • view: View Draws system information, such as onMeasure, onLayout, etc. It would be more helpful to analyze Carden.
  • am: Information about ActivityManager calls; It is useful to analyze the startup process of an Activity.
  • dalvik: Information about virtual machines, such as GC pauses.
  • binder_driverBinder driver information, if you suspect a Binder IPC problem, open this.
  • core_services: Information about core services in SystemServer, used to analyze specific problems

Description of common module combinations

  • GFX Input View HWUI sliding list test
  • GFX Input Sched View am WM RES page seconds open test
  • GFX Input View Dalvik Disk GC or IO causes a lag
  • GFX Input View RES AM WM Power problem analysis

Thread Status Description

  • Green: running During normal operation, click to view the running time
  • Blue: The longer the duration of Runnable, the busier the CPU is, and there may be too many threads running.
  • White: Sleeping, thread sleep, need to pay attention to UIThread’s sleep condition, after selected color change, is actually white.
  • Orange: WATING blocking, IO blocking, network blocking, synchronization blocking, etc.

Systrace Table operation shortcut keys

Key operation w zoom in, [+ Shift] faster, S zoom out, [+ Shift] faster, A move left, [+ Shift] faster, D move right, F Zoom in on the current selected area M mark the current selected area V Highlight VSync G Toggle whether to display 60Hz grid lines 0 Restore trace to initial state, H Toggle whether to display details/Search keyword Enter to display the search results. See ← → Locating the Search results' Show/hide the script console? Display help functionCopy the code

Systrace case study

Sleep 500ms in the onCreate method of the main Activity

try {
    Thread.currentThread().sleep(500);
} catch (InterruptedException e) {
    e.printStackTrace();
}
Copy the code

Start command to capture trace file:

python systrace.py -o ~/mytrace1.html -a com.hch.hive gfx view wm am sched  
Copy the code

Chrome opens as follows:

  • 1 allows you to filter specific processes, like the processes we’re looking at in our app.
  • 2 can highlight vSYNC signal
  • The situation of each frame in the whole flow can be observed at 3 points. Generally, the orange or red frames are stuck frames, and we need to focus on analysis
  • Four are the reasons for our problems.
  • The thread status is displayed at 5.

The actual analysis

  • Select a specific process, usually the process of your app

  • Find the left frames line, where each frame takes less than 16ms in green and severe lag in yellow and red, which are the frames we are analyzing

  • It turned out that the thread was asleep for 500ms