Zygote进程【1】——Zygote的诞生

时间:2021-08-01 08:31:53

Android中存在着C和Java两个完全不同的世界,前者直接建立在Linux的基础上,后者直接建立在JVM的基础上。zygote的中文名字为“受精卵”,这个名字很好的诠释了zygote进程的作用。作为java世界的孵化者,zygote本身是一个native程序,是由init根据init.rc文件中的配置项创建的。

@/system/core/rootdir/init.rc

  1. service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
  2. class main
  3. socket zygote stream 660 root system
  4. onrestart write /sys/android_power/request_state wake
  5. onrestart write /sys/power/state on
  6. onrestart restart media
  7. onrestart restart netd

关于init是如何解析和创建zygote进程的,这里不再赘述,不明的同学可以参考init进程【2】——解析配置文件一文。这里解析一下上面的第一行:service是rc脚本中的一种SECTION,zygote表示service的名字,/system/bin/app_process表示service的路径,-Xzygote /system/bin --zygote --start-system-server则表示传入的参数。

zygote的实现在app_main.cpp中:

@frameworks/base/cmds/app_process/app_main.cpp

  1. int main(int argc, char* const argv[])
  2. {
  3. //针对ARM平台的特殊逻辑
  4. #ifdef __arm__
  5. /*
  6. * b/7188322 - Temporarily revert to the compat memory layout
  7. * to avoid breaking third party apps.
  8. *
  9. * THIS WILL GO AWAY IN A FUTURE ANDROID RELEASE.
  10. *
  11. * http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commitdiff;h=7dbaa466
  12. * changes the kernel mapping from bottom up to top-down.
  13. * This breaks some programs which improperly embed
  14. * an out of date copy of Android's linker.
  15. */
  16. char value[PROPERTY_VALUE_MAX];
  17. property_get("ro.kernel.qemu", value, "");
  18. bool is_qemu = (strcmp(value, "1") == 0);
  19. if ((getenv("NO_ADDR_COMPAT_LAYOUT_FIXUP") == NULL) && !is_qemu) {
  20. int current = personality(0xFFFFFFFF);
  21. if ((current & ADDR_COMPAT_LAYOUT) == 0) {
  22. personality(current | ADDR_COMPAT_LAYOUT);
  23. setenv("NO_ADDR_COMPAT_LAYOUT_FIXUP", "1", 1);
  24. execv("/system/bin/app_process", argv);
  25. return -1;
  26. }
  27. }
  28. unsetenv("NO_ADDR_COMPAT_LAYOUT_FIXUP");
  29. #endif
  30. // These are global variables in ProcessState.cpp
  31. mArgC = argc;
  32. mArgV = argv;
  33. mArgLen = 0;
  34. for (int i=0; i<argc; i++) {
  35. mArgLen += strlen(argv[i]) + 1;
  36. }
  37. mArgLen--;
  38. AppRuntime runtime;
  39. const char* argv0 = argv[0];
  40. // Process command line arguments
  41. // ignore argv[0]
  42. argc--;
  43. argv++;
  44. // Everything up to '--' or first non '-' arg goes to the vm
  45. int i = runtime.addVmArguments(argc, argv);
  46. // Parse runtime arguments.  Stop at first unrecognized option.
  47. bool zygote = false;
  48. bool startSystemServer = false;
  49. bool application = false;
  50. const char* parentDir = NULL;
  51. const char* niceName = NULL;
  52. const char* className = NULL;
  53. while (i < argc) {//根据传入的参数,初始化启动zygote所需的参数
  54. const char* arg = argv[i++];
  55. if (!parentDir) {
  56. parentDir = arg;
  57. } else if (strcmp(arg, "--zygote") == 0) {
  58. zygote = true;
  59. niceName = "zygote";
  60. } else if (strcmp(arg, "--start-system-server") == 0) {
  61. startSystemServer = true;
  62. } else if (strcmp(arg, "--application") == 0) {
  63. application = true;
  64. } else if (strncmp(arg, "--nice-name=", 12) == 0) {
  65. niceName = arg + 12;
  66. } else {
  67. className = arg;
  68. break;
  69. }
  70. }
  71. if (niceName && *niceName) {
  72. setArgv0(argv0, niceName);
  73. set_process_name(niceName);//设置本进程的名称为zygote,至此进程有app_process变为了zygote
  74. }
  75. runtime.mParentDir = parentDir;
  76. if (zygote) {//根据我们传入的参考,这里的zygote值为TRUE
  77. runtime.start("com.android.internal.os.ZygoteInit",
  78. startSystemServer ? "start-system-server" : "");
  79. } else if (className) {//可以看出除了zygote,RuntimeInit也是在这里启动的
  80. // Remainder of args get passed to startup class main()
  81. runtime.mClassName = className;
  82. runtime.mArgC = argc - i;
  83. runtime.mArgV = argv + i;
  84. runtime.start("com.android.internal.os.RuntimeInit",
  85. application ? "application" : "tool");
  86. } else {
  87. fprintf(stderr, "Error: no class name or --zygote supplied.\n");
  88. app_usage();
  89. LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
  90. return 10;
  91. }
  92. }

通过对main()函数的分析,可以看出main()主要根据传入的参数初始化启动参数,具体的启动过程是由AppRuntime完成的。AppRuntime的声明和实现都在app_main.cpp中,它继承自AndroidRuntime,AppRuntime的实现如下:

Zygote进程【1】——Zygote的诞生

可以看出start是AndroidRuntime中的方法。通过start函数前面的注释我们了解到它的主要作用是:启动Android运行时环境,包括启动虚拟机和调用className参数所指定的类的main()方法(即:Java中的main方法)。
  1. /*
  2. * Start the Android runtime.  This involves starting the virtual machine
  3. * and calling the "static void main(String[] args)" method in the class
  4. * named by "className".
  5. *
  6. * Passes the main function two arguments, the class name and the specified
  7. * options string.
  8. */
  9. void AndroidRuntime::start(const char* className, const char* options)
  10. {
  11. ALOGD("\n>>>>>> AndroidRuntime START %s <<<<<<\n",
  12. className != NULL ? className : "(unknown)");
  13. /*
  14. * 'startSystemServer == true' means runtime is obsolete and not run from
  15. * init.rc anymore, so we print out the boot start event here.
  16. */
  17. if (strcmp(options, "start-system-server") == 0) {
  18. /* track our progress through the boot sequence */
  19. const int LOG_BOOT_PROGRESS_START = 3000;
  20. LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,
  21. ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
  22. }
  23. //环境变量ANDROID_ROOT是否已经设置,如果未设置,则设置其值为"/system"
  24. const char* rootDir = getenv("ANDROID_ROOT");
  25. if (rootDir == NULL) {
  26. rootDir = "/system";
  27. if (!hasDir("/system")) {
  28. LOG_FATAL("No root directory specified, and /android does not exist.");
  29. return;
  30. }
  31. setenv("ANDROID_ROOT", rootDir, 1);
  32. }
  33. //const char* kernelHack = getenv("LD_ASSUME_KERNEL");
  34. //ALOGD("Found LD_ASSUME_KERNEL='%s'\n", kernelHack);
  35. /* start the virtual machine */
  36. JniInvocation jni_invocation;
  37. jni_invocation.Init(NULL);
  38. JNIEnv* env;
  39. if (startVm(&mJavaVM, &env) != 0) {//启动Java虚拟机
  40. return;
  41. }
  42. onVmCreated(env);//空函数
  43. /*
  44. * Register android functions.
  45. */
  46. if (startReg(env) < 0) {//注册Android JNI函数
  47. ALOGE("Unable to register all android natives\n");
  48. return;
  49. }
  50. /*
  51. * We want to call main() with a String array with arguments in it.
  52. * At present we have two arguments, the class name and an option string.
  53. * Create an array to hold them.
  54. */
  55. jclass stringClass;
  56. jobjectArray strArray;
  57. jstring classNameStr;
  58. jstring optionsStr;
  59. stringClass = env->FindClass("java/lang/String");//JNI中调用java中的String类
  60. assert(stringClass != NULL);
  61. //创建包含2个元素的String数组,这里相当于Java中的String strArray[] = new String[2]
  62. strArray = env->NewObjectArray(2, stringClass, NULL);
  63. assert(strArray != NULL);
  64. classNameStr = env->NewStringUTF(className);//classNameStr的值为"com.android.internal.os.ZygoteInit"
  65. assert(classNameStr != NULL);
  66. env->SetObjectArrayElement(strArray, 0, classNameStr);
  67. optionsStr = env->NewStringUTF(options);//optionsStr的值为"start-system-server"
  68. env->SetObjectArrayElement(strArray, 1, optionsStr);
  69. /*
  70. * Start VM.  This thread becomes the main thread of the VM, and will
  71. * not return until the VM exits.
  72. */
  73. char* slashClassName = toSlashClassName(className);//将"com.android.internal.os.ZygoteInit"中的"."替换成"/"供JNI调用
  74. jclass startClass = env->FindClass(slashClassName);
  75. if (startClass == NULL) {
  76. ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
  77. /* keep going */
  78. } else {
  79. jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
  80. "([Ljava/lang/String;)V");//ZygoteInit类中的main()方法
  81. if (startMeth == NULL) {
  82. ALOGE("JavaVM unable to find main() in '%s'\n", className);
  83. /* keep going */
  84. } else {
  85. env->CallStaticVoidMethod(startClass, startMeth, strArray);//通过JNI调用main()方法
  86. #if 0
  87. if (env->ExceptionCheck())
  88. threadExitUncaughtException(env);
  89. #endif
  90. }
  91. }
  92. free(slashClassName);
  93. //如果JVM退出。这两句代码一般来说执行不到
  94. ALOGD("Shutting down VM\n");
  95. if (mJavaVM->DetachCurrentThread() != JNI_OK)
  96. ALOGW("Warning: unable to detach main thread\n");
  97. if (mJavaVM->DestroyJavaVM() != 0)
  98. ALOGW("Warning: VM did not shut down cleanly\n");
  99. }

