繼續學習
https://perfetto.dev/docs/quickstart/android-tracing
https://perfetto.dev/docs/concepts/config
之前看的https://ui.perfetto.dev/#!/record?p=instructions
其實就是開發者文檔裏的config文件
1.理論準備
perfetto普通模式的命令行
普通模式
在普通模式下使用 perfetto
的一般語法如下:
adb shell perfetto [ --txt ] --config CONFIG_FILE
下表列出了在普通模式下使用 perfetto
的可用選項:
選項 | 說明 |
---|---|
--config CONFIG_FILE | -c CONFIG_FILE |
指定配置文件的路徑。在普通模式下,某些配置可能會在配置協議緩衝區中進行編碼。此文件必須符合 AOSP trace_config.proto 中定義的協議緩衝區架構。
根據 AOSP |
--txt |
指示 perfetto 將配置文件解析爲 pbtxt。此標記爲實驗性標記,不建議您在正式版中啓用此標記。 |
這兩個標記是用來解析config文件的
至於加不加--txt參考
https://perfetto.dev/docs/concepts/config
PBTX vs binary format
There are two ways to pass the trace config when using the
perfetto
cmdline client format:Text format
It is the preferred format for human-driven workflows and exploration. It allows to pass directly the text file in the PBTX (ProtoBuf TeXtual representation) syntax, for the schema defined in the trace_config.proto (see reference docs)
When using this mode pass the
--txt
flag toperfetto
to indicate the config should be interpreted as a PBTX file:perfetto -c /path/to/config.pbtx --txt -o trace_file.pftrace
NOTE: The
--txt
option has been introduced only in Android 10 (Q). Older versions support only the binary format.WARNING: Do not use the text format for machine-to-machine interaction benchmark, scripts and tools) as it's more prone to breakages (e.g. if a field is renamed or an enum is turned into an integer)
Binary format
It is the preferred format for machine-to-machine (M2M) interaction. It involves passing the protobuf-encoded binary of the TraceConfig message. This can be obtained passing the PBTX in input to the protobuf's
protoc
compiler (which can be downloaded here).cd ~/code/perfetto # external/perfetto in the Android tree. protoc --encode=perfetto.protos.TraceConfig \ -I. protos/perfetto/config/perfetto_config.proto \ < config.txpb \ > config.bin
and then passing it to perfetto as follows, without the
--txt
argument:
perfetto -c config.bin -o trace_file.pftrace
步驟參考
https://perfetto.dev/docs/quickstart/android-tracing
adb push config.txt /data/local/tmp/trace_config.txt abb shell 'perfetto --txt -c - -o /data/misc/perfetto-traces/trace < /data/local/tmp/trace_config.txt'
NOTE: because of strict SELinux rules, on versions of older than Android 11 (R) passing directly the file path as
-c /data/local/tmp/config
might fail, hence the-c -
+ stdin piping above.Pull the file using
adb pull /data/misc/perfetto-traces/trace ~/trace.pftrace
and upload to the Perfetto UI.The full reference for the
perfetto
cmdline interface can be found here.
至於config文件可以在UI界面選擇需要trace的categories後看https://ui.perfetto.dev/#!/record?p=instructions
比如我需要加一個am的categories
然後點擊instructions
可以看到instructions裏多了個am
config文件截取中間的一段就可以了
buffers: {
size_kb: 8960
fill_policy: DISCARD
}
buffers: {
size_kb: 1280
fill_policy: DISCARD
}
data_sources: {
config {
name: "linux.ftrace"
ftrace_config {
ftrace_events: "ftrace/print"
atrace_categories: "am"
}
}
}
duration_ms: 10000
比如我如下執行結果
config源文件
buffers: {
size_kb: 8960
fill_policy: DISCARD
}
buffers: {
size_kb: 1280
fill_policy: DISCARD
}
data_sources: {
config {
name: "linux.sys_stats"
sys_stats_config {
stat_period_ms: 1000
stat_counters: STAT_CPU_TIMES
stat_counters: STAT_FORK_COUNT
}
}
}
data_sources: {
config {
name: "linux.ftrace"
ftrace_config {
ftrace_events: "ftrace/print"
atrace_categories: "gfx"
atrace_categories: "view"
atrace_categories: "wm"
atrace_categories: "am"
atrace_categories: "rs"
atrace_categories: "pm"
atrace_categories: "ss"
}
}
}
duration_ms: 10000
執行命令
F:\learn\systrace\bilibili>adb push perfetto_config.pbtx /data/misc/perfetto-traces/
perfetto_config.pbtx: 1 file pushed. 0.1 MB/s (784 bytes in 0.006s)
F:\learn\systrace\bilibili>adb shell perfetto --txt --config /data/misc/perfetto-traces/perfetto_config.pbtx --out /data/misc/perfetto-traces/jiatai.perfetto-trace
perfetto_cmd.cc:604 Connected to the Perfetto traced service, starting tracing for 10000 ms
perfetto_cmd.cc:677 Wrote 2475170 bytes into /data/misc/perfetto-traces/jiatai.perfetto-trace
F:\learn\systrace\bilibili>adb pull /data/misc/perfetto-traces/jiatai.perfetto-trace ./
/data/misc/perfetto-traces/jiatai.perfetto-trace: 1 file pulled. 27.4 MB/s (2475170 bytes in 0.086s)
用官方的把adb shell和子命令分開也是可以的
F:\learn\systrace\bilibili>adb shell
raphael:/ # perfetto --txt -c - -o /data/misc/perfetto-traces/trace < /data/misc/perfetto-traces/perfetto_config.pbtx
perfetto_cmd.cc:604 Connected to the Perfetto traced service, starting tracing for 10000 ms
perfetto_cmd.cc:677 Wrote 14014 bytes into /data/misc/perfetto-traces/trace
ata/misc/perfetto-traces/111.trace < /data/misc/perfetto-traces/perfetto_config.pbtx <
perfetto_cmd.cc:604 Connected to the Perfetto traced service, starting tracing for 10000 ms
perfetto_cmd.cc:677 Wrote 2755679 bytes into /data/misc/perfetto-traces/111.trace
raphael:/ # exit
F:\learn\systrace\bilibili>adb pull /data/misc/perfetto-traces/111.trace ./
/data/misc/perfetto-traces/111.trace: 1 file pulled. 26.4 MB/s (2755679 bytes in 0.099s)
用perfetto打開
2.開發者選項
https://developer.android.com/topic/performance/tracing/on-device
沒打得開,應該說的就是開發者選項的系統監控
https://developer.android.google.cn/studio/profile/systrace/on-device
啓用開發者選項(如果尚未啓用此選項)。
打開開發者選項設置屏幕。
在調試部分中,選擇系統跟蹤。此時會打開“系統跟蹤”應用,其中顯示了應用菜單。在應用菜單中,啓用顯示“快捷設置”圖塊,如圖 2 所示。系統會將系統跟蹤圖塊圖塊添加到快捷設置面板中,如圖 1 所示:
導出trace
cd /path-to-traces-on-my-dev-machine && \
adb pull /data/local/traces/ .
3.腳本
If you are running on a Mac or Linux host, or are using a bash-based terminal on Windows, you can use the following:
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
window下執行有點問題,後面ubuntu下試試,我自己改文件爲bat文件了。。。
F:\learn\systrace\bilibili>bash.bat
F:\learn\systrace\bilibili>adb shell perfetto \
perfetto_cmd.cc:441 The TraceConfig is empty
F:\learn\systrace\bilibili>-c - --txt \
'-c' 不是內部或外部命令,也不是可運行的程序
或批處理文件。
F:\learn\systrace\bilibili>-o /data/misc/perfetto-traces/trace \
'-o' 不是內部或外部命令,也不是可運行的程序
或批處理文件。
此時不應有 <<。
F:\learn\systrace\bilibili><<EOF
ubuntu試了下可以的,直接把上面的命令保存成.sh執行就可以了
可以正常打開
4.網頁端抓取
注意ubuntu下使用Google瀏覽器可以,windows下面試了不行。。。
點擊左側欄Record new trace,然後詳細設置選一些自己感興趣的,最後點擊Start Recording
點擊開始錄製,然後會彈出進度條
錄製完成後會自動打開
我設置了20s 50M google瀏覽器打開竟然很卡。。。。是Ubuntu配置了太低了麼。。
過會就好了,可以看到生命週期啥的都有
5.總結
抓trace的方法系統提供了3個,腳本應該是最方便的,其次是開發者選項裏的,最差的是config,操作有些複雜
漏了一個 https://ui.perfetto.dev/#!/
大多對Ubuntu支持的很好,windows支持的不怎麼樣。