< prev index next >

src/hotspot/share/runtime/thread.cpp

Print this page
rev 47862 : imported patch 10.07.open.rebase_20171110.dcubed
rev 47863 : imported patch 10.08.open.rebase_20171114.rehn
rev 47864 : imported patch 10.09.open.TLH_hang_fix.rehn
rev 47865 : dholmes CR: Fix indents, trailing spaces and various typos. Add descriptions for the '_cnt', '_max' and '_times" fields, add impl notes to document the type choices.
rev 47866 : robinw CR: Fix some inefficient code, update some comments, fix some indents, and add some 'const' specifiers.


3412     // process it here to make sure it isn't unloaded in the middle of
3413     // a scan.
3414     cf->do_code_blob(_scanned_compiled_method);
3415   }
3416 }
3417 
3418 void CodeCacheSweeperThread::nmethods_do(CodeBlobClosure* cf) {
3419   JavaThread::nmethods_do(cf);
3420   if (_scanned_compiled_method != NULL && cf != NULL) {
3421     // Safepoints can occur when the sweeper is scanning an nmethod so
3422     // process it here to make sure it isn't unloaded in the middle of
3423     // a scan.
3424     cf->do_code_blob(_scanned_compiled_method);
3425   }
3426 }
3427 
3428 
3429 // ======= Threads ========
3430 
3431 // The Threads class links together all active threads, and provides
3432 // operations over all threads.  It is protected by its own Mutex
3433 // lock, which is also used in other contexts to protect thread
3434 // operations from having the thread being operated on from exiting
3435 // and going away unexpectedly (e.g., safepoint synchronization)





3436 
3437 JavaThread*           Threads::_thread_list = NULL;
3438 int                   Threads::_number_of_threads = 0;
3439 int                   Threads::_number_of_non_daemon_threads = 0;
3440 int                   Threads::_return_code = 0;
3441 int                   Threads::_thread_claim_parity = 0;
3442 size_t                JavaThread::_stack_size_at_create = 0;

3443 Monitor*              Threads::_smr_delete_lock =
3444                           new Monitor(Monitor::special, "smr_delete_lock",
3445                                       false /* allow_vm_block */,
3446                                       Monitor::_safepoint_check_never);
3447 // The '_cnt', '_max' and '_times" fields are enabled via
3448 // -XX:+EnableThreadSMRStatistics:




3449 uint                  Threads::_smr_delete_lock_wait_cnt = 0;



3450 uint                  Threads::_smr_delete_lock_wait_max = 0;
3451 volatile jint         Threads::_smr_delete_notify = 0;
3452 volatile jint         Threads::_smr_deleted_thread_cnt = 0;
3453 volatile jint         Threads::_smr_deleted_thread_time_max = 0;
3454 volatile jint         Threads::_smr_deleted_thread_times = 0;



















3455 ThreadsList* volatile Threads::_smr_java_thread_list = new ThreadsList(0);
3456 long                  Threads::_smr_java_thread_list_alloc_cnt = 1;
3457 long                  Threads::_smr_java_thread_list_free_cnt = 0;











3458 uint                  Threads::_smr_java_thread_list_max = 0;




3459 uint                  Threads::_smr_nested_thread_list_max = 0;
3460 volatile jint         Threads::_smr_tlh_cnt = 0;
3461 volatile jint         Threads::_smr_tlh_time_max = 0;
3462 volatile jint         Threads::_smr_tlh_times = 0;
















3463 ThreadsList*          Threads::_smr_to_delete_list = NULL;




3464 uint                  Threads::_smr_to_delete_list_cnt = 0;



3465 uint                  Threads::_smr_to_delete_list_max = 0;
3466 
3467 #ifdef ASSERT
3468 bool                  Threads::_vm_complete = false;
3469 #endif
3470 
3471 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3472   Prefetch::read((void*)addr, prefetch_interval);
3473   return *addr;
3474 }
3475 
3476 // Possibly the ugliest for loop the world has seen. C++ does not allow
3477 // multiple types in the declaration section of the for loop. In this case
3478 // we are only dealing with pointers and hence can cast them. It looks ugly
3479 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3480 #define DO_JAVA_THREADS(LIST, X)                                                                                          \
3481     for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes,                           \
3482              *MACRO_list = (JavaThread*)(LIST),                                                                           \
3483              **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(),  \
3484              **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(),                                     \


