Optimizing Android System Cold Boot Time

Introduction

There are known knowns; there are things we know we know. We also know there are known unknowns; that is to say, we know there are some things we do not know. But there are also unknown unknowns—the ones we don’t know we don’t know.

——Donald Rumsfeld, Former U.S. Secretary of Defense, February 12, 2002

Overview

System performance is the study of the performance of the entire computer system, including major hardware components and software components. Understandably, this is a very broad topic, and only by deeply understanding the relationships between various components can one avoid missing the forest for the trees. Performance issues can arise from anywhere, including places in the system that you may not have checked (unknown unknowns).

The typical goal of system performance optimization is to improve the end-user experience by reducing latency and lowering computational costs. Optimizing system cold boot time is one aspect of performance optimization, and while it may not occur frequently in end-user scenarios, why should we still pursue it? This leads us to the trilogy of thinking: what, why, and how. Optimizing cold boot time means minimizing the time the system takes during the startup process, so why do we do this? For car users, cold booting is not common because most of the time the system wakes from sleep and does not undergo a complete power-on startup process. Just because something happens infrequently does not mean it won’t happen; when it does occur, you can make it better. This is the significance of optimizing cold boot time. If you optimize a 5-second startup time, it means reducing the driver’s 5 seconds of waiting time (the driver may need to wait for the system to start to navigate back to a long-lost hometown, and the road is too far to remember the way homeOptimizing Android System Cold Boot Time). If it’s 5 seconds for one person, what about 300,000 people (if a car model sells 300,000 units, that’s already quite good). Now that we’ve discussed what and why, let’s explore how to do it. Let’s go!

System Cold Boot Optimization – Methodology

Methodology is how we understand cold boot duration and the methods to optimize cold boot time. Without a methodology, our investigation would turn into a fishing expedition: randomly trying things in hopes of achieving success. This approach is both time-consuming and ineffective, and it may also overlook important matters. Once you have a deep understanding of a theory through practice, you can describe it in simple terms, such as:

  • Maximize the utilization of system resources (CPU & GPU & IO) to fully utilize resources;

  • What can be done earlier, do it earlier; what can be delayed, do it later; what can be trimmed, trim it; what can be parallelized, parallelize it;

  • The improvement of boot speed must not sacrifice system stability.

System Cold Boot Optimization – Tool Usage

To do a good job, one must first sharpen their tools.

AOSP itself provides many useful tools, let’s take a look.

  • Using Scripts for Analysis

Code located at $AOSP/system/extras/boottime_tools/bootanalyze

Optimizing Android System Cold Boot Time

First, use bugreportz -p to capture a bug report, then use bugreport_analyze.py for analysis. You can visually see the time taken for each startup stage in Android, and based on this, we can optimize accordingly.

Optimizing Android System Cold Boot Time

Optimizing Android System Cold Boot Time

Optimizing Android System Cold Boot Time

  • Using simpleperf to capture cold boot data

Refer to Google’s patches for specifics; understanding the why is just as important as understanding the how.

simpleperf is used to track CPU usage; its actual utility may not be significant but can serve as a learning reference.

https://android-review.googlesource.com/c/platform/system/extras/+/1950976.https://android-review.googlesource.com/c/platform/system/extras/+/1963689.https://android-review.googlesource.com/c/platform/system/sepolicy/+/1950977.
  • Using logcat events to track

This provides a rough overview of the timing status of each major process, serving as a reference for fine-tuning optimizations.

console:/ # logcat -b events | grep -i boot_progress01-01 00:00:13.848   549   549 I boot_progress_start: 973001-01 00:00:16.266   549   549 I boot_progress_preload_start: 1214801-01 00:00:18.265   549   549 I boot_progress_preload_end: 1414701-01 00:00:19.449  1116  1116 I boot_progress_system_run: 1533101-01 00:00:20.521  1116  1116 I boot_progress_pms_start: 1640301-01 00:00:20.767  1116  1116 I boot_progress_pms_system_scan_start: 1664801-01 00:00:21.062  1116  1116 I boot_progress_pms_data_scan_start: 1694401-01 00:00:21.067  1116  1116 I boot_progress_pms_scan_end: 1694901-01 00:00:21.223  1116  1116 I boot_progress_pms_ready: 1710505-12 18:53:14.364  1116  1116 I boot_progress_ams_ready: 1878505-12 18:53:18.188  1116  1142 I boot_progress_enable_screen: 22609
  • Using perfetto to capture the startup process trace

