手机开关机、重启时间优化方案

640?wx_fmt=gif

640?wx_fmt=other

极力推荐文章:欢迎收藏Android 干货分享 

640?wx_fmt=other

本篇文章主要介绍 Android 开发中 开关机 重启时间 部分知识点,通过阅读本篇文章,您将收获以下内容:

一、zygote,预加载class、resources、开机动画进程 bootanimation、SystemServer.java 代码逻辑、非必要服务请放在system_server进程外启动、kernel init 时间过长、排查驱动设备初始化是否完成、apk dex2oat时间过长、尽量少把APP设置为persist、定频定核,调高CPU频率,会带来一定的功耗、PackageManagerService 扫描apk 优化、关机时间优化、优化第三apk 后台服务、谷歌官方参考资料

开机性能是使用功能和其它因素多方面平衡的结果,片面追求单方面的性能没有太大意义;

具体开机优化方案如下:

一、zygote,预加载class、resources

zygote预加载多了,会影响开机时间,所以需要优化预加载内容。

MTK 平台 开机时间信息保存在 /proc/bootprof下,或mtklog/mobilelog/Aplog下。

640?wx_fmt=other

开机时间信息

bootprof举例如下:

<< /proc/bootprof >>:
----------------------------------------
0       BOOT PROF (unit:msec)
----------------------------------------
      1612        : preloader
      1915        : lk (Start->Show logo: 981)
----------------------------------------
        34.132076 : ON
        88.563768 :    1-swapper/0       : initcall: of_init    17.915384ms
       131.311461 :    1-swapper/0       : initcall: customize_machine    24.395769ms
       164.462461 :    1-swapper/0       : initcall: param_sysfs_init    16.619385ms
       228.096692 :    1-swapper/0       : initcall: event_trace_init    30.488846ms
       297.817461 :    1-swapper/0       : initcall: populate_rootfs    53.605462ms
       353.950769 :    1-swapper/0       : initcall: mt_i2c_init    16.164154ms
      2367.197389 :    1-swapper/0       : probe: probe=i2c_device_probe drv=mt6311(c0e57f6c)  1998.596236ms
      2367.745697 :    1-swapper/0       : initcall: mt6311_init  1999.229312ms
      2385.294389 :    1-swapper/0       : probe: probe=platform_drv_probe drv=musb-hdrc(c0e5876c)    16.914000ms
      2385.426543 :    1-swapper/0       : initcall: musb_init    17.640462ms
      2503.717235 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mtk-msdc(c0ea070c)    36.834692ms
      2540.662159 :    1-swapper/0       : probe: probe=platform_drv_probe drv=mtk-msdc(c0ea070c)    36.847000ms
      2542.779389 :    1-swapper/0       : initcall: mt_msdc_init    76.216231ms
      2852.160621 :    6-kworker/u8:0    : probe: probe=i2c_device_probe drv=fts_ts(c0e9b9cc)   234.830693ms
      2855.127621 :    6-kworker/u8:0    : probe: probe=platform_drv_probe drv=mtk-tpd(c0e78d4c)   238.288846ms
      2869.938159 :    1-swapper/0       : initcall: battery_init   253.557308ms
      2879.950852 :    1-swapper/0       : Kernel_init_done

     ... ...
 
     29772.775531 :  727-system_server   : AMS:systemReady
     29786.000916 :  727-system_server   : AMS:AMS_READY
     30075.941840 :  727-system_server   : AP_Init:[service]:[com.google.android.inputmethod.latin]:[com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME]:pid:975
     30116.813224 :  727-system_server   : AP_Init:[service]:[com.android.systemui]:[com.android.systemui/.ImageWallpaper]:pid:986:(PersistAP)
     30244.397225 :  919-system-server-i : AP_Init:[]:[WebViewLoader-armeabi-v7a]:pid:1011
     
     ... ...

     31891.278382 :  727-system_server   : Android:SysServerInit_END
     32461.362615 :  763-ActivityManager : AMS:ENABLE_SCREEN
     32463.281768 :  770-android.display : AP_Launch: com.android.settings/.FallbackHome 665ms
     36239.598547 :  763-ActivityManager : AP_Init:[broadcast]:[com.android.dialer]:[com.android.dialer/.app.voicemail.LegacyVoicemailNotificationReceiver]:pid:1524
     38411.551244 :  325-Binder:300_1    : BOOT_Animation:END
     38411.595629 : OFF
