開始進入Framework的源碼分析,在分析源碼之前,先來了解一下android系統啟動的流程,如下圖所示:
在Zygote進程中會孵化一個SystemServer進程,在這個SystemServer進程中完成了大量的系統服務的啟動,這些系統服務啟動完成后才啟動桌面Launcher進程,那么這個SystemServer又是怎么啟動的。
SystemServer的啟動由于需要啟動大量的系統服務,所以每次開機都需要耗時較長才會顯示桌面。
啟動過程:
在進入源碼分析之前,先來根據下面的思維導圖了解一下整體流程,按照執行順序進行了排序。
1、準備主線程Looper
說明:
1、SystemServer的啟動從其Main方法執行;創建SystemServer實例調用其run方法;
2、配置系統時間、時區、語言等信息;
3、準備主線程Looper
//SystemServer.java
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
try {
// Record the process start information in sys props.
SystemProperties.set(SYSPROP_START_COUNT, String.valueOf(mStartCount));
SystemProperties.set(SYSPROP_START_ELAPSED, String.valueOf(mRuntimeStartElapsedTime));
SystemProperties.set(SYSPROP_START_UPTIME, String.valueOf(mRuntimeStartUptime));
if (System.currentTimeMillis() < EARLIEST_SUPPORTED_TIME) {
Slog.w(TAG, "System clock is before 1970; setting to 1970.");
SystemClock.setCurrentTimeMillis(EARLIEST_SUPPORTED_TIME);
}
//
// Default the timezone property to GMT if not set.
//設置時間
String timezoneProperty = SystemProperties.get("persist.sys.timezone");
if (timezoneProperty == null || timezoneProperty.isEmpty()) {
Slog.w(TAG, "Timezone not set; setting to GMT.");
SystemProperties.set("persist.sys.timezone", "GMT");
}
// If the system has "persist.sys.language" and friends set, replace them with
// "persist.sys.locale". Note that the default locale at this point is calculated
// using the "-Duser.locale" command line flag. That flag is usually populated by
// AndroidRuntime using the same set of system properties, but only the system_server
// and system apps are allowed to set them.
//
// NOTE: Most changes made here will need an equivalent change to
// core/jni/AndroidRuntime.cpp
if (!SystemProperties.get("persist.sys.language").isEmpty()) {
final String languageTag = Locale.getDefault().toLanguageTag();
SystemProperties.set("persist.sys.locale", languageTag);
SystemProperties.set("persist.sys.language", "");
SystemProperties.set("persist.sys.country", "");
SystemProperties.set("persist.sys.localevar", "");
}
// The system server should never make non-oneway calls
Binder.setWarnOnBlocking(true);
// The system server should always load safe labels
PackageItemInfo.forceSafeLabels();
// Default to FULL within the system server.
SQLiteGlobal.sDefaultSyncMode = SQLiteGlobal.SYNC_MODE_FULL;
// Deactivate SQLiteCompatibilityWalFlags until settings provider is initialized
SQLiteCompatibilityWalFlags.init(null);
// Here we go!
Slog.i(TAG, "Entered the Android system server!");
int uptimeMillis = (int) SystemClock.elapsedRealtime();
EventLog.writeEvent(EventLogTags.BOOT_PROGRESS_SYSTEM_RUN, uptimeMillis);
if (!mRuntimeRestart) {
MetricsLogger.histogram(null, "boot_system_server_init", uptimeMillis);
}
// In case the runtime switched since last boot (such as when
// the old runtime was removed in an OTA), set the system
// property so that it is in sync. We can | xq oqi't do this in
// libnativehelper's JniInvocation::Init code where we already
// had to fallback to a different runtime because it is
// running as root and we need to be the system user to set
// the property. http://b/11463182
SystemProperties.set("persist.sys.dalvik.vm.lib.2", VMRuntime.getRuntime().vmLibrary());
// Mmmmmm... more memory!
VMRuntime.getRuntime().clearGrowthLimit();
// The system server has to run all of the time, so it needs to be
// as efficient as possible with its memory usage.
VMRuntime.getRuntime().setTargetHeapUtilization(0.8f);
// Some devices rely on runtime fingerprint generation, so make sure
// we've defined it before booting further.
Build.ensureFingerprintProperty();
// Within the system server, it is an error to access Environment paths without
// explicitly specifying a user.
Environment.setUserRequired(true);
// Within the system server, any incoming Bundles should be defused
// to avoid throwing BadParcelableException.
BaseBundle.setShouldDefuse(true);
// Within the system server, when parceling exceptions, include the stack trace
Parcel.setStackTraceParceling(true);
// Ensure binder calls into the system always run at foreground priority.
BinderInternal.disableBackgroundScheduling(true);
// Increase the number of binder threads in system_server
BinderInternal.setMaxThreads(sMaxBinderThreads);
// Prepare the main looper thread (this thread).
android.os.Process.setThreadPriority(
android.os.Process.THREAD_PRIORITY_FOREGROUND);
android.os.Process.setCanSelfBackground(false);
//這里開始準備主線程的Looper
Looper.prepareMainLooper();
Looper.getMainLooper().setSlowLogThresholdMs(
SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
// Initialize native services.
System.loadLibrary("android_servers");
// Debug builds - allow heap profiling.
if (Build.IS_DEBUGGABLE) {
initZygoteChildHeapProfiling();
}
// Check whether we failed to shut down last time we tried.
// This call may not return.
performPendingShutdown();
// Initialize the system context.
createSystemContext();
// Create the system service manager.
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart,
mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
// Prepare the thread pool for init tasks that can be parallelized
SystemServerInitThreadPool.get();
} finally {
traceEnd(); // InitBeforeStartServices
}
2、加載nativeService:
// Initialize native services.
System.loadLibrary("android_servers");
3、創建systemContext
- 創建ActivityThread實例;
- 獲取systemContext、systemUiContext;
- 設置系統theme
// Initialize the system context.
createSystemContext();
private void createSystemContext() {
//創建ActivityThread
ActivityThread activityThread = ActivityThread.systemMain();
//獲取systemContext,并設置theme
mSystemContext = activityThread.getSystemContext();
mSystemContext.setTheme(DEFAULT_SYSTEM_THEME);
//獲取systemUiContenx ,設置theme
final Context systemUiContext = activityThread.getSystemUiContext();
systemUiContext.setTheme(DEFAULT_SYSTEM_THEME);
}
3.1 創建ActivityThread
//ActivityThread.java
public final class ActivityThread extends ClientTransactionHandler {
public static ActivityThread systemMain() {
// The system process on low-memory devices do not get to use hardware
// accelerated drawing, since this can add too much overhead to the process.
if (!ActivityManager.isHighEndGfx()) {
ThreadedRenderer.disable(true);
} else {
ThreadedRenderer.enableForegroundTrimming();
}
//1.這里new一個ActivityThread出來,通過構造方法可以明白是獲得了資源管理器實例
ActivityThread thread = new ActivityThread();
//2.這里的attach第一個參數是true,
thread.attach(true, 0);
return thread;
}
//構造方法
ActivityThread() {
mResourcesManager = ResourcesManager.getInstance();
}
private void attach(boolean system, long startSeq) {
sCurrentActivityThread = this;
mSystemThread = system;
if (!system) {
//注意由于第一個參數這時傳遞進來的true ,所以不會進入該分支
} else {
android.ddm.DdmHandleAppName.setAppName("system_process",
UserHandle.myUserId());
try {
//1.1 創建Instrumentation隊列,并將當前的ActivityThead賦值給instrumentation的成員變量
mInstrumentation = new Instrumentation();
mInstrumentation.basicInit(this);
//1.2 創建app上下文
ContextImpl context = ContextImpl.createAppContext(this, getSystemContext().mPackageInfo);
//1.3
mInitialApplication = context.mPackageInfo.makeApplication(true, null);
mInitialApplication.onCreate();
} catch (Exception e) {
throw new RuntimeException(
"Unable to instantiate Application():" + e.toString(), e);
}
}
//..........
}
//1.2.1 獲取systemContext
public ContextImpl getSystemContext() {
synchronized (this) {
if (mSystemContext == null) {
//1.2.1.1 創建systemContext
mSystemContext = ContextImpl.createSystemContext(this);
}
return mSystemContext;
}
}
}
//ContextImpl.java
class ContextImpl extends Context {
//1.2.1.2創建systemContext
static ContextImpl createSystemContext(ActivityThread mainThread) {
//1.2.1.2.1進入LoadedApk的構造方法:創建applicationInfo,系統資源、類加載器等
LoadedApk packageInfo = new LoadedApk(mainThread);
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
null, null);
//1.2.1.2.2 給context設置資源
context.setResources(packageInfo.getResources());
context.mResources.updateConfiguration(context.mResourcesManager.getConfiguration(),
context.mResourcesManager.getDisplayMetrics());
return context;
}
private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
@NonNull LoadedApk packageInfo, @Nullable String splitName,
@Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
@Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) {
mOuterContext = this;
// If creator didn't specify which storage to use, use the default
// location for application.
if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
| Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
final File dataDir = packageInfo.getDataDirFile();
if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
} else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
}
}
mMainThread = mainThread;
mActivityToken = activityToken;
mFlags = flags;
if (user == null) {
user = Process.myUserHandle();
}
mUser = user;
mPackageInfo = packageInfo;
mSplitName = splitName;
mClassLoader = classLoader;
mResourcesManager = ResourcesManager.getInstance();
String opPackageName;
if (container != null) {
mBasePackageName = container.mBasePackageName;
opPackageName = container.mOpPackageName;
setResources(container.mResources);
mDisplay = container.mDisplay;
} else {
mBasePackageName = packageInfo.mPackageName;
ApplicationInfo ainfo = packageInfo.getApplicationInfo();
if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
// Special case: system components allow themselves to be loaded in to other
// processes. For purposes of app ops, we must then consider the context as
// belonging to the package of this process, not the system itself, otherwise
// the package+uid verifications in app ops will fail.
opPackageName = ActivityThread.currentPackageName();
} else {
opPackageName = mBasePackageName;
}
}
mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
mContentResolver = new ApplicationContentResolver(this, mainThread);
}
//1.2.2 創建app上線文
static ContextImpl createAppContext(ActivityThread mainThread, LoadedApk packageInfo,
String opPackageName) {
if (packageInfo == null) throw new IllegalArgumentException("packageInfo");
//1.2.2.1
ContextImpl context = new ContextImpl(null, mainThread, packageInfo, null, null, null, 0,
null, opPackageName);
context.setResources(packageInfo.getResources());
return context;
}
}
//LoadedApk.java
public final class LoadedApk {
LoadedApk(ActivityThread activityThread) {
mActivityThread = activityThread;
mApplicationInfo = new ApplicationInfo();
mApplicationInfo.packageName = "android";
mPackageName = "android";
mAppDir = null;
mResDir = null;
mSplitAppDirs = null;
mSplitResDirs = null;
mSplitClassLoaderNames = null;
mOverlayDirs = null;
mDataDir = null;
mDataDirFile = null;
mDeviceProtectedDataDirFile = null;
mCredentialProtectedDataDirFile = null;
mLibDir = null;
mBaseClassLoader = null;
mSecurityViolation = false;
mIncludeCode = true;
mRegisterPackage = false;
mResources = Resources.getSystem();
mDefaultClassLoader = ClassLoader.getSystemClassLoader();
mAppComponentFactory = createAppFactory(mApplicationInfo, mDefaultClassLoader);
mClassLoader = mAppComponentFactory.instantiateClassLoader(mDefaultClassLoader,
new ApplicationInfo(mApplicationInfo));
}
//1.3.1
public Application makeApplication(boolean forceDefaultAppClass,
Instrumentation instrumentation) {
if (mApplication != null) {
return mApplication;
}
Application app = null;
String appClass = mApplicationInfo.className;
if (forceDefaultAppClass || (appClass == null)) {
appClass = "android.app.Application";
}
try {
java.lang.ClassLoader cl = getClassLoader();
if (!mPackageName.equals("android")) {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER,
"initializeJavaContextClassLoader");
initializeJavaContextClassLoader();
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
}
ContextImpl appContext = ContextImpl.createAppContext(mActivityThread, this);
//1.3.1.1
app = mActivityThread.mInstrumentation.newApplication(
cl, appClass, appContext);
appContext.setOuterContext(app);
} catch (Exception e) {
if (!mActivityThread.mInstrumentation.onException(app, e)) {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
throw new RuntimeException(
"Unable to instantiate application " + appClass
+ ": " + e.toString(), e);
}
}
mActivityThread.mAllApplications.add(app);
mApplication = app;
if (instrumentation != null) {
try {
//1.3.1.2
instrumentation.callApplicationOnCreate(app);
} catch (Exception e) {
if (!instrumentation.onException(app, e)) {
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
throw new RuntimeException(
"Unable to create application " + app.getClass().getName()
+ ": " + e.toString(), e);
}
}
}
return app;
}
}
3.2 getSystemContext()
這一部分其實和3.1中獲取systemContext是一樣的步驟,這里就不在啰嗦了。
4、創建systemServiceManager,準備線程池初始化任務
//SystemServer.java
mSystemServiceManager = new SystemServiceManager(mSystemContext);
mSystemServiceManager.setStartInfo(mRuntimeRestart, mRuntimeStartElapsedTime, mRuntimeStartUptime);
LocalServices.addService(SystemServiceManager.class, mSystemServiceManager);
SystemServerInitThreadPool.start();
5、啟動服務
5.1、啟動引導服務
引導服務主要有常見的:看門狗、AMS ,installer安裝器、PKMS應用包管理器,電量管理PMS等引導服務,詳細細節看下面的源碼:
注:下面的源碼筆者進行了刪減,僅保留了一些常見的服務及其執行的順序
//SystemServer.java
//這里是啟動引導服務
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
/**
啟動watchDog
首先啟動看門狗,是為了在引導服務期間發生死鎖時是系統服務奔潰
*/
final Watchdog watchdog = Watchdog.getInstance();
watchdog.start();
/**
>>>>>>>>創建安裝器installer ,用于PKMS安裝應用程序使用
*/
Installer installer = mSystemServiceManager.startService(Installer.class);
// AMS創建啟動
ActivityTaskManagerService atm = mSystemServiceManager.startService(
ActivityTaskManagerService.Lifecycle.class).getService();
mActivityManagerService = ActivityManagerService.Lifecycle.startService(
mSystemServiceManager, atm);
mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
mActivityManagerService.setInstaller(installer);
mWindowManagerGlobalLock = atm.getGlobalLock();
//PMS 電量管理服務
mPowerManagerService = mSystemServiceManager.startService(PowerManagerService.class);
//命令終端管理器
mSystemServiceManager.startService(ThermalManagerService.class);
//初始化電量管理PowerManager
mActivityManagerService.initPowerManagement();
// Manages LEDs and display backlight so we need it to bring up the display.
mSystemServiceManager.startService(LightsService.class);
//顯示管理器需要在包管理器啟動之前提供顯示指標
mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);
// 啟動顯示管理器
mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);
//啟動PKMS
Watchdog.getInstance().pauseWatchingCurrentThread("packagemanagermain");
mPackageManagerService = PackageManagerService.main(mSystemContext, installer,
mFactoryTestMode != FactoryTest.FACTORY_TEST_OFF, mOnlyCore);
// 現在包管理器已經啟動,請注冊dex load reporter以捕獲任何系統服務器加載的dex文件。
// 這些dex文件將由BackgroundDexOptService進行優化。
SystemServerDexLoadReporter.configureSystemServerDexReporter(mPackageManagerService);
mPackageManager = mSystemContext.getPackageManager();
// 初始化用于緩存包中資源的屬性緩存。
AttributeCache.init(mSystemContext);
// 為系統進程設置應用程序實例并開始。
mActivityManagerService.setSystemProcess();
//看門狗的設置
watchdog.init(mSystemContext, mActivityManagerService);
// 初始化:管理覆蓋包服務
mSystemServiceManager.startService(new OverlayManagerService(mSystemContext));
}
5.2、啟動核心服務
//SystemServer.java
private void startCoreServices(@NonNull TimingsTraceAndSlog t) {
// 初始化系統配置服務
mSystemServiceManager.startService(SystemConfigService.class);
// 初始化燈光服務,獲取電池電量等
mSystemServiceManager.startService(BatteryService.class);
// 啟動應用程序使用統計信息服務
mSystemServiceManager.startService(UsageStatsService.class);
mActivityManagerService.setUsageStatsManager(LocalServices.getService(UsageStatsManagerInternal.class));
// 跟蹤可更新WebView是否處于就緒狀態,并監視更新安裝。
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
mWebViewUpdateService = mSystemServiceManager.startService(WebViewUpdateService.class);
}
//跟蹤并緩存設備狀態。
mSystemServiceManager.startService(CachedDeviceStateService.class);
// 跟蹤在Binder調用中花費的cpu時間
mSystemServiceManager.startService(BinderCallsStatsService.LifeCycle.class);
//跟蹤處理程序中處理消息所花費的時間。.
mSystemServiceManager.startService(LooperStatsService.Lifecycle.class);
//管理apk回滾
mSystemServiceManager.startService(ROLLBACK_MANAGER_SERVICE_CLASS);
// 用于捕獲錯誤報告的服務.
mSystemServiceManager.startService(BugreportManagerService.class);
//GPU和GPU驅動程序的服務
mSystemServiceManager.startService(GpuService.class);
}
5.3、啟動其他服務
在這個其他服務中包含了很多我們需要使用到的服務,本文下面僅列出一部分常見的服務,更多細節見源碼
//SystemServer.java
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
try {
mActivityManagerService.installSystemProviders();
vibrator = new VibratorService(context);
ServiceManager.addService("vibrator", vibrator);
mSystemServiceManager.startService(new AlarmManagerService(context));
inputManager = new InputManagerService(context);
// WMS(Window Manager Service)啟動過程
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);
//給AMS設置WindowManagerService
mActivityManagerService.setWindowManager(wm);
//wms 初始化
wm.onInitReady();
if (!isWatch && enableVrService) {
//VR管理服務
mSystemServiceManager.startService(VrManagerService.class);
}
inputManager.setWindowManagerCallbacks(wm.getInputManagerCallback());
inputManager.start();
// TODO: Use service dependencies instead.
mDisplayManagerService.windowManagerAndInputReady();
if (mFactoryTestMode == FactoryTest.FACTORY_TEST_LOW_LEVEL) {
} else if (!context.getPackageManager().hasSystemFeature
(PackageManager.FEATURE_BLUETOOTH)) {
} else {
//啟動藍牙服務
mSystemServiceManager.startService(BluetoothService.class);
}
} catch (Throwable e) {
throw e;
}
if (mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
if (!isWatch) {
try {
//狀態欄管理服務
statusBar = new StatusBarManagerService(context);
ServiceManager.addService(Context.STATUS_BAR_SERVICE, statusBar);
} catch (Throwable e) {
}
}
try {
//網絡管理服務
networkManagement = NetworkManagementService.create(context);
ServiceManager.addService(Context.NETWORKMANAGEMENT_SERVICE, networkManagement);
} catch (Throwable e) {
reportWtf("starting NetworkManagement Service", e);
}
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_WIFI)) {
//Wifi相關的服務:掃描wifi
mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
mSystemServiceManager.startService(
"com.android.server.wifi.scanner.WifiScanningService");
}
//狀態欄的通知服務
mSystemServiceManager.startService(NotificationManagerService.class);
SystemNotificationChannels.removeDeprecated(context);
SystemNotificationChannels.createAll(context);
notification = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
//定位管理服務
mSystemServiceManager.startService(LocationManagerService.Lifecycle.class);
if (context.getResources().getBoolean(R.bool.config_enableWallpaperService)) {
//桌面壁紙管理服務
mSystemServiceManager.startService(WALLPAPER_SERVICE_CLASS);
}
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
|| mPackageManager.hasSystemFeature(
PackageManager.FEATURE_USB_ACCESSORY)
|| isEmulator) {
// USB服務
mSystemServiceManager.startService(USB_SERVICE_CLASS);
}
// TODO(aml-jobscheduler): Think about how to do it properly.
//JobScheduler
mSystemServiceManager.startService(JOB_SCHEDULER_SERVICE_CLASS);
try {
//runtime服務
ServiceManager.addService("runtime", new RuntimeService(context));
} catch (Throwable e) {
reportWtf("starting RuntimeService", e);
}
if (hasFeatureFace) {
//人臉識別的服務
mSystemServiceManager.startService(FaceService.class);
}
}
if (!disableCameraService) {
//相機服務代理
mSystemServiceManager.startService(CameraServiceProxy.class);
}
// Permission policy service
//權限管理策略服務
mSystemServiceManager.startService(PermissionPolicyService.class);
}
6、Looper開始工作
SystemServer在run中執行啟動一些列服務后,最后啟動Looper進入Handler的消息永動機中開始工作。關于Handler另作分析。
總結:
通過上述源碼分析,我們可以清楚的知道,SystemServer的啟動過程:
準備主線程的Looper ,
加載NativeSystem ,加載好之后創建SystemContext提供經常允許的環境等;
創建SystemServiceManager,并準備線程池來進行初始化任務;
創建并啟動一系列的服務(引導服務,核心服務,其他服務)
最后Looper永動機開始工作,這樣SystemServer就啟動完成了。
ActivityThread的創建過程,需要特別注意,而且非常重要。
在上面的源碼中,我們可以看到大部分的服務是通過SystemServerManager來啟動的,那么要詳情了解具體一個Server的啟動就要從SystemServerManager的startService()開始了。