src/share/vm/prims/jni.cpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>


  57 #include "prims/jniFastGetField.hpp"
  58 #include "prims/jvm.h"
  59 #include "prims/jvm_misc.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "runtime/compilationPolicy.hpp"
  63 #include "runtime/fieldDescriptor.hpp"
  64 #include "runtime/fprofiler.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/interfaceSupport.hpp"
  67 #include "runtime/java.hpp"
  68 #include "runtime/javaCalls.hpp"
  69 #include "runtime/jfieldIDWorkaround.hpp"
  70 #include "runtime/reflection.hpp"
  71 #include "runtime/sharedRuntime.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/thread.inline.hpp"
  74 #include "runtime/vm_operations.hpp"
  75 #include "services/runtimeService.hpp"
  76 #include "trace/tracing.hpp"
  77 #include "trace/traceEventTypes.hpp"
  78 #include "utilities/defaultStream.hpp"
  79 #include "utilities/dtrace.hpp"
  80 #include "utilities/events.hpp"
  81 #include "utilities/histogram.hpp"
  82 #ifdef TARGET_OS_FAMILY_linux
  83 # include "os_linux.inline.hpp"
  84 #endif
  85 #ifdef TARGET_OS_FAMILY_solaris
  86 # include "os_solaris.inline.hpp"
  87 #endif
  88 #ifdef TARGET_OS_FAMILY_windows
  89 # include "os_windows.inline.hpp"
  90 #endif
  91 #ifdef TARGET_OS_FAMILY_bsd
  92 # include "os_bsd.inline.hpp"
  93 #endif
  94 
  95 static jint CurrentVersion = JNI_VERSION_1_8;
  96 
  97 


4997   DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);
4998 
4999   if (Threads::is_supported_jni_version(args->version)) {
5000     ret = JNI_OK;
5001   }
5002   // 1.1 style no longer supported in hotspot.
5003   // According the JNI spec, we should update args->version on return.
5004   // We also use the structure to communicate with launcher about default
5005   // stack size.
5006   if (args->version == JNI_VERSION_1_1) {
5007     args->version = JNI_VERSION_1_2;
5008     // javaStackSize is int in arguments structure
5009     assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
5010     args->javaStackSize = (jint)(ThreadStackSize * K);
5011   }
5012   return ret;
5013 }
5014 
5015 #ifndef PRODUCT
5016 

