src/share/vm/runtime/thread.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>


  60 #include "runtime/mutexLocker.hpp"
  61 #include "runtime/objectMonitor.hpp"
  62 #include "runtime/osThread.hpp"
  63 #include "runtime/safepoint.hpp"
  64 #include "runtime/sharedRuntime.hpp"
  65 #include "runtime/statSampler.hpp"
  66 #include "runtime/stubRoutines.hpp"
  67 #include "runtime/task.hpp"
  68 #include "runtime/thread.inline.hpp"
  69 #include "runtime/threadCritical.hpp"
  70 #include "runtime/threadLocalStorage.hpp"
  71 #include "runtime/vframe.hpp"
  72 #include "runtime/vframeArray.hpp"
  73 #include "runtime/vframe_hp.hpp"
  74 #include "runtime/vmThread.hpp"
  75 #include "runtime/vm_operations.hpp"
  76 #include "services/attachListener.hpp"
  77 #include "services/management.hpp"
  78 #include "services/memTracker.hpp"
  79 #include "services/threadService.hpp"
  80 #include "trace/traceEventTypes.hpp"

  81 #include "utilities/defaultStream.hpp"
  82 #include "utilities/dtrace.hpp"
  83 #include "utilities/events.hpp"
  84 #include "utilities/preserveException.hpp"
  85 #include "utilities/macros.hpp"
  86 #ifdef TARGET_OS_FAMILY_linux
  87 # include "os_linux.inline.hpp"
  88 #endif
  89 #ifdef TARGET_OS_FAMILY_solaris
  90 # include "os_solaris.inline.hpp"
  91 #endif
  92 #ifdef TARGET_OS_FAMILY_windows
  93 # include "os_windows.inline.hpp"
  94 #endif
  95 #ifdef TARGET_OS_FAMILY_bsd
  96 # include "os_bsd.inline.hpp"
  97 #endif
  98 #if INCLUDE_ALL_GCS
  99 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
 100 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"


 221   set_handle_area(new (mtThread) HandleArea(NULL));
 222   set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(300, true));
 223   set_active_handles(NULL);
 224   set_free_handle_block(NULL);
 225   set_last_handle_mark(NULL);
 226 
 227   // This initial value ==> never claimed.
 228   _oops_do_parity = 0;
 229 
 230   // the handle mark links itself to last_handle_mark
 231   new HandleMark(this);
 232 
 233   // plain initialization
 234   debug_only(_owned_locks = NULL;)
 235   debug_only(_allow_allocation_count = 0;)
 236   NOT_PRODUCT(_allow_safepoint_count = 0;)
 237   NOT_PRODUCT(_skip_gcalot = false;)
 238   CHECK_UNHANDLED_OOPS_ONLY(_gc_locked_out_count = 0;)
 239   _jvmti_env_iteration_count = 0;
 240   set_allocated_bytes(0);
 241   set_trace_buffer(NULL);
 242   _vm_operation_started_count = 0;
 243   _vm_operation_completed_count = 0;
 244   _current_pending_monitor = NULL;
 245   _current_pending_monitor_is_from_java = true;
 246   _current_waiting_monitor = NULL;
 247   _num_nested_signal = 0;
 248   omFreeList = NULL ;
 249   omFreeCount = 0 ;
 250   omFreeProvision = 32 ;
 251   omInUseList = NULL ;
 252   omInUseCount = 0 ;
 253 
 254 #ifdef ASSERT
 255   _visited_for_critical_count = false;
 256 #endif
 257 
 258   _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true);
 259   _suspend_flags = 0;
 260 
 261   // thread-specific hashCode stream generator state - Marsaglia shift-xor form


1642 
1643   this->cache_global_variables();
1644 
1645   // Thread is now sufficient initialized to be handled by the safepoint code as being
1646   // in the VM. Change thread state from _thread_new to _thread_in_vm
1647   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1648 
1649   assert(JavaThread::current() == this, "sanity check");
1650   assert(!Thread::current()->owns_locks(), "sanity check");
1651 
1652   DTRACE_THREAD_PROBE(start, this);
1653 
1654   // This operation might block. We call that after all safepoint checks for a new thread has
1655   // been completed.
1656   this->set_active_handles(JNIHandleBlock::allocate_block());
1657 
1658   if (JvmtiExport::should_post_thread_life()) {
1659     JvmtiExport::post_thread_start(this);
1660   }
1661 
1662   EVENT_BEGIN(TraceEventThreadStart, event);
1663   EVENT_COMMIT(event,
1664      EVENT_SET(event, javalangthread, java_lang_Thread::thread_id(this->threadObj())));


