-
Android perfetto 简介
使用 perfetto
工具,您可以通过 Android 调试桥 (adb) 在 Android 设备上收集性能信息。使用 adb shell perfetto ...
命令调用 perfetto
工具。 perfetto
从您的设备上收集性能跟踪数据时会使用多种来源,例如:
-
使用
ftrace
收集内核信息 -
使用
atrace
收集服务和应用中的用户空间注释 -
使用
heapprofd
收集服务和应用的本地内存使用情况信息
-
Perfetto 系统框图介绍和运行原理
Perfetto 是一个高级的开源工具,专为性能监测和分析而设计。它配备了一整套服务和库,能够捕获和记录系统层面以及应用程序层面的活动数据。此外,Perfetto 还提供了内存分析工具,既适用于本地应用也适用于 Java 环境。它的一个强大功能是,可以通过 SQL 查询库来分析跟踪数据,让你能够深入理解性能数据背后的细节。为了更好地处理和理解大规模数据集,Perfetto 还提供了一个基于 Web 的用户界面,使你能够直观地可视化和探索多 GB 大小的跟踪文件。简而言之,Perfetto 是一个全面的解决方案,旨在帮助开发者和性能工程师以前所未有的深度和清晰度来分析和优化软件性能。
Perfetto 是完全兼容 Systrace 的。你之前抓的 Systrace 文件,可以直接扔到 Perfetto Viewer 网站里面直接打开。如果你还没有适应 Perfetto ,你也可以从 Perfetto Viewer 一键打开 Systrace。
下图是 Perfetto 的架构图,可以看到 Perfetto 包含了三大块:
-
Record traces :即数据抓取模块,可以看到抓取的内容和来源非常丰富,Java、 Native 、Linux 都有涉及到,相比 Systrace 要丰富很多。
-
Analyze traces :主要是 trace 分析模块,包括 Trace 解析、SQL 查询、Metrics 分析等,这部分有专门的命令行工具提供,方便大家直接调用或者在工具链里面去调用。
-
Visualize Traces:Trace 的呈现、抓取等
Perfetto 工具就提供了这样的一个上帝视角,通过上帝视角我们可以看到 Android 系统在运行时的各个细节,比如
-
Input 事件是怎么流转的
-
你正在使用的 App 的每一帧是怎么从 App 产生到上屏的
-
CPU 的实时频率、负载、摆核、唤醒等
-
系统中的各个 App 是怎么运行的
-
....
App 开发者和 Android 系统开发者也都会在重要的代码逻辑处加上 Trace 点,打开部分 Debug 选项之后,更是可以得到非常详细的信息,甚至一个 Task 为什么摆在某个 cpu 上,都会有详细的记载。通过这些在 Perfetto 上所展示的信息,我们能初步分析到性能问题的原因,接下来继续分析就会有针对性
-
perfetto 工具使用方法
3.1、使用perfetto抓取trace
atrace --list_categories 列出抓取的trace策略 示例: 1)perfetto抓trace文件 配置 atrace 类别列表。 adb shell perfetto gfx view wm am sm hal res rs ss -o /data/misc/perfetto-traces/trace -t 5s 或 adb shell perfetto gfx view wm am hal rs ss aidl power -o /data/misc/perfetto-traces/trace --app app包名 -t 5s 或 adb shell perfetto gfx input view wm am sm hal res rs ss aidl sched irq freq load workq binder_driver thermal -o /data/misc/perfetto-traces/trace -t 10s 2)导出trace文件 adb pull /data/misc/perfetto-traces/trace .
3.2、分析trace文件工具
Google Chrome浏览器打开https://ui.perfetto.dev 1)打开https://ui.perfetto.dev/网页后,点击open trace file,选择刚才抓取的trace文件。 2)搜索trace关键字 -> f -> shift + m标记 例如搜索:activityConfigChanged。 或者搜索自己添加的RootWindowContainer_onDisplayChanged等等等 示例: 1)在搜索框输入:RootWindowContainer_onDisplayChanged 点击左右箭头跳到上一处或下一处该关键字出现的地方。 2)点击f键会滚动并放大到刚才选择关键字的地方 3)点击m临时标记或者shift + m永久标记
3.3、添加自己的trace方法
1,framework层: import android.os.Trace; Trace.traceBegin(long traceTag, String methodName) Trace.traceEnd(long traceTag) 在代码中必须成对出现,一般将traceEnd放入到finally语句块,另外,必须在同一个线程。 例如: Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastReceiveReg"); ... Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); 2,app层 import android.os.Trace; android.os.Trace.beginSection("lqy111"); Log.d("systrace_log_for_app_lqy111"); android.os.Trace.endSection(); 3,native framework层 #define ATRACE_TAG xxx // xxx和当前相关代码有关 #define ATRACE_TAG ATRACE_TAG_GRAPHICS #include<utils/Trace.h> ATRACE_CALL(); ATRACE_NAME("lqy111"); //#define ATRACE_CALL() ATRACE_NAME(__FUNCTION__) 或 ATRACE_BEGIN("lqy111"); ... ATRACE_END(); 或 #include <gui/TraceUtils.h> ATRACE_FORMAT("onMessageInvalidate %s", "hahaha"); 示例:AOSP在SurfaceFlinger.cpp添加的trace http://aospxref.com/android-12.0.0_r3/xref/frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp #define ATRACE_TAG ATRACE_TAG_GRAPHICS ATRACE_FORMAT("onMessageInvalidate %" PRId64 " vsyncIn %.2fms%s", vsyncId, vsyncIn, mExpectedPresentTime == expectedVSyncTime ? "" : " (adjusted)"); 示例:AOSP在ActivityConfigurationChangeItem.java添加的trace http://aospxref.com/android-12.0.0_r3/xref/frameworks/base/core/java/android/app/servertransaction/ActivityConfigurationChangeItem.java public void execute(ClientTransactionHandler client, ActivityClientRecord r, PendingTransactionActions pendingActions) { // TODO(lifecycler): detect if PIP or multi-window mode changed and report it here. Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityConfigChanged"); client.handleActivityConfigurationChanged(r, mConfiguration, INVALID_DISPLAY); Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); }
3.4、命令参数解析
下表列出了在 perfetto
的两种模式下都可使用的选项:
表 1. 可用的常规 perfetto 工具选项列表。
选项 | 说明 |
--background | -d |
perfetto 立即退出命令行界面,并继续在后台记录您的跟踪数据。 |
--background-wait | -D | 与 --background 类似,但会在退出前等待(最多 30 秒)所有数据源启动。如果收到成功的确认,则退出代码为零,否则为非零(错误或超时)。 |
--alert-id | 触发此跟踪记录的警报的 ID。 |
--config-id | 触发配置的 ID。 |
--config-uid | 已注册配置的应用的 UID。 |
--subscription-id | 触发此跟踪记录的订阅的 ID。 |
--out OUT_FILE | -o OUT_FILE |
为输出跟踪文件或 stdout 的 - 指定所需的路径。 perfetto 将输出写入上述标志中所述的文件。输出格式将用 AOSP trace.proto 中定义的格式编译。注意:您必须指定输出文件的完整路径名。通常应将这些文件写入 /data/misc/perfetto-traces 文件夹。 |
--upload | 完成后,将轨迹传递给 proto 轨迹配置中的 IncidentReportConfig 消息指定的软件包。 |
--no-guardrails | 在测试中启用 --upload 标志时,停用防止过多使用资源的保护措施。 |
--reset-guardrails | 重置 guardrails 的持久状态并退出(用于测试)。 |
--rsave-for-bugreport | 如果正在运行 bugreport_score 大于 0 的跟踪记录,则系统会将跟踪记录保存到文件中。完成后输出路径。 |
--query | 查询服务状态,并将其输出为用户可读的文本。 |
--query-raw | 与 --query 类似,但会输出 tracing_service_state.proto. 的原始 proto 编码字节。 |
--help | -h | 输出 perfetto 工具的帮助文本。 |
在轻量模式下使用 perfetto
的一般语法如下:
adb shell perfetto [ --time TIMESPEC ] [ --buffer SIZE ] [ --size SIZE ] [ ATRACE_CAT | FTRACE_GROUP/FTRACE_NAME | FTRACE_GROUP/* ]... --out FILE adb shell perfetto [ --txt ] --config CONFIG_FILE --out FILE console:/data # perfetto -h [385.873] perfetto_cmd.cc:213 Usage: perfetto --background -d : Exits immediately and continues tracing in background --config -c : /path/to/trace/config/file or - for stdin --out -o : /path/to/out/trace/file or - for stdout --upload : Upload field trace (Android only) --dropbox TAG : DEPRECATED: Use --upload instead TAG should always be set to 'perfetto' --no-guardrails : Ignore guardrails triggered when using --upload (for testing). --txt : Parse config as pbtxt. Not for production use. Not a stable API. --reset-guardrails : Resets the state of the guardails and exits (for testing). --query : Queries the service state and prints it as human-readable text. --query-raw : Like --query, but prints raw proto-encoded bytes of tracing_service_state.proto. --save-for-bugreport : If a trace with bugreport_score > 0 is running, it saves it into a file. Outputs the path when done. --help -h light configuration flags: (only when NOT using -c/--config) --time -t : Trace duration N[s,m,h] (default: 10s) --buffer -b : Ring buffer size N[mb,gb] (default: 32mb) --size -s : Max file size N[mb,gb] (default: in-memory ring-buffer only) --app -a : Android (atrace) app name ATRACE_CAT : Record ATRACE_CAT (e.g. wm) FTRACE_GROUP/FTRACE_NAME : Record ftrace event (e.g. sched/sched_switch) statsd-specific flags: --alert-id : ID of the alert that triggered this trace. --config-id : ID of the triggering config. --config-uid : UID of app which registered the config. --subscription-id : ID of the subscription that triggered this trace. Detach mode. DISCOURAGED, read https://perfett