CSDN GitHub Blog zhihu The Denver nuggets
Purple night – Green ling lane grass debug/tools/perfetto OS Kernel Laboratory 358956093 6978869597248159757


This work is licensed under creative Commons Attribution – Non-commercial Use – Same way Share 4.0 International license, please indicate the source of reproduction, thank you for your cooperation

Because my technical level and knowledge is limited, if there is a flaw or need to correct the content, welcome you to correct, but also welcome you to provide some other good debugging tools for inclusion, I thank you here


The date of update
2021/03/20 Explain information security in section 1.3

1 Perfetto overview


1.1 Why Perfetto


The Perfetto tool is the next generation of The Unified Trace collection and analysis framework for Android devices running on Android 9.0(API level 28) or later. The System Tracing App can be used to record System Tracing on the device, and trace information of the platform and App can be captured

Although Perfetto was created to replace Systrace, systrace will still exist for historical reasons, and the trace files that Perfetto fetches can also be converted to Systrace views.

If you’re comfortable with systrace, you can use the Open with Legacy UI of Perfetto UI to convert it to the Systrace view

1.2 Perfetto advantages


  1. Supports whole-system tracing on Android and Linux, can capture trace online for a long time (up to hours), subsystem tracing processor is designed to efficiently save hours of trace data to local, It also exposes SQL query interface to support SQL query based on popular SQLite query engine. So you can turn it on in the background and let it grab trace all the time, especially if you have a very low probability of recurrence and a very serious performance problem.

  2. Perfetto has good scalability. In addition to supporting standard Tracepoints (such as CPU scheduling information, memory information, etc.), it can also listen to a variety of system information, such as procFS and SYSFS interface, etc. In Android P, Google has added a new Atrace HAL layer. The Atrace process can call the HAL interface to retrieve current extension information, such as adding statistics for recording battery and power usage. The execution path of the program. The code can be submitted to Google so that if tracepoints need to be extended, they can be added in accordance with the graphic example.

  3. Provides the new Perfetto UI site for open tracing and processing locally through the browser, without any server-side interaction. The command for capturing trace can be automatically generated by selecting the switch above and the trace file can be opened at the same time. Several predefined trace analysis statistical tools are also integrated, as can be seen from the Metrics and Auditors options

Perfetto itself is a framework. For details about its architecture and modules, you can refer to its doc website. The Source code of Perfetto can be found in the /external/ Perfetto directory of the Android Source Tree. Configuration and scripts can be used directly.

1.3 Information security issues


Note:

This article introduces the Perfetto tool, but

  1. Data analysis with Perfetto, whether the data will be sent back to Google server, has not been verified;

  2. The logs that Perfetto captures do contain some information about the machine;

  3. Perfetto’s data storage format cannot be directly parsed, and the information involved is unparsed;

So if it’s classified data (i.e. logs you grab on an unreleased machine/server/development board/phone/other end product).

  1. There is no data to prove that the data will not be collected and analyzed.

  2. Therefore, exercise caution when using devices or data that involve information security.

Update 2021/03/20 (I have been struggling with whether to update this content or not. When DISCUSSING perfetto with my colleagues some time ago, this content was also mentioned when talking about information security issues, so I update it today to explain my own understanding). I found the following description in the document of Google Perfetto earlier:

The Perfetto UI works fully offline after it has been opened once. Traces opened with the UI are processed locally by the browser and do not require any server-side interaction.
Copy the code

As mentioned above, the entire Perfetto UI runs completely offline after being opened once, and the processing of trace data is done locally without making any server requests.

You can see that Perfetto UI is really not a local url. Despite the above statement, in view of the consistent environment and situation of information security at home and abroad, it is recommended that you be cautious if your environment and data involve privacy information. After all, not being evil is not really not being evil.

Outside the chain picture archiving failure, the source station might be hotlinking prevention mechanism, proposed to directly upload picture preserved quubzhx (img – 5-1616169787582) (raw.githubusercontent.com/gatieme/LDD…)”

For details, see Perfetto-system profiling, App Tracing and Trace Analysis.

Of course, PerFetto also provides a way for you to build your own services, see build-instructions. If you are worried about information leakage, it is better to use your own service. Later, I also considered writing a technical post on my own Peretto service when I had time.

The blogger here reminds that if the problems caused by careless use, the blogger does not assume any responsibility.

Since I do not know much about network security, I also welcome those who are willing to take a look at the network requests during the use of Perfetto to confirm whether the current version has data returned.

Thank you. I’m not grateful to be here.

2 Using Perfetto on Android


2.1 can make perfetto


Because Perfetto has a service framework, to catch traces, you need to run both traced(session daemons) and traced_probes(probes and ftrace-interop daemons).

By default, these services are not enabled. Check to see if both processes are running on the phone.

adb shell "ps -ef | grep -E "traced|traced_probes" | grep -v grep"
Copy the code

If neither of these services are running, you can use the following command to enable Perfetto

adb shell setprop persist.traced.enable 1
Copy the code

2.2 grab trace


Like Systrace, Perfetto gives us two ways to fetch trace logs.

  1. Use the logging page in the Perfetto UI for Quickstart: Record Traces on Android.

  2. Use the Perfetto command line interface.

We naturally use perfetto CLI to fetch.