----------------------------------------
================ END of FILE ===============

1.预加载Class

因为少加载类会影响APP启动速度,开机过程会涉及到APP启动,此地没有优化空间;Class 类路径 /frameworks/base/config/preloaded-classes

640?wx_fmt=other

预加载Class

2.预加载资源

/frameworks/base/core/res/下的资源文件会被打包成 framework-res.apkXML 是否在系统中有用到。

二、开机动画进程 bootanimation

开机动画保存在手机system/media下,

开机动画要求:

两种开机动画

1.播放开机声音

开机动画需要等待开机铃声播放完,动画才能退出,开机才能完成;开机铃声的mp3文件不能过长,最好不要超过system_server启动时间;

通过查看bootprof文件可以大致确定system_server启动时间。

system_server 启动时间举例如下:

     23340.916362 :  727-system_server   : Android:SysServerInit_START
     24217.928364 :  727-system_server   : Android:PackageManagerService_Start
      ....
     31891.278382 :  727-system_server   : Android:SysServerInit_END

2.不播放开机声音

不播放开机声音,不会影响开机时间,bootanimation.zip中图片越少越好。

三、 SystemServer.java 代码逻辑

不建议更改SystemServer 代码逻辑,修改风险较大,除非对开机速度有特别严苛的要求才修改:/frameworks/base/services/java/com/android/server/SystemServer.java

可以优化的Service举例如下:

DropBoxManagerService

和调试相关,可以异步加载或者直接阉割掉

PinnerService

没有配置相关则可以去除;

其他Service需要通过查看耗时挨个排查,耗时log查看如下:

    Line 126: 07-12 18:01:14.616410  1024  1024 I SystemServer: InitBeforeStartServices
    Line 127: 07-12 18:01:14.616637  1024  1024 I SystemServer: Entered the Android system server!
    Line 128: 07-12 18:01:14.881713  1024  1024 D SystemServerTiming: InitBeforeStartServices took to complete: 265ms
    Line 129: 07-12 18:01:14.881877  1024  1024 I SystemServer: StartServices
    Line 130: 07-12 18:01:14.881922  1024  1024 I SystemServer: Reading configuration...
    Line 131: 07-12 18:01:14.881945  1024  1024 I SystemServer: ReadingSystemConfig
    Line 132: 07-12 18:01:14.882617  1024  1024 D SystemServerTiming: ReadingSystemConfig took to complete: 1ms
    Line 133: 07-12 18:01:14.882714  1024  1024 I SystemServer: StartInstaller
    Line 135: 07-12 18:01:14.883365  1024  1091 D SystemServerInitThreadPool: Started executing ReadingSystemConfig
    Line 136: 07-12 18:01:14.887243  1024  1024 D SystemServerTiming: StartInstaller took to complete: 5ms
    Line 137: 07-12 18:01:14.887310  1024  1024 I SystemServer: DeviceIdentifiersPolicyService
    Line 139: 07-12 18:01:14.889138  1024  1024 D SystemServerTiming: DeviceIdentifiersPolicyService took to complete: 2ms
    Line 140: 07-12 18:01:14.889247  1024  1024 I SystemServer: StartActivityManager
    Line 144: 07-12 18:01:14.954297  1024  1091 D SystemServerInitThreadPool: Finished executing ReadingSystemConfig
    Line 162: 07-12 18:01:15.720682  1024  1024 D SystemServerTiming: StartActivityManager took to complete: 831ms
    Line 163: 07-12 18:01:15.720717  1024  1024 I SystemServer: StartPowerManager
    Line 165: 07-12 18:01:15.737378  1024  1024 D SystemServerTiming: StartPowerManager took to complete: 17ms
    Line 166: 07-12 18:01:15.737495  1024  1024 I SystemServer: InitPowerManagement
    Line 167: 07-12 18:01:15.739204  1024  1024 D SystemServerTiming: InitPowerManagement took to complete: 2ms
    Line 168: 07-12 18:01:15.739280  1024  1024 I SystemServer: StartRecoverySystemService
    Line 170: 07-12 18:01:15.742151  1024  1024 D SystemServerTiming: StartRecoverySystemService took to complete: 3ms
    Line 172: 07-12 18:01:15.750585  1024  1024 I SystemServer: StartLightsService
    Line 174: 07-12 18:01:15.752248  1024  1024 D SystemServerTiming: StartLightsService took to complete: 2ms
    Line 175: 07-12 18:01:15.752348  1024  1024 I SystemServer: StartSidekickService
    Line 176: 07-12 18:01:15.752405  1024  1024 D SystemServerTiming: StartSidekickService took to complete: 0ms
    Line 177: 07-12 18:01:15.752434  1024  1024 I SystemServer: StartDisplayManager
    Line 179: 07-12 18:01:15.778824  1024  1024 D SystemServerTiming: StartDisplayManager took to complete: 26ms
    Line 180: 07-12 18:01:15.778955  1024  1024 I SystemServer: WaitForDisplay
    Line 184: 07-12 18:01:15.798983  1024  1024 D SystemServerTiming: WaitForDisplay took to complete: 20ms
    Line 185: 07-12 18:01:15.799476  1024  1024 I SystemServer: StartPackageManagerService
    Line 216: 07-12 18:01:18.624952  1024  1131 D SystemServerInitThreadPool: Started executing prepareAppData
    Line 217: 07-12 18:01:18.834938  1024  1131 D SystemServerTimingAsync: AppDataFixup took to complete: 209ms
    Line 226: 07-12 18:01:19.226578  1024  1024 D SystemServerTiming: StartPackageManagerService took to complete: 3427ms
    Line 227: 07-12 18:01:19.226960  1024  1024 I SystemServer: StartOtaDexOptService
    Line 229: 07-12 18:01:19.230308  1024  1024 D SystemServerTiming: StartOtaDexOptService took to complete: 3ms
    Line 230: 07-12 18:01:19.230339  1024  1024 I SystemServer: StartUserManagerService
    Line 232: 07-12 18:01:19.231780  1024  1024 D SystemServerTiming: StartUserManagerService took to complete: 1ms
    Line 233: 07-12 18:01:19.231879  1024  1024 I SystemServer: InitAttributerCache
    Line 234: 07-12 18:01:19.232738  1024  1024 D SystemServerTiming: InitAttributerCache took to complete: 1ms


