Android Systrace

Android性能分析工具

下载android-sdk-platform-tools

https://developer.android.google.cn/studio/releases/platform-tools?hl=zh-cn

解压后直接使用

获取systrace日志并转换为html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
ip="127.0.0.1"
port="5555"

TRACE_BUFFER_SZ_KB=16384 #16MB
TRACE_FILE="/data/local/tmp/trace_$port.log"

adb disconnect
adb connect $ip:$port
adb root

sleep 1

# adb shell atrace --list_categories
tracedump=$(echo `adb shell atrace --list_categories | awk '{print $1}'` | sed 's/\n//g')

adb shell "atrace -b $TRACE_BUFFER_SZ_KB -t 30 -z $tracedump > $TRACE_FILE"
adb pull $TRACE_FILE

python platform-tools/systrace/systrace.py -b $TRACE_BUFFER_SZ_KB --from-file trace_$port.log -o trace_$port.html

查看trace文件的快捷键

快捷键描述
w放大
s缩小
a左移
e右移

使用Systrace 检测卡顿丢帧问题

  • Systrace报告列出了每个进程呈现UI frame,并显示沿着时间线的每个渲染帧。 在绿色框架圆圈中,是指在16.6ms内呈现每秒稳定60帧, 花费16.6ms以上渲染的帧用黄色或红色圆圈表示。

错误

ImportError: No module named six

1
sudo apt install python-six

权限问题

1
2
3
4
5
6
7
8
error opening /sys/kernel/debug/tracing/options/overwrite: Permission denied (13)
error opening /sys/kernel/debug/tracing/buffer_size_kb: Permission denied (13)
error opening /sys/kernel/debug/tracing/trace_clock: Permission denied (13)
error opening /sys/kernel/debug/tracing/tracing_on: Permission denied (13)
error opening /sys/kernel/debug/tracing/tracing_on: Permission denied (13)
unable to start tracing
error opening /sys/kernel/debug/tracing/options/overwrite: Permission denied (13)
error opening /sys/kernel/debug/tracing/buffer_size_kb: Permission denied (13)

通过adb root获取权限

参考

  • Android Systrace 使用详解