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