Bootstrap服务
startBootstrapServices() 不是按类名排列的服务清单,而是一组相互纠缠的最小依赖图。Android 17 的源码把 ART module manager、Watchdog、Installer、ActivityManager/ActivityTaskManager、PowerManager、DisplayManager、PackageManager 和 SensorService 放在一条有明确前置条件的顺序上。
本文只追踪 Bootstrap 组的调用方、状态变化、生效时机和失败传播;Core、Other、Apex 服务留给后续文章。
1. 组入口与trace
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startBootstrapServices");
// 依赖纠缠的基础服务按源码顺序启动。
...
t.traceEnd();
}SystemServer.run() 只调用一次该方法;每个内部 traceBegin/traceEnd 是启动耗时和失败定位的阶段边界。任一服务抛异常都会被 run() 的 StartServices catch 记录并重新抛出,Core/Other/Apex 不再执行。
2. ART与Watchdog
t.traceBegin("ArtModuleServiceInitializer");
ArtModuleServiceInitializer.setArtModuleServiceManager(
new ArtModuleServiceManager());
t.traceEnd();
t.traceBegin("StartWatchdog");
final Watchdog watchdog = Watchdog.getInstance();
watchdog.start();
mDumper.addDumpable(watchdog);
t.traceEnd();ART module manager 来自独立 service-art.jar。源码注释要求它早于 DexUseManagerLocal 和 PackageManagerService:class linker 与 GC 互斥,而 PMS 会大量分配对象,过晚加载可能持续等待 GC。Watchdog 紧接着启动,目的是从早期启动阶段就能检测主线程或关键 handler 的死锁。
3. 日志与兼容
ServiceManager.addService(
Context.PROTOLOG_CONFIGURATION_SERVICE,
new ProtoLogConfigurationServiceImpl());
ProtoLog.init(WmProtoLogGroups.values());
PlatformCompat platformCompat =
new PlatformCompat(mSystemContext);
ServiceManager.addService(
Context.PLATFORM_COMPAT_SERVICE, platformCompat);
ServiceManager.addService(
Context.PLATFORM_COMPAT_NATIVE_SERVICE,
new PlatformCompatNative(platformCompat));
AppCompatCallbacks.install(
new long[0], new long[0], false);ProtoLog 与 PlatformCompat 通过 ServiceManager 进入跨进程名称表;AppCompatCallbacks 写入当前 runtime 的兼容性状态。AMS、PMS 等后续消费者依赖 PlatformCompat,所以它必须在这些服务前发布。
4. Installer前置
mSystemServiceManager.startService(FileIntegrityService.class);
Installer installer = mSystemServiceManager.startService(
Installer.class);
mSystemServiceManager.startService(
DeviceIdentifiersPolicyService.class);FileIntegrityService 要在应用成为第一个消费者前启动;Installer 通过 installd 准备 /data/user 等关键目录和权限。installer 实例随后传给 PackageManagerService.main(),因此这里建立的是后续调用方会消费的对象状态。
5. 策略迁移
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
mSystemServiceManager.startService(FeatureFlagsService.class);
mSystemServiceManager.startService(
UriGrantsManagerService.Lifecycle.class);
mSystemServiceManager.startService(PowerStatsService.class);
startIStatsService();
startMemtrackProxyService();
LocalServices.addService(PermissionMigrationHelper.class,
new PermissionMigrationHelperImpl());
LocalServices.addService(AppOpMigrationHelper.class,
new AppOpMigrationHelperImpl());
mSystemServiceManager.startService(AccessCheckingService.class);两个 migration helper 用 LocalServices 供同一 SystemServer 进程消费,不会生成 ServiceManager handle;AccessCheckingService 再通过 SystemService 生命周期接入。
6. ATM与AMS顺序
ActivityTaskManagerService atm =
mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class)
.getService();
mActivityManagerService =
ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(
mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();ATM 先创建,再把实例交给 AMS 生命周期启动;AMS 保存 SystemServiceManager/Installer,并暴露后续系统进程状态。mWindowManagerGlobalLock 从 ATM 取出,成为 Activity/Window 协调的锁 owner。
7. Power与数据加载
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
mDataLoaderManagerService =
mSystemServiceManager.startService(
DataLoaderManagerService.class);
mIncrementalServiceHandle = startIncrementalService();
mPowerManagerService = mSystemServiceManager.startService(
PowerManagerService.class);
mSystemServiceManager.startService(ThermalManagerService.class);
mActivityManagerService.initPowerManagement();DataLoader/Incremental 必须早于 PMS;PowerManager 要尽早处理 native daemon 的 Binder 请求,并在 AMS 初始化 power management 前可用。
8. Display与PMS
mSystemServiceManager.startService(
RecoverySystemService.Lifecycle.class);
mSystemServiceManager.startService(LightsService.class);
mDisplayManagerService = mSystemServiceManager.startService(
DisplayManagerService.class);
mSystemServiceManager.startBootPhase(
t, SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
DomainVerificationService domainVerificationService =
new DomainVerificationService(
mSystemContext, SystemConfig.getInstance(),
platformCompat);
mSystemServiceManager.startService(domainVerificationService);
Watchdog.getInstance().pauseWatchingCurrentThread(
"packagemanagermain");
try {
mPackageManagerService = PackageManagerService.main(
mSystemContext, installer,
domainVerificationService,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF);
} finally {
Watchdog.getInstance().resumeWatchingCurrentThread(
"packagemanagermain");
}DisplayManager 先提供 display metrics,再通过 PHASE_WAIT_FOR_DEFAULT_DISPLAY 建立默认 display,PMS 才继续初始化。PMS 主初始化可能很慢,源码只暂停当前线程的 Watchdog 监控并在 finally 恢复,不是关闭 Watchdog。
9. Bootstrap收束
源码文件:frameworks/base/services/java/com/android/server/SystemServer.java
mUserManagerService = mSystemServiceManager
.startService(UserManagerService.LifeCycle.class)
.getService();
AttributeCache.init(mSystemContext);
mActivityManagerService.setSystemProcess();
platformCompat.registerPackageReceiver(mSystemContext);
watchdog.init(mSystemContext, mActivityManagerService);
mDisplayManagerService.setupSchedulerPolicies();
mSystemServiceManager.startService(
new OverlayManagerService(mSystemContext));
mSystemServiceManager.startService(
new ResourcesManagerService(mSystemContext));
mSystemServiceManager.startService(
new SensorPrivacyService(mSystemContext));
mSystemServiceManager.startService(SensorService.class);PMS 成功后,Bootstrap 才能建立 UserManager、AttributeCache、system process、Watchdog 的 AMS 绑定、Display scheduler、Overlay、Resources、SensorPrivacy 和 Sensor。SensorService 放在最后,因为源码明确要求它依赖 PackageManager、AppOps 和权限服务。
10. 失败定位与导航
- ART/class linker:检查
ArtModuleServiceInitializer是否早于 PMS; - 目录/权限:检查 Installer、installd 和 PMS 入参;
- ATM/AMS:检查 lifecycle 启动与 global lock;
- Display:检查 DisplayManager 和
PHASE_WAIT_FOR_DEFAULT_DISPLAY; - PMS:区分 Watchdog 暂停期间的合法长任务与真实异常;
- Sensor:确认 PackageManager、AppOps 和权限服务已经完成。
# 输入 Bootstrap 方法,输出服务调用顺序和 trace 标签。
rg -n "startBootstrapServices|ArtModuleServiceInitializer|StartWatchdog|StartInstaller|StartActivityManager|StartPowerManager|StartDisplayManager|StartPackageManagerService|StartSensorService" \
frameworks/base/services/java/com/android/server/SystemServer.java
# 输入生命周期管理器,输出 startService/startBootPhase 的实例化和回调。
rg -n "startService\\(|startBootPhase\\(|mServices|onStart" \
frameworks/base/services/core/java/com/android/server/SystemServiceManager.java下一篇将展开 startCoreServices(),不会重复本文的 Bootstrap 依赖和 PackageManager 前置条件。
