文档模板
= Android性能分析工具--Perfetto
:keywords: adoc, example
:author: Jack
:email: weijaky@gmail.com
:revnumber: v1.0
:revdate: 2022-12-20
:stem: latexmath
:icons: font
:source-highlighter: coderay
:sectnums:
:sectlinks:
:sectnumlevels: 4
:toc:
:toc-title: 目录
:toclevels: 3
:doctype: article
//图片默认目录
:imagesdir: ./images
// 图标默认目录
:iconsdir: ./icons
// CSS样式默认目录
:stylesdir: ./styles
== 序言
本文将从启动速度、内存、卡顿、电量、流量、瘦身等多个维度进行原理剖析及解决实战。
[plantuml, hello]
----
A --> B : call
B --> A : replay
----
== 什么是Perfetto
Perfetto 是 Android 10 中引入的全新平台级跟踪工具。这是适用于 Android、Linux 和 Chrome 的更加通用和复杂的开源跟踪项目。
=== 和Systrace的关系
与 Systrace 不同,它提供数据源超集,可以用 protobuf
编码的二进制流形式记录任意长度的跟踪记录。总而言之,可以将Perfetto理解为sysyrace的升级版,
用在更新的平台、新图表展示更多的信息。
同时在android系统中,“系统跟踪”应用是一款用于将设备活动保存到跟踪文件的 Android 工具。
在搭载 Android 10(API 级别 29)或更高版本的设备上,跟踪文件会以 Perfetto 格式保存。
在搭载较低版本 Android 系统的设备上,跟踪文件会以 systrace 格式保存。
在兼容性上,考虑到一些历史原因,Perfetto工具抓取的trace文件也可以转换成sysytrace视图,
习惯用systrace的也可以用Perfetto的Open with legacy UI来完成转换功能。
=== 相比Systrace的优势
* *UI:* perfetto的操作和systrace的操作类似,界面UI更加美观,进程相关信息显示更全。
* *流畅性:* 打开较大的trace文件比systrace更流畅一些。
* *线程:* 如果你对某些线程感兴趣,可以置顶放在一起分析。看线程被谁唤醒很方便。
* *Binder:* Binder跨进程点击跳转的跟踪更加⽅便。
== 如何使用Perfetto
=== 常规操作
要想使用perfetto,主要执行命令:
[source, bash]
----
#1 设置属性
adb shell setprop persist.traced.enable 1
#2 信息收集perfetto命令
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
#3 pull出 trace文件
adb pull /data/misc/perfetto-traces/trace_file.perfetto-trace .
----
生成的trace文件可以用网页版的Perfetto UI直接查看。
Perfetto可以在浏览器中可以直接打开,网址为:
http://ui.perfetto.dev[Perfetto UI],点进后是这样:
image::image-2022-12-20-22-46-17-647.jpg[Perfetto界面]
打开trace文件后效果如下所示:
image::image-2022-12-20-23-50-14-845.jpg[]
基本操作和sysytrace几乎全部通用。
== perfetto关于内存调试文档整理
. 调试内存可参照文档:Android 上调试内存使用情况 Perfetto Tracing文档(Chrome/中文翻译)
. 内存事件和计数器相关:counters and events-Perfetto Tracing文档(Chrome/中文翻译)
. native heap调试相关:Native heap profiler-Perfetto Tracing文档(Chrome/中文翻译)
. java heap调试相关:java heap profiler-Perfetto Tracing文档(Chrome/中文翻译)
== 总结
perfotto功能主要涉及 数据源、应用检测、痕迹分析、追踪可视化等功能。
----
Perfetto provides also a brand new trace visualizer for opening and querying hours-long traces, available at ui.perfetto.dev. The new visualizer takes advantage of modern web platform technologies. Its multi-threading design based WebWorkers keeps the UI always responsive; the analytical power of Trace Processor and SQLite is fully available in-browser through WebAssembly.
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.
----