Zygote进程启动后会启动System进程,在System进程启动过程中会启动系统中的关键服务,如AMS、PMS以及这里要分析的PerfService。先看下流程图:
SystemServer启动PerfService服务是通过实例化PerfServiceImpl的对象perfService,并把该服务的Binder对象添加到ServiceManager中。
先看SystemServer类中的startOtherServices方法:
private void startOtherServices() {
. . .
PerfServiceStateNotifier perfServiceNotifier = null;
IPerfServiceManager perfServiceMgr = null;
. . .
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
. . .
/// M: add for PerfService feature @{
if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {
try {
Slog.i(TAG, "PerfService state notifier");
// 实例化PerfService的状态通知者
perfServiceNotifier = new PerfServiceStateNotifier();
// 把通知者注册到AMS的观察者中,有状态变化时会通知所有注册过的通知者
mActivityManagerService.registerActivityStateNotifier(perfServiceNotifier);
} catch (Throwable e) {
Slog.e(TAG, "FAIL starting PerfServiceStateNotifier", e);
}
// Create PerfService manager thread and add service
try {
// 实例化PerfServiceManager线程
perfServiceMgr = new PerfServiceManager(context);
IPerfService perfService = null;
// 实例化服务
perfService = new PerfServiceImpl(context, perfServiceMgr);
Slog.d("perfservice", "perfService=" + perfService);
if (perfService != null) {
// 把服务添加到ServiceManager中
ServiceManager.addService(Context.MTK_PERF_SERVICE, perfService.asBinder());
}
} catch (Throwable e) {
Slog.e(TAG, "perfservice Failure starting PerfService", e);
}
}
/// @}
. . .
}
. . .
/// M: add for hdmi feature
final IPerfServiceManager perfServiceF = perfServiceMgr;
// We now tell the activity manager it is okay to run third party
// code. It will call back into us once it has gotten to the state
// where third party code can really run (but before it has actually
// started launching the initial applications), for us to complete our
// initialization.
mActivityManagerService.systemReady(new Runnable() {
@Override
public void run() {
Slog.i(TAG, "Making services ready");
. . .
/// M: add for PerfService feature @{
if (SystemProperties.get("ro.mtk_perfservice_support").equals("1")) {
// Notify PerfService manager of system ready
try {
Trace.traceBegin(Trace.TRACE_TAG_SYSTEM_SERVER, "MakePerfServiceReady");
// 系统启动后回调IPerfServiceManager的systemReady方法
if (perfServiceF != null) perfServiceF.systemReady();
Trace.traceEnd(Trace.TRACE_TAG_SYSTEM_SERVER);
} catch (Throwable e) {
reportWtf("making PerfServiceManager ready", e);
}
}
/// @}
. . .
}
});
. . .
}
再看PerfServiceStateNotifier类,先看下它实现的接口IActivityStateNotifier:
public interface IActivityStateNotifier {
public enum ActivityState {
Paused,
Resumed,
Destroyed,
Stopped
}
/**
* Notify activity state change.
*
* @param packageName The target package name.
* @param pid The process id package belongs to.
* @param className The class name of the package.
* @param ac