这篇博客讲下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)