赞
踩
折腾了几天终于把trace 底层原理搞明白了,后面整理一下写出来,今天开始整systrace,工作任务在身。其实我之前主要想研究bpf的,之所以研究trace,主要是因为Android的systrace使用的是trace,意想不到是trace底层源码读起来这么爽(有兴趣的可以去读一下^~^)。
Systrace 是 Android4.1 中新增的性能数据采样和分析工具。它可帮助开发者收集 Android 关键子系统(如 SurfaceFlinger/SystemServer/Kernel/Input/Display 等 Framework 部分关键模块、服务,View系统等)的运行信息,并生成HTML报告。
Systrace 的功能包括跟踪系统的 I/O 操作、内核工作队列、CPU 负载以及 Android 各个子系统的运行状况等。在 Android 平台中,它主要由3部分组成:
我是用的是RK3399基于android 7.1源码,由于客户现场问题,让现场人员定位,所以主要是用atrace,命令行方式。其他方式大家可以看参考文章Android Systrace,文章写得很全面。
atrace 是Android系统内置的工具,可以帮助我们使用后台方式抓取日志。下面简单介绍一下。
usage: atrace [options] [categories...]
options include:-a appname enable app-level tracing for a comma separated list of cmdlines 要抓取的app名字
-b N use a trace buffer size of N KB ,使用trace buffer大小,单位KB
-c trace into a circular buffer
-f filename use the categories written in a file as space-separated values in a line
-k fname,... trace the listed kernel functions
-n ignore signals
-s N sleep for N seconds before tracing [default 0] 延时N秒
-t N trace for N seconds [defualt 5] 抓取时长
-z compress the trace dump 压缩方式下面三个是用于循环抓取
--async_start start circular trace and return immediatly
--async_dump dump the current contents of circular trace buffer
--async_stop stop tracing and dump the current contents of circular
trace buffer
--stream stream trace to stdout as it enters the trace buffer
Note: this can take significant CPU time, and is best
used for measuring things that are not affected by
CPU performance, like pagecache usage.
--list_categories
list the available tracing categories 列出连接设备可用的跟踪类别参数
-o filename write the trace to the specified file instead of stdout. 定向到文件,默认输出stdout
我们看一下当前设备可以跟踪的类别。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
app - Application
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
sched - CPU Scheduling
freq - CPU Frequency
idle - CPU Idle
load - CPU Load
memreclaim - Kernel Memory Reclaim
binder_driver - Binder Kernel driver
binder_lock - Binder global lock trace
adb root 避免个别类别需要root权限,
adb shell atrace -z -b 40000 gfx input view wm am webview hal res dalvik rs sched freq idle disk mmc -t 10 > E:\android\sdk\platform-tools\systrace\trace_out
systrace.py --from-file trace_out -o trace.html
将trace文件拖进Chrome浏览器(必须的),可以看到丰富的trace信息了。
在进程的上面有一条很细的进度条,包含了该线程的状态:
灰色: 睡眠。
蓝色: 可以运行(它可以运行,但还未被调度运行)。
绿色: 正在运行(调度程序认为它正在运行)。
红色: 不间断的睡眠(通常发生在内核锁上), 指出I / O负载,对于性能问题的调试非常有用
橙色: 由于I / O负载导致的不间断睡眠。
要查看不间断睡眠的原因(可从sched_blocked_reason跟踪点获取),请选择红色不间断睡眠切片。
快捷键使用
按键操作 作用
w 放大,[+shift]速度更快
s 缩小,[+shift]速度更快
a 左移,[+shift]速度更快
d 右移,[+shift]速度更快
f 放大当前选定区域
m 标记当前选定区域
v 高亮VSync
g 切换是否显示60hz的网格线
0 恢复trace到初始态,这里是数字0而非字母o
h 切换是否显示详情
/ 搜索关键字
enter 显示搜索结果,可通过← →定位搜索结果
` 显示/隐藏脚本控制台
? 显示帮助功能
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。