通过上面对start()函数的分析可以发现,在start()中主要完成了如下三项工作:

  1. 启动JVM。
  2. 注册Android JNI函数。
  3. 调用ZygoteInit的main()方法。

创建Java虚拟机

start()中与创建虚拟机相关的代码如下:
  1. /* start the virtual machine */
  2. JniInvocation jni_invocation;
  3. jni_invocation.Init(NULL);
  4. JNIEnv* env;
  5. if (startVm(&mJavaVM, &env) != 0) {//启动Java虚拟机
  6. return;
  7. }
  8. onVmCreated(env);//空函数

这里代码中 创建一个JniInvocation实例,并且调用它的成员函数init来初始化JNI环境:

@/libnativehelper/jniInvocation.cpp
  1. bool JniInvocation::Init(const char* library) {
  2. #ifdef HAVE_ANDROID_OS
  3. char default_library[PROPERTY_VALUE_MAX];
  4. property_get("persist.sys.dalvik.vm.lib", default_library, "libdvm.so");
  5. #else
  6. const char* default_library = "libdvm.so";
  7. #endif
  8. if (library == NULL) {
  9. library = default_library;
  10. }
  11. handle_ = dlopen(library, RTLD_NOW);
  12. if (handle_ == NULL) {
  13. ALOGE("Failed to dlopen %s: %s", library, dlerror());
  14. return false;
  15. }
  16. if (!FindSymbol(reinterpret_cast<void**>(&JNI_GetDefaultJavaVMInitArgs_),
  17. "JNI_GetDefaultJavaVMInitArgs")) {
  18. return false;
  19. }
  20. if (!FindSymbol(reinterpret_cast<void**>(&JNI_CreateJavaVM_),
  21. "JNI_CreateJavaVM")) {
  22. return false;
  23. }
  24. if (!FindSymbol(reinterpret_cast<void**>(&JNI_GetCreatedJavaVMs_),
  25. "JNI_GetCreatedJavaVMs")) {
  26. return false;
  27. }
  28. return true;
  29. }

JniInvocation类的成员函数init所做的事情很简单。它首先是读取系统属性persist.sys.dalvik.vm.lib的值。系统属性persist.sys.dalvik.vm.lib的值要么等于libdvm.so,要么等于libart.so,这两个so库分别对应着Dalvik虚拟机和ART虚拟机环境。

在初始化完虚拟机环境后,接下来调用startVm()来创建虚拟机。

