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(), \
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) {
|
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 // Safe Memory Reclamation (SMR) support:
3444 Monitor* Threads::_smr_delete_lock =
3445 new Monitor(Monitor::special, "smr_delete_lock",
3446 false /* allow_vm_block */,
3447 Monitor::_safepoint_check_never);
3448 // The '_cnt', '_max' and '_times" fields are enabled via
3449 // -XX:+EnableThreadSMRStatistics:
3450
3451 // # of parallel threads in _smr_delete_lock->wait().
3452 // Impl note: Hard to imagine > 64K waiting threads so this could be 16-bit,
3453 // but there is no nice 16-bit _FORMAT support.
3454 uint Threads::_smr_delete_lock_wait_cnt = 0;
3455
3456 // Max # of parallel threads in _smr_delete_lock->wait().
3457 // Impl note: See _smr_delete_lock_wait_cnt note.
3458 uint Threads::_smr_delete_lock_wait_max = 0;
3459
3460 // Flag to indicate when an _smr_delete_lock->notify() is needed.
3461 // Impl note: See _smr_delete_lock_wait_cnt note.
3462 volatile uint Threads::_smr_delete_notify = 0;
3463
3464 // # of threads deleted over VM lifetime.
3465 // Impl note: Atomically incremented over VM lifetime so use unsigned for more
3466 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3467 // isn't available everywhere (or is it?).
3468 volatile uint Threads::_smr_deleted_thread_cnt = 0;
3469
3470 // Max time in millis to delete a thread.
3471 // Impl note: 16-bit might be too small on an overloaded machine. Use
3472 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
3473 // loop for correctness.
3474 volatile uint Threads::_smr_deleted_thread_time_max = 0;
3475
3476 // Cumulative time in millis to delete threads.
3477 // Impl note: Atomically added to over VM lifetime so use unsigned for more
3478 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3479 // isn't available everywhere (or is it?).
3480 volatile uint Threads::_smr_deleted_thread_times = 0;
3481
3482 ThreadsList* volatile Threads::_smr_java_thread_list = new ThreadsList(0);
3483
3484 // # of ThreadsLists allocated over VM lifetime.
3485 // Impl note: We allocate a new ThreadsList for every thread create and
3486 // every thread delete so we need a bigger type than the
3487 // _smr_deleted_thread_cnt field.
3488 uint64_t Threads::_smr_java_thread_list_alloc_cnt = 1;
3489
3490 // # of ThreadsLists freed over VM lifetime.
3491 // Impl note: See _smr_java_thread_list_alloc_cnt note.
3492 uint64_t Threads::_smr_java_thread_list_free_cnt = 0;
3493
3494 // Max size ThreadsList allocated.
3495 // Impl note: Max # of threads alive at one time should fit in unsigned 32-bit.
3496 uint Threads::_smr_java_thread_list_max = 0;
3497
3498 // Max # of nested ThreadsLists for a thread.
3499 // Impl note: Hard to imagine > 64K nested ThreadsLists so this could be
3500 // 16-bit, but there is no nice 16-bit _FORMAT support.
3501 uint Threads::_smr_nested_thread_list_max = 0;
3502
3503 // # of ThreadsListHandles deleted over VM lifetime.
3504 // Impl note: Atomically incremented over VM lifetime so use unsigned for
3505 // more range. There will be fewer ThreadsListHandles than threads so
3506 // unsigned 32-bit should be fine.
3507 volatile uint Threads::_smr_tlh_cnt = 0;
3508
3509 // Max time in millis to delete a ThreadsListHandle.
3510 // Impl note: 16-bit might be too small on an overloaded machine. Use
3511 // unsigned since this is a time value. Set via Atomic::cmpxchg() in a
3512 // loop for correctness.
3513 volatile uint Threads::_smr_tlh_time_max = 0;
3514
3515 // Cumulative time in millis to delete ThreadsListHandles.
3516 // Impl note: Atomically added to over VM lifetime so use unsigned for more
3517 // range. Unsigned 64-bit would be more future proof, but 64-bit atomic inc
3518 // isn't available everywhere (or is it?).
3519 volatile uint Threads::_smr_tlh_times = 0;
3520
3521 ThreadsList* Threads::_smr_to_delete_list = NULL;
3522
3523 // # of parallel ThreadsLists on the to-delete list.
3524 // Impl note: Hard to imagine > 64K ThreadsLists needing to be deleted so
3525 // this could be 16-bit, but there is no nice 16-bit _FORMAT support.
3526 uint Threads::_smr_to_delete_list_cnt = 0;
3527
3528 // Max # of parallel ThreadsLists on the to-delete list.
3529 // Impl note: See _smr_to_delete_list_cnt note.
3530 uint Threads::_smr_to_delete_list_max = 0;
3531
3532 #ifdef ASSERT
3533 bool Threads::_vm_complete = false;
3534 #endif
3535
3536 static inline void *prefetch_and_load_ptr(void **addr, intx prefetch_interval) {
3537 Prefetch::read((void*)addr, prefetch_interval);
3538 return *addr;
3539 }
3540
3541 // Possibly the ugliest for loop the world has seen. C++ does not allow
3542 // multiple types in the declaration section of the for loop. In this case
3543 // we are only dealing with pointers and hence can cast them. It looks ugly
3544 // but macros are ugly and therefore it's fine to make things absurdly ugly.
3545 #define DO_JAVA_THREADS(LIST, X) \
3546 for (JavaThread *MACRO_scan_interval = (JavaThread*)(uintptr_t)PrefetchScanIntervalInBytes, \
3547 *MACRO_list = (JavaThread*)(LIST), \
3548 **MACRO_end = ((JavaThread**)((ThreadsList*)MACRO_list)->threads()) + ((ThreadsList*)MACRO_list)->length(), \
3549 **MACRO_current_p = (JavaThread**)((ThreadsList*)MACRO_list)->threads(), \
4882 // because this JavaThread is not on the Threads list.
4883 Threads::smr_delete_lock()->wait(Mutex::_no_safepoint_check_flag, 0,
4884 !Mutex::_as_suspend_equivalent_flag);
4885 if (EnableThreadSMRStatistics) {
4886 _smr_delete_lock_wait_cnt--;
4887 }
4888
4889 Threads::clear_smr_delete_notify();
4890 Threads::smr_delete_lock()->unlock();
4891 // Retry the whole scenario.
4892 }
4893
4894 // If someone set a handshake on us just as we entered exit path, we simple cancel it.
4895 if (ThreadLocalHandshakes) {
4896 thread->cancel_handshake();
4897 }
4898
4899 delete thread;
4900 if (EnableThreadSMRStatistics) {
4901 timer.stop();
4902 uint millis = (uint)timer.milliseconds();
4903 Threads::inc_smr_deleted_thread_cnt();
4904 Threads::add_smr_deleted_thread_times(millis);
4905 Threads::update_smr_deleted_thread_time_max(millis);
4906 }
4907
4908 log_debug(thread, smr)("tid=" UINTX_FORMAT ": Threads::smr_delete: thread=" INTPTR_FORMAT " is deleted.", os::current_thread_id(), p2i(thread));
4909 }
4910
4911 bool Threads::smr_delete_notify() {
4912 // Use load_acquire() in order to see any updates to _smr_delete_notify
4913 // earlier than when smr_delete_lock is grabbed.
4914 return (OrderAccess::load_acquire(&_smr_delete_notify) != 0);
4915 }
4916
4917 // set_smr_delete_notify() and clear_smr_delete_notify() are called
4918 // under the protection of the smr_delete_lock, but we also use an
4919 // Atomic operation to ensure the memory update is seen earlier than
4920 // when the smr_delete_lock is dropped.
4921 //
4922 void Threads::set_smr_delete_notify() {
5467 _smr_java_thread_list->length());
5468 print_smr_info_elements_on(st, _smr_java_thread_list);
5469 st->print_cr("}");
5470 if (_smr_to_delete_list != NULL) {
5471 st->print_cr("_smr_to_delete_list=" INTPTR_FORMAT ", length=%u, "
5472 "elements={", p2i(_smr_to_delete_list),
5473 _smr_to_delete_list->length());
5474 print_smr_info_elements_on(st, _smr_to_delete_list);
5475 st->print_cr("}");
5476 for (ThreadsList *t_list = _smr_to_delete_list->next_list();
5477 t_list != NULL; t_list = t_list->next_list()) {
5478 st->print("next-> " INTPTR_FORMAT ", length=%u, "
5479 "elements={", p2i(t_list), t_list->length());
5480 print_smr_info_elements_on(st, t_list);
5481 st->print_cr("}");
5482 }
5483 }
5484 if (!EnableThreadSMRStatistics) {
5485 return;
5486 }
5487 st->print_cr("_smr_java_thread_list_alloc_cnt=" UINT64_FORMAT ","
5488 "_smr_java_thread_list_free_cnt=" UINT64_FORMAT ","
5489 "_smr_java_thread_list_max=%u, "
5490 "_smr_nested_thread_list_max=%u",
5491 _smr_java_thread_list_alloc_cnt,
5492 _smr_java_thread_list_free_cnt,
5493 _smr_java_thread_list_max,
5494 _smr_nested_thread_list_max);
5495 if (_smr_tlh_cnt > 0) {
5496 st->print_cr("_smr_tlh_cnt=%u"
5497 ", _smr_tlh_times=%u"
5498 ", avg_smr_tlh_time=%0.2f"
5499 ", _smr_tlh_time_max=%u",
5500 _smr_tlh_cnt, _smr_tlh_times,
5501 ((double) _smr_tlh_times / _smr_tlh_cnt),
5502 _smr_tlh_time_max);
5503 }
5504 if (_smr_deleted_thread_cnt > 0) {
5505 st->print_cr("_smr_deleted_thread_cnt=%u"
5506 ", _smr_deleted_thread_times=%u"
5507 ", avg_smr_deleted_thread_time=%0.2f"
5508 ", _smr_deleted_thread_time_max=%u",
5509 _smr_deleted_thread_cnt, _smr_deleted_thread_times,
5510 ((double) _smr_deleted_thread_times / _smr_deleted_thread_cnt),
5511 _smr_deleted_thread_time_max);
5512 }
5513 st->print_cr("_smr_delete_lock_wait_cnt=%u, _smr_delete_lock_wait_max=%u",
5514 _smr_delete_lock_wait_cnt, _smr_delete_lock_wait_max);
5515 st->print_cr("_smr_to_delete_list_cnt=%u, _smr_to_delete_list_max=%u",
5516 _smr_to_delete_list_cnt, _smr_to_delete_list_max);
5517 }
5518
5519 // Print ThreadsList elements (4 per line).
5520 void Threads::print_smr_info_elements_on(outputStream* st,
5521 ThreadsList* t_list) {
5522 uint cnt = 0;
5523 JavaThreadIterator jti(t_list);
5524 for (JavaThread *jt = jti.first(); jt != NULL; jt = jti.next()) {
5525 st->print(INTPTR_FORMAT, p2i(jt));
5526 if (cnt < t_list->length() - 1) {
5527 // Separate with comma or comma-space except for the last one.
5528 if (((cnt + 1) % 4) == 0) {
|