一、抓取trace
Perfetto配置
Perfetto在Android 9以上默认预制,但没有开启
开启方法: adb shell setprop presist.traced.enable 1
Perfetto实现类似systrace或atrace功能
adb shell perfetto -o /data/misc/perfetto-traces/trace -t 20s schedfreq idle am wm gfx view
adb shell perfetto \
-c - --txt \
-o /data/misc/perfetto-traces/trace \
<<EOF
duration_ms: 10000
buffers: {
size_kb: 8960
fill_policy: DISCARD
}
buffers: {
size_kb: 1280
fill_policy: DISCARD
}
data_sources: {
config {
name: "linux.ftrace"
ftrace_config {
ftrace_events: "sched/sched_switch"
ftrace_events: "power/suspend_resume"
ftrace_events: "sched/sched_process_exit"
ftrace_events: "sched/sched_process_free"
ftrace_events: "task/task_newtask"
ftrace_events: "task/task_rename"
ftrace_events: "ftrace/print"
atrace_categories: "gfx"
atrace_categories: "view"
atrace_categories: "webview"
atrace_categories: "camera"
atrace_categories: "dalvik"
atrace_categories: "power"
}
}
}
data_sources: {
config {
name: "linux.process_stats"
target_buffer: 1
process_stats_config {
scan_all_processes_on_start: true
}
}
}
EOF
也可以将配置文件push进设备。如:
adb push config.txt /data/local/tmp/trace_config.txt
adb shell 'perfetto --txt -c - -o /data/misc/perfetto-traces/trace < /data/local/tmp/trace_config.txt'
二、Trace转换
支持的格式
-
text
- protobuf text format: a text based representation of protos -
json
- Chrome JSON format: the format used by chrome://tracing -
systrace
: the ftrace text format used by Android systrace -
profile
(heap profiler only): pprof-like format. This is only valid for traces with native heap profiler dumps.
工具下载及用法
curl -LO https://get.perfetto.dev/traceconv
chmod +x traceconv
./traceconv [text|json|systrace|profile] [input proto file] [output file]
三、Heap profiling
环境配置
- Android 10以上设备
- 如果是user版本需要在manifest配置profileable 或debuggable
<manifest ...>
<application>
<profileable android:shell="true"/>
...
</application>
</manifest>
工具下载及抓取方法
curl -LO https://raw.githubusercontent.com/google/perfetto/master/tools/heap_profile
chmod +x heap_profile
./heap_profile -n system_server
四、LMK和oom_score_adj抓取
adb shell perfetto \
-c - --txt \
-o /data/misc/perfetto-traces/trace \
<<EOF
buffers: {
size_kb: 8960
fill_policy: DISCARD
}
buffers: {
size_kb: 1280
fill_policy: DISCARD
}
data_sources: {
config {
name: "linux.process_stats"
target_buffer: 1
process_stats_config {
scan_all_processes_on_start: true
}
}
}
data_sources: {
config {
name: "linux.ftrace"
ftrace_config {
ftrace_events: "lowmemorykiller/lowmemory_kill"
ftrace_events: "oom/oom_score_adj_update"
ftrace_events: "ftrace/print"
atrace_apps: "lmkd"
}
}
}
duration_ms: 60000
EOF
五、Java Heap
curl -LO https://raw.githubusercontent.com/google/perfetto/master/tools/java_heap_dump
chmod +x java_heap_dump
java_heap_dump -n com.android.systemui