< prev index next >

src/hotspot/share/runtime/threadSMR.cpp

Print this page




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

  29 #include "runtime/thread.inline.hpp"
  30 #include "runtime/threadSMR.inline.hpp"
  31 #include "runtime/vmOperations.hpp"
  32 #include "services/threadService.hpp"

  33 #include "utilities/copy.hpp"
  34 #include "utilities/globalDefinitions.hpp"
  35 #include "utilities/ostream.hpp"
  36 #include "utilities/resourceHash.hpp"
  37 #include "utilities/vmError.hpp"
  38 
  39 // The '_cnt', '_max' and '_times" fields are enabled via
  40 // -XX:+EnableThreadSMRStatistics:
  41 
  42 // # of parallel threads in _delete_lock->wait().
  43 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
  44 // but there is no nice 16-bit _FORMAT support.
  45 uint                  ThreadsSMRSupport::_delete_lock_wait_cnt = 0;
  46 
  47 // Max # of parallel threads in _delete_lock->wait().
  48 // Impl note: See _delete_lock_wait_cnt note.
  49 uint                  ThreadsSMRSupport::_delete_lock_wait_max = 0;
  50 
  51 // Flag to indicate when an _delete_lock->notify() is needed.
  52 // Impl note: See _delete_lock_wait_cnt note.


 112 // loop for correctness.
 113 volatile uint         ThreadsSMRSupport::_tlh_time_max = 0;
 114 
 115 // Cumulative time in millis to delete ThreadsListHandles.
 116 // Impl note: Atomically added to over VM lifetime so use unsigned for more
 117 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
 118 // isn't available everywhere (or is it?).
 119 volatile uint         ThreadsSMRSupport::_tlh_times = 0;
 120 
 121 ThreadsList*          ThreadsSMRSupport::_to_delete_list = NULL;
 122 
 123 // # of parallel ThreadsLists on the to-delete list.
 124 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
 125 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
 126 uint                  ThreadsSMRSupport::_to_delete_list_cnt = 0;
 127 
 128 // Max # of parallel ThreadsLists on the to-delete list.
 129 // Impl note: See _to_delete_list_cnt note.
 130 uint                  ThreadsSMRSupport::_to_delete_list_max = 0;
 131 
 132 
 133 // 'inline' functions first so the definitions are before first use:
 134 
 135 inline void ThreadsSMRSupport::add_deleted_thread_times(uint add_value) {
 136   Atomic::add(add_value, &_deleted_thread_times);
 137 }
 138 
 139 inline void ThreadsSMRSupport::inc_deleted_thread_cnt() {
 140   Atomic::inc(&_deleted_thread_cnt);
 141 }
 142 
 143 inline void ThreadsSMRSupport::inc_java_thread_list_alloc_cnt() {
 144   _java_thread_list_alloc_cnt++;
 145 }
 146 
 147 inline bool ThreadsSMRSupport::is_bootstrap_list(ThreadsList* list) {
 148   return list == &_bootstrap_list;
 149 }
 150 
 151 inline void ThreadsSMRSupport::update_deleted_thread_time_max(uint new_value) {
 152   while (true) {


 591 
 592 void ThreadsList::dec_nested_handle_cnt() {
 593   // The decrement only needs to be MO_ACQ_REL since the reference
 594   // counter is volatile (and the hazard ptr is already NULL).
 595   Atomic::dec(&_nested_handle_cnt);
 596 }
 597 
 598 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
 599   if (target == NULL) {
 600     return -1;
 601   }
 602   for (uint i = 0; i < length(); i++) {
 603     if (target == thread_at(i)) {
 604       return (int)i;
 605     }
 606   }
 607   return -1;
 608 }
 609 
 610 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {





 611   for (uint i = 0; i < length(); i++) {
 612     JavaThread* thread = thread_at(i);
 613     oop tobj = thread->threadObj();
 614     // Ignore the thread if it hasn't run yet, has exited
 615     // or is starting to exit.
 616     if (tobj != NULL && !thread->is_exiting() &&
 617         java_tid == java_lang_Thread::thread_id(tobj)) {
 618       // found a match
 619       return thread;
 620     }
 621   }



 622   return NULL;
 623 }
 624 
 625 void ThreadsList::inc_nested_handle_cnt() {
 626   // The increment needs to be MO_SEQ_CST so that the reference counter
 627   // update is seen before the subsequent hazard ptr update.
 628   Atomic::inc(&_nested_handle_cnt);
 629 }
 630 
 631 bool ThreadsList::includes(const JavaThread * const p) const {
 632   if (p == NULL) {
 633     return false;
 634   }
 635   for (uint i = 0; i < length(); i++) {
 636     if (thread_at(i) == p) {
 637       return true;
 638     }
 639   }
 640   return false;
 641 }


 725     }
 726   }
 727 
 728   // Return a live JavaThread that is "protected" by the
 729   // ThreadsListHandle in the caller.
 730   *jt_pp = java_thread;
 731   return true;
 732 }
 733 
 734 void ThreadsSMRSupport::add_thread(JavaThread *thread){
 735   ThreadsList *new_list = ThreadsList::add_thread(get_java_thread_list(), thread);
 736   if (EnableThreadSMRStatistics) {
 737     inc_java_thread_list_alloc_cnt();
 738     update_java_thread_list_max(new_list->length());
 739   }
 740   // Initial _java_thread_list will not generate a "Threads::add" mesg.
 741   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 742 
 743   ThreadsList *old_list = xchg_java_thread_list(new_list);
 744   free_list(old_list);




 745 }
 746 
 747 // set_delete_notify() and clear_delete_notify() are called
 748 // under the protection of the delete_lock, but we also use an
 749 // Atomic operation to ensure the memory update is seen earlier than
 750 // when the delete_lock is dropped.
 751 //
 752 void ThreadsSMRSupport::clear_delete_notify() {
 753   Atomic::dec(&_delete_notify);
 754 }
 755 
 756 bool ThreadsSMRSupport::delete_notify() {
 757   // Use load_acquire() in order to see any updates to _delete_notify
 758   // earlier than when delete_lock is grabbed.
 759   return (OrderAccess::load_acquire(&_delete_notify) != 0);
 760 }
 761 
 762 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
 763 // The specified ThreadsList may not get deleted during this call if it
 764 // is still in-use (referenced by a hazard ptr). Other ThreadsLists


 892   const char* log_str = is_nested ? "nested hazard ptr" : "regular hazard ptr";
 893 
 894   // Note: delete_lock is held in smr_delete() for the entire
 895   // hazard ptr search so that we do not lose this notify() if
 896   // the exiting thread has to wait. That code path also holds
 897   // Threads_lock (which was grabbed before delete_lock) so that
 898   // threads_do() can be called. This means the system can't start a
 899   // safepoint which means this thread can't take too long to get to
 900   // a safepoint because of being blocked on delete_lock.
 901   //
 902   MonitorLocker ml(ThreadsSMRSupport::delete_lock(), Monitor::_no_safepoint_check_flag);
 903   if (ThreadsSMRSupport::delete_notify()) {
 904     // Notify any exiting JavaThreads that are waiting in smr_delete()
 905     // that we've released a ThreadsList.
 906     ml.notify_all();
 907     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list notified %s", os::current_thread_id(), log_str);
 908   }
 909 }
 910 
 911 void ThreadsSMRSupport::remove_thread(JavaThread *thread) {




 912   ThreadsList *new_list = ThreadsList::remove_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 913   if (EnableThreadSMRStatistics) {
 914     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 915     // This list is smaller so no need to check for a "longest" update.
 916   }
 917 
 918   // Final _java_thread_list will not generate a "Threads::remove" mesg.
 919   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 920 
 921   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 922   ThreadsSMRSupport::free_list(old_list);
 923 }
 924 
 925 // See note for clear_delete_notify().
 926 //
 927 void ThreadsSMRSupport::set_delete_notify() {
 928   Atomic::inc(&_delete_notify);
 929 }
 930 
 931 // Safely delete a JavaThread when it is no longer in use by a




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "logging/logStream.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "runtime/jniHandles.inline.hpp"
  29 #include "runtime/sharedRuntime.hpp"
  30 #include "runtime/thread.inline.hpp"
  31 #include "runtime/threadSMR.inline.hpp"
  32 #include "runtime/vmOperations.hpp"
  33 #include "services/threadService.hpp"
  34 #include "services/threadTable.hpp"
  35 #include "utilities/copy.hpp"
  36 #include "utilities/globalDefinitions.hpp"
  37 #include "utilities/ostream.hpp"
  38 #include "utilities/resourceHash.hpp"
  39 #include "utilities/vmError.hpp"
  40 
  41 // The '_cnt', '_max' and '_times" fields are enabled via
  42 // -XX:+EnableThreadSMRStatistics:
  43 
  44 // # of parallel threads in _delete_lock->wait().
  45 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
  46 // but there is no nice 16-bit _FORMAT support.
  47 uint                  ThreadsSMRSupport::_delete_lock_wait_cnt = 0;
  48 
  49 // Max # of parallel threads in _delete_lock->wait().
  50 // Impl note: See _delete_lock_wait_cnt note.
  51 uint                  ThreadsSMRSupport::_delete_lock_wait_max = 0;
  52 
  53 // Flag to indicate when an _delete_lock->notify() is needed.
  54 // Impl note: See _delete_lock_wait_cnt note.


 114 // loop for correctness.
 115 volatile uint         ThreadsSMRSupport::_tlh_time_max = 0;
 116 
 117 // Cumulative time in millis to delete ThreadsListHandles.
 118 // Impl note: Atomically added to over VM lifetime so use unsigned for more
 119 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
 120 // isn't available everywhere (or is it?).
 121 volatile uint         ThreadsSMRSupport::_tlh_times = 0;
 122 
 123 ThreadsList*          ThreadsSMRSupport::_to_delete_list = NULL;
 124 
 125 // # of parallel ThreadsLists on the to-delete list.
 126 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
 127 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
 128 uint                  ThreadsSMRSupport::_to_delete_list_cnt = 0;
 129 
 130 // Max # of parallel ThreadsLists on the to-delete list.
 131 // Impl note: See _to_delete_list_cnt note.
 132 uint                  ThreadsSMRSupport::_to_delete_list_max = 0;
 133 

 134 // 'inline' functions first so the definitions are before first use:
 135 
 136 inline void ThreadsSMRSupport::add_deleted_thread_times(uint add_value) {
 137   Atomic::add(add_value, &_deleted_thread_times);
 138 }
 139 
 140 inline void ThreadsSMRSupport::inc_deleted_thread_cnt() {
 141   Atomic::inc(&_deleted_thread_cnt);
 142 }
 143 
 144 inline void ThreadsSMRSupport::inc_java_thread_list_alloc_cnt() {
 145   _java_thread_list_alloc_cnt++;
 146 }
 147 
 148 inline bool ThreadsSMRSupport::is_bootstrap_list(ThreadsList* list) {
 149   return list == &_bootstrap_list;
 150 }
 151 
 152 inline void ThreadsSMRSupport::update_deleted_thread_time_max(uint new_value) {
 153   while (true) {


 592 
 593 void ThreadsList::dec_nested_handle_cnt() {
 594   // The decrement only needs to be MO_ACQ_REL since the reference
 595   // counter is volatile (and the hazard ptr is already NULL).
 596   Atomic::dec(&_nested_handle_cnt);
 597 }
 598 
 599 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
 600   if (target == NULL) {
 601     return -1;
 602   }
 603   for (uint i = 0; i < length(); i++) {
 604     if (target == thread_at(i)) {
 605       return (int)i;
 606     }
 607   }
 608   return -1;
 609 }
 610 
 611 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {
 612   ThreadTable::lazy_initialize(this);
 613   JavaThread* java_thread = ThreadTable::find_thread_by_tid(java_tid);
 614   if (java_thread == NULL) {
 615     // If the thread is not found in the table find it
 616     // with a linear search and add to the table.
 617     for (uint i = 0; i < length(); i++) {
 618       JavaThread* thread = thread_at(i);
 619       oop tobj = thread->threadObj();
 620       // Ignore the thread if it hasn't run yet, has exited
 621       // or is starting to exit.
 622       if (tobj != NULL && !thread->is_exiting() &&
 623           java_tid == java_lang_Thread::thread_id(tobj)) {
 624         ThreadTable::add_thread(java_tid, thread);
 625         return thread;
 626       }
 627     }
 628   } else if (java_thread != NULL && !java_thread->is_exiting()) {
 629       return java_thread;
 630   }
 631   return NULL;
 632 }
 633     
 634 void ThreadsList::inc_nested_handle_cnt() {
 635   // The increment needs to be MO_SEQ_CST so that the reference counter
 636   // update is seen before the subsequent hazard ptr update.
 637   Atomic::inc(&_nested_handle_cnt);
 638 }
 639 
 640 bool ThreadsList::includes(const JavaThread * const p) const {
 641   if (p == NULL) {
 642     return false;
 643   }
 644   for (uint i = 0; i < length(); i++) {
 645     if (thread_at(i) == p) {
 646       return true;
 647     }
 648   }
 649   return false;
 650 }


 734     }
 735   }
 736 
 737   // Return a live JavaThread that is "protected" by the
 738   // ThreadsListHandle in the caller.
 739   *jt_pp = java_thread;
 740   return true;
 741 }
 742 
 743 void ThreadsSMRSupport::add_thread(JavaThread *thread){
 744   ThreadsList *new_list = ThreadsList::add_thread(get_java_thread_list(), thread);
 745   if (EnableThreadSMRStatistics) {
 746     inc_java_thread_list_alloc_cnt();
 747     update_java_thread_list_max(new_list->length());
 748   }
 749   // Initial _java_thread_list will not generate a "Threads::add" mesg.
 750   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::add: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 751 
 752   ThreadsList *old_list = xchg_java_thread_list(new_list);
 753   free_list(old_list);
 754   if (ThreadTable::is_initialized()) {
 755     jlong tid = SharedRuntime::get_java_tid(thread);
 756     ThreadTable::add_thread(tid, thread);
 757   }
 758 }
 759 
 760 // set_delete_notify() and clear_delete_notify() are called
 761 // under the protection of the delete_lock, but we also use an
 762 // Atomic operation to ensure the memory update is seen earlier than
 763 // when the delete_lock is dropped.
 764 //
 765 void ThreadsSMRSupport::clear_delete_notify() {
 766   Atomic::dec(&_delete_notify);
 767 }
 768 
 769 bool ThreadsSMRSupport::delete_notify() {
 770   // Use load_acquire() in order to see any updates to _delete_notify
 771   // earlier than when delete_lock is grabbed.
 772   return (OrderAccess::load_acquire(&_delete_notify) != 0);
 773 }
 774 
 775 // Safely free a ThreadsList after a Threads::add() or Threads::remove().
 776 // The specified ThreadsList may not get deleted during this call if it
 777 // is still in-use (referenced by a hazard ptr). Other ThreadsLists


 905   const char* log_str = is_nested ? "nested hazard ptr" : "regular hazard ptr";
 906 
 907   // Note: delete_lock is held in smr_delete() for the entire
 908   // hazard ptr search so that we do not lose this notify() if
 909   // the exiting thread has to wait. That code path also holds
 910   // Threads_lock (which was grabbed before delete_lock) so that
 911   // threads_do() can be called. This means the system can't start a
 912   // safepoint which means this thread can't take too long to get to
 913   // a safepoint because of being blocked on delete_lock.
 914   //
 915   MonitorLocker ml(ThreadsSMRSupport::delete_lock(), Monitor::_no_safepoint_check_flag);
 916   if (ThreadsSMRSupport::delete_notify()) {
 917     // Notify any exiting JavaThreads that are waiting in smr_delete()
 918     // that we've released a ThreadsList.
 919     ml.notify_all();
 920     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list notified %s", os::current_thread_id(), log_str);
 921   }
 922 }
 923 
 924 void ThreadsSMRSupport::remove_thread(JavaThread *thread) {
 925   if (ThreadTable::is_initialized()) {
 926     jlong tid = SharedRuntime::get_java_tid(thread);
 927     ThreadTable::remove_thread(tid);
 928   }
 929   ThreadsList *new_list = ThreadsList::remove_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 930   if (EnableThreadSMRStatistics) {
 931     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 932     // This list is smaller so no need to check for a "longest" update.
 933   }
 934 
 935   // Final _java_thread_list will not generate a "Threads::remove" mesg.
 936   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 937 
 938   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 939   ThreadsSMRSupport::free_list(old_list);
 940 }
 941 
 942 // See note for clear_delete_notify().
 943 //
 944 void ThreadsSMRSupport::set_delete_notify() {
 945   Atomic::inc(&_delete_notify);
 946 }
 947 
 948 // Safely delete a JavaThread when it is no longer in use by a


< prev index next >