safepoint.cpp revision 6628:45831d971923
18876Srgrimes/*
24Srgrimes * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
34Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44Srgrimes *
58876Srgrimes * This code is free software; you can redistribute it and/or modify it
64Srgrimes * under the terms of the GNU General Public License version 2 only, as
74Srgrimes * published by the Free Software Foundation.
84Srgrimes *
94Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
104Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
118876Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
128876Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
134Srgrimes * accompanied this code).
144Srgrimes *
158876Srgrimes * You should have received a copy of the GNU General Public License version
164Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
178876Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
184Srgrimes *
194Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
204Srgrimes * or visit www.oracle.com if you need additional information or have any
214Srgrimes * questions.
228876Srgrimes *
234Srgrimes */
244Srgrimes
254Srgrimes#include "precompiled.hpp"
264Srgrimes#include "classfile/stringTable.hpp"
274Srgrimes#include "classfile/systemDictionary.hpp"
284Srgrimes#include "code/codeCache.hpp"
294Srgrimes#include "code/icBuffer.hpp"
304Srgrimes#include "code/nmethod.hpp"
314Srgrimes#include "code/pcDesc.hpp"
324Srgrimes#include "code/scopeDesc.hpp"
33116176Sobrien#include "gc_interface/collectedHeap.hpp"
34116176Sobrien#include "interpreter/interpreter.hpp"
35116176Sobrien#include "memory/resourceArea.hpp"
36116176Sobrien#include "memory/universe.inline.hpp"
372056Swollman#include "oops/oop.inline.hpp"
3842654Sjdp#include "oops/symbol.hpp"
3986998Sdd#include "runtime/compilationPolicy.hpp"
4086998Sdd#include "runtime/deoptimization.hpp"
4186998Sdd#include "runtime/frame.inline.hpp"
4217848Spst#include "runtime/interfaceSupport.hpp"
4386998Sdd#include "runtime/mutexLocker.hpp"
442056Swollman#include "runtime/orderAccess.inline.hpp"
4549558Sphk#include "runtime/osThread.hpp"
46126399Sphk#include "runtime/safepoint.hpp"
4712734Sbde#include "runtime/signature.hpp"
482056Swollman#include "runtime/stubCodeGenerator.hpp"
4912473Sbde#include "runtime/stubRoutines.hpp"
504Srgrimes#include "runtime/sweeper.hpp"
514Srgrimes#include "runtime/synchronizer.hpp"
524Srgrimes#include "runtime/thread.inline.hpp"
53118990Smarcel#include "services/memTracker.hpp"
5479418Sjulian#include "services/runtimeService.hpp"
554Srgrimes#include "utilities/events.hpp"
564Srgrimes#include "utilities/macros.hpp"
574Srgrimes#ifdef TARGET_ARCH_x86
584Srgrimes# include "nativeInst_x86.hpp"
594Srgrimes# include "vmreg_x86.inline.hpp"
6018296Sbde#endif
614Srgrimes#ifdef TARGET_ARCH_sparc
624Srgrimes# include "nativeInst_sparc.hpp"
634Srgrimes# include "vmreg_sparc.inline.hpp"
644Srgrimes#endif
654Srgrimes#ifdef TARGET_ARCH_zero
6678161Speter# include "nativeInst_zero.hpp"
6778161Speter# include "vmreg_zero.inline.hpp"
6878161Speter#endif
6912515Sphk#ifdef TARGET_ARCH_arm
7017848Spst# include "nativeInst_arm.hpp"
7186998Sdd# include "vmreg_arm.inline.hpp"
7285944Speter#endif
73126399Sphk#ifdef TARGET_ARCH_ppc
7417848Spst# include "nativeInst_ppc.hpp"
7518296Sbde# include "vmreg_ppc.inline.hpp"
7618296Sbde#endif
7718296Sbde#if INCLUDE_ALL_GCS
784Srgrimes#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
794Srgrimes#include "gc_implementation/shared/suspendibleThreadSet.hpp"
804Srgrimes#endif // INCLUDE_ALL_GCS
814Srgrimes#ifdef COMPILER1
824Srgrimes#include "c1/c1_globals.hpp"
8312515Sphk#endif
844Srgrimes
854SrgrimesPRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
864Srgrimes
874Srgrimes// --------------------------------------------------------------------------------------------------
884Srgrimes// Implementation of Safepoint begin/end
894Srgrimes
904SrgrimesSafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
914Srgrimesvolatile int  SafepointSynchronize::_waiting_to_block = 0;
924Srgrimesvolatile int SafepointSynchronize::_safepoint_counter = 0;
934Srgrimesint SafepointSynchronize::_current_jni_active_count = 0;
944Srgrimeslong  SafepointSynchronize::_end_of_last_safepoint = 0;
954Srgrimesstatic volatile int PageArmed = 0 ;        // safepoint polling page is RO|RW vs PROT_NONE
964Srgrimesstatic volatile int TryingToBlock = 0 ;    // proximate value -- for advisory use only
974Srgrimesstatic bool timeout_error_printed = false;
984Srgrimes
994Srgrimes// Roll all threads forward to a safepoint and suspend them all
1004Srgrimesvoid SafepointSynchronize::begin() {
1014Srgrimes
1024Srgrimes  Thread* myThread = Thread::current();
1034Srgrimes  assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
1044Srgrimes
1054Srgrimes  if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
10693009Sbde    _safepoint_begin_time = os::javaTimeNanos();
10793009Sbde    _ts_of_current_safepoint = tty->time_stamp().seconds();
10892756Salfred  }
10993009Sbde
11093009Sbde#if INCLUDE_ALL_GCS
11192756Salfred  if (UseConcMarkSweepGC) {
11293009Sbde    // In the future we should investigate whether CMS can use the
11393009Sbde    // more-general mechanism below.  DLD (01/05).
11412473Sbde    ConcurrentMarkSweepThread::synchronize(false);
1154Srgrimes  } else if (UseG1GC) {
1164Srgrimes    SuspendibleThreadSet::synchronize();
1174Srgrimes  }
11812515Sphk#endif // INCLUDE_ALL_GCS
11978161Speter
1204Srgrimes  // By getting the Threads_lock, we assure that no threads are about to start or
1214Srgrimes  // exit. It is released again in SafepointSynchronize::end().
12218296Sbde  Threads_lock->lock();
12378161Speter
1244Srgrimes  assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
1254Srgrimes
1264Srgrimes  int nof_threads = Threads::number_of_threads();
12718296Sbde
1284Srgrimes  if (TraceSafepoint) {
1294Srgrimes    tty->print_cr("Safepoint synchronization initiated. (%d)", nof_threads);
1304Srgrimes  }
1314Srgrimes
1324Srgrimes  RuntimeService::record_safepoint_begin();
1334Srgrimes
1344Srgrimes  MutexLocker mu(Safepoint_lock);
1354Srgrimes
1364Srgrimes  // Reset the count of active JNI critical threads
1374Srgrimes  _current_jni_active_count = 0;
1384Srgrimes
1394Srgrimes  // Set number of threads to wait for, before we initiate the callbacks
1404Srgrimes  _waiting_to_block = nof_threads;
1414Srgrimes  TryingToBlock     = 0 ;
1424Srgrimes  int still_running = nof_threads;
1434Srgrimes
1444Srgrimes  // Save the starting time, so that it can be compared to see if this has taken
1454Srgrimes  // too long to complete.
1464Srgrimes  jlong safepoint_limit_time;
1474Srgrimes  timeout_error_printed = false;
1484Srgrimes
1494Srgrimes  // PrintSafepointStatisticsTimeout can be specified separately. When
1504Srgrimes  // specified, PrintSafepointStatistics will be set to true in
1514Srgrimes  // deferred_initialize_stat method. The initialization has to be done
1524Srgrimes  // early enough to avoid any races. See bug 6880029 for details.
1534Srgrimes  if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
1544Srgrimes    deferred_initialize_stat();
1554Srgrimes  }
1564Srgrimes
1574Srgrimes  // Begin the process of bringing the system to a safepoint.
1584Srgrimes  // Java threads can be in several different states and are
1594Srgrimes  // stopped by different mechanisms:
16018296Sbde  //
16118296Sbde  //  1. Running interpreted
16278161Speter  //     The interpreter dispatch table is changed to force it to
16318296Sbde  //     check for a safepoint condition between bytecodes.
16418296Sbde  //  2. Running in native code
16518296Sbde  //     When returning from the native code, a Java thread must check
16618296Sbde  //     the safepoint _state to see if we must block.  If the
16718296Sbde  //     VM thread sees a Java thread in native, it does
16818296Sbde  //     not wait for this thread to block.  The order of the memory
16918296Sbde  //     writes and reads of both the safepoint state and the Java
17018296Sbde  //     threads state is critical.  In order to guarantee that the
17118296Sbde  //     memory writes are serialized with respect to each other,
17218296Sbde  //     the VM thread issues a memory barrier instruction
17318296Sbde  //     (on MP systems).  In order to avoid the overhead of issuing
17418296Sbde  //     a memory barrier for each Java thread making native calls, each Java
17518296Sbde  //     thread performs a write to a single memory page after changing
17618296Sbde  //     the thread state.  The VM thread performs a sequence of
17718296Sbde  //     mprotect OS calls which forces all previous writes from all
17818296Sbde  //     Java threads to be serialized.  This is done in the
17918296Sbde  //     os::serialize_thread_states() call.  This has proven to be
18018296Sbde  //     much more efficient than executing a membar instruction
18118296Sbde  //     on every call to native code.
18218296Sbde  //  3. Running compiled Code
18318296Sbde  //     Compiled code reads a global (Safepoint Polling) page that
18418296Sbde  //     is set to fault if we are trying to get to a safepoint.
18518296Sbde  //  4. Blocked
18618296Sbde  //     A thread which is blocked will not be allowed to return from the
18718296Sbde  //     block condition until the safepoint operation is complete.
18818296Sbde  //  5. In VM or Transitioning between states
18918296Sbde  //     If a Java thread is currently running in the VM or transitioning
19018296Sbde  //     between states, the safepointing code will wait for the thread to
19118296Sbde  //     block itself when it attempts transitions to a new state.
1924Srgrimes  //
1934Srgrimes  _state            = _synchronizing;
1944Srgrimes  OrderAccess::fence();
1954Srgrimes
1964Srgrimes  // Flush all thread states to memory
1974Srgrimes  if (!UseMembar) {
1984Srgrimes    os::serialize_thread_states();
1994Srgrimes  }
2004Srgrimes
20112515Sphk  // Make interpreter safepoint aware
20278161Speter  Interpreter::notice_safepoints();
2034Srgrimes
20418296Sbde  if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
20578161Speter    // Make polling safepoint aware
2064Srgrimes    guarantee (PageArmed == 0, "invariant") ;
2074Srgrimes    PageArmed = 1 ;
20818296Sbde    os::make_polling_page_unreadable();
2094Srgrimes  }
2104Srgrimes
2114Srgrimes  // Consider using active_processor_count() ... but that call is expensive.
2124Srgrimes  int ncpus = os::processor_count() ;
2134Srgrimes
21418296Sbde#ifdef ASSERT
21518296Sbde  for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
21678161Speter    assert(cur->safepoint_state()->is_running(), "Illegal initial state");
21718296Sbde    // Clear the visited flag to ensure that the critical counts are collected properly.
21818296Sbde    cur->set_visited_for_critical_count(false);
21918296Sbde  }
2204Srgrimes#endif // ASSERT
2214Srgrimes
22212515Sphk  if (SafepointTimeout)
22378161Speter    safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
2244Srgrimes
2254Srgrimes  // Iterate through all threads until it have been determined how to stop them all at a safepoint
22618296Sbde  unsigned int iterations = 0;
22778161Speter  int steps = 0 ;
2284Srgrimes  while(still_running > 0) {
2294Srgrimes    for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
2304Srgrimes      assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
2314Srgrimes      ThreadSafepointState *cur_state = cur->safepoint_state();
2324Srgrimes      if (cur_state->is_running()) {
233798Swollman        cur_state->examine_state_of_thread();
2344Srgrimes        if (!cur_state->is_running()) {
2354Srgrimes           still_running--;
2364Srgrimes           // consider adjusting steps downward:
2374Srgrimes           //   steps = 0
2384Srgrimes           //   steps -= NNN
2394Srgrimes           //   steps >>= 1
2404Srgrimes           //   steps = MIN(steps, 2000-100)
2414Srgrimes           //   if (iterations != 0) steps -= NNN
2424Srgrimes        }
2434Srgrimes        if (TraceSafepoint && Verbose) cur_state->print();
2444Srgrimes      }
2454Srgrimes    }
24610348Sbde
2474Srgrimes    if (PrintSafepointStatistics && iterations == 0) {
2484Srgrimes      begin_statistics(nof_threads, still_running);
2494Srgrimes    }
2504Srgrimes
2514Srgrimes    if (still_running > 0) {
2524Srgrimes      // Check for if it takes to long
2534Srgrimes      if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
2544Srgrimes        print_safepoint_timeout(_spinning_timeout);
2554Srgrimes      }
2564Srgrimes
2574Srgrimes      // Spin to avoid context switching.
2584Srgrimes      // There's a tension between allowing the mutators to run (and rendezvous)
2594Srgrimes      // vs spinning.  As the VM thread spins, wasting cycles, it consumes CPU that
2604Srgrimes      // a mutator might otherwise use profitably to reach a safepoint.  Excessive
26118296Sbde      // spinning by the VM thread on a saturated system can increase rendezvous latency.
26278161Speter      // Blocking or yielding incur their own penalties in the form of context switching
2634Srgrimes      // and the resultant loss of $ residency.
2644Srgrimes      //
2654Srgrimes      // Further complicating matters is that yield() does not work as naively expected
2664Srgrimes      // on many platforms -- yield() does not guarantee that any other ready threads
2674Srgrimes      // will run.   As such we revert to naked_short_sleep() after some number of iterations.
2684Srgrimes      // nakes_short_sleep() is implemented as a short unconditional sleep.
2694Srgrimes      // Typical operating systems round a "short" sleep period up to 10 msecs, so sleeping
2704Srgrimes      // can actually increase the time it takes the VM thread to detect that a system-wide
2714Srgrimes      // stop-the-world safepoint has been reached.  In a pathological scenario such as that
2724Srgrimes      // described in CR6415670 the VMthread may sleep just before the mutator(s) become safe.
2734Srgrimes      // In that case the mutators will be stalled waiting for the safepoint to complete and the
27478161Speter      // the VMthread will be sleeping, waiting for the mutators to rendezvous.  The VMthread
2754Srgrimes      // will eventually wake up and detect that all mutators are safe, at which point
2764Srgrimes      // we'll again make progress.
2774Srgrimes      //
2784Srgrimes      // Beware too that that the VMThread typically runs at elevated priority.
2794Srgrimes      // Its default priority is higher than the default mutator priority.
2804Srgrimes      // Obviously, this complicates spinning.
28118296Sbde      //
28218296Sbde      // Note too that on Windows XP SwitchThreadTo() has quite different behavior than Sleep(0).
283104321Sphk      // Sleep(0) will _not yield to lower priority threads, while SwitchThreadTo() will.
28478161Speter      //
28578161Speter      // See the comments in synchronizer.cpp for additional remarks on spinning.
286104321Sphk      //
28718296Sbde      // In the future we might:
2884Srgrimes      // 1. Modify the safepoint scheme to avoid potentially unbounded spinning.
2894Srgrimes      //    This is tricky as the path used by a thread exiting the JVM (say on
29078161Speter      //    on JNI call-out) simply stores into its state field.  The burden
2914Srgrimes      //    is placed on the VM thread, which must poll (spin).
2924Srgrimes      // 2. Find something useful to do while spinning.  If the safepoint is GC-related
2934Srgrimes      //    we might aggressively scan the stacks of threads that are already safe.
2944Srgrimes      // 3. Use Solaris schedctl to examine the state of the still-running mutators.
2954Srgrimes      //    If all the mutators are ONPROC there's no reason to sleep or yield.
2964Srgrimes      // 4. YieldTo() any still-running mutators that are ready but OFFPROC.
2974Srgrimes      // 5. Check system saturation.  If the system is not fully saturated then
2984Srgrimes      //    simply spin and avoid sleep/yield.
2994Srgrimes      // 6. As still-running mutators rendezvous they could unpark the sleeping
3004Srgrimes      //    VMthread.  This works well for still-running mutators that become
3014Srgrimes      //    safe.  The VMthread must still poll for mutators that call-out.
3024Srgrimes      // 7. Drive the policy on time-since-begin instead of iterations.
3034Srgrimes      // 8. Consider making the spin duration a function of the # of CPUs:
3044Srgrimes      //    Spin = (((ncpus-1) * M) + K) + F(still_running)
3054Srgrimes      //    Alternately, instead of counting iterations of the outer loop
3064Srgrimes      //    we could count the # of threads visited in the inner loop, above.
3074Srgrimes      // 9. On windows consider using the return value from SwitchThreadTo()
3084Srgrimes      //    to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.
3094Srgrimes
3104Srgrimes      if (UseCompilerSafepoints && int(iterations) == DeferPollingPageLoopCount) {
3114Srgrimes         guarantee (PageArmed == 0, "invariant") ;
3124Srgrimes         PageArmed = 1 ;
3134Srgrimes         os::make_polling_page_unreadable();
3144Srgrimes      }
3154Srgrimes
3164Srgrimes      // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
3174Srgrimes      // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
3184Srgrimes      ++steps ;
3194Srgrimes      if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
3204Srgrimes        SpinPause() ;     // MP-Polite spin
3214Srgrimes      } else
3224Srgrimes      if (steps < DeferThrSuspendLoopCount) {
3234Srgrimes        os::NakedYield() ;
3244Srgrimes      } else {
3254Srgrimes        os::naked_short_sleep(1);
3264Srgrimes      }
3274Srgrimes
3284Srgrimes      iterations ++ ;
3294Srgrimes    }
3304Srgrimes    assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
3314Srgrimes  }
3324Srgrimes  assert(still_running == 0, "sanity check");
3334Srgrimes
3344Srgrimes  if (PrintSafepointStatistics) {
3354Srgrimes    update_statistics_on_spin_end();
3364Srgrimes  }
3374Srgrimes
3384Srgrimes  // wait until all threads are stopped
3394Srgrimes  while (_waiting_to_block > 0) {
3404Srgrimes    if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block);
3414Srgrimes    if (!SafepointTimeout || timeout_error_printed) {
3424Srgrimes      Safepoint_lock->wait(true);  // true, means with no safepoint checks
3434Srgrimes    } else {
3444Srgrimes      // Compute remaining time
3454Srgrimes      jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
3464Srgrimes
3474Srgrimes      // If there is no remaining time, then there is an error
3484Srgrimes      if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
349118268Sjhb        print_safepoint_timeout(_blocking_timeout);
3504Srgrimes      }
3514Srgrimes    }
3524Srgrimes  }
3534Srgrimes  assert(_waiting_to_block == 0, "sanity check");
3544Srgrimes
3554Srgrimes#ifndef PRODUCT
3564Srgrimes  if (SafepointTimeout) {
3574Srgrimes    jlong current_time = os::javaTimeNanos();
3584Srgrimes    if (safepoint_limit_time < current_time) {
3594Srgrimes      tty->print_cr("# SafepointSynchronize: Finished after "
3604Srgrimes                    INT64_FORMAT_W(6) " ms",
3614Srgrimes                    ((current_time - safepoint_limit_time) / MICROUNITS +
3624Srgrimes                     SafepointTimeoutDelay));
3634Srgrimes    }
3644Srgrimes  }
3654Srgrimes#endif
3664Srgrimes
3674Srgrimes  assert((_safepoint_counter & 0x1) == 0, "must be even");
3684Srgrimes  assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
3694Srgrimes  _safepoint_counter ++;
3704Srgrimes
3714Srgrimes  // Record state
3724Srgrimes  _state = _synchronized;
3734Srgrimes
3744Srgrimes  OrderAccess::fence();
3754Srgrimes
3762112Swollman#ifdef ASSERT
37712515Sphk  for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
3784Srgrimes    // make sure all the threads were visited
3791147Sguido    assert(cur->was_visited_for_critical_count(), "missed a thread");
3802320Sdg  }
3811549Srgrimes#endif // ASSERT
3824Srgrimes
3834Srgrimes  // Update the count of active JNI critical regions
3844Srgrimes  GC_locker::set_jni_lock_count(_current_jni_active_count);
38512515Sphk
3864Srgrimes  if (TraceSafepoint) {
3874Srgrimes    VM_Operation *op = VMThread::vm_operation();
3884Srgrimes    tty->print_cr("Entering safepoint region: %s", (op != NULL) ? op->name() : "no vm operation");
3894Srgrimes  }
3904Srgrimes
3914Srgrimes  RuntimeService::record_safepoint_synchronized();
3924Srgrimes  if (PrintSafepointStatistics) {
3934Srgrimes    update_statistics_on_sync_end(os::javaTimeNanos());
3944Srgrimes  }
3954Srgrimes
39612515Sphk  // Call stuff that needs to be run when a safepoint is just about to be completed
3974Srgrimes  do_cleanup_tasks();
3986920Sjoerg
3994Srgrimes  if (PrintSafepointStatistics) {
4004Srgrimes    // Record how much time spend on the above cleanup tasks
4014Srgrimes    update_statistics_on_cleanup_end(os::javaTimeNanos());
4024Srgrimes  }
4034Srgrimes}
4044Srgrimes
4054Srgrimes// Wake up all threads, so they are ready to resume execution after the safepoint
4064Srgrimes// operation has been carried out
4074Srgrimesvoid SafepointSynchronize::end() {
4084Srgrimes
4094Srgrimes  assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
41079573Sbsd  assert((_safepoint_counter & 0x1) == 1, "must be odd");
41179573Sbsd  _safepoint_counter ++;
4124Srgrimes  // memory fence isn't required here since an odd _safepoint_counter
4134Srgrimes  // value can do no harm and a fence is issued below anyway.
4144Srgrimes
4154Srgrimes  DEBUG_ONLY(Thread* myThread = Thread::current();)
4164Srgrimes  assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
4174Srgrimes
4184Srgrimes  if (PrintSafepointStatistics) {
4194Srgrimes    end_statistics(os::javaTimeNanos());
420115607Sphk  }
4214Srgrimes
4224Srgrimes#ifdef ASSERT
4231147Sguido  // A pending_exception cannot be installed during a safepoint.  The threads
42417848Spst  // may install an async exception after they come back from a safepoint into
42585944Speter  // pending_exception after they unblock.  But that should happen later.
42686998Sdd  for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
427126399Sphk    assert (!(cur->has_pending_exception() &&
4284Srgrimes              cur->safepoint_state()->is_at_poll_safepoint()),
4294Srgrimes            "safepoint installed a pending exception");
4304Srgrimes  }
43112515Sphk#endif // ASSERT
4324Srgrimes
4337090Sbde  if (PageArmed) {
4344Srgrimes    // Make polling safepoint aware
4354Srgrimes    os::make_polling_page_readable();
4364Srgrimes    PageArmed = 0 ;
4374Srgrimes  }
4384Srgrimes
4394Srgrimes  // Remove safepoint check from interpreter
4404Srgrimes  Interpreter::ignore_safepoints();
4414Srgrimes
4424Srgrimes  {
4434Srgrimes    MutexLocker mu(Safepoint_lock);
4444Srgrimes
4457090Sbde    assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
4464Srgrimes
44733296Sbde    // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
44833296Sbde    // when they get restarted.
44933296Sbde    _state = _not_synchronized;
45033296Sbde    OrderAccess::fence();
45133296Sbde
4526204Sphk    if (TraceSafepoint) {
4537170Sdg       tty->print_cr("Leaving safepoint region");
4546204Sphk    }
4556204Sphk
4566204Sphk    // Start suspended threads
4574Srgrimes    for(JavaThread *current = Threads::first(); current; current = current->next()) {
4584Srgrimes      // A problem occurring on Solaris is when attempting to restart threads
4594Srgrimes      // the first #cpus - 1 go well, but then the VMThread is preempted when we get
4604Srgrimes      // to the next one (since it has been running the longest).  We then have
4614Srgrimes      // to wait for a cpu to become available before we can continue restarting
4624Srgrimes      // threads.
4634Srgrimes      // FIXME: This causes the performance of the VM to degrade when active and with
4644Srgrimes      // large numbers of threads.  Apparently this is due to the synchronous nature
4654Srgrimes      // of suspending threads.
4664Srgrimes      //
4674Srgrimes      // TODO-FIXME: the comments above are vestigial and no longer apply.
4684Srgrimes      // Furthermore, using solaris' schedctl in this particular context confers no benefit
4694Srgrimes      if (VMThreadHintNoPreempt) {
4704Srgrimes        os::hint_no_preempt();
4714Srgrimes      }
4724Srgrimes      ThreadSafepointState* cur_state = current->safepoint_state();
4734Srgrimes      assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
4744Srgrimes      cur_state->restart();
47518296Sbde      assert(cur_state->is_running(), "safepoint state has not been reset");
47678161Speter    }
4774Srgrimes
4784Srgrimes    RuntimeService::record_safepoint_end();
4794Srgrimes
4804Srgrimes    // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
4814Srgrimes    // blocked in signal_thread_blocked
482103746Smarkm    Threads_lock->unlock();
4834Srgrimes
4844Srgrimes  }
48579884Skris#if INCLUDE_ALL_GCS
4864Srgrimes  // If there are any concurrent GC threads resume them.
4874Srgrimes  if (UseConcMarkSweepGC) {
4884Srgrimes    ConcurrentMarkSweepThread::desynchronize(false);
4894Srgrimes  } else if (UseG1GC) {
4904Srgrimes    SuspendibleThreadSet::desynchronize();
4914Srgrimes  }
4924Srgrimes#endif // INCLUDE_ALL_GCS
4934Srgrimes  // record this time so VMThread can keep track how much time has elapsed
4944Srgrimes  // since last safepoint.
49512515Sphk  _end_of_last_safepoint = os::javaTimeMillis();
49610348Sbde}
49710348Sbde
49810348Sbdebool SafepointSynchronize::is_cleanup_needed() {
49910348Sbde  // Need a safepoint if some inline cache buffers is non-empty
50010348Sbde  if (!InlineCacheBuffer::is_empty()) return true;
5014Srgrimes  return false;
5024Srgrimes}
50312473Sbde
5044Srgrimes
5054Srgrimes
5064Srgrimes// Various cleaning tasks that should be done periodically at safepoints
50792756Salfredvoid SafepointSynchronize::do_cleanup_tasks() {
50893009Sbde  {
50993009Sbde    TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime);
51012473Sbde    ObjectSynchronizer::deflate_idle_monitors();
5114Srgrimes  }
5124Srgrimes
5134Srgrimes  {
5144Srgrimes    TraceTime t2("updating inline caches", TraceSafepointCleanupTime);
5154Srgrimes    InlineCacheBuffer::update_inline_caches();
5164Srgrimes  }
5174Srgrimes  {
51812473Sbde    TraceTime t3("compilation policy safepoint handler", TraceSafepointCleanupTime);
5194Srgrimes    CompilationPolicy::policy()->do_safepoint_work();
5204Srgrimes  }
5214Srgrimes
5224Srgrimes  {
5234Srgrimes    TraceTime t4("mark nmethods", TraceSafepointCleanupTime);
5244Srgrimes    NMethodSweeper::mark_active_nmethods();
5254Srgrimes  }
5264Srgrimes
5274Srgrimes  if (SymbolTable::needs_rehashing()) {
5284Srgrimes    TraceTime t5("rehashing symbol table", TraceSafepointCleanupTime);
5294Srgrimes    SymbolTable::rehash_table();
5304Srgrimes  }
5314Srgrimes
5324Srgrimes  if (StringTable::needs_rehashing()) {
5334Srgrimes    TraceTime t6("rehashing string table", TraceSafepointCleanupTime);
5344Srgrimes    StringTable::rehash_table();
5354Srgrimes  }
5364Srgrimes
5374Srgrimes  // rotate log files?
5384Srgrimes  if (UseGCLogFileRotation) {
5394Srgrimes    gclog_or_tty->rotate_log(false);
5404Srgrimes  }
5414Srgrimes
5424Srgrimes  {
5434Srgrimes    // CMS delays purging the CLDG until the beginning of the next safepoint and to
5444Srgrimes    // make sure concurrent sweep is done
5454Srgrimes    TraceTime t7("purging class loader data graph", TraceSafepointCleanupTime);
5464Srgrimes    ClassLoaderDataGraph::purge_if_needed();
5474Srgrimes  }
5484Srgrimes
5494Srgrimes  if (MemTracker::is_on()) {
5504Srgrimes    MemTracker::sync();
5514Srgrimes  }
5524Srgrimes}
55348407Speter
5544Srgrimes
55517848Spstbool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
55617848Spst  switch(state) {
55717848Spst  case _thread_in_native:
558111017Sphk    // native threads are safe if they have no java stack or have walkable stack
55946783Sphk    return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
56046783Sphk
56146658Smckusick   // blocked threads should have already have walkable stack
56217848Spst  case _thread_blocked:
56317848Spst    assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
56417848Spst    return true;
56517848Spst
56617848Spst  default:
56717848Spst    return false;
56817848Spst  }
56946658Smckusick}
570111017Sphk
57146658Smckusick
57246658Smckusick// See if the thread is running inside a lazy critical native and
57346658Smckusick// update the thread critical count if so.  Also set a suspend flag to
57446658Smckusick// cause the native wrapper to return into the JVM to do the unlock
57517848Spst// once the native finishes.
57617848Spstvoid SafepointSynchronize::check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state) {
57717848Spst  if (state == _thread_in_native &&
57817848Spst      thread->has_last_Java_frame() &&
57917848Spst      thread->frame_anchor()->walkable()) {
58017848Spst    // This thread might be in a critical native nmethod so look at
58185944Speter    // the top of the stack and increment the critical count if it
58285944Speter    // is.
58386998Sdd    frame wrapper_frame = thread->last_frame();
58486998Sdd    CodeBlob* stub_cb = wrapper_frame.cb();
58586998Sdd    if (stub_cb != NULL &&
58686998Sdd        stub_cb->is_nmethod() &&
58786998Sdd        stub_cb->as_nmethod_or_null()->is_lazy_critical_native()) {
58886998Sdd      // A thread could potentially be in a critical native across
58986998Sdd      // more than one safepoint, so only update the critical state on
59086998Sdd      // the first one.  When it returns it will perform the unlock.
59186998Sdd      if (!thread->do_critical_native_unlock()) {
59286998Sdd#ifdef ASSERT
59386998Sdd        if (!thread->in_critical()) {
59486998Sdd          GC_locker::increment_debug_jni_lock_count();
59586998Sdd        }
59686998Sdd#endif
59786998Sdd        thread->enter_critical();
59886998Sdd        // Make sure the native wrapper calls back on return to
59986998Sdd        // perform the needed critical unlock.
60086998Sdd        thread->set_critical_native_unlock();
60186998Sdd      }
60286998Sdd    }
60386998Sdd  }
60486998Sdd}
60586998Sdd
60686998Sdd
60786998Sdd
60886998Sdd// -------------------------------------------------------------------------------------------------------
60986998Sdd// Implementation of Safepoint callback point
61086998Sdd
61186998Sddvoid SafepointSynchronize::block(JavaThread *thread) {
61286998Sdd  assert(thread != NULL, "thread must be set");
61386998Sdd  assert(thread->is_Java_thread(), "not a Java thread");
61486998Sdd
61586998Sdd  // Threads shouldn't block if they are in the middle of printing, but...
61686998Sdd  ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
61786998Sdd
61886998Sdd  // Only bail from the block() call if the thread is gone from the
61986998Sdd  // thread list; starting to exit should still block.
62089442Smjacob  if (thread->is_terminated()) {
62186998Sdd     // block current thread if we come here from native code when VM is gone
62286998Sdd     thread->block_if_vm_exited();
62386998Sdd
62489442Smjacob     // otherwise do nothing
62586998Sdd     return;
62686998Sdd  }
62786998Sdd
62886998Sdd  JavaThreadState state = thread->thread_state();
62986998Sdd  thread->frame_anchor()->make_walkable(thread);
63086998Sdd
63186998Sdd  // Check that we have a valid thread_state at this point
63286998Sdd  switch(state) {
63386998Sdd    case _thread_in_vm_trans:
63486998Sdd    case _thread_in_Java:        // From compiled code
63586998Sdd
63685944Speter      // We are highly likely to block on the Safepoint_lock. In order to avoid blocking in this case,
63785944Speter      // we pretend we are still in the VM.
63885944Speter      thread->set_thread_state(_thread_in_vm);
63985944Speter
64085944Speter      if (is_synchronizing()) {
64185944Speter         Atomic::inc (&TryingToBlock) ;
64285944Speter      }
64385944Speter
64485944Speter      // We will always be holding the Safepoint_lock when we are examine the state
645126399Sphk      // of a thread. Hence, the instructions between the Safepoint_lock->lock() and
646126399Sphk      // Safepoint_lock->unlock() are happening atomic with regards to the safepoint code
647126399Sphk      Safepoint_lock->lock_without_safepoint_check();
648126399Sphk      if (is_synchronizing()) {
649126399Sphk        // Decrement the number of threads to wait for and signal vm thread
650126399Sphk        assert(_waiting_to_block > 0, "sanity check");
651126399Sphk        _waiting_to_block--;
652126399Sphk        thread->safepoint_state()->set_has_called_back(true);
653126399Sphk
654126399Sphk        DEBUG_ONLY(thread->set_visited_for_critical_count(true));
655126399Sphk        if (thread->in_critical()) {
656126399Sphk          // Notice that this thread is in a critical section
657126399Sphk          increment_jni_active_count();
658126399Sphk        }
659126399Sphk
660126399Sphk        // Consider (_waiting_to_block < 2) to pipeline the wakeup of the VM thread
661126399Sphk        if (_waiting_to_block == 0) {
662126399Sphk          Safepoint_lock->notify_all();
663        }
664      }
665
666      // We transition the thread to state _thread_blocked here, but
667      // we can't do our usual check for external suspension and then
668      // self-suspend after the lock_without_safepoint_check() call
669      // below because we are often called during transitions while
670      // we hold different locks. That would leave us suspended while
671      // holding a resource which results in deadlocks.
672      thread->set_thread_state(_thread_blocked);
673      Safepoint_lock->unlock();
674
675      // We now try to acquire the threads lock. Since this lock is hold by the VM thread during
676      // the entire safepoint, the threads will all line up here during the safepoint.
677      Threads_lock->lock_without_safepoint_check();
678      // restore original state. This is important if the thread comes from compiled code, so it
679      // will continue to execute with the _thread_in_Java state.
680      thread->set_thread_state(state);
681      Threads_lock->unlock();
682      break;
683
684    case _thread_in_native_trans:
685    case _thread_blocked_trans:
686    case _thread_new_trans:
687      if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back) {
688        thread->print_thread_state();
689        fatal("Deadlock in safepoint code.  "
690              "Should have called back to the VM before blocking.");
691      }
692
693      // We transition the thread to state _thread_blocked here, but
694      // we can't do our usual check for external suspension and then
695      // self-suspend after the lock_without_safepoint_check() call
696      // below because we are often called during transitions while
697      // we hold different locks. That would leave us suspended while
698      // holding a resource which results in deadlocks.
699      thread->set_thread_state(_thread_blocked);
700
701      // It is not safe to suspend a thread if we discover it is in _thread_in_native_trans. Hence,
702      // the safepoint code might still be waiting for it to block. We need to change the state here,
703      // so it can see that it is at a safepoint.
704
705      // Block until the safepoint operation is completed.
706      Threads_lock->lock_without_safepoint_check();
707
708      // Restore state
709      thread->set_thread_state(state);
710
711      Threads_lock->unlock();
712      break;
713
714    default:
715     fatal(err_msg("Illegal threadstate encountered: %d", state));
716  }
717
718  // Check for pending. async. exceptions or suspends - except if the
719  // thread was blocked inside the VM. has_special_runtime_exit_condition()
720  // is called last since it grabs a lock and we only want to do that when
721  // we must.
722  //
723  // Note: we never deliver an async exception at a polling point as the
724  // compiler may not have an exception handler for it. The polling
725  // code will notice the async and deoptimize and the exception will
726  // be delivered. (Polling at a return point is ok though). Sure is
727  // a lot of bother for a deprecated feature...
728  //
729  // We don't deliver an async exception if the thread state is
730  // _thread_in_native_trans so JNI functions won't be called with
731  // a surprising pending exception. If the thread state is going back to java,
732  // async exception is checked in check_special_condition_for_native_trans().
733
734  if (state != _thread_blocked_trans &&
735      state != _thread_in_vm_trans &&
736      thread->has_special_runtime_exit_condition()) {
737    thread->handle_special_runtime_exit_condition(
738      !thread->is_at_poll_safepoint() && (state != _thread_in_native_trans));
739  }
740}
741
742// ------------------------------------------------------------------------------------------------------
743// Exception handlers
744
745
746void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
747  assert(thread->is_Java_thread(), "polling reference encountered by VM thread");
748  assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
749  assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
750
751  if (ShowSafepointMsgs) {
752    tty->print("handle_polling_page_exception: ");
753  }
754
755  if (PrintSafepointStatistics) {
756    inc_page_trap_count();
757  }
758
759  ThreadSafepointState* state = thread->safepoint_state();
760
761  state->handle_polling_page_exception();
762}
763
764
765void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
766  if (!timeout_error_printed) {
767    timeout_error_printed = true;
768    // Print out the thread info which didn't reach the safepoint for debugging
769    // purposes (useful when there are lots of threads in the debugger).
770    tty->cr();
771    tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
772    if (reason ==  _spinning_timeout) {
773      tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
774    } else if (reason == _blocking_timeout) {
775      tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
776    }
777
778    tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
779    ThreadSafepointState *cur_state;
780    ResourceMark rm;
781    for(JavaThread *cur_thread = Threads::first(); cur_thread;
782        cur_thread = cur_thread->next()) {
783      cur_state = cur_thread->safepoint_state();
784
785      if (cur_thread->thread_state() != _thread_blocked &&
786          ((reason == _spinning_timeout && cur_state->is_running()) ||
787           (reason == _blocking_timeout && !cur_state->has_called_back()))) {
788        tty->print("# ");
789        cur_thread->print();
790        tty->cr();
791      }
792    }
793    tty->print_cr("# SafepointSynchronize::begin: (End of list)");
794  }
795
796  // To debug the long safepoint, specify both DieOnSafepointTimeout &
797  // ShowMessageBoxOnError.
798  if (DieOnSafepointTimeout) {
799    char msg[1024];
800    VM_Operation *op = VMThread::vm_operation();
801    sprintf(msg, "Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
802            SafepointTimeoutDelay,
803            op != NULL ? op->name() : "no vm operation");
804    fatal(msg);
805  }
806}
807
808
809// -------------------------------------------------------------------------------------------------------
810// Implementation of ThreadSafepointState
811
812ThreadSafepointState::ThreadSafepointState(JavaThread *thread) {
813  _thread = thread;
814  _type   = _running;
815  _has_called_back = false;
816  _at_poll_safepoint = false;
817}
818
819void ThreadSafepointState::create(JavaThread *thread) {
820  ThreadSafepointState *state = new ThreadSafepointState(thread);
821  thread->set_safepoint_state(state);
822}
823
824void ThreadSafepointState::destroy(JavaThread *thread) {
825  if (thread->safepoint_state()) {
826    delete(thread->safepoint_state());
827    thread->set_safepoint_state(NULL);
828  }
829}
830
831void ThreadSafepointState::examine_state_of_thread() {
832  assert(is_running(), "better be running or just have hit safepoint poll");
833
834  JavaThreadState state = _thread->thread_state();
835
836  // Save the state at the start of safepoint processing.
837  _orig_thread_state = state;
838
839  // Check for a thread that is suspended. Note that thread resume tries
840  // to grab the Threads_lock which we own here, so a thread cannot be
841  // resumed during safepoint synchronization.
842
843  // We check to see if this thread is suspended without locking to
844  // avoid deadlocking with a third thread that is waiting for this
845  // thread to be suspended. The third thread can notice the safepoint
846  // that we're trying to start at the beginning of its SR_lock->wait()
847  // call. If that happens, then the third thread will block on the
848  // safepoint while still holding the underlying SR_lock. We won't be
849  // able to get the SR_lock and we'll deadlock.
850  //
851  // We don't need to grab the SR_lock here for two reasons:
852  // 1) The suspend flags are both volatile and are set with an
853  //    Atomic::cmpxchg() call so we should see the suspended
854  //    state right away.
855  // 2) We're being called from the safepoint polling loop; if
856  //    we don't see the suspended state on this iteration, then
857  //    we'll come around again.
858  //
859  bool is_suspended = _thread->is_ext_suspended();
860  if (is_suspended) {
861    roll_forward(_at_safepoint);
862    return;
863  }
864
865  // Some JavaThread states have an initial safepoint state of
866  // running, but are actually at a safepoint. We will happily
867  // agree and update the safepoint state here.
868  if (SafepointSynchronize::safepoint_safe(_thread, state)) {
869    SafepointSynchronize::check_for_lazy_critical_native(_thread, state);
870    roll_forward(_at_safepoint);
871    return;
872  }
873
874  if (state == _thread_in_vm) {
875    roll_forward(_call_back);
876    return;
877  }
878
879  // All other thread states will continue to run until they
880  // transition and self-block in state _blocked
881  // Safepoint polling in compiled code causes the Java threads to do the same.
882  // Note: new threads may require a malloc so they must be allowed to finish
883
884  assert(is_running(), "examine_state_of_thread on non-running thread");
885  return;
886}
887
888// Returns true is thread could not be rolled forward at present position.
889void ThreadSafepointState::roll_forward(suspend_type type) {
890  _type = type;
891
892  switch(_type) {
893    case _at_safepoint:
894      SafepointSynchronize::signal_thread_at_safepoint();
895      DEBUG_ONLY(_thread->set_visited_for_critical_count(true));
896      if (_thread->in_critical()) {
897        // Notice that this thread is in a critical section
898        SafepointSynchronize::increment_jni_active_count();
899      }
900      break;
901
902    case _call_back:
903      set_has_called_back(false);
904      break;
905
906    case _running:
907    default:
908      ShouldNotReachHere();
909  }
910}
911
912void ThreadSafepointState::restart() {
913  switch(type()) {
914    case _at_safepoint:
915    case _call_back:
916      break;
917
918    case _running:
919    default:
920       tty->print_cr("restart thread "INTPTR_FORMAT" with state %d",
921                      _thread, _type);
922       _thread->print();
923      ShouldNotReachHere();
924  }
925  _type = _running;
926  set_has_called_back(false);
927}
928
929
930void ThreadSafepointState::print_on(outputStream *st) const {
931  const char *s;
932
933  switch(_type) {
934    case _running                : s = "_running";              break;
935    case _at_safepoint           : s = "_at_safepoint";         break;
936    case _call_back              : s = "_call_back";            break;
937    default:
938      ShouldNotReachHere();
939  }
940
941  st->print_cr("Thread: " INTPTR_FORMAT
942              "  [0x%2x] State: %s _has_called_back %d _at_poll_safepoint %d",
943               _thread, _thread->osthread()->thread_id(), s, _has_called_back,
944               _at_poll_safepoint);
945
946  _thread->print_thread_state_on(st);
947}
948
949
950// ---------------------------------------------------------------------------------------------------------------------
951
952// Block the thread at the safepoint poll or poll return.
953void ThreadSafepointState::handle_polling_page_exception() {
954
955  // Check state.  block() will set thread state to thread_in_vm which will
956  // cause the safepoint state _type to become _call_back.
957  assert(type() == ThreadSafepointState::_running,
958         "polling page exception on thread not running state");
959
960  // Step 1: Find the nmethod from the return address
961  if (ShowSafepointMsgs && Verbose) {
962    tty->print_cr("Polling page exception at " INTPTR_FORMAT, thread()->saved_exception_pc());
963  }
964  address real_return_addr = thread()->saved_exception_pc();
965
966  CodeBlob *cb = CodeCache::find_blob(real_return_addr);
967  assert(cb != NULL && cb->is_nmethod(), "return address should be in nmethod");
968  nmethod* nm = (nmethod*)cb;
969
970  // Find frame of caller
971  frame stub_fr = thread()->last_frame();
972  CodeBlob* stub_cb = stub_fr.cb();
973  assert(stub_cb->is_safepoint_stub(), "must be a safepoint stub");
974  RegisterMap map(thread(), true);
975  frame caller_fr = stub_fr.sender(&map);
976
977  // Should only be poll_return or poll
978  assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" );
979
980  // This is a poll immediately before a return. The exception handling code
981  // has already had the effect of causing the return to occur, so the execution
982  // will continue immediately after the call. In addition, the oopmap at the
983  // return point does not mark the return value as an oop (if it is), so
984  // it needs a handle here to be updated.
985  if( nm->is_at_poll_return(real_return_addr) ) {
986    // See if return type is an oop.
987    bool return_oop = nm->method()->is_returning_oop();
988    Handle return_value;
989    if (return_oop) {
990      // The oop result has been saved on the stack together with all
991      // the other registers. In order to preserve it over GCs we need
992      // to keep it in a handle.
993      oop result = caller_fr.saved_oop_result(&map);
994      assert(result == NULL || result->is_oop(), "must be oop");
995      return_value = Handle(thread(), result);
996      assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
997    }
998
999    // Block the thread
1000    SafepointSynchronize::block(thread());
1001
1002    // restore oop result, if any
1003    if (return_oop) {
1004      caller_fr.set_saved_oop_result(&map, return_value());
1005    }
1006  }
1007
1008  // This is a safepoint poll. Verify the return address and block.
1009  else {
1010    set_at_poll_safepoint(true);
1011
1012    // verify the blob built the "return address" correctly
1013    assert(real_return_addr == caller_fr.pc(), "must match");
1014
1015    // Block the thread
1016    SafepointSynchronize::block(thread());
1017    set_at_poll_safepoint(false);
1018
1019    // If we have a pending async exception deoptimize the frame
1020    // as otherwise we may never deliver it.
1021    if (thread()->has_async_condition()) {
1022      ThreadInVMfromJavaNoAsyncException __tiv(thread());
1023      Deoptimization::deoptimize_frame(thread(), caller_fr.id());
1024    }
1025
1026    // If an exception has been installed we must check for a pending deoptimization
1027    // Deoptimize frame if exception has been thrown.
1028
1029    if (thread()->has_pending_exception() ) {
1030      RegisterMap map(thread(), true);
1031      frame caller_fr = stub_fr.sender(&map);
1032      if (caller_fr.is_deoptimized_frame()) {
1033        // The exception patch will destroy registers that are still
1034        // live and will be needed during deoptimization. Defer the
1035        // Async exception should have deferred the exception until the
1036        // next safepoint which will be detected when we get into
1037        // the interpreter so if we have an exception now things
1038        // are messed up.
1039
1040        fatal("Exception installed and deoptimization is pending");
1041      }
1042    }
1043  }
1044}
1045
1046
1047//
1048//                     Statistics & Instrumentations
1049//
1050SafepointSynchronize::SafepointStats*  SafepointSynchronize::_safepoint_stats = NULL;
1051jlong  SafepointSynchronize::_safepoint_begin_time = 0;
1052int    SafepointSynchronize::_cur_stat_index = 0;
1053julong SafepointSynchronize::_safepoint_reasons[VM_Operation::VMOp_Terminating];
1054julong SafepointSynchronize::_coalesced_vmop_count = 0;
1055jlong  SafepointSynchronize::_max_sync_time = 0;
1056jlong  SafepointSynchronize::_max_vmop_time = 0;
1057float  SafepointSynchronize::_ts_of_current_safepoint = 0.0f;
1058
1059static jlong  cleanup_end_time = 0;
1060static bool   need_to_track_page_armed_status = false;
1061static bool   init_done = false;
1062
1063// Helper method to print the header.
1064static void print_header() {
1065  tty->print("         vmop                    "
1066             "[threads: total initially_running wait_to_block]    ");
1067  tty->print("[time: spin block sync cleanup vmop] ");
1068
1069  // no page armed status printed out if it is always armed.
1070  if (need_to_track_page_armed_status) {
1071    tty->print("page_armed ");
1072  }
1073
1074  tty->print_cr("page_trap_count");
1075}
1076
1077void SafepointSynchronize::deferred_initialize_stat() {
1078  if (init_done) return;
1079
1080  if (PrintSafepointStatisticsCount <= 0) {
1081    fatal("Wrong PrintSafepointStatisticsCount");
1082  }
1083
1084  // If PrintSafepointStatisticsTimeout is specified, the statistics data will
1085  // be printed right away, in which case, _safepoint_stats will regress to
1086  // a single element array. Otherwise, it is a circular ring buffer with default
1087  // size of PrintSafepointStatisticsCount.
1088  int stats_array_size;
1089  if (PrintSafepointStatisticsTimeout > 0) {
1090    stats_array_size = 1;
1091    PrintSafepointStatistics = true;
1092  } else {
1093    stats_array_size = PrintSafepointStatisticsCount;
1094  }
1095  _safepoint_stats = (SafepointStats*)os::malloc(stats_array_size
1096                                                 * sizeof(SafepointStats), mtInternal);
1097  guarantee(_safepoint_stats != NULL,
1098            "not enough memory for safepoint instrumentation data");
1099
1100  if (UseCompilerSafepoints && DeferPollingPageLoopCount >= 0) {
1101    need_to_track_page_armed_status = true;
1102  }
1103  init_done = true;
1104}
1105
1106void SafepointSynchronize::begin_statistics(int nof_threads, int nof_running) {
1107  assert(init_done, "safepoint statistics array hasn't been initialized");
1108  SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
1109
1110  spstat->_time_stamp = _ts_of_current_safepoint;
1111
1112  VM_Operation *op = VMThread::vm_operation();
1113  spstat->_vmop_type = (op != NULL ? op->type() : -1);
1114  if (op != NULL) {
1115    _safepoint_reasons[spstat->_vmop_type]++;
1116  }
1117
1118  spstat->_nof_total_threads = nof_threads;
1119  spstat->_nof_initial_running_threads = nof_running;
1120  spstat->_nof_threads_hit_page_trap = 0;
1121
1122  // Records the start time of spinning. The real time spent on spinning
1123  // will be adjusted when spin is done. Same trick is applied for time
1124  // spent on waiting for threads to block.
1125  if (nof_running != 0) {
1126    spstat->_time_to_spin = os::javaTimeNanos();
1127  }  else {
1128    spstat->_time_to_spin = 0;
1129  }
1130}
1131
1132void SafepointSynchronize::update_statistics_on_spin_end() {
1133  SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
1134
1135  jlong cur_time = os::javaTimeNanos();
1136
1137  spstat->_nof_threads_wait_to_block = _waiting_to_block;
1138  if (spstat->_nof_initial_running_threads != 0) {
1139    spstat->_time_to_spin = cur_time - spstat->_time_to_spin;
1140  }
1141
1142  if (need_to_track_page_armed_status) {
1143    spstat->_page_armed = (PageArmed == 1);
1144  }
1145
1146  // Records the start time of waiting for to block. Updated when block is done.
1147  if (_waiting_to_block != 0) {
1148    spstat->_time_to_wait_to_block = cur_time;
1149  } else {
1150    spstat->_time_to_wait_to_block = 0;
1151  }
1152}
1153
1154void SafepointSynchronize::update_statistics_on_sync_end(jlong end_time) {
1155  SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
1156
1157  if (spstat->_nof_threads_wait_to_block != 0) {
1158    spstat->_time_to_wait_to_block = end_time -
1159      spstat->_time_to_wait_to_block;
1160  }
1161
1162  // Records the end time of sync which will be used to calculate the total
1163  // vm operation time. Again, the real time spending in syncing will be deducted
1164  // from the start of the sync time later when end_statistics is called.
1165  spstat->_time_to_sync = end_time - _safepoint_begin_time;
1166  if (spstat->_time_to_sync > _max_sync_time) {
1167    _max_sync_time = spstat->_time_to_sync;
1168  }
1169
1170  spstat->_time_to_do_cleanups = end_time;
1171}
1172
1173void SafepointSynchronize::update_statistics_on_cleanup_end(jlong end_time) {
1174  SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
1175
1176  // Record how long spent in cleanup tasks.
1177  spstat->_time_to_do_cleanups = end_time - spstat->_time_to_do_cleanups;
1178
1179  cleanup_end_time = end_time;
1180}
1181
1182void SafepointSynchronize::end_statistics(jlong vmop_end_time) {
1183  SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
1184
1185  // Update the vm operation time.
1186  spstat->_time_to_exec_vmop = vmop_end_time -  cleanup_end_time;
1187  if (spstat->_time_to_exec_vmop > _max_vmop_time) {
1188    _max_vmop_time = spstat->_time_to_exec_vmop;
1189  }
1190  // Only the sync time longer than the specified
1191  // PrintSafepointStatisticsTimeout will be printed out right away.
1192  // By default, it is -1 meaning all samples will be put into the list.
1193  if ( PrintSafepointStatisticsTimeout > 0) {
1194    if (spstat->_time_to_sync > PrintSafepointStatisticsTimeout * MICROUNITS) {
1195      print_statistics();
1196    }
1197  } else {
1198    // The safepoint statistics will be printed out when the _safepoin_stats
1199    // array fills up.
1200    if (_cur_stat_index == PrintSafepointStatisticsCount - 1) {
1201      print_statistics();
1202      _cur_stat_index = 0;
1203    } else {
1204      _cur_stat_index++;
1205    }
1206  }
1207}
1208
1209void SafepointSynchronize::print_statistics() {
1210  SafepointStats* sstats = _safepoint_stats;
1211
1212  for (int index = 0; index <= _cur_stat_index; index++) {
1213    if (index % 30 == 0) {
1214      print_header();
1215    }
1216    sstats = &_safepoint_stats[index];
1217    tty->print("%.3f: ", sstats->_time_stamp);
1218    tty->print("%-26s       ["
1219               INT32_FORMAT_W(8)INT32_FORMAT_W(11)INT32_FORMAT_W(15)
1220               "    ]    ",
1221               sstats->_vmop_type == -1 ? "no vm operation" :
1222               VM_Operation::name(sstats->_vmop_type),
1223               sstats->_nof_total_threads,
1224               sstats->_nof_initial_running_threads,
1225               sstats->_nof_threads_wait_to_block);
1226    // "/ MICROUNITS " is to convert the unit from nanos to millis.
1227    tty->print("  ["
1228               INT64_FORMAT_W(6)INT64_FORMAT_W(6)
1229               INT64_FORMAT_W(6)INT64_FORMAT_W(6)
1230               INT64_FORMAT_W(6)"    ]  ",
1231               sstats->_time_to_spin / MICROUNITS,
1232               sstats->_time_to_wait_to_block / MICROUNITS,
1233               sstats->_time_to_sync / MICROUNITS,
1234               sstats->_time_to_do_cleanups / MICROUNITS,
1235               sstats->_time_to_exec_vmop / MICROUNITS);
1236
1237    if (need_to_track_page_armed_status) {
1238      tty->print(INT32_FORMAT"         ", sstats->_page_armed);
1239    }
1240    tty->print_cr(INT32_FORMAT"   ", sstats->_nof_threads_hit_page_trap);
1241  }
1242}
1243
1244// This method will be called when VM exits. It will first call
1245// print_statistics to print out the rest of the sampling.  Then
1246// it tries to summarize the sampling.
1247void SafepointSynchronize::print_stat_on_exit() {
1248  if (_safepoint_stats == NULL) return;
1249
1250  SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
1251
1252  // During VM exit, end_statistics may not get called and in that
1253  // case, if the sync time is less than PrintSafepointStatisticsTimeout,
1254  // don't print it out.
1255  // Approximate the vm op time.
1256  _safepoint_stats[_cur_stat_index]._time_to_exec_vmop =
1257    os::javaTimeNanos() - cleanup_end_time;
1258
1259  if ( PrintSafepointStatisticsTimeout < 0 ||
1260       spstat->_time_to_sync > PrintSafepointStatisticsTimeout * MICROUNITS) {
1261    print_statistics();
1262  }
1263  tty->cr();
1264
1265  // Print out polling page sampling status.
1266  if (!need_to_track_page_armed_status) {
1267    if (UseCompilerSafepoints) {
1268      tty->print_cr("Polling page always armed");
1269    }
1270  } else {
1271    tty->print_cr("Defer polling page loop count = %d\n",
1272                 DeferPollingPageLoopCount);
1273  }
1274
1275  for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) {
1276    if (_safepoint_reasons[index] != 0) {
1277      tty->print_cr("%-26s"UINT64_FORMAT_W(10), VM_Operation::name(index),
1278                    _safepoint_reasons[index]);
1279    }
1280  }
1281
1282  tty->print_cr(UINT64_FORMAT_W(5)" VM operations coalesced during safepoint",
1283                _coalesced_vmop_count);
1284  tty->print_cr("Maximum sync time  "INT64_FORMAT_W(5)" ms",
1285                _max_sync_time / MICROUNITS);
1286  tty->print_cr("Maximum vm operation time (except for Exit VM operation)  "
1287                INT64_FORMAT_W(5)" ms",
1288                _max_vmop_time / MICROUNITS);
1289}
1290
1291// ------------------------------------------------------------------------------------------------
1292// Non-product code
1293
1294#ifndef PRODUCT
1295
1296void SafepointSynchronize::print_state() {
1297  if (_state == _not_synchronized) {
1298    tty->print_cr("not synchronized");
1299  } else if (_state == _synchronizing || _state == _synchronized) {
1300    tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
1301                  "synchronized");
1302
1303    for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
1304       cur->safepoint_state()->print();
1305    }
1306  }
1307}
1308
1309void SafepointSynchronize::safepoint_msg(const char* format, ...) {
1310  if (ShowSafepointMsgs) {
1311    va_list ap;
1312    va_start(ap, format);
1313    tty->vprint_cr(format, ap);
1314    va_end(ap);
1315  }
1316}
1317
1318#endif // !PRODUCT
1319