1665 
1666   // We call another function to do the rest so we are sure that the stack addresses used
1667   // from there will be lower than the stack base just computed
1668   thread_main_inner();
1669 
1670   // Note, thread is no longer valid at this point!
1671 }
1672 
1673 
1674 void JavaThread::thread_main_inner() {
1675   assert(JavaThread::current() == this, "sanity check");
1676   assert(this->threadObj() != NULL, "just checking");
1677 
1678   // Execute thread entry point unless this thread has a pending exception
1679   // or has been stopped before starting.
1680   // Note: Due to JVM_StopThread we can have pending exceptions already!
1681   if (!this->has_pending_exception() &&
1682       !java_lang_Thread::is_stillborn(this->threadObj())) {
1683     {
1684       ResourceMark rm(this);


1774                                   vmSymbols::uncaughtException_name(),
1775                                   vmSymbols::thread_throwable_void_signature(),
1776                                   threadObj,           // Arg 1
1777                                   uncaught_exception,  // Arg 2
1778                                   THREAD);
1779         }
1780         if (HAS_PENDING_EXCEPTION) {
1781           ResourceMark rm(this);
1782           jio_fprintf(defaultStream::error_stream(),
1783                 "\nException: %s thrown from the UncaughtExceptionHandler"
1784                 " in thread \"%s\"\n",
1785                 pending_exception()->klass()->external_name(),
1786                 get_thread_name());
1787           CLEAR_PENDING_EXCEPTION;
1788         }
1789       }
1790     }
1791 
1792     // Called before the java thread exit since we want to read info
1793     // from java_lang_Thread object
1794     EVENT_BEGIN(TraceEventThreadEnd, event);
1795     EVENT_COMMIT(event,
1796         EVENT_SET(event, javalangthread, java_lang_Thread::thread_id(this->threadObj())));


1797 
1798     // Call after last event on thread
1799     EVENT_THREAD_EXIT(this);
1800 
1801     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1802     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1803     // is deprecated anyhow.
1804     { int count = 3;
1805       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1806         EXCEPTION_MARK;
1807         JavaValue result(T_VOID);
1808         KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1809         JavaCalls::call_virtual(&result,
1810                               threadObj, thread_klass,
1811                               vmSymbols::exit_method_name(),
1812                               vmSymbols::void_method_signature(),
1813                               THREAD);
1814         CLEAR_PENDING_EXCEPTION;
1815       }
1816     }