@/frameworks/base/core/jni/AndroidRuntime.cpp
  1. /*
  2. * Start the Dalvik Virtual Machine.
  3. *
  4. * Various arguments, most determined by system properties, are passed in.
  5. * The "mOptions" vector is updated.
  6. *
  7. * Returns 0 on success.
  8. */
  9. int AndroidRuntime::startVm(JavaVM** pJavaVM, JNIEnv** pEnv)
  10. {
  11. int result = -1;
  12. JavaVMInitArgs initArgs;
  13. JavaVMOption opt;
  14. char propBuf[PROPERTY_VALUE_MAX];
  15. char stackTraceFileBuf[PROPERTY_VALUE_MAX];
  16. char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
  17. char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
  18. char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
  19. char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
  20. char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
  21. char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
  22. char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
  23. char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
  24. char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
  25. char jitcodecachesizeOptsBuf[sizeof("-Xjitcodecachesize:")-1 + PROPERTY_VALUE_MAX];
  26. char extraOptsBuf[PROPERTY_VALUE_MAX];
  27. char* stackTraceFile = NULL;
  28. bool checkJni = false;
  29. bool checkDexSum = false;
  30. bool logStdio = false;
  31. enum {
  32. kEMDefault,
  33. kEMIntPortable,
  34. kEMIntFast,
  35. kEMJitCompiler,
  36. } executionMode = kEMDefault;
  37. property_get("dalvik.vm.checkjni", propBuf, "");
  38. if (strcmp(propBuf, "true") == 0) {
  39. checkJni = true;
  40. } else if (strcmp(propBuf, "false") != 0) {
  41. /* property is neither true nor false; fall back on kernel parameter */
  42. property_get("ro.kernel.android.checkjni", propBuf, "");
  43. if (propBuf[0] == '1') {
  44. checkJni = true;
  45. }
  46. }
  47. property_get("dalvik.vm.execution-mode", propBuf, "");
  48. if (strcmp(propBuf, "int:portable") == 0) {
  49. executionMode = kEMIntPortable;
  50. } else if (strcmp(propBuf, "int:fast") == 0) {
  51. executionMode = kEMIntFast;
  52. } else if (strcmp(propBuf, "int:jit") == 0) {
  53. executionMode = kEMJitCompiler;
  54. }
  55. property_get("dalvik.vm.stack-trace-file", stackTraceFileBuf, "");
  56. property_get("dalvik.vm.check-dex-sum", propBuf, "");
  57. if (strcmp(propBuf, "true") == 0) {
  58. checkDexSum = true;
  59. }
  60. property_get("log.redirect-stdio", propBuf, "");
  61. if (strcmp(propBuf, "true") == 0) {
  62. logStdio = true;
  63. }
  64. strcpy(enableAssertBuf, "-ea:");
  65. property_get("dalvik.vm.enableassertions", enableAssertBuf+4, "");
  66. strcpy(jniOptsBuf, "-Xjniopts:");
  67. property_get("dalvik.vm.jniopts", jniOptsBuf+10, "");
  68. /* route exit() to our handler */
  69. opt.extraInfo = (void*) runtime_exit;
  70. opt.optionString = "exit";
  71. mOptions.add(opt);
  72. /* route fprintf() to our handler */
  73. opt.extraInfo = (void*) runtime_vfprintf;
  74. opt.optionString = "vfprintf";
  75. mOptions.add(opt);
  76. /* register the framework-specific "is sensitive thread" hook */
  77. opt.extraInfo = (void*) runtime_isSensitiveThread;
  78. opt.optionString = "sensitiveThread";
  79. mOptions.add(opt);
  80. opt.extraInfo = NULL;
  81. /* enable verbose; standard options are { jni, gc, class } */
  82. //options[curOpt++].optionString = "-verbose:jni";
  83. opt.optionString = "-verbose:gc";
  84. mOptions.add(opt);
  85. //options[curOpt++].optionString = "-verbose:class";
  86. /*
  87. * The default starting and maximum size of the heap.  Larger
  88. * values should be specified in a product property override.
  89. */
  90. strcpy(heapstartsizeOptsBuf, "-Xms");
  91. property_get("dalvik.vm.heapstartsize", heapstartsizeOptsBuf+4, "4m");
  92. opt.optionString = heapstartsizeOptsBuf;
  93. mOptions.add(opt);
  94. strcpy(heapsizeOptsBuf, "-Xmx");
  95. property_get("dalvik.vm.heapsize", heapsizeOptsBuf+4, "16m");
  96. opt.optionString = heapsizeOptsBuf;
  97. mOptions.add(opt);
  98. // Increase the main thread's interpreter stack size for bug 6315322.
  99. opt.optionString = "-XX:mainThreadStackSize=24K";
  100. mOptions.add(opt);
  101. // Set the max jit code cache size.  Note: size of 0 will disable the JIT.
  102. strcpy(jitcodecachesizeOptsBuf, "-Xjitcodecachesize:");
  103. property_get("dalvik.vm.jit.codecachesize", jitcodecachesizeOptsBuf+19,  NULL);
  104. if (jitcodecachesizeOptsBuf[19] != '\0') {
  105. opt.optionString = jitcodecachesizeOptsBuf;
  106. mOptions.add(opt);
  107. }
  108. strcpy(heapgrowthlimitOptsBuf, "-XX:HeapGrowthLimit=");
  109. property_get("dalvik.vm.heapgrowthlimit", heapgrowthlimitOptsBuf+20, "");
  110. if (heapgrowthlimitOptsBuf[20] != '\0') {
  111. opt.optionString = heapgrowthlimitOptsBuf;
  112. mOptions.add(opt);
  113. }
  114. strcpy(heapminfreeOptsBuf, "-XX:HeapMinFree=");
  115. property_get("dalvik.vm.heapminfree", heapminfreeOptsBuf+16, "");
  116. if (heapminfreeOptsBuf[16] != '\0') {
  117. opt.optionString = heapminfreeOptsBuf;
  118. mOptions.add(opt);
  119. }
  120. strcpy(heapmaxfreeOptsBuf, "-XX:HeapMaxFree=");
  121. property_get("dalvik.vm.heapmaxfree", heapmaxfreeOptsBuf+16, "");
  122. if (heapmaxfreeOptsBuf[16] != '\0') {
  123. opt.optionString = heapmaxfreeOptsBuf;
  124. mOptions.add(opt);
  125. }
  126. strcpy(heaptargetutilizationOptsBuf, "-XX:HeapTargetUtilization=");
  127. property_get("dalvik.vm.heaptargetutilization", heaptargetutilizationOptsBuf+26, "");
  128. if (heaptargetutilizationOptsBuf[26] != '\0') {
  129. opt.optionString = heaptargetutilizationOptsBuf;
  130. mOptions.add(opt);
  131. }
  132. property_get("ro.config.low_ram", propBuf, "");
  133. if (strcmp(propBuf, "true") == 0) {
  134. opt.optionString = "-XX:LowMemoryMode";
  135. mOptions.add(opt);
  136. }
  137. /*
  138. * Enable or disable dexopt features, such as bytecode verification and
  139. * calculation of register maps for precise GC.
  140. */
  141. property_get("dalvik.vm.dexopt-flags", dexoptFlagsBuf, "");
  142. if (dexoptFlagsBuf[0] != '\0') {
  143. const char* opc;
  144. const char* val;
  145. opc = strstr(dexoptFlagsBuf, "v=");     /* verification */
  146. if (opc != NULL) {
  147. switch (*(opc+2)) {
  148. case 'n':   val = "-Xverify:none";      break;
  149. case 'r':   val = "-Xverify:remote";    break;
  150. case 'a':   val = "-Xverify:all";       break;
  151. default:    val = NULL;                 break;
  152. }
  153. if (val != NULL) {
  154. opt.optionString = val;
  155. mOptions.add(opt);
  156. }
  157. }
  158. opc = strstr(dexoptFlagsBuf, "o=");     /* optimization */
  159. if (opc != NULL) {
  160. switch (*(opc+2)) {
  161. case 'n':   val = "-Xdexopt:none";      break;
  162. case 'v':   val = "-Xdexopt:verified";  break;
  163. case 'a':   val = "-Xdexopt:all";       break;
  164. case 'f':   val = "-Xdexopt:full";      break;
  165. default:    val = NULL;                 break;
  166. }
  167. if (val != NULL) {
  168. opt.optionString = val;
  169. mOptions.add(opt);
  170. }
  171. }
  172. opc = strstr(dexoptFlagsBuf, "m=y");    /* register map */
  173. if (opc != NULL) {
  174. opt.optionString = "-Xgenregmap";
  175. mOptions.add(opt);
  176. /* turn on precise GC while we're at it */
  177. opt.optionString = "-Xgc:precise";
  178. mOptions.add(opt);
  179. }
  180. }
  181. /* enable debugging; set suspend=y to pause during VM init */
  182. /* use android ADB transport */
  183. opt.optionString =
  184. "-agentlib:jdwp=transport=dt_android_adb,suspend=n,server=y";
  185. mOptions.add(opt);
  186. ALOGD("CheckJNI is %s\n", checkJni ? "ON" : "OFF");
  187. if (checkJni) {
  188. /* extended JNI checking */
  189. opt.optionString = "-Xcheck:jni";
  190. mOptions.add(opt);
  191. /* set a cap on JNI global references */
  192. opt.optionString = "-Xjnigreflimit:2000";
  193. mOptions.add(opt);
  194. /* with -Xcheck:jni, this provides a JNI function call trace */
  195. //opt.optionString = "-verbose:jni";
  196. //mOptions.add(opt);
  197. }
  198. char lockProfThresholdBuf[sizeof("-Xlockprofthreshold:") + sizeof(propBuf)];
  199. property_get("dalvik.vm.lockprof.threshold", propBuf, "");
  200. if (strlen(propBuf) > 0) {
  201. strcpy(lockProfThresholdBuf, "-Xlockprofthreshold:");
  202. strcat(lockProfThresholdBuf, propBuf);
  203. opt.optionString = lockProfThresholdBuf;
  204. mOptions.add(opt);
  205. }
  206. /* Force interpreter-only mode for selected opcodes. Eg "1-0a,3c,f1-ff" */
  207. char jitOpBuf[sizeof("-Xjitop:") + PROPERTY_VALUE_MAX];
  208. property_get("dalvik.vm.jit.op", propBuf, "");
  209. if (strlen(propBuf) > 0) {
  210. strcpy(jitOpBuf, "-Xjitop:");
  211. strcat(jitOpBuf, propBuf);
  212. opt.optionString = jitOpBuf;
  213. mOptions.add(opt);
  214. }
  215. /* Force interpreter-only mode for selected methods */
  216. char jitMethodBuf[sizeof("-Xjitmethod:") + PROPERTY_VALUE_MAX];
  217. property_get("dalvik.vm.jit.method", propBuf, "");
  218. if (strlen(propBuf) > 0) {
  219. strcpy(jitMethodBuf, "-Xjitmethod:");
  220. strcat(jitMethodBuf, propBuf);
  221. opt.optionString = jitMethodBuf;
  222. mOptions.add(opt);
  223. }
  224. if (executionMode == kEMIntPortable) {
  225. opt.optionString = "-Xint:portable";
  226. mOptions.add(opt);
  227. } else if (executionMode == kEMIntFast) {
  228. opt.optionString = "-Xint:fast";
  229. mOptions.add(opt);
  230. } else if (executionMode == kEMJitCompiler) {
  231. opt.optionString = "-Xint:jit";
  232. mOptions.add(opt);
  233. }
  234. if (checkDexSum) {
  235. /* perform additional DEX checksum tests */
  236. opt.optionString = "-Xcheckdexsum";
  237. mOptions.add(opt);
  238. }
  239. if (logStdio) {
  240. /* convert stdout/stderr to log messages */
  241. opt.optionString = "-Xlog-stdio";
  242. mOptions.add(opt);
  243. }
  244. if (enableAssertBuf[4] != '\0') {
  245. /* accept "all" to mean "all classes and packages" */
  246. if (strcmp(enableAssertBuf+4, "all") == 0)
  247. enableAssertBuf[3] = '\0';
  248. ALOGI("Assertions enabled: '%s'\n", enableAssertBuf);
  249. opt.optionString = enableAssertBuf;
  250. mOptions.add(opt);
  251. } else {
  252. ALOGV("Assertions disabled\n");
  253. }
  254. if (jniOptsBuf[10] != '\0') {
  255. ALOGI("JNI options: '%s'\n", jniOptsBuf);
  256. opt.optionString = jniOptsBuf;
  257. mOptions.add(opt);
  258. }
  259. if (stackTraceFileBuf[0] != '\0') {
  260. static const char* stfOptName = "-Xstacktracefile:";
  261. stackTraceFile = (char*) malloc(strlen(stfOptName) +
  262. strlen(stackTraceFileBuf) +1);
  263. strcpy(stackTraceFile, stfOptName);
  264. strcat(stackTraceFile, stackTraceFileBuf);
  265. opt.optionString = stackTraceFile;
  266. mOptions.add(opt);
  267. }
  268. /* extra options; parse this late so it overrides others */
  269. property_get("dalvik.vm.extra-opts", extraOptsBuf, "");
  270. parseExtraOpts(extraOptsBuf);
  271. /* Set the properties for locale */
  272. {
  273. char langOption[sizeof("-Duser.language=") + 3];
  274. char regionOption[sizeof("-Duser.region=") + 3];
  275. strcpy(langOption, "-Duser.language=");
  276. strcpy(regionOption, "-Duser.region=");
  277. readLocale(langOption, regionOption);
  278. opt.extraInfo = NULL;
  279. opt.optionString = langOption;
  280. mOptions.add(opt);
  281. opt.optionString = regionOption;
  282. mOptions.add(opt);
  283. }
  284. /*
  285. * We don't have /tmp on the device, but we often have an SD card.  Apps
  286. * shouldn't use this, but some test suites might want to exercise it.
  287. */
  288. opt.optionString = "-Djava.io.tmpdir=/sdcard";
  289. mOptions.add(opt);
  290. initArgs.version = JNI_VERSION_1_4;
  291. initArgs.options = mOptions.editArray();
  292. initArgs.nOptions = mOptions.size();
  293. initArgs.ignoreUnrecognized = JNI_FALSE;
  294. /*
  295. * Initialize the VM.
  296. *
  297. * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
  298. * If this call succeeds, the VM is ready, and we can start issuing
  299. * JNI calls.
  300. */
  301. if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
  302. ALOGE("JNI_CreateJavaVM failed\n");
  303. goto bail;
  304. }
  305. result = 0;
  306. bail:
  307. free(stackTraceFile);
  308. return result;
  309. }

