src/share/vm/runtime/safepoint.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/runtime

src/share/vm/runtime/safepoint.cpp

Print this page
rev 5732 : [mq]: comments2


 139   int still_running = nof_threads;
 140 
 141   // Save the starting time, so that it can be compared to see if this has taken
 142   // too long to complete.
 143   jlong safepoint_limit_time;
 144   timeout_error_printed = false;
 145 
 146   // PrintSafepointStatisticsTimeout can be specified separately. When
 147   // specified, PrintSafepointStatistics will be set to true in
 148   // deferred_initialize_stat method. The initialization has to be done
 149   // early enough to avoid any races. See bug 6880029 for details.
 150   if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
 151     deferred_initialize_stat();
 152   }
 153 
 154   // Begin the process of bringing the system to a safepoint.
 155   // Java threads can be in several different states and are
 156   // stopped by different mechanisms:
 157   //
 158   //  1. Running interpreted
 159   //     The interpeter dispatch table is changed to force it to
 160   //     check for a safepoint condition between bytecodes.
 161   //  2. Running in native code
 162   //     When returning from the native code, a Java thread must check
 163   //     the safepoint _state to see if we must block.  If the
 164   //     VM thread sees a Java thread in native, it does
 165   //     not wait for this thread to block.  The order of the memory
 166   //     writes and reads of both the safepoint state and the Java
 167   //     threads state is critical.  In order to guarantee that the
 168   //     memory writes are serialized with respect to each other,
 169   //     the VM thread issues a memory barrier instruction
 170   //     (on MP systems).  In order to avoid the overhead of issuing
 171   //     a memory barrier for each Java thread making native calls, each Java
 172   //     thread performs a write to a single memory page after changing
 173   //     the thread state.  The VM thread performs a sequence of
 174   //     mprotect OS calls which forces all previous writes from all
 175   //     Java threads to be serialized.  This is done in the
 176   //     os::serialize_thread_states() call.  This has proven to be
 177   //     much more efficient than executing a membar instruction
 178   //     on every call to native code.
 179   //  3. Running compiled Code


 265       // Yield_all() is implemented as a short unconditional sleep on some platforms.
 266       // Typical operating systems round a "short" sleep period up to 10 msecs, so sleeping
 267       // can actually increase the time it takes the VM thread to detect that a system-wide
 268       // stop-the-world safepoint has been reached.  In a pathological scenario such as that
 269       // described in CR6415670 the VMthread may sleep just before the mutator(s) become safe.
 270       // In that case the mutators will be stalled waiting for the safepoint to complete and the
 271       // the VMthread will be sleeping, waiting for the mutators to rendezvous.  The VMthread
 272       // will eventually wake up and detect that all mutators are safe, at which point
 273       // we'll again make progress.
 274       //
 275       // Beware too that that the VMThread typically runs at elevated priority.
 276       // Its default priority is higher than the default mutator priority.
 277       // Obviously, this complicates spinning.
 278       //
 279       // Note too that on Windows XP SwitchThreadTo() has quite different behavior than Sleep(0).
 280       // Sleep(0) will _not yield to lower priority threads, while SwitchThreadTo() will.
 281       //
 282       // See the comments in synchronizer.cpp for additional remarks on spinning.
 283       //
 284       // In the future we might:
 285       // 1. Modify the safepoint scheme to avoid potentally unbounded spinning.
 286       //    This is tricky as the path used by a thread exiting the JVM (say on
 287       //    on JNI call-out) simply stores into its state field.  The burden
 288       //    is placed on the VM thread, which must poll (spin).
 289       // 2. Find something useful to do while spinning.  If the safepoint is GC-related
 290       //    we might aggressively scan the stacks of threads that are already safe.
 291       // 3. Use Solaris schedctl to examine the state of the still-running mutators.
 292       //    If all the mutators are ONPROC there's no reason to sleep or yield.
 293       // 4. YieldTo() any still-running mutators that are ready but OFFPROC.
 294       // 5. Check system saturation.  If the system is not fully saturated then
 295       //    simply spin and avoid sleep/yield.
 296       // 6. As still-running mutators rendezvous they could unpark the sleeping
 297       //    VMthread.  This works well for still-running mutators that become
 298       //    safe.  The VMthread must still poll for mutators that call-out.
 299       // 7. Drive the policy on time-since-begin instead of iterations.
 300       // 8. Consider making the spin duration a function of the # of CPUs:
 301       //    Spin = (((ncpus-1) * M) + K) + F(still_running)
 302       //    Alternately, instead of counting iterations of the outer loop
 303       //    we could count the # of threads visited in the inner loop, above.
 304       // 9. On windows consider using the return value from SwitchThreadTo()
 305       //    to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.


 472       assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
 473       cur_state->restart();
 474       assert(cur_state->is_running(), "safepoint state has not been reset");
 475     }
 476 
 477     RuntimeService::record_safepoint_end();
 478 
 479     // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
 480     // blocked in signal_thread_blocked
 481     Threads_lock->unlock();
 482 
 483   }
 484 #if INCLUDE_ALL_GCS
 485   // If there are any concurrent GC threads resume them.
 486   if (UseConcMarkSweepGC) {
 487     ConcurrentMarkSweepThread::desynchronize(false);
 488   } else if (UseG1GC) {
 489     ConcurrentGCThread::safepoint_desynchronize();
 490   }
 491 #endif // INCLUDE_ALL_GCS
 492   // record this time so VMThread can keep track how much time has elasped
 493   // since last safepoint.
 494   _end_of_last_safepoint = os::javaTimeMillis();
 495 }
 496 
 497 bool SafepointSynchronize::is_cleanup_needed() {
 498   // Need a safepoint if some inline cache buffers is non-empty
 499   if (!InlineCacheBuffer::is_empty()) return true;
 500   return false;
 501 }
 502 
 503 
 504 
 505 // Various cleaning tasks that should be done periodically at safepoints
 506 void SafepointSynchronize::do_cleanup_tasks() {
 507   {
 508     TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime);
 509     ObjectSynchronizer::deflate_idle_monitors();
 510   }
 511 
 512   {


 809   */
 810 
 811   if (ShowSafepointMsgs) {
 812     tty->print("handle_polling_page_exception: ");
 813   }
 814 
 815   if (PrintSafepointStatistics) {
 816     inc_page_trap_count();
 817   }
 818 
 819   ThreadSafepointState* state = thread->safepoint_state();
 820 
 821   state->handle_polling_page_exception();
 822   // print_me(sp,stack_copy,was_oops);
 823 }
 824 
 825 
 826 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
 827   if (!timeout_error_printed) {
 828     timeout_error_printed = true;
 829     // Print out the thread infor which didn't reach the safepoint for debugging
 830     // purposes (useful when there are lots of threads in the debugger).
 831     tty->print_cr("");
 832     tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
 833     if (reason ==  _spinning_timeout) {
 834       tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
 835     } else if (reason == _blocking_timeout) {
 836       tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
 837     }
 838 
 839     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
 840     ThreadSafepointState *cur_state;
 841     ResourceMark rm;
 842     for(JavaThread *cur_thread = Threads::first(); cur_thread;
 843         cur_thread = cur_thread->next()) {
 844       cur_state = cur_thread->safepoint_state();
 845 
 846       if (cur_thread->thread_state() != _thread_blocked &&
 847           ((reason == _spinning_timeout && cur_state->is_running()) ||
 848            (reason == _blocking_timeout && !cur_state->has_called_back()))) {
 849         tty->print("# ");


1076     // Block the thread
1077     SafepointSynchronize::block(thread());
1078     set_at_poll_safepoint(false);
1079 
1080     // If we have a pending async exception deoptimize the frame
1081     // as otherwise we may never deliver it.
1082     if (thread()->has_async_condition()) {
1083       ThreadInVMfromJavaNoAsyncException __tiv(thread());
1084       Deoptimization::deoptimize_frame(thread(), caller_fr.id());
1085     }
1086 
1087     // If an exception has been installed we must check for a pending deoptimization
1088     // Deoptimize frame if exception has been thrown.
1089 
1090     if (thread()->has_pending_exception() ) {
1091       RegisterMap map(thread(), true);
1092       frame caller_fr = stub_fr.sender(&map);
1093       if (caller_fr.is_deoptimized_frame()) {
1094         // The exception patch will destroy registers that are still
1095         // live and will be needed during deoptimization. Defer the
1096         // Async exception should have defered the exception until the
1097         // next safepoint which will be detected when we get into
1098         // the interpreter so if we have an exception now things
1099         // are messed up.
1100 
1101         fatal("Exception installed and deoptimization is pending");
1102       }
1103     }
1104   }
1105 }
1106 
1107 
1108 //
1109 //                     Statistics & Instrumentations
1110 //
1111 SafepointSynchronize::SafepointStats*  SafepointSynchronize::_safepoint_stats = NULL;
1112 jlong  SafepointSynchronize::_safepoint_begin_time = 0;
1113 int    SafepointSynchronize::_cur_stat_index = 0;
1114 julong SafepointSynchronize::_safepoint_reasons[VM_Operation::VMOp_Terminating];
1115 julong SafepointSynchronize::_coalesced_vmop_count = 0;
1116 jlong  SafepointSynchronize::_max_sync_time = 0;




 139   int still_running = nof_threads;
 140 
 141   // Save the starting time, so that it can be compared to see if this has taken
 142   // too long to complete.
 143   jlong safepoint_limit_time;
 144   timeout_error_printed = false;
 145 
 146   // PrintSafepointStatisticsTimeout can be specified separately. When
 147   // specified, PrintSafepointStatistics will be set to true in
 148   // deferred_initialize_stat method. The initialization has to be done
 149   // early enough to avoid any races. See bug 6880029 for details.
 150   if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
 151     deferred_initialize_stat();
 152   }
 153 
 154   // Begin the process of bringing the system to a safepoint.
 155   // Java threads can be in several different states and are
 156   // stopped by different mechanisms:
 157   //
 158   //  1. Running interpreted
 159   //     The interpreter dispatch table is changed to force it to
 160   //     check for a safepoint condition between bytecodes.
 161   //  2. Running in native code
 162   //     When returning from the native code, a Java thread must check
 163   //     the safepoint _state to see if we must block.  If the
 164   //     VM thread sees a Java thread in native, it does
 165   //     not wait for this thread to block.  The order of the memory
 166   //     writes and reads of both the safepoint state and the Java
 167   //     threads state is critical.  In order to guarantee that the
 168   //     memory writes are serialized with respect to each other,
 169   //     the VM thread issues a memory barrier instruction
 170   //     (on MP systems).  In order to avoid the overhead of issuing
 171   //     a memory barrier for each Java thread making native calls, each Java
 172   //     thread performs a write to a single memory page after changing
 173   //     the thread state.  The VM thread performs a sequence of
 174   //     mprotect OS calls which forces all previous writes from all
 175   //     Java threads to be serialized.  This is done in the
 176   //     os::serialize_thread_states() call.  This has proven to be
 177   //     much more efficient than executing a membar instruction
 178   //     on every call to native code.
 179   //  3. Running compiled Code


 265       // Yield_all() is implemented as a short unconditional sleep on some platforms.
 266       // Typical operating systems round a "short" sleep period up to 10 msecs, so sleeping
 267       // can actually increase the time it takes the VM thread to detect that a system-wide
 268       // stop-the-world safepoint has been reached.  In a pathological scenario such as that
 269       // described in CR6415670 the VMthread may sleep just before the mutator(s) become safe.
 270       // In that case the mutators will be stalled waiting for the safepoint to complete and the
 271       // the VMthread will be sleeping, waiting for the mutators to rendezvous.  The VMthread
 272       // will eventually wake up and detect that all mutators are safe, at which point
 273       // we'll again make progress.
 274       //
 275       // Beware too that that the VMThread typically runs at elevated priority.
 276       // Its default priority is higher than the default mutator priority.
 277       // Obviously, this complicates spinning.
 278       //
 279       // Note too that on Windows XP SwitchThreadTo() has quite different behavior than Sleep(0).
 280       // Sleep(0) will _not yield to lower priority threads, while SwitchThreadTo() will.
 281       //
 282       // See the comments in synchronizer.cpp for additional remarks on spinning.
 283       //
 284       // In the future we might:
 285       // 1. Modify the safepoint scheme to avoid potentially unbounded spinning.
 286       //    This is tricky as the path used by a thread exiting the JVM (say on
 287       //    on JNI call-out) simply stores into its state field.  The burden
 288       //    is placed on the VM thread, which must poll (spin).
 289       // 2. Find something useful to do while spinning.  If the safepoint is GC-related
 290       //    we might aggressively scan the stacks of threads that are already safe.
 291       // 3. Use Solaris schedctl to examine the state of the still-running mutators.
 292       //    If all the mutators are ONPROC there's no reason to sleep or yield.
 293       // 4. YieldTo() any still-running mutators that are ready but OFFPROC.
 294       // 5. Check system saturation.  If the system is not fully saturated then
 295       //    simply spin and avoid sleep/yield.
 296       // 6. As still-running mutators rendezvous they could unpark the sleeping
 297       //    VMthread.  This works well for still-running mutators that become
 298       //    safe.  The VMthread must still poll for mutators that call-out.
 299       // 7. Drive the policy on time-since-begin instead of iterations.
 300       // 8. Consider making the spin duration a function of the # of CPUs:
 301       //    Spin = (((ncpus-1) * M) + K) + F(still_running)
 302       //    Alternately, instead of counting iterations of the outer loop
 303       //    we could count the # of threads visited in the inner loop, above.
 304       // 9. On windows consider using the return value from SwitchThreadTo()
 305       //    to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.


 472       assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
 473       cur_state->restart();
 474       assert(cur_state->is_running(), "safepoint state has not been reset");
 475     }
 476 
 477     RuntimeService::record_safepoint_end();
 478 
 479     // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
 480     // blocked in signal_thread_blocked
 481     Threads_lock->unlock();
 482 
 483   }
 484 #if INCLUDE_ALL_GCS
 485   // If there are any concurrent GC threads resume them.
 486   if (UseConcMarkSweepGC) {
 487     ConcurrentMarkSweepThread::desynchronize(false);
 488   } else if (UseG1GC) {
 489     ConcurrentGCThread::safepoint_desynchronize();
 490   }
 491 #endif // INCLUDE_ALL_GCS
 492   // record this time so VMThread can keep track how much time has elapsed
 493   // since last safepoint.
 494   _end_of_last_safepoint = os::javaTimeMillis();
 495 }
 496 
 497 bool SafepointSynchronize::is_cleanup_needed() {
 498   // Need a safepoint if some inline cache buffers is non-empty
 499   if (!InlineCacheBuffer::is_empty()) return true;
 500   return false;
 501 }
 502 
 503 
 504 
 505 // Various cleaning tasks that should be done periodically at safepoints
 506 void SafepointSynchronize::do_cleanup_tasks() {
 507   {
 508     TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime);
 509     ObjectSynchronizer::deflate_idle_monitors();
 510   }
 511 
 512   {


 809   */
 810 
 811   if (ShowSafepointMsgs) {
 812     tty->print("handle_polling_page_exception: ");
 813   }
 814 
 815   if (PrintSafepointStatistics) {
 816     inc_page_trap_count();
 817   }
 818 
 819   ThreadSafepointState* state = thread->safepoint_state();
 820 
 821   state->handle_polling_page_exception();
 822   // print_me(sp,stack_copy,was_oops);
 823 }
 824 
 825 
 826 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
 827   if (!timeout_error_printed) {
 828     timeout_error_printed = true;
 829     // Print out the thread info which didn't reach the safepoint for debugging
 830     // purposes (useful when there are lots of threads in the debugger).
 831     tty->print_cr("");
 832     tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
 833     if (reason ==  _spinning_timeout) {
 834       tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
 835     } else if (reason == _blocking_timeout) {
 836       tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
 837     }
 838 
 839     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
 840     ThreadSafepointState *cur_state;
 841     ResourceMark rm;
 842     for(JavaThread *cur_thread = Threads::first(); cur_thread;
 843         cur_thread = cur_thread->next()) {
 844       cur_state = cur_thread->safepoint_state();
 845 
 846       if (cur_thread->thread_state() != _thread_blocked &&
 847           ((reason == _spinning_timeout && cur_state->is_running()) ||
 848            (reason == _blocking_timeout && !cur_state->has_called_back()))) {
 849         tty->print("# ");


1076     // Block the thread
1077     SafepointSynchronize::block(thread());
1078     set_at_poll_safepoint(false);
1079 
1080     // If we have a pending async exception deoptimize the frame
1081     // as otherwise we may never deliver it.
1082     if (thread()->has_async_condition()) {
1083       ThreadInVMfromJavaNoAsyncException __tiv(thread());
1084       Deoptimization::deoptimize_frame(thread(), caller_fr.id());
1085     }
1086 
1087     // If an exception has been installed we must check for a pending deoptimization
1088     // Deoptimize frame if exception has been thrown.
1089 
1090     if (thread()->has_pending_exception() ) {
1091       RegisterMap map(thread(), true);
1092       frame caller_fr = stub_fr.sender(&map);
1093       if (caller_fr.is_deoptimized_frame()) {
1094         // The exception patch will destroy registers that are still
1095         // live and will be needed during deoptimization. Defer the
1096         // Async exception should have deferred the exception until the
1097         // next safepoint which will be detected when we get into
1098         // the interpreter so if we have an exception now things
1099         // are messed up.
1100 
1101         fatal("Exception installed and deoptimization is pending");
1102       }
1103     }
1104   }
1105 }
1106 
1107 
1108 //
1109 //                     Statistics & Instrumentations
1110 //
1111 SafepointSynchronize::SafepointStats*  SafepointSynchronize::_safepoint_stats = NULL;
1112 jlong  SafepointSynchronize::_safepoint_begin_time = 0;
1113 int    SafepointSynchronize::_cur_stat_index = 0;
1114 julong SafepointSynchronize::_safepoint_reasons[VM_Operation::VMOp_Terminating];
1115 julong SafepointSynchronize::_coalesced_vmop_count = 0;
1116 jlong  SafepointSynchronize::_max_sync_time = 0;


src/share/vm/runtime/safepoint.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File