Detailed Explanation of Android zygote Startup Process

  • 2021-12-11 19:02:18
  • OfStack

Directory's understanding of zygote Action Start the process Startup entry
The script explains the startup process
App_main::mainAndroidRuntime::start

Understanding of zygote

In the Android system, zygote is an native process, which is the parent process of all application processes. zygote is the first process in the user space of Linux system-init process, which is created and started by fork.

Action

When the zygote process starts, it will create an Dalvik virtual machine instance. Every time a new application process is hatched, this Dalvik virtual machine instance will be copied to the new application process, so that each application process has an independent Dalvik virtual machine instance.

The main functions of the zygote process are twofold:

Start SystemServer.
Incubate the application process.

Start the process

Startup entry

The Zygote process is started and created as an service (service) in the init process by parsing the init. zygote. rc configuration file.

Take init.zygote32.rc as an example:

Script explanation


//  system\core\rootdir\init.zygote32.rc
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
  class main
  priority -20
  user root
  group root readproc reserved_disk
  socket zygote stream 660 root system
  socket usap_pool_primary stream 660 root system
  onrestart write /sys/power/state on
  onrestart restart audioserver
  onrestart restart cameraserver
  onrestart restart media
  onrestart restart netd
  onrestart restart wificond
  writepid /dev/cpuset/foreground/tasks

This script requires the init process to create a process named zygote that executes the program "/system/bin/app_process". And create an socket resource for the zygote process (for inter-process communication, through which ActivityManagerService requests the zygote process, fork, an application process).

The following **--zygote ** is a parameter indicating that the zygote process is started. This parameter is used in the main function of app_process to decide whether to execute the ZygoteInit or Java class.

Startup process

The program to be executed by zygote is system/bin/app_process, and its source code is in frameworks/base/cmds/app_process/app_main. cpp

App_main::main

int main(int argc, char* const argv[])
{
  ...
  while (i < argc) {
    const char* arg = argv[i++];
    if (strcmp(arg, "--zygote") == 0) {// Whether there is --zygote Parameter. This is the start zygote Parameters at the time of the process 
      zygote = true;
			// Process name, set to zygote
      niceName = ZYGOTE_NICE_NAME;
    } else if (strcmp(arg, "--start-system-server") == 0) {// Whether there is --start-system-server
      startSystemServer = true;
	....
  if (zygote) {
		// The most important method. . . If it is zygote Process, start 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.");
  }
}

AndroidRuntime::start

void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
{
  ...
  JNIEnv* env;
	// Emphasis method     Create VM Virtual machine, the parameter is a pointer, which can be used to get the returned value, and can use the env Come and Java Layer to interact 
  if (startVm(&mJavaVM, &env, zygote) != 0) {
    return;
  }
  onVmCreated(env);
  // Emphasis method     Register a virtual machine 1 Some JNI Function, (system so Library, user-defined so Library   Load functions, etc.) 
  if (startReg(env) < 0) {
    ALOGE("Unable to register all android natives\n");
    return;
  }

  	// Find the main Method and call the. If it is zygote If so, it will start here ZygoteInit Class main Method 
    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 {
    	// Call main Method. Through here JNI Call Java Method, Zygote(Native Layer ) It entered Java The world, thus opening up Android Medium Java The world. 
      env->CallStaticVoidMethod(startClass, startMeth, strArray);
}

App_main.main
 AndroidRuntime.start
  startVm// Create a virtual machine 
  startReg// Registration JNI Function 
  ZygoteInit.main// This is where you enter Java Layered 
    registerZygoteSocket// Establish IPC Communication mechanism of 
    preload// Preload classes and resources 
    startSystemServer// Start system_server
    runSelectLoop// Waiting for a request created by a process 

Corresponding source address: /frameworks/base/cmds/app_process/App_main. cpp (including AppRuntime class)/frameworks/base/core/jni/AndroidRuntime. cpp/cpp/cpp/cpp/cpp/com/com/android/os/ZygoteInit. EN/internal/os/Zygote. java/frameworks/base/core/java/android/net/LocalServerSocket. java

During the startup of the Zygote process, in addition to creating an Dalvik virtual machine instance, the Java runtime library will be loaded into the process, and some JNI methods of Android core classes will be registered into the created Dalvik virtual machine instance.

When the zygote process initializes, it starts virtualization and loads some system resources. In this way, after zygote fork comes out of the sub-process, the sub-process will inherit the virtual machine and various system resources that can work normally, and the rest only need to load the bytecode of APK file to run the program.

The Java application cannot run as a local process and must run in a separate virtual machine. If you restart the virtual machine every time, it will definitely slow down the startup speed of the application.

Note: When the APK application process is hatched by the zygote process, it not only obtains a copy of the Dalvik virtual machine instance, but also shares the Java runtime library with Zygote1.

The above is Android zygote startup process details, more information about Android zygote startup process please pay attention to other related articles on this site!


Related articles: