src/share/vm/runtime/thread.cpp

Print this page
rev 13113 : 8182651: Add TRACE_ONLY conditional macro to support more fine-grained INCLUDE_TRACE programming
Reviewed-by:


  76 #include "runtime/orderAccess.inline.hpp"
  77 #include "runtime/osThread.hpp"
  78 #include "runtime/safepoint.hpp"
  79 #include "runtime/sharedRuntime.hpp"
  80 #include "runtime/statSampler.hpp"
  81 #include "runtime/stubRoutines.hpp"
  82 #include "runtime/sweeper.hpp"
  83 #include "runtime/task.hpp"
  84 #include "runtime/thread.inline.hpp"
  85 #include "runtime/threadCritical.hpp"
  86 #include "runtime/vframe.hpp"
  87 #include "runtime/vframeArray.hpp"
  88 #include "runtime/vframe_hp.hpp"
  89 #include "runtime/vmThread.hpp"
  90 #include "runtime/vm_operations.hpp"
  91 #include "runtime/vm_version.hpp"
  92 #include "services/attachListener.hpp"
  93 #include "services/management.hpp"
  94 #include "services/memTracker.hpp"
  95 #include "services/threadService.hpp"
  96 #include "trace/traceMacros.hpp"
  97 #include "trace/tracing.hpp"
  98 #include "utilities/defaultStream.hpp"
  99 #include "utilities/dtrace.hpp"
 100 #include "utilities/events.hpp"
 101 #include "utilities/macros.hpp"
 102 #include "utilities/preserveException.hpp"
 103 #if INCLUDE_ALL_GCS
 104 #include "gc/cms/concurrentMarkSweepThread.hpp"
 105 #include "gc/g1/concurrentMarkThread.inline.hpp"
 106 #include "gc/parallel/pcTasks.hpp"
 107 #endif // INCLUDE_ALL_GCS
 108 #if INCLUDE_JVMCI
 109 #include "jvmci/jvmciCompiler.hpp"
 110 #include "jvmci/jvmciRuntime.hpp"
 111 #endif
 112 #ifdef COMPILER1
 113 #include "c1/c1_Compiler.hpp"
 114 #endif
 115 #ifdef COMPILER2
 116 #include "opto/c2compiler.hpp"
 117 #include "opto/idealGraphPrinter.hpp"
 118 #endif
 119 #if INCLUDE_RTM_OPT
 120 #include "runtime/rtmLocking.hpp"
 121 #endif




 122 
 123 // Initialization after module runtime initialization
 124 void universe_post_module_init();  // must happen after call_initPhase2
 125 
 126 #ifdef DTRACE_ENABLED
 127 
 128 // Only bother with this argument setup if dtrace is available
 129 
 130   #define HOTSPOT_THREAD_PROBE_start HOTSPOT_THREAD_START
 131   #define HOTSPOT_THREAD_PROBE_stop HOTSPOT_THREAD_STOP
 132 
 133   #define DTRACE_THREAD_PROBE(probe, javathread)                           \
 134     {                                                                      \
 135       ResourceMark rm(this);                                               \
 136       int len = 0;                                                         \
 137       const char* name = (javathread)->get_thread_name();                  \
 138       len = strlen(name);                                                  \
 139       HOTSPOT_THREAD_PROBE_##probe(/* probe = start, stop */               \
 140         (char *) name, len,                                                \
 141         java_lang_Thread::thread_id((javathread)->threadObj()),            \


1646     delete deferred;
1647   }
1648 
1649   // All Java related clean up happens in exit
1650   ThreadSafepointState::destroy(this);
1651   if (_thread_profiler != NULL) delete _thread_profiler;
1652   if (_thread_stat != NULL) delete _thread_stat;
1653 
1654 #if INCLUDE_JVMCI
1655   if (JVMCICounterSize > 0) {
1656     if (jvmci_counters_include(this)) {
1657       for (int i = 0; i < JVMCICounterSize; i++) {
1658         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1659       }
1660     }
1661     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1662   }
1663 #endif // INCLUDE_JVMCI
1664 }
1665 


