可以看出这个函数的绝大部分都是在设置Java虚拟机的各项参数,没有什么好说。看到下面这一段变量定义,不知道大家有没有去思考过,这里为什么用PROPERTY_VALUE_MAX作为初始大小?

  1. char propBuf[PROPERTY_VALUE_MAX];
  2. char stackTraceFileBuf[PROPERTY_VALUE_MAX];
  3. char dexoptFlagsBuf[PROPERTY_VALUE_MAX];
  4. char enableAssertBuf[sizeof("-ea:")-1 + PROPERTY_VALUE_MAX];
  5. char jniOptsBuf[sizeof("-Xjniopts:")-1 + PROPERTY_VALUE_MAX];
  6. char heapstartsizeOptsBuf[sizeof("-Xms")-1 + PROPERTY_VALUE_MAX];
  7. char heapsizeOptsBuf[sizeof("-Xmx")-1 + PROPERTY_VALUE_MAX];
  8. char heapgrowthlimitOptsBuf[sizeof("-XX:HeapGrowthLimit=")-1 + PROPERTY_VALUE_MAX];
  9. char heapminfreeOptsBuf[sizeof("-XX:HeapMinFree=")-1 + PROPERTY_VALUE_MAX];
  10. char heapmaxfreeOptsBuf[sizeof("-XX:HeapMaxFree=")-1 + PROPERTY_VALUE_MAX];
  11. char heaptargetutilizationOptsBuf[sizeof("-XX:HeapTargetUtilization=")-1 + PROPERTY_VALUE_MAX];
  12. char jitcodecachesizeOptsBuf[sizeof("-Xjitcodecachesize:")-1 + PROPERTY_VALUE_MAX];

下面是PROPERTY_VALUE_MAX的定义:

@system/core/include/cutils/properties.h
  1. /* System properties are *small* name value pairs managed by the
  2. ** property service.  If your data doesn't fit in the provided
  3. ** space it is not appropriate for a system property.
  4. **
  5. ** WARNING: system/bionic/include/sys/system_properties.h also defines
  6. **          these, but with different names.  (TODO: fix that)
  7. */
  8. #define PROPERTY_KEY_MAX   PROP_NAME_MAX
  9. #define PROPERTY_VALUE_MAX  PROP_VALUE_MAX

所以,没错,PROPERTY_VALUE_MAX是Android中属性value的最大长度,而java虚拟机的这些参数都是通过Android属性赋值和控制的,所以他们的值得大小肯定不能超过属性的最大长度。下面是我的小米2S手机中的一部分Java参数。Android中所有属性都可以通过getprop命令来查看。

  1. [dalvik.vm.heapconcurrentstart]: [2097152]
  2. [dalvik.vm.heapgrowthlimit]: [96m]
  3. [dalvik.vm.heapidealfree]: [8388608]
  4. [dalvik.vm.heapsize]: [384m]
  5. [dalvik.vm.heapstartsize]: [8m]
  6. [dalvik.vm.heaputilization]: [0.25]
  7. [dalvik.vm.stack-trace-file]: [/data/anr/traces.txt]

下面来看一下startVm()中的最后几句:

  1. /*
  2. * Initialize the VM.
  3. *
  4. * The JavaVM* is essentially per-process, and the JNIEnv* is per-thread.
  5. * If this call succeeds, the VM is ready, and we can start issuing
  6. * JNI calls.
  7. */
  8. if (JNI_CreateJavaVM(pJavaVM, pEnv, &initArgs) < 0) {
  9. ALOGE("JNI_CreateJavaVM failed\n");
  10. goto bail;
  11. }

startVm()在最后会调用JNI_CreateJavaVM()来创建虚拟机。这里顺便看一下JNI_CreateJavaVM()的这段说明:”Java虚拟机对象JavaVm对象每个进程有一个,JNI环境变量JNIEnv每个线程有一个“。这里也告诉我们,在写JNI代码时要注意:JNIEnv不能在任意线程中使用,必须是原本就是Java线程(Java代码通过JNI调用native代码时,发起调用的那个肯定是Java线程),或者是让已有的native线程通过JNI来attach到Java环境。具体这里不做详细介绍,感兴趣的读者可以参考Oracle官方文档http://docs.oracle.com/javase/6/docs/technotes/guides/jni/spec/jniTOC.html。在JNI_CreateJavaVM()调用成功后虚拟机VM就已经创建好了,接下来就可以进行JNI相关调用了。


注册JNI函数