4809     if (EnableThreadSMRStatistics) {
4810       _smr_delete_lock_wait_cnt++;
4811       if (_smr_delete_lock_wait_cnt > _smr_delete_lock_wait_max) {
4812         _smr_delete_lock_wait_max = _smr_delete_lock_wait_cnt;
4813       }
4814     }
4815     // Wait for a release_stable_list() call before we check again. No
4816     // safepoint check, no timeout, and not as suspend equivalent flag
4817     // because this JavaThread is not on the Threads list.
4818     Threads::smr_delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
4819                                      !Mutex::_as_suspend_equivalent_flag);
4820     if (EnableThreadSMRStatistics) {
4821       _smr_delete_lock_wait_cnt--;
4822     }
4823 
4824     Threads::clear_smr_delete_notify();
4825     Threads::smr_delete_lock()->unlock();
4826     // Retry the whole scenario.
4827   }
4828 
4829   // If someone set a handshake on us just as we entered exit path, we simple cancel it.
4830   if (ThreadLocalHandshakes) {

4831     thread->cancel_handshake();
4832   }
4833 
4834   delete thread;
4835   if (EnableThreadSMRStatistics) {
4836     timer.stop();
4837     jint millis = (jint)timer.milliseconds();
4838     Threads::inc_smr_deleted_thread_cnt();
4839     Threads::add_smr_deleted_thread_times(millis);
4840     Threads::update_smr_deleted_thread_time_max(millis);
4841   }
4842 
4843   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
4844 }
4845 
4846 bool Threads::smr_delete_notify() {
4847   // Use load_acquire() in order to see any updates to _smr_delete_notify
4848   // earlier than when smr_delete_lock is grabbed.
4849   return (OrderAccess::load_acquire(&_smr_delete_notify) != 0);
4850 }
4851 
4852 // set_smr_delete_notify() and clear_smr_delete_notify() are called
4853 // under the protection of the smr_delete_lock, but we also use an
4854 // Atomic operation to ensure the memory update is seen earlier than
4855 // when the smr_delete_lock is dropped.
4856 //
4857 void Threads::set_smr_delete_notify() {


5402                _smr_java_thread_list->length());
5403   print_smr_info_elements_on(st, _smr_java_thread_list);
5404   st->print_cr("}");
5405   if (_smr_to_delete_list != NULL) {
5406     st->print_cr("_smr_to_delete_list=" INTPTR_FORMAT ", length=%u, "
5407                  "elements={", p2i(_smr_to_delete_list),
5408                  _smr_to_delete_list->length());
5409     print_smr_info_elements_on(st, _smr_to_delete_list);
5410     st->print_cr("}");
5411     for (ThreadsList *t_list = _smr_to_delete_list->next_list();
5412          t_list != NULL; t_list = t_list->next_list()) {
5413       st->print("next-> " INTPTR_FORMAT ", length=%u, "
5414                 "elements={", p2i(t_list), t_list->length());
5415       print_smr_info_elements_on(st, t_list);
5416       st->print_cr("}");
5417     }
5418   }
5419   if (!EnableThreadSMRStatistics) {
5420     return;
5421   }
5422   st->print_cr("_smr_java_thread_list_alloc_cnt=%ld, "
5423                "_smr_java_thread_list_free_cnt=%ld, "
5424                "_smr_java_thread_list_max=%u, "
5425                "_smr_nested_thread_list_max=%u",
5426                _smr_java_thread_list_alloc_cnt,
5427                _smr_java_thread_list_free_cnt,
5428                _smr_java_thread_list_max,
5429                _smr_nested_thread_list_max);
5430   if (_smr_tlh_cnt > 0) {
5431     st->print_cr("_smr_tlh_cnt=" INT32_FORMAT
5432                  ", _smr_tlh_times=" INT32_FORMAT
5433                  ", avg_smr_tlh_time=%0.2f"
5434                  ", _smr_tlh_time_max=" INT32_FORMAT,
5435                  _smr_tlh_cnt, _smr_tlh_times,
5436                  ((double) _smr_tlh_times / _smr_tlh_cnt),
5437                  _smr_tlh_time_max);
5438   }
5439   if (_smr_deleted_thread_cnt > 0) {
5440     st->print_cr("_smr_deleted_thread_cnt=" INT32_FORMAT
5441                  ", _smr_deleted_thread_times=" INT32_FORMAT
5442                  ", avg_smr_deleted_thread_time=%0.2f"
5443                  ", _smr_deleted_thread_time_max=" INT32_FORMAT,
5444                  _smr_deleted_thread_cnt, _smr_deleted_thread_times,
5445                  ((double) _smr_deleted_thread_times / _smr_deleted_thread_cnt),
5446                  _smr_deleted_thread_time_max);
5447   }
5448   st->print_cr("_smr_delete_lock_wait_cnt=%u, _smr_delete_lock_wait_max=%u",
5449                _smr_delete_lock_wait_cnt, _smr_delete_lock_wait_max);
5450   st->print_cr("_smr_to_delete_list_cnt=%u, _smr_to_delete_list_max=%u",
5451                _smr_to_delete_list_cnt, _smr_to_delete_list_max);
5452 }
5453 
5454 // Print ThreadsList elements (4 per line).
5455 void Threads::print_smr_info_elements_on(outputStream* st,
5456                                          ThreadsList* t_list) {
5457   uint cnt = 0;
5458   JavaThreadIterator jti(t_list);
5459   for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
5460     st->print(INTPTR_FORMAT, p2i(jt));
5461     if (cnt < t_list->length() - 1) {
5462       // Separate with comma or comma-space except for the last one.
5463       if (((cnt + 1) % 4) == 0) {




3412     // process it here to make sure it isn't unloaded in the middle of
3413     // a scan.
3414     cf->do_code_blob(_scanned_compiled_method);
3415   }
3416 }
3417 
3418 void CodeCacheSweeperThread::nmethods_do(CodeBlobClosure* cf) {
3419   JavaThread::nmethods_do(cf);
3420   if (_scanned_compiled_method != NULL && cf != NULL) {
3421     // Safepoints can occur when the sweeper is scanning an nmethod so
3422     // process it here to make sure it isn't unloaded in the middle of
3423     // a scan.
3424     cf->do_code_blob(_scanned_compiled_method);
3425   }
3426 }
3427 
3428 
3429 // ======= Threads ========
3430 
3431 // The Threads class links together all active threads, and provides
3432 // operations over all threads. It is protected by the Threads_lock,
3433 // which is also used in other global contexts like safepointing.
3434 // ThreadsListHandles are used to safely perform operations on one
3435 // or more threads without the risk of the thread exiting during the
3436 // operation.
3437 //
3438 // Note: The Threads_lock is currently more widely used than we
3439 // would like. We are actively migrating Threads_lock uses to other
3440 // mechanisms in order to reduce Threads_lock contention.
3441 
3442 JavaThread*           Threads::_thread_list = NULL;
3443 int                   Threads::_number_of_threads = 0;
3444 int                   Threads::_number_of_non_daemon_threads = 0;
3445 int                   Threads::_return_code = 0;
3446 int                   Threads::_thread_claim_parity = 0;
3447 size_t                JavaThread::_stack_size_at_create = 0;
3448 // Safe Memory Reclamation (SMR) support:
3449 Monitor*              Threads::_smr_delete_lock =
3450                           new Monitor(Monitor::special, "smr_delete_lock",
3451                                       false /* allow_vm_block */,
3452                                       Monitor::_safepoint_check_never);
3453 // The '_cnt', '_max' and '_times" fields are enabled via
3454 // -XX:+EnableThreadSMRStatistics:
3455 
3456 // # of parallel threads in _smr_delete_lock->wait().
3457 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
3458 // but there is no nice 16-bit _FORMAT support.
3459 uint                  Threads::_smr_delete_lock_wait_cnt = 0;
3460 
3461 // Max # of parallel threads in _smr_delete_lock->wait().
3462 // Impl note: See _smr_delete_lock_wait_cnt note.
3463 uint                  Threads::_smr_delete_lock_wait_max = 0;
3464 
3465 // Flag to indicate when an _smr_delete_lock->notify() is needed.
3466 // Impl note: See _smr_delete_lock_wait_cnt note.
3467 volatile uint         Threads::_smr_delete_notify = 0;
3468 
3469 // # of threads deleted over VM lifetime.
3470 // Impl note: Atomically incremented over VM lifetime so use unsigned for more
3471 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3472 // isn't available everywhere (or is it?).
3473 volatile uint         Threads::_smr_deleted_thread_cnt = 0;
3474 
3475 // Max time in millis to delete a thread.
3476 // Impl note: 16-bit might be too small on an overloaded machine. Use
3477 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
3478 // loop for correctness.
3479 volatile uint         Threads::_smr_deleted_thread_time_max = 0;
3480 
3481 // Cumulative time in millis to delete threads.
3482 // Impl note: Atomically added to over VM lifetime so use unsigned for more
3483 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3484 // isn't available everywhere (or is it?).
3485 volatile uint         Threads::_smr_deleted_thread_times = 0;
3486 
3487 ThreadsList* volatile Threads::_smr_java_thread_list = new ThreadsList(0);
3488 
3489 // # of ThreadsLists allocated over VM lifetime.
3490 // Impl note: We allocate a new ThreadsList for every thread create and
3491 // every thread delete so we need a bigger type than the
3492 // _smr_deleted_thread_cnt field.
3493 uint64_t              Threads::_smr_java_thread_list_alloc_cnt = 1;
3494 
3495 // # of ThreadsLists freed over VM lifetime.
3496 // Impl note: See _smr_java_thread_list_alloc_cnt note.
3497 uint64_t              Threads::_smr_java_thread_list_free_cnt = 0;
3498 
3499 // Max size ThreadsList allocated.
3500 // Impl note: Max # of threads alive at one time should fit in unsigned 32-bit.
3501 uint                  Threads::_smr_java_thread_list_max = 0;
3502 
3503 // Max # of nested ThreadsLists for a thread.
3504 // Impl note: Hard to imagine > 64K nested ThreadsLists so this could be
3505 // 16-bit, but there is no nice 16-bit _FORMAT support.
3506 uint                  Threads::_smr_nested_thread_list_max = 0;
3507 
3508 // # of ThreadsListHandles deleted over VM lifetime.
3509 // Impl note: Atomically incremented over VM lifetime so use unsigned for
3510 // more range. There will be fewer ThreadsListHandles than threads so
3511 // unsigned 32-bit should be fine.
3512 volatile uint         Threads::_smr_tlh_cnt = 0;
3513 
3514 // Max time in millis to delete a ThreadsListHandle.
3515 // Impl note: 16-bit might be too small on an overloaded machine. Use
3516 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
3517 // loop for correctness.
3518 volatile uint         Threads::_smr_tlh_time_max = 0;
3519 
3520 // Cumulative time in millis to delete ThreadsListHandles.
3521 // Impl note: Atomically added to over VM lifetime so use unsigned for more
3522 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3523 // isn't available everywhere (or is it?).
3524 volatile uint         Threads::_smr_tlh_times = 0;
3525 
3526 ThreadsList*          Threads::_smr_to_delete_list = NULL;
3527 
3528 // # of parallel ThreadsLists on the to-delete list.
3529 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
3530 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
3531 uint                  Threads::_smr_to_delete_list_cnt = 0;
3532 
3533 // Max # of parallel ThreadsLists on the to-delete list.
3534 // Impl note: See _smr_to_delete_list_cnt note.
3535 uint                  Threads::_smr_to_delete_list_max = 0;
3536 
3537 #ifdef ASSERT
3538 bool                  Threads::_vm_complete = false;
3539 #endif
3540 
3541 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3542   Prefetch::read((void*)addr, prefetch_interval);
3543   return *addr;
3544 }
3545 
3546 // Possibly the ugliest for loop the world has seen. C++ does not allow
3547 // multiple types in the declaration section of the for loop. In this case
3548 // we are only dealing with pointers and hence can cast them. It looks ugly
3549 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3550 #define DO_JAVA_THREADS(LIST, X)                                                                                          \
3551     for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes,                           \
3552              *MACRO_list = (JavaThread*)(LIST),                                                                           \
3553              **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(),  \
3554              **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(),                                     \


