Android performance optimization – Systrace tool

====

Systrace generates an output HTML file that contains a series of sections. The report lists threads for each process. If a given thread renders a UI frame, the report also indicates the rendered frame along the timeline. Time passes forward as you move from left to right in the report.

The report contains the following sections from top to bottom

  • UI interactions

The first section contains bar graphs that represent specific user interactions in the app or game, such as tapping the device screen. These interactions serve as useful time markers

UI interactions

  • CPU activity

A bar graph representing thread activity in each CPU is shown. The bar graph shows CPU activity for all applications, including yours or games.

Example of a collapsed CPU activity section

folded graph

The CPU activity section is scalable and allows you to see the clock frequency of each CPU

![](https://imgconvert.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW Android Open Source Project "ali1024.coding.net/public/P7/Android/git" 8vdXBsb2FkX2ltYWdlcy8xMzE1NTkyLTVmOGEzZTY3NDkwMDdhYmUucG5n?imageprocess=x-image/png,format)

CPU activity (expanded view), showing CPU clock frequency in Systrace report

  • system events

Histograms show specific system-level events such as texture counts and the total size of a specific object.

system level events

A histogram worth checking carefully is the one labeled SurfaceView. The count represents the number of combined framebuffers that have been passed to the display pipeline and are waiting to be displayed on the device screen. Since most devices are double or triple buffered, this count is almost always 0, 1 or 2.

Other histograms describing the Surface Flinger process, including VSync events and UI thread swap work, as shown below

[External link image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the image and upload it directly (img-oZejS9fy-1650347913260) (https://imgconver "Android study notes summary + latest mobile architecture video + big factory Android" Interview questions + project actual combat source code handout "open source t.csdnimg.cn/aHR0cHM6Ly91cGxvYWQtaW1hZ2VzLmppYW5zaHUuaW8vdXBsb2FkX2ltYWdlcy8xMzE1NTkyLTUyMzBkZjFiYjM4ZmM3MjIucG5n?x-oss-process=image/format,png)]

Surface Flinger example graph from Systrace report

  • display frame

display frame

This section, usually the most in a report, depicts a colored line followed by stacks of bar graphs. These shapes represent the state and frame stack of a particular thread that has been created.

The UI thread or the main thread where an application or game usually runs is always shown as the first thread.

frame stack information

The multi-colored lines above each stack of bars represent the set of states for a particular thread over time. Each part of the row can contain one of the following colors:

Green: Running

The thread is completing process-related work or responding to an interrupt.

Blue: Runnable

The thread can run but is not currently scheduled.

White: Sleeping

The thread has no work to do, probably because the thread is blocked on the mutex.

Orange: Uninterruptable sleep

The thread is blocked on I/O or waiting for the disk operation to complete.

Purple: Interruptable sleep (sleep can be interrupted)

The thread is blocked on another kernel operation (usually memory management).

  • Report Analysis Operation Shortcuts

| key | description |

| — | — |

| W | Zoom in on Timeline |

| A | Move left on the tracking timeline |

| S | Zoom Out Timeline |

| D | Pan right on the tracking timeline |

| E | Center the tracking timeline at the current mouse position |

| M | Select the current frame |

| 1 | Changes the currently active selection model to Selection mode. Corresponds to the first button displayed in the mouse selector toolbar |

| 2 | Changes the currently active selection model to Pan mode. Corresponds to the second button displayed in the mouse selector toolbar |

| 3 | Changes the currently active selection model to Zoom mode. Corresponds to the 3rd button displayed in the mouse selector toolbar |

| 4 | Changes the currently active selection model to Timekeeping mode. Corresponds to the 4th button displayed in the mouse selector toolbar |

| G | Show grid at the beginning of the currently selected task |

| Shift + G | Show grid at the end of the currently selected task |

| Left Button | Selects the previous event on the currently selected timeline |

| Right Click | Selects Next Event on the Currently Selected Timeline |

Custom systrace data acquisition

===============

The above data only shows information about processes at the system level by default, so it is sometimes difficult to know which methods of an application or game are executing at a given time relative to system events.

The Android platform provides a trace API that can be used to mark specific pieces of code. If you capture a new system trace for an application “debug”version and include the -a option, as shown in the snippet below, these custom events will appear in the Systrace report:

python systrace.py -a com.zerone.qrcode -b 16384 -o my_systrace_report.html sched freq idle am wm gfx view binder_driver hal dalvik camera input res

java layer code

Each level of the stack represents beginSection()the invocation or start of a custom trace event defined for the app or game

  • Trace.beginSection(String sectionName)and  Trace.endSection()needs to be paired
  • In order to ensure that each  Trace.beginSection(String sectionName) will have a corresponding  Trace.endSection(), it is recommended to use try {……} finally {……}
  • If  Trace.endSection()there are more than one before  Trace.beginSection(String sectionName), Trace.endSection()it will match the closest unmatched one Trace.beginSection(String sectionName)
  • Trace.beginSection(String sectionName)and  Trace.endSection()needs to be in the same thread

public class MyAdapter extends RecyclerView.Adapter {

@Override

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

Trace.beginSection(“MyAdapter.onCreateViewHolder”);

MyViewHolder myViewHolder;

try {

myViewHolder = MyViewHolder.newInstance(parent);

} finally {

//In try and catch statements, always call "endSection()" in a

// "Finally" blocks. This way the method is called even when a

//An exception occurs.

Trace.endSection();

}

return myViewHolder;

}

@Override

public void onBindViewHolder(MyViewHolder holder, int position) {

Trace.beginSection(“MyAdapter.onBindViewHolder”);

try {

try {

Trace.beginSection(“MyAdapter.queryDatabase”);

RowItem rowItem = queryDatabase(position);

dataset.add(rowItem);

} finally {

Trace.endSection();

}

holder.bind(dataset.get(position));

} finally {

Trace.endSection();

}

}

At last

Here, some friends around me and I have specially compiled a systematic and comprehensive learning material for rapid advancement as an Android senior engineer. Covers the basic learning skills of Android - Android senior architects.

Attached: 20 sets of real Android interview questions for first- and second-tier Internet companies (including BAT, Xiaomi, Huawei, Meituan, Didi) that we collected because of the autumn recruitment and my own Android review notes (including Android basic knowledge points, Android extensions) Knowledge points, Android source code analysis, summary of design patterns, Gradle knowledge points, summary of common algorithm problems.)

With some friends around me, I specially compiled a systematic and comprehensive learning material for rapid advancement as an Android senior engineer. Covers the basic learning skills of Android - Android senior architects. **

Attached: 20 sets of real Android interview questions for first- and second-tier Internet companies (including BAT, Xiaomi, Huawei, Meituan, Didi) that we collected because of the autumn recruitment and my own Android review notes (including Android basic knowledge points, Android extensions) Knowledge points, Android source code analysis, design pattern summary, Gradle knowledge points, summary of common algorithm questions.)
[External link image dumping...(img-CnK6yIej-1650347913262)]

Guess you like

Origin blog.csdn.net/m0_54883970/article/details/124272153