5017 #include "gc_interface/collectedHeap.hpp"
5018 #if INCLUDE_ALL_GCS
5019 #include "gc_implementation/g1/heapRegionRemSet.hpp"
5020 #endif
5021 #include "utilities/quickSort.hpp"
5022 #if INCLUDE_VM_STRUCTS
5023 #include "runtime/vmStructs.hpp"
5024 #endif
5025 
5026 #define run_unit_test(unit_test_function_call)              \
5027   tty->print_cr("Running test: " #unit_test_function_call); \
5028   unit_test_function_call
5029 
5030 void execute_internal_vm_tests() {
5031   if (ExecuteInternalVMTests) {
5032     tty->print_cr("Running internal VM tests");
5033     run_unit_test(GlobalDefinitions::test_globals());

5034     run_unit_test(arrayOopDesc::test_max_array_length());
5035     run_unit_test(CollectedHeap::test_is_in());
5036     run_unit_test(QuickSort::test_quick_sort());
5037     run_unit_test(AltHashing::test_alt_hash());
5038 #if INCLUDE_VM_STRUCTS
5039     run_unit_test(VMStructs::test());
5040 #endif
5041 #if INCLUDE_ALL_GCS
5042     run_unit_test(HeapRegionRemSet::test_prt());
5043 #endif
5044     tty->print_cr("All internal VM tests passed");
5045   }
5046 }
5047 
5048 #undef run_unit_test
5049 
5050 #endif
5051 
5052 #ifndef USDT2
5053 HS_DTRACE_PROBE_DECL3(hotspot_jni, CreateJavaVM__entry, vm, penv, args);


5114    * sets safe_to_recreate_vm to 1, such that any new call to
5115    * JNI_CreateJavaVM will immediately fail using the above logic.
5116    */
5117   bool can_try_again = true;
5118 
5119   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
5120   if (result == JNI_OK) {
5121     JavaThread *thread = JavaThread::current();
5122     /* thread is thread_in_vm here */
5123     *vm = (JavaVM *)(&main_vm);
5124     *(JNIEnv**)penv = thread->jni_environment();
5125 
5126     // Tracks the time application was running before GC
5127     RuntimeService::record_application_start();
5128 
5129     // Notify JVMTI
5130     if (JvmtiExport::should_post_thread_life()) {
5131        JvmtiExport::post_thread_start(thread);
5132     }
5133 
5134     EVENT_BEGIN(TraceEventThreadStart, event);
5135     EVENT_COMMIT(event,
5136         EVENT_SET(event, javalangthread, java_lang_Thread::thread_id(thread->threadObj())));


5137 
5138     // Check if we should compile all classes on bootclasspath
5139     NOT_PRODUCT(if (CompileTheWorld) ClassLoader::compile_the_world();)
5140     NOT_PRODUCT(if (ReplayCompiles) ciReplay::replay(thread);)
5141     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
5142     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5143   } else {
5144     if (can_try_again) {
5145       // reset safe_to_recreate_vm to 1 so that retrial would be possible
5146       safe_to_recreate_vm = 1;
5147     }
5148 
5149     // Creation failed. We must reset vm_created
5150     *vm = 0;
5151     *(JNIEnv**)penv = 0;
5152     // reset vm_created last to avoid race condition. Use OrderAccess to
5153     // control both compiler and architectural-based reordering.
5154     OrderAccess::release_store(&vm_created, 0);
5155   }
5156 


5317   if (attach_failed) {
5318     // Added missing cleanup
5319     thread->cleanup_failed_attach_current_thread();
5320     return JNI_ERR;
5321   }
5322 
5323   // mark the thread as no longer attaching
5324   // this uses a fence to push the change through so we don't have
5325   // to regrab the threads_lock
5326   thread->set_done_attaching_via_jni();
5327 
5328   // Set java thread status.
5329   java_lang_Thread::set_thread_status(thread->threadObj(),
5330               java_lang_Thread::RUNNABLE);
5331 
5332   // Notify the debugger
5333   if (JvmtiExport::should_post_thread_life()) {
5334     JvmtiExport::post_thread_start(thread);
5335   }
5336 
5337   EVENT_BEGIN(TraceEventThreadStart, event);
5338   EVENT_COMMIT(event,
5339       EVENT_SET(event, javalangthread, java_lang_Thread::thread_id(thread->threadObj())));


5340 
5341   *(JNIEnv**)penv = thread->jni_environment();
5342 
5343   // Now leaving the VM, so change thread_state. This is normally automatically taken care
5344   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
5345   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
5346   // needed.
5347 
5348   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5349 
5350   // Perform any platform dependent FPU setup
5351   os::setup_fpu();
5352 
5353   return JNI_OK;
5354 }
5355 
5356 
5357 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
5358 #ifndef USDT2
5359   DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args);




  57 #include "prims/jniFastGetField.hpp"
  58 #include "prims/jvm.h"
  59 #include "prims/jvm_misc.hpp"
  60 #include "prims/jvmtiExport.hpp"
  61 #include "prims/jvmtiThreadState.hpp"
  62 #include "runtime/compilationPolicy.hpp"
  63 #include "runtime/fieldDescriptor.hpp"
  64 #include "runtime/fprofiler.hpp"
  65 #include "runtime/handles.inline.hpp"
  66 #include "runtime/interfaceSupport.hpp"
  67 #include "runtime/java.hpp"
  68 #include "runtime/javaCalls.hpp"
  69 #include "runtime/jfieldIDWorkaround.hpp"
  70 #include "runtime/reflection.hpp"
  71 #include "runtime/sharedRuntime.hpp"
  72 #include "runtime/signature.hpp"
  73 #include "runtime/thread.inline.hpp"
  74 #include "runtime/vm_operations.hpp"
  75 #include "services/runtimeService.hpp"
  76 #include "trace/tracing.hpp"

  77 #include "utilities/defaultStream.hpp"
  78 #include "utilities/dtrace.hpp"
  79 #include "utilities/events.hpp"
  80 #include "utilities/histogram.hpp"
  81 #ifdef TARGET_OS_FAMILY_linux
  82 # include "os_linux.inline.hpp"
  83 #endif
  84 #ifdef TARGET_OS_FAMILY_solaris
  85 # include "os_solaris.inline.hpp"
  86 #endif
  87 #ifdef TARGET_OS_FAMILY_windows
  88 # include "os_windows.inline.hpp"
  89 #endif
  90 #ifdef TARGET_OS_FAMILY_bsd
  91 # include "os_bsd.inline.hpp"
  92 #endif
  93 
  94 static jint CurrentVersion = JNI_VERSION_1_8;
  95 
  96 