1666 
1667 // The first routine called by a new Java thread
1668 void JavaThread::run() {
1669   // initialize thread-local alloc buffer related fields
1670   this->initialize_tlab();
1671 
1672   // used to test validity of stack trace backs
1673   this->record_base_of_stack_pointer();
1674 
1675   // Record real stack base and size.
1676   this->record_stack_base_and_size();
1677 
1678   this->create_stack_guard_pages();
1679 
1680   this->cache_global_variables();
1681 
1682   // Thread is now sufficient initialized to be handled by the safepoint code as being
1683   // in the VM. Change thread state from _thread_new to _thread_in_vm
1684   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1685 
1686   assert(JavaThread::current() == this, "sanity check");
1687   assert(!Thread::current()->owns_locks(), "sanity check");
1688 
1689   DTRACE_THREAD_PROBE(start, this);
1690 
1691   // This operation might block. We call that after all safepoint checks for a new thread has
1692   // been completed.
1693   this->set_active_handles(JNIHandleBlock::allocate_block());
1694 
1695   if (JvmtiExport::should_post_thread_life()) {
1696     JvmtiExport::post_thread_start(this);
1697   }
1698 
1699   EventThreadStart event;
1700   if (event.should_commit()) {
1701     event.set_thread(THREAD_TRACE_ID(this));
1702     event.commit();
1703   }
1704 
1705   // We call another function to do the rest so we are sure that the stack addresses used
1706   // from there will be lower than the stack base just computed
1707   thread_main_inner();
1708 
1709   // Note, thread is no longer valid at this point!
1710 }
1711 
1712 
1713 void JavaThread::thread_main_inner() {
1714   assert(JavaThread::current() == this, "sanity check");
1715   assert(this->threadObj() != NULL, "just checking");
1716 
1717   // Execute thread entry point unless this thread has a pending exception
1718   // or has been stopped before starting.
1719   // Note: Due to JVM_StopThread we can have pending exceptions already!
1720   if (!this->has_pending_exception() &&
1721       !java_lang_Thread::is_stillborn(this->threadObj())) {
1722     {
1723       ResourceMark rm(this);


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




  76 #include "runtime/orderAccess.inline.hpp"
  77 #include "runtime/osThread.hpp"
  78 #include "runtime/safepoint.hpp"
  79 #include "runtime/sharedRuntime.hpp"
  80 #include "runtime/statSampler.hpp"
  81 #include "runtime/stubRoutines.hpp"
  82 #include "runtime/sweeper.hpp"
  83 #include "runtime/task.hpp"
  84 #include "runtime/thread.inline.hpp"
  85 #include "runtime/threadCritical.hpp"
  86 #include "runtime/vframe.hpp"
  87 #include "runtime/vframeArray.hpp"
  88 #include "runtime/vframe_hp.hpp"
  89 #include "runtime/vmThread.hpp"
  90 #include "runtime/vm_operations.hpp"
  91 #include "runtime/vm_version.hpp"
  92 #include "services/attachListener.hpp"
  93 #include "services/management.hpp"
  94 #include "services/memTracker.hpp"
  95 #include "services/threadService.hpp"


  96 #include "utilities/defaultStream.hpp"
  97 #include "utilities/dtrace.hpp"
  98 #include "utilities/events.hpp"
  99 #include "utilities/macros.hpp"
 100 #include "utilities/preserveException.hpp"
 101 #if INCLUDE_ALL_GCS
 102 #include "gc/cms/concurrentMarkSweepThread.hpp"
 103 #include "gc/g1/concurrentMarkThread.inline.hpp"
 104 #include "gc/parallel/pcTasks.hpp"
 105 #endif // INCLUDE_ALL_GCS
 106 #if INCLUDE_JVMCI
 107 #include "jvmci/jvmciCompiler.hpp"
 108 #include "jvmci/jvmciRuntime.hpp"
 109 #endif
 110 #ifdef COMPILER1
 111 #include "c1/c1_Compiler.hpp"
 112 #endif
 113 #ifdef COMPILER2
 114 #include "opto/c2compiler.hpp"
 115 #include "opto/idealGraphPrinter.hpp"
 116 #endif
 117 #if INCLUDE_RTM_OPT
 118 #include "runtime/rtmLocking.hpp"
 119 #endif
 120 #if INCLUDE_TRACE
 121 #include "trace/traceMacros.hpp"
 122 #include "trace/tracing.hpp"
 123 #endif
 124 
 125 // Initialization after module runtime initialization
 126 void universe_post_module_init();  // must happen after call_initPhase2
 127 
 128 #ifdef DTRACE_ENABLED
 129 
 130 // Only bother with this argument setup if dtrace is available
 131 
 132   #define HOTSPOT_THREAD_PROBE_start HOTSPOT_THREAD_START
 133   #define HOTSPOT_THREAD_PROBE_stop HOTSPOT_THREAD_STOP
 134 
 135   #define DTRACE_THREAD_PROBE(probe, javathread)                           \
 136     {                                                                      \
 137       ResourceMark rm(this);                                               \
 138       int len = 0;                                                         \
 139       const char* name = (javathread)->get_thread_name();                  \
 140       len = strlen(name);                                                  \
 141       HOTSPOT_THREAD_PROBE_##probe(/* probe = start, stop */               \
 142         (char *) name, len,                                                \
 143         java_lang_Thread::thread_id((javathread)->threadObj()),            \


1648     delete deferred;
1649   }
1650 
1651   // All Java related clean up happens in exit
1652   ThreadSafepointState::destroy(this);
1653   if (_thread_profiler != NULL) delete _thread_profiler;
1654   if (_thread_stat != NULL) delete _thread_stat;
1655 
1656 #if INCLUDE_JVMCI
1657   if (JVMCICounterSize > 0) {
1658     if (jvmci_counters_include(this)) {
1659       for (int i = 0; i < JVMCICounterSize; i++) {
1660         _jvmci_old_thread_counters[i] += _jvmci_counters[i];
1661       }
1662     }
1663     FREE_C_HEAP_ARRAY(jlong, _jvmci_counters);
1664   }
1665 #endif // INCLUDE_JVMCI
1666 }
1667 
1668 #if INCLUDE_TRACE
1669 static void post_thread_start_event(const JavaThread* jt) {
1670   assert(jt != NULL, "invariant");
1671   EventThreadStart event;
1672   if (event.should_commit()) {
1673     event.set_thread(THREAD_TRACE_ID(jt));
1674     event.commit();
1675   }
1676 }
1677 
1678 static void post_thread_end_event(const JavaThread* jt) {
1679   EventThreadEnd event;
1680   if (event.should_commit()) {
1681     event.set_thread(THREAD_TRACE_ID(jt));
1682     event.commit();
1683   }
1684 }
1685 #endif // INCLUDE_TRACE
1686 
1687 // The first routine called by a new Java thread
1688 void JavaThread::run() {
1689   // initialize thread-local alloc buffer related fields
1690   this->initialize_tlab();
1691 
1692   // used to test validity of stack trace backs
1693   this->record_base_of_stack_pointer();
1694 
1695   // Record real stack base and size.
1696   this->record_stack_base_and_size();
1697 
1698   this->create_stack_guard_pages();
1699 
1700   this->cache_global_variables();
1701 
1702   // Thread is now sufficient initialized to be handled by the safepoint code as being
1703   // in the VM. Change thread state from _thread_new to _thread_in_vm
1704   ThreadStateTransition::transition_and_fence(this, _thread_new, _thread_in_vm);
1705 
1706   assert(JavaThread::current() == this, "sanity check");
1707   assert(!Thread::current()->owns_locks(), "sanity check");
1708 
1709   DTRACE_THREAD_PROBE(start, this);
1710 
1711   // This operation might block. We call that after all safepoint checks for a new thread has
1712   // been completed.
1713   this->set_active_handles(JNIHandleBlock::allocate_block());
1714 
1715   if (JvmtiExport::should_post_thread_life()) {
1716     JvmtiExport::post_thread_start(this);
1717   }
1718 
1719   TRACE_ONLY(post_thread_start_event(this));




1720 
1721   // We call another function to do the rest so we are sure that the stack addresses used
1722   // from there will be lower than the stack base just computed
1723   thread_main_inner();
1724 
1725   // Note, thread is no longer valid at this point!
1726 }
1727 
1728 
1729 void JavaThread::thread_main_inner() {
1730   assert(JavaThread::current() == this, "sanity check");
1731   assert(this->threadObj() != NULL, "just checking");
1732 
1733   // Execute thread entry point unless this thread has a pending exception
1734   // or has been stopped before starting.
1735   // Note: Due to JVM_StopThread we can have pending exceptions already!
1736   if (!this->has_pending_exception() &&
1737       !java_lang_Thread::is_stillborn(this->threadObj())) {
1738     {
1739       ResourceMark rm(this);


1800       JavaValue result(T_VOID);
1801       JavaCalls::call_virtual(&result,
1802                               threadObj, thread_klass,
1803                               vmSymbols::dispatchUncaughtException_name(),
1804                               vmSymbols::throwable_void_signature(),
1805                               uncaught_exception,
1806                               THREAD);
1807       if (HAS_PENDING_EXCEPTION) {
1808         ResourceMark rm(this);
1809         jio_fprintf(defaultStream::error_stream(),
1810                     "\nException: %s thrown from the UncaughtExceptionHandler"
1811                     " in thread \"%s\"\n",
1812                     pending_exception()->klass()->external_name(),
1813                     get_thread_name());
1814         CLEAR_PENDING_EXCEPTION;
1815       }
1816     }
1817 
1818     // Called before the java thread exit since we want to read info
1819     // from java_lang_Thread object
1820     TRACE_ONLY(post_thread_end_event(this);)




1821 
1822     // Call after last event on thread
1823     EVENT_THREAD_EXIT(this);
1824 
1825     // Call Thread.exit(). We try 3 times in case we got another Thread.stop during
1826     // the execution of the method. If that is not enough, then we don't really care. Thread.stop
1827     // is deprecated anyhow.
1828     if (!is_Compiler_thread()) {
1829       int count = 3;
1830       while (java_lang_Thread::threadGroup(threadObj()) != NULL && (count-- > 0)) {
1831         EXCEPTION_MARK;
1832         JavaValue result(T_VOID);
1833         Klass* thread_klass = SystemDictionary::Thread_klass();
1834         JavaCalls::call_virtual(&result,
1835                                 threadObj, thread_klass,
1836                                 vmSymbols::exit_method_name(),
1837                                 vmSymbols::void_method_signature(),
1838                                 THREAD);
1839         CLEAR_PENDING_EXCEPTION;
1840       }