Android 6.0 开机启动流程

这篇博客讲下Android开机启动流程,init那部分之前在分析init进程的时候,讲过了,我们就不看了。当然我们结合log看。

整个开机的时间,我们一SurfaceFlinger来算,先从如下log开始,下面这句log,是在SurfaceFlinger的构造函数的第一句打印,记录了当前的时间。

  1. 10-31 10:21:57.563044  1588  1588 I SurfaceFlinger: SurfaceFlinger is starting  

下面我们再来看SystemServer,如下log,SystemServer在每个阶段都有log打印,我们可以根据这个来看到底哪个模块比较耗时。

  1. Line 9655: 10-24 11:16:08.119069  2335  2335 I SystemServer: Entered the Android system server!  
  2. Line 9688: 10-24 11:16:08.525047  2335  2335 I SystemServer: Package Manager  
  3. Line 9840: 10-24 11:16:11.046506  2335  2335 I SystemServer: User Service  
  4. Line 9945: 10-24 11:16:11.118712  2335  2335 I SystemServer: Reading configuration...  
  5. Line 9946: 10-24 11:16:11.118785  2335  2335 I SystemServer: Scheduling Policy  
  6. Line 9948: 10-24 11:16:11.126440  2335  2335 I SystemServer: Telephony Registry  
  7. Line 9949: 10-24 11:16:11.134762  2335  2335 I SystemServer: Entropy Mixer  
  8. Line 9951: 10-24 11:16:11.149893  2335  2335 I SystemServer: Camera Service  
  9. Line 9954: 10-24 11:16:11.159449  2335  2335 I SystemServer: Account Manager  
  10. Line 9960: 10-24 11:16:11.167307  2335  2335 I SystemServer: Content Manager  
  11. Line 9964: 10-24 11:16:11.171901  2335  2335 I SystemServer: System Content Providers  
  12. Line 9966: 10-24 11:16:11.201064  2335  2335 I SystemServer: Vibrator Service  
  13. Line 9967: 10-24 11:16:11.204308  2335  2335 I SystemServer: Consumer IR Service  
  14. Line 9971: 10-24 11:16:11.214877  2335  2335 I SystemServer: Init Watchdog  
  15. Line 9972: 10-24 11:16:11.215451  2335  2335 I SystemServer: Input Manager  
  16. Line 9976: 10-24 11:16:11.222449  2335  2335 I SystemServer: Window Manager  
  17. Line 9981: 10-24 11:16:11.366857  2335  2335 I SystemServer: Bluetooth Service  
  18. Line 9989: 10-24 11:16:11.378571  2335  2335 I SystemServer: Input Method Service  
  19. Line 9993: 10-24 11:16:11.413864  2335  2335 I SystemServer: Accessibility Manager  
  20. Line 10087: 10-24 11:16:12.154045  2335  2335 I SystemServer: LockSettingsService  
  21. Line 10095: 10-24 11:16:12.179184  2335  2335 I SystemServer: Status Bar  
  22. Line 10096: 10-24 11:16:12.192914  2335  2335 I SystemServer: Clipboard Service  
  23. Line 10097: 10-24 11:16:12.195539  2335  2335 I SystemServer: NetworkManagement Service  
  24. Line 10098: 10-24 11:16:12.202412  2335  2335 I SystemServer: Text Service Manager Service  
  25. Line 10099: 10-24 11:16:12.220153  2335  2335 I SystemServer: Network Score Service  
  26. Line 10105: 10-24 11:16:12.233771  2335  2335 I SystemServer: NetworkStats Service  
  27. Line 10114: 10-24 11:16:12.323907  2335  2335 I SystemServer: NetworkPolicy Service  
  28. Line 10158: 10-24 11:16:13.977198  2335  2335 I SystemServer: Connectivity Service  
  29. Line 10164: 10-24 11:16:14.002456  2335  2335 I SystemServer: Network Service Discovery Service  
  30. Line 10166: 10-24 11:16:14.039438  2335  2335 I SystemServer: UpdateLock Service  
  31. Line 10179: 10-24 11:16:14.486079  2335  2335 I SystemServer: Location Manager  
  32. Line 10181: 10-24 11:16:14.534753  2335  2335 I SystemServer: Country Detector  
  33. Line 10182: 10-24 11:16:14.576690  2335  2335 I SystemServer: Search Service  
  34. Line 10183: 10-24 11:16:14.585494  2335  2335 I SystemServer: DropBox Service  
  35. Line 10184: 10-24 11:16:14.595203  2335  2335 I SystemServer: Wallpaper Service  
  36. Line 10185: 10-24 11:16:14.629071  2335  2335 I SystemServer: Audio Service  
  37. Line 10445: 10-24 11:16:26.748242  2335  2335 I SystemServer: Wired Accessory Manager  
  38. Line 10470: 10-24 11:16:26.792381  2335  2335 I SystemServer: Serial Service  
  39. Line 10512: 10-24 11:16:26.838924  2335  2335 I SystemServer: Gesture Launcher Service  
  40. Line 10516: 10-24 11:16:26.844651  2335  2335 I SystemServer: DiskStats Service  
  41. Line 10517: 10-24 11:16:26.846403  2335  2335 I SystemServer: SamplingProfiler Service  
  42. Line 10522: 10-24 11:16:26.880410  2335  2335 I SystemServer: NetworkTimeUpdateService  
  43. Line 10523: 10-24 11:16:26.881338  2335  2335 I SystemServer: CommonTimeManagementService  
  44. Line 10524: 10-24 11:16:26.883369  2335  2335 I SystemServer: CertBlacklister  
  45. Line 10526: 10-24 11:16:26.891389  2335  2335 I SystemServer: Assets Atlas Service  
  46. Line 10538: 10-24 11:16:26.925759  2335  2335 I SystemServer: Media Router Service  
  47. Line 10624: 10-24 11:16:27.376132  2335  2335 I SystemServer: BackgroundDexOptService  
  48. Line 10730: 10-24 11:16:27.690378  2335  2335 I SystemServer: Making services ready   //AMS的systemReady  


