一 概述
在android性能分析工具中,systrace比较好用的工具,工具强大。有的时候系统或者调用运行慢,可以借助systrace分析各个操作耗时,从而进行性能耗时定位和fix。
二 命令使用
systrace在android SDK默认包含,安装位置,以ubuntu为例:
~/Android/Sdk/platform-tools/systrace
通过 --list-categories查看支持的类别
$ python systrace.py --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
pdx - PDX services
sched - CPU Scheduling
irq - IRQ Events
i2c - I2C Events
freq - CPU Frequency
idle - CPU Idle
disk - Disk I/O
load - CPU Load
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
比如抓bionic下的trace执行
$ python ./systrace.py -t 23 -o 4.html bionic
-t 运行23s,输出到4.html中,取bionic相关trace调用
三 添加trace代码
native code开发
#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
#include <utils/Trace.h>
void test()
{
ATRACE_CALL();
...
}
java 层 code开发
import android.os.Trace;
Trace.beginSection(String sectionName)
Trace.EndSection()
看代码实现frameworks/base/core/java/android/os/Trace.java
TAG为TRACE_TAG_APP
public static void beginSection(String sectionName) {
if (isTagEnabled(TRACE_TAG_APP)) {
if (sectionName.length() > MAX_SECTION_NAME_LEN) {
throw new IllegalArgumentException("sectionName is too long");
}
nativeTraceBegin(TRACE_TAG_APP, sectionName);
}
}
四 trace分析
trace输出为html文件,通过chrome浏览器,输入chrome://tracing 分析trace文件,其中键盘的w放大s缩小 a往左,d往右,快捷键比较好用
网友评论