创建好了虚拟机,接下来要给虚拟机注册一些JNI函数。不知道各位读者有没有想过,这里为什么要注册JNI函数呢?没错,因为在基于虚拟机的Java世界里,Java并不是万能的,它用到的很多方法(例如:音频、视频相关、Binder等)都需要以native的方式实现,而这些方法就需要虚拟机以JNI的方式进行加载。
@/frameworks/base/core/jni/AndroidRuntime.cpp
  1. /*
  2. * Register android native functions with the VM.
  3. */
  4. /*static*/ int AndroidRuntime::startReg(JNIEnv* env)
  5. {
  6. /*
  7. * This hook causes all future threads created in this process to be
  8. * attached to the JavaVM.  (This needs to go away in favor of JNI
  9. * Attach calls.)
  10. */
  11. <span style="white-space:pre">    </span>//设置Thread类的线程创建函数为javaCreateThreadEtc
  12. androidSetCreateThreadFunc((android_create_thread_fn) javaCreateThreadEtc);
  13. ALOGV("--- registering native functions ---\n");
  14. /*
  15. * Every "register" function calls one or more things that return
  16. * a local reference (e.g. FindClass).  Because we haven't really
  17. * started the VM yet, they're all getting stored in the base frame
  18. * and never released.  Use Push/Pop to manage the storage.
  19. */
  20. env->PushLocalFrame(200);
  21. if (register_jni_procs(gRegJNI, NELEM(gRegJNI), env) < 0) {
  22. env->PopLocalFrame(NULL);
  23. return -1;
  24. }
  25. env->PopLocalFrame(NULL);
  26. //createJavaThread("fubar", quickTest, (void*) "hello");
  27. return 0;
  28. }

我们来看一下register_jni_procs()的代码:

  1. static int register_jni_procs(const RegJNIRec array[], size_t count, JNIEnv* env)
  2. {
  3. for (size_t i = 0; i < count; i++) {
  4. if (array[i].mProc(env) < 0) {
  5. #ifndef NDEBUG
  6. ALOGD("----------!!! %s failed to load\n", array[i].mName);
  7. #endif
  8. return -1;
  9. }
  10. }
  11. return 0;
  12. }

看一下从startReg()传过来的参数gRegJNI:

  1. static const RegJNIRec gRegJNI[] = {
  2. REG_JNI(register_android_debug_JNITest),
  3. REG_JNI(register_com_android_internal_os_RuntimeInit),
  4. REG_JNI(register_android_os_SystemClock),
  5. REG_JNI(register_android_util_EventLog),
  6. REG_JNI(register_android_util_Log),
  7. REG_JNI(register_android_util_FloatMath),
  8. REG_JNI(register_android_text_format_Time),
  9. REG_JNI(register_android_content_AssetManager),
  10. REG_JNI(register_android_content_StringBlock),
  11. REG_JNI(register_android_content_XmlBlock),
  12. REG_JNI(register_android_emoji_EmojiFactory),
  13. REG_JNI(register_android_text_AndroidCharacter),
  14. REG_JNI(register_android_text_AndroidBidi),
  15. REG_JNI(register_android_view_InputDevice),
  16. REG_JNI(register_android_view_KeyCharacterMap),
  17. REG_JNI(register_android_os_Process),
  18. REG_JNI(register_android_os_SystemProperties),
  19. REG_JNI(register_android_os_Binder),
  20. REG_JNI(register_android_os_Parcel),
  21. REG_JNI(register_android_view_DisplayEventReceiver),
  22. REG_JNI(register_android_nio_utils),
  23. REG_JNI(register_android_graphics_Graphics),
  24. REG_JNI(register_android_view_GraphicBuffer),
  25. REG_JNI(register_android_view_GLES20DisplayList),
  26. REG_JNI(register_android_view_GLES20Canvas),
  27. REG_JNI(register_android_view_HardwareRenderer),
  28. REG_JNI(register_android_view_Surface),
  29. REG_JNI(register_android_view_SurfaceControl),
  30. REG_JNI(register_android_view_SurfaceSession),
  31. REG_JNI(register_android_view_TextureView),
  32. REG_JNI(register_com_google_android_gles_jni_EGLImpl),
  33. REG_JNI(register_com_google_android_gles_jni_GLImpl),
  34. REG_JNI(register_android_opengl_jni_EGL14),
  35. REG_JNI(register_android_opengl_jni_EGLExt),
  36. REG_JNI(register_android_opengl_jni_GLES10),
  37. REG_JNI(register_android_opengl_jni_GLES10Ext),
  38. REG_JNI(register_android_opengl_jni_GLES11),
  39. REG_JNI(register_android_opengl_jni_GLES11Ext),
  40. REG_JNI(register_android_opengl_jni_GLES20),
  41. REG_JNI(register_android_opengl_jni_GLES30),
  42. REG_JNI(register_android_graphics_Bitmap),
  43. REG_JNI(register_android_graphics_BitmapFactory),
  44. REG_JNI(register_android_graphics_BitmapRegionDecoder),
  45. REG_JNI(register_android_graphics_Camera),
  46. REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor),
  47. REG_JNI(register_android_graphics_Canvas),
  48. REG_JNI(register_android_graphics_ColorFilter),
  49. REG_JNI(register_android_graphics_DrawFilter),
  50. REG_JNI(register_android_graphics_Interpolator),
  51. REG_JNI(register_android_graphics_LayerRasterizer),
  52. REG_JNI(register_android_graphics_MaskFilter),
  53. REG_JNI(register_android_graphics_Matrix),
  54. REG_JNI(register_android_graphics_Movie),
  55. REG_JNI(register_android_graphics_NinePatch),
  56. REG_JNI(register_android_graphics_Paint),
  57. REG_JNI(register_android_graphics_Path),
  58. REG_JNI(register_android_graphics_PathMeasure),
  59. REG_JNI(register_android_graphics_PathEffect),
  60. REG_JNI(register_android_graphics_Picture),
  61. REG_JNI(register_android_graphics_PorterDuff),
  62. REG_JNI(register_android_graphics_Rasterizer),
  63. REG_JNI(register_android_graphics_Region),
  64. REG_JNI(register_android_graphics_Shader),
  65. REG_JNI(register_android_graphics_SurfaceTexture),
  66. REG_JNI(register_android_graphics_Typeface),
  67. REG_JNI(register_android_graphics_Xfermode),
  68. REG_JNI(register_android_graphics_YuvImage),
  69. REG_JNI(register_android_graphics_pdf_PdfDocument),
  70. REG_JNI(register_android_database_CursorWindow),
  71. REG_JNI(register_android_database_SQLiteConnection),
  72. REG_JNI(register_android_database_SQLiteGlobal),
  73. REG_JNI(register_android_database_SQLiteDebug),
  74. REG_JNI(register_android_os_Debug),
  75. REG_JNI(register_android_os_FileObserver),
  76. REG_JNI(register_android_os_FileUtils),
  77. REG_JNI(register_android_os_MessageQueue),
  78. REG_JNI(register_android_os_SELinux),
  79. REG_JNI(register_android_os_Trace),
  80. REG_JNI(register_android_os_UEventObserver),
  81. REG_JNI(register_android_net_LocalSocketImpl),
  82. REG_JNI(register_android_net_NetworkUtils),
  83. REG_JNI(register_android_net_TrafficStats),
  84. REG_JNI(register_android_net_wifi_WifiNative),
  85. REG_JNI(register_android_os_MemoryFile),
  86. REG_JNI(register_com_android_internal_os_ZygoteInit),
  87. REG_JNI(register_android_hardware_Camera),
  88. REG_JNI(register_android_hardware_camera2_CameraMetadata),
  89. REG_JNI(register_android_hardware_SensorManager),
  90. REG_JNI(register_android_hardware_SerialPort),
  91. REG_JNI(register_android_hardware_UsbDevice),
  92. REG_JNI(register_android_hardware_UsbDeviceConnection),
  93. REG_JNI(register_android_hardware_UsbRequest),
  94. REG_JNI(register_android_media_AudioRecord),
  95. REG_JNI(register_android_media_AudioSystem),
  96. REG_JNI(register_android_media_AudioTrack),
  97. REG_JNI(register_android_media_JetPlayer),
  98. REG_JNI(register_android_media_RemoteDisplay),
  99. REG_JNI(register_android_media_ToneGenerator),
  100. REG_JNI(register_android_opengl_classes),
  101. REG_JNI(register_android_server_NetworkManagementSocketTagger),
  102. REG_JNI(register_android_server_Watchdog),
  103. REG_JNI(register_android_ddm_DdmHandleNativeHeap),
  104. REG_JNI(register_android_backup_BackupDataInput),
  105. REG_JNI(register_android_backup_BackupDataOutput),
  106. REG_JNI(register_android_backup_FileBackupHelperBase),
  107. REG_JNI(register_android_backup_BackupHelperDispatcher),
  108. REG_JNI(register_android_app_backup_FullBackup),
  109. REG_JNI(register_android_app_ActivityThread),
  110. REG_JNI(register_android_app_NativeActivity),
  111. REG_JNI(register_android_view_InputChannel),
  112. REG_JNI(register_android_view_InputEventReceiver),
  113. REG_JNI(register_android_view_InputEventSender),
  114. REG_JNI(register_android_view_InputQueue),
  115. REG_JNI(register_android_view_KeyEvent),
  116. REG_JNI(register_android_view_MotionEvent),
  117. REG_JNI(register_android_view_PointerIcon),
  118. REG_JNI(register_android_view_VelocityTracker),
  119. REG_JNI(register_android_content_res_ObbScanner),
  120. REG_JNI(register_android_content_res_Configuration),
  121. REG_JNI(register_android_animation_PropertyValuesHolder),
  122. REG_JNI(register_com_android_internal_content_NativeLibraryHelper),
  123. REG_JNI(register_com_android_internal_net_NetworkStatsFactory),
  124. };

