Android ART initialization and startup

Source: Internet
Author: User

ART Initialization
Next we will go to jni_getdefajavjavavminitargs(), JNI_CreateJavaVM () and JNI_GetCreatedJavaVMs () functions to learn about the initialization process of ART. The Code of these three functions is located in jni_internal.cc.
Jni_getdefajavjavavminitargs() The function does not act in ART, but returns JNI_ERR. As follows:
Extern "C" jintjni_getdefajavjavavminitargs(Void *){
Return JNI_ERR;
}
The JNI_GetCreatedJavaVMs () function is used to return the JavaVMExt pointer saved in the Runtime. The function code is as follows:
Extern "C" jint JNI_GetCreatedJavaVMs (JavaVM ** vms, jsize, jsize * vm_count ){
Runtime * runtime = Runtime: Current ();
If (runtime = NULL ){
* Vm_count = 0;
} Else {
* Vm_count = 1;
Vms [0] = runtime-> GetJavaVM ();
}
Return JNI_ OK;
}
The GetJavaVM () function of Runtime only returns the member variable java_vm _ Of the Runtime class, as shown below.
JavaVMExt * GetJavaVM () const {
Return java_vm _;
}
Java_vm _ is a pointer of the JavaVMExt type and is defined as follows:
JavaVMExt * java_vm _;


After understanding two simple functions, let's analyze the JNI_CreateJavaVM () function. The Code is as follows:
Extern "C" jint JNI_CreateJavaVM (JavaVM ** p_vm, JNIEnv ** p_env, void * vm_args ){
Const JavaVMInitArgs * args = static_cast (vm_args );
// Check the JNI version
If (IsBadJniVersion (args-> version )){
LOG (ERROR) <"Bad JNI version passed to CreateJavaVM:" <args-> version;
Return JNI_EVERSION;
}
// Save the startup parameters to options.
Runtime: Optionsoptions;
For (int I = 0; I nOptions; ++ I ){
JavaVMOption * option = & args-> options [I];
Options. push_back (std: make_pair (std: string (option-> optionString), option-> extraInfo ));
}
Bool ignore_unrecognized = args-> ignoreUnrecognized;
If (! Runtime: Create (options, ignore_unrecognized )){// Create a VM
Return JNI_ERR;
}
Runtime * runtime = Runtime: Current ();
Bool started = runtime-> Start (); // Start the VM
If (! Started ){
Delete Thread: Current ()-> GetJniEnv ();
Delete runtime-> GetJavaVM ();
Return JNI_ERR; // startup failure, Method
}
* P_env = Thread: Current ()-> GetJniEnv ();
* P_vm = runtime-> GetJavaVM ();
Return JNI_ OK;
}
The JNI_CreateJavaVM () function calls the Create () function of Runtime to Create a virtual machine, and then calls the start () function to start it. In ART, the position of the Runtime object is similar to that of the DvmGlobals object gDVm in Dalvik, which contains all important variables.
Next we will continue to analyze the Create () function:
Bool Runtime: Create (const Options & options, bool ignore_unrecognized ){
If (Runtime: instance _! = NULL ){
Return false; // Only one Runtime instance can be created
}
InitLogging (NULL );// Calllocks: Init () as aside effect.
Instance _ = newRuntime; // Create a Runtime class instance
If (! Instance _-> Init (options, ignore_unrecognized )){// Initialize the Runtime object
Delete instance _;
Instance _ = NULL;
Return false;
}
Return true;
}
The Create () function of Runtime creates a Runtime object and calls its Init () function for initialization. The function code is as follows:
Bool Runtime: Init (const Options & raw_options, bool ignore_unrecognized ){
UniquePtroptions (ParsedOptions: Create (raw_options,
Ignore_unrecognized ));
......
QuasiAtomic: Startup ();
Monitor: Init (options-> lock_profiling_threshold _,
Options-> hook_is_sensitive_thread _);
Host_prefix _ = options-> host_prefix _;
Boot_class_path_string _ = options-> boot_class_path_string _;
... // More value assignment statements
Monitor_list _ = newMonitorList;
Thread_list _ = newThreadList;
Intern_table _ = newInternTable;
If (options-> interpreter_only _){
GetInstrumentation ()-> ForceInterpretOnly ();
}
// Create a Heap object
Heap _ = newgc: Heap (options-> heap_initial_size _,
Options-> heap_growth_limit _,
......);
BlockSignals ();
InitPlatformSignalHandleRs ();
// Create a JavaVMExt object
Java_vm _ = new JavaVMExt (this, options. get ());
// Convert the current main thread into a "Java" Thread
Thread: Startup ();
Thread * self = Thread: Attach ("main", false, NULL, false );
Self-> TransitionFromSuspendedTORunnable ();
GetHeap ()-> EnableObjectValidation ();
If (GetHeap ()-> GetContinuousSpaces () [0]-> IsImageSpace ()){
Class_linker _ = ClassLinker: CreateFromImage (intern_table _);
} Else {
Class_linker _ = ClassLinker: CreateFromCompiler (* options-> boot_class_path _,Intern_table _);
}
......
Return true;
}
The most important task of the Init () function is to create a Heap object and a ClassLinker object. We only need to compare ART and Dalvik to understand it, because in ART mode, it does not need to parse and execute bytecode, so it works much less than Dalivk. Even if the application has been compiled into executable code, it also needs to support the garbage collection function, so the Heap module is still indispensable. The Heap module of Art has almost the same functions as Dalvik. The garbage collection algorithm also marks and clears the code, but the implementation of the Code is even more obscure, therefore, this book does not intend to analyze the Heap algorithm of Art again.
Although ART does not need to load and execute bytecode, it still needs to retain information about all Java classes. Java programs and C ++ programs have different memory management methods, the biggest difference is that Java programs can dynamically obtain various types of information, including methods and variables. Therefore, these functions must also be provided in ART, otherwise the compiled program cannot be used. ClassLinker class is used to provide various Java class parsing functions within ART.

