39 #include "oops/symbol.hpp" 40 #include "runtime/atomic.inline.hpp" 41 #include "runtime/compilationPolicy.hpp" 42 #include "runtime/deoptimization.hpp" 43 #include "runtime/frame.inline.hpp" 44 #include "runtime/interfaceSupport.hpp" 45 #include "runtime/mutexLocker.hpp" 46 #include "runtime/orderAccess.inline.hpp" 47 #include "runtime/osThread.hpp" 48 #include "runtime/safepoint.hpp" 49 #include "runtime/signature.hpp" 50 #include "runtime/stubCodeGenerator.hpp" 51 #include "runtime/stubRoutines.hpp" 52 #include "runtime/sweeper.hpp" 53 #include "runtime/synchronizer.hpp" 54 #include "runtime/thread.inline.hpp" 55 #include "services/runtimeService.hpp" 56 #include "utilities/events.hpp" 57 #include "utilities/macros.hpp" 58 #if INCLUDE_ALL_GCS 59 #include "gc/cms/concurrentMarkSweepThread.hpp" 60 #include "gc/g1/suspendibleThreadSet.hpp" 61 #endif // INCLUDE_ALL_GCS 62 #ifdef COMPILER1 63 #include "c1/c1_globals.hpp" 64 #endif 65 66 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 67 68 // -------------------------------------------------------------------------------------------------- 69 // Implementation of Safepoint begin/end 70 71 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized; 72 volatile int SafepointSynchronize::_waiting_to_block = 0; 73 volatile int SafepointSynchronize::_safepoint_counter = 0; 74 int SafepointSynchronize::_current_jni_active_count = 0; 75 long SafepointSynchronize::_end_of_last_safepoint = 0; 76 static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE 77 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only 78 static bool timeout_error_printed = false; 79 80 // Roll all threads forward to a safepoint and suspend them all 81 void SafepointSynchronize::begin() { 82 83 Thread* myThread = Thread::current(); 84 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint"); 85 86 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) { 87 _safepoint_begin_time = os::javaTimeNanos(); 88 _ts_of_current_safepoint = tty->time_stamp().seconds(); 89 } 90 91 #if INCLUDE_ALL_GCS 92 if (UseConcMarkSweepGC) { 93 // In the future we should investigate whether CMS can use the 94 // more-general mechanism below. DLD (01/05). 95 ConcurrentMarkSweepThread::synchronize(false); 96 } else if (UseG1GC) { 97 SuspendibleThreadSet::synchronize(); 98 } 99 #endif // INCLUDE_ALL_GCS 100 101 // By getting the Threads_lock, we assure that no threads are about to start or 102 // exit. It is released again in SafepointSynchronize::end(). 103 Threads_lock->lock(); 104 105 assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state"); 106 107 int nof_threads = Threads::number_of_threads(); 108 109 if (TraceSafepoint) { 110 tty->print_cr("Safepoint synchronization initiated. (%d)", nof_threads); 111 } 112 113 RuntimeService::record_safepoint_begin(); 114 115 MutexLocker mu(Safepoint_lock); 116 117 // Reset the count of active JNI critical threads 118 _current_jni_active_count = 0; 452 } 453 ThreadSafepointState* cur_state = current->safepoint_state(); 454 assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint"); 455 cur_state->restart(); 456 assert(cur_state->is_running(), "safepoint state has not been reset"); 457 } 458 459 RuntimeService::record_safepoint_end(); 460 461 // Release threads lock, so threads can be created/destroyed again. It will also starts all threads 462 // blocked in signal_thread_blocked 463 Threads_lock->unlock(); 464 465 } 466 #if INCLUDE_ALL_GCS 467 // If there are any concurrent GC threads resume them. 468 if (UseConcMarkSweepGC) { 469 ConcurrentMarkSweepThread::desynchronize(false); 470 } else if (UseG1GC) { 471 SuspendibleThreadSet::desynchronize(); 472 } 473 #endif // INCLUDE_ALL_GCS 474 // record this time so VMThread can keep track how much time has elapsed 475 // since last safepoint. 476 _end_of_last_safepoint = os::javaTimeMillis(); 477 } 478 479 bool SafepointSynchronize::is_cleanup_needed() { 480 // Need a safepoint if some inline cache buffers is non-empty 481 if (!InlineCacheBuffer::is_empty()) return true; 482 return false; 483 } 484 485 486 487 // Various cleaning tasks that should be done periodically at safepoints 488 void SafepointSynchronize::do_cleanup_tasks() { 489 { 490 TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime); 491 ObjectSynchronizer::deflate_idle_monitors(); 951 RegisterMap map(thread(), true); 952 frame caller_fr = stub_fr.sender(&map); 953 954 // Should only be poll_return or poll 955 assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" ); 956 957 // This is a poll immediately before a return. The exception handling code 958 // has already had the effect of causing the return to occur, so the execution 959 // will continue immediately after the call. In addition, the oopmap at the 960 // return point does not mark the return value as an oop (if it is), so 961 // it needs a handle here to be updated. 962 if( nm->is_at_poll_return(real_return_addr) ) { 963 // See if return type is an oop. 964 bool return_oop = nm->method()->is_returning_oop(); 965 Handle return_value; 966 if (return_oop) { 967 // The oop result has been saved on the stack together with all 968 // the other registers. In order to preserve it over GCs we need 969 // to keep it in a handle. 970 oop result = caller_fr.saved_oop_result(&map); 971 assert(result == NULL || result->is_oop(), "must be oop"); 972 return_value = Handle(thread(), result); 973 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer"); 974 } 975 976 // Block the thread 977 SafepointSynchronize::block(thread()); 978 979 // restore oop result, if any 980 if (return_oop) { 981 caller_fr.set_saved_oop_result(&map, return_value()); 982 } 983 } 984 985 // This is a safepoint poll. Verify the return address and block. 986 else { 987 set_at_poll_safepoint(true); 988 989 // verify the blob built the "return address" correctly 990 assert(real_return_addr == caller_fr.pc(), "must match"); | 39 #include "oops/symbol.hpp" 40 #include "runtime/atomic.inline.hpp" 41 #include "runtime/compilationPolicy.hpp" 42 #include "runtime/deoptimization.hpp" 43 #include "runtime/frame.inline.hpp" 44 #include "runtime/interfaceSupport.hpp" 45 #include "runtime/mutexLocker.hpp" 46 #include "runtime/orderAccess.inline.hpp" 47 #include "runtime/osThread.hpp" 48 #include "runtime/safepoint.hpp" 49 #include "runtime/signature.hpp" 50 #include "runtime/stubCodeGenerator.hpp" 51 #include "runtime/stubRoutines.hpp" 52 #include "runtime/sweeper.hpp" 53 #include "runtime/synchronizer.hpp" 54 #include "runtime/thread.inline.hpp" 55 #include "services/runtimeService.hpp" 56 #include "utilities/events.hpp" 57 #include "utilities/macros.hpp" 58 #if INCLUDE_ALL_GCS 59 #include "gc/shenandoah/shenandoahConcurrentThread.hpp" 60 #include "gc/cms/concurrentMarkSweepThread.hpp" 61 #include "gc/g1/suspendibleThreadSet.hpp" 62 #endif // INCLUDE_ALL_GCS 63 #ifdef COMPILER1 64 #include "c1/c1_globals.hpp" 65 #endif 66 67 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 68 69 // -------------------------------------------------------------------------------------------------- 70 // Implementation of Safepoint begin/end 71 72 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized; 73 volatile int SafepointSynchronize::_waiting_to_block = 0; 74 volatile int SafepointSynchronize::_safepoint_counter = 0; 75 int SafepointSynchronize::_current_jni_active_count = 0; 76 long SafepointSynchronize::_end_of_last_safepoint = 0; 77 static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE 78 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only 79 static bool timeout_error_printed = false; 80 81 // Roll all threads forward to a safepoint and suspend them all 82 void SafepointSynchronize::begin() { 83 84 Thread* myThread = Thread::current(); 85 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint"); 86 87 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) { 88 _safepoint_begin_time = os::javaTimeNanos(); 89 _ts_of_current_safepoint = tty->time_stamp().seconds(); 90 } 91 92 #if INCLUDE_ALL_GCS 93 if (UseConcMarkSweepGC) { 94 // In the future we should investigate whether CMS can use the 95 // more-general mechanism below. DLD (01/05). 96 ConcurrentMarkSweepThread::synchronize(false); 97 } else if (UseG1GC) { 98 SuspendibleThreadSet::synchronize(); 99 } else if (UseShenandoahGC) { 100 ShenandoahConcurrentThread::safepoint_synchronize(); 101 } 102 103 #endif // INCLUDE_ALL_GCS 104 105 // By getting the Threads_lock, we assure that no threads are about to start or 106 // exit. It is released again in SafepointSynchronize::end(). 107 Threads_lock->lock(); 108 109 assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state"); 110 111 int nof_threads = Threads::number_of_threads(); 112 113 if (TraceSafepoint) { 114 tty->print_cr("Safepoint synchronization initiated. (%d)", nof_threads); 115 } 116 117 RuntimeService::record_safepoint_begin(); 118 119 MutexLocker mu(Safepoint_lock); 120 121 // Reset the count of active JNI critical threads 122 _current_jni_active_count = 0; 456 } 457 ThreadSafepointState* cur_state = current->safepoint_state(); 458 assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint"); 459 cur_state->restart(); 460 assert(cur_state->is_running(), "safepoint state has not been reset"); 461 } 462 463 RuntimeService::record_safepoint_end(); 464 465 // Release threads lock, so threads can be created/destroyed again. It will also starts all threads 466 // blocked in signal_thread_blocked 467 Threads_lock->unlock(); 468 469 } 470 #if INCLUDE_ALL_GCS 471 // If there are any concurrent GC threads resume them. 472 if (UseConcMarkSweepGC) { 473 ConcurrentMarkSweepThread::desynchronize(false); 474 } else if (UseG1GC) { 475 SuspendibleThreadSet::desynchronize(); 476 } else if (UseShenandoahGC) { 477 ShenandoahConcurrentThread::safepoint_desynchronize(); 478 } 479 #endif // INCLUDE_ALL_GCS 480 // record this time so VMThread can keep track how much time has elapsed 481 // since last safepoint. 482 _end_of_last_safepoint = os::javaTimeMillis(); 483 } 484 485 bool SafepointSynchronize::is_cleanup_needed() { 486 // Need a safepoint if some inline cache buffers is non-empty 487 if (!InlineCacheBuffer::is_empty()) return true; 488 return false; 489 } 490 491 492 493 // Various cleaning tasks that should be done periodically at safepoints 494 void SafepointSynchronize::do_cleanup_tasks() { 495 { 496 TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime); 497 ObjectSynchronizer::deflate_idle_monitors(); 957 RegisterMap map(thread(), true); 958 frame caller_fr = stub_fr.sender(&map); 959 960 // Should only be poll_return or poll 961 assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" ); 962 963 // This is a poll immediately before a return. The exception handling code 964 // has already had the effect of causing the return to occur, so the execution 965 // will continue immediately after the call. In addition, the oopmap at the 966 // return point does not mark the return value as an oop (if it is), so 967 // it needs a handle here to be updated. 968 if( nm->is_at_poll_return(real_return_addr) ) { 969 // See if return type is an oop. 970 bool return_oop = nm->method()->is_returning_oop(); 971 Handle return_value; 972 if (return_oop) { 973 // The oop result has been saved on the stack together with all 974 // the other registers. In order to preserve it over GCs we need 975 // to keep it in a handle. 976 oop result = caller_fr.saved_oop_result(&map); 977 if (ShenandoahVerifyReadsToFromSpace) { 978 result = oopDesc::bs()->read_barrier(result); 979 } 980 assert(result == NULL || result->is_oop(), "must be oop"); 981 return_value = Handle(thread(), result); 982 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer"); 983 } 984 985 // Block the thread 986 SafepointSynchronize::block(thread()); 987 988 // restore oop result, if any 989 if (return_oop) { 990 caller_fr.set_saved_oop_result(&map, return_value()); 991 } 992 } 993 994 // This is a safepoint poll. Verify the return address and block. 995 else { 996 set_at_poll_safepoint(true); 997 998 // verify the blob built the "return address" correctly 999 assert(real_return_addr == caller_fr.pc(), "must match"); |