Taking init startup as an example, we can visually see the specific reasons for the time taken, based on which we can optimize.

Optimizing Android System Cold Boot Time

Using system_server as an example, we can visually see the startup duration of each service, based on which we can optimize.

Optimizing Android System Cold Boot Time

  • Using bootprof to track bootloader and kernel timing

bootprof is a tool developed by Mediatek for instrumentation printing; we can learn from this for other platforms as well, and it is quite useful.

console:/ # cat /proc/bootprof                                                 ----------------------------------------0           BOOT PROF (unit:msec)----------------------------------------      1794        : preloader      6670        : lk----------------------------------------         3.044076 : ON (Threshold:   15ms)        67.891076 :    1-swapper/0       : initcall: of_platform_default_populate_init    29.992923ms       111.268691 :    1-swapper/0       : initcall: tracer_init_tracefs    26.218846ms       129.068384 :    1-swapper/0       : probe: probe=platform_drv_probe drv=pateo_gpio(0xffffff9b525afc80)    16.369461ms       129.093922 :    1-swapper/0       : initcall: gpio_power_init    16.604000ms       367.956384 :    1-swapper/0       : initcall: populate_rootfs   227.612077ms      1043.026463 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mt6577-uart(0xffffff9b52541d78)   655.779771ms      1047.885001 :    1-swapper/0       : initcall: mtk8250_platform_driver_init   660.785232ms      1083.230232 :    1-swapper/0       : probe: probe=platform_drv_probe drv=gpufreq(0xffffff9b52545ab0)    32.324615ms      1085.065309 :    1-swapper/0       : initcall: __mt_gpufreq_init    34.282846ms      1115.272001 :    1-swapper/0       : initcall: mtk_spi_driver_init    16.221769ms      1146.894848 :    1-swapper/0       : probe: probe=platform_drv_probe drv=Accdet_Driver(0xffffff9b5254ce10)    18.433462ms      1149.585001 :    1-swapper/0       : initcall: accdet_mod_init    21.386385ms      1198.660079 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mtk_clkbuf_driver(0xffffff9b52574f50)    19.946385ms      1200.354848 :    1-swapper/0       : initcall: mtk_clkbuf_driver_init    21.944692ms      1278.662233 :    1-swapper/0       : initcall: ISP_Init    63.668923ms      1692.988926 :    1-swapper/0       : probe: probe=platform_drv_probe drv=image_sensor(0xffffff9b5257a1e0)   380.002693ms      1694.780080 :    1-swapper/0       : initcall: imgsensor_init   382.919693ms      1753.994464 :    1-swapper/0       : initcall: disp_probe_1    35.409847ms      1786.621234 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mtkfb(0xffffff9b52596498)    30.687462ms      1791.018849 :    1-swapper/0       : initcall: mtkfb_init    35.915000ms      1897.658465 :    1-swapper/0       : initcall: init_perfmgr    90.587693ms      1996.535080 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mtk_mmdvfs_pmqos(0xffffff9b525a23c0)    96.115770ms      1999.156388 :    1-swapper/0       : initcall: mmdvfs_pmqos_init    99.041154ms      2100.770465 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mtk-thermal(0xffffff9b525a46c0)    92.930461ms      2102.705004 :    1-swapper/0       : initcall: tscpu_init    95.767385ms      2148.588388 :    1-swapper/0       : initcall: mt6358tsbuck1_init    29.006230ms      2181.734542 :    1-swapper/0       : initcall: mt6358tsbuck2_init    31.967000ms      2211.887773 :    1-swapper/0       : initcall: mt6358tsbuck3_init    28.982461ms      2252.484619 :    1-swapper/0       : initcall: VPU_INIT    18.516000ms      2395.483158 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mt-rtc(0xffffff9b525cdc08)    54.275692ms      2395.522466 :    1-swapper/0       : initcall: mtk_rtc_pdrv_init    54.884539ms      2394.672620 :  262-mtu3_thread     : probe: probe=platform_drv_probe drv=xhci-mtk(0xffffff9b525bf340)    83.506154ms      2441.471466 :    1-swapper/0       : probe: probe=platform_drv_probe drv=battery(0xffffff9b525d6bc8)    44.903692ms      2481.346543 :    1-swapper/0       : initcall: battery_init    84.968308ms      2513.009312 :    1-swapper/0       : initcall: mt_msdc_init    18.985231ms      2541.665236 :    1-swapper/0       : initcall: cis_main_init    22.029615ms      2594.468082 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mt-soc-dl1-pcm(0xffffff9b5261e798)    32.551000ms      2597.565313 :    1-swapper/0       : initcall: mtk_soc_platform_init    36.386231ms      2681.636082 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mtk-audio(0xffffff9b52627810)    73.374154ms      2684.059082 :    1-swapper/0       : initcall: mt_soc_snd_init    75.883078ms      2748.485005 :    1-swapper/0       : initcall: tipc_init    16.255616ms      2791.259390 :    1-swapper/0       : initcall: pinctrl_mtk_debug_v2_init    16.227923ms      2867.720621 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mt-cpufreq(0xffffff9b52567828)    63.925923ms      2869.433236 :    1-swapper/0       : initcall: _mt_cpufreq_pdrv_init    68.277923ms      2924.937236 :    1-swapper/0       : initcall: mt_spm_init    38.467615ms      3094.374468 :    1-swapper/0       : probe: probe=i2c_device_probe drv=ICM42607_ACCEL(0xffffff9b52582c30)   161.108077ms      3096.220621 :    1-swapper/0       : initcall: acc_init   163.068615ms      3180.307468 :    1-swapper/0       : Kernel_init_done      5857.348320 :  329-init            : INIT:early-init      6635.633861 :  329-init            : INIT:init      6680.954784 :  329-init            : INIT:late-init      6724.796630 :  329-init            : INIT:early-fs      6725.362169 :  329-init            : INIT:fs      6732.352015 :  329-init            : INIT:Mount_START      7409.806786 :  329-init            : INIT:Mount_END      7506.673247 :  274-kworker/u16:2   : probe: probe=platform_drv_probe drv=xhci-mtk(0xffffff9b525bf340)    99.747000ms      7516.208401 :  329-init            : INIT:post-fs      7722.876633 :  329-init            : INIT:late-fs      7727.973325 :  329-init            : INIT:Mount_START --late      8971.286559 :  329-init            : INIT:Mount_END --late      9013.209943 :  381-binder:381_2    : vold:fbeEnable:START      9014.265020 :  381-binder:381_2    : vold:retrieveOrGenerateKey:START      9019.617943 :  381-binder:381_2    : vold:retrieveOrGenerateKey:END, rtn =1      9020.814789 :  381-binder:381_2    : vold:install_storage_key(device_key):START      9022.617559 :  381-binder:381_2    : vold:install_storage_key:END, rtn =1      9029.566559 :  381-binder:381_2    : vold:generateStorageKey(per_boot_key):START      9030.873943 :  381-binder:381_2    : vold:generateStorageKey:END, rtn =1      9032.019405 :  381-binder:381_2    : vold:install_storage_key(per_boot_key):START      9033.528713 :  381-binder:381_2    : vold:install_storage_key:END, rtn =1      9036.999405 :  381-binder:381_2    : vold:fbeEnable:END, rtn =1      9542.817021 :  381-binder:381_2    : vold:initUser0:START      9584.440714 :  381-binder:381_2    : vold:initUser0:END, rtn =1      9816.008945 :  329-init            : INIT:post-fs-data      9939.744715 :  329-init            : post-fs-data: on modem start     10560.260409 :  329-init            : INIT:zygote-start     10564.873947 :  329-init            : INIT:early-boot     10977.542871 :  329-init            : INIT:boot     11009.869563 :  649-ccci_mdinit     : ccci_md1: md_init srv start     11015.539410 :  652-ccci_mdinit     : ccci_md3: md_init srv start     11300.854487 :  451-wmt_loader      : probe: probe=platform_drv_probe drv=mtk_wmt(alarm_timer_handler [wmt_drv])    50.083385ms     11800.660335 :  808-android.hardwar : EvsEnumeratorHw service is starting     11800.710488 :  808-android.hardwar : ##EvsEnumerator ctr +     11800.730488 :  808-android.hardwar : ##EvsEnumerator ctr -     11822.872412 :  808-android.hardwar : begin to registerAsService     11834.240412 :  [email protected] : EvsEnumeratorHw IS READY.     11927.662873 :  543-zygote64        : boot_progress_start     12341.725874 :  329-init            : probe: probe=platform_drv_probe drv=fm(mt_fm_dev_drv [fmradio_drv_mt6631])    78.951154ms     12349.091413 :  329-init            : initcall: init_module [fmradio_drv_mt6631]   101.158770ms     12639.316644 :  329-init            : USB ready     13449.468415 :  543-main            : Zygote:Preload Start     13460.199492 :  544-main            : Zygote:Preload End     15326.278189 :  543-main            : Zygote:Preload 16236 classes in 1749ms     15413.496651 :  543-main            : Zygote:Preload 64 obtain resources in 43ms     15422.495112 :  543-main            : Zygote:Preload 41 resources in 8ms     15664.622882 :  543-main            : Zygote:Preload End     16354.456422 : 1093-system_server   : Android:SysServerInit_START     17182.076424 : 1093-system_server   : Android:PackageManagerService_Start     17411.809271 : 1093-system_server   : Android:PMS_scan_START     17423.021579 : 1093-system_server   : Android:PMS_scan_data:/product/overlay     17445.905579 : 1093-system_server   : Android:PMS_scan_data:/vendor/overlay     17458.837348 : 1093-system_server   : Android:PMS_scan_data:/system/framework     17475.739810 : 1093-system_server   : Android:PMS_scan_data:/system/priv-app     17541.987425 : 1093-system_server   : Android:PMS_scan_data:/system/app     17610.909041 : 1093-system_server   : Android:PMS_scan_data:/vendor/priv-app     17622.846810 : 1093-system_server   : Android:PMS_scan_data:/vendor/app     17653.638195 : 1093-system_server   : Android:PMS_scan_data:/product/priv-app     17664.301887 : 1093-system_server   : Android:PMS_scan_data:/product/app     17670.214887 : 1093-system_server   : Android:PMS_scan_data:/system/system_ext/priv-app     17693.423118 : 1093-system_server   : Android:PMS_scan_data:/system/system_ext/app     17704.339810 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.wifi/priv-app     17718.895426 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.wifi/app     17729.378272 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.uwb/priv-app     17734.117964 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.tethering/priv-app     17740.168656 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.tethering/app     17750.308041 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.permission/priv-app     17769.576426 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.ondevicepersonalization/app     17783.625349 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.mediaprovider/priv-app     17801.494580 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.extservices/priv-app     17813.263810 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.car.framework/priv-app     17824.753272 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.car.framework/app     17836.328964 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.btservices/app     17857.441580 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.adservices/priv-app     17867.973580 : 1093-system_server   : Android:PMS_scan_data:/apex/com.android.adservices/app     17887.077041 : 1093-system_server   : Android:PMS_scan_END     17932.220426 : 1093-system_server   : PMS:reconcileAppsDataLI     18051.248426 : 1093-system_server   : Android:PMS_READY     19662.871430 : 1093-system_server   : AMS:systemReady     19685.941276 : 1093-system_server   : AMS:AMS_READY     19867.188123 :  544-main            : Zygote:Preload 16236 classes in 1476ms     19954.766585 : 1093-system_server   : SystemServer:NetworkStatsService systemReady     19956.351200 :  544-main            : Zygote:Preload 64 obtain resources in 57ms     19968.021585 :  544-main            : Zygote:Preload 41 resources in 9ms     19972.374816 : 1093-system_server   : SystemServer:ConnectivityService systemReady     19987.229969 : 1093-system_server   : SystemServer:NetworkPolicyManagerServ systemReady     20224.980047 : 1127-ActivityManager : AP_Init:[service]:[com.android.car]:[{com.android.car/com.android.car.CarService}]:pid:1373:(PersistAP)     20351.732355 : 1093-system_server   : SystemServer:PhaseThirdPartyAppsCanStart     20377.113663 : 1127-ActivityManager : AP_Init:[]:[WebViewLoader-armeabi-v7a]:pid:1400     20387.599509 : 1127-ActivityManager : AP_Init:[]:[WebViewLoader-arm64-v8a]:pid:1401     20398.989970 : 1127-ActivityManager : AP_Init:[service]:[android.ext.services]:[{android.ext.services/android.ext.services.autofill.InlineSuggestionRenderServiceImpl}]:pid:1545     20516.452355 : 1127-ActivityManager : AP_Init:[added application]:[com.android.networkstack.process]:[com.android.networkstack.process]:pid:1451:(PersistAP)     20538.621586 : 1093-system_server   : Keyguard bindService     20542.673586 : 1127-ActivityManager : AP_Init:[added application]:[com.android.se]:[com.android.se]:pid:1481:(PersistAP)     20543.482894 : 1093-system_server   : Android:SysServerInit_END     20566.420432 : 1127-ActivityManager : AP_Init:[added application]:[com.hq.brightness]:[com.hq.brightness]:pid:1494:(PersistAP)     20585.782279 : 1127-ActivityManager : AP_Init:[added application]:[com.android.systemui]:[com.android.systemui]:pid:1505:(PersistAP)     20613.765894 : 1127-ActivityManager : AP_Init:[added application]:[com.hq.timesync.service]:[com.hq.timesync.service]:pid:1517:(PersistAP)     20635.150279 : 1127-ActivityManager : AP_Init:[top-activity]:[com.sgmw.launcher]:[{com.sgmw.launcher/com.sgmw.launcher.MainActivity}]:pid:1531     20666.003433 : 1127-ActivityManager : AP_Init:[service]:[android.ext.services]:[{android.ext.services/android.ext.services.autofill.InlineSuggestionRenderServiceImpl}]:pid:1545     20830.106587 : 1127-ActivityManager : AP_Init:[service]:[com.aispeech.lyra.daemon]:[{com.aispeech.lyra.daemon/com.aispeech.unit.smartapp.model.AccessibilityActionService}]:pid:1632     23117.411131 : 1115-android.display : AMS:ENABLE_SCREEN     23264.970593 : OFF----------------------------------------

