Android TV开机优化
Android開機(jī)優(yōu)化相關(guān)點(diǎn)
1 關(guān)鍵路徑
bootloader > kernel > init > zygote > system server > launcher
2 打印優(yōu)化
2.1 關(guān)閉bootloader打印
2.2 關(guān)閉 kernel打印
2.3 提高Android log打印等級(jí)
system/core/logd/LogBuffer.cpp
3 uboot
1 修改emmc速度提升讀寫速度加快uboot啟動(dòng)時(shí)間
2 提高uboot cpu的頻率
4 kernel
如果不是非常緊急的驅(qū)動(dòng)延遲初始化,可以等zygote啟動(dòng)后,在early-boot或boot階段去加載ko, 讓zygote早點(diǎn)起來(lái),zygote 在late-init階段后面起來(lái)
比如wifi和藍(lán)牙驅(qū)動(dòng)
on bootchown bluetooth bluetooth /proc/bluetooth/sleep/btwritechown bluetooth bluetooth /proc/bluetooth/sleep/lpmchmod 0660 /proc/bluetooth/sleep/btwritechmod 0660 /proc/bluetooth/sleep/lpminsmod /vendor/lib/modules/btusb.ko5 init
SecondStageMain 觸發(fā)的各個(gè)階段
zygote在late-init稍后的階段啟動(dòng)
# Mount filesystems and start core system services. on late-inittrigger early-fs# Mount fstab in init.{$device}.rc by mount_all command. Optional parameter# '--early' can be specified to skip entries with 'latemount'.# /system and /vendor must be mounted by the end of the fs stage,# while /data is optional.trigger fstrigger post-fs# Mount fstab in init.{$device}.rc by mount_all with '--late' parameter# to only mount entries with 'latemount'. This is needed if '--early' is# specified in the previous mount_all command on the fs stage.# With /system mounted and properties form /system + /factory available,# some services can be started.trigger late-fs# Now we can mount /data. File encryption requires keymaster to decrypt# /data, which in turn can only be loaded when system properties are present.trigger post-fs-data# Load persist properties and override properties (if enabled) from /data.trigger load_persist_props_action# Should be before netd, but after apex, properties and logging is available.trigger load_bpf_programs# Now we can start zygote for devices with file based encryption# 觸發(fā)zygote啟動(dòng)trigger zygote-start# Remove a file to wake up anything waiting for firmware.trigger firmware_mounts_completetrigger early-boottrigger boot5.1 并行執(zhí)行uevent(enable_parallel_restorecon)
vendor/ueventd.rc中加入parallel_restorecon enable
parallel_restorecon enable5.2 CPU開啟性能模式
1 開機(jī)的時(shí)候,cpu開啟性能模式
on early-initwrite /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor performance# 查看當(dāng)前頻率 # cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq2 開機(jī)完成后,CPU頻率變成自適應(yīng)
on property:sys.boot_completed=1write /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor schedutil5.3 讀寫IO調(diào)整
read_ahead_kb: 事先預(yù)讀數(shù)據(jù)的Kb數(shù)
nr_requests: 默認(rèn)IO請(qǐng)求隊(duì)列的長(zhǎng)度
加大read_ahead_kb和nr_requests 大小,優(yōu)化時(shí)間不明顯
on late-fs# boot time fs tunewrite /sys/block/mmcblk0/queue/iostats 0write /sys/block/mmcblk0/queue/read_ahead_kb 2048write /sys/block/mmcblk0/queue/nr_requests 256on property:sys.boot_completed=1# end boot time fs tunewrite /sys/block/mmcblk0/queue/read_ahead_kb 1285.4 移除沒有用到的模塊
如CI模塊
on post-fs-datainsmod /vendor/lib/modules/cimax-usb.koinsmod /vendor/lib/modules/ci.ko5.5 延遲vendor/etc/init下的非關(guān)鍵服務(wù),放在early-boot或者boot階段執(zhí)行,加快zygote的啟動(dòng)
// 放在early-boot或者boot階段執(zhí)行 on bootstart xxxservice xxx /system/bin/xxxuser rootgroup system6 zygote
6.1 lazy-preload
classloader懶加載,懶加載會(huì)比直接加載更耗內(nèi)存,懶加載是通過(guò)system-server發(fā)送指令給zygote做加載相關(guān)動(dòng)作,在發(fā)送指令前,system-server會(huì)加載一部分自己使用的類,會(huì)和zygote中存在相同的一部分備份
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server --enable-lazy-preload6.2 zygote 添加 task_profiles ProcessCapacityHigh MaxPerformance
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server --enable-lazy-preloadclass mainpriority -20user rootgroup root readproc reserved_disksocket zygote stream 660 root systemsocket usap_pool_primary stream 660 root systemonrestart exec_background - system system -- /system/bin/vdc volume abort_fuseonrestart write /sys/power/state ononrestart restart audioserveronrestart restart cameraserveronrestart restart mediaonrestart restart netdonrestart restart wificondtask_profiles ProcessCapacityHigh MaxPerformance6.3 精簡(jiǎn)preload的classes , TV系統(tǒng)可以去掉如下class
如果需要再做一些大的裁剪,可以使用frameworks\base\config\generate-preloaded-classes.sh下的腳本來(lái)生成preloaded-classes
// 生物識(shí)別 android.hardware.biometrics// 人臉識(shí)別 android.hardware.face// 打印服務(wù) android.hardware.fingerprint android.print.// 部分定位相關(guān), 還有GPS定位相關(guān) android.hardware.location com.android.internal.location.GpsNetInitiatedHandler android.location.Gnss*// 手機(jī)通話相關(guān) android.telephony. android.telecom. com.android.i18n.phonenumbers. com.android.ims android.hardware.radio// nfc相關(guān) android.nfc.6.4 關(guān)閉 systemServer虛擬機(jī)實(shí)列的 ClassVerify
確認(rèn)是否需要Disable_verify,不建議關(guān)閉
/** Turn off the verifier. */ public static final int DISABLE_VERIFIER = 1 << 9; parsedArgs.mRuntimeFlags |= Zygote.DISABLE_VERIFIER;/* Request to fork the system server process */pid = Zygote.forkSystemServer(parsedArgs.mUid, parsedArgs.mGid,parsedArgs.mGids,parsedArgs.mRuntimeFlags,null,parsedArgs.mPermittedCapabilities,parsedArgs.mEffectiveCapabilities);7 systemServer
7.1 調(diào)整systemServer進(jìn)程,線程優(yōu)先級(jí)
android.os.Process.setThreadPriority(-19); android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND); Process.setProcessGroup(pid, Process.THREAD_GROUP_TOP_APP); private void run() {android.os.Process.setThreadPriority(-19);int defaultGroup = getProcessGroup(Process.myPid());Process.setProcessGroup(Process.myPid(), Process.THREAD_GROUP_TOP_APP);// Start services.try {t.traceBegin("StartServices");startBootstrapServices(t);startCoreServices(t);startOtherServices(t);} catch (Throwable ex) {Slog.e("System", "******************************************");Slog.e("System", "************ Failure starting system services", ex);throw ex;} finally {t.traceEnd(); // StartServices}android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);Process.setProcessGroup(Process.myPid(), defaultGroup);// Loop forever.Looper.loop();}7.2 調(diào)整SystemServerInitThreadPool線程池相關(guān)優(yōu)先級(jí)
private SystemServerInitThreadPool() {final int size = Runtime.getRuntime().availableProcessors();Slog.i(TAG, "Creating instance with " + size + " threads");mService = ConcurrentUtils.newFixedThreadPool(size,"system-server-init-thread", -18);//Process.THREAD_PRIORITY_FOREGROUND); }7.3 裁剪TV系統(tǒng)中沒有用到的服務(wù),可以用feture的形式來(lái)控制開關(guān)
if (mPackageManager.hasSystemFeature(PackageManager.FEATURE_xxx)) { mSystemServiceManager.startService(xxx_SERVICE_CLASS); }查看systemServer中的服務(wù)
service list可以裁剪的服務(wù)
TelecomLoaderService TelephonyRegistry StatusBarManagerService SearchManagerService SerialService FingerprintService CameraService MmsService7.4 延時(shí)啟動(dòng)presist app
void startPersistentApps(int matchFlags) {if (mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL) return;synchronized (this) {try {final List<ApplicationInfo> apps = AppGlobals.getPackageManager().getPersistentApplications(STOCK_PM_FLAGS | matchFlags).getList();for (ApplicationInfo app : apps) {if (!"android".equals(app.packageName)) {mHandler.postDelayed(() -> {addAppLocked(app, null, false, null /* ABI override */,ZYGOTE_POLICY_FLAG_BATCH_LAUNCH);}, 1000);}}} catch (RemoteException ex) {}} }8 Laucher
1 Laucher APK的編譯方式改為 speed-profile
LOCAL_DEX_PREOPT_FLAGS += --compiler-filter=speed-profile// debug pm compile -m speed -f package dumpsys package package2 如果是內(nèi)置的launch,可以將launch設(shè)置為presist apk
9 調(diào)試方法
9.1 打開init log
1 init中的log會(huì)輸出到kernel log中,打開init log查看耗時(shí)的時(shí)間點(diǎn),不同平臺(tái)方法可能不一樣,需要將kernel log輸出等級(jí)設(shè)置為7
2 rc中設(shè)置如下參數(shù)
vendor/etc/init/hw/init.xxx.rc
on early-initloglevel 79.2 通過(guò)logcat -b events,過(guò)濾出啟動(dòng)階段的主要事件
logcat -b events |grep "boot_p" boot_progress_start: 6466 01-18 11:28:13.935 647 647 I boot_progress_system_run: 10467 01-18 11:28:15.368 647 647 I boot_progress_pms_start: 11900 01-18 11:28:15.748 647 647 I boot_progress_pms_system_scan_start: 12280 01-18 11:28:16.338 647 647 I boot_progress_pms_data_scan_start: 12870 01-18 11:28:16.350 647 647 I boot_progress_pms_scan_end: 12881 01-18 11:28:16.642 647 647 I boot_progress_pms_ready: 13174 01-18 11:28:18.973 647 647 I boot_progress_ams_ready: 15505 01-18 11:28:24.131 647 722 I boot_progress_enable_screen: 20663 01-18 11:28:24.143 392 767 I sf_stop_bootanim: 20675 01-18 11:28:24.144 647 722 I wm_boot_animation_done: 206769.3 通過(guò) logcat -b system查看系統(tǒng)中一些流程的耗時(shí)點(diǎn), 結(jié)合trace一起分析
Zygote32Timing: ZygoteInit took to complete: 1454ms SystemServerInitThreadPool: Creating instance with 4 threads [20220127_12:10:10:054]01-18 12:04:51.352 667 667 D SystemServerTiming: InitBeforeStartServices took to complete: 745ms **Zygote32Timing: ZygoteInit took to complete: 1454ms** SystemServerTiming: InitBeforeStartServices took to complete: 745ms SystemConfig: readAllPermissions took to complete: 160ms SystemServerTimingAsync: InitThreadPoolExec:ReadingSystemConfig took to complete: 161ms SystemServerTiming: StartActivityManager took to complete: 364ms **PackageManagerTiming: read user settings took to complete: 313ms** SystemServerTimingAsync: AppDataFixup took to complete: 177ms PackageManagerTiming: GC took to complete: 106ms **PackageManagerTiming: create package manager took to complete: 1411ms** **SystemServerTiming: StartPackageManagerService took to complete: 1425ms** SystemServerTiming: startBootstrapServices took to complete: 2129ms SystemServerTimingAsync: InitThreadPoolExec:PersistentDataBlockService.onStart took to complete: 80ms SystemServerTiming: OnBootPhase_500 took to complete: 204ms SystemServerTiming: StartBootPhaseSystemServicesReady took to complete: 204ms SystemServerTiming: MakePowerManagerServiceReady took to complete: 105ms ZygoteInitTiming_lazy: PreloadClasses took to complete: 1737ms ZygoteInitTiming_lazy: PreloadResources took to complete: 124ms SystemServerTimingAsync: SecondaryZygotePreload took to complete: 1960ms SystemServerTiming: OnBootPhase_550 took to complete: 212ms SystemServerTiming: StartActivityManagerReadyPhase took to complete: 213ms OnBootPhase_600 took to complete: 122ms SystemServerTiming: PhaseThirdPartyAppsCanStart took to complete: 122ms SystemServerTiming: ssm.StartUser-0 took to complete: 165ms SystemServerTiming: showSystemReadyErrorDialogs took to complete: 119ms SystemServerTiming: ActivityManagerStartApps took to complete: 537ms** SystemServerTiming: PhaseActivityManagerReady took to complete: 1129ms SystemServerTiming: startOtherServices took to complete: 3032ms SystemServerTiming: StartServices took to complete: 5217ms **SystemUIBootTiming: StartServicescom.android.systemui.volume.VolumeUI took to complete: 255ms** **SystemUIBootTiming: StartServices took to complete: 429ms** **WindowManager: Keyguard drawn timeout. Setting mKeyguardDrawComplete** ActivityManagerTiming: OnBootPhase_1000 took to complete: 109ms ActivityManagerTiming: TotalBootTime took to complete: 9939ms **ActivityManagerTiming: FinishBooting took to complete: 134ms** **SystemServerTiming: SystemUserUnlock took to complete: 611ms9.4 抓取trace
atrace -z -b 65536 gfx input view wm am sm audio video hal res dalvik rs power pm ss database aidl sched irq freq idle disk mmc sync workq memreclaim binder_driver binder_lock pagecache gfx -t 20 > /data/trace9.5 bootchart
啟用 bootchart
adb shell 'touch /data/bootchart/enabled' adb reboot9.6 perfboot
代碼位置在system/core/init/perfboot.py,腳本執(zhí)行結(jié)束后,可以在Excel里直接打開文件進(jìn)行分析
./perfboot.py --iterations=2 --interval=30 -v --output=/tmp/J5D_UE.tsvhttps://source.android.google.cn/devices/tech/perf/boot-times?hl=zh-cn
https://source.android.google.cn/security/verifiedboot/dm-verity?hl=zh-cn
總結(jié)
以上是生活随笔為你收集整理的Android TV开机优化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 朱敏:美国往事---硅谷第一代中国大陆创
- 下一篇: android 键盘隐藏监听,Andro