3631   // Signal Dispatcher needs to be started before VMInit event is posted
3632   os::signal_init();
3633 
3634   // Start Attach Listener if +StartAttachListener or it can't be started lazily
3635   if (!DisableAttachMechanism) {
3636     if (StartAttachListener || AttachListener::init_at_startup()) {
3637       AttachListener::init();
3638     }
3639   }
3640 
3641   // Launch -Xrun agents
3642   // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3643   // back-end can launch with -Xdebug -Xrunjdwp.
3644   if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3645     create_vm_init_libraries();
3646   }
3647 
3648   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3649   JvmtiExport::post_vm_initialized();
3650 
3651   if (!TRACE_START()) {
3652     vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3653   }
3654 
3655   if (CleanChunkPoolAsync) {
3656     Chunk::start_chunk_pool_cleaner_task();
3657   }
3658 
3659   // initialize compiler(s)
3660 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
3661   CompileBroker::compilation_init();
3662 #endif
3663 
3664 #if INCLUDE_MANAGEMENT
3665   Management::initialize(THREAD);
3666 #endif // INCLUDE_MANAGEMENT
3667 
3668   if (HAS_PENDING_EXCEPTION) {
3669     // management agent fails to start possibly due to
3670     // configuration problem and is responsible for printing
3671     // stack trace if appropriate. Simply exit VM.
3672     vm_exit(1);




  60 #include "runtime/mutexLocker.hpp"
  61 #include "runtime/objectMonitor.hpp"
  62 #include "runtime/osThread.hpp"
  63 #include "runtime/safepoint.hpp"
  64 #include "runtime/sharedRuntime.hpp"
  65 #include "runtime/statSampler.hpp"
  66 #include "runtime/stubRoutines.hpp"
  67 #include "runtime/task.hpp"
  68 #include "runtime/thread.inline.hpp"
  69 #include "runtime/threadCritical.hpp"
  70 #include "runtime/threadLocalStorage.hpp"
  71 #include "runtime/vframe.hpp"
  72 #include "runtime/vframeArray.hpp"
  73 #include "runtime/vframe_hp.hpp"
  74 #include "runtime/vmThread.hpp"
  75 #include "runtime/vm_operations.hpp"
  76 #include "services/attachListener.hpp"
  77 #include "services/management.hpp"
  78 #include "services/memTracker.hpp"
  79 #include "services/threadService.hpp"
  80 #include "trace/tracing.hpp"
  81 #include "trace/traceMacros.hpp"
  82 #include "utilities/defaultStream.hpp"
  83 #include "utilities/dtrace.hpp"
  84 #include "utilities/events.hpp"
  85 #include "utilities/preserveException.hpp"
  86 #include "utilities/macros.hpp"
  87 #ifdef TARGET_OS_FAMILY_linux
  88 # include "os_linux.inline.hpp"
  89 #endif
  90 #ifdef TARGET_OS_FAMILY_solaris
  91 # include "os_solaris.inline.hpp"
  92 #endif
  93 #ifdef TARGET_OS_FAMILY_windows
  94 # include "os_windows.inline.hpp"
  95 #endif
  96 #ifdef TARGET_OS_FAMILY_bsd
  97 # include "os_bsd.inline.hpp"
  98 #endif
  99 #if INCLUDE_ALL_GCS
 100 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
 101 #include "gc_implementation/g1/concurrentMarkThread.inline.hpp"


 222   set_handle_area(new (mtThread) HandleArea(NULL));
 223   set_metadata_handles(new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(300, true));
 224   set_active_handles(NULL);
 225   set_free_handle_block(NULL);
 226   set_last_handle_mark(NULL);
 227 
 228   // This initial value ==> never claimed.
 229   _oops_do_parity = 0;
 230 
 231   // the handle mark links itself to last_handle_mark
 232   new HandleMark(this);
 233 
 234   // plain initialization
 235   debug_only(_owned_locks = NULL;)
 236   debug_only(_allow_allocation_count = 0;)
 237   NOT_PRODUCT(_allow_safepoint_count = 0;)
 238   NOT_PRODUCT(_skip_gcalot = false;)
 239   CHECK_UNHANDLED_OOPS_ONLY(_gc_locked_out_count = 0;)
 240   _jvmti_env_iteration_count = 0;
 241   set_allocated_bytes(0);

 242   _vm_operation_started_count = 0;
 243   _vm_operation_completed_count = 0;
 244   _current_pending_monitor = NULL;
 245   _current_pending_monitor_is_from_java = true;
 246   _current_waiting_monitor = NULL;
 247   _num_nested_signal = 0;
 248   omFreeList = NULL ;
 249   omFreeCount = 0 ;
 250   omFreeProvision = 32 ;
 251   omInUseList = NULL ;
 252   omInUseCount = 0 ;
 253 
 254 #ifdef ASSERT
 255   _visited_for_critical_count = false;
 256 #endif
 257 
 258   _SR_lock = new Monitor(Mutex::suspend_resume, "SR_lock", true);
 259   _suspend_flags = 0;
 260 
 261   // thread-specific hashCode stream generator state - Marsaglia shift-xor form


1642 
1643   this->cache_global_variables();
1644 
1645   // Thread is now sufficient initialized to be handled by the safepoint code as being
1646   // in the VM. Change thread state from _thread_new to _thread_in_vm
1647   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1648 
1649   assert(JavaThread::current() == this, "sanity check");
1650   assert(!Thread::current()->owns_locks(), "sanity check");
1651 
1652   DTRACE_THREAD_PROBE(start, this);
1653 
1654   // This operation might block. We call that after all safepoint checks for a new thread has
1655   // been completed.
1656   this->set_active_handles(JNIHandleBlock::allocate_block());
1657 
1658   if (JvmtiExport::should_post_thread_life()) {
1659     JvmtiExport::post_thread_start(this);
1660   }
1661 
1662   EventThreadStart event;
1663   if (event.should_commit()) {
1664      event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1665      event.commit();
1666   }
1667 
1668   // We call another function to do the rest so we are sure that the stack addresses used
1669   // from there will be lower than the stack base just computed
1670   thread_main_inner();
1671 
1672   // Note, thread is no longer valid at this point!
1673 }
1674 
1675 
1676 void JavaThread::thread_main_inner() {
1677   assert(JavaThread::current() == this, "sanity check");
1678   assert(this->threadObj() != NULL, "just checking");
1679 
1680   // Execute thread entry point unless this thread has a pending exception
1681   // or has been stopped before starting.
1682   // Note: Due to JVM_StopThread we can have pending exceptions already!
1683   if (!this->has_pending_exception() &&
1684       !java_lang_Thread::is_stillborn(this->threadObj())) {
1685     {
1686       ResourceMark rm(this);


1776                                   vmSymbols::uncaughtException_name(),
1777                                   vmSymbols::thread_throwable_void_signature(),
1778                                   threadObj,           // Arg 1
1779                                   uncaught_exception,  // Arg 2
1780                                   THREAD);
1781         }
1782         if (HAS_PENDING_EXCEPTION) {
1783           ResourceMark rm(this);
1784           jio_fprintf(defaultStream::error_stream(),
1785                 "\nException: %s thrown from the UncaughtExceptionHandler"
1786                 " in thread \"%s\"\n",
1787                 pending_exception()->klass()->external_name(),
1788                 get_thread_name());
1789           CLEAR_PENDING_EXCEPTION;
1790         }
1791       }
1792     }
1793 
1794     // Called before the java thread exit since we want to read info
1795     // from java_lang_Thread object
1796     EventThreadEnd event;
1797     if (event.should_commit()) {
1798         event.set_javalangthread(java_lang_Thread::thread_id(this->threadObj()));
1799         event.commit();
1800     }
1801 
1802     // Call after last event on thread
1803     EVENT_THREAD_EXIT(this);
1804 
1805     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1806     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1807     // is deprecated anyhow.
1808     { int count = 3;
1809       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1810         EXCEPTION_MARK;
1811         JavaValue result(T_VOID);
1812         KlassHandle thread_klass(THREAD, SystemDictionary::Thread_klass());
1813         JavaCalls::call_virtual(&result,
1814                               threadObj, thread_klass,
1815                               vmSymbols::exit_method_name(),
1816                               vmSymbols::void_method_signature(),
1817                               THREAD);
1818         CLEAR_PENDING_EXCEPTION;
1819       }
1820     }