System Cold Boot Optimization – Optimization Measures

Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime.

Optimize boot time from the startup sequence of each module

  • Bootloader section — Optimization target: within 2 seconds
1. Reduce unnecessary logging;
2. Speed up the entry into the kernel as much as possible while ensuring normal startup, trimming unnecessary modules;
3. Check the CPU clock and consider parallelizing kernel loading and I/O initialization;
4. Pass the startup time spent in the bootloader to the kernel in the form of command line arguments.
  • Kernel section — Optimization target: within 1 second
1. Minimize device deconfiguration;
2. Trim kernel logs;
3. Minimize driver size and execution time;
4. Delay the initialization of non-essential startup modules.
  • Android section — Optimization target: within 4 seconds
1. Improve I/O efficiency and adjust filesystem parameters;
2. Run tasks in parallel;
3. Use scheduling adjustments;
4. Start zygote as early as possible;
5. Delay non-critical initializations;
6. Optimize zygote service startup time;
7. Parallelize class loading;
8. Trim unnecessary services and system APKs to reduce service startup time and package scanning time;
9. Consider whether there are actions that can be run in advance or in parallel.
  • Boot animation section — Optimization target: within 2 seconds
Compress the animation time as much as possible while ensuring the effect.
Can we use the GPU to render the boot animation?
  • Launcher section — Optimization target: within 1 second
Start as quickly as possible while meeting business requirements.

Optimize boot time from the perspective of system resources

  • Maximize resource utilization during boot; after boot, restore to normal resource usage (CPU, GPU, IO, etc.)

CPU: Depending on the platform, bring all online, raise the frequency to the maximum, remove power limits.

GPU: Boost.

IO: Tune read_ahead_kb and nr_requests according to the project’s actual situation.

  • Maximize resource utilization for specific processes

Take the process shown in the figure as an example, giving it a higher I/O scheduling and process scheduling priority.

Optimizing Android System Cold Boot Time

  • Filesystem optimization

Optimizing Android System Cold Boot Time

Conclusion

The process of optimizing system cold boot is actually a process of trade-offs, just as software inherently carries the wisdom of trading time for space and space for time.

Leave a Comment

×