[toc]
一、四种使用方式
- 命令行方式
- 1、使用 /external/perfetto/tools/record_android_trace 自动化脚本
- 需要安装python,下载python脚本工具 record_android_trace
- 2、直接使用手机上的命令行客户端工具 /system/bin/perfetto
- 1、使用 /external/perfetto/tools/record_android_trace 自动化脚本
- UI工具
- 3、通过ui.perfetto.dev/ 中的”录制“页面
- 自测在 window 不管用
- 大概因为需要基于 bash 的命令行工具
- 4、使用手机上的 ”系统跟踪“,在开发者选项里边
- 对于大部分人足够使用了,但是对于搞性能的还是使用命令行更灵活。
- 3、通过ui.perfetto.dev/ 中的”录制“页面
二、推荐的使用方式:命令行客户端 /system/bin/perfetto
-
命令行客户端有两种模式:
-
轻量模式
- 使用 atrace 和 ftrace 作为数据来源。类似于 systrace 接口
-
通用模式
- 从协议缓冲区获取其配置,并且可以让您使用 atrace 和 ftrace 之外的数据源
-
注:本篇文章介绍的使用步骤使用 通用模式,使用window系统的cmd命令行。
注:ui.perfetto.dev/#!/record/i… 给的命令是基于 bash 的命令,并不能在window cmd命令行上直接执行。
注:以下使用步骤是在Android 12 系统上。Android 11 以及以下不可以。
三、使用步骤
1、执行以下命令。这时候命令行窗口会处于等待输入状态
cat <<EOF > /data/misc/perfetto-configs/config && perfetto --txt -o /data/misc/perfetto-traces/trace -c /data/misc/perfetto-configs/config
<<EOF 表示 EOF 作为输入流的结束符
2、在 ui.perfetto.dev/#!/record 选择trace的配置信息
- 在 Probes项下,选择 CPU、GPU、power、Memory、Android apps & svcs 这些项中的内容,按需选择。可以无脑全选。
- 点击 Recording command ,复制小窗口中 <<EOF 之后的信息 到手机等待输入的shell窗口,回车结束输入后开始抓trace。
或者忽略第一步,简单点,直接复制以下内容到手机shell中,回车就可以抓trace了。只不过用的是我配置的这个数据源。
cat <<EOF > /data/misc/perfetto-configs/config && perfetto --txt -o /data/misc/perfetto-traces/trace -c /data/misc/perfetto-configs/config
buffers: {
size_kb: 63488
fill_policy: DISCARD
}
buffers: {
size_kb: 2048
fill_policy: DISCARD
}
data_sources: {
config {
name: "android.gpu.memory"
}
}
data_sources: {
config {
name: "linux.process_stats"
target_buffer: 1
process_stats_config {
scan_all_processes_on_start: true
proc_stats_poll_ms: 1000
}
}
}
data_sources: {
config {
name: "android.log"
android_log_config {
}
}
}
data_sources: {
config {
name: "android.surfaceflinger.frametimeline"
}
}
data_sources: {
config {
name: "linux.sys_stats"
sys_stats_config {
meminfo_period_ms: 1000
vmstat_period_ms: 1000
stat_period_ms: 1000
stat_counters: STAT_CPU_TIMES
stat_counters: STAT_FORK_COUNT
}
}
}
data_sources: {
config {
name: "linux.ftrace"
ftrace_config {
ftrace_events: "sched/sched_switch"
ftrace_events: "power/suspend_resume"
ftrace_events: "sched/sched_wakeup"
ftrace_events: "sched/sched_wakeup_new"
ftrace_events: "sched/sched_waking"
ftrace_events: "power/cpu_frequency"
ftrace_events: "power/cpu_idle"
ftrace_events: "power/gpu_frequency"
ftrace_events: "gpu_mem/gpu_mem_total"
ftrace_events: "raw_syscalls/sys_enter"
ftrace_events: "raw_syscalls/sys_exit"
ftrace_events: "mm_event/mm_event_record"
ftrace_events: "kmem/rss_stat"
ftrace_events: "ion/ion_stat"
ftrace_events: "dmabuf_heap/dma_heap_stat"
ftrace_events: "kmem/ion_heap_grow"
ftrace_events: "kmem/ion_heap_shrink"
ftrace_events: "sched/sched_process_exit"
ftrace_events: "sched/sched_process_free"
ftrace_events: "task/task_newtask"
ftrace_events: "task/task_rename"
ftrace_events: "lowmemorykiller/lowmemory_kill"
ftrace_events: "oom/oom_score_adj_update"
ftrace_events: "ftrace/print"
atrace_apps: "*"
}
}
}
duration_ms: 15000
EOF
3、pull出trace文件
adb pull /data/misc/perfetto-traces/trace
4、浏览器查看trace
- 打开网站 ui.perfetto.dev/
- 点击左侧 Open trace file 选择pull出的trace文件
- 等待浏览器加载trace文件,解析完成就可以查看trace了
四、进阶
- 查看 perfetto.dev/docs/refere… 配置信息,自己选择需要的配置抓trace
- 或者查看 /external/perfetto/protos/perfetto/config/trace_config.proto
五、问题排查
- echo 0 > /sys/kernel/tracing/tracing_on
- 0 开启,“1” 则放弃该数据源
- 有其他进程正在获取ftrace。这种情况,我们需要退出相关进程
- Android 9 (P) and 10 (Q) 非 Pixel 的手机 默认不开启。Android 12 默认开启
- 开启命令:
- adb shell setprop persist.traced.enable 1
- 服务进程是否开启、 ftrace是否被占用
- traced_probes、traced :使用 ps -el | grep traced 查看是否有这俩进程
- getprop debug.atrace.user_initiated,这个值需要为“空”,或未设置
附:
浏览器查看trace使用的快捷键:
- shift+m 选中区域
- W键放大或者ctrl+滚轮
- Perfetto查看runnable的唤醒方法和systrace不一样
- 点击runnable,看不到是谁唤醒的。需要紧接着点击:running