ZygoteInit is the Java side startup class for Zygote Process.
/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
This pre-initializes some classes and waits for commands on the zygote socket
Based on these commands, forks off child processes that inherit the initial state of the VM.
When main process is called with start-system-server as arg:
2. preload();
Preload
-Classes
List maintained at /system/etc/preloaded-classes
Load and explicitly initialize the given class
Class.forName(line, true, null); //This would load and initialize the class
-Resources
Load in commonly used resources, so they can be shared across processes.
Resouces.startPreloading()
TypedArray ar = mResources.obtainTypedArray(
com.android.internal.R.array.preloaded_drawables);
int N = preloadDrawables(runtime, ar);
ar.recycle();
ar = mResources.obtainTypedArray(
com.android.internal.R.array.preloaded_color_state_lists);
N = preloadColorStateLists(runtime, ar);
ar.recycle();
mResources.finishPreloading();
-OpenGL
-SharedLibraries
System.loadLibrary("android");
System.loadLibrary("compiler_rt");
System.loadLibrary("jnigraphics");
-TextResources
Hypenator.init();
Load hyphenation patterns at initialization time. We want to have patterns for all locales loaded and ready to use so we don't have to do any file IO
on the UI thread when drawing text in different locales.
/system/usr/hyphen-data/
-PrepareWebViewInZygote()
Ask the WebViewFactory to do any initialization that must run in the zygote process, for memory sharing purposes.
3. startSystemServer(abiList, socketName);
- Zygote.forkSystemServer();
VM_HOOKS.preFork();
*VM_HOOKS - Provides hooks for the zygote to call back into the runtime to perform parent or child specific initialization.
/libcore/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
Daemons.stop();
Stop all daemons
*Daemons: A common base class to a set of daemons that are run on each Zygote forked process. They are stopped and started so that Zygote can fork out processes
without issues.
/libcore/libart/src/main/java/java/lang/Daemons.java
Current Daemons are:
ReferenceQueueDaemon: Heap management thread moves elements from the garbage collector's pending list to the managed reference queue.
FinalizerDaemon: Runs through the Finalizer objects that the last GC pushed onto the Finalizer reference queue.
FinalizerWatchdogDaemon: The watchdog exits the VM if the finalizer ever gets stuck. We consider the finalizer to be stuck if it spends more than 10 secs
on one instance.
HeapTaskDaemon: GC Normally runs on this thread.
waitUntilAllThreadsStopped();
token = nativePreFork();
Calls the runtime prefork,which in turn calls the heap->prefork function. This kind of prepares the Zygote space for the process. Need to check further
- int pid = nativeForkSystemServer()
ForkAndSpecializeCommon()
Utility function to fork and specialize a zygote process.
pid_t pid = fork();
Actual Fork Command (would return 0 for child process and child's pid for parent
SetSigChldHandler();
Sets the Signal Handler for the zygote mode. If just crashed process is system server this kills zygote and init would restart zygote again.
For non-systemserver processes we createprocess group here.
Set Properities and capabilities
UnsetSigChildHandler
env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags, is_system_server ? NULL : instructionSet);
Call post fork VM Hooks
This initializes the child's main thread etc, as values must have changed after fork
if (waitpid(pid, &status, WNOHANG) == pid) {
ALOGE("System server process %d has died. Restarting Zygote!", pid);
RuntimeAbort(env);
}
The parent process waits on the systemserver pid and fires a runtime abort whenever system server crashes
VM_HOOKS.postForkCommon();
Restart All Daemons for both parent ad child
- handleSystemServerProcess(parsedArgs);
Finish remianing works in the system server
- Set owner only permissions to files and directories
performSystemServerDexOpt();
Creates a new InstallerConnection and runs installer.dexopt() for all classes in SYSTEMSERVERCLASSPATH
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
commonInit();
set default values like timezone, http useragent logger reset eyc
nativeZygoteInit();
start the process thread pool (MAX 16 binder threads)
applicationInit(targetSdkVersion, argv, classLoader);
nativeSetExitWithoutCleanup(true);
If the application calls System.exit(), terminate the process immediately without running any shutdown hooks. It is not possible to shutdown an Android application gracefully. Among other things, the Android runtime shutdown hooks close the Binder driver, which can cause
leftover running threads to crash before the process actually exits.
VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);
VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);
invokeStaticMain(args.startClass, args.startArgs, classLoader);
Calls the main of systemserver
runSelectLoop(abiList);
Wait for more commands on Zygote Socket
/frameworks/base/core/java/com/android/internal/os/ZygoteInit.java
This pre-initializes some classes and waits for commands on the zygote socket
Based on these commands, forks off child processes that inherit the initial state of the VM.
When main process is called with start-system-server as arg:
Steps:
1, registerZygoteSocket(socketName);
Registers a server socket for zygote command connections2. preload();
Preload
-Classes
List maintained at /system/etc/preloaded-classes
Load and explicitly initialize the given class
Class.forName(line, true, null); //This would load and initialize the class
-Resources
Load in commonly used resources, so they can be shared across processes.
Resouces.startPreloading()
TypedArray ar = mResources.obtainTypedArray(
com.android.internal.R.array.preloaded_drawables);
int N = preloadDrawables(runtime, ar);
ar.recycle();
ar = mResources.obtainTypedArray(
com.android.internal.R.array.preloaded_color_state_lists);
N = preloadColorStateLists(runtime, ar);
ar.recycle();
mResources.finishPreloading();
-OpenGL
-SharedLibraries
System.loadLibrary("android");
System.loadLibrary("compiler_rt");
System.loadLibrary("jnigraphics");
-TextResources
Hypenator.init();
Load hyphenation patterns at initialization time. We want to have patterns for all locales loaded and ready to use so we don't have to do any file IO
on the UI thread when drawing text in different locales.
/system/usr/hyphen-data/
-PrepareWebViewInZygote()
Ask the WebViewFactory to do any initialization that must run in the zygote process, for memory sharing purposes.
3. startSystemServer(abiList, socketName);
- Zygote.forkSystemServer();
VM_HOOKS.preFork();
*VM_HOOKS - Provides hooks for the zygote to call back into the runtime to perform parent or child specific initialization.
/libcore/dalvik/src/main/java/dalvik/system/ZygoteHooks.java
Daemons.stop();
Stop all daemons
*Daemons: A common base class to a set of daemons that are run on each Zygote forked process. They are stopped and started so that Zygote can fork out processes
without issues.
/libcore/libart/src/main/java/java/lang/Daemons.java
Current Daemons are:
ReferenceQueueDaemon: Heap management thread moves elements from the garbage collector's pending list to the managed reference queue.
FinalizerDaemon: Runs through the Finalizer objects that the last GC pushed onto the Finalizer reference queue.
FinalizerWatchdogDaemon: The watchdog exits the VM if the finalizer ever gets stuck. We consider the finalizer to be stuck if it spends more than 10 secs
on one instance.
HeapTaskDaemon: GC Normally runs on this thread.
waitUntilAllThreadsStopped();
token = nativePreFork();
Calls the runtime prefork,which in turn calls the heap->prefork function. This kind of prepares the Zygote space for the process. Need to check further
- int pid = nativeForkSystemServer()
ForkAndSpecializeCommon()
Utility function to fork and specialize a zygote process.
pid_t pid = fork();
Actual Fork Command (would return 0 for child process and child's pid for parent
SetSigChldHandler();
Sets the Signal Handler for the zygote mode. If just crashed process is system server this kills zygote and init would restart zygote again.
For non-systemserver processes we createprocess group here.
Set Properities and capabilities
UnsetSigChildHandler
env->CallStaticVoidMethod(gZygoteClass, gCallPostForkChildHooks, debug_flags, is_system_server ? NULL : instructionSet);
Call post fork VM Hooks
This initializes the child's main thread etc, as values must have changed after fork
if (waitpid(pid, &status, WNOHANG) == pid) {
ALOGE("System server process %d has died. Restarting Zygote!", pid);
RuntimeAbort(env);
}
The parent process waits on the systemserver pid and fires a runtime abort whenever system server crashes
VM_HOOKS.postForkCommon();
Restart All Daemons for both parent ad child
- handleSystemServerProcess(parsedArgs);
Finish remianing works in the system server
- Set owner only permissions to files and directories
performSystemServerDexOpt();
Creates a new InstallerConnection and runs installer.dexopt() for all classes in SYSTEMSERVERCLASSPATH
RuntimeInit.zygoteInit(parsedArgs.targetSdkVersion, parsedArgs.remainingArgs, cl);
commonInit();
set default values like timezone, http useragent logger reset eyc
nativeZygoteInit();
start the process thread pool (MAX 16 binder threads)
applicationInit(targetSdkVersion, argv, classLoader);
nativeSetExitWithoutCleanup(true);
If the application calls System.exit(), terminate the process immediately without running any shutdown hooks. It is not possible to shutdown an Android application gracefully. Among other things, the Android runtime shutdown hooks close the Binder driver, which can cause
leftover running threads to crash before the process actually exits.
VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);
VMRuntime.getRuntime().setTargetSdkVersion(targetSdkVersion);
invokeStaticMain(args.startClass, args.startArgs, classLoader);
Calls the main of systemserver
runSelectLoop(abiList);
Wait for more commands on Zygote Socket
Comments
Post a Comment