Perfetto入门

Perfetto 入门

Perfetto 概述

Perfetto 是 Google 从 Android10 开始引入的一个全新的平台级跟踪分析工具。适用于 Android、Linux 和 Chrome 的更加通用和复杂的用于性能检测和跟踪分析的生产级开源项目。在 Android 系统中对性能分析是尤为重要的一部分,仅从 Logcat 中进行时间分解太繁琐,Android 系统中可以通过 trace 来分析性能。

Perfetto 提供了用于记录系统级和应用级活动的服务和库、低开销的 native+java 内存分析工具,可供 SQL 分析跟踪文件的库,以及一个基于 Web 用于将追踪文件可视化方便分析的 Perfetto UI。相比 systrace 的优势:

Perfetto 是很好的性能分析工具,发展很快。它不仅可以看 systrace,也可以将 logcat 也一起提取出来,方便与其他信息放在同一个时间轴上去分析。它也可以分析 memory 问题,如 iondma_buf 泄露。

Perfetto 整体

Perfetto 是 Google 开源的一套性能检测和分析框架。按照功能可分成 3 大块,Record traces(采集)、Analyze traces(分析)、Visualize traces(可视化)。

image.png|1000

Record traces

Trace 采集能力,支持采集多种类型的数据源,支持内核空间和用户空间数据源。

内核空间数据源是 Perfetto 内置的,需要系统权限,主要的数据源包括:

用户空间数据采集,Perfetto 提供了统一的 Tracing C++ 库,支持用户空间数据性能数据收集,也可用 atrace 在用户层添加 Trace 收集代码采集用户空间 Trace。

Analyze traces

Trace 分析能力,提供 Trace Processor 模块可以把支持的 Trace 文件解析成一个内存数据库,数据库实现基于 SQLite,提供 SQL 查询功能,同时提供了 python API,可基于 Trace Processor 开发了一套 Trace 自动化分析工具集。

Visualize traces

Perfetto 还提供了一个全新的 Trace 可视化工具,工具是一个网站:https://ui.perfetto.dev/ 。在可视化工具中可导入 Trace 文件,并且可使用 Trace Processor 和 SQLite 的查询和分析能力。

Trace Processor

Perfetto 提供了强大的 Trace 分析模块:Trace Processor,把多种类型的日志文件(Android systrace、Perfetto、linux ftrace)通过解析、提取其中的数据,结构化为 SQLite 数据库,并且提供基于 SQL 查询的 python API,可通过 python 实现自动化分析。

为提高效率,需基于 Trace Processor 的 python API,开发一套 Trace 自动分析工具集,实现快速高效分析版本启动劣化问题。

Perfetto 使用

命令行

开启 traced 权限

# 设置 traced 权限
adb shell setprop persist.traced.enable 1

# 验证是否开启了traced 权限
adb shell
getprop persist.traced.enable
ps -A|grep -i trace

image.png|700

perfettor 格式

adb shell perfetto [ --time TIMESPEC ] [ --buffer SIZE ] [ --size SIZE ]
    [ ATRACE_CAT | FTRACE_GROUP/FTRACE_NAME]...

Perfetto 使用步骤

1、输入命令

adb shell perfetto -o /data/misc/perfetto-traces/trace_file.perfetto-trace -t 10s sched freq idle am wm gfx view binder_driver hal dalvik camera input res memory

image.png|700

其中:

2、将 trace 拷贝到电脑:

adb pull /data/misc/perfetto-traces/trace_file.perfetto-trace .

3、浏览器打开:Perfetto UI

4、通过 Perfetto UI 打开 trace

5、分析 trace

Perfetto UI

Perfetto 录制设置

Perfetto UI 录制界面

image.png|2000

Recording settings

三种录制设置,适用不同的使用场景

Stop when full

image.png|500

Ring buffer

image.png|500

该模式只会收到 Max duration 的影响,时间到了就停止抓取 trace,但是 trace 会有被 overwrite 的风险。

Long trace

image.png|500

用于长时间地抓取 trace,但是由于需要定时将 buffer 中的 trace 写到文件里面去,会有 IO 的影响。

Recording command

配置汇总和执行,完成相关参数配置后,可以在改选项中获得所有的配置参数,拷贝命令,直接 adb 开启录制追踪。

image.png|2000

在 shell 中可以直接复制配置文件通过 adb 设置到到设备中,可以手动修改 duration_ms 参数来修改抓取 trace 时间。

或者不使用配置文件。通过命令行方式执行,但需要手动键入需要抓取的模块。

adb shell perfetto -o /data/misc/perfetto-traces/trace_file.perfetto-trace -t 20s \
sched freq idle am wm gfx view binder_driver hal dalvik camera input res memory

Probes

CPU

CPU 相关的常见功能统计

image.png|500

image.png|500

GPU

记录 GPU 的主频和内存使用

image.png|500

image.png|1000

image.png|1000

Power

image.png|500

Memory

image.png|500

Android apps & svcs

image.png|500

image.png|600

Chrome

如果需要分析 Webview 相关的性能问题,可以开启该选项的相关功能

image.png|500

Stack Samples

image.png|500

Advanced settings

目前只有一个开启 ftrace 功能,用于分析内核性能问题,可以选择相应的 tag 进行记录

image.png|500

查看 trace 报告

主界面

红框部分就是配置文件中自行配置的需求:

image.png|2000

基本操作方法

快捷键

基本操作通过 "W"、"S"、"A"、"D" 实现,英文键盘下,w 可以实现对 trace 范围的放大,s 可以缩小范围,a 整体左移,d 整体右移。

或者按住 ctrl 键 + 鼠标齿轮

线程操作

image.png|900

image.png|2000

image.png|2000

通过对每个线程进行精准标注,就可以知道每个操作之间的耗时,以点击拍照到拍照完成为例,deliverInputEvent 表示点击时触摸按下到抬起的时间,耗时在 116.9ms 左右,触摸抬起后启动拍照流程耗时 128.5ms,最后拍照完成 still capture 耗时 377.9ms。

image.png|2000

image.png|2000