< prev index next >

src/hotspot/share/runtime/threads.cpp

Print this page

   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"

  27 #include "cds/cds_globals.hpp"
  28 #include "cds/cdsConfig.hpp"


  29 #include "cds/metaspaceShared.hpp"

  30 #include "classfile/classLoader.hpp"
  31 #include "classfile/javaClasses.hpp"
  32 #include "classfile/javaThreadStatus.hpp"
  33 #include "classfile/systemDictionary.hpp"

  34 #include "classfile/vmClasses.hpp"
  35 #include "classfile/vmSymbols.hpp"

  36 #include "compiler/compileBroker.hpp"
  37 #include "compiler/compileTask.hpp"
  38 #include "compiler/compilerThread.hpp"
  39 #include "gc/shared/barrierSet.hpp"
  40 #include "gc/shared/barrierSetNMethod.hpp"
  41 #include "gc/shared/gcVMOperations.hpp"
  42 #include "gc/shared/oopStorage.hpp"
  43 #include "gc/shared/oopStorageSet.hpp"
  44 #include "gc/shared/stringdedup/stringDedup.hpp"

  45 #include "jfr/jfrEvents.hpp"
  46 #include "jvm.h"
  47 #include "jvmtifiles/jvmtiEnv.hpp"
  48 #include "logging/log.hpp"
  49 #include "logging/logAsyncWriter.hpp"
  50 #include "logging/logConfiguration.hpp"
  51 #include "memory/allocation.inline.hpp"
  52 #include "memory/iterator.hpp"
  53 #include "memory/oopFactory.hpp"
  54 #include "memory/resourceArea.hpp"
  55 #include "memory/universe.hpp"
  56 #include "nmt/memTracker.hpp"
  57 #include "oops/instanceKlass.hpp"
  58 #include "oops/klass.inline.hpp"
  59 #include "oops/oop.inline.hpp"

  60 #include "oops/symbol.hpp"
  61 #include "prims/jvm_misc.hpp"
  62 #include "prims/jvmtiAgentList.hpp"
  63 #include "runtime/arguments.hpp"
  64 #include "runtime/fieldDescriptor.inline.hpp"
  65 #include "runtime/flags/jvmFlagLimit.hpp"
  66 #include "runtime/globals.hpp"
  67 #include "runtime/handles.inline.hpp"
  68 #include "runtime/interfaceSupport.inline.hpp"
  69 #include "runtime/java.hpp"
  70 #include "runtime/javaCalls.hpp"
  71 #include "runtime/javaThread.inline.hpp"
  72 #include "runtime/jniHandles.inline.hpp"
  73 #include "runtime/jniPeriodicChecker.hpp"
  74 #include "runtime/lockStack.inline.hpp"
  75 #include "runtime/monitorDeflationThread.hpp"
  76 #include "runtime/mutexLocker.hpp"
  77 #include "runtime/nonJavaThread.hpp"
  78 #include "runtime/objectMonitor.inline.hpp"
  79 #include "runtime/osThread.hpp"

 302 //     enable the startup code to use lambda and other language features in this
 303 //     phase and onward.
 304 //
 305 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
 306 static void call_initPhase2(TRAPS) {
 307   TraceTime timer("Initialize module system", TRACETIME_LOG(Info, startuptime));
 308 
 309   Klass* klass = vmClasses::System_klass();
 310 
 311   JavaValue result(T_INT);
 312   JavaCallArguments args;
 313   args.push_int(DisplayVMOutputToStderr);
 314   args.push_int(log_is_enabled(Debug, init)); // print stack trace if exception thrown
 315   JavaCalls::call_static(&result, klass, vmSymbols::initPhase2_name(),
 316                                          vmSymbols::boolean_boolean_int_signature(), &args, CHECK);
 317   if (result.get_jint() != JNI_OK) {
 318     vm_exit_during_initialization(); // no message or exception
 319   }
 320 
 321   universe_post_module_init();











 322 }
 323 
 324 // Phase 3. final setup - set security manager, system class loader and TCCL
 325 //
 326 //     This will instantiate and set the security manager, set the system class
 327 //     loader as well as the thread context class loader.  The security manager
 328 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
 329 //     other modules or the application's classpath.
 330 static void call_initPhase3(TRAPS) {
 331   Klass* klass = vmClasses::System_klass();
 332   JavaValue result(T_VOID);
 333   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
 334                                          vmSymbols::void_method_signature(), CHECK);
 335 }
 336 
 337 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
 338   TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
 339 
 340   initialize_class(vmSymbols::java_lang_String(), CHECK);
 341 
 342   // Inject CompactStrings value after the static initializers for String ran.
 343   java_lang_String::set_compact_strings(CompactStrings);
 344 
 345   // Initialize java_lang.System (needed before creating the thread)
 346   initialize_class(vmSymbols::java_lang_System(), CHECK);
 347   // The VM creates & returns objects of this class. Make sure it's initialized.
 348   initialize_class(vmSymbols::java_lang_Class(), CHECK);
 349   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
 350   Handle thread_group = create_initial_thread_group(CHECK);
 351   Universe::set_main_thread_group(thread_group());
 352   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
 353   create_initial_thread(thread_group, main_thread, CHECK);
 354 


 355   // The VM creates objects of this class.
 356   initialize_class(vmSymbols::java_lang_Module(), CHECK);
 357 
 358 #ifdef ASSERT
 359   InstanceKlass *k = vmClasses::UnsafeConstants_klass();
 360   assert(k->is_not_initialized(), "UnsafeConstants should not already be initialized");
 361 #endif
 362 
 363   // initialize the hardware-specific constants needed by Unsafe
 364   initialize_class(vmSymbols::jdk_internal_misc_UnsafeConstants(), CHECK);
 365   jdk_internal_misc_UnsafeConstants::set_unsafe_constants();
 366 
 367   // The VM preresolves methods to these classes. Make sure that they get initialized
 368   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
 369   initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);
 370 
 371   // Phase 1 of the system initialization in the library, java.lang.System class initialization
 372   call_initPhase1(CHECK);
 373 
 374   // Get the Java runtime name, version, and vendor info after java.lang.System is initialized.

 383     JDK_Version::set_runtime_name(get_java_version_info(ik, vmSymbols::java_runtime_name_name()));
 384 
 385     JDK_Version::set_runtime_version(get_java_version_info(ik, vmSymbols::java_runtime_version_name()));
 386 
 387     JDK_Version::set_runtime_vendor_version(get_java_version_info(ik, vmSymbols::java_runtime_vendor_version_name()));
 388 
 389     JDK_Version::set_runtime_vendor_vm_bug_url(get_java_version_info(ik, vmSymbols::java_runtime_vendor_vm_bug_url_name()));
 390   }
 391 
 392   // an instance of OutOfMemory exception has been allocated earlier
 393   initialize_class(vmSymbols::java_lang_OutOfMemoryError(), CHECK);
 394   initialize_class(vmSymbols::java_lang_NullPointerException(), CHECK);
 395   initialize_class(vmSymbols::java_lang_ClassCastException(), CHECK);
 396   initialize_class(vmSymbols::java_lang_ArrayStoreException(), CHECK);
 397   initialize_class(vmSymbols::java_lang_ArithmeticException(), CHECK);
 398   initialize_class(vmSymbols::java_lang_StackOverflowError(), CHECK);
 399   initialize_class(vmSymbols::java_lang_IllegalMonitorStateException(), CHECK);
 400   initialize_class(vmSymbols::java_lang_IllegalArgumentException(), CHECK);
 401 }
 402 
































 403 void Threads::initialize_jsr292_core_classes(TRAPS) {
 404   TraceTime timer("Initialize java.lang.invoke classes", TRACETIME_LOG(Info, startuptime));
 405 
 406   initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK);
 407   initialize_class(vmSymbols::java_lang_invoke_ResolvedMethodName(), CHECK);
 408   initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK);
 409   initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK);




 410 }
 411 
 412 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
 413   extern void JDK_Version_init();
 414 
 415   // Preinitialize version info.
 416   VM_Version::early_initialize();
 417 
 418   // Check version
 419   if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
 420 
 421   // Initialize library-based TLS
 422   ThreadLocalStorage::init();
 423 
 424   // Initialize the output stream module
 425   ostream_init();
 426 
 427   // Process java launcher properties.
 428   Arguments::process_sun_java_launcher_properties(args);
 429 

 515     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtJVMCI);
 516     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
 517   } else {
 518     JavaThread::_jvmci_old_thread_counters = nullptr;
 519   }
 520 #endif // INCLUDE_JVMCI
 521 
 522   // Initialize OopStorage for threadObj
 523   JavaThread::_thread_oop_storage = OopStorageSet::create_strong("Thread OopStorage", mtThread);
 524 
 525   // Attach the main thread to this os thread
 526   JavaThread* main_thread = new JavaThread();
 527   main_thread->set_thread_state(_thread_in_vm);
 528   main_thread->initialize_thread_current();
 529   // must do this before set_active_handles
 530   main_thread->record_stack_base_and_size();
 531   main_thread->register_thread_stack_with_NMT();
 532   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
 533   MACOS_AARCH64_ONLY(main_thread->init_wx());
 534 


 535   if (!main_thread->set_as_starting_thread()) {
 536     vm_shutdown_during_initialization(
 537                                       "Failed necessary internal allocation. Out of swap space");
 538     main_thread->smr_delete();
 539     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
 540     return JNI_ENOMEM;
 541   }
 542 
 543   // Enable guard page *after* os::create_main_thread(), otherwise it would
 544   // crash Linux VM, see notes in os_linux.cpp.
 545   main_thread->stack_overflow_state()->create_stack_guard_pages();
 546 
 547   // Initialize Java-Level synchronization subsystem
 548   ObjectMonitor::Initialize();
 549   ObjectSynchronizer::initialize();
 550 



 551   // Initialize global modules
 552   jint status = init_globals();
 553   if (status != JNI_OK) {
 554     main_thread->smr_delete();
 555     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
 556     return status;
 557   }



 558 
 559   // Create WatcherThread as soon as we can since we need it in case
 560   // of hangs during error reporting.
 561   WatcherThread::start();
 562 
 563   // Add main_thread to threads list to finish barrier setup with
 564   // on_thread_attach.  Should be before starting to build Java objects in
 565   // init_globals2, which invokes barriers.
 566   {
 567     MutexLocker mu(Threads_lock);
 568     Threads::add(main_thread);
 569   }
 570 
 571   status = init_globals2();
 572   if (status != JNI_OK) {
 573     Threads::remove(main_thread, false);
 574     // It is possible that we managed to fully initialize Universe but have then
 575     // failed by throwing an exception. In that case our caller JNI_CreateJavaVM
 576     // will want to report it, so we can't delete the main thread.
 577     if (!main_thread->has_pending_exception()) {

 675     if (StartAttachListener || AttachListener::init_at_startup()) {
 676       AttachListener::init();
 677     }
 678   }
 679 
 680   // Launch -Xrun agents if EagerXrunInit is not set.
 681   if (!EagerXrunInit) {
 682     JvmtiAgentList::load_xrun_agents();
 683   }
 684 
 685   Arena::start_chunk_pool_cleaner_task();
 686 
 687   // Start the service thread
 688   // The service thread enqueues JVMTI deferred events and does various hashtable
 689   // and other cleanups.  Needs to start before the compilers start posting events.
 690   ServiceThread::initialize();
 691 
 692   // Start the monitor deflation thread:
 693   MonitorDeflationThread::initialize();
 694 
 695   // initialize compiler(s)
 696 #if defined(COMPILER1) || COMPILER2_OR_JVMCI
 697   bool init_compilation = true;
 698 #if INCLUDE_JVMCI
 699   bool force_JVMCI_initialization = false;
 700   if (EnableJVMCI) {
 701     // Initialize JVMCI eagerly when it is explicitly requested.
 702     // Or when JVMCILibDumpJNIConfig or JVMCIPrintProperties is enabled.
 703     force_JVMCI_initialization = EagerJVMCI || JVMCIPrintProperties || JVMCILibDumpJNIConfig;
 704     if (!force_JVMCI_initialization && UseJVMCICompiler && !UseJVMCINativeLibrary && (!UseInterpreter || !BackgroundCompilation)) {
 705       // Force initialization of jarjvmci otherwise requests for blocking
 706       // compilations will not actually block until jarjvmci is initialized.
 707       force_JVMCI_initialization = true;
 708     }
 709     if (JVMCIPrintProperties || JVMCILibDumpJNIConfig) {
 710       // Both JVMCILibDumpJNIConfig and JVMCIPrintProperties exit the VM
 711       // so compilation should be disabled. This prevents dumping or
 712       // printing from happening more than once.
 713       init_compilation = false;
 714     }
 715   }
 716 #endif
 717   if (init_compilation) {
 718     CompileBroker::compilation_init(CHECK_JNI_ERR);
 719   }
 720 #endif
 721 
 722   // Start string deduplication thread if requested.
 723   if (StringDedup::is_enabled()) {
 724     StringDedup::start();
 725   }
 726 
 727   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
 728   // It is done after compilers are initialized, because otherwise compilations of
 729   // signature polymorphic MH intrinsics can be missed
 730   // (see SystemDictionary::find_method_handle_intrinsic).
 731   initialize_jsr292_core_classes(CHECK_JNI_ERR);
 732 





 733   // This will initialize the module system.  Only java.base classes can be
 734   // loaded until phase 2 completes
 735   call_initPhase2(CHECK_JNI_ERR);
 736 
 737   JFR_ONLY(Jfr::on_create_vm_2();)
 738 
 739   // Always call even when there are not JVMTI environments yet, since environments
 740   // may be attached late and JVMTI must track phases of VM execution
 741   JvmtiExport::enter_start_phase();
 742 
 743   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
 744   JvmtiExport::post_vm_start();
 745 
 746   // Final system initialization including security manager and system class loader
 747   call_initPhase3(CHECK_JNI_ERR);
 748 
 749   // cache the system and platform class loaders
 750   SystemDictionary::compute_java_loaders(CHECK_JNI_ERR);
 751 
 752   if (Continuations::enabled()) {

 754     // special nmethods due to limited CodeCache size can be treated as a fatal error at
 755     // startup with the proper message that CodeCache size is too small.
 756     initialize_class(vmSymbols::jdk_internal_vm_Continuation(), CHECK_JNI_ERR);
 757   }
 758 
 759 #if INCLUDE_CDS
 760   // capture the module path info from the ModuleEntryTable
 761   ClassLoader::initialize_module_path(THREAD);
 762   if (HAS_PENDING_EXCEPTION) {
 763     java_lang_Throwable::print(PENDING_EXCEPTION, tty);
 764     vm_exit_during_initialization("ClassLoader::initialize_module_path() failed unexpectedly");
 765   }
 766 #endif
 767 
 768 #if INCLUDE_JVMCI
 769   if (force_JVMCI_initialization) {
 770     JVMCI::initialize_compiler(CHECK_JNI_ERR);
 771   }
 772 #endif
 773 





 774   if (NativeHeapTrimmer::enabled()) {
 775     NativeHeapTrimmer::initialize();
 776   }
 777 
 778   // Always call even when there are not JVMTI environments yet, since environments
 779   // may be attached late and JVMTI must track phases of VM execution
 780   JvmtiExport::enter_live_phase();
 781 
 782   // Make perfmemory accessible
 783   PerfMemory::set_accessible(true);
 784 
 785   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
 786   JvmtiExport::post_vm_initialized();
 787 
 788   JFR_ONLY(Jfr::on_create_vm_3();)
 789 
 790 #if INCLUDE_MANAGEMENT
 791   Management::initialize(THREAD);
 792 
 793   if (HAS_PENDING_EXCEPTION) {

 806 #endif
 807 
 808   call_postVMInitHook(THREAD);
 809   // The Java side of PostVMInitHook.run must deal with all
 810   // exceptions and provide means of diagnosis.
 811   if (HAS_PENDING_EXCEPTION) {
 812     CLEAR_PENDING_EXCEPTION;
 813   }
 814 
 815   // Let WatcherThread run all registered periodic tasks now.
 816   // NOTE:  All PeriodicTasks should be registered by now. If they
 817   //   aren't, late joiners might appear to start slowly (we might
 818   //   take a while to process their first tick).
 819   WatcherThread::run_all_tasks();
 820 
 821   create_vm_timer.end();
 822 #ifdef ASSERT
 823   _vm_complete = true;
 824 #endif
 825 
 826   if (CDSConfig::is_dumping_static_archive()) {






 827     MetaspaceShared::preload_and_dump();



















 828   }
 829 
 830   return JNI_OK;
 831 }
 832 
 833 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
 834 // the program falls off the end of main(). Another VM exit path is through
 835 // vm_exit() when the program calls System.exit() to return a value or when
 836 // there is a serious error in VM. The two shutdown paths are not exactly
 837 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
 838 // and VM_Exit op at VM level.
 839 //
 840 // Shutdown sequence:
 841 //   + Shutdown native memory tracking if it is on
 842 //   + Wait until we are the last non-daemon thread to execute
 843 //     <-- every thing is still working at this moment -->
 844 //   + Call java.lang.Shutdown.shutdown(), which will invoke Java level
 845 //        shutdown hooks
 846 //   + Call before_exit(), prepare for VM exit
 847 //      > run VM level shutdown hooks (they are registered through JVM_OnExit(),

   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "c1/c1_Runtime1.hpp"
  28 #include "cds/cds_globals.hpp"
  29 #include "cds/cdsConfig.hpp"
  30 #include "cds/classPrelinker.hpp"
  31 #include "cds/heapShared.hpp"
  32 #include "cds/metaspaceShared.hpp"
  33 #include "cds/methodProfiler.hpp"
  34 #include "classfile/classLoader.hpp"
  35 #include "classfile/javaClasses.hpp"
  36 #include "classfile/javaThreadStatus.hpp"
  37 #include "classfile/systemDictionary.hpp"
  38 #include "classfile/systemDictionaryShared.hpp"
  39 #include "classfile/vmClasses.hpp"
  40 #include "classfile/vmSymbols.hpp"
  41 #include "code/SCCache.hpp"
  42 #include "compiler/compileBroker.hpp"
  43 #include "compiler/compileTask.hpp"
  44 #include "compiler/compilerThread.hpp"
  45 #include "gc/shared/barrierSet.hpp"
  46 #include "gc/shared/barrierSetNMethod.hpp"
  47 #include "gc/shared/gcVMOperations.hpp"
  48 #include "gc/shared/oopStorage.hpp"
  49 #include "gc/shared/oopStorageSet.hpp"
  50 #include "gc/shared/stringdedup/stringDedup.hpp"
  51 #include "interpreter/interpreterRuntime.hpp"
  52 #include "jfr/jfrEvents.hpp"
  53 #include "jvm.h"
  54 #include "jvmtifiles/jvmtiEnv.hpp"
  55 #include "logging/log.hpp"
  56 #include "logging/logAsyncWriter.hpp"
  57 #include "logging/logConfiguration.hpp"
  58 #include "memory/allocation.inline.hpp"
  59 #include "memory/iterator.hpp"
  60 #include "memory/oopFactory.hpp"
  61 #include "memory/resourceArea.hpp"
  62 #include "memory/universe.hpp"
  63 #include "nmt/memTracker.hpp"
  64 #include "oops/instanceKlass.hpp"
  65 #include "oops/klass.inline.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "opto/runtime.hpp"
  68 #include "oops/symbol.hpp"
  69 #include "prims/jvm_misc.hpp"
  70 #include "prims/jvmtiAgentList.hpp"
  71 #include "runtime/arguments.hpp"
  72 #include "runtime/fieldDescriptor.inline.hpp"
  73 #include "runtime/flags/jvmFlagLimit.hpp"
  74 #include "runtime/globals.hpp"
  75 #include "runtime/handles.inline.hpp"
  76 #include "runtime/interfaceSupport.inline.hpp"
  77 #include "runtime/java.hpp"
  78 #include "runtime/javaCalls.hpp"
  79 #include "runtime/javaThread.inline.hpp"
  80 #include "runtime/jniHandles.inline.hpp"
  81 #include "runtime/jniPeriodicChecker.hpp"
  82 #include "runtime/lockStack.inline.hpp"
  83 #include "runtime/monitorDeflationThread.hpp"
  84 #include "runtime/mutexLocker.hpp"
  85 #include "runtime/nonJavaThread.hpp"
  86 #include "runtime/objectMonitor.inline.hpp"
  87 #include "runtime/osThread.hpp"

 310 //     enable the startup code to use lambda and other language features in this
 311 //     phase and onward.
 312 //
 313 //     After phase 2, The VM will begin search classes from -Xbootclasspath/a.
 314 static void call_initPhase2(TRAPS) {
 315   TraceTime timer("Initialize module system", TRACETIME_LOG(Info, startuptime));
 316 
 317   Klass* klass = vmClasses::System_klass();
 318 
 319   JavaValue result(T_INT);
 320   JavaCallArguments args;
 321   args.push_int(DisplayVMOutputToStderr);
 322   args.push_int(log_is_enabled(Debug, init)); // print stack trace if exception thrown
 323   JavaCalls::call_static(&result, klass, vmSymbols::initPhase2_name(),
 324                                          vmSymbols::boolean_boolean_int_signature(), &args, CHECK);
 325   if (result.get_jint() != JNI_OK) {
 326     vm_exit_during_initialization(); // no message or exception
 327   }
 328 
 329   universe_post_module_init();
 330 
 331   // Preload all boot classes outside of java.base module
 332   ClassPrelinker::runtime_preload(THREAD, Handle());
 333   if (CDSConfig::is_loading_full_module_graph()) {
 334     // SystemDictionary::java_{platform,system}_loader are already assigned. We can spin
 335     // this up a little quicker.
 336     assert(SystemDictionary::java_platform_loader() != nullptr, "must be");
 337     assert(SystemDictionary::java_system_loader() != nullptr,   "must be");
 338     ClassPrelinker::runtime_preload(THREAD, Handle(THREAD, SystemDictionary::java_platform_loader()));
 339     ClassPrelinker::runtime_preload(THREAD, Handle(THREAD, SystemDictionary::java_system_loader()));
 340   }
 341 }
 342 
 343 // Phase 3. final setup - set security manager, system class loader and TCCL
 344 //
 345 //     This will instantiate and set the security manager, set the system class
 346 //     loader as well as the thread context class loader.  The security manager
 347 //     and system class loader may be a custom class loaded from -Xbootclasspath/a,
 348 //     other modules or the application's classpath.
 349 static void call_initPhase3(TRAPS) {
 350   Klass* klass = vmClasses::System_klass();
 351   JavaValue result(T_VOID);
 352   JavaCalls::call_static(&result, klass, vmSymbols::initPhase3_name(),
 353                                          vmSymbols::void_method_signature(), CHECK);
 354 }
 355 
 356 void Threads::initialize_java_lang_classes(JavaThread* main_thread, TRAPS) {
 357   TraceTime timer("Initialize java.lang classes", TRACETIME_LOG(Info, startuptime));
 358 
 359   initialize_class(vmSymbols::java_lang_String(), CHECK);
 360 
 361   // Inject CompactStrings value after the static initializers for String ran.
 362   java_lang_String::set_compact_strings(CompactStrings);
 363 
 364   // Initialize java_lang.System (needed before creating the thread)
 365   initialize_class(vmSymbols::java_lang_System(), CHECK);
 366   // The VM creates & returns objects of this class. Make sure it's initialized.
 367   initialize_class(vmSymbols::java_lang_Class(), CHECK);
 368   initialize_class(vmSymbols::java_lang_ThreadGroup(), CHECK);
 369   Handle thread_group = create_initial_thread_group(CHECK);
 370   Universe::set_main_thread_group(thread_group());
 371   initialize_class(vmSymbols::java_lang_Thread(), CHECK);
 372   create_initial_thread(thread_group, main_thread, CHECK);
 373 
 374   HeapShared::init_box_classes(CHECK);
 375 
 376   // The VM creates objects of this class.
 377   initialize_class(vmSymbols::java_lang_Module(), CHECK);
 378 
 379 #ifdef ASSERT
 380   InstanceKlass *k = vmClasses::UnsafeConstants_klass();
 381   assert(k->is_not_initialized(), "UnsafeConstants should not already be initialized");
 382 #endif
 383 
 384   // initialize the hardware-specific constants needed by Unsafe
 385   initialize_class(vmSymbols::jdk_internal_misc_UnsafeConstants(), CHECK);
 386   jdk_internal_misc_UnsafeConstants::set_unsafe_constants();
 387 
 388   // The VM preresolves methods to these classes. Make sure that they get initialized
 389   initialize_class(vmSymbols::java_lang_reflect_Method(), CHECK);
 390   initialize_class(vmSymbols::java_lang_ref_Finalizer(), CHECK);
 391 
 392   // Phase 1 of the system initialization in the library, java.lang.System class initialization
 393   call_initPhase1(CHECK);
 394 
 395   // Get the Java runtime name, version, and vendor info after java.lang.System is initialized.

 404     JDK_Version::set_runtime_name(get_java_version_info(ik, vmSymbols::java_runtime_name_name()));
 405 
 406     JDK_Version::set_runtime_version(get_java_version_info(ik, vmSymbols::java_runtime_version_name()));
 407 
 408     JDK_Version::set_runtime_vendor_version(get_java_version_info(ik, vmSymbols::java_runtime_vendor_version_name()));
 409 
 410     JDK_Version::set_runtime_vendor_vm_bug_url(get_java_version_info(ik, vmSymbols::java_runtime_vendor_vm_bug_url_name()));
 411   }
 412 
 413   // an instance of OutOfMemory exception has been allocated earlier
 414   initialize_class(vmSymbols::java_lang_OutOfMemoryError(), CHECK);
 415   initialize_class(vmSymbols::java_lang_NullPointerException(), CHECK);
 416   initialize_class(vmSymbols::java_lang_ClassCastException(), CHECK);
 417   initialize_class(vmSymbols::java_lang_ArrayStoreException(), CHECK);
 418   initialize_class(vmSymbols::java_lang_ArithmeticException(), CHECK);
 419   initialize_class(vmSymbols::java_lang_StackOverflowError(), CHECK);
 420   initialize_class(vmSymbols::java_lang_IllegalMonitorStateException(), CHECK);
 421   initialize_class(vmSymbols::java_lang_IllegalArgumentException(), CHECK);
 422 }
 423 
 424 bool Threads::initialize_compilation(TRAPS) {
 425   // initialize compiler(s)
 426   bool force_JVMCI_initialization = false;
 427 
 428 #if defined(COMPILER1) || COMPILER2_OR_JVMCI
 429   bool init_compilation = true;
 430 #if INCLUDE_JVMCI
 431   if (EnableJVMCI) {
 432     // Initialize JVMCI eagerly when it is explicitly requested.
 433     // Or when JVMCILibDumpJNIConfig or JVMCIPrintProperties is enabled.
 434     force_JVMCI_initialization = EagerJVMCI || JVMCIPrintProperties || JVMCILibDumpJNIConfig;
 435     if (!force_JVMCI_initialization && UseJVMCICompiler && !UseJVMCINativeLibrary && (!UseInterpreter || !BackgroundCompilation)) {
 436       // Force initialization of jarjvmci otherwise requests for blocking
 437       // compilations will not actually block until jarjvmci is initialized.
 438       force_JVMCI_initialization = true;
 439     }
 440     if (JVMCIPrintProperties || JVMCILibDumpJNIConfig) {
 441       // Both JVMCILibDumpJNIConfig and JVMCIPrintProperties exit the VM
 442       // so compilation should be disabled. This prevents dumping or
 443       // printing from happening more than once.
 444       init_compilation = false;
 445     }
 446   }
 447 #endif
 448   if (init_compilation) {
 449     CompileBroker::compilation_init(CHECK_false);
 450   }
 451 #endif
 452 
 453   return force_JVMCI_initialization;
 454 }
 455 
 456 void Threads::initialize_jsr292_core_classes(TRAPS) {
 457   TraceTime timer("Initialize java.lang.invoke classes", TRACETIME_LOG(Info, startuptime));
 458 
 459   initialize_class(vmSymbols::java_lang_invoke_MethodHandle(), CHECK);
 460   initialize_class(vmSymbols::java_lang_invoke_ResolvedMethodName(), CHECK);
 461   initialize_class(vmSymbols::java_lang_invoke_MemberName(), CHECK);
 462   initialize_class(vmSymbols::java_lang_invoke_MethodHandleNatives(), CHECK);
 463 
 464   if (UseSharedSpaces) {
 465     HeapShared::initialize_java_lang_invoke(CHECK);
 466   }
 467 }
 468 
 469 jint Threads::create_vm(JavaVMInitArgs* args, bool* canTryAgain) {
 470   extern void JDK_Version_init();
 471 
 472   // Preinitialize version info.
 473   VM_Version::early_initialize();
 474 
 475   // Check version
 476   if (!is_supported_jni_version(args->version)) return JNI_EVERSION;
 477 
 478   // Initialize library-based TLS
 479   ThreadLocalStorage::init();
 480 
 481   // Initialize the output stream module
 482   ostream_init();
 483 
 484   // Process java launcher properties.
 485   Arguments::process_sun_java_launcher_properties(args);
 486 

 572     JavaThread::_jvmci_old_thread_counters = NEW_C_HEAP_ARRAY(jlong, JVMCICounterSize, mtJVMCI);
 573     memset(JavaThread::_jvmci_old_thread_counters, 0, sizeof(jlong) * JVMCICounterSize);
 574   } else {
 575     JavaThread::_jvmci_old_thread_counters = nullptr;
 576   }
 577 #endif // INCLUDE_JVMCI
 578 
 579   // Initialize OopStorage for threadObj
 580   JavaThread::_thread_oop_storage = OopStorageSet::create_strong("Thread OopStorage", mtThread);
 581 
 582   // Attach the main thread to this os thread
 583   JavaThread* main_thread = new JavaThread();
 584   main_thread->set_thread_state(_thread_in_vm);
 585   main_thread->initialize_thread_current();
 586   // must do this before set_active_handles
 587   main_thread->record_stack_base_and_size();
 588   main_thread->register_thread_stack_with_NMT();
 589   main_thread->set_active_handles(JNIHandleBlock::allocate_block());
 590   MACOS_AARCH64_ONLY(main_thread->init_wx());
 591 
 592   MutexLockerImpl::init_counters(); // depends on mutex_init(), perfMemory_init(), and Thread::initialize_thread_current().
 593 
 594   if (!main_thread->set_as_starting_thread()) {
 595     vm_shutdown_during_initialization(
 596                                       "Failed necessary internal allocation. Out of swap space");
 597     main_thread->smr_delete();
 598     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
 599     return JNI_ENOMEM;
 600   }
 601 
 602   // Enable guard page *after* os::create_main_thread(), otherwise it would
 603   // crash Linux VM, see notes in os_linux.cpp.
 604   main_thread->stack_overflow_state()->create_stack_guard_pages();
 605 
 606   // Initialize Java-Level synchronization subsystem
 607   ObjectMonitor::Initialize();
 608   ObjectSynchronizer::initialize();
 609 
 610   Deoptimization::init_counters();
 611   VMThread::init_counters();
 612 
 613   // Initialize global modules
 614   jint status = init_globals();
 615   if (status != JNI_OK) {
 616     main_thread->smr_delete();
 617     *canTryAgain = false; // don't let caller call JNI_CreateJavaVM again
 618     return status;
 619   }
 620   if (xtty != nullptr)
 621     xtty->elem("vm_main_thread thread='" UINTX_FORMAT "'",
 622                (uintx) main_thread->osthread()->thread_id());
 623 
 624   // Create WatcherThread as soon as we can since we need it in case
 625   // of hangs during error reporting.
 626   WatcherThread::start();
 627 
 628   // Add main_thread to threads list to finish barrier setup with
 629   // on_thread_attach.  Should be before starting to build Java objects in
 630   // init_globals2, which invokes barriers.
 631   {
 632     MutexLocker mu(Threads_lock);
 633     Threads::add(main_thread);
 634   }
 635 
 636   status = init_globals2();
 637   if (status != JNI_OK) {
 638     Threads::remove(main_thread, false);
 639     // It is possible that we managed to fully initialize Universe but have then
 640     // failed by throwing an exception. In that case our caller JNI_CreateJavaVM
 641     // will want to report it, so we can't delete the main thread.
 642     if (!main_thread->has_pending_exception()) {

 740     if (StartAttachListener || AttachListener::init_at_startup()) {
 741       AttachListener::init();
 742     }
 743   }
 744 
 745   // Launch -Xrun agents if EagerXrunInit is not set.
 746   if (!EagerXrunInit) {
 747     JvmtiAgentList::load_xrun_agents();
 748   }
 749 
 750   Arena::start_chunk_pool_cleaner_task();
 751 
 752   // Start the service thread
 753   // The service thread enqueues JVMTI deferred events and does various hashtable
 754   // and other cleanups.  Needs to start before the compilers start posting events.
 755   ServiceThread::initialize();
 756 
 757   // Start the monitor deflation thread:
 758   MonitorDeflationThread::initialize();
 759 
 760   // Start the method sampler
 761   MethodProfiler::initialize();
 762 

 763   bool force_JVMCI_initialization = false;
 764   if (!CDSConfig::has_preloaded_classes()) {
 765     force_JVMCI_initialization = initialize_compilation(CHECK_JNI_ERR);













 766   }
 767 
 768   ClassPrelinker::init_javabase_preloaded_classes(CHECK_JNI_ERR);



 769 
 770   // Start string deduplication thread if requested.
 771   if (StringDedup::is_enabled()) {
 772     StringDedup::start();
 773   }
 774 
 775   // Pre-initialize some JSR292 core classes to avoid deadlock during class loading.
 776   // It is done after compilers are initialized, because otherwise compilations of
 777   // signature polymorphic MH intrinsics can be missed
 778   // (see SystemDictionary::find_method_handle_intrinsic).
 779   initialize_jsr292_core_classes(CHECK_JNI_ERR);
 780 
 781   if (CDSConfig::has_preloaded_classes()) {
 782     force_JVMCI_initialization = initialize_compilation(CHECK_JNI_ERR);
 783     ClassPrelinker::replay_training_at_init_for_javabase_preloaded_classes(CHECK_JNI_ERR);
 784   }
 785 
 786   // This will initialize the module system.  Only java.base classes can be
 787   // loaded until phase 2 completes
 788   call_initPhase2(CHECK_JNI_ERR);
 789 
 790   JFR_ONLY(Jfr::on_create_vm_2();)
 791 
 792   // Always call even when there are not JVMTI environments yet, since environments
 793   // may be attached late and JVMTI must track phases of VM execution
 794   JvmtiExport::enter_start_phase();
 795 
 796   // Notify JVMTI agents that VM has started (JNI is up) - nop if no agents.
 797   JvmtiExport::post_vm_start();
 798 
 799   // Final system initialization including security manager and system class loader
 800   call_initPhase3(CHECK_JNI_ERR);
 801 
 802   // cache the system and platform class loaders
 803   SystemDictionary::compute_java_loaders(CHECK_JNI_ERR);
 804 
 805   if (Continuations::enabled()) {

 807     // special nmethods due to limited CodeCache size can be treated as a fatal error at
 808     // startup with the proper message that CodeCache size is too small.
 809     initialize_class(vmSymbols::jdk_internal_vm_Continuation(), CHECK_JNI_ERR);
 810   }
 811 
 812 #if INCLUDE_CDS
 813   // capture the module path info from the ModuleEntryTable
 814   ClassLoader::initialize_module_path(THREAD);
 815   if (HAS_PENDING_EXCEPTION) {
 816     java_lang_Throwable::print(PENDING_EXCEPTION, tty);
 817     vm_exit_during_initialization("ClassLoader::initialize_module_path() failed unexpectedly");
 818   }
 819 #endif
 820 
 821 #if INCLUDE_JVMCI
 822   if (force_JVMCI_initialization) {
 823     JVMCI::initialize_compiler(CHECK_JNI_ERR);
 824   }
 825 #endif
 826 
 827 #if defined(COMPILER2)
 828   // Pre-load cached compiled methods
 829   SCCache::preload_code(THREAD);
 830 #endif
 831 
 832   if (NativeHeapTrimmer::enabled()) {
 833     NativeHeapTrimmer::initialize();
 834   }
 835 
 836   // Always call even when there are not JVMTI environments yet, since environments
 837   // may be attached late and JVMTI must track phases of VM execution
 838   JvmtiExport::enter_live_phase();
 839 
 840   // Make perfmemory accessible
 841   PerfMemory::set_accessible(true);
 842 
 843   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
 844   JvmtiExport::post_vm_initialized();
 845 
 846   JFR_ONLY(Jfr::on_create_vm_3();)
 847 
 848 #if INCLUDE_MANAGEMENT
 849   Management::initialize(THREAD);
 850 
 851   if (HAS_PENDING_EXCEPTION) {

 864 #endif
 865 
 866   call_postVMInitHook(THREAD);
 867   // The Java side of PostVMInitHook.run must deal with all
 868   // exceptions and provide means of diagnosis.
 869   if (HAS_PENDING_EXCEPTION) {
 870     CLEAR_PENDING_EXCEPTION;
 871   }
 872 
 873   // Let WatcherThread run all registered periodic tasks now.
 874   // NOTE:  All PeriodicTasks should be registered by now. If they
 875   //   aren't, late joiners might appear to start slowly (we might
 876   //   take a while to process their first tick).
 877   WatcherThread::run_all_tasks();
 878 
 879   create_vm_timer.end();
 880 #ifdef ASSERT
 881   _vm_complete = true;
 882 #endif
 883 
 884   if (CDSConfig::is_dumping_classic_static_archive()) {
 885     // Classic -Xshare:dump, aka "old workflow"
 886     MetaspaceShared::preload_and_dump();
 887   } else if (CDSConfig::is_dumping_final_static_archive()) {
 888     // TODO: copy the verification and loader constraints from preimage to final image
 889     // TODO: load archived classes for custom loaders as well.
 890     log_info(cds)("Dumping final image of CacheDataStore %s", CacheDataStore);
 891     MetaspaceShared::preload_and_dump();
 892     vm_direct_exit(0, "CacheDataStore dumping is complete");
 893   }
 894 
 895   log_info(init)("Before main:");
 896   log_vm_init_stats();
 897 
 898   if (UsePerfData) {
 899     if (ProfileVMLocks) {
 900       main_thread->set_profile_vm_locks(true);
 901     }
 902     if (ProfileVMCalls) {
 903       main_thread->set_profile_vm_calls(true);
 904     }
 905     if (ProfileRuntimeCalls) {
 906       main_thread->set_profile_rt_calls(true);
 907     }
 908     if (ProfileVMOps) {
 909       main_thread->set_profile_vm_ops(true);
 910     }
 911   }
 912 
 913   return JNI_OK;
 914 }
 915 
 916 // Threads::destroy_vm() is normally called from jni_DestroyJavaVM() when
 917 // the program falls off the end of main(). Another VM exit path is through
 918 // vm_exit() when the program calls System.exit() to return a value or when
 919 // there is a serious error in VM. The two shutdown paths are not exactly
 920 // the same, but they share Shutdown.shutdown() at Java level and before_exit()
 921 // and VM_Exit op at VM level.
 922 //
 923 // Shutdown sequence:
 924 //   + Shutdown native memory tracking if it is on
 925 //   + Wait until we are the last non-daemon thread to execute
 926 //     <-- every thing is still working at this moment -->
 927 //   + Call java.lang.Shutdown.shutdown(), which will invoke Java level
 928 //        shutdown hooks
 929 //   + Call before_exit(), prepare for VM exit
 930 //      > run VM level shutdown hooks (they are registered through JVM_OnExit(),
< prev index next >