Parameters using describe
–out Used to specify the trace output file
–config To specify the configuration, such as how long to capture, how often to write memory data back to a file, which tracepoints to capture, etc., the contents of the config file can be written manually, or can be generated using the Perfetto UI website
adb shell perfetto --config :test --out /data/misc/perfetto-traces/trace // use the built-in test configuration, and output it to /data/misc/perfetto-traces/trace
Copy the code

You can use the following command to fetch a trace file that uses test config

3 Use Perfetto on the server


3.1 compile perfetto


Download the PerFetto core code

git clone https://android.googlesource.com/platform/external/perfetto/ && cd perfetto
Copy the code

Download and extract build dependencies:

tools/install-build-deps
Copy the code

If the script fails due to an SSL error, try calling the script python3 tools/install-build-deps or upgrading your OpenSSL library.

Generate all the most common GN build configurations:

tools/build_all_configs.py
Copy the code

Build the Linux trace binary (on Linux, it uses the sealed Clang toolchain, downloaded as part of Step 2) :

tools/ninja -C out/linux_clang_release traced traced_probes perfetto
Copy the code

This step is optional when using the handy script under Tools/TMUx.

3.2 use perfetto


Perfetto is a command line tool executed in a shell environment. It relies on two service processes running in the system to get their work done: Traced and Traced_probes.

Android automatically executes both traced(session daemons) and Traced_probes (probes and ftrace-interop daemons) by enabling the Perfetto service. But in Linux we have to start both services manually.

3.2.1 One-click TMUX Scripts


Perfetto provides us with tools/tmux scripts that do similar things on Android, starting the service process and setting up a work panel.

You can run the following command to capture the 10S scheduling logs using tools/tmux scripts

OUT=out/linux_clang_release CONFIG=test/configs/scheduling.cfg tools/tmux -n
Copy the code

When the cli tool is running, you need to make a config file. By default, Perfetto provides multiple configuration templates in the test/configs directory in the repository path. Above we used the scheduling configuration provided by default. We can also customize the config or generate the config using the Perfeto UI.

The script first copies the required service program and the perfetto CLI tool and its dependent libraries to the TMP directory/TMP /perfetto.xxxxxx. Then use TMUx to open a TMUX window with three panels that are started from top to bottom: The Traced, Traced_probes and Perfetto log fetching workbench. In the bottom perfetto workpanel, we have prefilled the fetch perfetto command, we just need to enter to fetch the 10S scheduling log.

[this in the original image, moving beyond the size can’t upload) (raw.githubusercontent.com/gatieme/LDD…)”

We can use Ctrl-b D to exit the TMUX session or use tMUx attach -t demo to reconnect the tMUx session. Tmux kill-session -t demo. For more information on tMUx, see another blog from tMUx, Terminal Tools for Linux —- tMUx. Note do not use the configuration files provided in the TMUx blog, which are rebound with shortcut keys, or you may need to modify the TMUx script or configuration files.

The script will trace the log information in binary format to the protobuf, refer to TracePacket

3.2.2 Manually Running the Service


After running it with TMUx earlier, we know exactly which services were started and which commands perfetto fetched.

Manually start the service background, then run perfetto, specify config, and fetch the trace log.

As follows:

cd out/linux_clang_release ./traced_probes & ./traced & ./perfetto --txt -c .. /.. /test/configs/scheduling.cfg -o traceCopy the code

4 Some tricks of Perfetto


4.1 Customizing Config


The most convenient way to generate a configuration file is to use the Perfetto UI website to help generate it. Click on Record New Trace and you’ll see a lot of configuration interfaces. Select the tracepoints you want and then trace Command. Copy the command content and execute it directly on the terminal.

4.2 Querying and Analyzing Logs using SQL


The subsystem TraceProcessor provides Perfetto with the ability to query and analyze data based on SQLite. Also provided as a separate executable file: trace_Processor.

For more information on this section, see Quickstart: SQL-Based Analysis and Trace-based Metrics

4.3 Trace Format conversion tool


Perfetto provides a compatible UI to open the original Systrace. See catapult_trace_viewer. After all, it is intended to replace Systrace, so there are some problems with using it, and you can’t open too many Systrace files.

However, Perfetto also provides some methods that let you convert your captured logs to systrace format.

  • The provided Traceconv tool converts Perfetto traces to other trace formats.

  • Tap the Legacy UI of the Perfetto interface to convert your logs to systrace format and open them with catapult_trace_Viewer. You can click Save to save your Systrace log.

5 Reference Materials


Introduction to the Perfetto tool

Learn perfetto(2) — generate perfetto trace

Perfetto use

android-app-performance-analysis-with-perfetto

perfetto docs

(7) The next Android performance optimization Perfetto detailed introduction


  • This work/blog (AderStep- Purple Night Appendix – Qingling Lane Grass Copyright ©2013-2017), by Cheng Jian (Gatieme).

  • usingCreative Commons Attribution – Non-commercial Use – Same way Share 4.0 International LicenseWelcome to reprint, use and re-publish the article, but be sure to keep the bylineAs thou dost gatieme(Includes links to:blog.csdn.net/gatieme) shall not be used for commercial purposes.

  • Any work modified based on this article must be distributed under the same license. If you have any questions, please contact me.