前言:
Systrace是分析Android性能问题的神器。一般抓trace命令是需要在AndroidSDK下的\platform-tools\systrace执行\systrace.py,很奇怪我的AndroidSDK并没有systrace文件夹,于是CSDN单独下载了trace文件 但是我一运行.\systrace.py -b 10240 -t gfx input view webview wm sm audio camera hal res -o trace.html就奔溃。最终发现还是懒人方法适合我,即使用adb命令和开发者选项抓取trace
方法一:adb 命令抓取trace
没想到直接adb命令进行抓取就可以,既然有这个自行车,还要啥systrace.py
adb shell perfetto -o /data/misc/perfetto-traces/trace_file.perfetto-trace -t 20s sched freq idle am wm gfx view
要更改抓取时间则将 -t 后面的值替换即可,如果要增加缓存大小,则加 -b 对应的大小值mb
发命令示例:adb shell perfetto -o /data/misc/perfetto-traces/trace_file.perfetto-trace -t 20s sched freq idle am wm gfx view
Warning: No PTY. CTRL+C won't gracefully stop the trace. If you are running perfetto via adb shell, use the -tt arg (adb shell -t perfetto ...) or consider using the helper script tools/record_android_trace from the Perfetto repository.
[269.031] perfetto_cmd.cc:1051 Connected to the Perfetto traced service, TTL: 20s
[289.232] perfetto_cmd.cc:1213 Wrote 2001382 bytes into /data/misc/perfetto-traces/trace_file.perfetto-trace // trace的存放目录
trace所支持的类别,可以使用adb shell atrace --list_categories 查看 :
adb shell atrace --list_categories
gfx - Graphics
input - Input
view - View System
webview - WebView
wm - Window Manager
am - Activity Manager
sm - Sync Manager
audio - Audio
video - Video
camera - Camera
hal - Hardware Modules
res - Resource Loading
dalvik - Dalvik VM
rs - RenderScript
bionic - Bionic C Library
power - Power Management
pm - Package Manager
ss - System Server
database - Database
network - Network
adb - ADB
vibrator - Vibrator
aidl - AIDL calls
nnapi - NNAPI
rro - Runtime Resource Overlay
pdx - PDX services
sched - CPU Scheduling
irq - IRQ Events
i2c - I2C Events
freq - CPU Frequency
idle - CPU Idle
disk - Disk I/O
sync - Synchronization
workq - Kernel Workqueues
memreclaim - Kernel Memory Reclaim
regulators - Voltage and Current Regulators
binder_driver - Binder Kernel driver
binder_lock - Binder global lock trace
pagecache - Page cache
memory - Memory
thermal - Thermal event
trace的存放目录 /data/misc/perfetto-traces/trace_file.perfetto-trace,使用adb pull导出即可
C:\Users\fadi.su>adb pull /data/misc/perfetto-traces/trace_file.perfetto-trace C:\Users\fadi.su\Desktop\traceTest
/data/misc/perfetto-traces/trace_file.perfetto-trace: 1 file pulled, 0 skipped. 1.7 MB/s (2001382 bytes in 1.132s)
方法二:开发者选项抓取trace
具体开发者选项->系统跟踪:记录CPU性能分析结果,如果需要指定类别也可以勾选
trace的存放目录:/data/local/traces/,使用adb pull导出即可
C:\Users\fadi.su>adb pull /data/local/traces C:\Users\fadi.su\Desktop\traceTest
/data/local/traces/: 1 file pulled, 0 skipped. 2.6 MB/s (940553 bytes in 0.342s)
Google浏览器使用perfetto打开trace文件
输出trace的地址:https://ui.perfetto.dev/
将trace文件拖拽进去就行,由于需要解析和绘制html,故需要等一下。例如我的是下述解析结果。
学了下还是挺方便的。和功耗分析的BugReport类似的分析界面。