AMS的启动过程

基于Android U

AMS的启动是在SystemServer进程中启动的。从SystemServer的main()方法开始。

1
2
3
4
frameworks/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) {
new SystemServer().run();
}

调用了run()方法。

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
frameworks/base/services/java/com/android/server/SystemServer.java
private void run() {
...
try {
...
// 创建消息Looper
Looper.prepareMainLooper();
...
// Initialize native services.
System.loadLibrary("android_servers"); // 1
...
// Initialize the system context.
// 创建系统的Context
createSystemContext();
...
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext); // 2
mSystemServiceManager.setStartInfo(mRuntimeRestart,
mRuntimeStartElapsedTime, mRuntimeStartUptime);
mDumper.addDumpable(mSystemServiceManager);

LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
...
} finally {
...
}
...
// Start services.
try {
t.traceBegin("StartServices");
// 启动引导服务
startBootstrapServices(t); //3
// 启动核心服务
startCoreServices(t); // 4
// 启动其他服务
startOtherServices(t); // 5
// 启动apex服务
startApexServices(t); // 6
// Only update the timeout after starting all the services so that we use
// the default timeout to start system server.
updateWatchdogTimeout(t);
} catch (Throwable ex) {
...
} finally {
...
}
...
// Loop forever.
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}

注释1处加载了动态库libandroid_servers.so。

注释2处创建SystemServiceManager,它会对系统的服务进行创建、启动和生命周期管理。

注释3处的startBootstrapServices()方法中用SystemServiceManager启动了ActivityManagerService、PowerManagerService、PackageManagerService等服务。

注释4处的startCoreServices()方法中则启动了BatteryService、UsageStatsService、WebViewUpdateService等服务。

注释5处的startOtherServices()方法中启动了AlarmManagerService、VrManagerService、InputManagerService等服务,主要是一些和设备功能相关的服务,是一些非紧要和不需要立即启动的服务。

注释6处的startApexServices()方法会遍历所有已安装的Apex服务,并调用它们的启动方法,使它们在系统启动时自动运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
...
// Activity manager runs the show.
t.traceBegin("StartActivityManager");
// TODO: Might need to move after migration to WM.
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm); // 1
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
t.traceEnd();
...
}

调用ActivityManagerService#Lifecycle.startService()。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService$Lifecycle
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;
private static ActivityTaskManagerService sAtm;

public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context, sAtm);
}

public static ActivityManagerService startService(
SystemServiceManager ssm, ActivityTaskManagerService atm) {
sAtm = atm;
return ssm.startService(ActivityManagerService.Lifecycle.class).getService();
}
}

调用SystemServiceManager的startService()方法。

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
33
34
35
36
37
frameworks/base/services/core/java/com/android/server/SystemServiceManager.java
public <T extends SystemService> T startService(Class<T> serviceClass) {
try {
...
final T service;
try {
Constructor<T> constructor = serviceClass.getConstructor(Context.class);
service = constructor.newInstance(mContext); // 1
} catch (InstantiationException ex) {
...
} catch (IllegalAccessException ex) {
...
} catch (NoSuchMethodException ex) {
...
} catch (InvocationTargetException ex) {
...
}

startService(service); // 2
return service;
} finally {
...
}
}

public void startService(@NonNull final SystemService service) {
...
// Register it.
mServices.add(service); // 3
...
try {
service.onStart(); // 4
} catch (RuntimeException ex) {
...
}
...
}

注释1处SystemServiceManager中通过反射,调用了ActivityManagerService$Lifecycle的构造方法。

注释2处又调用了startService()。

注释3处将service对象添加到ArrayList类型的mServices中来完成注册。

注释4处调用service.onStart()来启动service对象,即ActivityManagerService$Lifecycle.onStart()。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService$Lifecycle
public static final class Lifecycle extends SystemService {
private final ActivityManagerService mService;

public Lifecycle(Context context) {
super(context);
mService = new ActivityManagerService(context, sAtm); // 1
}

public void onStart() {
mService.start(); // 2
}

public ActivityManagerService getService() {
return mService; // 3
}
}

上面的代码需要结合SystemServiceManager的startService()方法来分析。

注释1处,在Lifecycle的构造方法中创建了AMS实例。当调用SystemService类型的service的onStart()方法时,实际上是调用了注释2处AMS的start()方法。注释3处的Lifecycle的getService()方法返回AMS实例,这样我们就知道SystemServer的startBootstrapServices()方法的注释1处实际得到的就是AMS实例。


AMS的启动过程
https://citrus-maxima.github.io/2024/03/10/AMS的启动过程/
作者
柚子树
发布于
2024年3月10日
许可协议