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

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

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

Dalvik 虛擬機基本配置
四、Zygote 啟動流程
1.Zygote 啟動代碼
Zygote服務(wù)時通過init.rc進程啟動的,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}
... ...
詳細可以參考init.rc啟動分析。
Android init 啟動流程
2.Zygote main 函數(shù)
app_main.cpp是Zygote進程的main函數(shù),frameworksasecmdsapp_processapp_main.cpp
Zygote是由init.rc腳本啟動,在init腳本中,我們可以看到會導(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目錄下,會根據(jù)ro.zygote屬性值不同,啟動不同的腳本,主要包含以下四個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 啟動流程
五、Zygote 啟動分析

Zygote 啟動分析
六、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 啟動方法:線程池
1.Zygote 啟動優(yōu)化
1:加載類和資源是可重入操作,所以在并行模式下,不存在互斥的場景
2:Android提供了Executors和ExecutorService多線程類,因此可以使用多線程來加載類和資源。
3:硬件平臺最好是多核,否則加速也不明顯;

線程池 優(yōu)化Zygote 啟動
2.Zygote 啟動優(yōu)化實質(zhì)
使我們的進程最大限度的搶占CPU
十三、fork SystemServer
經(jīng)過一系列初始化后,在ZygoteInit類中forkSystemServer,為啟動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文章
4026瀏覽量
134031 -
JAVA
+關(guān)注
關(guān)注
20文章
3001瀏覽量
116456 -
應(yīng)用程序
+關(guān)注
關(guān)注
38文章
3344瀏覽量
60272 -
虛擬機
+關(guān)注
關(guān)注
1文章
972瀏覽量
30489 -
ART
+關(guān)注
關(guān)注
0文章
28瀏覽量
10879
原文標(biāo)題:十三、fork SystemServer
文章出處:【微信號:哆啦安全,微信公眾號:哆啦安全】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
關(guān)于ok6410 android 自己編譯的源碼不啟動問題
關(guān)于ok6410 android 自己編譯的源碼不啟動問題
[資料分享]+Android框架揭秘
安卓底層開發(fā)學(xué)習(xí)經(jīng)驗第十五期
瘋殼Android嵌入式Linux平板開發(fā)教程4-1Linux引導(dǎo)過程
關(guān)于Android系統(tǒng)啟動的理解
【嵌入式開發(fā)教程1】手把手教你做平板電腦-Linux 引導(dǎo)過程
【嵌入式開發(fā)教程1】瘋殼·平板電腦-Linux 引導(dǎo)過程
【開發(fā)教程1】手把手教你做平板電腦-Linux 引導(dǎo)過程
基于Android系統(tǒng)自啟動程序設(shè)計
Android 9.0 Crash機制調(diào)用鏈
Android安全機制介紹及實踐
深入解析RK平臺Android/Linux Bootloader核心文件:android_bootloader.c
深入解析rk平臺Android Bootloader核心代碼:從啟動流程到AVB驗證
Android Zygote啟動流程
評論