四、 非必要服务请放在system_server进程外启动

system_server 特定的服务导致开机变慢,比如:指纹系统;system_server进程外启动;

五、 kernel init 时间过长

kernel init需要先看一下客户的版本上init.rc文件相对Driver only版本是否有添加新的init,这些是否都是必须添加的。

uartlog 中,需要查关键字 cut here ,找到在kernel init过程中,频繁打出的这些call stack,看这些call stack,排查一下贵司所客制化的点。

六、排查驱动设备初始化是否完成

uartlog中排查驱动设备初始化是否有完成或延时较长。

比如之前驱动代码异常问题,耗时8s,这个有很大优化空间。

640?wx_fmt=other

遇到问题举例

七、 apk dex2oat时间过长

如果是刷机后第一次因为对apk进行dex2oat导致的开机慢:

bootprof文件中包含 PMS:performDexOpt,说明在编译时没有打开dex2oat选项;/device/build目录下,修改下面的宏,具体下面3个宏的位置可以在代码中搜索一下:

build/core/dex_preopt.mk

WITH_DEXPREOPT := true 
WITH_DEXPREOPT_PIC := true
 
DONT_DEXPREOPT_PREBUILTS := false //或者注释掉

Log举例:

     67720.299622 : 1211-system_server   : PMS:performDexOpt:com.mediatek.ims
     67727.107469 : 1211-system_server   : PMS:performDexOpt:com.google.android.ext.services
     67732.884007 : 1211-system_server   : PMS:performDexOpt:com.android.providers.telephony
     67740.281545 : 1211-system_server   : PMS:performDexOpt:com.micromaxinfo.coreupdater
     67750.098776 : 1211-system_server   : PMS:performDexOpt:com.touchtype.swiftkey
     82794.094658 : 1211-system_server   : PMS:performDexOpt:com.mediatek.fwk.plugin
     82798.845812 : 1211-system_server   :

     .......

    116880.481509 : 1211-system_server   : PMS:performDexOpt:com.android.keychain
    116884.277509 : 1211-system_server   : PMS:performDexOpt:com.android.chrome
    116901.748740 : 1211-system_server   : PMS:performDexOpt:com.android.gallery3d
    116909.980047 : 1211-system_server   :
    126659.802071 : 1211-system_server   : PMS:performDexOpt:com.google.android.syncadapters.calendar
    126665.240840 : 1211-system_server   : PMS:performDexOpt:com.android.managedpr
    134272.287243 : 1211-system_server   : PMS:performDexOpt:com.jio.adc.embedded
    134581.037782 : 1211-system_server   : PMS:performDexOpt:com.android.wallpaperbackup
    134583.934936 : 1211-system_server   : PMS:performDexOpt:com.android.providers.blockednumber
    134589.891166 : 1211-system_server   :
    137465.599866 : 1211-system_server   : PMS:performDexOpt:com.android.captiveportallogin

