safepoint.cpp revision 1825
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7e6c9a9a733f7a57ace98e4692573f42a2cad0edBob Halley * This code is free software; you can redistribute it and/or modify it
7e6c9a9a733f7a57ace98e4692573f42a2cad0edBob Halley * under the terms of the GNU General Public License version 2 only, as
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * published by the Free Software Foundation.
15a44745412679c30a6d022733925af70a38b715David Lawrence * This code is distributed in the hope that it will be useful, but WITHOUT
15a44745412679c30a6d022733925af70a38b715David Lawrence * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15a44745412679c30a6d022733925af70a38b715David Lawrence * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15a44745412679c30a6d022733925af70a38b715David Lawrence * version 2 for more details (a copy is included in the LICENSE file that
15a44745412679c30a6d022733925af70a38b715David Lawrence * accompanied this code).
15a44745412679c30a6d022733925af70a38b715David Lawrence * You should have received a copy of the GNU General Public License version
7e6c9a9a733f7a57ace98e4692573f42a2cad0edBob Halley * 2 along with this work; if not, write to the Free Software Foundation,
7e6c9a9a733f7a57ace98e4692573f42a2cad0edBob Halley * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * or visit www.oracle.com if you need additional information or have any
28fc90e6c81338c5f34e065fdda49d320e362583Mark Andrews# include "incls/_precompiled.incl"
559b10cc8f3e1dc4d93f55c9336f74839e9fa362Andreas Gustafsson# include "incls/_safepoint.cpp.incl"
7e6c9a9a733f7a57ace98e4692573f42a2cad0edBob Halley// --------------------------------------------------------------------------------------------------
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson// Implementation of Safepoint begin/end
6017f424ee3c02d7f22132c77576ea38542fa949Andreas GustafssonSafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
f1eda76d36a9744ab47007a1666197b375483912Andreas Gustafssonvolatile int SafepointSynchronize::_waiting_to_block = 0;
ad82fd0a25eb13b3b9c3cfa1d8e614656d43b14dDanny Mayervolatile int SafepointSynchronize::_safepoint_counter = 0;
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafssonlong SafepointSynchronize::_end_of_last_safepoint = 0;
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafssonstatic volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
4d9b99ccb25be2d74644b150372c208e24f191f5Andreas Gustafssonstatic volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafssonstatic bool timeout_error_printed = false;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson// Roll all threads forward to a safepoint and suspend them all
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
ad61cbe04f3660c2db07a9d288a2594319cea90bAndreas Gustafsson if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
143c2d39fafc0e36e0fa291b7708b60309e5a1b6Andreas Gustafsson _safepoint_begin_time = os::javaTimeNanos();
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson _ts_of_current_safepoint = tty->time_stamp().seconds();
582732ce15d48cb521a32a551de3ce5ad2cc3290Andreas Gustafsson // In the future we should investigate whether CMS can use the
65a66336a6e2ce2032f68139ef9c8afe18d08c14Andreas Gustafsson // more-general mechanism below. DLD (01/05).
65a66336a6e2ce2032f68139ef9c8afe18d08c14Andreas Gustafsson ConcurrentMarkSweepThread::synchronize(false);
65a66336a6e2ce2032f68139ef9c8afe18d08c14Andreas Gustafsson ConcurrentGCThread::safepoint_synchronize();
a1747570262ed336c213aaf6bd31bc91993a46deAndreas Gustafsson#endif // SERIALGC
582732ce15d48cb521a32a551de3ce5ad2cc3290Andreas Gustafsson // By getting the Threads_lock, we assure that no threads are about to start or
d22b4de3f1c3151979e2a064cb271007c710c5a7Mark Andrews // exit. It is released again in SafepointSynchronize::end().
e8e73ca025c558c0b62026bca758c3621c45598aAndreas Gustafsson assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
d4e608c5bc38af9d50fed2e89442ed05dd3c335fBrian Wellington int nof_threads = Threads::number_of_threads();
0fc89c4ee660e825ac66774f2d4912cfc396386aMark Andrews tty->print_cr("Safepoint synchronization initiated. (%d)", nof_threads);
d3be9a9c6ef76a5d7671b0962785ca025b153d2bAndreas Gustafsson // Set number of threads to wait for, before we initiate the callbacks
eb8713ed947fdf22a41dad673d561896dd6fe4a2Brian Wellington // Save the starting time, so that it can be compared to see if this has taken
532989b206894bdaf6de6cb883d2e31169c4bfacAndreas Gustafsson // too long to complete.
28fc90e6c81338c5f34e065fdda49d320e362583Mark Andrews // PrintSafepointStatisticsTimeout can be specified separately. When
28fc90e6c81338c5f34e065fdda49d320e362583Mark Andrews // specified, PrintSafepointStatistics will be set to true in
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson // deferred_initialize_stat method. The initialization has to be done
c0564c15e73fc366a9200fc47dfcc7894382aa98Andreas Gustafsson // early enough to avoid any races. See bug 6880029 for details.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
c0564c15e73fc366a9200fc47dfcc7894382aa98Andreas Gustafsson // Begin the process of bringing the system to a safepoint.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson // Java threads can be in several different states and are
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson // stopped by different mechanisms:
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson // 1. Running interpreted
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson // The interpeter dispatch table is changed to force it to
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson // check for a safepoint condition between bytecodes.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson // 2. Running in native code
b8257bd2d872546e7cc358fdf32331ce8850bca4Bob Halley // When returning from the native code, a Java thread must check
b8257bd2d872546e7cc358fdf32331ce8850bca4Bob Halley // the safepoint _state to see if we must block. If the
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafsson // VM thread sees a Java thread in native, it does
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafsson // not wait for this thread to block. The order of the memory
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafsson // writes and reads of both the safepoint state and the Java
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafsson // threads state is critical. In order to guarantee that the
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafsson // memory writes are serialized with respect to each other,
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafsson // the VM thread issues a memory barrier instruction
b45b364f2255f4abfec674e5ff94b803e34b3e83Andreas Gustafsson // (on MP systems). In order to avoid the overhead of issuing
7e6c9a9a733f7a57ace98e4692573f42a2cad0edBob Halley // a memory barrier for each Java thread making native calls, each Java
2a34beb5ab81dd9420cb1b007df32c08d93eda81Mark Andrews // thread performs a write to a single memory page after changing
7e4135abb92a44425e9a653b1623db7cb2837c3cMark Andrews // the thread state. The VM thread performs a sequence of
2a34beb5ab81dd9420cb1b007df32c08d93eda81Mark Andrews // mprotect OS calls which forces all previous writes from all
2a34beb5ab81dd9420cb1b007df32c08d93eda81Mark Andrews // Java threads to be serialized. This is done in the
2a34beb5ab81dd9420cb1b007df32c08d93eda81Mark Andrews // os::serialize_thread_states() call. This has proven to be
44fee668021c7ceef4ee1c848031d883a508b359James Brister // much more efficient than executing a membar instruction
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer // on every call to native code.
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson // 3. Running compiled Code
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson // Compiled code reads a global (Safepoint Polling) page that
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson // is set to fault if we are trying to get to a safepoint.
94b50bce2b5deeac93734457d5474736d7b76af1Michael Sawyer // A thread which is blocked will not be allowed to return from the
9dafd058e3cfdd8218247811cea792588ec19052Andreas Gustafsson // block condition until the safepoint operation is complete.
9dafd058e3cfdd8218247811cea792588ec19052Andreas Gustafsson // 5. In VM or Transitioning between states
9dafd058e3cfdd8218247811cea792588ec19052Andreas Gustafsson // If a Java thread is currently running in the VM or transitioning
9dafd058e3cfdd8218247811cea792588ec19052Andreas Gustafsson // between states, the safepointing code will wait for the thread to
9dafd058e3cfdd8218247811cea792588ec19052Andreas Gustafsson // block itself when it attempts transitions to a new state.
984ca288f1291c7b7bda9b3809a7af714e3ec82aAndreas Gustafsson // Flush all thread states to memory
5fe21da364d4397c9a413fe689ce82dea36a7b29Mark Andrews // Make interpreter safepoint aware
5fe21da364d4397c9a413fe689ce82dea36a7b29Mark Andrews if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
d3be9a9c6ef76a5d7671b0962785ca025b153d2bAndreas Gustafsson // Make polling safepoint aware
d3be9a9c6ef76a5d7671b0962785ca025b153d2bAndreas Gustafsson guarantee (PageArmed == 0, "invariant") ;
ce1f5b8d0ae5936fd187c1f414ff12a7e3b0aa37Andreas Gustafsson // Consider using active_processor_count() ... but that call is expensive.
a3a11c4f3fc9ba972802b811c4d95a9884d6ff4aMichael Sawyer for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
a3a11c4f3fc9ba972802b811c4d95a9884d6ff4aMichael Sawyer assert(cur->safepoint_state()->is_running(), "Illegal initial state");
eb8713ed947fdf22a41dad673d561896dd6fe4a2Brian Wellington#endif // ASSERT
eb8713ed947fdf22a41dad673d561896dd6fe4a2Brian Wellington safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
dfceef7e68b56f6970dd2e8eea9980ad16bcc653Brian Wellington // Iterate through all threads until it have been determined how to stop them all at a safepoint
dfceef7e68b56f6970dd2e8eea9980ad16bcc653Brian Wellington unsigned int iterations = 0;
dfceef7e68b56f6970dd2e8eea9980ad16bcc653Brian Wellington for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
dfceef7e68b56f6970dd2e8eea9980ad16bcc653Brian Wellington assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington ThreadSafepointState *cur_state = cur->safepoint_state();
c20ffa38dee7efa0dc01822d4bac5e41729b9b61Brian Wellington // consider adjusting steps downward:
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington // steps -= NNN
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington // steps >>= 1
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington // steps = MIN(steps, 2000-100)
e4cd5a1e5d0358abeee7618b02b4592c055d957fBrian Wellington // if (iterations != 0) steps -= NNN
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (TraceSafepoint && Verbose) cur_state->print();
if (still_running > 0) {
// See the comments in synchronizer.cpp for additional remarks on spinning.
// to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.
++steps ;
iterations ++ ;
if (PrintSafepointStatistics) {
while (_waiting_to_block > 0) {
#ifndef PRODUCT
if (SafepointTimeout) {
if (TraceSafepoint) {
if (PrintSafepointStatistics) {
if (PrintSafepointStatistics) {
if (PrintSafepointStatistics) {
#ifdef ASSERT
if (PageArmed) {
PageArmed = 0 ;
if (TraceSafepoint) {
if (VMThreadHintNoPreempt) {
#ifndef SERIALGC
if (UseConcMarkSweepGC) {
} else if (UseG1GC) {
switch(state) {
case _thread_in_native:
case _thread_blocked:
assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
// -------------------------------------------------------------------------------------------------------
switch(state) {
case _thread_in_vm_trans:
if (is_synchronizing()) {
if (is_synchronizing()) {
if (_waiting_to_block == 0) {
case _thread_in_native_trans:
case _thread_blocked_trans:
case _thread_new_trans:
// ------------------------------------------------------------------------------------------------------
#ifndef PRODUCT
#ifdef _LP64
newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" "));
newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" "));
#ifdef SPARC
#ifdef _LP64
tty->print("blob %c%d |"PTR_FORMAT" ","LO"[i>>3],i&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
tty->print(" G1 |"PTR_FORMAT" ",new_sp); print_longs(*(jlong*)old_sp,*(jlong*)new_sp,was_oops[incr-1]); old_sp += incr; new_sp += incr; was_oops += incr;
tty->print(" G3 |"PTR_FORMAT" ",new_sp); print_longs(*(jlong*)old_sp,*(jlong*)new_sp,was_oops[incr-1]); old_sp += incr; new_sp += incr; was_oops += incr;
tty->print(" G4 |"PTR_FORMAT" ",new_sp); print_longs(*(jlong*)old_sp,*(jlong*)new_sp,was_oops[incr-1]); old_sp += incr; new_sp += incr; was_oops += incr;
tty->print(" G5 |"PTR_FORMAT" ",new_sp); print_longs(*(jlong*)old_sp,*(jlong*)new_sp,was_oops[incr-1]); old_sp += incr; new_sp += incr; was_oops += incr;
tty->print_cr(" FSR |"PTR_FORMAT" "PTR64_FORMAT" "PTR64_FORMAT,new_sp,*(jlong*)old_sp,*(jlong*)new_sp);
tty->print("call %c%d |"PTR_FORMAT" ","LI"[i2>>3],i2&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
if (ShowSafepointMsgs) {
if (PrintSafepointStatistics) {
if (!timeout_error_printed) {
timeout_error_printed = true;
if (DieOnSafepointTimeout) {
// -------------------------------------------------------------------------------------------------------
_has_called_back = false;
_at_poll_safepoint = false;
if (is_suspended) {
switch(_type) {
case _at_safepoint:
case _call_back:
set_has_called_back(false);
case _running:
switch(type()) {
case _at_safepoint:
case _call_back:
case _running:
set_has_called_back(false);
switch(_type) {
// ---------------------------------------------------------------------------------------------------------------------
if (return_oop) {
if (return_oop) {
set_at_poll_safepoint(true);
set_at_poll_safepoint(false);
static bool need_to_track_page_armed_status = false;
static bool init_done = false;
static void print_header() {
if (init_done) return;
if (PrintSafepointStatisticsCount <= 0) {
int stats_array_size;
if (PrintSafepointStatisticsTimeout > 0) {
PrintSafepointStatistics = true;
* sizeof(SafepointStats));
need_to_track_page_armed_status = true;
init_done = true;
if (nof_running != 0) {
if (_waiting_to_block != 0) {
if ( PrintSafepointStatisticsTimeout > 0) {
_cur_stat_index = 0;
print_header();
if ( PrintSafepointStatisticsTimeout < 0 ||
if (!need_to_track_page_armed_status) {
if (UseCompilerSafepoints) {
#ifndef PRODUCT
if (ShowSafepointMsgs) {