TracePlugin 卡顿分析插件 他可以监听以下情况
- 监控ANR、
- 界面流畅性、
- 启动耗时、
- 页面切换耗时、
- 慢函数及卡顿等
这是Matrix最核心的插件之一,原理的实现细节非常值得我们研究学习,其中涉及到很多底层知识handler、编舞者,反射 绘制原理、anr监控原理等等,学会他可以让你的安卓基础更上一层楼
下面开始源码分析
根据上一节的总结,我们知道每个插件通过继承和实现接口,都具备这些基本方法的实现
- IPlugin:拥有插件的特性,其中包含插件生命周期的回调;
- IssuePublisher.OnIssueDetectListener:可以监听 Issue 报告;
- IAppForeground:可以接收 App 前后台切换的信息。
所以插件的初始化都会经过init 到start开始记录 通过stop暂停 destory销毁
所以进入init看看
private final TraceConfig traceConfig;
private EvilMethodTracer evilMethodTracer;
private StartupTracer startupTracer;
private FrameTracer frameTracer;
private LooperAnrTracer looperAnrTracer;
private SignalAnrTracer signalAnrTracer;
private IdleHandlerLagTracer idleHandlerLagTracer;
private TouchEventLagTracer touchEventLagTracer;
private ThreadPriorityTracer threadPriorityTracer;
private static boolean supportFrameMetrics;
public TracePlugin(TraceConfig config) {
this.traceConfig = config;
}
@Override
public void init(Application app, PluginListener listener) {
super.init(app, listener);
MatrixLog.i(TAG, "trace plugin init, trace config: %s", traceConfig.toString());
int sdkInt = Build.VERSION.SDK_INT;
if (sdkInt < Build.VERSION_CODES.JELLY_BEAN) {
MatrixLog.e(TAG, "[FrameBeat] API is low Build.VERSION_CODES.JELLY_BEAN(16), TracePlugin is not supported");
unSupportPlugin();
return;
} else if (sdkInt >= Build.VERSION_CODES.O) {
supportFrameMetrics = true;
}
looperAnrTracer = new LooperAnrTracer(traceConfig);
frameTracer = new FrameTracer(traceConfig, supportFrameMetrics);
evilMethodTracer = new EvilMethodTracer(traceConfig);
startupTracer = new StartupTracer(traceConfig);
}
这里 TracePlugin 属性包含了众多Tracer,在init首先初始化了四个核心 Tracer,暂且取名为追踪器:
AnrTracer:ANR 追踪器。
EvilMethodTracer:慢函数追踪器;
StartupTracer:启动追踪器;
FrameTracer:帧率追踪器;
TracePlugin 的功能主要依靠这四个追踪器来实现,也是我们重点学习的目标。
Tracer又是什么呢?我们看看他的父类
public abstract class Tracer extends LooperObserver implements ITracer {
private volatile boolean isAlive = false;
private static final String TAG = "Matrix.Tracer";
@CallSuper
protected void onAlive() {
MatrixLog.i(TAG, "[onAlive] %s", this.getClass().getName());
}
@CallSuper
protected void onDead() {
MatrixLog.i(TAG, "[onDead] %s", this.getClass().getName());
}
@Override
final synchronized public void onStartTrace() {
if (!isAlive) {
this.isAlive = true;
onAlive();
}
}
@Override
final synchronized public void onCloseTrace() {
if (isAlive) {
this.isAlive = false;
onDead();
}
}
@Override
public void onForeground(boolean isForeground) {
}
@Override
public boolean isAlive() {
return isAlive;
}
public boolean isForeground() {
return AppActiveMatrixDelegate.INSTANCE.isAppForeground();
}
}
因为追踪器都关注固定的生命周期,所以这里抽取了一下开始追踪,结束追踪,活跃死亡等节点,方便实现类根据自己情况自由扩展Tracker又继承抽象类LooperObserver 该类可以监测到每个handler消息的耗时,帧回调,具体怎么实现后序会详细说。
public abstract class LooperObserver {
private boolean isDispatchBegin = false;
@CallSuper
public void dispatchBegin(long beginNs, long cpuBeginNs, long token) {
isDispatchBegin = true;
}
public void doFrame(String focusedActivity, long startNs, long endNs, boolean isVsyncFrame, long intendedFrameTimeNs, long inputCostNs, long animationCostNs, long traversalCostNs) {
}
@CallSuper
public void dispatchEnd(long beginNs, long cpuBeginMs, long endNs, long cpuEndMs, long token, boolean isVsyncFrame) {
isDispatchBegin = false;
}
public boolean isDispatchBegin() {
return isDispatchBegin;
}
}
最上层的接口 追踪开关回调
public interface ITracer extends IAppForeground {
boolean isAlive();
void onStartTrace();
void onCloseTrace();
}
通过Tracer的抽取,很好的实现了不同追踪功能的解耦,这种设计思想非常值得我们学习
最后根据不同功能实现了以下Tracker 下一章我们会优先分析LooperAnrTracer
private EvilMethodTracer evilMethodTracer;
private StartupTracer startupTracer;
private FrameTracer frameTracer;
private LooperAnrTracer looperAnrTracer;
private SignalAnrTracer signalAnrTracer;
private IdleHandlerLagTracer idleHandlerLagTracer;
private TouchEventLagTracer touchEventLagTracer;
private ThreadPriorityTracer threadPriorityTracer;
作者:小虎牙007
链接:https://juejin.cn/post/7157243057199382541
网友评论