REG_JNI是系统定义的一个宏:

  1. #ifdef NDEBUG
  2. #define REG_JNI(name)      { name }
  3. struct RegJNIRec {
  4. int (*mProc)(JNIEnv*);
  5. };
  6. #else
  7. #define REG_JNI(name)      { name, #name }
  8. struct RegJNIRec {
  9. int (*mProc)(JNIEnv*);
  10. const char* mName;
  11. };
  12. #endif

以gRegJNI数组中的一项为例,REG_JNI(register_android_debug_JNITest) 展开REG_JNI后变为:

  1. { register_android_debug_JNITest, "register_android_debug_JNITest" }

所以当register_jni_procs()中调用mProcess时,最终调用的是android_debug_JNITest类中的register_android_debug_JNITest:

  1. int register_android_debug_JNITest(JNIEnv* env)
  2. {
  3. return jniRegisterNativeMethods(env, "android/debug/JNITest",
  4. gMethods, NELEM(gMethods));
  5. }

到这里JNI注册就讲完了。

ZygoteInit初始化

在JNI注册完成后,让我们再回头继续看AndroidRuntime中start函数:

  1. env->CallStaticVoidMethod(startClass, startMeth, strArray);//通过JNI调用main()方法

在start()中通过JNI调用ZygoteInit类的main()方法,这个main()方法即使从native世界到Java世界的入口。

@/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
  1. public static void main(String argv[]) {
  2. try {
  3. // Start profiling the zygote initialization.
  4. SamplingProfilerIntegration.start();//启动性能统计
  5. registerZygoteSocket();//注册zygote用的socket
  6. EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
  7. SystemClock.uptimeMillis());
  8. preload();//初始化,主要进行framework中一些类和资源的预加载
  9. EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
  10. SystemClock.uptimeMillis());
  11. // Finish profiling the zygote initialization.
  12. SamplingProfilerIntegration.writeZygoteSnapshot();//结束统计并生成结果文件
  13. // Do an initial gc to clean up after startup
  14. gc();//强制进行一次回收
  15. // Disable tracing so that forked processes do not inherit stale tracing tags from
  16. // Zygote.
  17. Trace.setTracingEnabled(false);
  18. // If requested, start system server directly from Zygote
  19. if (argv.length != 2) {
  20. throw new RuntimeException(argv[0] + USAGE_STRING);
  21. }
  22. if (argv[1].equals("start-system-server")) {
  23. startSystemServer();//启动system_server进程
  24. } else if (!argv[1].equals("")) {
  25. throw new RuntimeException(argv[0] + USAGE_STRING);
  26. }
  27. Log.i(TAG, "Accepting command socket connections");
  28. runSelectLoop();//变成守护进程,接收socket信息进行处理
  29. closeServerSocket();
  30. } catch (MethodAndArgsCaller caller) {
  31. caller.run();
  32. } catch (RuntimeException ex) {
  33. Log.e(TAG, "Zygote died with exception", ex);
  34. closeServerSocket();
  35. throw ex;
  36. }
  37. }

简单总结一下ZygoteInit类中main()函数主要做了如下几件事:

  1. 注册Zygote用的socket
  2. 类和资源的预加载
  3. 启动system_server进程
  4. 进入一个死循环,等待接收和处理socket事件
接下来将对它们一一进行分析。


registerZygoteSocket()方法

registerZygoteSocket()方法的实现如下:
  1. /**
  2. * Registers a server socket for zygote command connections
  3. *
  4. * @throws RuntimeException when open fails
  5. */
  6. private static void registerZygoteSocket() {
  7. if (sServerSocket == null) {
  8. int fileDesc;
  9. try {
  10. String env = System.getenv(ANDROID_SOCKET_ENV);//从环境变量中获取socket的fd
  11. fileDesc = Integer.parseInt(env);
  12. } catch (RuntimeException ex) {
  13. throw new RuntimeException(
  14. ANDROID_SOCKET_ENV + " unset or invalid", ex);
  15. }
  16. try {
  17. sServerSocket = new LocalServerSocket(
  18. createFileDescriptor(fileDesc));
  19. } catch (IOException ex) {
  20. throw new RuntimeException(
  21. "Error binding to local socket '" + fileDesc + "'", ex);
  22. }
  23. }
  24. }

registerZygoteSocket()方法比较简单,就是创建了一个服务端Socket。

类和资源的预加载

在Zygote中通过preload()方法完成类和资源的加载,它的实现如下:

  1. static void preload() {
  2. preloadClasses();//加载类
  3. preloadResources();//加载资源
  4. preloadOpenGL();//加载OpenGL
  5. }