当最后一句就是调用了AMS的systemReady函数了,AMS之前也分析过,这里我们主要看下systemReady函数下面这段代码:

  1. synchronized (this) {  
  2.     if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {  
  3.         try {  
  4.             List apps = AppGlobals.getPackageManager().  
  5.                 getPersistentApplications(STOCK_PM_FLAGS);  
  6.             if (apps != null) {  
  7.                 int N = apps.size();  
  8.                 int i;  
  9.                 for (i=0; i<N; i++) {  
  10.                     ApplicationInfo info  
  11.                         = (ApplicationInfo)apps.get(i);  
  12.                     if (info != null &&  
  13.                             !info.packageName.equals("android")) {  
  14.                         addAppLocked(info, false, null /* ABI override */);  
  15.                     }  
  16.                 }  
  17.             }  
  18.         } catch (RemoteException ex) {  
  19.             // pm is in same process, this will never happen.  
  20.         }  
  21.     }  
  22.   
  23.     // Start up initial activity.  
  24.     mBooting = true;  
  25.     startHomeActivityLocked(mCurrentUserId, "systemReady");  

systemReady中先会检查系统中带有FLAG_PERSISTENT标志的应用,然后启动应用。紧接着调用startHomeActivityLocked函数启动Home应用。

startHomeActivityLocked函数会先调用resolveActivityInfo函数通过PKMS获取ActivityInfo,最后在调用ActivityStackSupervisor的startHomeActivity函数

  1. boolean startHomeActivityLocked(int userId, String reason) {  
  2.     if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL  
  3.             && mTopAction == null) {  
  4.         // We are running in factory test mode, but unable to find  
  5.         // the factory test app, so just sit around displaying the  
  6.         // error message and don't try to start anything.  
  7.         return false;  
  8.     }  
  9.     Intent intent = getHomeIntent();  
  10.     ActivityInfo aInfo =  
  11.         resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);  
  12.     if (aInfo != null) {  
  13.         intent.setComponent(new ComponentName(  
  14.                 aInfo.applicationInfo.packageName, aInfo.name));  
  15.         // Don't do this if the home app is currently being  
  16.         // instrumented.  
  17.         aInfo = new ActivityInfo(aInfo);  
  18.         aInfo.applicationInfo = getAppInfoForUser(aInfo.applicationInfo, userId);  
  19.         ProcessRecord app = getProcessRecordLocked(aInfo.processName,  
  20.                 aInfo.applicationInfo.uid, true);  
  21.         if (app == null || app.instrumentationClass == null) {  
  22.             EventLog.writeEvent(EventLogTags.AM_PROC_START,"AMS -> startHomeActivityLocked startHomeActivity then startActivityLock : "+ aInfo.processName);  
  23.             intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);  
  24.             mStackSupervisor.startHomeActivity(intent, aInfo, reason);  
  25.         }  
  26.     }  
  27.   
  28.     return true;  
  29. }  

ActivityStackSupervisor的startHomeActivity函数如下。

  1. void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {  
  2.     moveHomeStackTaskToTop(HOME_ACTIVITY_TYPE, reason);  
  3.     startActivityLocked(null /* caller */, intent, null /* resolvedType */, aInfo,  
  4.             null /* voiceSession */, null /* voiceInteractor */, null /* resultTo */,  
  5.             null /* resultWho */, 0 /* requestCode */, 0 /* callingPid */, 0 /* callingUid */,  
  6.             null /* callingPackage */, 0 /* realCallingPid */, 0 /* realCallingUid */,  
  7.             0 /* startFlags */, null /* options */false /* ignoreTargetSecurity */,  
  8.             false /* componentSpecified */,  
  9.             null /* outActivity */, null /* container */,  null /* inTask */);  
  10.     if (inResumeTopActivity) {  
  11.         // If we are in resume section already, home activity will be initialized, but not  
  12.         // resumed (to avoid recursive resume) and will stay that way until something pokes it  
  13.         // again. We need to schedule another resume.  
  14.         scheduleResumeTopActivities();  
  15.     }  
  16. }  


这样我们的Launcher应用就启动了,会在AMS打印如下log。

  1. 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。会把开机动画终结。

  1. void SurfaceFlinger::bootFinished()  
  2. {  
  3.     const nsecs_t now = systemTime();  
  4.     const nsecs_t duration = now - mBootTime;  
  5.     ALOGI("Boot is finished (%ld ms)"long(ns2ms(duration)) );  
  6.     mBootFinished = true;  
  7.   
  8.     // wait patiently for the window manager death  
  9.     const String16 name("window");  
  10.     sp<IBinder> window(defaultServiceManager()->getService(name));  
  11.     if (window != 0) {  
  12.         window->linkToDeath(static_cast<IBinder::DeathRecipient*>(this));  
  13.     }  
  14.   
  15.     // stop boot animation  
  16.     // formerly we would just kill the process, but we now ask it to exit so it  
  17.     // can choose where to stop the animation.  
  18.     property_set("service.bootanim.exit""1");  
  19.     char boot_exit_value[32];  
  20.     property_get("service.bootanim.exit", boot_exit_value, "0");  
  21.     ALOGD("The service.bootanim.exit property value is %d", atoi(boot_exit_value));  
  22. }  

同事也会打印如下log,这样开机过程就结束了。

  1. 10-31 10:22:30.491797  1588  2952 I SurfaceFlinger: Boot is finished (32929 ms) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值