ART starts running
After initialization, call the start () function of Runtime to start running. The function code is as follows:
Bool Runtime: Start (){
VLOG (startup) <"Runtime: Start entering ";
Thread * self = Thread: Current ();
Self-> TransitionFromRunnableToSushortded (kNative );
Started _ = true;
InitNativeMethods () // Initialize the local JNI Method
InitThreadGroups (self );
Thread: FinishStartup ();
If (is_zygote _){
If (! InitZygote ()){
Return false;
}
} Else {
DidForkFromZygote ();
}
StartDaemonThreads (); // Call the start method of java. lang. Daemons
System_class_loader _ = CreateSystemClassLoader ();// Create a ClassLoader object
Self-> GetJniEnv ()-> locals. AssertEmpty ();
VLOG (startup) <"Runtime: Start exiting ";
Finished_starting _ = true;
Return true;
}
The Start () method calls InitNativeMethods () to initialize the local JNI method. The JNI function is also supported in ART, but the JNI function of some systems is overwritten in ART, however, the implementation principle is not much different from that in Dalvik, while the JNI functions of most other modules remain unchanged. After the InitNativeMethods () method is called, these JNI functions can be used.
Next, in the Zygote process, the InitZygote () function will be called for initialization. The implementation of this function is almost the same as that in Dalvik. The main task is to set the groupid of the process, and mountrootfs File System to the root directory. If it is not a Zygote process, call the DidForkFromZygote () function to call the CreateThreadPool () function of the Heap object to create a thread pool.
Finally, the Start () function calls the StartDaemonThreads () function. This function calls the start () method of Java Daemons to Start some Deamon threads, we have analyzed these Deamon statements. This process is actually the same as the last task completed at Dalvik startup.
Compared with the work to be done when the Dalivk starts, ART is quite simple.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.