创世记(二)——system进程的启动

《创世记(一)——Zygote进程的启动》之Step10 ZygoteInit::startSystemServer(…)中,Zygote启动system进程,并执行ZygoteInit::handleSystemServerProcess(…)函数作为该进程入口。接下来就一次函数为起点继续分析。

Step1 ZygoteInit::handleSystemServerProcess(…)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// frameworks/base/core/java/com/android/internal/os/ZygoteInit.java:415
private static void handleSystemServerProcess(
ZygoteConnection.Arguments parsedArgs)
throws ZygoteInit.MethodAndArgsCaller {

closeServerSocket(); // 由于是从Zygote进程fork而来,先关闭掉system不需要的socket
...
if (parsedArgs.invokeWith != null) {...
} else {
...
/*
* 🏁Pass the remaining arguments to SystemServer.
*/
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
}

/* should never reach here */
}

Step2 RuntimeInit::zygoteInit(…)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// frameworks/base/core/java/com/android/internal/os/RuntimeInit.java:269
public static final void zygoteInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
throws ZygoteInit.MethodAndArgsCaller {
...
commonInit();
nativeZygoteInit(); // 启动一个Binder线程池
applicationInit(targetSdkVersion, argv, classLoader);
}
// :299
private static void applicationInit(int targetSdkVersion, String[] argv, ClassLoader classLoader)
throws ZygoteInit.MethodAndArgsCaller {
// If the application calls System.exit(), terminate the process
// immediately without running any shutdown hooks. It is not possible to
// shutdown an Android application gracefully. Among other things, the
// Android runtime shutdown hooks close the Binder driver, which can cause
// leftover running threads to crash before the process actually exits.
nativeSetExitWithoutCleanup(true);

// We want to be fairly aggressive about heap utilization, to avoid
// holding on to a lot of memory that isn't needed.
VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);
VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);

final Arguments args;
try {
args = new Arguments(argv);
} catch (IllegalArgumentException ex) {...}
...
// Remaining arguments are passed to the start class's static main
// 🏁调用函数com.android.server.SystemServer::main(...)
invokeStaticMain(args.startClass, args.startArgs, classLoader);
}

参见《创世记(一)——Zygote进程的启动》之Step10 ,在函数ZygoteInit::startSystemServer(…)中以硬编码的形式组织system的启动参数,其中args.startClass=”com.android.server.SystemServer”。

Step3 RuntimeInit::invokeStaticMain(…)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// frameworks/base/core/java/com/android/internal/os/RuntimeInit.java:198
private static void invokeStaticMain(String className, String[] argv, ClassLoader classLoader)
throws ZygoteInit.MethodAndArgsCaller {
Class<?> cl;

try {
cl = Class.forName(className, true, classLoader);
} catch (ClassNotFoundException ex) {...}

Method m;
try {
m = cl.getMethod("main", new Class[] { String[].class });
} catch
...
/*
* This throw gets caught in ZygoteInit.main(), which responds
* by invoking the exception's run() method. This arrangement
* clears up all the stack frames that were required in setting
* up the process.
*/
throw new ZygoteInit.MethodAndArgsCaller(m, argv);
}

函数尾部的注释说的很明白,异常是在《创世记(一)——Zygote进程的启动》之Step8 ZygoteInit::main函数中捕获和执行,这个异常是com.android.server.SystemServer::main函数,捕获该异常后将执行此函数。这个手法是为了清除所有堆栈,使system进程认为函数com.android.server.SystemServer::main是进程执行的起点。

Step4 SystemServer::main(…)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// frameworks/base/services/java/com/android/server/SystemServer.java
public final class SystemServer {
// :167
public static void main(String[] args) {
new SystemServer().run();
}
// :176
private void run() {
...
// Start services.
try {
startBootstrapServices(); // 🏁
startCoreServices();
startOtherServices();
} catch (Throwable ex) {...}
...
// Loop forever.
Looper.loop();
...
}

Step5 SystemServer::startBootstrapServices()

在这里可以找到很多熟悉的Service启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// frameworks/base/services/java/com/android/server/SystemServer.java:322
private void startBootstrapServices() {
...
// Activity manager runs the show.
mActivityManagerService = mSystemServiceManager.startService(
ActivityManagerService.Lifecycle.class).getService();
...
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);

...
mSystemServiceManager.startService(LightsService.class);

...
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);

...
mSystemServiceManager.startBootPhase(SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);

...
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
...
startSensorService();
}

总结

本节system进程的启动只是前一节Zygote进程启动的一个环节,system进程的主要作用就是启动了若干系统服务,如ActivityManagerService、PackageManagerService等。启动完毕之后返回到Zygote进程的ZygoteInit::main(…)函数 ,将等待处理来自ActivityManagerService启动新进程的请求。
system进程的启动过程如下:
system进程的启动过程