Android系統(tǒng)包含netd、servicemanager、surfaceflinger、zygote、media、installd、bootanimation等基本服務(wù),具體作用請(qǐng)看下圖。

Android 系統(tǒng)基本服務(wù)
二、虛擬機(jī)創(chuàng)建和第一個(gè)Java 程序引導(dǎo)
為了讓APK在不同的虛擬機(jī)都可以運(yùn)行,Google采取了適配器模式,在讓虛擬機(jī)運(yùn)行之前先執(zhí)行dexopt,即將dex文件優(yōu)化成odex文件,可以讓虛擬機(jī)更加優(yōu)化的執(zhí)行。
在ART虛擬機(jī)中,dexopt將dex文件優(yōu)化成二進(jìn)制格式的問(wèn)題,從而可以讓ART虛擬機(jī)執(zhí)行。dexopt會(huì)調(diào)用dex2oat進(jìn)行優(yōu)化,dex2oat的任務(wù)是將原來(lái)的dex文件進(jìn)行預(yù)翻譯,從而可以加快app運(yùn)行的時(shí)間,但是由于某些app比較復(fù)雜,所以優(yōu)化的時(shí)間就比較長(zhǎng)。
優(yōu)化是以dex文件中的Method方法為單位,dex2oat在優(yōu)化時(shí)候,會(huì)根據(jù)需求優(yōu)化一定量的Method,即不是所有的Method都回翻譯成oat模式。

虛擬機(jī)創(chuàng)建和第一個(gè)Java 程序引導(dǎo)
三、Dalvik 虛擬機(jī)基本配置
在Android系統(tǒng)中,Dalvik虛擬機(jī) 和ART、應(yīng)用程序進(jìn)程,以及運(yùn)行系統(tǒng)的關(guān)鍵服務(wù)SystemServer進(jìn)程都是由Zygote進(jìn)程創(chuàng)建孵化的。
1.Dalvik 虛擬機(jī)基本配置

Dalvik 虛擬機(jī)基本配置
四、Zygote 啟動(dòng)流程
1.Zygote 啟動(dòng)代碼
Zygote服務(wù)時(shí)通過(guò)init.rc進(jìn)程啟動(dòng)的,Zygote的classname為main.
init.rc文件配置代碼如下:
... ...
on nonencrypted
class_start main
class_start late_start
on property:sys.init_log_level=*
loglevel ${sys.init_log_level}
... ...
詳細(xì)可以參考init.rc啟動(dòng)分析。
Android init 啟動(dòng)流程
2.Zygote main 函數(shù)
app_main.cpp是Zygote進(jìn)程的main函數(shù),frameworksasecmdsapp_processapp_main.cpp
Zygote是由init.rc腳本啟動(dòng),在init腳本中,我們可以看到會(huì)導(dǎo)入import /init.${ro.zygote}.rc腳本
# Copyright (C) 2012 The Android Open Source Project # # IMPORTANT: Do not create world writable files or directories. # This is a common source of Android security bugs. # import /init.environ.rc import /init.usb.rc import /init.${ro.hardware}.rc import /vendor/etc/init/hw/init.${ro.hardware}.rc import /init.usb.configfs.rc ... ... import /init.${ro.zygote}.rc ... ...
在system/core/rootdir目錄下,會(huì)根據(jù)ro.zygote屬性值不同,啟動(dòng)不同的腳本,主要包含以下四個(gè)zygote腳本。
1.init.zygote32.rc 支持32為系統(tǒng)
2.init.zygote32_64.rc
3.init.zygote64.rc
4.init.zygote64_32.rc

init.zygte.rc腳本

Zygote 啟動(dòng)流程
五、Zygote 啟動(dòng)分析

Zygote 啟動(dòng)分析
六、Zygote 創(chuàng)建system_server主要方法

Zygote 創(chuàng)建system_server主要方法
七、Zygote 創(chuàng)建System_server 分析

Zygote 創(chuàng)建System_server
八、Zygote 創(chuàng)建應(yīng)用

Zygote 創(chuàng)建應(yīng)用
九、Zygote 創(chuàng)建應(yīng)用流程

Zygote 創(chuàng)建應(yīng)用流程
十、Zygote 預(yù)加載資源

Zygote 預(yù)加載資源

preloadClasses()

preloadResources()
十一、Zygote 預(yù)加載的目的

Zygote 預(yù)加載的目的
十二、優(yōu)化Zygote 啟動(dòng)方法:線程池
1.Zygote 啟動(dòng)優(yōu)化
1:加載類和資源是可重入操作,所以在并行模式下,不存在互斥的場(chǎng)景
2:Android提供了Executors和ExecutorService多線程類,因此可以使用多線程來(lái)加載類和資源。
3:硬件平臺(tái)最好是多核,否則加速也不明顯;

