android6.0 開機啟動流程
這篇部落格講下android開機啟動流程,init那部分之前在分析init程序的時候,講過了,我們就不看了。當然我們結合log看。
整個開機的時間,我們一SurfaceFlinger來算,先從如下log開始,下面這句log,是在SurfaceFlinger的建構函式的第一句列印,記錄了當前的時間。
10-31 10:21:57.563044 1588 1588 I SurfaceFlinger: SurfaceFlinger is starting
下面我們再來看SystemServer,如下log,SystemServer在每個階段都有log列印,我們可以根據這個來看到底哪個模組比較耗時。
Line 9655: 10-24 11:16:08.119069 2335 2335 I SystemServer: Entered the Android system server! Line 9688: 10-24 11:16:08.525047 2335 2335 I SystemServer: Package Manager Line 9840: 10-24 11:16:11.046506 2335 2335 I SystemServer: User Service Line 9945: 10-24 11:16:11.118712 2335 2335 I SystemServer: Reading configuration... Line 9946: 10-24 11:16:11.118785 2335 2335 I SystemServer: Scheduling Policy Line 9948: 10-24 11:16:11.126440 2335 2335 I SystemServer: Telephony Registry Line 9949: 10-24 11:16:11.134762 2335 2335 I SystemServer: Entropy Mixer Line 9951: 10-24 11:16:11.149893 2335 2335 I SystemServer: Camera Service Line 9954: 10-24 11:16:11.159449 2335 2335 I SystemServer: Account Manager Line 9960: 10-24 11:16:11.167307 2335 2335 I SystemServer: Content Manager Line 9964: 10-24 11:16:11.171901 2335 2335 I SystemServer: System Content Providers Line 9966: 10-24 11:16:11.201064 2335 2335 I SystemServer: Vibrator Service Line 9967: 10-24 11:16:11.204308 2335 2335 I SystemServer: Consumer IR Service Line 9971: 10-24 11:16:11.214877 2335 2335 I SystemServer: Init Watchdog Line 9972: 10-24 11:16:11.215451 2335 2335 I SystemServer: Input Manager Line 9976: 10-24 11:16:11.222449 2335 2335 I SystemServer: Window Manager Line 9981: 10-24 11:16:11.366857 2335 2335 I SystemServer: Bluetooth Service Line 9989: 10-24 11:16:11.378571 2335 2335 I SystemServer: Input Method Service Line 9993: 10-24 11:16:11.413864 2335 2335 I SystemServer: Accessibility Manager Line 10087: 10-24 11:16:12.154045 2335 2335 I SystemServer: LockSettingsService Line 10095: 10-24 11:16:12.179184 2335 2335 I SystemServer: Status Bar Line 10096: 10-24 11:16:12.192914 2335 2335 I SystemServer: Clipboard Service Line 10097: 10-24 11:16:12.195539 2335 2335 I SystemServer: NetworkManagement Service Line 10098: 10-24 11:16:12.202412 2335 2335 I SystemServer: Text Service Manager Service Line 10099: 10-24 11:16:12.220153 2335 2335 I SystemServer: Network Score Service Line 10105: 10-24 11:16:12.233771 2335 2335 I SystemServer: NetworkStats Service Line 10114: 10-24 11:16:12.323907 2335 2335 I SystemServer: NetworkPolicy Service Line 10158: 10-24 11:16:13.977198 2335 2335 I SystemServer: Connectivity Service Line 10164: 10-24 11:16:14.002456 2335 2335 I SystemServer: Network Service Discovery Service Line 10166: 10-24 11:16:14.039438 2335 2335 I SystemServer: UpdateLock Service Line 10179: 10-24 11:16:14.486079 2335 2335 I SystemServer: Location Manager Line 10181: 10-24 11:16:14.534753 2335 2335 I SystemServer: Country Detector Line 10182: 10-24 11:16:14.576690 2335 2335 I SystemServer: Search Service Line 10183: 10-24 11:16:14.585494 2335 2335 I SystemServer: DropBox Service Line 10184: 10-24 11:16:14.595203 2335 2335 I SystemServer: Wallpaper Service Line 10185: 10-24 11:16:14.629071 2335 2335 I SystemServer: Audio Service Line 10445: 10-24 11:16:26.748242 2335 2335 I SystemServer: Wired Accessory Manager Line 10470: 10-24 11:16:26.792381 2335 2335 I SystemServer: Serial Service Line 10512: 10-24 11:16:26.838924 2335 2335 I SystemServer: Gesture Launcher Service Line 10516: 10-24 11:16:26.844651 2335 2335 I SystemServer: DiskStats Service Line 10517: 10-24 11:16:26.846403 2335 2335 I SystemServer: SamplingProfiler Service Line 10522: 10-24 11:16:26.880410 2335 2335 I SystemServer: NetworkTimeUpdateService Line 10523: 10-24 11:16:26.881338 2335 2335 I SystemServer: CommonTimeManagementService Line 10524: 10-24 11:16:26.883369 2335 2335 I SystemServer: CertBlacklister Line 10526: 10-24 11:16:26.891389 2335 2335 I SystemServer: Assets Atlas Service Line 10538: 10-24 11:16:26.925759 2335 2335 I SystemServer: Media Router Service Line 10624: 10-24 11:16:27.376132 2335 2335 I SystemServer: BackgroundDexOptService Line 10730: 10-24 11:16:27.690378 2335 2335 I SystemServer: Making services ready //AMS的systemReady
當最後一句就是呼叫了AMS的systemReady函數了,AMS之前也分析過,這裡我們主要看下systemReady函式下面這段程式碼:
synchronized (this) { if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) { try { List apps = AppGlobals.getPackageManager(). getPersistentApplications(STOCK_PM_FLAGS); if (apps != null) { int N = apps.size(); int i; for (i=0; i<N; i++) { ApplicationInfo info = (ApplicationInfo)apps.get(i); if (info != null && !info.packageName.equals("android")) { addAppLocked(info, false, null /* ABI override */); } } } } catch (RemoteException ex) { // pm is in same process, this will never happen. } } // Start up initial activity. mBooting = true; startHomeActivityLocked(mCurrentUserId, "systemReady");
systemReady中先會檢查系統中帶有FLAG_PERSISTENT標誌的應用,然後啟動應用。緊接著呼叫startHomeActivityLocked函式啟動Home應用。
startHomeActivityLocked函式會先呼叫resolveActivityInfo函式通過PKMS獲取ActivityInfo,最後在呼叫ActivityStackSupervisor的startHomeActivity函式
boolean startHomeActivityLocked(int userId, String reason) { if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL && mTopAction == null) { // We are running in factory test mode, but unable to find // the factory test app, so just sit around displaying the // error message and don't try to start anything. return false; } Intent intent = getHomeIntent(); ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId); if (aInfo != null) { intent.setComponent(new ComponentName( aInfo.applicationInfo.packageName, aInfo.name)); // Don't do this if the home app is currently being // instrumented. aInfo = new ActivityInfo(aInfo); aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId); ProcessRecord app = getProcessRecordLocked(aInfo.processName, aInfo.applicationInfo.uid, true); if (app == null || app.instrumentationClass == null) { EventLog.writeEvent(EventLogTags.AM_PROC_START,"AMS -> startHomeActivityLocked startHomeActivity then startActivityLock : "+ aInfo.processName); intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK); mStackSupervisor.startHomeActivity(intent, aInfo, reason); } } return true; }
ActivityStackSupervisor的startHomeActivity函式如下。
void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);
startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,
null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,
null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,
null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,
0 /* startFlags */, null /* options */, false /* ignoreTargetSecurity */,
false /* componentSpecified */,
null /* outActivity */, null /* container */, null /* inTask */);
if (inResumeTopActivity) {
// If we are in resume section already, home activity will be initialized, but not
// resumed (to avoid recursive resume) and will stay that way until something pokes it
// again. We need to schedule another resume.
scheduleResumeTopActivities();
}
}
這樣我們的Launcher應用就啟動了,會在AMS列印如下log。
10-31 10:22:24.616814 2437 2437 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher3/.Launcher} from uid 0 on display 0
而我們之前在http://blog.csdn.net/kc58236582/article/details/52921978部落格中也分析過,在Launcher應用空閒的時候,會呼叫AMS的介面,然後AMS又會呼叫SurfaceFlinger,最後到SurfaceFlinger的bootFinished。會把開機動畫終結。
void SurfaceFlinger::bootFinished()
{
const nsecs_t now = systemTime();
const nsecs_t duration = now - mBootTime;
ALOGI("Boot is finished (%ld ms)", long(ns2ms(duration)) );
mBootFinished = true;
// wait patiently for the window manager death
const String16 name("window");
sp<IBinder> window(defaultServiceManager()->getService(name));
if (window != 0) {
window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));
}
// stop boot animation
// formerly we would just kill the process, but we now ask it to exit so it
// can choose where to stop the animation.
property_set("service.bootanim.exit", "1");
char boot_exit_value[32];
property_get("service.bootanim.exit", boot_exit_value, "0");
ALOGD("The service.bootanim.exit property value is %d", atoi(boot_exit_value));
}
同事也會列印如下log,這樣開機過程就結束了。
10-31 10:22:30.491797 1588 2952 I SurfaceFlinger: Boot is finished (32929 ms)