4879     if (EnableThreadSMRStatistics) {
4880       _smr_delete_lock_wait_cnt++;
4881       if (_smr_delete_lock_wait_cnt > _smr_delete_lock_wait_max) {
4882         _smr_delete_lock_wait_max = _smr_delete_lock_wait_cnt;
4883       }
4884     }
4885     // Wait for a release_stable_list() call before we check again. No
4886     // safepoint check, no timeout, and not as suspend equivalent flag
4887     // because this JavaThread is not on the Threads list.
4888     Threads::smr_delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
4889                                      !Mutex::_as_suspend_equivalent_flag);
4890     if (EnableThreadSMRStatistics) {
4891       _smr_delete_lock_wait_cnt--;
4892     }
4893 
4894     Threads::clear_smr_delete_notify();
4895     Threads::smr_delete_lock()->unlock();
4896     // Retry the whole scenario.
4897   }
4898 

4899   if (ThreadLocalHandshakes) {
4900     // The thread is about to be deleted so cancel any handshake.
4901     thread->cancel_handshake();
4902   }
4903 
4904   delete thread;
4905   if (EnableThreadSMRStatistics) {
4906     timer.stop();
4907     uint millis = (uint)timer.milliseconds();
4908     Threads::inc_smr_deleted_thread_cnt();
4909     Threads::add_smr_deleted_thread_times(millis);
4910     Threads::update_smr_deleted_thread_time_max(millis);
4911   }
4912 
4913   log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
4914 }
4915 
4916 bool Threads::smr_delete_notify() {
4917   // Use load_acquire() in order to see any updates to _smr_delete_notify
4918   // earlier than when smr_delete_lock is grabbed.
4919   return (OrderAccess::load_acquire(&_smr_delete_notify) != 0);
4920 }
4921 
4922 // set_smr_delete_notify() and clear_smr_delete_notify() are called
4923 // under the protection of the smr_delete_lock, but we also use an
4924 // Atomic operation to ensure the memory update is seen earlier than
4925 // when the smr_delete_lock is dropped.
4926 //
4927 void Threads::set_smr_delete_notify() {


5472                _smr_java_thread_list->length());
5473   print_smr_info_elements_on(st, _smr_java_thread_list);
5474   st->print_cr("}");
5475   if (_smr_to_delete_list != NULL) {
5476     st->print_cr("_smr_to_delete_list=" INTPTR_FORMAT ", length=%u, "
5477                  "elements={", p2i(_smr_to_delete_list),
5478                  _smr_to_delete_list->length());
5479     print_smr_info_elements_on(st, _smr_to_delete_list);
5480     st->print_cr("}");
5481     for (ThreadsList *t_list = _smr_to_delete_list->next_list();
5482          t_list != NULL; t_list = t_list->next_list()) {
5483       st->print("next-> " INTPTR_FORMAT ", length=%u, "
5484                 "elements={", p2i(t_list), t_list->length());
5485       print_smr_info_elements_on(st, t_list);
5486       st->print_cr("}");
5487     }
5488   }
5489   if (!EnableThreadSMRStatistics) {
5490     return;
5491   }
5492   st->print_cr("_smr_java_thread_list_alloc_cnt=" UINT64_FORMAT ","
5493                "_smr_java_thread_list_free_cnt=" UINT64_FORMAT ","
5494                "_smr_java_thread_list_max=%u, "
5495                "_smr_nested_thread_list_max=%u",
5496                _smr_java_thread_list_alloc_cnt,
5497                _smr_java_thread_list_free_cnt,
5498                _smr_java_thread_list_max,
5499                _smr_nested_thread_list_max);
5500   if (_smr_tlh_cnt > 0) {
5501     st->print_cr("_smr_tlh_cnt=%u"
5502                  ", _smr_tlh_times=%u"
5503                  ", avg_smr_tlh_time=%0.2f"
5504                  ", _smr_tlh_time_max=%u",
5505                  _smr_tlh_cnt, _smr_tlh_times,
5506                  ((double) _smr_tlh_times / _smr_tlh_cnt),
5507                  _smr_tlh_time_max);
5508   }
5509   if (_smr_deleted_thread_cnt > 0) {
5510     st->print_cr("_smr_deleted_thread_cnt=%u"
5511                  ", _smr_deleted_thread_times=%u"
5512                  ", avg_smr_deleted_thread_time=%0.2f"
5513                  ", _smr_deleted_thread_time_max=%u",
5514                  _smr_deleted_thread_cnt, _smr_deleted_thread_times,
5515                  ((double) _smr_deleted_thread_times / _smr_deleted_thread_cnt),
5516                  _smr_deleted_thread_time_max);
5517   }
5518   st->print_cr("_smr_delete_lock_wait_cnt=%u, _smr_delete_lock_wait_max=%u",
5519                _smr_delete_lock_wait_cnt, _smr_delete_lock_wait_max);
5520   st->print_cr("_smr_to_delete_list_cnt=%u, _smr_to_delete_list_max=%u",
5521                _smr_to_delete_list_cnt, _smr_to_delete_list_max);
5522 }
5523 
5524 // Print ThreadsList elements (4 per line).
5525 void Threads::print_smr_info_elements_on(outputStream* st,
5526                                          ThreadsList* t_list) {
5527   uint cnt = 0;
5528   JavaThreadIterator jti(t_list);
5529   for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
5530     st->print(INTPTR_FORMAT, p2i(jt));
5531     if (cnt < t_list->length() - 1) {
5532       // Separate with comma or comma-space except for the last one.
5533       if (((cnt + 1) % 4) == 0) {


< prev index next >