背景:
经常在做性能优化时候,可能会有一个开机时间优化的题目,对于这个题目我们有一个比较明显的痛点:
想要抓取开机启动整个过程的trace,放到perfetto上进分析,但是发现开机过程中无法连接上设备,没办法抓到完整的trace,所以perfetto无法分析出整个开机过程。
那么有没有方法可以不需要依赖adb这种连接设备方式抓取,而是系统开机自己就会智能抓取相关的trace呢?
针对上面的痛点,google在andorid 13上就已经有解决哈
下面来教大家解决这个痛点。
步骤1:
准备好一个config文件,主要用于配置perfetto抓取时候的相关配置,比如要抓哪些tag等,是否也抓取日志等,这个配置官网给了一个最简单的版本
# One buffer allocated within the central tracing binary for the entire trace,
# shared by the two data sources below.
buffers {
size_kb: 32768
fill_policy: DISCARD
}
# Ftrace data from the kernel, mainly the process scheduling events.
data_sources {
config {
name: "linux.ftrace"
target_buffer: 0
ftrace_config {
ftrace_events: "sched_switch"
ftrace_events: "sched_waking"
ftrace_events: "sched_wakeup_new"
ftrace_events: "task_newtask"
ftrace_events: "task_rename"
ftrace_events: "sched_process_exec"
ftrace_events: "sched_process_exit"
ftrace_events: "sched_process_fork"
ftrace_events: "sched_process_free"
ftrace_events: "sched_process_hang"
ftrace_events: "sched_process_wait"
}
}
}
# Resolve process commandlines and parent/child relationships, to better
# interpret the ftrace events, which are in terms of pids.
data_sources {
config {
name: "linux.process_stats"
target_buffer: 0
}
}
# 10s trace, but can be stopped prematurely via `adb shell pkill -u perfetto`.
duration_ms: 10000
把上面的文本内容拷贝保存成boottrace.pbtxt文件。
但是说明一下上面的官方给的基本上抓到的trace没办法分析,主要有以下几个问题:
1、抓到trace没有任何的tag,所以分析也就没有任何意义
2、抓取的时间比较短,内存也比较小
3、抓取trace没有android log,不方便和log结合分析
所以整改后的config如下:
buffers {
size_kb: 65536
fill_policy: DISCARD
}
buffers {
size_kb: 4096
fill_policy: DISCARD
}
data_sources {
config {
name: "linux.ftrace"
ftrace_config {
ftrace_events: "sched/sched_process_exit"
ftrace_events: "sched/sched_process_free"
ftrace_events: "task/task_newtask"
ftrace_events: "task/task_rename"
ftrace_events: "sched/sched_switch"
ftrace_events: "power/suspend_resume"
ftrace_events: "sched/sched_blocked_reason"
ftrace_events: "sched/sched_wakeup"
ftrace_events: "sched/sched_wakeup_new"
ftrace_events: "sched/sched_waking"
ftrace_events: "sched/sched_process_exit"
ftrace_events: "sched/sched_process_free"
ftrace_events: "task/task_newtask"
ftrace_events: "task/task_rename"
ftrace_events: "power/cpu_frequency"
ftrace_events: "power/cpu_idle"
ftrace_events: "power/suspend_resume"
ftrace_events: "ftrace/print"
atrace_categories: "adb"
atrace_categories: "aidl"
atrace_categories: "am"
atrace_categories: "audio"
atrace_categories: "binder_driver"
atrace_categories: "binder_lock"
atrace_categories: "bionic"
atrace_categories: "database"
atrace_categories: "gfx"
atrace_categories: "hal"
atrace_categories: "input"
atrace_categories: "pm"
atrace_categories: "power"
atrace_categories: "res"
atrace_categories: "ss"
atrace_categories: "view"
atrace_categories: "wm"
disable_generic_events: true
}
}
}
data_sources {
config {
name: "linux.process_stats"
process_stats_config {
}
}
}
data_sources {
config {
name: "linux.sys_stats"
sys_stats_config {
stat_period_ms: 500
stat_counters: STAT_CPU_TIMES
stat_counters: STAT_FORK_COUNT
cpufreq_period_ms: 500
}
}
}
duration_ms: 30000
同样用上面内容保存到boottrace.pbtxt文件。
步骤2
push boottrace.pbtxt文件到/data/misc/perfetto-configs/boottrace.pbtxt
adb push <yourfile> /data/misc/perfetto-configs/boottrace.pbtxt
步骤3
使能相关属性
Enable the perfetto_trace_on_boot service
adb shell setprop persist.debug.perfetto.boottrace 1
这个属性设置以后就可以在下一次开机会自动抓取到perfetto相关trace文件
步骤4
重启设备,获取perfetto的trace文件
执行 adb reboot命令
等待20s,然后等系统桌面显示完整后
adb pull /data/misc/perfetto-traces/boottrace.perfetto-trace
打开
https://ui.perfetto.dev
网站导入boottrace.perfetto-trace进行分析:
官方链接参考:
https://perfetto.dev/docs/case-studies/android-boot-tracing