This paper introduces some methods and skills of applying statistics of startup time analysis.

“Wind” solution falls three autumn leaves, can open February flowers. Thousands of feet across the river, into the bamboo rod inclined. – Li Jiao

Use system tracking to obtain reports

There is a new system-level application called system tracker for Android 9.0 and above. This is similar to the systrace command line tool and is used to replace the systrace tool. In addition, reports generated by the systrace tool cannot be opened after chrome80. You can do this in the developer optiondebuggingSection found, can be set to display in the shortcut drop-down menu. Ps: Some phone manufacturers have eliminated this feature, you can use Pixel or Xiaomi phones to get results.You can select the category you are interested in, click the newly displayed icon to start tracking, and click again to end tracking.

How does the Release package use Trace

The beginSection and endSection provided by Trace can add custom events, but they will only be enabled in the Debug package. Since the performance of the Debug package and the Release package is very different, it is necessary to enable Trace tracing in the Release package. The method of setting the switch is as follows: Trace.java

    /**
     * Set whether application tracing is allowed for this process.  This is intended to be set
     * once at application start-up time based on whether the application is debuggable.
     *
     * @hide
     */
    @UnsupportedAppUsage
    public static void setAppTracingAllowed(boolean allowed) {
        nativeSetAppTracingAllowed(allowed);

        // Setting whether app tracing is allowed may change the tags, so we update the cached
        // tags here.
        cacheEnabledTags();
    }
Copy the code

The @unsupportedAPpusage annotation is added, so we cannot call it directly in the application. According to the annotation, the system calls this method to set the switch according to whether the application is DEBUG, so we can use reflection to enable Trace in the release package, which can be obtained in the code below.

Asynchronous event tracking

BeginSection and endSection provided by Trace must be paired on the same thread. Android10.0 adds the ability to track asynchronous events by simply passing in the same methodName and cookie at the beginning and end.

public static void beginAsyncSection (String methodName, int cookie)
public static void endAsyncSection (String methodName, int cookie)
Copy the code

This adds a new line to the result of the trace:If you want to use this feature on phones lower than 10.0, you can also use reflection:

public class TraceUtil { private static boolean ENABLE = false; private static Class cTrace; public static void enable(boolean enable) { if (! enable) { return; } ENABLE = true; try { cTrace = Class.forName("android.os.Trace"); cTrace.getDeclaredMethod("setAppTracingAllowed", Boolean.TYPE).invoke(null, Boolean.TRUE); } catch (Throwable th) { th.printStackTrace(); } } public static void asyncTraceBegin(String name, int session) { if (! ENABLE) { return; } try { cTrace.getDeclaredMethod("asyncTraceBegin", new Class[]{Long.TYPE, String.class, Integer.TYPE}).invoke(null, new Object[]{Long.valueOf(4096), name, Integer.valueOf(session)}); } catch (Throwable th) { th.printStackTrace(); } } public static void asyncTraceEnd(String name, int session) { if (! ENABLE) { return; } try { cTrace.getDeclaredMethod("asyncTraceEnd", new Class[]{Long.TYPE, String.class, Integer.TYPE}).invoke(null, new Object[]{Long.valueOf(4096), name, Integer.valueOf(session)}); } catch (Throwable th) { th.printStackTrace(); }}}Copy the code

Perfetto is used to analyze the results

After obtaining the result using system trace, you can use adb to pull it to your computer and then use Perfetto(ui.perfetto.dev/#! / viewer) to play…

 adb pull /data/local/traces/ .
Copy the code

With the synchronous and asynchronous custom event capability provided by Trace, you can quickly analyze the specific cause of time consumption.

For example, the main thread sleep in the figure above, after checking the code, the main thread is locked, causing the main thread sleep.

The SQL query

Perfetto provides a very powerful SQL query function, you can use SQL statements to query the data you want. Because process ids are reusable in Linux, Perfetto provides a unique UPID. Query app upID by process name:

SELECT upid,name FROM process
WHERE name='com.example.android'
Copy the code

Query all threads by upID:

SELECT * FROM thread
WHERE upid=175
order by name
Copy the code

Startup Time Statistics

After analyzing and optimizing the code, it is necessary to quickly produce the time consuming data. You can use the following shell script to batch and quickly produce the time consuming data fully displayed on the startup page:

for i in `seq 1 20`
do
  adb shell am force-stop com.example.android
  sleep 1
  adb shell am start-activity -W -n com.example.android/.LaunchActivity | grep "TotalTime" | cut -d ' ' -f 2
done
Copy the code

The results are then copied into the table to count the average time consuming results.

conclusion

This article showed you how to enable Trace and custom asynchronous events with the Release package, analyze the elapsed time using Perfetto, and finally count the elapsed time using adb commands.

reference

  • Mp.weixin.qq.com/s/ohKG7i5tt…
  • Developer.android.com/topic/perfo…

Follow me by searching “yuweiguocn” on wechat