八、尽量少把APP设置为persist

1. 优化每一个有源码的persist APP;使他们启动尽可能快;

比如以下persist APP请尽量优化。

  • 1.com.android.systemui(PersistAP)

  • 2.com.mediatek.ims(PersistAP)

  • 3.com.android.phone(PersistAP)

  • 4.com.android.settings

2. 精简apk

(1)删除没有用到的,图片、资源文件、没有用到的 jar包文件、不需要使用的so文件;APP,假如设备只会加载drawable-xxhdpi中的资源,那么可以在drawable包 重复的资源可以直接删除;APP,假如设置只支持英文,values-da、values-fa这样的多语言支持资源都 可以删除;apk中只保留和系统适配的so文件,比如:armv7arm64so文件;

  1. App 的onCreate方法中不要用耗时的代码片段。

  2. 修改app的启动模式

通过修改--compiler-filter为 speed、quick、speed-profile来提高apk的启动速度;speed 模式优化的类较多,这时优化后的vdex、odex的文件较大,应用启动过程包括映射apk文件的过程,文件偏大导致有一定的时间损耗;speed模式优化后,Java类执行更快;所以这个需要针对具体的应用多次验证,没有普适性;

九、 定频定核,调高CPU频率,会带来一定的功耗

6763O1版本代码为例:Init进程中进行如下修改。/system/core/rootdir/init.rc

on early-init
#mtk begin
write /proc/ppm/policy/ut_fix_core_num "4 4"
write /proc/ppm/policy/ut_fix_freq_idx "0 0"
#mtk end

on property:sys.boot_completed=1
bootchart stop
#mtk begin
write /proc/ppm/policy/ut_fix_core_num "-1 -1"
write /proc/ppm/policy/ut_fix_freq_idx "-1 -1"
#mtk end

10.PackageManagerService 扫描apk 优化

1.scanDirTracedLI

(1)减少预置APP的数量(对开机速度会有较为明显的提升);apk包;scan分区里面的apk并不一定能充分使用IO资源,尝试改为多线程异步scan;(部分手机厂商有做出此种修改,且效果较为明显,但修改需谨慎);apk包、有重复功能的apk移除(比如:我司代码默认包含 有计算器APP,如果贵司有自己单独的计算器APP则可以移除我司apk),这样既可以使系统有更大的剩余存储空间又可以减少scan的时间,加快开机;

十一、关机时间优化

MTK6763为例:[ro.mediatek.version.branch]: [alps-mp-o1.mp1]1s(贵司可以自己测试找一个最优值),然后关机音频控制在1s(否则音频播放不完整),或者关机时不播放铃声,把这个值设置为10ms

/frameworks/base/services/core/java/com/android/server/power/ShutdownThread.java

149 // Shutdown Animation
150 private static final int MIN_SHUTDOWN_ANIMATION_PLAY_TIME = 1 * 1000;

十二、优化第三apk 后台服务

关机时间长的另外一个原因有可能是后台应用乱跑;APP,在后台都在积极抢占CPU,在手机系统资源紧张时对系统的性能影响是非常大的;

手机使用过程中,适当的限制后台进程的数量,会一定程度提高系统性能和更快的关机;

还有些APP一直保持有1个像素的悬浮窗,使自己一直为可见进程,可见进程能更多的占用系统资源,手机系统可以增加悬浮窗的权限管控开关只有获取到了才允许悬浮,可以更加合理的非配系统资源。

13.谷歌官方参考资料

谷歌官方启动时间优化链接

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

640?wx_fmt=jpeg每日十点,和您一起学Android 

为您的每个 “在看”640?wx_fmt=gif 怦然心动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Android

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值