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