预备知识:
SystemServer是在Zygote进程中启动的;
上代码:
if (startSystemServer) {
Runnable r = forkSystemServer(abiList, socketName, zygoteServer);
// {@code r == null} in the parent (zygote) process, and {@code r != null} in the
// child (system_server) process.
if (r != null) {
r.run();
return;
}
}
在ZygoteInit的main()方法中,我们看到有一个bool变量startSystemServer,当startSystemServer为true时,Zygote就开始启动SystemServer,首先调用forkSystemServer方法:
private static Runnable forkSystemServer(String abiList, String socketName,
ZygoteServer zygoteServer) {
...
/* Request to fork the system server process */
pid = Zygote.forkSystemServer()....);
...
/* For child process */
if (pid == 0) {
...
return handleSystemServerProcess(parsedArgs);
}
return null;
}
可以看到,在for看SystemServer()方法中,Zygote调用forSystemServer()方法来fork一个子进程(创建成功了我们的SystemServer进程)后,(当pid返回为0时就是在子进程),在Systemserver进程中执行handleystemServerProcess()方法,接下来我们看handleSystemServerProcess()中的执行情况:
/**
* Finish remaining work for the newly forked system server process.
*/
private static Runnable handleSystemServerProcess(ZygoteConnection.Arguments parsedArgs) {
...
return ZygoteInit.zygoteInit(parsedArgs.targetSdkVersion,
parsedArgs.remainingArgs, cl);
}
在handleystemServerProcess()方法中主要我们看这个ZygoteInit.zygoteInit()方法,这个...init()方法中包括了三部分...init(),接下来看看包括那三部分的...init:
public static final Runnable zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader) {
if (RuntimeInit.DEBUG) {
Slog.d(RuntimeInit.TAG, "RuntimeInit: Starting application from zygote");
}
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ZygoteInit");
RuntimeInit.redirectLogStreams();
RuntimeInit.commonInit();
ZygoteInit.nativeZygoteInit();
return RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);
}
这三个部分分别为:
- RuntimeInit.commonInit();
- ZygoteInit.nativeZygoteInit();
- RuntimeInit.applicationInit(targetSdkVersion, argv, classLoader);
其中commonInit()就是常规的一些初始化,
第二部分就是nativeZygoteInit();
static void com_android_internal_os_ZygoteInit_nativeZygoteInit(JNIEnv* env, jobject clazz)
{
gCurRuntime->onZygoteInit();
}
virtual void onZygoteInit()
{
sp<ProcessState> proc = ProcessState::self();
ALOGV("App process: starting thread pool.\n");
proc->startThreadPool();
}
nativeZygoteInit()调用了onZygoteInit()方法,主要适用于启用binder机制,并且启动一个binder线程,因为接下来SystemServer里面很多 系统服务都要跟其他进程通信,比如和应用进程,Servicemanager进程通信;
第三块就是applicationInit(),
protected static Runnable applicationInit(int targetSdkVersion, String[] argv,
ClassLoader classLoader) {
...
// Remaining arguments are passed to the start class's static main
return findStaticMain(args.startClass, args.startArgs, classLoader);
}
这个主要是调用java类的一个入口函数,就是SystemServer这个java类,我们看看这个入口函数是怎么样的:
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
public SystemServer() {
// Check for factory test mode.
mFactoryTestMode = FactoryTest.getMode();
// Remember if it's runtime restart(when sys.boot_completed is already set) or reboot
mRuntimeRestart = "1".equals(SystemProperties.get("sys.boot_completed"));
mRuntimeStartElapsedTime = SystemClock.elapsedRealtime();
mRuntimeStartUptime = SystemClock.uptimeMillis();
}
private void run() {
...
Looper.prepareMainLooper();
Looper.getMainLooper().setSlowLogThresholdMs(
SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
// Initialize native services.
System.loadLibrary("android_servers");
...
// Start services.
traceBeginAndSlog("StartServices");
startBootstrapServices();
startCoreServices();
startOtherServices();
...
// Loop forever.
Looper.loop();
...
}
首先new 了一个SystemServer对象,然后调用他的run()方法,在run方法中,为主线程创建一个looper,然后加载了一个共享库"android_servers",这个共享库就是SystemServer系统服务的那些代码,,在创建一个系统上下文Context,这里我们可以吧SystemServer理解成一个应用(这个SystemServer里面也有Context,也有application也有ActivityThread,和我们的应用进程很相似);接下来分别三次startXXXServiece.
- startBootstrapService();
- startCoreService();
- startOthersServicemanager();
将系统服务分为三批依次启动;最后调用Looper的loop函数进入Looper循环.所以SystemServer 的主线程是不能退出.
有几个问题
系统服务是怎么启动的?
主要看两个内容:
1.系统服务是怎么发布,让应用程序可见?
/**
* Publish the service so it is accessible to other services and apps.
*
* @param name the name of the new service
* @param service the service object
*/
protected final void publishBinderService(String name, IBinder service) {
publishBinderService(name, service, false);
}
...
protected final void publishBinderService(String name, IBinder service,
boolean allowIsolated) {
publishBinderService(name, service, allowIsolated, DUMP_FLAG_PRIORITY_DEFAULT);
}
protected final void publishBinderService(String name, IBinder service,
boolean allowIsolated, int dumpPriority) {
ServiceManager.addService(name, service, allowIsolated, dumpPriority);
}
我们看到系统服务还是要讲自己的binder注册到Servicemanager里面统一管理的,通过Servicemanager就可以让其他进程对其进行请求和远程调用.
2.系统服务跑在什么线程?
??还在探索??
怎么解决系统服务之间的互相依赖?
解决方法:
- 分批启动 先启动AMS,PMS,PKMS
- 分阶段启动 阶段一,阶段二,阶段三 ...
桌面的启动
在AMS就绪后,就开始调用systemReady()方法:
public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
...
startHomeActivityLocked(currentUserId, "systemReady");
...
}
在systemReady()方法中就会调用startHomeActivityLocked()方法启动桌面,(桌面可以看做是一个单独的系统级别的应用,)这里启动了桌面的xxActivity类.....