Initialization and startup of Android ART

Source: Internet
Author: User

the initialization of art
Below we from Jni_getdefaultjavavminitargs(), JNI_CREATEJAVAVM () and Jni_getcreatedjavavms () three functions to understand the initialization of art. The code for these three functions is in jni_internal.cc.
Jni_getdefaultjavavminitargs() function does not act in art, just returns JNI_ERR. As shown below:
extern "C" Jintjni_getdefaultjavavminitargs(void*) {
return jni_err;
}
The Jni_getcreatedjavavms () function returns a pointer to the Javavmext saved in runtime, as shown in the following function code:
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 runtime's GETJAVAVM () function simply returns the member variable java_vm_ of the runtime class, as shown below.
javavmext* GETJAVAVM () const {
return java_vm_;
}
Java_vm_ is a pointer to the Javavmext type, defined as follows:
javavmext* java_vm_;


After understanding the two simple functions, let's analyze the JNI_CREATEJAVAVM () function with the following code:
extern "C" Jint jni_createjavavm (javavm** p_vm,jnienv** p_env, void* Vm_args) {
Const javavminitargs* args =static_cast (Vm_args);
Check the version of JNI
if (Isbadjniversion (args->version)) {
LOG (ERROR) << "Bad JNI version passed to CREATEJAVAVM:" << args->version;
return jni_eversion;
}
Save the startup parameters in the options.
Runtime::optionsoptions;
for (int i = 0; I <args->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 virtual machine
return jni_err;
}
runtime* runtime =runtime::current ();
BOOL started =runtime->start ();Start a virtual machine
if (!started) {
Delete Thread::current ()->getjnienv ();
Delete Runtime->getjavavm ();
return jni_err; Startup failure, method
}
*p_env =thread::current ()->getjnienv ();
*P_VM =RUNTIME-&GT;GETJAVAVM ();
return JNI_OK;
}
Call the runtime's Create () function in the JNI_CREATEJAVAVM () function to make the virtual machine, and then call the start () function to start it. In art, the runtime object's position is similar to the Dvmglobals object GDVM in Dalvik, and contains all the important variables.
Below we 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);Calls Locks::init () as aside effect.
Instance_ = Newruntime;Created an instance of the runtime class
if (!instance_->init (options, ignore_unrecognized)) {Initializing runtime objects
Delete Instance_;
Instance_ = NULL;
return false;
}
return true;
}
The runtime object is created in the runtime's Create () function, and its init () function is called to initialize. 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-&GT;HOOK_IS_SENSITIVE_THREAD_);
Host_prefix_ =options->host_prefix_;
Boot_class_path_string_ =options->boot_class_path_string_;
...///More Assignment statements
Monitor_list_ = newmonitorlist;
Thread_list_ = newthreadlist;
Intern_table_ = newinterntable;
if (options->interpreter_only_) {
Getinstrumentation ()->forceinterpretonly ();
}
Create 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 ());
Turn 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 work of the Init () function is to create the heap object and the Classlinker object. We can understand that by comparing art with Dalvik, because the art mode does not need to parse and execute bytecode, so it works much less than DALIVK. The heap module is essential even if the application has been compiled into executable code, but it also supports garbage collection. The function of the heap module of art is almost the same as in Dalvik, and the garbage collection algorithm is also a sign and clear method, but the implementation of the code is more obscure, so the book does not intend to analyze the heap algorithm of art again.
Although art does not have to load and execute bytecode, but retains all the Java class information, Java programs and C + + programs, in addition to the memory management method, the biggest difference is that Java programs can dynamically obtain information of various classes, including methods, variables, etc. So the art also provides these functions, otherwise the compiled program will not be used. The purpose of the Classlinker class is to provide parsing capabilities for various Java classes within art.

Art starts running
After the initialization is complete, the next step is to call the runtime's start () function and the function code is as follows:
BOOL Runtime::start () {
VLOG (startup) << "Runtime::start entering";
thread* self =thread::current ();
Self->transitionfromrunnabletoSuspended (knative);
Started_ = true;
Initnativemethods ()Initializing 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, and the JNI function is also supported in art, although the JNI functions of some systems are rewritten in art, but the implementation principle is not much different from the Dalvik, The JNI function of most other modules remains the same. These JNI functions can be used after the Initnativemethods () method is called.
Next, if it is in the zygote process, it will call the Initzygote () function to initialize, the implementation of this function is almost the same as in Dalvik, the main task is to set the groupid of the process, as well as the Mountrootfs file system to the root directory. If it is not a zygote process, call the Didforkfromzygote () function, which works primarily by calling the Createthreadpool () function of the heap object to create a thread pool.
The Startdaemonthreads () function is called in the last start () function, and the function is to invoke the start () method of the Java class daemons to start some deamon threads that we have analyzed before. This process is actually the same as the last work done at Dalvik startup.
Compared to the work that needs 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.