清霜辰-博客

Android性能优化--Perfetto抓取trace

[TOC]

本文首发地址 https://blog.csdn.net/CSqingchen/article/details/128900541
最新更新地址 https://gitee.com/chenjim/chenjimblog
Perfetto 官方链接地址 https://github.com/google/perfetto/

介绍

Perfetto 是基于 Android 的系统追踪服务, Android的trace跟踪服务在 Android11(R) 之后是默认打开的,但是如果你是 Android 9 ( P ) 或者 10 ( Q ) ,那么就需要手动设置一下相应的 prop 属性。
adb shell setprop persist.traced.enable 1


使用 adb 抓取

adb shell perfetto -o /data/misc/perfetto-traces/trace_file.perfetto-trace -t 60s sched freq idle am wm gfx view binder_driver hal dalvik camera input res memory
-o /data/misc/perfetto-traces/trace_file.perfetto-trace 输出trace的路径
-t 60s 最多抓取时长,可以Ctrl+C停止
sched ... memory 要抓取相关模块
更多配置说明,参见文档:https://perfetto.dev/docs/concepts/config
使用配置文件,通过adb抓取,参考如下
adb push atrace.cfg /data/local/tmp/atrace.cfg
adb shell "cat /data/local/tmp/atrace.cfg | perfetto --txt -c - -o /data/misc/perfetto-traces/trace.perfetto-trace"
atrace.cfg 是配置信息,默认最多只抓取10秒,可以修改更长
可以Ctrl+C停止,需执行adb shell
再执行 cat /data/local/tmp/atrace.cfg | perfetto --txt -c - -o /data/misc/perfetto-traces/trace.perfetto-trace
更多配置参见 https://github.com/google/perfetto/blob/master/test/configs
然后 adb pull /data/misc/perfetto-traces/trace.perfetto-trace ,在 https://perfetto.dev/ 导入文件分析


通过 perfetto 网页抓取

打开 https://ui.perfetto.dev/#!/record
"Add ADB Device" 选择手机设备
如下图,有一些参数配置,根据自己的需要添加修改

最后右上角 "Start Recording"


直接在手机上抓取

开启开发者模式:系统设置--关于手机--连续点击。部分机型可网上搜索开启方式。
进入开发者模式,选择"系统跟踪",里面有一些相关设置类别,可以选择要抓取的数据种类
抓取的trace信息会直接存储在手机 /data/local/traces,"查看跟踪文件",可以分享
此方式比较适合没有电脑,随时抓取


使用 record_android_trace 抓取

执行如下命令,会自动抓取,并打开 https://perfetto.dev/ 分析
python record_android_trace -c atrace.cfg -o out.perfetto.trace
record_android_trace 是 perfetto 中文件,具体参见以上命令的链接
atrace.cfg 是配置信息,更多配置说明参见文档:https://perfetto.dev/docs/concepts/config
默认只抓取10秒,可以修改更长,可以Ctrl+C停止,更多配置参见 https://github.com/google/perfetto/blob/master/test/configs
首次运行会从 googleapis.com 下载文件,如果遇到超时,可能需要修改如下
Windows可参考 Android基于perfetto分析native内存泄露 放文件

diff --git a/tools/heap_profile b/tools/heap_profile
@@ -243,8 +243,9 @@ def download_or_get_cached(file_name, url, sha256):
    if needs_download:
        # Either the filed doesn't exist or the SHA256 doesn't match.
        tmp_path = bin_path + '.tmp'
+    proxy_7890='http://127.0.0.1:7890'
        print('Downloading ' + url)
-    subprocess.check_call(['curl', '-f', '-L', '-#', '-o', tmp_path, url])
+    subprocess.check_call(['curl', '-f', '-L', '-#','-x',proxy_7890, '-o', tmp_path, url])
        with open(tmp_path, 'rb') as fd:
        actual_sha256 = hashlib.sha256(fd.read()).hexdigest()
        if actual_sha256 != sha256:

熟悉 perfetto 快捷键,会有事半功倍效果


注意事项



相关文章连接
Android性能优化--Perfetto抓取trace
Android性能优化--perfetto分析native内存泄露
Android性能优化--Perfetto用SQL性能分析

当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »