SystemServer是由zygote启动的,启动后会先走main方法,看注释也可以看到此方法是"The main entry point from zygote."
接着往下走,就到了run方法
frameworks/base/services/java/com/android/server/SystemServer.java
public static void main(String[] args) { new SystemServer().run(); } private void run() { ... Looper.prepareMainLooper(); Looper.getMainLooper().setSlowLogThresholdMs( SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS); // Start services. try { t.traceBegin("StartServices"); startBootstrapServices(t); startCoreServices(t); startOtherServices(t); } catch (Throwable ex) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting system services", ex); throw ex; } finally { t.traceEnd(); // StartServices } ... // Loop forever. Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }然后走startBootstrapServices,这里主要是启动一些系统的关键服务,这些服务具有复杂的相互依赖关系。
/** * Starts the small tangle of critical services that are needed to get the system off the * ground. These services have complex mutual dependencies which is why we initialize them all * in one place here. Unless your service is also entwined in these dependencies, it should be * initialized in one of the other functions. */ private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) { t.traceBegin("startBootstrapServices"); ... // 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); mActivityManagerService.setSystemServiceManager(mSystemServiceManager); mActivityManagerService.setInstaller(installer); mWindowManagerGlobalLock = atm.getGlobalLock(); t.traceEnd(); ... // Set up the Application instance for the system process and get started. t.traceBegin("SetSystemProcess"); mActivityManagerService.setSystemProcess(); t.traceEnd(); ... t.traceBegin("StartPackageManagerService"); try { Watchdog.getInstance().pauseWatchingCurrentThread("packagemanagermain"); mPackageManagerService = PackageManagerService.main(mSystemContext, installer, domainVerificationService, mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore); } finally { Watchdog.getInstance().resumeWatchingCurrentThread("packagemanagermain"); } }然后走startCoreServices方法,这个方法主要是启动一些在开机过程中没有互相依赖的基本服务。
/** * Starts some essential services that are not tangled up in the bootstrap process. */ private void startCoreServices(@NonNull TimingsTraceAndSlog t) { t.traceBegin("startCoreServices"); // Service for system config t.traceBegin("StartSystemConfigService"); mSystemServiceManager.startService(SystemConfigService.class); t.traceEnd(); t.traceBegin("StartBatteryService"); // Tracks the battery level. Requires LightService. mSystemServiceManager.startService(BatteryService.class); t.traceEnd(); ... // Service for GPU and GPU driver. t.traceBegin("GpuService"); mSystemServiceManager.startService(GpuService.class); t.traceEnd(); // Handles system process requests for remotely provisioned keys & data. t.traceBegin("StartRemoteProvisioningService"); mSystemServiceManager.startService(RemoteProvisioningService.class); t.traceEnd(); // TODO(b/277600174): Start CpuMonitorService on all builds and not just on debuggable // builds once the Android JobScheduler starts using this service. if (Build.IS_DEBUGGABLE || Build.IS_ENG) { // Service for CPU monitor. t.traceBegin("CpuMonitorService"); mSystemServiceManager.startService(CpuMonitorService.class); t.traceEnd(); } t.traceEnd(); // startCoreServices }然后走startOtherServices方法,此方法中主要是启动一些系统中其他的服务
/** * Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized. */ private void startOtherServices(@NonNull TimingsTraceAndSlog t) { t.traceBegin("startOtherServices"); final Context context = mSystemContext; DynamicSystemService dynamicSystem = null; IStorageManager storageManager = null; NetworkManagementService networkManagement = null; IpSecService ipSecService = null; VpnManagerService vpnManager = null; VcnManagementService vcnManagement = null; NetworkStatsService networkStats = null; NetworkPolicyManagerService networkPolicy = null; NsdService serviceDiscovery = null; WindowManagerService wm = null; SerialService serial = null; NetworkTimeUpdateService networkTimeUpdater = null; InputManagerService inputManager = null; TelephonyRegistry telephonyRegistry = null; ConsumerIrService consumerIr = null; MmsServiceBroker mmsService = null; HardwarePropertiesManagerService hardwarePropertiesService = null; PacProxyService pacProxyService = null; ... t.traceBegin("StartVibratorManagerService"); mSystemServiceManager.startService(VibratorManagerService.Lifecycle.class); t.traceEnd(); t.traceBegin("StartDynamicSystemService"); dynamicSystem = new DynamicSystemService(context); ServiceManager.addService("dynamic_system", dynamicSystem); t.traceEnd(); if (!isWatch) { t.traceBegin("StartConsumerIrService"); consumerIr = new ConsumerIrService(context); ServiceManager.addService(Context.CONSUMER_IR_SERVICE, consumerIr); t.traceEnd(); } t.traceBegin("StartAlarmManagerService"); mSystemServiceManager.startService(ALARM_MANAGER_SERVICE_CLASS); t.traceEnd(); t.traceBegin("StartInputManagerService"); inputManager = new InputManagerService(context); t.traceEnd(); ... }以下是一个在systemserver中启动AMS的流程
且WindowManagerService以及ActivityManagerService等的服务的进程号都是SystemServer,但是是跑在不同的线程上