Android14 init启动Zygote详解
on init阶段,已经启动了servicemanager, hwservicemanager, vndservicemangaer核心服务。
on init-late阶段中,在post-fs-data阶段之后会trigger zygote-start,从而启动zygote。
1 启动zygote的三种配置
为了适配不同设备的加密状态,按照ro.crypto.state属性值,确保各种配置服务都能启动成功。
属性值 | 含义 | 说明 |
---|---|---|
unencrypted | 未启用磁盘加密 | 常见于开发设备或旧设备 |
unsupported | 设备不支持加密 | 可能因硬件限制 |
encrypted + file | 启用了 FBE(File-Based Encryption) | Android 7+ 推荐的加密方式 |
on zygote-start && property:ro.crypto.state=unencrypted
or
on zygote-start && property:ro.crypto.state=unsupported
or
on zygote-start && property:ro.crypto.state=encrypted && property:ro.crypto.type=filewait_for_prop odsign.verification.done 1# A/B update verifier that marks a successful boot.exec_start update_verifier_nonencryptedstart statsdstart netdstart zygotestart zygote_secondary
- odsign:On-device-Signing,是google的一种设备端签名验证机制,用于验证OTA包或者系统组件的完整性。
- 在 zygote 启动前,必须确保 od 签名验证已完成,否则可能加载被篡改的代码。
- update_verifier_nonencrypted:该Service负责在设备成功启动后,向update_engine 报告本次启动是“成功的”,从而将当前槽(slot)标记为“可信赖”,防止下次启动回滚。
- statsd:stats Daemon,统计守护进程,收集系统级性能、功耗、应用行为等指标。
- netd:network Daemon,负责管理网络接口(WIFI,Ethernet),实现防火墙、NAT、DHCP、DNS、带宽控制;提供
ConnectivityManager
的底层支持。 - zygote:Android应用进程的孵化器。
- zygote_secondary:若主zygote是64位,则zygote_secondary是32位(反之亦然)。
2 zygote Service的定义
- 快速启动:通过
USAP Pool
和高性能配置加速应用启动。 - 高可用性:
onrestart
机制确保崩溃后能自愈。 - 系统稳定性:
critical
配置防止系统陷入无限崩溃循环。 - 安全性:以
root
运行但限制通信权限(socket 权限控制)。 - 可配置性:通过属性(如
zygote.critical_window.minute
)实现差异化策略。
service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygoteclass 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 on# NOTE: If the wakelock name here is changed, then also# update it in SystemSuspend.cpponrestart write /sys/power/wake_lock zygote_kwlonrestart restart audioserveronrestart restart cameraserveronrestart restart mediaonrestart restart media.tuneronrestart restart netdonrestart restart wificondtask_profiles ProcessCapacityHigh MaxPerformancecritical window=${zygote.critical_window.minute:-off} target=zygote-fatal
2.1 Zygote Service选项
- system/bin/app_process64:zygote的path。
- -Xzygote:传递给ART虚拟机的选项,启用Zygote特定行为。
- --zygote:告诉app_process以Zygote模式启动
- --start--system-server:启动后立即fork并启动system_server进程。
- --socket-name=zygote:创建一个名为Zygote的socket,用于接受启动新应用的请求。
- Zygote启动-->system_server启动-->各个系统服务启动-->应用程序启动。
2.2 priority -20:
设置进程的 调度优先级(nice value) 为 -20
(最高优先级)。
2.3 usap_pool_primary socket
socket usap_pool_primary stream 660 root system:创建usap_pool_primary socket,USAP(Unspecialized APP Process)池机制。Zygote预先fork一些空闲进程,放入池中,当有新应用请求时,直接specialize这些进程,大幅缩减应用启动时间,这是Android10+后的特性。
2.4 onrestart指令
onrestart exec_background - system system -- /system/bin/vdc volume abort_fuse
- exec_background:后台执行,不阻塞重启流程
- 调用vdc指令,终止fuse挂载,防止挂起。
onrestart write /sys/power/state on
- 重启时唤醒系统(如果处于休眠状态),确保重启流程继续。
onrestart write /sys/power/wake_lock zygote_kwl
- 重启时获取一个名为zygote_kwl的wakelock,防止系统在重启过程中休眠。
onrestart restart audioserver/ cameraserver/ media/ media.tuner/ netd/ wificond
- 重启时重启一些列关键的native Service。
task_profiles ProcessCapacityHigh MaxPerformance
- taks_profiles:12+引入的任务配置文件机制。
- ProcessCapacityHigh:表示该进程需要高的CPU容量(适用于大核)。
- MaxPerformance:要求CPU运行在最高性能模式。
- 目的:确保 Zygote 在启动应用时能获得最佳 CPU 性能,减少应用启动延迟。
critical window=${zygote.critical_window.minute:-off} target=zygote-fatal
- critical:表示该服务是关键服务
- window=${zygote.critical_window.minute:-off}:如果zygote在指定时间窗口内崩溃超过4次,系统将触发reboot到recovery。如果
zygote.critical_window.minute
未设置,默认为off
(关闭此机制);否则,值为分钟数(如5
表示 5 分钟内崩溃 4 次则重启)。 target=zygote-fatal:
当达到崩溃阈值时,触发名为zygote-fatal时间,通常在该事件中执行reboot recovery。
2.5 zygote_secondary定义
import /system/etc/init/hw/init.zygote64.rcservice zygote_secondary /system/bin/app_process32 -Xzygote /system/bin --zygote --socket-name=zygote_secondary --enable-lazy-preloadclass mainpriority -20user rootgroup root readproc reserved_disksocket zygote_secondary stream 660 root systemsocket usap_pool_secondary stream 660 root systemonrestart restart zygotetask_profiles ProcessCapacityHigh MaxPerformance