1. 启动perfetto关联进程
perfetto依赖traced、traced_probes等进程,首先检查这两个进程是否已经启动,如果没有启动,则设置属性adb shell setprop persist.traced.enable 1启动这些进程。
2. perfetto配置
(1)模式选择
Stop when full模式:当缓存的数据大小超过In-memory buffer size,或者抓取时长超过Max duration,都会自动停止抓取
Ring buffer模式:当时长超过Max duration时会自动停止抓取,当数据大于In-memory buffer size时会循环覆盖前面的数据
Long trace模式:每隔Flush on disk every时长,就会将内存中的数据缓存到Flash上,文件最长Max file size;内存中的数据达到In-memory buffer size时还没有缓存到Flash上,会循环覆盖;超时Max duration后,停止抓取。
(2)atrace配置
以下界面中选取要抓取哪些模块的atrace
(3)trace event配置
如下界面中选取抓取哪些模块的atrace,并且配置ring buffer的大小,和间隔多久去读取ring buffer。
(4)保存配置文件
Saved configs中把配置内容保存到指定文件。
3. perfetto抓trace命令
(1)把配置文件push到手机,要放到shell用户有权限访问的目录,一般放在/data/misc/perfetto-configs/
(2)抓取trace
adb shell perfetto --txt -c /data/misc/perfetto-configs/xxx.pbtx -o /data/misc/perfetto-traces/trace_file.perfetto-trace
(3)离线抓取
adb shell perfetto --txt -c /data/misc/perfetto-configs/xxx.pbtx -o /data/misc/perfetto-traces/trace_file.perfetto-trace --detach=perf_debug
离线抓取可以主动停止adb shell perfetto --attach=perf_debug --stop
4.perfetto大文件解析
抓取到的文件在网页Perfetto UI上完成可视化,当trace文件大于1G时,浏览器会提示文件过大,并提供了解决办法。但是这个解决办法只适用于Linux开发环境。
在windows上需要下载windows-amd64.zip,下载链接为https://github.com/google/perfetto/releases/,选择最新的版本下载。
运行命令:trace_processor_shell.exe --httpd trace路径
trace文件load完毕后,打开perfetto网站,会自动弹出如下提示,选择即可