Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use TraceView performance Analysis tool in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use TraceView performance analysis tool in Android, for this problem, this article details the corresponding analysis and solution, hoping to help more friends who want to solve this problem find a simpler and easier way.

What is TraceView?

TraceView is a built-in tool in Android SDK. It can load trace files and display the execution time, times and call stack of code in the form of graphs for our analysis.

Generating trace files

A trace file is a type of log information file that can be generated by code, Android Studio, or DDMS.

Generate trace files using code

Call Debug.startMethodTracing("sample") where you want to log, specifying the name of the trace file.

Call Debug.stopMethodTracing() at the end of the recording and the file will be saved to the/sdcard/Android/data/packageName/files folder.

Debug.startMethodTracing("sample"); //start trace

...

Debug.stopMethodTracing(); //End trace

Trace files can be exported using the adb command and opened using Android Studio Profiler or DDMS.

Generate trace files using Android Studio

Click Profiler in the toolbar (Android Studio version 3.4.2), click anywhere on the CPU Timeline to open CPU Profiler.

1. Event Timeline: Shows how an Activity in an app transitions through various states over its lifecycle and indicates user interaction with the device, including screen rotation events.

2. CPU Timeline: Displays the app's real-time CPU usage and the total number of threads the app is currently using. Historical CPU usage data can also be examined by moving the mouse along the horizontal axis of the timeline.

3. Thread Activity Timeline: All threads of the application process. Different colors have different meanings:

Green: Indicates that the thread is active or ready to use the CPU. That is, it is "running" or in a "runnable" state.

Yellow: Indicates that the thread is active, but it is waiting for an I/O operation (such as disk or network I/O) before it can complete its work.

Gray: Indicates that the thread is sleeping and consuming no CPU time. This happens occasionally when threads need access to resources that are not yet available. The thread goes into autonomous sleep or the kernel puts the thread to sleep until the required resources are available.

To start logging trace data, click on the drop-down box at the top of CPU Profiler to select the appropriate logging configuration:

Sampling Java methods: Frequent capture of an application's call stack during execution of its Java code. The analyzer compares the captured data sets to derive time and resource usage information about the Java code execution of the application.

Trace Java methods: Detect applications at runtime to record a timestamp at the beginning and end of each method call. These timestamps are collected and compared to generate method tracking data, including time information and CPU usage.

Sampling C/C++ functions: Capture sample trace data for native threads of an app.

After selecting the configuration, click Record to track, and click Stop to end the data tracking after the interaction is completed. The analyzer analyzes trace data, as shown in the following figure.

1. Select Time Range: Determines which part of the recorded time range you want to examine in the trace pane. When a function trace is first recorded, CPU Profiler automatically selects the full length in the CPU timeline. If you want to examine function trace data for only a small portion of the recorded time range, you can click and drag the edge of the highlighted area to modify its length.

2. Timestamps: Used to indicate the start and end times of the recorded function trace (relative to the time the analyzer started collecting CPU usage information from the device). You can click on the timestamp to automatically select the complete record.

3. Trace pane: Used to display function trace data for the selected time range and thread.

4. Trace Data pane tab: Displays function traces in the form of Call Chart, Flame Chart, Top Down tree, or Bottom Up tree.

Call Chart : The horizontal axis represents the time of the function call (or caller), and its callee is displayed along the vertical axis. Function calls to system APIs are shown in orange, calls to app-owned functions are shown in green, and function calls to third-party APIs (including Java language APIs) are shown in blue.

Flame Chart: An inverted invocation chart where the horizontal axis no longer represents a timeline, it represents the relative execution time of each function.

Top Down: Displays a list of function calls in which expanding the function node displays the callee of the function.

Bottom Up: Displays a list of function calls in which expanding the function node displays the caller of the function.

5. Time reference menu: Determine how to measure the time information for each function call:

Wall clock time: actual elapsed time.

Thread time: The actual elapsed time minus the time the thread is not consuming CPU resources.

Using DDMS to generate trace files

DDMS, Dalvik Debug Monitor Server, is an Android debugging monitoring tool that provides us with screenshots, view logs, view hierarchies, view memory usage and other functions.

After Android Studio 3.0, you can find monitor.bat in the tools directory of the Android SDK, use the command line to start it, and you can open DDMS.

DDMS interface Click Start Method Profiling button to start trace recording, the same button to stop trace. DDMS will automatically enable TraceView to load trace files, as shown below:

The upper part of the figure shows the execution time of different threads, where different colors represent different methods, the longer the same color, the longer the execution time, and the blank indicates that there is no execution content in this time period.

The lower half shows the execution time information for the different methods. Meaning of each indicator:

Incl Cpu Time: CPU time consumed by the method (including time spent calling subfunctions).

Excl Cpu Time: CPU time consumed by the method itself (excluding the time spent calling other methods).

Incl Real Time: The real time the method runs (including the time spent calling subfunctions).

Excl Real Time: The real time the method itself runs (excluding the time spent calling other methods).

Calls+RecurCalls/Total: The number of times the method was called + the number of times it was repeated.

CPU Time/Call: The ratio of CPU time to the number of method calls, which is equivalent to the average method execution time.

Real Time/Call: Similar to CPU Time/Call, but in real time.

There are generally two situations when analysis takes time:

Not many calls. However, it is very time-consuming in itself.

Not very time-consuming in itself. But calls are frequent.

In the first case, you can use Cpu Time to see how long it takes.

In the second case, you can use Calls+RecurCalls/Total to see how it is called.

About Android how to use TraceView performance analysis tool questions to share here, I hope the above content can have some help for everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report