[DESCRIPTION]
開(kāi)關(guān)機(jī)诵姜、重啟時(shí)間優(yōu)化:
開(kāi)機(jī)性能優(yōu)化:是用功能和其它因素多方面平衡的結(jié)果,片面追求單方面的性能沒(méi)有太大意義;
有些產(chǎn)品設(shè)計(jì)開(kāi)機(jī)動(dòng)畫(huà)非常酷炫,動(dòng)畫(huà)圖片過(guò)多窿侈、高幀率會(huì)影響開(kāi)機(jī)速度,這時(shí)就需要看是開(kāi)機(jī)速度優(yōu)先還是體驗(yàn)優(yōu)先秋茫;
[SOLUTION]
1.zygote史简,預(yù)加載class、resources肛著;加載的多了圆兵,會(huì)影響開(kāi)機(jī)時(shí)間;
/sdcard/mtklog/bootprof
12450.516490 : 860-system_server : Android:PMS_READY
13136.536031 : 506-main : Zygote:Preload 4715 classes in 495ms
13263.841416 : 506-main : Zygote:Preload 64 obtain resources in 42ms
13271.525031 : 506-main : Zygote:Preload 41 resources in 6ms
13639.574263 : 860-system_server : AMS:systemReady
13645.305493 : 860-system_server : AMS:AMS_READY
/frameworks/base/config/preloaded-classes枢贿,因?yàn)樯偌虞d類會(huì)影響APP啟動(dòng)速度殉农,開(kāi)機(jī)過(guò)程會(huì)涉及到APP啟動(dòng),此地沒(méi)有優(yōu)化空間局荚;
/frameworks/base/core/res/會(huì)被打包成:framework-res.apk超凳,確保沒(méi)有冗余的資源圖片,可以挨個(gè)檢查圖片耀态、XML是否在系統(tǒng)中有用到轮傍;
2.開(kāi)機(jī)動(dòng)畫(huà)進(jìn)程bootanimation或者mtkbootanimation,走android比走movie流程占用內(nèi)存資源要少些首装,開(kāi)機(jī)會(huì)快些创夜,測(cè)試大概快上百毫秒;
開(kāi)機(jī)動(dòng)畫(huà)分兩種情況:
(1)播放mp3仙逻,需要等到mp3播放完動(dòng)畫(huà)才能退出驰吓,開(kāi)機(jī)才能完成涧尿;所以mp3文件不能過(guò)長(zhǎng),最好不要超過(guò)system_server啟動(dòng)時(shí)間檬贰;
system_server啟動(dòng)時(shí)間查看如下:
/sdcard/mtklog/bootprof
10367.726870 : 860-system_server : Android:SysServerInit_START
……
14137.026648 : 860-system_server : Android:SysServerInit_END
(2)不播放mp3姑廉,不會(huì)影響開(kāi)機(jī)時(shí)間;
bootanimation.zip中圖片越少越好偎蘸;
3.
不建議更改庄蹋,修改風(fēng)險(xiǎn)較大瞬内,除非對(duì)開(kāi)機(jī)速度有特別嚴(yán)苛的要求才修改:
/frameworks/base/services/java/com/android/server/SystemServer.java
比如:DropBoxManagerService和調(diào)試相關(guān)迷雪,可以異步加載或者直接閹割掉:
比如:PinnerService沒(méi)有配置相關(guān)則可以去除;
其他Service可以挨個(gè)排查虫蝶;
4.
system_server特定的服務(wù)導(dǎo)致開(kāi)機(jī)變慢章咧,比如:指紋系統(tǒng);
非必要的服務(wù)可以放在system_server進(jìn)程外啟動(dòng);
5.kernel init 時(shí)間長(zhǎng)能真,需要先看一下客戶的版本上init.rc文件相對(duì)Driveronly版本是否有添加新的init赁严,這些是否都是必須添加的。
在uartlog 中粉铐,需要查關(guān)鍵字-----[ cut here ]------------ 疼约,找到在kernel init 過(guò)程中,頻繁打出的這些call stack蝙泼,看這些call stack程剥,排查一下貴司所客制化的點(diǎn)。
6.在uartlog中排查驅(qū)動(dòng)設(shè)備初始化是否有完成或延時(shí)較長(zhǎng)
7.因?yàn)閷?duì)apk進(jìn)行dex2oat導(dǎo)致的開(kāi)機(jī)慢問(wèn)題確認(rèn)
7.1汤踏、如果是刷機(jī)后第一次:
可以在sys_log.boot里面check?這兩個(gè)log的時(shí)間,如果時(shí)間較長(zhǎng)的話,就會(huì)有相關(guān)的dex的耗時(shí),然后可以打開(kāi)DEBUG_DEXOPT的log,進(jìn)一步check是哪個(gè)apk在進(jìn)行dex2oat
3364 01-01 00:01:17.923181? 1008? 1008 I SystemServer: UpdatePackagesIfNeeded
4825 01-01 00:01:28.833169? 1008? 1008 I SystemServer: PerformFstrimIfNeeded
7.2织鲸、非第一次開(kāi)機(jī)log:
bootprof文件中包含PMS:performDexOpt,說(shuō)明在編譯時(shí)沒(méi)有打開(kāi)dex2oat選項(xiàng)溪胶;
/sdcard/mtklog/mobile/../bootprof
49643.453733 : 777-system_server?: Android:PMS_READY
50662.038197 : 777-system_server : PMS:performDexOpt:com.mediatek.ims
50958.422582 : 434-main : Zygote:Preload 4715 classes in 957ms
51027.330659 : 777-system_server : PMS:performDexOpt:com.google.android.ext.services
51175.849890 : 434-main : Zygote:Preload 64 obtain resources in 89ms
51191.674429 : 434-main : Zygote:Preload 41 resources in 15ms
52011.623200 : 777-system_server : PMS:performDexOpt:com.android.providers.telephony
53090.475818 : 777-system_server : PMS:performDexOpt:com.valmul.defcontainer
53500.331742 : 777-system_server : PMS:performDexOpt:com.google.android.ext.shared
53759.386897 : 777-system_server : PMS:performDexOpt:com.figoglobal.gpsinitialization
55253.543054 : 777-system_server : PMS:performDexOpt:com.android.mms.service
55263.628131 : 777-system_server : PMS:performDexOpt:com.mediatek.wfo.impl
55270.579900 : 777-system_server : PMS:performDexOpt:com.android.defcontainer
55276.243746 : 777-system_server : PMS:performDexOpt:com.android.providers.settings
55278.248439 : 777-system_server : PMS:performDexOpt:com.android.inputdevices
55282.328746 : 777-system_server : PMS:performDexOpt:com.android.server.telecom
55294.016439 : 777-system_server : PMS:performDexOpt:com.android.dialer
55310.146285 : 777-system_server : PMS:performDexOpt:com.google.android.packageinstaller
56816.388981 : 777-system_server : PMS:performDexOpt:com.android.proxyhandler
56822.447212 : 777-system_server : PMS:performDexOpt:com.android.settings
56839.445365 : 777-system_server : PMS:performDexOpt:com.android.phone
56857.746212 : 777-system_server : PMS:performDexOpt:com.android.shell
56866.477442 : 777-system_server : PMS:performDexOpt:com.android.location.fused
56871.597365 : 777-system_server : PMS:performDexOpt:com.android.systemui
56890.546827 : 777-system_server : PMS:performDexOpt:com.mediatek.mtklogger
56902.493673 : 777-system_server : PMS:performDexOpt:com.mediatek.dataprotection
57298.609059 : 777-system_server : PMS:performDexOpt:com.google.android.inputmethod.latin
61557.196915 : 777-system_server : PMS:performDexOpt:com.android.fmradio
61565.777377 : 777-system_server : PMS:performDexOpt:com.mediatek.gba
61571.754300 : 777-system_server : PMS:performDexOpt:com.google.android.youtube
75712.856180 : 777-system_server : PMS:performDexOpt:com.google.android.googlequicksearchbox
99753.523929 : 777-system_server : PMS:performDexOpt:com.mediatek.camera
99774.462545 : 777-system_server : PMS:performDexOpt:com.android.providers.calendar
99784.232852 : 777-system_server : PMS:performDexOpt:com.android.providers.media
99789.938160 : 777-system_server : PMS:performDexOpt:com.google.android.apps.docs.editors.docs
114430.013733 : 777-system_server : PMS:performDexOpt:com.google.android.onetimeinitializer
114811.085504 : 777-system_server : PMS:performDexOpt:com.mediatek.location.lppe.main
115139.649966 : 777-system_server : PMS:performDexOpt:com.android.wallpapercropper
115144.646197 : 777-system_server : PMS:performDexOpt:com.android.protips
115150.169581 : 777-system_server : PMS:performDexOpt:com.android.documentsui
115166.986274 : 777-system_server : PMS:performDexOpt:com.android.externalstorage
115171.057197 : 777-system_server : PMS:performDexOpt:com.mediatek.ygps
115177.234966 : 777-system_server : PMS:performDexOpt:com.android.htmlviewer
115181.287581 : 777-system_server : PMS:performDexOpt:com.android.companiondevicemanager
115187.204274 : 777-system_server : PMS:performDexOpt:com.google.android.apps.docs.editors.sheets
130281.575156 : 777-system_server : PMS:performDexOpt:com.android.providers.downloads
130297.580156 : 777-system_server : PMS:performDexOpt:com.google.android.apps.messaging
136510.900478 : 777-system_server : PMS:performDexOpt:com.android.writeimei
136517.015402 : 777-system_server : PMS:performDexOpt:com.mediatek.engineermode
136533.084478 : 777-system_server : PMS:performDexOpt:com.otaupdater.client.pci
139099.409869 : 777-system_server : PMS:performDexOpt:com.google.android.apps.genie.geniewidget
141841.039414 : 777-system_server : PMS:performDexOpt:com.mediatek.omacp
141852.033722 : 777-system_server : PMS:performDexOpt:com.google.android.configupdater
143942.007804 : 777-system_server : PMS:performDexOpt:com.android.providers.downloads.ui
143950.975881 : 777-system_server : PMS:performDexOpt:com.android.vending
153350.969057 : 777-system_server : PMS:performDexOpt:com.android.pacprocessor
153357.482442 : 777-system_server : PMS:performDexOpt:com.android.certinstaller
153362.990057 : 777-system_server : PMS:performDexOpt:com.android.carrierconfig
153367.606672 : 777-system_server : PMS:performDexOpt:com.google.android.marvin.talkback
154832.208599 : 777-system_server : PMS:performDexOpt:com.google.android.apps.work.oobconfig
160397.116228 : 777-system_server : PMS:performDexOpt:com.android.egg
160407.464382 : 777-system_server : PMS:performDexOpt:com.android.mtp
160414.571766 : 777-system_server : PMS:performDexOpt:com.android.nfc
160427.387843 : 777-system_server : PMS:performDexOpt:com.android.stk
160435.046920 : 777-system_server : PMS:performDexOpt:com.android.launcher3
160447.006074 : 777-system_server : PMS:performDexOpt:com.android.backupconfirm
160452.568689 : 777-system_server : PMS:performDexOpt:com.mediatek.bluetooth.dtt
160465.013843 : 777-system_server : PMS:performDexOpt:com.google.android.deskclock
162244.236617 : 777-system_server : PMS:performDexOpt:com.android.statementservice
162251.229540 : 777-system_server : PMS:performDexOpt:com.google.android.gm
172015.771409 : 777-system_server : PMS:performDexOpt:com.google.android.apps.tachyon
176043.776419 : 777-system_server : PMS:performDexOpt:com.mediatek.mdmlsample
176051.331573 : 777-system_server : PMS:performDexOpt:com.mediatek.providers.drm
176056.306573 : 777-system_server : PMS:performDexOpt:com.google.android.setupwizard
177954.596039 : 777-system_server : PMS:performDexOpt:com.android.sharedstoragebackup
177960.795346 : 777-system_server : PMS:performDexOpt:com.mediatek.batterywarning
178219.600347 : 777-system_server : PMS:performDexOpt:com.google.android.music
187542.735908 : 777-system_server : PMS:performDexOpt:com.android.printspooler
187556.019446 : 777-system_server : PMS:performDexOpt:com.android.dreams.basic
187560.172908 : 777-system_server : PMS:performDexOpt:com.android.bips
187565.581754 : 777-system_server : PMS:performDexOpt:com.example
187844.819832 : 777-system_server : PMS:performDexOpt:com.mediatek.duraspeed
187854.295601 : 777-system_server : PMS:performDexOpt:com.android.musicfx
187859.364370 : 777-system_server : PMS:performDexOpt:com.google.android.apps.docs
195943.050389 : 777-system_server : PMS:performDexOpt:com.google.android.apps.maps
221750.079451 : 777-system_server : PMS:performDexOpt:com.google.android.apps.plus
230560.679933 : 777-system_server : PMS:performDexOpt:com.android.cellbroadcastreceiver
230576.272087 : 777-system_server : PMS:performDexOpt:com.google.android.webview
230830.527934 : 777-system_server : PMS:performDexOpt:com.google.android.contacts
234057.796942 : 777-system_server : PMS:performDexOpt:com.mediatek.nlpservice
234310.225942 : 777-system_server : PMS:performDexOpt:com.google.android.syncadapters.contacts
235439.648484 : 777-system_server : PMS:performDexOpt:com.android.facelock
236278.940486 : 777-system_server : PMS:performDexOpt:com.android.keychain
236285.839716 : 777-system_server : PMS:performDexOpt:com.google.android.calculator
237256.699719 : 777-system_server : PMS:performDexOpt:com.android.chrome
247269.808050 : 777-system_server : PMS:performDexOpt:com.google.android.gms
291173.412385 : 777-system_server : PMS:performDexOpt:com.google.android.gsf
291889.272310 : 777-system_server : PMS:performDexOpt:com.google.android.ims
294370.807931 : 777-system_server : PMS:performDexOpt:com.google.android.tag
294895.197548 : 777-system_server : PMS:performDexOpt:com.google.android.tts
296506.454398 : 777-system_server : PMS:performDexOpt:com.google.android.gmsintegration
296517.176552 : 777-system_server : PMS:performDexOpt:com.android.calllogbackup
296520.955705 : 777-system_server : PMS:performDexOpt:com.google.android.partnersetup
297375.172707 : 777-system_server : PMS:performDexOpt:com.valmedia.fdelux
299746.301790 : 777-system_server : PMS:performDexOpt:com.google.android.videos
305182.171495 : 777-system_server : PMS:performDexOpt:com.android.carrierdefaultapp
305190.510726 : 777-system_server : PMS:performDexOpt:com.google.android.feedback
305740.677035 : 777-system_server : PMS:performDexOpt:com.google.android.printservice.recommendation
306011.271036 : 777-system_server : PMS:performDexOpt:com.google.android.apps.photos
319736.751530 : 777-system_server : PMS:performDexOpt:com.google.android.calendar
324612.068619 : 777-system_server : PMS:performDexOpt:com.android.managedprovisioning
324623.492234 : 777-system_server : PMS:performDexOpt:com.mediatek.calendarimporter
324631.520311 : 777-system_server : PMS:performDexOpt:com.mediatek.thermalmanager
324959.254850 : 777-system_server : PMS:performDexOpt:com.mediatek.callrecorder
324964.480081 : 777-system_server : PMS:performDexOpt:com.mediatek.webview
327421.028548 : 777-system_server : PMS:performDexOpt:com.android.providers.partnerbookmarks
327429.625548 : 777-system_server : PMS:performDexOpt:com.mediatek.factorymode
327437.938548 : 777-system_server : PMS:performDexOpt:com.mtk.telephony
327691.050318 : 777-system_server : PMS:performDexOpt:com.baidu.map.location
328728.378167 : 777-system_server : PMS:performDexOpt:com.google.android.apps.translate
332677.203715 : 777-system_server : PMS:performDexOpt:com.google.android.backuptransport
332928.584946 : 777-system_server : PMS:performDexOpt:com.android.storagemanager
332944.970254 : 777-system_server : PMS:performDexOpt:com.android.bookmarkprovider
332949.513254 : 777-system_server : PMS:performDexOpt:com.google.android.inputmethod.pinyin
335415.889952 : 777-system_server : PMS:performDexOpt:com.mediatek.mdmconfig
335425.267183 : 777-system_server : PMS:performDexOpt:com.google.android.apps.books
341578.219044 : 777-system_server : PMS:performDexOpt:com.mediatek.lbs.em2.ui
341986.969814 : 777-system_server : PMS:performDexOpt:com.android.vpndialogs
341993.090198 : 777-system_server : PMS:performDexOpt:com.google.android.keep
346305.760132 : 777-system_server : PMS:performDexOpt:com.android.wallpaperbackup
346314.091670 : 777-system_server : PMS:performDexOpt:com.android.providers.blockednumber
346323.088055 : 777-system_server : PMS:performDexOpt:com.android.providers.userdictionary
346327.427440 : 777-system_server : PMS:performDexOpt:com.android.emergency
346342.475132 : 777-system_server : PMS:performDexOpt:com.android.bluetoothmidiservice
346346.436670 : 777-system_server : PMS:performDexOpt:com.mediatek.location.mtknlp
346351.760286 : 777-system_server : PMS:performDexOpt:com.mediatek.filemanager
346359.031132 : 777-system_server : PMS:performDexOpt:com.mediatek.sensorhub.ui
346363.830824 : 777-system_server : PMS:performDexOpt:com.google.android.play.games
350019.136756 : 777-system_server : PMS:performDexOpt:com.google.android.apps.magazines
357144.564619 : 777-system_server : PMS:performDexOpt:com.android.bluetooth
357162.350619 : 777-system_server : PMS:performDexOpt:com.android.wallpaperpicker
357168.592542 : 777-system_server : PMS:performDexOpt:com.android.providers.contacts
357180.100773 : 777-system_server : PMS:performDexOpt:com.android.captiveportallogin
357184.506389 : 777-system_server : PMS:performDexOpt:com.mediatek.mtklogger.proxy
362317.530247 : 777-system_server : AMS:systemReady
362327.296170 : 777-system_server : AMS:AMS_READY
7.3搂擦,若確認(rèn)為dex2oat導(dǎo)致的問(wèn)題可以按照下面修改
在/device和/build目錄下,修改下面的宏,具體下面3個(gè)宏的位置可以在代碼中搜索一下:
build/core/dex_preopt.mk
WITH_DEXPREOPT := true?
WITH_DEXPREOPT_PIC := true
DONT_DEXPREOPT_PREBUILTS := false //或者注釋掉
8.
盡量少把APP設(shè)置為persist哗脖;
優(yōu)化每一個(gè)有源碼的persist APP瀑踢;使他們啟動(dòng)盡可能快;
com.android.systemui(PersistAP)
com.mediatek.ims(PersistAP)
com.android.phone(PersistAP)
com.android.settings
精簡(jiǎn)apk包才避;
(1)刪除沒(méi)有用到的丘损,圖片、資源文件工扎、沒(méi)有用到的jar包文件徘钥、不需要使用的so文件;
(2)預(yù)置自己的APP肢娘,假如設(shè)備只會(huì)加載drawable-xxhdpi中的資源呈础,那么可以在drawable包重復(fù)的資源可以直接刪除舆驶;
(3)預(yù)置自己的APP,假如設(shè)置只支持英文而钞,values-da沙廉、values-fa這樣的多語(yǔ)言支持資源都可以刪除;
(4)apk中只保留和系統(tǒng)適配的so文件臼节,比如:armv7和arm64的so文件撬陵;
Application的onCreate方法中不要有耗時(shí)的代碼段;
通過(guò)修改--compiler-filter為speed网缝、quick巨税、speed-profile來(lái)提高apk的啟動(dòng)速度;speed模式優(yōu)化的類較多粉臊,這時(shí)優(yōu)化后的vdex草添、odex的文件較大,應(yīng)用啟動(dòng)過(guò)程包括映射apk文件的過(guò)程扼仲,文件偏大導(dǎo)致有一定的時(shí)間損耗远寸;但speed模式優(yōu)化后,Java類執(zhí)行更快屠凶;所以這個(gè)需要針對(duì)具體的應(yīng)用多次驗(yàn)證驰后,沒(méi)有普適性;
[FAQ20644] Android O矗愧、N版本修改dex2oat編譯選項(xiàng)灶芝,減少占用ROM空間或者加快安裝速度
9.定頻定核,調(diào)高CPU頻率贱枣,會(huì)帶來(lái)一定的功耗:
以6763的O1版本代碼為例:
/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.
客制化進(jìn)程的啟動(dòng)速度關(guān)注一下监署,是否有影響開(kāi)機(jī)速度的;
11.
PackageManagerService
scanDirTracedLI
(1)減少預(yù)置APP的數(shù)量(對(duì)開(kāi)機(jī)速度會(huì)有較為明顯的提升)纽哥;
(2)刪除沒(méi)有必要的apk包钠乏;
(3)單線程scan分區(qū)里面的apk并不一定能充分使用IO資源,嘗試改為多線程異步scan春塌;
(部分手機(jī)廠商有做出此種修改晓避,且效果較為明顯,但修改需謹(jǐn)慎)只壳;
(4)精簡(jiǎn)系統(tǒng)俏拱,把系統(tǒng)中用不到的apk包、有重復(fù)功能的apk移除(比如:我司代碼默認(rèn)包含有計(jì)算器APP吼句,如果貴司有自己?jiǎn)为?dú)的計(jì)算器APP則可以移除我司apk)锅必,這樣既可以使系統(tǒng)有更大的剩余存儲(chǔ)空間又可以減少scan的時(shí)間,加快開(kāi)機(jī);
12.關(guān)機(jī)時(shí)間優(yōu)化:
以MTK6763為例:
[ro.mediatek.version.branch]: [alps-mp-o1.mp1]
可以把下面這個(gè)時(shí)間改為1s(貴司可以自己測(cè)試找一個(gè)最優(yōu)值)搞隐,然后關(guān)機(jī)音頻控制在1s(否則音頻播放不完整)驹愚,或者關(guān)機(jī)時(shí)不播放鈴聲,把這個(gè)值設(shè)置為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;
13.
關(guān)機(jī)時(shí)間長(zhǎng)的另外一個(gè)原因有可能是后臺(tái)應(yīng)用亂跑逢捺;尤其國(guó)內(nèi)一些有保活的APP癞季,在后臺(tái)都在積極搶占CPU劫瞳,在手機(jī)系統(tǒng)資源緊張時(shí)對(duì)系統(tǒng)的性能影響是非常大的;手機(jī)使用過(guò)程中绷柒,適當(dāng)?shù)南拗坪笈_(tái)進(jìn)程的數(shù)量志于,會(huì)一定程度提高系統(tǒng)性能和更快的關(guān)機(jī);
還有些APP一直保持有1個(gè)像素的懸浮窗辉巡,使自己一直為可見(jiàn)進(jìn)程恨憎,可見(jiàn)進(jìn)程能更多的占用系統(tǒng)資源蕊退,手機(jī)系統(tǒng)可以增加懸浮窗的權(quán)限管控開(kāi)關(guān)只有獲取到了才允許懸浮郊楣,可以更加合理的非配系統(tǒng)資源。
谷歌官方開(kāi)機(jī)優(yōu)化資料:
https://source.android.com/devices/tech/perf/boot-times