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