SystemServer
上一篇中我们讲到Zygote进程启动了SystemServer进程,那么来看看它是如何处理System进程的。先来一个完整的时序图,如下:
在ZygoteInit.java的forkSystemServer方法fork了SystemServer的子进程并启动,然后执行了handleSystemServerProcess方法,在这里最终会执行zygoteInit函数来进行初始化,这个函数如下:
public static final void zygoteInit(int targetSdkVersion, String[] argv,
ClassLoader classLoader) throws Zygote.MethodAndArgsCaller {
...
RuntimeInit.redirectLogStreams();
RuntimeInit.commonInit();
ZygoteInit.nativeZygoteInit();
RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);
}
倒数第二行代码调用了nativeZygoteInit方法,这是一个Native层的函数,用来启动Binder线程池,这样SystemServer进程就可以使用Binder与其他进程进行通信。
最后一行代码则通过RuntimeInit的applicationInit方法来进入SystemServer的main方法。
下面来具体看一下。
Binder线程池
nativeZygoteInit是一个Native方法,定义在AndroidRuntime.cpp(frameworks/base/core/jni/AndroidRuntime.cpp)文件中:
static void com_android_internal_os_RuntimeInit_nativeZygoteInit(JNIEnv* env, jobject clazz){
gCurRuntime->onZygoteInit();
}
这里gCurRuntime是AndroidRuntime类型的指针,实际上是AndroidRuntime的子类AppRuntime,它在app_main.cpp中定义,它的onZygoteInit代码如下:
virtual void onZygoteInit()
{
sp<ProcessState> proc = ProcessState::self();
proc->startThreadPool();
}
可以看到通过startThreadPool启动了一个Binder线程池,这样SystemServer进程就可以使用Binder与其他进程进行通信。
SystemServer的main方法
回到上面,在启动线程池后执行了RuntimeInit的applicationInit方法,这个方法主要是找到“com.android.server.SystemServer”这个类,并通过反射获取它的main函数并最终执行起来,这部分就不细说了。
SystemServer的main函数如下:
public static void main(String[] args) {
new SystemServer().run();
}
只有一行,创建了SystemServer并执行run函数,代码如下:
private void run() {
...
Looper.prepareMainLooper();
System.loadLibrary("android_servers");//1
performPendingShutdown();
createSystemContext();
mSystemServiceManager = new SystemServiceManager(mSystemContext);//2
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
try {
startBootstrapServices();
startCoreServices();
startOtherServices();
} catch (Throwable ex) {
...
}
...
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
首先通过loadLibrary加载了libandroid_servers.so动态库,然后创建了SystemServiceManager,它会对系统服务进行创建、启动和生命周期管理。
然后在try代码块中通过startBootstrapServices方法启动了ActivityManagerService、PowerManagerService、PackageManagerService等服务;通过startCoreServices方法启动BatteryService、WebviewUpdateService等服务;通过startOtherServices方法启动CameraService、AlermManagerService等服务。这些服务的父类均为SystemService。
从这部分可以看到官方把系统服务分成了三个类型:引导服务、核心服务和其他服务,其他服务中是一个非紧要和不需要立即启动的服务。
在三个方法中会分别创建这些服务并注册到ServiceManager中,ServiceManager用来管理系统中各种Service,用于系统C/S架构的Binder通信机制:Client端要使用某个Service,需先到ServiceManager查询相关信息,根据这些信息与Service所在的Server进程建立通信通路,这样Client端就可以使用Service了。
SystemServer总结
SystemServer进程创建后主要做了如下工作:
Launcher
SystemServer进程启动过程中会启动PackageManagerService,它启动后会将系统中的应用程序安装完成。在此之前已经启动的AMS会将Launcher启动起来,Launcher启动过程如图:
启动Launcher的入口是AMS的systemReady方法,它在SystemServer的startOtherServices方法中被调用。Launcher启动后会查询系统中已经安装的应用程序及信息,然后显示到桌面上。
总结
我们通过两篇文章来简单的了解了Android系统的启动流程,这个流程主要有几个部分:
以上就是Android系统启动的完整流程,当然很多细节没有讲,大家有兴趣可以自行研究一下。