線程池 優(yōu)化Zygote 啟動(dòng)
2.Zygote 啟動(dòng)優(yōu)化實(shí)質(zhì)
使我們的進(jìn)程最大限度的搶占CPU
十三、fork SystemServer
經(jīng)過(guò)一系列初始化后,在ZygoteInit類中forkSystemServer,為啟動(dòng)SystemServer做準(zhǔn)備。ZygoteInit.java代碼路徑如下:alpsframeworksasecorejavacomandroidinternalosygoteInit.java
/**
* Prepare the arguments and forks for the system server process.
*
* Returns an {@code Runnable} that provides an entrypoint into system_server code in the
* child process, and {@code null} in the parent.
*/
private static Runnable forkSystemServer(String abiList, String socketName,
ZygoteServer zygoteServer) {
long capabilities = posixCapabilitiesAsBits(
OsConstants.CAP_IPC_LOCK,
OsConstants.CAP_KILL,
OsConstants.CAP_NET_ADMIN,
OsConstants.CAP_NET_BIND_SERVICE,
OsConstants.CAP_NET_BROADCAST,
OsConstants.CAP_NET_RAW,
OsConstants.CAP_SYS_MODULE,
OsConstants.CAP_SYS_NICE,
OsConstants.CAP_SYS_PTRACE,
OsConstants.CAP_SYS_TIME,
OsConstants.CAP_SYS_TTY_CONFIG,
OsConstants.CAP_WAKE_ALARM,
OsConstants.CAP_BLOCK_SUSPEND
);
/* Containers run without some capabilities, so drop any caps that are not available. */
StructCapUserHeader header = new StructCapUserHeader(
OsConstants._LINUX_CAPABILITY_VERSION_3, 0);
StructCapUserData[] data;
try {
data = Os.capget(header);
} catch (ErrnoException ex) {
throw new RuntimeException("Failed to capget()", ex);
}
capabilities &= ((long) data[0].effective) | (((long) data[1].effective) << 32);
/* Hardcoded command line to start the system server */
String args[] = {
"--setuid=1000",
"--setgid=1000",
/// M: [Wi-Fi Hotspot Manager] system_server add dhcp (1014) group to access
/// "/data/misc/dhcp/dnsmasq.leases"
"--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1014,1018,1021,1023," +
"1024,1032,1065,3001,3002,3003,3006,3007,3009,3010",
"--capabilities=" + capabilities + "," + capabilities,
"--nice-name=system_server",
"--runtime-args",
"--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT,
"com.android.server.SystemServer",
};
ZygoteConnection.Arguments parsedArgs = null;
int pid;
try {
parsedArgs = new ZygoteConnection.Arguments(args);
ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);
boolean profileSystemServer = SystemProperties.getBoolean(
"dalvik.vm.profilesystemserver", false);
if (profileSystemServer) {
parsedArgs.runtimeFlags |= Zygote.PROFILE_SYSTEM_SERVER;
}
/* Request to fork the system server process */
pid = Zygote.forkSystemServer(
parsedArgs.uid, parsedArgs.gid,
parsedArgs.gids,
parsedArgs.runtimeFlags,
null,
parsedArgs.permittedCapabilities,
parsedArgs.effectiveCapabilities);
} catch (IllegalArgumentException ex) {
throw new RuntimeException(ex);
}
/* For child process */
if (pid == 0) {
if (hasSecondZygote(abiList)) {
waitForSecondaryZygote(socketName);
}
zygoteServer.closeServerSocket();
return handleSystemServerProcess(parsedArgs);
}
return null;
}
審核編輯:湯梓紅
-
Android
+關(guān)注
關(guān)注
12文章
3981瀏覽量
132821 -
JAVA
+關(guān)注
關(guān)注
20文章
2995瀏覽量
115397 -
應(yīng)用程序
+關(guān)注
關(guān)注
38文章
3341瀏覽量
59845 -
虛擬機(jī)
+關(guān)注
關(guān)注
1文章
966瀏覽量
30093 -
ART
+關(guān)注
關(guān)注
0文章
28瀏覽量
10787
原文標(biāo)題:十三、fork SystemServer
文章出處:【微信號(hào):哆啦安全,微信公眾號(hào):哆啦安全】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
關(guān)于ok6410 android 自己編譯的源碼不啟動(dòng)問(wèn)題
關(guān)于ok6410 android 自己編譯的源碼不啟動(dòng)問(wèn)題
[資料分享]+Android框架揭秘
安卓底層開(kāi)發(fā)學(xué)習(xí)經(jīng)驗(yàn)第十五期
芯靈思開(kāi)發(fā)板安卓底層開(kāi)發(fā)資料十五
Android程序的執(zhí)行流程分析
瘋殼Android嵌入式Linux平板開(kāi)發(fā)教程4-1Linux引導(dǎo)過(guò)程
關(guān)于Android系統(tǒng)啟動(dòng)的理解
Android系統(tǒng)啟動(dòng)流程總結(jié)
【嵌入式開(kāi)發(fā)教程1】手把手教你做平板電腦-Linux 引導(dǎo)過(guò)程
【嵌入式開(kāi)發(fā)教程1】瘋殼·平板電腦-Linux 引導(dǎo)過(guò)程
【開(kāi)發(fā)教程1】手把手教你做平板電腦-Linux 引導(dǎo)過(guò)程
基于Android系統(tǒng)自啟動(dòng)程序設(shè)計(jì)
Android 9.0 Crash機(jī)制調(diào)用鏈
Android安全機(jī)制介紹及實(shí)踐

Android Zygote啟動(dòng)流程
評(píng)論