SystemServer的路径为/frameworks/base/services/java/com/android/server/SystemServer.java。
SystemServer类主要是启动系统服务的,系统服务分为三类:引导服务BootstrapServices,核心服务CoreServices,其他服务OtherServices。除了这些系统服务外,还有SystemService、ServiceManager、SystemServiceManager类,请参考本博客的前三节,之后会结合SystemServer类整理这几个类的类图,梳理service的完成启动流程。
Framework层SystemServiceManager类的详解
Framework层ServiceManager类的详细介绍
目录
5. SystemServiceManager.startService和ServiceManger.addService的区别
6. private void startBootstrapService()
6. private void startCoreServices()
7. private void startOtherServices()
下图和本文都是基于高通平台的Android 12的SystemServer,与原生谷歌的SystemServer有些许函数上的差别。
一、成员变量
1.各个SystemService的全路径类名
eg: private static final String WALLPAPER_SERVICE_CLASS = "com.android.server.wallpaper.WallpaperManagerService$Lifecycle";
2. private static final int sMaxBinderThreads = 31
system_server最大的binder数目。
3. private Context mSystemContext;
该属性在run()中调用createSystemContext()被赋值。
private void createSystemContext() {
ActivityThread activityThread = ActivityThread.systemMain();
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
4. private SystemServiceManager mSystemServiceManager;
5. 各种service的引用
private DisplayManagerService mDisplayManagerService;
private PackageManagerService mPackageManagerService;
private PackageManager mPackageManager;
private ContentResolver mContentResolver;
private DataLoaderManagerService mDataLoaderManagerService;
6. private boolean mRuntimeRestart
该属性在SystemServer的构造函数中被赋值,只有sys.boot_competed属性为1时,mRuntimeRestart = true.
二、成员函数
1. 声明的native 方法
(1) private static native void startSensorService();
启动sensor service,这是一个阻塞的调用,可能需要时间。
(2) private static native void startHidlServices();
启动所有运行在system server中的hidl service。这个方法是耗时操作。
2. main()方法
public static void main(String[] args) {
new SystemServer().run();
}
3. 构造函数
public SystemServer() {
//主要是记录mStartCount, 初始化mRuntimeStartElapsedTime和mRuntimeStartUpTime。
}
4. private void run()
该方法主要的逻辑有
- 设置各种系统属性,通过SystemProperties.set("key", "value");
- 准备主Looper线程。
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
Looper.prepareMainLooper();
Looper.getMainLooper().setSlowLogThresholdMs(SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
..
Looper.loop();
- 加载native service so:System.loadLibrary("android_servers");
- 初始化mSystemContext,它取值为ActivityThread.systemMain().getSystemContext();
- 初始化mSystemServiceManager;
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart, mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
- startBootstrapServices()
- startCoreServices()
- startOtherServices()
5. SystemServiceManager.startService和ServiceManger.addService的区别
(1) 由mSystemServiceManager.startService()的Service必须是SystemService的子类,可以直接extends SystemService,也可以创建静态内部类Lifecycle extends SystemService。它的时序图如下图所示,最终也会走到ServiceManager的addService()。
(2) 由SystemManager.addService()添加的service只要是IBinder即可,不需要是SystemService的子类。
6. private void startBootstrapService()
启动系统所需要的最小关键服务集,这些服务具有复杂的相互依赖关系,所以在这里初始化他们。除非您的服务也与这些依赖项密切相关,否则应该在其他函数中对其初始化。
启动的服务有:
(1) SystemConfig,该service提交给SystemServiceInitThreadPool线程池执行处理。
(2) PlatformCompat和PlatformCompatNativer这两个service由ServiceManager.addService()到service manager中。
(3) FileIntegrityService、Installer(该service完成启动后才可以使用合适的权限创建关键目录,例如/data/user等,要在其他服务初始化之前完成该操作)、DeviceIdentifiersPolicyService、UriGrantsManagerService.Lifecycle、PowerStatsService、DataLoaderManagerService、PowerManagerService、ThermalManagerService、RecoverySystemService、DisplayManagerService、LightsService、UserManagerService、OverlayManagerService、SensorPrivacyService,以上service都是通过mSystemServiceManager.startService(Class),该方法调用各自service的onStart()方法。
(4) 初始化mActivityManagerService。
ActivityTaskManagerService atm = mSystemServiceManager.startService(ActivityTaskManagerService.Lifecycle.class).getService();
mActivityManagerService = ActivityManagerService.Lifecycle.startService(mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
在启动PowerManagerService启动之后,AMS要初始化power manager。
// Now that the power manager has been started, let the activity manager
// initialize power management features.
t.traceBegin("InitPowerManagement");
mActivityManagerService.initPowerManagement();
t.traceEnd();
(5) 启动DisplayManagerService之后,会调用mSystemServiceManager.startBootPhase(),回调这个boot_phase=SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY。
startBootPhase(int phase)会回调所有启动的service这个boot_phase。
(6) 启动PackageManagerService。
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");
}
6. private void startCoreServices()
启动在bootstrap进程中没有纠缠的基础服务。
SystemConfigService、BatteryService、UsageStatsService、WebViewUpdateService、CachedDeviceStateService、BinderCallsStatsService、LooperStatsServices、BugreportManagerService、GpuService,以上service均是通过mSystemServiceManager.startService(Class)启动。
7. private void startOtherServices()
启动一大堆其他服务。如果新增systemService没有其他依赖,则最好在该方法中启动。
(1) 使用SystemServerInitThreadPool执行Second_Zygote_Preload、starHIDLServices。
(2) mContentResolver初始化
mContentResolver = mSystemContext.getContentResolver()
(3) 使用ServiceManager.addService("name", IBinder)启动SchedulingPolicyService、TelephonyRegistry、VibratorService、DynamicSystemService、ConsumerIrService、
WindowManagerService、InputManagerService、NetworkManagementService、NetworkStatsService等。
(4) 使用mSystemServiceManager.startService()启动TelecomLoaderService、AlarmManagerService、VrManagerService、BluetoothService、IpConnectivityMetrics、
NetworkWatchlistService、StorageManagerService、StorageStatsService、UiModeManagerService、LockSettingsService、PersistentDataBlock、AudioServise、CameraServiceProxy等。
t.traceBegin("StartBluetoothService");
mSystemServiceManager.startService(BluetoothService.class);
t.traceEnd();
t.traceBegin("NetworkWatchlistService");
mSystemServiceManager.startService(NetworkWatchlistService.Lifecycle.class);
t.traceEnd();
(5) 初始化inputManger Service并启动。
inputManager = new InputManagerService(context);
(6)初始化WindowManagerService,并赋值给AMS。
t.traceBegin("StartWindowManagerService");
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore, new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager, /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
t.traceEnd();
t.traceBegin("SetWindowManagerService");
mActivityManagerService.setWindowManager(wm);
t.traceEnd();
t.traceBegin("WindowManagerServiceOnInitReady");
wm.onInitReady();
t.traceEnd();
(7) 启动inputManagerService,注意启动的时机,它要在WindowManagerService之前初始化,在wm之后启动,然后把WindowManagerService的回调赋值给inputManager。
t.traceBegin("StartInputManager");
inputManager.setWindowManagerCallbacks(wm.getInputManagerCallback());
inputManager.start();
t.traceEnd();
(8) 调用mSystemServiceManager.startBootPhase(),回调不同的phase给全部启动的service的onBootPhase(int phase)。
- LockSettingService启动完成后,调用lockSetting.systemReady()之后,回调phase=SystemService.PHASE_LOCK_SETTINGS_READY和phase=SystemService.PHASE_SYSTEM_SERVICES_READY。
t.traceBegin("MakeLockSettingsServiceReady");
if (lockSettings != null) {
try {
lockSettings.systemReady();
} catch (Throwable e) {
reportWtf("making Lock Settings Service ready", e);
}
}
t.traceEnd();
// Needed by DevicePolicyManager for initialization
t.traceBegin("StartBootPhaseLockSettingsReady");
mSystemServiceManager.startBootPhase(t, SystemService.PHASE_LOCK_SETTINGS_READY);
t.traceEnd();
t.traceBegin("StartBootPhaseSystemServicesReady");
mSystemServiceManager.startBootPhase(t, SystemService.PHASE_SYSTEM_SERVICES_READY);
t.traceEnd();
注意:回调这两个phase之后,会调用WindowManagerService的wm.systemReady()和mPowerManagerService.systemReady()、 mPackageManagerService.systemReady()和mDisplayManagerService.systemReady()。
- 启动DeviceSpecificServices之后,回调phase=SystemService.PHASE_DEVICE_SPECIFIC_SERVICE_READY。
- 在mActvivityManagerService.systemReady()方法中最初先回调phase=SystemService.PHASE_ACTIVITY_MANAGER_READY。
AMS.systemReady()中还执行了以下方法:
1.startSystemUi()也是该该方法中启动SystemUi的;
2.NetworkManagementServiceReady.systemReady();3.NetworkStatsServiceReady.systemReady();
4.ConnectivityServiceReady.systemReady();
5.mPackageManagerService.waitForAppDataPrepared();
6.执行完第5步后回调phase = SystemService.PHASE_ACTIVITY_MANAGER_READY;
7. InputManagerService.systemRunning()等。
(10) startOtherServices()总结
①.启动各种服务包括WindowManagerService、InputMangerService、AudioServise、CameraServiceProxy、BluetoothService、VrManagerService等。
②.调用mServiceManagerService.startBootPhase()回调6种phase给启动的systemService的onBootPhase()。
③.调用各个主要服务的systemReady(),包括WindowManagerService、PowerManagerService、PackageManagerService、DisplayManagerService和ActivityManagerService等。
④.在ActivityManagerService.systemReady()的参数中调用startSystemUi()。
private static void startSystemUi(Context context, WindowManagerService windowManager) {
PackageManagerInternal pm = LocalServices.getService(PackageManagerInternal.class);
Intent intent = new Intent();
intent.setComponent(pm.getSystemUiServiceComponent());
intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
//Slog.d(TAG, "Starting service: " + intent);
context.startServiceAsUser(intent, UserHandle.SYSTEM);
windowManager.onSystemUiStarted();
}