一、简介
TracePlugin这个模块中包含EvilMethodTracer(函数耗时检测)、StartupTracer(启动时间检测)、FrameTracer(帧率检测)、AnrTracer(ANR检测)。
二、功能简介
TracePlugin中主要根据Activity生命周期的对4个Tracer的开启、关闭进行管理。
三、主要方法解析
1、init方法
该方法主要是对4个Tracer进行初始化操作,代码如下:
@Override
public void init(Application app, PluginListener listener) {
super.init(app, listener);
MatrixLog.i(TAG, "trace plugin init, trace config: %s", traceConfig.toString());
//APi小于16 不支持
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
MatrixLog.e(TAG, "[FrameBeat] API is low Build.VERSION_CODES.JELLY_BEAN(16), TracePlugin is not supported");
unSupportPlugin();
return;
}
anrTracer = new AnrTracer(traceConfig);
frameTracer = new FrameTracer(traceConfig);
evilMethodTracer = new EvilMethodTracer(traceConfig);
startupTracer = new StartupTracer(traceConfig);
}
2、TracePlugin.start()
在start方法中主要是启动UIThreadMonitor、AppMethodBeat和4个Tracer。关于UIThreadMonitor和AppMethodBeat的作用后续在解析Tracer时会分析。
@Override
public void start() {
super.start();
if (!isSupported()) {
MatrixLog.w(TAG, "[start] Plugin is unSupported!");
return;
}
MatrixLog.w(TAG, "start!");
Runnable runnable = new Runnable() {
@Override
public void run() {
//初始化 UIThreadMonitor
if (!UIThreadMonitor.getMonitor().isInit()) {
try {
UIThreadMonitor.getMonitor().init(traceConfig);
} catch (java.lang.RuntimeException e) {
MatrixLog.e(TAG, "[start] RuntimeException:%s", e);
return;
}
}
//启动 AppMethodBeat
AppMethodBeat.getInstance().onStart();
//启动 UIThreadMonitor
UIThreadMonitor.getMonitor().onStart();
anrTracer.onStartTrace();
frameTracer.onStartTrace();
evilMethodTracer.onStartTrace();
startupTracer.onStartTrace();
}
};
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
runnable.run();
} else {
//提示 warning TracePlugin 应该在 主线程启动
MatrixLog.w(TAG, "start TracePlugin in Thread[%s] but not in mainThread!", Thread.currentThread().getId());
//post到主线程启动
MatrixHandlerThread.getDefaultMainHandler().post(runnable);
}
}
3、TracePlugin.stop()
stop()中主要是在主线程中关闭和停止Tracer、AppMethodBeat、UIThreadMonitor,代码如下:
@Override
public void stop() {
super.stop();
if (!isSupported()) {
MatrixLog.w(TAG, "[stop] Plugin is unSupported!");
return;
}
MatrixLog.w(TAG, "stop!");
Runnable runnable = new Runnable() {
@Override
public void run() {
AppMethodBeat.getInstance().onStop();
UIThreadMonitor.getMonitor().onStop();
anrTracer.onCloseTrace();
frameTracer.onCloseTrace();
evilMethodTracer.onCloseTrace();
startupTracer.onCloseTrace();
}
};
if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
runnable.run();
} else {
MatrixLog.w(TAG, "stop TracePlugin in Thread[%s] but not in mainThread!", Thread.currentThread().getId());
MatrixHandlerThread.getDefaultMainHandler().post(runnable);
}
}
4、TracePlugin.onForeground()
onForeground就是将App当前处于前台或者是后台的状态分发给各个Tracer。代码如下:
@Override
public void onForeground(boolean isForeground) {
super.onForeground(isForeground);
if (!isSupported()) {
return;
}
if (frameTracer != null) {
frameTracer.onForeground(isForeground);
}
if (anrTracer != null) {
anrTracer.onForeground(isForeground);
}
if (evilMethodTracer != null) {
evilMethodTracer.onForeground(isForeground);
}
if (startupTracer != null) {
startupTracer.onForeground(isForeground);
}
}
总结
- TracePlugin中有4个主要方法,分别是init,onStart,onStop,onForeground。
- TracePlugin中主要是对Tracer、AppMethodBeat、UIThreadMonitor进行初始化、启动、暂停以及App前后台状态分发等操作。
网友评论