4996   DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);
4997 
4998   if (Threads::is_supported_jni_version(args->version)) {
4999     ret = JNI_OK;
5000   }
5001   // 1.1 style no longer supported in hotspot.
5002   // According the JNI spec, we should update args->version on return.
5003   // We also use the structure to communicate with launcher about default
5004   // stack size.
5005   if (args->version == JNI_VERSION_1_1) {
5006     args->version = JNI_VERSION_1_2;
5007     // javaStackSize is int in arguments structure
5008     assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
5009     args->javaStackSize = (jint)(ThreadStackSize * K);
5010   }
5011   return ret;
5012 }
5013 
5014 #ifndef PRODUCT
5015 
5016 #include "gc_implementation/shared/gcTimer.hpp"
5017 #include "gc_interface/collectedHeap.hpp"
5018 #if INCLUDE_ALL_GCS
5019 #include "gc_implementation/g1/heapRegionRemSet.hpp"
5020 #endif
5021 #include "utilities/quickSort.hpp"
5022 #if INCLUDE_VM_STRUCTS
5023 #include "runtime/vmStructs.hpp"
5024 #endif
5025 
5026 #define run_unit_test(unit_test_function_call)              \
5027   tty->print_cr("Running test: " #unit_test_function_call); \
5028   unit_test_function_call
5029 
5030 void execute_internal_vm_tests() {
5031   if (ExecuteInternalVMTests) {
5032     tty->print_cr("Running internal VM tests");
5033     run_unit_test(GlobalDefinitions::test_globals());
5034     run_unit_test(GCTimerAllTest::all());
5035     run_unit_test(arrayOopDesc::test_max_array_length());
5036     run_unit_test(CollectedHeap::test_is_in());
5037     run_unit_test(QuickSort::test_quick_sort());
5038     run_unit_test(AltHashing::test_alt_hash());
5039 #if INCLUDE_VM_STRUCTS
5040     run_unit_test(VMStructs::test());
5041 #endif
5042 #if INCLUDE_ALL_GCS
5043     run_unit_test(HeapRegionRemSet::test_prt());
5044 #endif
5045     tty->print_cr("All internal VM tests passed");
5046   }
5047 }
5048 
5049 #undef run_unit_test
5050 
5051 #endif
5052 
5053 #ifndef USDT2
5054 HS_DTRACE_PROBE_DECL3(hotspot_jni, CreateJavaVM__entry, vm, penv, args);


5115    * sets safe_to_recreate_vm to 1, such that any new call to
5116    * JNI_CreateJavaVM will immediately fail using the above logic.
5117    */
5118   bool can_try_again = true;
5119 
5120   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
5121   if (result == JNI_OK) {
5122     JavaThread *thread = JavaThread::current();
5123     /* thread is thread_in_vm here */
5124     *vm = (JavaVM *)(&main_vm);
5125     *(JNIEnv**)penv = thread->jni_environment();
5126 
5127     // Tracks the time application was running before GC
5128     RuntimeService::record_application_start();
5129 
5130     // Notify JVMTI
5131     if (JvmtiExport::should_post_thread_life()) {
5132        JvmtiExport::post_thread_start(thread);
5133     }
5134 
5135     EventThreadStart event;
5136     if (event.should_commit()) {
5137       event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5138       event.commit();
5139     }
5140 
5141     // Check if we should compile all classes on bootclasspath
5142     NOT_PRODUCT(if (CompileTheWorld) ClassLoader::compile_the_world();)
5143     NOT_PRODUCT(if (ReplayCompiles) ciReplay::replay(thread);)
5144     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
5145     ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5146   } else {
5147     if (can_try_again) {
5148       // reset safe_to_recreate_vm to 1 so that retrial would be possible
5149       safe_to_recreate_vm = 1;
5150     }
5151 
5152     // Creation failed. We must reset vm_created
5153     *vm = 0;
5154     *(JNIEnv**)penv = 0;
5155     // reset vm_created last to avoid race condition. Use OrderAccess to
5156     // control both compiler and architectural-based reordering.
5157     OrderAccess::release_store(&vm_created, 0);
5158   }
5159 


5320   if (attach_failed) {
5321     // Added missing cleanup
5322     thread->cleanup_failed_attach_current_thread();
5323     return JNI_ERR;
5324   }
5325 
5326   // mark the thread as no longer attaching
5327   // this uses a fence to push the change through so we don't have
5328   // to regrab the threads_lock
5329   thread->set_done_attaching_via_jni();
5330 
5331   // Set java thread status.
5332   java_lang_Thread::set_thread_status(thread->threadObj(),
5333               java_lang_Thread::RUNNABLE);
5334 
5335   // Notify the debugger
5336   if (JvmtiExport::should_post_thread_life()) {
5337     JvmtiExport::post_thread_start(thread);
5338   }
5339 
5340   EventThreadStart event;
5341   if (event.should_commit()) {
5342     event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
5343     event.commit();
5344   }
5345 
5346   *(JNIEnv**)penv = thread->jni_environment();
5347 
5348   // Now leaving the VM, so change thread_state. This is normally automatically taken care
5349   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
5350   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
5351   // needed.
5352 
5353   ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native);
5354 
5355   // Perform any platform dependent FPU setup
5356   os::setup_fpu();
5357 
5358   return JNI_OK;
5359 }
5360 
5361 
5362 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
5363 #ifndef USDT2
5364   DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args);