The previous two chapters have covered the preparation of statistics metrics, startup frameworks, and so on. This chapter will begin to analyze how to leverage the tools to analyze and solve problems. This chapter covers analyzing time consuming issues and improving thread scheduling.
Time consuming Solution
Method time consuming tool
“To do a good job, you must first sharpen your tools”, so we need to find the right tools for startup optimization analysis. The official Method Trace tool is provided, but the performance loss is high and the results obtained are not real. Nanoscope is very realistic, but you need to swipe a customized ROM to use Nanoscope; Systrace makes it easy to track the elapsed time of key system calls, but does not support elapsed time analysis of application code.
Method Trace
First of all, we analyze how to use the Method Trace tool. There are actually two ways to use Method Trace, one is to capture Method Trace through the Profile mode of Android Studio, the other is to use codeDebug.startMethodTracing()
Fetching.
Method. trace file visualization
1) Profile mode in Android Studio
In this way, you only need to open the Profile mode, click ON CPU, and click on Record after entering the CPU page.
Method Trace in the startup phase needs to be configured to enable.
Advantages and disadvantages of this approach
- Advantages: No code to write coupling.
- Disadvantages: Trace file capture must be carried out in Profile mode, which can only be carried out in debug mode. In addition, the performance loss is very high, leading to a large error in problem analysis.
2) through the code to grab the app startup startMethodTracingSampling, after completion of loading stopMethodTracing home page, and then generates the trace files, trace files exported from the cell phone to the computer, through the profile window open.
Debug.startMethodTracingSampling(traceName, 0, sampleInterval);
Debug.stopMethodTracing();
Copy the code
Advantages and disadvantages of this approach
-
Advantages: Trace files can be captured in debug and release mode, and the analysis of captured files in release mode is more accurate.
-
Disadvantages: You need to add code to the project, but the amount of code is small.
3) Analysis In fact, we found that method Trace in relase mode in code mode had a good impact on performance, and the overall startup time only increased by 300ms after Method Trace was enabled on low-end computers.
Systrace
There is also a “Systrace + function peg” scheme. The packaging process generates Trace information files by inserting Trace nodes on all methods. Add Trace tags using Gradle automation
class Trace { public static void i(String tag) { Trace.beginSection(name); } public static void o() { Trace.endSection(); }}Copy the code
Scheme selection
In fact, after analyzing the above two approaches, we will adopt different approaches for different scenarios.
- Local development quickly analyzes problems through
Debug.startMethodTracingSampling
Mode, high debugging efficiency. - Analysis adopted for very detailed depth
Systrace + function peg
The way.
Time troubleshooting and troubleshooting
Network data parsing time
The time-consuming tasks related to the startup process are investigated through Method Trace. A typical scenario is shown here. After the network data request is returned, the main thread parses the data. Here we can just do it in asynchrony.
Layout Parsing time
Create the home page layout asynchronously by enabling preloading.
For most time-consuming scenario we processing plan is to put the time-consuming tasks in asynchronous thread of execution, this time we need to think about, put the task in asynchronous thread execution, will take the CPU, that we how to analyze compatible or not on the asynchronous, or is there a better way, we continue to analyze the following.
Solve thread scheduling, improve execution efficiency
Systrace is a new performance data sampling and analysis tool for Android4.1. It can help developers to collect Android key subsystems such as SurfaceFlinger/SystemServer/Kernel/Input/Display Framework and some key module, a service, the View system, etc.) the operation of the information, To help developers more intuitive analysis of system bottlenecks, improve performance.
Systrace tracks system I/O operations, kernel work queues, CPU load, and the health of Android subsystems. About Systrace official introduction and use of the can see here: the explanations of the great god Systrace article can see androidperformance.com/2019/07/23/…
To get the basics straight, each thread has a line above it that represents the current state of execution.
- Green: Running
- Blue: Running
- White: Hibernating
Systrace use
Systrace calls start and end points in code that must correspond.
Trace.beginSection("tag");
Trace.endSection();
Copy the code
Resolving task dependencies
Part of the solution to dependencies is business understanding. Some code is known to have obvious dependencies, such as downloading modules that depend on HTTP modules, which will not be described in detail.
2 Troubleshoot some dependencies that are difficult to analyze to resolve the lock problem.
We will Trace the start and end of all tasks to find the thread line where the line is white, indicating that the current is now dormant. Zoom in and click belowmonitor
The following window will display the current thread lock waiting insee-network-1
thegetInterface
We combine the Method Trace call stack with the corresponding threadsee-network-1
Search this method and you’ll see a clear call stack, at which point we’llgetInterface
Method ahead, configure the current task togetInterface
Corresponding to the task later
Resolve CPU preemption problems
As mentioned above, when we make a large number of time-consuming tasks asynchronous, we will grab the CPU. In this case, we focus on the blue part of the thread execution status, indicating that the current task can be executed, but the CPU is not.
So to understand what I’m going to assume here, the HttpTask and AccountTask tasks have to be executed in app Create, but if we look at the status of these two tasks, the blue line on the top of the status bar, we’re going to look at the current point in time in the CPU state at the top, Which tasks the CPU is allocated to, and why the current task is not executed. Once the analysis is complete, our focus is to ensure the execution status of the important tasks. Here are two ways
1 Increase the EXECUTION time slice of CPU allocation You can increase the priority of important task threads to increase the execution time slice of CPU allocation
android.os.Process.setThreadPriority(-10);
Copy the code
2 Separate tasks and execute them
Split tasks into phases for execution
3 Do not start sub-processes during startup. Sub-processes share CPU resources, which causes CPU tension in the primary process
conclusion
Startup optimization summary is the three axe, asynchronous, lazy loading and preloading. Need to use tools to play the three axe reasonably, to achieve the kaleidoscope, million change does not leave its ancestor.
This chapter mainly summarizes the use of tools to solve the problem of stalling and thread scheduling, analyzes the mainstream business problem solutions, and will share some solutions for the optimization, governance and protection of black technology.
Thank you very much for reading this article, if it can help you, help to follow Github, share knowledge together.