publicstaticvoidmain(String[] argv) {
//1.创建ZygoteServer
ZygoteServer zygoteServer = null;
// Mark zygote start. This ensures that thread creation will throw
// an error.
//调用native函数,确保当前没有其它线程在运行
ZygoteHooks.startZygoteNoThreadCreation();
// Zygote goes into its own process group.
//设置pid为0,Zygote进入自己的进程组
try{
Os.setpgid(0, 0);
} catch(ErrnoException ex) {
thrownewRuntimeException("Failed to setpgid(0,0)", ex);
}
Runnable caller;
try{
// Store now for StatsLogging later.
finallongstartTime = SystemClock.elapsedRealtime();
finalbooleanisRuntimeRestarted = "1".equals(
SystemProperties.get("sys.boot_completed"));
//得到systrace的监控TAG
String bootTimeTag = Process.is64Bit() ? "Zygote64Timing": "Zygote32Timing";
TimingsTraceLog bootTimingsTraceLog = newTimingsTraceLog(bootTimeTag,
Trace.TRACE_TAG_DALVIK);
//通过systrace来追踪函数ZygoteInit,可以通过systrace工具来进行分析
//traceBegin和traceEnd要成对出现,而且需要使用同一个tag
bootTimingsTraceLog.traceBegin("ZygoteInit");
//开启DDMS(Dalvik Debug Monitor Service)功能
//注册所有已知的Java VM的处理块的监听器。线程监听、内存监听、native 堆内存监听、debug模式监听等等
RuntimeInit.preForkInit();
booleanstartSystemServer = false;
String zygoteSocketName = "zygote";
String abiList = null;
booleanenableLazyPreload = false;
//2.解析app_main.cpp#start()传入的参数
for(inti = 1; i < argv.length; i++) {
if("start-system-server".equals(argv[i])) {
startSystemServer = true;//启动zygote时,才会传入参数:start-system-server
} elseif("--enable-lazy-preload".equals(argv[i])) {
enableLazyPreload = true;//启动zygote_secondary时,才会传入参数:enable-lazy-preload
} elseif(argv[i].startsWith(ABI_LIST_ARG)) {
abiList = argv[i].substring(ABI_LIST_ARG.length());//通过属性ro.product.cpu.abilist64\ro.product.cpu.abilist32 从C空间传来的值
} elseif(argv[i].startsWith(SOCKET_NAME_ARG)) {
zygoteSocketName = argv[i].substring(SOCKET_NAME_ARG.length());//会有两种值:zygote和zygote_secondary
} else{
thrownewRuntimeException("Unknown command line argument: "+ argv[i]);
}
}
//根据传入socket name来决定是创建zygote还是zygote_secondary
finalbooleanisPrimaryZygote = 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);
} elseif(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) {
thrownewRuntimeException("No ABI list supplied.");
}
// In some configurations, we avoid preloading resources and classes eagerly.
// In such cases, we will preload things prior to our first fork.
// 在第一次zygote启动时,enableLazyPreload为false,执行preload
if(!enableLazyPreload) {
//systrace 追踪 ZygotePreload
bootTimingsTraceLog.traceBegin("ZygotePreload");
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
SystemClock.uptimeMillis());
/// M: Added for BOOTPROF
addBootEvent("Zygote:Preload Start");
/// @}
// 3.加载进程的资源和类
preload(bootTimingsTraceLog);
EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
SystemClock.uptimeMillis());
//systrace结束 ZygotePreload的追踪
bootTimingsTraceLog.traceEnd(); // ZygotePreload
}
// Do an initial gc to clean up after startup
bootTimingsTraceLog.traceBegin("PostZygoteInitGC");
gcAndFinalize();
bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC
//结束ZygoteInit的systrace追踪
bootTimingsTraceLog.traceEnd(); // ZygoteInit
Zygote.initNativeState(isPrimaryZygote);
/// M: Added for BOOTPROF
addBootEvent("Zygote:Preload End");
/// @}
ZygoteHooks.stopZygoteNoThreadCreation();
// 4.调用ZygoteServer 构造函数,创建socket,会根据传入的参数
// 创建两个socket:/dev/socket/zygote 和 /dev/socket/zygote_secondary
zygoteServer = newZygoteServer(isPrimaryZygote);
if(startSystemServer) {
//5.fork出SystemServer
Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);
// {@code r == null} in the parent (zygote) process, and {@code r != null} in the
// child (system_server) process.
//启动SystemServer
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.
//6.zygote进程进入无限循环,处理请求
caller = zygoteServer.runSelectLoop(abiList);
} catch(Throwable ex) {
Log.e(TAG, "System zygote died with fatal exception", ex);
throwex;
} 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();
}
}
|