先看一下preloadClasses():

  1. /**
  2. * Performs Zygote process initialization. Loads and initializes
  3. * commonly used classes.
  4. *
  5. * Most classes only cause a few hundred bytes to be allocated, but
  6. * a few will allocate a dozen Kbytes (in one case, 500+K).
  7. */
  8. private static void preloadClasses() {
  9. final VMRuntime runtime = VMRuntime.getRuntime();
  10. InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(
  11. PRELOADED_CLASSES);//加载preloaded-classes这个文件中定义的需要预加载的类
  12. if (is == null) {
  13. Log.e(TAG, "Couldn't find " + PRELOADED_CLASSES + ".");
  14. } else {
  15. Log.i(TAG, "Preloading classes...");
  16. long startTime = SystemClock.uptimeMillis();
  17. // Drop root perms while running static initializers.
  18. setEffectiveGroup(UNPRIVILEGED_GID);
  19. setEffectiveUser(UNPRIVILEGED_UID);
  20. // Alter the target heap utilization.  With explicit GCs this
  21. // is not likely to have any effect.
  22. float defaultUtilization = runtime.getTargetHeapUtilization();
  23. runtime.setTargetHeapUtilization(0.8f);
  24. // Start with a clean slate.
  25. System.gc();
  26. runtime.runFinalizationSync();
  27. Debug.startAllocCounting();
  28. try {
  29. BufferedReader br
  30. = new BufferedReader(new InputStreamReader(is), 256);
  31. int count = 0;
  32. String line;
  33. while ((line = br.readLine()) != null) {
  34. // Skip comments and blank lines.
  35. line = line.trim();
  36. if (line.startsWith("#") || line.equals("")) {
  37. continue;
  38. }
  39. try {
  40. if (false) {
  41. Log.v(TAG, "Preloading " + line + "...");
  42. }
  43. Class.forName(line);//以反射的方式加载类
  44. if (Debug.getGlobalAllocSize() > PRELOAD_GC_THRESHOLD) {
  45. if (false) {
  46. Log.v(TAG,
  47. " GC at " + Debug.getGlobalAllocSize());
  48. }
  49. System.gc();
  50. runtime.runFinalizationSync();
  51. Debug.resetGlobalAllocSize();
  52. }
  53. count++;
  54. } catch (ClassNotFoundException e) {
  55. Log.w(TAG, "Class not found for preloading: " + line);
  56. } catch (Throwable t) {
  57. Log.e(TAG, "Error preloading " + line + ".", t);
  58. if (t instanceof Error) {
  59. throw (Error) t;
  60. }
  61. if (t instanceof RuntimeException) {
  62. throw (RuntimeException) t;
  63. }
  64. throw new RuntimeException(t);
  65. }
  66. }
  67. Log.i(TAG, "...preloaded " + count + " classes in "
  68. + (SystemClock.uptimeMillis()-startTime) + "ms.");
  69. } catch (IOException e) {
  70. Log.e(TAG, "Error reading " + PRELOADED_CLASSES + ".", e);
  71. } finally {
  72. IoUtils.closeQuietly(is);
  73. // Restore default.
  74. runtime.setTargetHeapUtilization(defaultUtilization);
  75. // Fill in dex caches with classes, fields, and methods brought in by preloading.
  76. runtime.preloadDexCaches();
  77. Debug.stopAllocCounting();
  78. // Bring back root. We'll need it later.
  79. setEffectiveUser(ROOT_UID);
  80. setEffectiveGroup(ROOT_GID);
  81. }
  82. }
  83. }

preloadClasses()的实现很简单,这里说一下preloaded-classes文件:

  1. # Classes which are preloaded by com.android.internal.os.ZygoteInit.
  2. # Automatically generated by frameworks/base/tools/preload/WritePreloadedClassFile.java.
  3. # MIN_LOAD_TIME_MICROS=1250
  4. # MIN_PROCESSES=10
  5. android.R$styleable
  6. android.accounts.Account
  7. android.accounts.Account$1
  8. android.accounts.AccountManager
  9. android.accounts.AccountManager$12
  10. android.accounts.AccountManager$13
  11. android.accounts.AccountManager$6
  12. android.accounts.AccountManager$AmsTask
  13. android.accounts.AccountManager$AmsTask$1
  14. android.accounts.AccountManager$AmsTask$Response

在Android4.4的源代码中有2782行,也就说这里在系统启动时需要预加载两千多个类,而这仅仅是源代码,在手机厂商的代码中,需要进行预加载的类的数量将会超过这个数。preloaded-classes文件时由WritePreloadedClassFile类生成的。WritePreloadedClassFile将某个类加入预加载文件preloaded-classes中的条件时:该类被不少于10个进程使用,并且家中该类好使超过1250微秒。WritePreloadedClassFile里面的实现非常简单,感兴趣的读者可以自行阅读。

  1. /**
  2. * Preload any class that take longer to load than MIN_LOAD_TIME_MICROS us.
  3. */
  4. static final int MIN_LOAD_TIME_MICROS = 1250;
  5. /**
  6. * Preload any class that was loaded by at least MIN_PROCESSES processes.
  7. */
  8. static final int MIN_PROCESSES = 10;

这里我简单估算了一下,在Android4.4中预加载这些类需要4秒钟作用,这对于系统启动来说是一个比较长的时间,因此在进行系统启动速度的优化时,这里可以作为一个优化大点。

在看preloadClass的代码时有些读者看的setEffectiveUser的几句代码不明白什么意思:

  1. private static void preloadClasses() {
  2. ......
  3. // Drop root perms while running static initializers.
  4. setEffectiveGroup(UNPRIVILEGED_GID);
  5. setEffectiveUser(UNPRIVILEGED_UID);
  6. ......
  7. } finally {
  8. ......
  9. // Bring back root. We'll need it later.
  10. setEffectiveUser(ROOT_UID);
  11. setEffectiveGroup(ROOT_GID);
  12. }
  13. }
  14. }

以setEffectiveUser为例看一下它的实现:

  1. /**
  2. * Sets effective user ID.
  3. */
  4. private static void setEffectiveUser(int uid) {
  5. int errno = setreuid(ROOT_UID, uid);
  6. if (errno != 0) {
  7. Log.e(TAG, "setreuid() failed. errno: " + errno);
  8. }
  9. }
  1. /**
  2. * The Linux syscall "setreuid()"
  3. * @param ruid real uid
  4. * @param euid effective uid
  5. * @return 0 on success, non-zero errno on fail
  6. */
  7. static native int setreuid(int ruid, int euid);

可以看出这里setEffectiveUser这几句的意思是:在类加载之前临时降低euid(真实用户ID)权限,加载完成后恢复。关于Linux各种userid的说明如下:

有效用户ID

有效用户IDEffective UID,即EUID)与有效用户组IDEffective Group ID,即EGID)在创建与访问文件的时候发挥作用;具体来说,创建文件时,系统内核将根据创建文件的进程的EUID与EGID设定文件的所有者/组属性,而在访问文件时,内核亦根据访问进程的EUID与EGID决定其能否访问文件。

真实用户ID

真实用户IDReal UID,即RUID)与真实用户组IDReal GID,即RGID)用于辨识进程的真正所有者,且会影响到进程发送信号的权限。没有超级用户权限的进程仅在其RUID与目标进程的RUID相匹配时才能向目标进程发送信号,例如在父子进程间,子进程父进程处继承了认证信息,使得父子进程间可以互相发送信号。

暂存用户ID

暂存用户IDSaved UID,即SUID)于以提升权限运行的进程暂时需要做一些不需特权的操作时使用,这种情况下进程会暂时将自己的有效用户ID从特权用户(常为root)对应的UID变为某个非特权用户对应的UID,而后将原有的特权用户UID复制为SUID暂存;之后当进程完成不需特权的操作后,进程使用SUID的值重置EUID以重新获得特权。在这里需要说明的是,无特权进程的EUID值只能设为与RUID、SUID与EUID(也即不改变)之一相同的值。

文件系统用户ID

文件系统用户IDFile System UID,即FSUID)在Linux中使用,且只用于对文件系统的访问权限控制,在没有明确设定的情况下与EUID相同(若FSUID为root的UID,则SUID、RUID与EUID必至少有一亦为root的UID),且EUID改变也会影响到FSUID。设立FSUID是为了允许程序(如NFS服务器)在不需获取向给定UID账户发送信号的情况下以给定UID的权限来限定自己的文件系统权限。

这段代码转自http://zh.wikipedia.org/wiki/%E7%94%A8%E6%88%B7ID

那这里这样做的用意何在?我猜这里是为了保证预加载的类是所有的用户都是可用的。

预加载资源的代码如下:

  1. /**
  2. * Load in commonly used resources, so they can be shared across
  3. * processes.
  4. *
  5. * These tend to be a few Kbytes, but are frequently in the 20-40K
  6. * range, and occasionally even larger.
  7. */
  8. private static void preloadResources() {
  9. final VMRuntime runtime = VMRuntime.getRuntime();
  10. Debug.startAllocCounting();
  11. try {
  12. System.gc();
  13. runtime.runFinalizationSync();
  14. mResources = Resources.getSystem();
  15. mResources.startPreloading();
  16. if (PRELOAD_RESOURCES) {
  17. Log.i(TAG, "Preloading resources...");
  18. long startTime = SystemClock.uptimeMillis();
  19. TypedArray ar = mResources.obtainTypedArray(
  20. com.android.internal.R.array.preloaded_drawables);
  21. int N = preloadDrawables(runtime, ar);//预加载Drawable
  22. ar.recycle();
  23. Log.i(TAG, "...preloaded " + N + " resources in "
  24. + (SystemClock.uptimeMillis()-startTime) + "ms.");
  25. startTime = SystemClock.uptimeMillis();
  26. ar = mResources.obtainTypedArray(
  27. com.android.internal.R.array.preloaded_color_state_lists);
  28. N = preloadColorStateLists(runtime, ar);//预加载Color
  29. ar.recycle();
  30. Log.i(TAG, "...preloaded " + N + " resources in "
  31. + (SystemClock.uptimeMillis()-startTime) + "ms.");
  32. }
  33. mResources.finishPreloading();
  34. } catch (RuntimeException e) {
  35. Log.w(TAG, "Failure preloading resources", e);
  36. } finally {
  37. Debug.stopAllocCounting();
  38. }
  39. }

