说到Zygote如何启动,那就要从盘古开天地开始说起。。。。。。
对不起跑题了,实际上对于Zygote的启动,在刚开机时就已经在谋划了
init.Zygote32(64).rc
首先在驱动层加载完毕后,首先会启动init进程,也就是用户进程。在system/core/init/init.cpp中会对init.rc文件进行解析,之后会孵化出一些进程和一些重要服务,最后会创建一个Zygote进程。我们可以看到在init.rc文件中会有这样两行
他的意思就是在这里,现在我们可以通过基于文件的加密为设备启动zygote。
那么Zygote启动的相关文件又分为几种,具体看是多少位的进程。
关于这一点,差距不大,加载的文件不一样,但是内容是大体相近的。
当然,具体还是要看文件内容是否有区别,在作本篇文章时用的机器没有区别
读取的目标文件会在/build/target/product/core_xx_bit.mk中间展示,所以我们到这个类去看
PRODUCT_VENDOR_PROPERTIES += ro.zygote=zygote64_32
根据这行代码以及注释我们可以得知,读取的目标文件是init.zygote64_32.rc,这代表这将允许64位应用程序,但仍然只能使用32位的JNI编译所有应用程序。因为要对32位的软件进行兼容,所以选择了64位为主,32位为辅的Zygote属性。
# /system/bin指代虚拟机所在目录 --zygote为虚拟机执行的入口
service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygote
class main
priority -20
user root
group root readproc reserved_disk
socket zygote stream 660 root system
#上面这个zygote代表zygote创建时产生的socket的名字,权限为600,用户名为root,用户组为system
socket usap_pool_primary stream 660 root system
# 下面所以带onrestart命令的行表示在服务重启时会执行的命令
onrestart exec_background - system system -- /system/bin/vdc volume abort_fuse
onrestart write /sys/power/state on
onrestart restart audioserver
onrestart restart cameraserver
onrestart restart media
onrestart restart netd
onrestart restart wificond
task_profiles ProcessCapacityHigh MaxPerformance
critical window=${zygote.critical_window.minute:-off} target=zygote-fatal
service zygote_secondary /system/bin/app_process32 -Xzygote /system/bin --zygote --socket-name=zygote_secondary --enable-lazy-preload
class main
priority -20
user root
group root readproc reserved_disk
socket zygote_secondary stream 660 root system
socket usap_pool_secondary stream 660 root system
onrestart restart zygote
task_profiles ProcessCapacityHigh MaxPerformance
所以我们可以发现,在这个.rc文件里面创立了两个新的service,为别为zygote和zygote_secondary,分别执行的是64位程序和32位程序。
app_main.cpp
在上面这个文件里面,不知道大家有没有发现,它的可执行文件也给我们写出来了,那就是/system/bin/app_process32以及/system/bin/app_process64.
app_process64和app_process32由frameworks/base/cmds/app_process文件夹内部文件生成,app_process64的执行入口是目录下的app_main.cpp中的main函数
frameworks/base.cmds/app_process/app_main.cpp
#if defined(__LP64__)
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
static const char ZYGOTE_NICE_NAME[] = "zygote64";
#else
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
static const char ZYGOTE_NICE_NAME[] = "zygote";
#endif
int main(int argc, char* const argv[])
{
if (!LOG_NDEBUG) {
String8 argv_String;
for (int i = 0; i < argc; ++i) {
argv_String.append("\"");
argv_String.append(argv[i]);
argv_String.append("\" ");
}
ALOGV("app_process main with argv: %s", argv_String.string());
}
//之后会创建有AppRuntime对象runtime
AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
// Process command line arguments
// ignore argv[0]
argc--;
argv++;
const char* spaced_commands[] = { "-cp", "-classpath" };
// Allow "spaced commands" to be succeeded by exactly 1 argument (regardless of -s).
bool known_command = false;
int i;
for (i = 0; i < argc; i++) {
if (known_command == true) {
runtime.addOption(strdup(argv[i]));
// The static analyzer gets upset that we don't ever free the above
// string. Since the allocation is from main, leaking it doesn't seem
// problematic. NOLINTNEXTLINE
ALOGV("app_process main add known option '%s'", argv[i]);
known_command = false;
continue;
}
for (int j = 0;
j < static_cast<int>(sizeof(spaced_commands) / sizeof(spaced_commands[0]));
++j) {
if (strcmp(argv[i], spaced_commands[j]) == 0) {
known_command = true;
ALOGV("app_process main found known command '%s'", argv[i]);
}
}
//begin
//在这个begin到end的部分,就是用来过滤开头是“-”的参数,把开头不是“-”和开头是“--"的去掉
if (argv[i][0] != '-') {
break;
}
if (argv[i][1] == '-' && argv[i][2] == 0) {
++i; // Skip --.
break;
}
//end
runtime.addOption(strdup(argv[i]));
// The static analyzer gets upset that we don't ever free the above
// string. Since the allocation is from main, leaking it doesn't seem
// problematic. NOLINTNEXTLINE
ALOGV("app_process main add option '%s'", argv[i]);
}
// 解析运行时参数。在第一次未被识别的选项时停止。
bool zygote = false;
bool startSystemServer = false;
bool application = false;
String8 niceName;
String8 className;
++i; // 跳过未使用的父目录参数
while (i < argc) {
const char* arg = argv[i++];
if (strcmp(arg, "--zygote") == 0) {
//是否存在--zygote参数
//设置zygot参数为true,代表启动zygote模式
zygote = true;
//将nicename设为zygote或者zygote64
niceName = ZYGOTE_NICE_NAME;
} else if (strcmp(arg, "--start-system-server") == 0) {
//启动zygot模式存在--start-system-server,将startSystemServer设置为true
startSystemServer = true;
} else if (strcmp(arg, "--application") == 0) {
//启动zygote模式不存在
application = true;
} else if (strncmp(arg, "--nice-name=", 12) == 0) {
//启动zygote不存在
niceName.setTo(arg + 12);
} else if (strncmp(arg, "--", 2) != 0) {
//启动zygote不存在
className.setTo(arg);
break;
} else {
--i;
break;
}
}
//运行上面代码之后,参数将会变为
//zygote=true,niceName = ZYGOTE_NICE_NAME,startSystemServer = true
Vector<String8> args;
if (!className.isEmpty()) {
//我们不在zygote中,我们需要传递给RuntimeInit的唯一参数是应用程序参数。
//将其他参数传至函数main()
args.add(application ? String8("application") : String8("tool"));
runtime.setClassNameAndArgs(className, argc - i, argv + i);
if (!LOG_NDEBUG) {
String8 restOfArgs;
char* const* argv_new = argv + i;
int argc_new = argc - i;
for (int k = 0; k < argc_new; ++k) {
restOfArgs.append("\"");
restOfArgs.append(argv_new[k]);
restOfArgs.append("\" ");
}
ALOGV("Class name = %s, args = %s", className.string(), restOfArgs.string());
}
} else {
// 启动zygote时
maybeCreateDalvikCache();
if (startSystemServer) {
//启动时挂载的参数
args.add(String8("start-system-server"));
}
char prop[PROP_VALUE_MAX];
if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
ABI_LIST_PROPERTY);
return 11;
}
String8 abiFlag("--abi-list=");
abiFlag.append(prop);
args.add(abiFlag);
//在zygote启动过程中,将所有剩余的参数传给zygote.main()中
for (; i < argc; ++i) {
args.add(String8(argv[i]));
}
}
if (!niceName.isEmpty()) {
//启动zygote时不成立
runtime.setArgv0(niceName.string(), true /* setProcName */);
}
if (zygote) {//启动zygote时成立,调用这个方法,调用到zygoteinit,并传入参数
runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
} else if (className) {
runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
} else {
fprintf(stderr, "Error: no class name or --zygote supplied.\n");
app_usage();
LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
}
}
根据这段代码我们得知,在最后zygote启动的时候,我们使用 runtime.start(“com.android.internal.os.ZygoteInit”, args, zygote);这句代码去调用,这里的runtime是 AppRuntime类,而它继承自AndroidRuntime对象
AndroidRuntime
AppRuntime中的start方法没有进行实现,实际上就是调用的AndroidRuntime中的start方法
frameworks/base/core/init/AndroidRuntime.cpp
// 启动Android运行时。这涉及启动虚拟机并在由 “className” 命名的类中调用 “静态void main(String[] args)” 方法。
//这里的start方法有三个参数:要启动的类名,参数,是否为zygote
void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
{
ALOGD(">>>>>> START %s uid %d <<<<<<\n",
className != NULL ? className : "(unknown)", getuid());
static const String8 startSystemServer("start-system-server");
// 这是否为最主要的zygote,也就是初始zygote,意味着这个zygote会fork出其他的系统服务,默认为false
bool primary_zygote = false;
//startSystemServer==true表示运行时已过时(也就是已经进行过了),不能从init运行。
//rc不再存在,所以我们在这里打印引导启动事件。
for (size_t i = 0; i < options.size(); ++i) {
if (options[i] == startSystemServer) {
primary_zygote = true;
//通过引导跟踪进度
const int LOG_BOOT_PROGRESS_START = 3000;
LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START, ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
addBootEvent("boot_progress_start");
}
}
const char* rootDir = getenv("ANDROID_ROOT");
if (rootDir == NULL) {
rootDir = "/system";
if (!hasDir("/system")) {
LOG_FATAL("No root directory specified, and /system does not exist.");
return;
}
setenv("ANDROID_ROOT", rootDir, 1);
}
const char* artRootDir = getenv("ANDROID_ART_ROOT");
if (artRootDir == NULL) {
LOG_FATAL("No ART directory specified with ANDROID_ART_ROOT environment variable.");
return;
}
const char* i18nRootDir = getenv("ANDROID_I18N_ROOT");
if (i18nRootDir == NULL) {
LOG_FATAL("No runtime directory specified with ANDROID_I18N_ROOT environment variable.");
return;
}
const char* tzdataRootDir = getenv("ANDROID_TZDATA_ROOT");
if (tzdataRootDir == NULL) {
LOG_FATAL("No tz data directory specified with ANDROID_TZDATA_ROOT environment variable.");
return;
}
//const char* kernelHack = getenv("LD_ASSUME_KERNEL");
//ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
//启动虚拟机
JniInvocation jni_invocation;
jni_invocation.Init(NULL);
JNIEnv* env;
if (startVm(&mJavaVM, &env, zygote, primary_zygote) != 0) {
return;
}
onVmCreated(env);
//在这里注册Android功能,如果注册队列小于0,则这个函数直接返回,不能注册所有的Android方法
if (startReg(env) < 0) {
ALOGE("Unable to register all android natives\n");
return;
}
//注册一个数组来保存类名以及选项字符串参数
jclass stringClass;
jobjectArray strArray;
jstring classNameStr;
stringClass = env->FindClass("java/lang/String");
assert(stringClass != NULL);
strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
assert(strArray != NULL);
classNameStr = env->NewStringUTF(className);
assert(classNameStr != NULL);
env->SetObjectArrayElement(strArray, 0, classNameStr);
for (size_t i = 0; i < options.size(); ++i) {
jstring optionsStr = env->NewStringUTF(options.itemAt(i).string());
assert(optionsStr != NULL);
env->SetObjectArrayElement(strArray, i + 1, optionsStr);
}
/*
* Start VM. This thread becomes the main thread of the VM, and will
* not return until the VM exits.
*/
char* slashClassName = toSlashClassName(className != NULL ? className : "");
jclass startClass = env->FindClass(slashClassName);
if (startClass == NULL) {
ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
/* keep going */
} else {
jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
"([Ljava/lang/String;)V");
if (startMeth == NULL) {
ALOGE("JavaVM unable to find main() in '%s'\n", className);
/* keep going */
} else {
//在这里调用ZygoteInit.main函数
env->CallStaticVoidMethod(startClass, startMeth, strArray);
#if 0
if (env->ExceptionCheck())
threadExitUncaughtException(env);
#endif
}
}
free(slashClassName);
ALOGD("Shutting down VM\n");
if (mJavaVM->DetachCurrentThread() != JNI_OK)
ALOGW("Warning: unable to detach main thread\n");
if (mJavaVM->DestroyJavaVM() != 0)
ALOGW("Warning: VM did not shut down cleanly\n");
}
根据源码得出AndroidRuntime.start内可分为如下几步:
- 设置了rootDir的目录为/system;
- 通过startVm(&mJavaVM, &env, zygote)启动了虚拟机;
- 调用startReg(env)注册jni函数;
- 创建字符串数组来保存类和设置选项
- 将生成的字符串数组传入并调用ZygoteInit.main方法
Zygoteinit.main
这是zygote的入口点。它创建Zygote服务器,加载资源,并处理相关的其他任务。
frameworks/base/core/java/com/android/internal/os/ZygoteInit
public static void main(String[] argv) {
ZygoteServer zygoteServer = null;
// 标记zygote的开始,这确保线程会抛出一个错误
ZygoteHooks.startZygoteNoThreadCreation();
// Zygote会进入自己的线程组
try {
Os.setpgid(0, 0);
} catch (ErrnoException ex) {
throw new RuntimeException("Failed to setpgid(0,0)", ex);
}
Runnable caller;
try {
// Store now for StatsLogging later.
final long startTime = SystemClock.elapsedRealtime();
final boolean isRuntimeRestarted = "1".equals(
SystemProperties.get("sys.boot_completed"));
String bootTimeTag = Process.is64Bit() ? "Zygote64Timing" : "Zygote32Timing";
TimingsTraceLog bootTimingsTraceLog = new TimingsTraceLog(bootTimeTag,
Trace.TRACE_TAG_DALVIK);
bootTimingsTraceLog.traceBegin("ZygoteInit");
RuntimeInit.preForkInit();
boolean startSystemServer = false;
//设置socket名字为zygote
String zygoteSocketName = "zygote";
String abiList = null;
//这里是启用懒加载,如果在配置里没有"--enable-lazy-preload"就不要开启,一定要设置为false
boolean enableLazyPreload = false;
for (int i = 1; i < argv.length; i++) {
if ("start-system-server".equals(argv[i])) {
startSystemServer = true;
} else if ("--enable-lazy-preload".equals(argv[i])) {
enableLazyPreload = true;
} else if (argv[i].startsWith(ABI_LIST_ARG)) {
abiList = argv[i].substring(ABI_LIST_ARG.length());
} else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
zygoteSocketName = argv[i].substring(SOCKET_NAME_ARG.length());
} else {
throw new RuntimeException("Unknown command line argument: " + argv[i]);
}
}
final boolean isPrimaryZygote = zygoteSocketName.equals(Zygote.PRIMARY_SOCKET_NAME);
if (!isRuntimeRestarted) {
if (isPrimaryZygote) {
FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__ZYGOTE_INIT_START,
startTime);
} else if (zygoteSocketName.equals(Zygote.SECONDARY_SOCKET_NAME)) {
FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__SECONDARY_ZYGOTE_INIT_START,
startTime);
}
}
if (abiList == null) {
throw new RuntimeException("No ABI list supplied.");
}
//有些配置急切被加载就不能懒加载,要预先加载资源
if (!enableLazyPreload) {
bootTimingsTraceLog.traceBegin("ZygotePreload");
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
SystemClock.uptimeMillis());
/// M: Added for BOOTPROF
addBootEvent("Zygote:Preload Start");
/// @}
preload(bootTimingsTraceLog);
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
SystemClock.uptimeMillis());
bootTimingsTraceLog.traceEnd(); // ZygotePreload
}
// 启动后做一次垃圾回收
bootTimingsTraceLog.traceBegin("PostZygoteInitGC");
gcAndFinalize();
bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC
bootTimingsTraceLog.traceEnd(); // ZygoteInit
Zygote.initNativeState(isPrimaryZygote);
/// M: Added for BOOTPROF
addBootEvent("Zygote:Preload End");
/// @}
ZygoteHooks.stopZygoteNoThreadCreation();
//注册zygote服务
zygoteServer = new ZygoteServer(isPrimaryZygote);
if (startSystemServer) {
Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);
// {@code r == null} in the parent (zygote) process, and {@code r != null} in the
// child (system_server) process.
if (r != null) {
r.run();
return;
}
}
Log.i(TAG, "Accepting command socket connections");
// The select loop returns early in the child process after a fork and
// loops forever in the zygote.
caller = zygoteServer.runSelectLoop(abiList);
} catch (Throwable ex) {
Log.e(TAG, "System zygote died with fatal exception", ex);
throw ex;
} finally {
if (zygoteServer != null) {
zygoteServer.closeServerSocket();
}
}
// We're in the child process and have exited the select loop. Proceed to execute the
// command.
if (caller != null) {
caller.run();
}
}
enableLazyPreload
在这个函数里面,我们预加载了一些资源
那我们现在看到preloadClasses()函数上来
frameworks/base/config/preloaded_classes
在这里我们就找到PRELOADED_CLASSES,在这个类里面就是加载了我们最常用的那些资源类
接着就会把读到的这些文件一个一个读取并加载进去,这个文件有1w多行,我就随便截个图了。
最后的循环
现在,我们把main函数里面的最后一段单独提取出来
caller = zygoteServer.runSelectLoop(abiList);
} catch (Throwable ex) {
Log.e(TAG, "System zygote died with fatal exception", ex);
throw ex;
} finally {
if (zygoteServer != null) {
zygoteServer.closeServerSocket();
}
}
在这个时候,我们的启动已经完成了,接下来它会无限的进行循环等待Activity管理服务ActivityManagerService(AMS)请求Zygote进程创建新的应用程序进程。
疑问与回答
- 为何zygote使用socket通信而不使用binder
答:因为binder是多线程的,可能会造成死锁或者不一致的问题。 - 为什么fork出进程启用app要交给zygote来做,而不使用systemservice或者其他进程直接创建?
答:在zygote中已经加载了各类资源并且创建了虚拟机,这样的话它fork出来的进程就可以直接继承它的资源而不用重新创建,缩短了进程启动的时间。
总结
Zygote进程启动总结:
- 启动Zygote进程
- 创建虚拟机,为Java虚拟机注册JNI方法
- 通过JNI调用ZygoteInit的main函数,进入Java框架层
- 通过registerZygoteSocket方法创建服务端Socket,通过funSelectLoop方法等待AMS请求
- 启动SystemServer进程