1 /*
   2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   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/vm_operations.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 Monitor*              ThreadsSMRSupport::_delete_lock =
  40                           new Monitor(Monitor::special, "Thread_SMR_delete_lock",
  41                                       false /* allow_vm_block */,
  42                                       Monitor::_safepoint_check_never);
  43 // The '_cnt', '_max' and '_times" fields are enabled via
  44 // -XX:+EnableThreadSMRStatistics:
  45 
  46 // # of parallel threads in _delete_lock->wait().
  47 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
  48 // but there is no nice 16-bit _FORMAT support.
  49 uint                  ThreadsSMRSupport::_delete_lock_wait_cnt = 0;
  50 
  51 // Max # of parallel threads in _delete_lock->wait().
  52 // Impl note: See _delete_lock_wait_cnt note.
  53 uint                  ThreadsSMRSupport::_delete_lock_wait_max = 0;
  54 
  55 // Flag to indicate when an _delete_lock->notify() is needed.
  56 // Impl note: See _delete_lock_wait_cnt note.
  57 volatile uint         ThreadsSMRSupport::_delete_notify = 0;
  58 
  59 // # of threads deleted over VM lifetime.
  60 // Impl note: Atomically incremented over VM lifetime so use unsigned for more
  61 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
  62 // isn't available everywhere (or is it?).
  63 volatile uint         ThreadsSMRSupport::_deleted_thread_cnt = 0;
  64 
  65 // Max time in millis to delete a thread.
  66 // Impl note: 16-bit might be too small on an overloaded machine. Use
  67 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
  68 // loop for correctness.
  69 volatile uint         ThreadsSMRSupport::_deleted_thread_time_max = 0;
  70 
  71 // Cumulative time in millis to delete threads.
  72 // Impl note: Atomically added to over VM lifetime so use unsigned for more
  73 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
  74 // isn't available everywhere (or is it?).
  75 volatile uint         ThreadsSMRSupport::_deleted_thread_times = 0;
  76 
  77 ThreadsList* volatile ThreadsSMRSupport::_java_thread_list = new ThreadsList(0);
  78 
  79 // # of ThreadsLists allocated over VM lifetime.
  80 // Impl note: We allocate a new ThreadsList for every thread create and
  81 // every thread delete so we need a bigger type than the
  82 // _deleted_thread_cnt field.
  83 uint64_t              ThreadsSMRSupport::_java_thread_list_alloc_cnt = 1;
  84 
  85 // # of ThreadsLists freed over VM lifetime.
  86 // Impl note: See _java_thread_list_alloc_cnt note.
  87 uint64_t              ThreadsSMRSupport::_java_thread_list_free_cnt = 0;
  88 
  89 // Max size ThreadsList allocated.
  90 // Impl note: Max # of threads alive at one time should fit in unsigned 32-bit.
  91 uint                  ThreadsSMRSupport::_java_thread_list_max = 0;
  92 
  93 // Max # of nested ThreadsLists for a thread.
  94 // Impl note: Hard to imagine > 64K nested ThreadsLists so this could be
  95 // 16-bit, but there is no nice 16-bit _FORMAT support.
  96 uint                  ThreadsSMRSupport::_nested_thread_list_max = 0;
  97 
  98 // # of ThreadsListHandles deleted over VM lifetime.
  99 // Impl note: Atomically incremented over VM lifetime so use unsigned for
 100 // more range. There will be fewer ThreadsListHandles than threads so
 101 // unsigned 32-bit should be fine.
 102 volatile uint         ThreadsSMRSupport::_tlh_cnt = 0;
 103 
 104 // Max time in millis to delete a ThreadsListHandle.
 105 // Impl note: 16-bit might be too small on an overloaded machine. Use
 106 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
 107 // loop for correctness.
 108 volatile uint         ThreadsSMRSupport::_tlh_time_max = 0;
 109 
 110 // Cumulative time in millis to delete ThreadsListHandles.
 111 // Impl note: Atomically added to over VM lifetime so use unsigned for more
 112 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
 113 // isn't available everywhere (or is it?).
 114 volatile uint         ThreadsSMRSupport::_tlh_times = 0;
 115 
 116 ThreadsList*          ThreadsSMRSupport::_to_delete_list = NULL;
 117 
 118 // # of parallel ThreadsLists on the to-delete list.
 119 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
 120 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
 121 uint                  ThreadsSMRSupport::_to_delete_list_cnt = 0;
 122 
 123 // Max # of parallel ThreadsLists on the to-delete list.
 124 // Impl note: See _to_delete_list_cnt note.
 125 uint                  ThreadsSMRSupport::_to_delete_list_max = 0;
 126 
 127 
 128 // 'inline' functions first so the definitions are before first use:
 129 
 130 inline void ThreadsSMRSupport::add_deleted_thread_times(uint add_value) {
 131   Atomic::add(add_value, &_deleted_thread_times);
 132 }
 133 
 134 inline void ThreadsSMRSupport::inc_deleted_thread_cnt() {
 135   Atomic::inc(&_deleted_thread_cnt);
 136 }
 137 
 138 inline void ThreadsSMRSupport::inc_java_thread_list_alloc_cnt() {
 139   _java_thread_list_alloc_cnt++;
 140 }
 141 
 142 inline void ThreadsSMRSupport::update_deleted_thread_time_max(uint new_value) {
 143   while (true) {
 144     uint cur_value = _deleted_thread_time_max;
 145     if (new_value <= cur_value) {
 146       // No need to update max value so we're done.
 147       break;
 148     }
 149     if (Atomic::cmpxchg(new_value, &_deleted_thread_time_max, cur_value) == cur_value) {
 150       // Updated max value so we're done. Otherwise try it all again.
 151       break;
 152     }
 153   }
 154 }
 155 
 156 inline void ThreadsSMRSupport::update_java_thread_list_max(uint new_value) {
 157   if (new_value > _java_thread_list_max) {
 158     _java_thread_list_max = new_value;
 159   }
 160 }
 161 
 162 inline ThreadsList* ThreadsSMRSupport::xchg_java_thread_list(ThreadsList* new_list) {
 163   return (ThreadsList*)Atomic::xchg(new_list, &_java_thread_list);
 164 }
 165 
 166 
 167 // Hash table of pointers found by a scan. Used for collecting hazard
 168 // pointers (ThreadsList references). Also used for collecting JavaThreads
 169 // that are indirectly referenced by hazard ptrs. An instance of this
 170 // class only contains one type of pointer.
 171 //
 172 class ThreadScanHashtable : public CHeapObj<mtThread> {
 173  private:
 174   static bool ptr_equals(void * const& s1, void * const& s2) {
 175     return s1 == s2;
 176   }
 177 
 178   static unsigned int ptr_hash(void * const& s1) {
 179     // 2654435761 = 2^32 * Phi (golden ratio)
 180     return (unsigned int)(((uint32_t)(uintptr_t)s1) * 2654435761u);
 181   }
 182 
 183   int _table_size;
 184   // ResourceHashtable SIZE is specified at compile time so our
 185   // dynamic _table_size is unused for now; 1031 is the first prime
 186   // after 1024.
 187   typedef ResourceHashtable<void *, int, &ThreadScanHashtable::ptr_hash,
 188                             &ThreadScanHashtable::ptr_equals, 1031,
 189                             ResourceObj::C_HEAP, mtThread> PtrTable;
 190   PtrTable * _ptrs;
 191 
 192  public:
 193   // ResourceHashtable is passed to various functions and populated in
 194   // different places so we allocate it using C_HEAP to make it immune
 195   // from any ResourceMarks that happen to be in the code paths.
 196   ThreadScanHashtable(int table_size) : _table_size(table_size), _ptrs(new (ResourceObj::C_HEAP, mtThread) PtrTable()) {}
 197 
 198   ~ThreadScanHashtable() { delete _ptrs; }
 199 
 200   bool has_entry(void *pointer) {
 201     int *val_ptr = _ptrs->get(pointer);
 202     return val_ptr != NULL && *val_ptr == 1;
 203   }
 204 
 205   void add_entry(void *pointer) {
 206     _ptrs->put(pointer, 1);
 207   }
 208 };
 209 
 210 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
 211 // (ThreadsList references) into a hash table. This closure handles part 2
 212 // of the dance - adding all the JavaThreads referenced by the hazard
 213 // pointer (ThreadsList reference) to the hash table.
 214 //
 215 class AddThreadHazardPointerThreadClosure : public ThreadClosure {
 216  private:
 217   ThreadScanHashtable *_table;
 218 
 219  public:
 220   AddThreadHazardPointerThreadClosure(ThreadScanHashtable *table) : _table(table) {}
 221 
 222   virtual void do_thread(Thread *thread) {
 223     if (!_table->has_entry((void*)thread)) {
 224       // The same JavaThread might be on more than one ThreadsList or
 225       // more than one thread might be using the same ThreadsList. In
 226       // either case, we only need a single entry for a JavaThread.
 227       _table->add_entry((void*)thread);
 228     }
 229   }
 230 };
 231 
 232 // Closure to gather JavaThreads indirectly referenced by hazard ptrs
 233 // (ThreadsList references) into a hash table. This closure handles part 1
 234 // of the dance - hazard ptr chain walking and dispatch to another
 235 // closure.
 236 //
 237 class ScanHazardPtrGatherProtectedThreadsClosure : public ThreadClosure {
 238  private:
 239   ThreadScanHashtable *_table;
 240  public:
 241   ScanHazardPtrGatherProtectedThreadsClosure(ThreadScanHashtable *table) : _table(table) {}
 242 
 243   virtual void do_thread(Thread *thread) {
 244     assert_locked_or_safepoint(Threads_lock);
 245 
 246     if (thread == NULL) return;
 247 
 248     // This code races with ThreadsSMRSupport::acquire_stable_list() which
 249     // is lock-free so we have to handle some special situations.
 250     //
 251     ThreadsList *current_list = NULL;
 252     while (true) {
 253       current_list = thread->get_threads_hazard_ptr();
 254       // No hazard ptr so nothing more to do.
 255       if (current_list == NULL) {
 256         return;
 257       }
 258 
 259       // If the hazard ptr is verified as stable (since it is not tagged),
 260       // then it is safe to use.
 261       if (!Thread::is_hazard_ptr_tagged(current_list)) break;
 262 
 263       // The hazard ptr is tagged as not yet verified as being stable
 264       // so we are racing with acquire_stable_list(). This exchange
 265       // attempts to invalidate the hazard ptr. If we win the race,
 266       // then we can ignore this unstable hazard ptr and the other
 267       // thread will retry the attempt to publish a stable hazard ptr.
 268       // If we lose the race, then we retry our attempt to look at the
 269       // hazard ptr.
 270       if (thread->cmpxchg_threads_hazard_ptr(NULL, current_list) == current_list) return;
 271     }
 272 
 273     // The current JavaThread has a hazard ptr (ThreadsList reference)
 274     // which might be _java_thread_list or it might be an older
 275     // ThreadsList that has been removed but not freed. In either case,
 276     // the hazard ptr is protecting all the JavaThreads on that
 277     // ThreadsList.
 278     AddThreadHazardPointerThreadClosure add_cl(_table);
 279     current_list->threads_do(&add_cl);
 280   }
 281 };
 282 
 283 // Closure to gather hazard ptrs (ThreadsList references) into a hash table.
 284 //
 285 class ScanHazardPtrGatherThreadsListClosure : public ThreadClosure {
 286  private:
 287   ThreadScanHashtable *_table;
 288  public:
 289   ScanHazardPtrGatherThreadsListClosure(ThreadScanHashtable *table) : _table(table) {}
 290 
 291   virtual void do_thread(Thread* thread) {
 292     assert_locked_or_safepoint(Threads_lock);
 293 
 294     if (thread == NULL) return;
 295     ThreadsList *threads = thread->get_threads_hazard_ptr();
 296     if (threads == NULL) {
 297       return;
 298     }
 299     // In this closure we always ignore the tag that might mark this
 300     // hazard ptr as not yet verified. If we happen to catch an
 301     // unverified hazard ptr that is subsequently discarded (not
 302     // published), then the only side effect is that we might keep a
 303     // to-be-deleted ThreadsList alive a little longer.
 304     threads = Thread::untag_hazard_ptr(threads);
 305     if (!_table->has_entry((void*)threads)) {
 306       _table->add_entry((void*)threads);
 307     }
 308   }
 309 };
 310 
 311 // Closure to print JavaThreads that have a hazard ptr (ThreadsList
 312 // reference) that contains an indirect reference to a specific JavaThread.
 313 //
 314 class ScanHazardPtrPrintMatchingThreadsClosure : public ThreadClosure {
 315  private:
 316   JavaThread *_thread;
 317  public:
 318   ScanHazardPtrPrintMatchingThreadsClosure(JavaThread *thread) : _thread(thread) {}
 319 
 320   virtual void do_thread(Thread *thread) {
 321     assert_locked_or_safepoint(Threads_lock);
 322 
 323     if (thread == NULL) return;
 324     ThreadsList *current_list = thread->get_threads_hazard_ptr();
 325     if (current_list == NULL) {
 326       return;
 327     }
 328     // If the hazard ptr is unverified, then ignore it.
 329     if (Thread::is_hazard_ptr_tagged(current_list)) return;
 330 
 331     // The current JavaThread has a hazard ptr (ThreadsList reference)
 332     // which might be _java_thread_list or it might be an older
 333     // ThreadsList that has been removed but not freed. In either case,
 334     // the hazard ptr is protecting all the JavaThreads on that
 335     // ThreadsList, but we only care about matching a specific JavaThread.
 336     JavaThreadIterator jti(current_list);
 337     for (JavaThread *p = jti.first(); p != NULL; p = jti.next()) {
 338       if (p == _thread) {
 339         log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread1=" INTPTR_FORMAT " has a hazard pointer for thread2=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread), p2i(_thread));
 340         break;
 341       }
 342     }
 343   }
 344 };
 345 
 346 // Closure to determine if the specified JavaThread is found by
 347 // threads_do().
 348 //
 349 class VerifyHazardPtrThreadClosure : public ThreadClosure {
 350  private:
 351   bool _found;
 352   Thread *_self;
 353 
 354  public:
 355   VerifyHazardPtrThreadClosure(Thread *self) : _found(false), _self(self) {}
 356 
 357   bool found() const { return _found; }
 358 
 359   virtual void do_thread(Thread *thread) {
 360     if (thread == _self) {
 361       _found = true;
 362     }
 363   }
 364 };
 365 
 366 
 367 // Acquire a stable ThreadsList.
 368 //
 369 void SafeThreadsListPtr::acquire_stable_list() {
 370   assert(_thread != NULL, "sanity check");
 371   _needs_release = true;
 372   _previous = _thread->_threads_list_ptr;
 373   _thread->_threads_list_ptr = this;
 374 
 375   if (_thread->get_threads_hazard_ptr() == NULL) {
 376     // The typical case is first.
 377     acquire_stable_list_fast_path();
 378     return;
 379   }
 380 
 381   // The nested case is rare.
 382   acquire_stable_list_nested_path();
 383 }
 384 
 385 // Fast path way to acquire a stable ThreadsList.
 386 //
 387 void SafeThreadsListPtr::acquire_stable_list_fast_path() {
 388   assert(_thread != NULL, "sanity check");
 389   assert(_thread->get_threads_hazard_ptr() == NULL, "sanity check");
 390 
 391   ThreadsList* threads;
 392 
 393   // Stable recording of a hazard ptr for SMR. This code does not use
 394   // locks so its use of the _smr_java_thread_list & _threads_hazard_ptr
 395   // fields is racy relative to code that uses those fields with locks.
 396   // OrderAccess and Atomic functions are used to deal with those races.
 397   //
 398   while (true) {
 399     threads = ThreadsSMRSupport::get_java_thread_list();
 400 
 401     // Publish a tagged hazard ptr to denote that the hazard ptr is not
 402     // yet verified as being stable. Due to the fence after the hazard
 403     // ptr write, it will be sequentially consistent w.r.t. the
 404     // sequentially consistent writes of the ThreadsList, even on
 405     // non-multiple copy atomic machines where stores can be observed
 406     // in different order from different observer threads.
 407     ThreadsList* unverified_threads = Thread::tag_hazard_ptr(threads);
 408     _thread->set_threads_hazard_ptr(unverified_threads);
 409 
 410     // If _smr_java_thread_list has changed, we have lost a race with
 411     // Threads::add() or Threads::remove() and have to try again.
 412     if (ThreadsSMRSupport::get_java_thread_list() != threads) {
 413       continue;
 414     }
 415 
 416     // We try to remove the tag which will verify the hazard ptr as
 417     // being stable. This exchange can race with a scanning thread
 418     // which might invalidate the tagged hazard ptr to keep it from
 419     // being followed to access JavaThread ptrs. If we lose the race,
 420     // we simply retry. If we win the race, then the stable hazard
 421     // ptr is officially published.
 422     if (_thread->cmpxchg_threads_hazard_ptr(threads, unverified_threads) == unverified_threads) {
 423       break;
 424     }
 425   }
 426 
 427   // A stable hazard ptr has been published letting other threads know
 428   // that the ThreadsList and the JavaThreads reachable from this list
 429   // are protected and hence they should not be deleted until everyone
 430   // agrees it is safe to do so.
 431 
 432   _list = threads;
 433 
 434   verify_hazard_ptr_scanned();
 435 }
 436 
 437 // Acquire a nested stable ThreadsList; this is rare so it uses
 438 // reference counting.
 439 //
 440 void SafeThreadsListPtr::acquire_stable_list_nested_path() {
 441   assert(_thread != NULL, "sanity check");
 442   assert(_thread->get_threads_hazard_ptr() != NULL,
 443          "cannot have a NULL regular hazard ptr when acquiring a nested hazard ptr");
 444 
 445   // The thread already has a hazard ptr (ThreadsList ref) so we need
 446   // to create a nested ThreadsListHandle with the current ThreadsList
 447   // since it might be different than our current hazard ptr. To remedy
 448   // the situation, the ThreadsList pointed to by the pre-existing
 449   // stable hazard ptr is reference counted before the hazard ptr may
 450   // be released and moved to a new ThreadsList. The old ThreadsList
 451   // is remembered in the ThreadsListHandle.
 452 
 453   ThreadsList* current_list = _previous->_list;
 454   if (EnableThreadSMRStatistics) {
 455     _thread->inc_nested_threads_hazard_ptr_cnt();
 456   }
 457   current_list->inc_nested_handle_cnt();
 458   _previous->_has_ref_count = true;  // promote SafeThreadsListPtr to be reference counted
 459   _thread->_threads_hazard_ptr = NULL;  // clear the hazard ptr so we can go through the fast path below
 460 
 461   if (EnableThreadSMRStatistics && _thread->nested_threads_hazard_ptr_cnt() > ThreadsSMRSupport::_nested_thread_list_max) {
 462     ThreadsSMRSupport::_nested_thread_list_max = _thread->nested_threads_hazard_ptr_cnt();
 463   }
 464 
 465   acquire_stable_list_fast_path();
 466 
 467   verify_hazard_ptr_scanned();
 468 
 469   log_debug(thread, smr)("tid=" UINTX_FORMAT ": SafeThreadsListPtr::acquire_stable_list: add nested list pointer to ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(_list));
 470 }
 471 
 472 // Release a stable ThreadsList.
 473 //
 474 void SafeThreadsListPtr::release_stable_list() {
 475   assert(_thread != NULL, "sanity check");
 476   assert(_thread->_threads_list_ptr == this, "sanity check");
 477   _thread->_threads_list_ptr = _previous;
 478 
 479   if (_has_ref_count) {
 480     // If a SafeThreadsListPtr has been promoted to use reference counting
 481     // due to nesting of ThreadsListHandles, then the reference count must be
 482     // decremented, at which point it may be freed. The forgotten value of
 483     // the list no longer matters at this point and should already be NULL.
 484     assert(_thread->get_threads_hazard_ptr() == NULL, "sanity check");
 485     if (EnableThreadSMRStatistics) {
 486       _thread->dec_nested_threads_hazard_ptr_cnt();
 487     }
 488     _list->dec_nested_handle_cnt();
 489 
 490     log_debug(thread, smr)("tid=" UINTX_FORMAT ": SafeThreadsListPtr::release_stable_list: delete nested list pointer to ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(_list));
 491   } else {
 492     // The normal case: a leaf ThreadsListHandle. This merely requires setting
 493     // the thread hazard ptr back to NULL.
 494     assert(_thread->get_threads_hazard_ptr() != NULL, "sanity check");
 495     _thread->set_threads_hazard_ptr(NULL);
 496   }
 497 
 498   // After releasing the hazard ptr, other threads may go ahead and
 499   // free up some memory temporarily used by a ThreadsList snapshot.
 500 
 501   // We use double-check locking to reduce traffic on the system
 502   // wide Thread-SMR delete_lock.
 503   if (ThreadsSMRSupport::delete_notify()) {
 504     // An exiting thread might be waiting in smr_delete(); we need to
 505     // check with delete_lock to be sure.
 506     ThreadsSMRSupport::release_stable_list_wake_up(_has_ref_count);
 507   }
 508 }
 509 
 510 // Verify that the stable hazard ptr used to safely keep threads
 511 // alive is scanned by threads_do() which is a key piece of honoring
 512 // the Thread-SMR protocol.
 513 void SafeThreadsListPtr::verify_hazard_ptr_scanned() {
 514 #ifdef ASSERT
 515   assert(_list != NULL, "_list must not be NULL");
 516 
 517   // The closure will attempt to verify that the calling thread can
 518   // be found by threads_do() on the specified ThreadsList. If it
 519   // is successful, then the specified ThreadsList was acquired as
 520   // a stable hazard ptr by the calling thread in a way that honored
 521   // the Thread-SMR protocol.
 522   //
 523   // If the calling thread cannot be found by threads_do() and if
 524   // it is not the shutdown thread, then the calling thread is not
 525   // honoring the Thread-SMR ptotocol. This means that the specified
 526   // ThreadsList is not a stable hazard ptr and can be freed by
 527   // another thread from the to-be-deleted list at any time.
 528   //
 529   // Note: The shutdown thread has removed itself from the Threads
 530   // list and is safe to have a waiver from this check because
 531   // VM_Exit::_shutdown_thread is not set until after the VMThread
 532   // has started the final safepoint which holds the Threads_lock
 533   // for the remainder of the VM's life.
 534   //
 535   VerifyHazardPtrThreadClosure cl(_thread);
 536   ThreadsSMRSupport::threads_do(&cl, _list);
 537 
 538   // If the calling thread is not honoring the Thread-SMR protocol,
 539   // then we will either crash in threads_do() above because 'threads'
 540   // was freed by another thread or we will fail the assert() below.
 541   // In either case, we won't get past this point with a badly placed
 542   // ThreadsListHandle.
 543 
 544   assert(cl.found() || _thread == VM_Exit::shutdown_thread(), "Acquired a ThreadsList snapshot from a thread not recognized by the Thread-SMR protocol.");
 545 #endif
 546 }
 547 
 548 // 'entries + 1' so we always have at least one entry.
 549 ThreadsList::ThreadsList(int entries) :
 550   _length(entries),
 551   _next_list(NULL),
 552   _threads(NEW_C_HEAP_ARRAY(JavaThread*, entries + 1, mtThread)),
 553   _nested_handle_cnt(0)
 554 {
 555   *(JavaThread**)(_threads + entries) = NULL;  // Make sure the extra entry is NULL.
 556 }
 557 
 558 ThreadsList::~ThreadsList() {
 559   FREE_C_HEAP_ARRAY(JavaThread*, _threads);
 560 }
 561 
 562 // Add a JavaThread to a ThreadsList. The returned ThreadsList is a
 563 // new copy of the specified ThreadsList with the specified JavaThread
 564 // appended to the end.
 565 ThreadsList *ThreadsList::add_thread(ThreadsList *list, JavaThread *java_thread) {
 566   const uint index = list->_length;
 567   const uint new_length = index + 1;
 568   const uint head_length = index;
 569   ThreadsList *const new_list = new ThreadsList(new_length);
 570 
 571   if (head_length > 0) {
 572     Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
 573   }
 574   *(JavaThread**)(new_list->_threads + index) = java_thread;
 575 
 576   return new_list;
 577 }
 578 
 579 void ThreadsList::dec_nested_handle_cnt() {
 580   // The decrement needs to be MO_ACQ_REL. At the moment, the Atomic::dec
 581   // backend on PPC does not yet conform to these requirements. Therefore
 582   // the decrement is simulated with an Atomic::sub(1, &addr).
 583   // Without this MO_ACQ_REL Atomic::dec simulation, the nested SMR mechanism
 584   // is not generally safe to use.
 585   Atomic::sub(1, &_nested_handle_cnt);
 586 }
 587 
 588 int ThreadsList::find_index_of_JavaThread(JavaThread *target) {
 589   if (target == NULL) {
 590     return -1;
 591   }
 592   for (uint i = 0; i < length(); i++) {
 593     if (target == thread_at(i)) {
 594       return (int)i;
 595     }
 596   }
 597   return -1;
 598 }
 599 
 600 JavaThread* ThreadsList::find_JavaThread_from_java_tid(jlong java_tid) const {
 601   for (uint i = 0; i < length(); i++) {
 602     JavaThread* thread = thread_at(i);
 603     oop tobj = thread->threadObj();
 604     // Ignore the thread if it hasn't run yet, has exited
 605     // or is starting to exit.
 606     if (tobj != NULL && !thread->is_exiting() &&
 607         java_tid == java_lang_Thread::thread_id(tobj)) {
 608       // found a match
 609       return thread;
 610     }
 611   }
 612   return NULL;
 613 }
 614 
 615 void ThreadsList::inc_nested_handle_cnt() {
 616   // The increment needs to be MO_SEQ_CST. At the moment, the Atomic::inc
 617   // backend on PPC does not yet conform to these requirements. Therefore
 618   // the increment is simulated with a load phi; cas phi + 1; loop.
 619   // Without this MO_SEQ_CST Atomic::inc simulation, the nested SMR mechanism
 620   // is not generally safe to use.
 621   intx sample = OrderAccess::load_acquire(&_nested_handle_cnt);
 622   for (;;) {
 623     if (Atomic::cmpxchg(sample + 1, &_nested_handle_cnt, sample) == sample) {
 624       return;
 625     } else {
 626       sample = OrderAccess::load_acquire(&_nested_handle_cnt);
 627     }
 628   }
 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 }
 642 
 643 // Remove a JavaThread from a ThreadsList. The returned ThreadsList is a
 644 // new copy of the specified ThreadsList with the specified JavaThread
 645 // removed.
 646 ThreadsList *ThreadsList::remove_thread(ThreadsList* list, JavaThread* java_thread) {
 647   assert(list->_length > 0, "sanity");
 648 
 649   uint i = (uint)list->find_index_of_JavaThread(java_thread);
 650   assert(i < list->_length, "did not find JavaThread on the list");
 651   const uint index = i;
 652   const uint new_length = list->_length - 1;
 653   const uint head_length = index;
 654   const uint tail_length = (new_length >= index) ? (new_length - index) : 0;
 655   ThreadsList *const new_list = new ThreadsList(new_length);
 656 
 657   if (head_length > 0) {
 658     Copy::disjoint_words((HeapWord*)list->_threads, (HeapWord*)new_list->_threads, head_length);
 659   }
 660   if (tail_length > 0) {
 661     Copy::disjoint_words((HeapWord*)list->_threads + index + 1, (HeapWord*)new_list->_threads + index, tail_length);
 662   }
 663 
 664   return new_list;
 665 }
 666 
 667 ThreadsListHandle::ThreadsListHandle(Thread *self) : _list_ptr(self, /* acquire */ true) {
 668   assert(self == Thread::current(), "sanity check");
 669   if (EnableThreadSMRStatistics) {
 670     _timer.start();
 671   }
 672 }
 673 
 674 ThreadsListHandle::~ThreadsListHandle() {
 675   if (EnableThreadSMRStatistics) {
 676     _timer.stop();
 677     uint millis = (uint)_timer.milliseconds();
 678     ThreadsSMRSupport::update_tlh_stats(millis);
 679   }
 680 }
 681 
 682 // Convert an internal thread reference to a JavaThread found on the
 683 // associated ThreadsList. This ThreadsListHandle "protects" the
 684 // returned JavaThread *.
 685 //
 686 // If thread_oop_p is not NULL, then the caller wants to use the oop
 687 // after this call so the oop is returned. On success, *jt_pp is set
 688 // to the converted JavaThread * and true is returned. On error,
 689 // returns false.
 690 //
 691 bool ThreadsListHandle::cv_internal_thread_to_JavaThread(jobject jthread,
 692                                                          JavaThread ** jt_pp,
 693                                                          oop * thread_oop_p) {
 694   assert(this->list() != NULL, "must have a ThreadsList");
 695   assert(jt_pp != NULL, "must have a return JavaThread pointer");
 696   // thread_oop_p is optional so no assert()
 697 
 698   // The JVM_* interfaces don't allow a NULL thread parameter; JVM/TI
 699   // allows a NULL thread parameter to signify "current thread" which
 700   // allows us to avoid calling cv_external_thread_to_JavaThread().
 701   // The JVM_* interfaces have no such leeway.
 702 
 703   oop thread_oop = JNIHandles::resolve_non_null(jthread);
 704   // Looks like an oop at this point.
 705   if (thread_oop_p != NULL) {
 706     // Return the oop to the caller; the caller may still want
 707     // the oop even if this function returns false.
 708     *thread_oop_p = thread_oop;
 709   }
 710 
 711   JavaThread *java_thread = java_lang_Thread::thread(thread_oop);
 712   if (java_thread == NULL) {
 713     // The java.lang.Thread does not contain a JavaThread * so it has
 714     // not yet run or it has died.
 715     return false;
 716   }
 717   // Looks like a live JavaThread at this point.
 718 
 719   if (java_thread != JavaThread::current()) {
 720     // jthread is not for the current JavaThread so have to verify
 721     // the JavaThread * against the ThreadsList.
 722     if (EnableThreadSMRExtraValidityChecks && !includes(java_thread)) {
 723       // Not on the JavaThreads list so it is not alive.
 724       return false;
 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
 765 // in the chain may get deleted by this call if they are no longer in-use.
 766 void ThreadsSMRSupport::free_list(ThreadsList* threads) {
 767   assert_locked_or_safepoint(Threads_lock);
 768 
 769   threads->set_next_list(_to_delete_list);
 770   _to_delete_list = threads;
 771   if (EnableThreadSMRStatistics) {
 772     _to_delete_list_cnt++;
 773     if (_to_delete_list_cnt > _to_delete_list_max) {
 774       _to_delete_list_max = _to_delete_list_cnt;
 775     }
 776   }
 777 
 778   // Hash table size should be first power of two higher than twice the length of the ThreadsList
 779   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 780   hash_table_size--;
 781   hash_table_size |= hash_table_size >> 1;
 782   hash_table_size |= hash_table_size >> 2;
 783   hash_table_size |= hash_table_size >> 4;
 784   hash_table_size |= hash_table_size >> 8;
 785   hash_table_size |= hash_table_size >> 16;
 786   hash_table_size++;
 787 
 788   // Gather a hash table of the current hazard ptrs:
 789   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 790   ScanHazardPtrGatherThreadsListClosure scan_cl(scan_table);
 791   threads_do(&scan_cl);
 792   OrderAccess::acquire(); // Must order reads of hazard ptr before reads of
 793                           // nested reference counters
 794 
 795   // Walk through the linked list of pending freeable ThreadsLists
 796   // and free the ones that are not referenced from hazard ptrs.
 797   ThreadsList* current = _to_delete_list;
 798   ThreadsList* prev = NULL;
 799   ThreadsList* next = NULL;
 800   bool threads_is_freed = false;
 801   while (current != NULL) {
 802     next = current->next_list();
 803     if (!scan_table->has_entry((void*)current) && current->_nested_handle_cnt == 0) {
 804       // This ThreadsList is not referenced by a hazard ptr.
 805       if (prev != NULL) {
 806         prev->set_next_list(next);
 807       }
 808       if (_to_delete_list == current) {
 809         _to_delete_list = next;
 810       }
 811 
 812       log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is freed.", os::current_thread_id(), p2i(current));
 813       if (current == threads) threads_is_freed = true;
 814       delete current;
 815       if (EnableThreadSMRStatistics) {
 816         _java_thread_list_free_cnt++;
 817         _to_delete_list_cnt--;
 818       }
 819     } else {
 820       prev = current;
 821     }
 822     current = next;
 823   }
 824 
 825   if (!threads_is_freed) {
 826     // Only report "is not freed" on the original call to
 827     // free_list() for this ThreadsList.
 828     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::free_list: threads=" INTPTR_FORMAT " is not freed.", os::current_thread_id(), p2i(threads));
 829   }
 830 
 831   delete scan_table;
 832 }
 833 
 834 // Return true if the specified JavaThread is protected by a hazard
 835 // pointer (ThreadsList reference). Otherwise, returns false.
 836 //
 837 bool ThreadsSMRSupport::is_a_protected_JavaThread(JavaThread *thread) {
 838   assert_locked_or_safepoint(Threads_lock);
 839 
 840   // Hash table size should be first power of two higher than twice
 841   // the length of the Threads list.
 842   int hash_table_size = MIN2((int)get_java_thread_list()->length(), 32) << 1;
 843   hash_table_size--;
 844   hash_table_size |= hash_table_size >> 1;
 845   hash_table_size |= hash_table_size >> 2;
 846   hash_table_size |= hash_table_size >> 4;
 847   hash_table_size |= hash_table_size >> 8;
 848   hash_table_size |= hash_table_size >> 16;
 849   hash_table_size++;
 850 
 851   // Gather a hash table of the JavaThreads indirectly referenced by
 852   // hazard ptrs.
 853   ThreadScanHashtable *scan_table = new ThreadScanHashtable(hash_table_size);
 854   ScanHazardPtrGatherProtectedThreadsClosure scan_cl(scan_table);
 855   threads_do(&scan_cl);
 856   OrderAccess::acquire(); // Must order reads of hazard ptr before reads of
 857                           // nested reference counters
 858 
 859   // Walk through the linked list of pending freeable ThreadsLists
 860   // and include the ones that are currently in use by a nested
 861   // ThreadsListHandle in the search set.
 862   ThreadsList* current = _to_delete_list;
 863   while (current != NULL) {
 864     if (current->_nested_handle_cnt != 0) {
 865       // 'current' is in use by a nested ThreadsListHandle so the hazard
 866       // ptr is protecting all the JavaThreads on that ThreadsList.
 867       AddThreadHazardPointerThreadClosure add_cl(scan_table);
 868       current->threads_do(&add_cl);
 869     }
 870     current = current->next_list();
 871   }
 872 
 873   bool thread_is_protected = false;
 874   if (scan_table->has_entry((void*)thread)) {
 875     thread_is_protected = true;
 876   }
 877   delete scan_table;
 878   return thread_is_protected;
 879 }
 880 
 881 // Wake up portion of the release stable ThreadsList protocol;
 882 // uses the delete_lock().
 883 //
 884 void ThreadsSMRSupport::release_stable_list_wake_up(bool is_nested) {
 885   const char* log_str = is_nested ? "nested hazard ptr" : "regular hazard ptr";
 886 
 887   // Note: delete_lock is held in smr_delete() for the entire
 888   // hazard ptr search so that we do not lose this notify() if
 889   // the exiting thread has to wait. That code path also holds
 890   // Threads_lock (which was grabbed before delete_lock) so that
 891   // threads_do() can be called. This means the system can't start a
 892   // safepoint which means this thread can't take too long to get to
 893   // a safepoint because of being blocked on delete_lock.
 894   //
 895   MonitorLockerEx ml(ThreadsSMRSupport::delete_lock(), Monitor::_no_safepoint_check_flag);
 896   if (ThreadsSMRSupport::delete_notify()) {
 897     // Notify any exiting JavaThreads that are waiting in smr_delete()
 898     // that we've released a ThreadsList.
 899     ml.notify_all();
 900     log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::release_stable_list notified %s", os::current_thread_id(), log_str);
 901   }
 902 }
 903 
 904 void ThreadsSMRSupport::remove_thread(JavaThread *thread) {
 905   ThreadsList *new_list = ThreadsList::remove_thread(ThreadsSMRSupport::get_java_thread_list(), thread);
 906   if (EnableThreadSMRStatistics) {
 907     ThreadsSMRSupport::inc_java_thread_list_alloc_cnt();
 908     // This list is smaller so no need to check for a "longest" update.
 909   }
 910 
 911   // Final _java_thread_list will not generate a "Threads::remove" mesg.
 912   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::remove: new ThreadsList=" INTPTR_FORMAT, os::current_thread_id(), p2i(new_list));
 913 
 914   ThreadsList *old_list = ThreadsSMRSupport::xchg_java_thread_list(new_list);
 915   ThreadsSMRSupport::free_list(old_list);
 916 }
 917 
 918 // See note for clear_delete_notify().
 919 //
 920 void ThreadsSMRSupport::set_delete_notify() {
 921   Atomic::inc(&_delete_notify);
 922 }
 923 
 924 // Safely delete a JavaThread when it is no longer in use by a
 925 // ThreadsListHandle.
 926 //
 927 void ThreadsSMRSupport::smr_delete(JavaThread *thread) {
 928   assert(!Threads_lock->owned_by_self(), "sanity");
 929 
 930   bool has_logged_once = false;
 931   elapsedTimer timer;
 932   if (EnableThreadSMRStatistics) {
 933     timer.start();
 934   }
 935 
 936   while (true) {
 937     {
 938       // No safepoint check because this JavaThread is not on the
 939       // Threads list.
 940       MutexLockerEx ml(Threads_lock, Mutex::_no_safepoint_check_flag);
 941       // Cannot use a MonitorLockerEx helper here because we have
 942       // to drop the Threads_lock first if we wait.
 943       ThreadsSMRSupport::delete_lock()->lock_without_safepoint_check();
 944       // Set the delete_notify flag after we grab delete_lock
 945       // and before we scan hazard ptrs because we're doing
 946       // double-check locking in release_stable_list().
 947       ThreadsSMRSupport::set_delete_notify();
 948 
 949       if (!is_a_protected_JavaThread(thread)) {
 950         // This is the common case.
 951         ThreadsSMRSupport::clear_delete_notify();
 952         ThreadsSMRSupport::delete_lock()->unlock();
 953         break;
 954       }
 955       if (!has_logged_once) {
 956         has_logged_once = true;
 957         log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is not deleted.", os::current_thread_id(), p2i(thread));
 958         if (log_is_enabled(Debug, os, thread)) {
 959           ScanHazardPtrPrintMatchingThreadsClosure scan_cl(thread);
 960           threads_do(&scan_cl);
 961           ThreadsList* current = _to_delete_list;
 962           while (current != NULL) {
 963             if (current->_nested_handle_cnt != 0 && current->includes(thread)) {
 964               log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: found nested hazard pointer to thread=" INTPTR_FORMAT, os::current_thread_id(), p2i(thread));
 965             }
 966             current = current->next_list();
 967           }
 968         }
 969       }
 970     } // We have to drop the Threads_lock to wait or delete the thread
 971 
 972     if (EnableThreadSMRStatistics) {
 973       _delete_lock_wait_cnt++;
 974       if (_delete_lock_wait_cnt > _delete_lock_wait_max) {
 975         _delete_lock_wait_max = _delete_lock_wait_cnt;
 976       }
 977     }
 978     // Wait for a release_stable_list() call before we check again. No
 979     // safepoint check, no timeout, and not as suspend equivalent flag
 980     // because this JavaThread is not on the Threads list.
 981     ThreadsSMRSupport::delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
 982                                      !Mutex::_as_suspend_equivalent_flag);
 983     if (EnableThreadSMRStatistics) {
 984       _delete_lock_wait_cnt--;
 985     }
 986 
 987     ThreadsSMRSupport::clear_delete_notify();
 988     ThreadsSMRSupport::delete_lock()->unlock();
 989     // Retry the whole scenario.
 990   }
 991 
 992   if (ThreadLocalHandshakes) {
 993     // The thread is about to be deleted so cancel any handshake.
 994     thread->cancel_handshake();
 995   }
 996 
 997   delete thread;
 998   if (EnableThreadSMRStatistics) {
 999     timer.stop();
1000     uint millis = (uint)timer.milliseconds();
1001     ThreadsSMRSupport::inc_deleted_thread_cnt();
1002     ThreadsSMRSupport::add_deleted_thread_times(millis);
1003     ThreadsSMRSupport::update_deleted_thread_time_max(millis);
1004   }
1005 
1006   log_debug(thread, smr)("tid=" UINTX_FORMAT ": ThreadsSMRSupport::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
1007 }
1008 
1009 // Apply the closure to all threads in the system, with a snapshot of
1010 // all JavaThreads provided by the list parameter.
1011 void ThreadsSMRSupport::threads_do(ThreadClosure *tc, ThreadsList *list) {
1012   list->threads_do(tc);
1013   Threads::non_java_threads_do(tc);
1014 }
1015 
1016 // Apply the closure to all threads in the system.
1017 void ThreadsSMRSupport::threads_do(ThreadClosure *tc) {
1018   threads_do(tc, _java_thread_list);
1019 }
1020 
1021 
1022 // Debug, logging, and printing stuff at the end:
1023 
1024 // Print SMR info for a SafeThreadsListPtr to a given output stream.
1025 void SafeThreadsListPtr::print_on(outputStream* st) {
1026   if (this == _thread->_threads_list_ptr) {
1027     // The top level hazard ptr.
1028     st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(_list));
1029   } else {
1030     // Nested hazard ptrs.
1031     st->print(", _nested_threads_hazard_ptr=" INTPTR_FORMAT, p2i(_list));
1032   }
1033 }
1034 
1035 // Log Threads class SMR info.
1036 void ThreadsSMRSupport::log_statistics() {
1037   LogTarget(Info, thread, smr) log;
1038   if (log.is_enabled()) {
1039     LogStream out(log);
1040     print_info_on(&out);
1041   }
1042 }
1043 
1044 // Print SMR info for a thread to a given output stream.
1045 void ThreadsSMRSupport::print_info_on(const Thread* thread, outputStream* st) {
1046   if (thread->_threads_hazard_ptr != NULL) {
1047     st->print(" _threads_hazard_ptr=" INTPTR_FORMAT, p2i(thread->_threads_hazard_ptr));
1048   }
1049   if (EnableThreadSMRStatistics && thread->_threads_list_ptr != NULL) {
1050     // The count is only interesting if we have a _threads_list_ptr.
1051     st->print(", _nested_threads_hazard_ptr_cnt=%u", thread->_nested_threads_hazard_ptr_cnt);
1052   }
1053   if (SafepointSynchronize::is_at_safepoint() || Thread::current() == thread) {
1054     // It is only safe to walk the list if we're at a safepoint or the
1055     // calling thread is walking its own list.
1056     SafeThreadsListPtr* current = thread->_threads_list_ptr;
1057     if (current != NULL) {
1058       // Skip the top nesting level as it is always printed above.
1059       current = current->previous();
1060     }
1061     while (current != NULL) {
1062       current->print_on(st);
1063       current = current->previous();
1064     }
1065   }
1066 }
1067 
1068 // Print Threads class SMR info.
1069 void ThreadsSMRSupport::print_info_on(outputStream* st) {
1070   // Only grab the Threads_lock if we don't already own it and if we
1071   // are not reporting an error.
1072   // Note: Not grabbing the Threads_lock during during error reporting
1073   // is dangerous because the data structures we want to print can be
1074   // freed concurrently. However, grabbing the Threads_lock during
1075   // error reporting can be equally dangerous since this thread might
1076   // block during error reporting or a nested error could leave the
1077   // Threads_lock held. The classic no win scenario.
1078   //
1079   MutexLockerEx ml((Threads_lock->owned_by_self() || VMError::is_error_reported()) ? NULL : Threads_lock);
1080 
1081   st->print_cr("Threads class SMR info:");
1082   st->print_cr("_java_thread_list=" INTPTR_FORMAT ", length=%u, "
1083                "elements={", p2i(_java_thread_list),
1084                _java_thread_list->length());
1085   print_info_elements_on(st, _java_thread_list);
1086   st->print_cr("}");
1087   if (_to_delete_list != NULL) {
1088     st->print_cr("_to_delete_list=" INTPTR_FORMAT ", length=%u, "
1089                  "elements={", p2i(_to_delete_list),
1090                  _to_delete_list->length());
1091     print_info_elements_on(st, _to_delete_list);
1092     st->print_cr("}");
1093     for (ThreadsList *t_list = _to_delete_list->next_list();
1094          t_list != NULL; t_list = t_list->next_list()) {
1095       st->print("next-> " INTPTR_FORMAT ", length=%u, "
1096                 "elements={", p2i(t_list), t_list->length());
1097       print_info_elements_on(st, t_list);
1098       st->print_cr("}");
1099     }
1100   }
1101   if (!EnableThreadSMRStatistics) {
1102     return;
1103   }
1104   st->print_cr("_java_thread_list_alloc_cnt=" UINT64_FORMAT ", "
1105                "_java_thread_list_free_cnt=" UINT64_FORMAT ", "
1106                "_java_thread_list_max=%u, "
1107                "_nested_thread_list_max=%u",
1108                _java_thread_list_alloc_cnt,
1109                _java_thread_list_free_cnt,
1110                _java_thread_list_max,
1111                _nested_thread_list_max);
1112   if (_tlh_cnt > 0) {
1113     st->print_cr("_tlh_cnt=%u"
1114                  ", _tlh_times=%u"
1115                  ", avg_tlh_time=%0.2f"
1116                  ", _tlh_time_max=%u",
1117                  _tlh_cnt, _tlh_times,
1118                  ((double) _tlh_times / _tlh_cnt),
1119                  _tlh_time_max);
1120   }
1121   if (_deleted_thread_cnt > 0) {
1122     st->print_cr("_deleted_thread_cnt=%u"
1123                  ", _deleted_thread_times=%u"
1124                  ", avg_deleted_thread_time=%0.2f"
1125                  ", _deleted_thread_time_max=%u",
1126                  _deleted_thread_cnt, _deleted_thread_times,
1127                  ((double) _deleted_thread_times / _deleted_thread_cnt),
1128                  _deleted_thread_time_max);
1129   }
1130   st->print_cr("_delete_lock_wait_cnt=%u, _delete_lock_wait_max=%u",
1131                _delete_lock_wait_cnt, _delete_lock_wait_max);
1132   st->print_cr("_to_delete_list_cnt=%u, _to_delete_list_max=%u",
1133                _to_delete_list_cnt, _to_delete_list_max);
1134 }
1135 
1136 // Print ThreadsList elements (4 per line).
1137 void ThreadsSMRSupport::print_info_elements_on(outputStream* st, ThreadsList* t_list) {
1138   uint cnt = 0;
1139   JavaThreadIterator jti(t_list);
1140   for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
1141     st->print(INTPTR_FORMAT, p2i(jt));
1142     if (cnt < t_list->length() - 1) {
1143       // Separate with comma or comma-space except for the last one.
1144       if (((cnt + 1) % 4) == 0) {
1145         // Four INTPTR_FORMAT fit on an 80 column line so end the
1146         // current line with just a comma.
1147         st->print_cr(",");
1148       } else {
1149         // Not the last one on the current line so use comma-space:
1150         st->print(", ");
1151       }
1152     } else {
1153       // Last one so just end the current line.
1154       st->cr();
1155     }
1156     cnt++;
1157   }
1158 }