基于Android10.0,分析SystemServer启动
一、SystemServer启动
上一节中讲到了从开机到SystemServer中main方法的启动过程,下面将介绍下SystemServer中的main方法,该方法主要执行的主要流程如下:
1 | SystemServer.main |
1.1 SystemServer.main
1 | /** |
1.2 SystemServer.run
1 | private void run() { |
1.3 performPendingShutdown
1 | private void performPendingShutdown() { |
1.4 createSystemContext
1 | private void createSystemContext() { |
1.5 startBootstrapServices
1 | /** |
1.6 startCoreServices
1 | /** |
1.7 startOtherServices
该方法较长,有一千多行,逻辑很简单,主要是启动一系列的服务分类,后面将会对服务进行一个简单分类。
1 | /** |
SystemServer启动各种服务的最后一个环节是AMS.systemReady,这里以后会对AMS启动流程详细介绍。
这样SystemServer的主线程的启动流程就结束了,进入loop状态,等待其他线程通过handler发送消息到主线程再处理。
二、服务启动阶段
整个SystemServer的启动过程中,mSystemServiceManager.startBootPhase一直在其中:
systemserver进程创建准备完成
0-100
创建DPS、AMS、PMS、RSS、LS、SS、DMS服务
100-480
创建PKMS、UMS、OMS、BS、BMS等服务
480-500无任何操作
500-520
WMS、PMS、PKMS、DMS依次调用systemReady方法
520-550
定制的特殊服务
550-600
启动SystemUi,剩下服务调用systemReady,启动watchDog
600-1000
各种服务调用systemRunning方法,AMS调用finishBooting
其中PHASE_BOOT_COMPLETED=1000,该阶段是Boot和home应用启动完成。系统服务更倾向监听该阶段,而不是注册广播ACTION_BOOT_COMPLETED,从而降低系统延迟。
各个阶段在源码的位置大概如下:
1 | public final class SystemServer{ |
1 | public class ActivityManagerService |
下面将介绍下每个阶段完成的工作:
2.1 Phase 0
创建下面引导服务
DeviceIdentifiersPolicyService
ActivityManagerService
PowerManagerService
RecoverySystemService
LightsService
SidekickService
DisplayManagerService
2.2 Phase 100
进入阶段PHASE_WAIT_FOR_DEFAULT_DISPLAY=100回调服务
onBootPhase(100)
DisplayManagerService监听调用
然后创建下面服务
PackageManagerService
UserManagerService
OverlayManagerService
BatteryService
UsageStatsService
WebViewUpdateService
BinderCallsStatsService
BugreportManagerService
GpuService
2.3 Phase 480
进入PHASE_LOCK_SETTINGS_READY=480回调服务
onBootPhase(480)
DevicePolicyManagerService监听调用
进入阶段480后立马进入到500
2.4 Phase 500
进入PHASE_SYSTEM_SERVICES_READY=500
进入到这个阶段,服务能安全的调用核心系统服务
各大服务执行systemReady
wm.systemReady();
mPowerManagerService.systemReady()
mPackageManagerService.systemReady()
mDisplayManagerService.systemReady()
DeviceSpecificServices
2.5 Phase 520
进入到PHASE_DEVICE_SPECIFIC_SERVICES_READY=520
onBootPhase(520)
DeviceSpecificServices监听调用,为厂商定制的一些特殊服务
最后执行AMS.SystemReady
2.6 Phase 550
进入到PHASE_ACTIVITY_MANAGER_READY=550
onBootPhase(550)
此时 AMS.mSystemReady = true,已经准备就绪,该阶段服务能广播Intent,但是systemserver主线程并没有就绪.
主要是个服务执行systemReady,还有AMS启动Native crash监控,加载webview,启动SystemUi等
mActivityManagerService.startObservingNativeCrashes();
mWebViewUpdateService.prepareWebViewInSystemServer();
CarServiceHelperService
startSystemUi(context, windowManagerF)
networkManagementF.systemReady()
networkPolicyF.networkScoreAndNetworkManagementServiceReady()
ipSecServiceF.systemReady()
networkStatsF.systemReady()
connectivityF.systemReady()
networkPolicyF.systemReady()
Watchdog.getInstance().start();
mPackageManagerService.waitForAppDataPrepared()
2.7 Phase 600
进入到PHASE_THIRD_PARTY_APPS_CAN_START=600
onBootPhase(600)
主要是各种服务的systemRunning
NetworkStackClient.getInstance().start(context);
locationF.systemRunning()
countryDetectorF.systemRunning()
networkTimeUpdaterF.systemRunning()
inputManagerF.systemRunning()
telephonyRegistryF.systemRunning()
mediaRouterF.systemRunning()
mmsServiceF.systemRunning()
incident.systemRunning()
2.8 Phase 1000
经过一序列的流程,最后执行AMS.finishBooting,则进入到PHASE_BOOT_COMPLETED=1000
onBootPhase(1000)
到此,系统服务启动完成,systemsever进程启动完成进入到Loop().loop状态,等待消息队列MessageQueue中的消息到来,则马上进入执行状态。
三、服务类别
systemsever进程,从源码的角度分为引导服务,核心服务,其他服务3类,主要如下:
1.引导服务(11个)
DeviceIdentifiersPolicyService
ActivityManagerService
PowerManagerService
RecoverySystemService
LightsService
SidekickService
DisplayManagerService
PackageManagerService
UserManagerService
OverlayManagerService
SensorService
2.核心服务(6个)
BatteryService
UsageStatsService
WebViewUpdateService
BinderCallsStatsService
BugreportManagerService
GpuService
3.其他服务(100个+)
KeyAttestationApplicationIdProviderService | KeyChainSystemService | SchedulingPolicyService |
---|---|---|
TelecomLoaderService | AccountManagerService | ContentService |
DropBoxManagerService | VibratorService | DynamicAndroidService |
ConsumerIrService | AlarmManagerService | InputManagerService |
WindowManagerService | VrManagerService | BluetoothService |
IpConnectivityMetrics | NetworkWatchlistService | PinnerService |
InputMethodManagerService | AccessibilityManagerService | StartStorageManagerService |
StorageStatsService | UiModeManagerService | LockSettingsService |
PersistentDataBlockService | OemLockService | DeviceIdleController |
DevicePolicyManagerService | StatusBarManagerService | ClipboardService |
NetworkManagementService | StartIpSecService | TextServicesManagerService |
TextClassificationManagerService | NetworkScoreService | NetworkStatsService |
NetworkPolicyManagerService | WifiService | WifiScanningService |
RttService | WifiAwareService | WifiP2pService |
LowpanService | EthernetService | ConnectivityService |
NsdService | SystemUpdateManagerService | UpdateLockService |
NotificationManagerService | DeviceStorageMonitorService | LocationManagerService |
CountryDetectorService | TimeDetectorService | SearchManagerService |
WallpaperManagerService | AudioService | BroadcastRadioService |
MidiService | UsbService | SerialService |
HardwarePropertiesManagerService | TwilightService | ColorDisplayService |
JobSchedulerService | SoundTriggerService | TrustManagerService |
BackupManagerService | AppWidgerService | VoiceInteractionManagerService |
GestureLauncherService | SensorNotificationService | ContextHubSystemService |
ContextHubSystemService | DiskStatsService | TimeZoneRulesManagerService |
NetworkTimeUpdateService | EmergencyAffordanceService | DreamManagerService |
GraphicsStatsService | CoverageService | PrintManagerService |
CompanionDeviceManagerService | RestrictionsManagerService | MediaSessionService |
MediaUpdateService | HdmiControlService | TvInputManagerService |
MediaResourceMonitorService | TvRemoteService | MediaRouterService |
FingerprintService | BackgroundDexOptService | PruneInstantAppsJobService |
ShortcutService | LauncherAppsService | CrossProfileAppsService |
MediaProjectionManagerService | WearConfigService | WearConnectivityService |
WearTimeService | WearLeftyService | WearGlobalActionsService |
SliceManagerService | CameraServiceProxy | IoTSystemService |
StatsCompanionService | MmsServiceBroker | AutofillManagerService |
DeviceSpecificServices |
以后的文章将介绍一些常用的Service。
下面是华为荣耀9(Android9.0),SystemServer的开机日志:
1 | --------- beginning of main |