3635   // Signal Dispatcher needs to be started before VMInit event is posted
3636   os::signal_init();
3637 
3638   // Start Attach Listener if +StartAttachListener or it can't be started lazily
3639   if (!DisableAttachMechanism) {
3640     if (StartAttachListener || AttachListener::init_at_startup()) {
3641       AttachListener::init();
3642     }
3643   }
3644 
3645   // Launch -Xrun agents
3646   // Must be done in the JVMTI live phase so that for backward compatibility the JDWP
3647   // back-end can launch with -Xdebug -Xrunjdwp.
3648   if (!EagerXrunInit && Arguments::init_libraries_at_startup()) {
3649     create_vm_init_libraries();
3650   }
3651 
3652   // Notify JVMTI agents that VM initialization is complete - nop if no agents.
3653   JvmtiExport::post_vm_initialized();
3654 
3655   if (TRACE_START() != JNI_OK) {
3656     vm_exit_during_initialization("Failed to start tracing backend.");
3657   }
3658 
3659   if (CleanChunkPoolAsync) {
3660     Chunk::start_chunk_pool_cleaner_task();
3661   }
3662 
3663   // initialize compiler(s)
3664 #if defined(COMPILER1) || defined(COMPILER2) || defined(SHARK)
3665   CompileBroker::compilation_init();
3666 #endif
3667 
3668 #if INCLUDE_MANAGEMENT
3669   Management::initialize(THREAD);
3670 #endif // INCLUDE_MANAGEMENT
3671 
3672   if (HAS_PENDING_EXCEPTION) {
3673     // management agent fails to start possibly due to
3674     // configuration problem and is responsible for printing
3675     // stack trace if appropriate. Simply exit VM.
3676     vm_exit(1);