preloadResources的加载过程又分为加载Drawable和加载Color。

除了加载类和资源,还会加载OpenGL的一些东西:

  1. private static void preloadOpenGL() {
  2. if (!SystemProperties.getBoolean(PROPERTY_DISABLE_OPENGL_PRELOADING, false)) {
  3. EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
  4. }
  5. }

在创建socket和资源加载前后有这么两句:

  1. // Start profiling the zygote initialization.
  2. SamplingProfilerIntegration.start();//启动性能统计
  3. registerZygoteSocket();//注册zygote用的socket
  4. EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
  5. SystemClock.uptimeMillis());
  6. preload();//初始化,主要进行framework中一些类和资源的预加载
  7. EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
  8. SystemClock.uptimeMillis());
  9. // Finish profiling the zygote initialization.
  10. SamplingProfilerIntegration.writeZygoteSnapshot();//结束统计并生成结果文件

所以,这里SamplingProfilerIntegration统计的是创建socket和类及资源初始化的时间。

启动system_server

  1. /**
  2. * Prepare the arguments and fork for the system server process.
  3. */
  4. private static boolean startSystemServer()
  5. throws MethodAndArgsCaller, RuntimeException {
  6. long capabilities = posixCapabilitiesAsBits(
  7. OsConstants.CAP_KILL,
  8. OsConstants.CAP_NET_ADMIN,
  9. OsConstants.CAP_NET_BIND_SERVICE,
  10. OsConstants.CAP_NET_BROADCAST,
  11. OsConstants.CAP_NET_RAW,
  12. OsConstants.CAP_SYS_MODULE,
  13. OsConstants.CAP_SYS_NICE,
  14. OsConstants.CAP_SYS_RESOURCE,
  15. OsConstants.CAP_SYS_TIME,
  16. OsConstants.CAP_SYS_TTY_CONFIG
  17. );
  18. /* Hardcoded command line to start the system server */
  19. String args[] = {
  20. "--setuid=1000",
  21. "--setgid=1000",
  22. "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1032,3001,3002,3003,3006,3007",
  23. "--capabilities=" + capabilities + "," + capabilities,
  24. "--runtime-init",
  25. "--nice-name=system_server",
  26. "com.android.server.SystemServer",
  27. };
  28. ZygoteConnection.Arguments parsedArgs = null;
  29. int pid;
  30. try {
  31. parsedArgs = new ZygoteConnection.Arguments(args);
  32. ZygoteConnection.applyDebuggerSystemProperty(parsedArgs);
  33. ZygoteConnection.applyInvokeWithSystemProperty(parsedArgs);
  34. /* Request to fork the system server process */
  35. pid = Zygote.forkSystemServer(//以fork的方式创建system_server进程
  36. parsedArgs.uid, parsedArgs.gid,
  37. parsedArgs.gids,
  38. parsedArgs.debugFlags,
  39. null,
  40. parsedArgs.permittedCapabilities,
  41. parsedArgs.effectiveCapabilities);
  42. } catch (IllegalArgumentException ex) {
  43. throw new RuntimeException(ex);
  44. }
  45. /* For child process */
  46. if (pid == 0) {//pid==0说明在子进程中,父进程为Zygote
  47. handleSystemServerProcess(parsedArgs);
  48. }
  49. return true;
  50. }

这里前面的一大段代码主要是在为fork准备参数parsedArgs,然后Zygote会forkSystemServer来创建system_server,forkSystemServer()方法最终会调用Linux中的fork()。

runSelectLoop()方法

在创建system_server后,Zygote调用runSelectLoop()进入到一个死循环中:

  1. /**
  2. * Runs the zygote process's select loop. Accepts new connections as
  3. * they happen, and reads commands from connections one spawn-request's
  4. * worth at a time.
  5. *
  6. * @throws MethodAndArgsCaller in a child process when a main() should
  7. * be executed.
  8. */
  9. private static void runSelectLoop() throws MethodAndArgsCaller {
  10. ArrayList<FileDescriptor> fds = new ArrayList<FileDescriptor>();
  11. ArrayList<ZygoteConnection> peers = new ArrayList<ZygoteConnection>();
  12. FileDescriptor[] fdArray = new FileDescriptor[4];
  13. fds.add(sServerSocket.getFileDescriptor());//registerZygoteSocket中创建的socket的描述符
  14. peers.add(null);
  15. int loopCount = GC_LOOP_COUNT;
  16. while (true) {//死循环
  17. int index;//selectReadable方法监控的句柄的下标(fdArray中的下标)
  18. /*
  19. * Call gc() before we block in select().
  20. * It's work that has to be done anyway, and it's better
  21. * to avoid making every child do it.  It will also
  22. * madvise() any free memory as a side-effect.
  23. *
  24. * Don't call it every time, because walking the entire
  25. * heap is a lot of overhead to free a few hundred bytes.
  26. */
  27. if (loopCount <= 0) {//Zygote每循环GC_LOOP_COUNT(这里的值是10)次就会进行一次内存回收
  28. gc();
  29. loopCount = GC_LOOP_COUNT;
  30. } else {
  31. loopCount--;
  32. }
  33. try {
  34. fdArray = fds.toArray(fdArray);
  35. index = selectReadable(fdArray);//内部由select()实现,在没有客户端事件时会堵塞
  36. } catch (IOException ex) {
  37. throw new RuntimeException("Error in select()", ex);
  38. }
  39. if (index < 0) {
  40. throw new RuntimeException("Error in select()");
  41. } else if (index == 0) {//index==0表示selcet接收到的是Zygote的socket的事件
  42. ZygoteConnection newPeer = acceptCommandPeer();
  43. peers.add(newPeer);
  44. fds.add(newPeer.getFileDesciptor());
  45. } else {//调用ZygoteConnection对象的runOnce方法,ZygoteConnection是在index == 0时被添加到peers的
  46. boolean done;
  47. done = peers.get(index).runOnce();
  48. if (done) {
  49. peers.remove(index);
  50. fds.remove(index);
  51. }
  52. }
  53. }
  54. }

下面是selcetReadable方法的代码:

  1. /**
  2. * Invokes select() on the provider array of file descriptors (selecting
  3. * for readability only). Array elements of null are ignored.
  4. *
  5. * @param fds non-null; array of readable file descriptors
  6. * @return index of descriptor that is now readable or -1 for empty array.
  7. * @throws IOException if an error occurs
  8. */
  9. static native int selectReadable(FileDescriptor[] fds) throws IOException;

selectReadable的native实现在com_android_internal_os_ZygoteInit.cpp中。

Zygote接收到socket客户端的链接后会将其(客户端Socket)保存到一个ZygoteConnection对象中,然后保存到peers

  1. /**
  2. * Waits for and accepts a single command connection. Throws
  3. * RuntimeException on failure.
  4. */
  5. private static ZygoteConnection acceptCommandPeer() {
  6. try {
  7. return new ZygoteConnection(sServerSocket.accept());
  8. } catch (IOException ex) {
  9. throw new RuntimeException(
  10. "IOException during accept()", ex);
  11. }
  12. }

最后,客户端的请求会有ZygoteConnection的runOnce来处理。