safepoint.cpp revision 3152
0N/A/*
3152N/A * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/systemDictionary.hpp"
1879N/A#include "code/codeCache.hpp"
1879N/A#include "code/icBuffer.hpp"
1879N/A#include "code/nmethod.hpp"
1879N/A#include "code/pcDesc.hpp"
1879N/A#include "code/scopeDesc.hpp"
1879N/A#include "gc_interface/collectedHeap.hpp"
1879N/A#include "interpreter/interpreter.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "memory/universe.inline.hpp"
1879N/A#include "oops/oop.inline.hpp"
2062N/A#include "oops/symbol.hpp"
1879N/A#include "runtime/compilationPolicy.hpp"
1879N/A#include "runtime/deoptimization.hpp"
1879N/A#include "runtime/frame.inline.hpp"
1879N/A#include "runtime/interfaceSupport.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "runtime/osThread.hpp"
1879N/A#include "runtime/safepoint.hpp"
1879N/A#include "runtime/signature.hpp"
1879N/A#include "runtime/stubCodeGenerator.hpp"
1879N/A#include "runtime/stubRoutines.hpp"
1879N/A#include "runtime/sweeper.hpp"
1879N/A#include "runtime/synchronizer.hpp"
1879N/A#include "services/runtimeService.hpp"
1879N/A#include "utilities/events.hpp"
1879N/A#ifdef TARGET_ARCH_x86
1879N/A# include "nativeInst_x86.hpp"
1879N/A# include "vmreg_x86.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_sparc
1879N/A# include "nativeInst_sparc.hpp"
1879N/A# include "vmreg_sparc.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_ARCH_zero
1879N/A# include "nativeInst_zero.hpp"
1879N/A# include "vmreg_zero.inline.hpp"
1879N/A#endif
2073N/A#ifdef TARGET_ARCH_arm
2073N/A# include "nativeInst_arm.hpp"
2073N/A# include "vmreg_arm.inline.hpp"
2073N/A#endif
2073N/A#ifdef TARGET_ARCH_ppc
2073N/A# include "nativeInst_ppc.hpp"
2073N/A# include "vmreg_ppc.inline.hpp"
2073N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_linux
1879N/A# include "thread_linux.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_solaris
1879N/A# include "thread_solaris.inline.hpp"
1879N/A#endif
1879N/A#ifdef TARGET_OS_FAMILY_windows
1879N/A# include "thread_windows.inline.hpp"
1879N/A#endif
2796N/A#ifdef TARGET_OS_FAMILY_bsd
2796N/A# include "thread_bsd.inline.hpp"
2796N/A#endif
1879N/A#ifndef SERIALGC
1879N/A#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
1879N/A#include "gc_implementation/shared/concurrentGCThread.hpp"
1879N/A#endif
1879N/A#ifdef COMPILER1
1879N/A#include "c1/c1_globals.hpp"
1879N/A#endif
0N/A
0N/A// --------------------------------------------------------------------------------------------------
0N/A// Implementation of Safepoint begin/end
0N/A
0N/ASafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
0N/Avolatile int SafepointSynchronize::_waiting_to_block = 0;
0N/Avolatile int SafepointSynchronize::_safepoint_counter = 0;
3152N/Aint SafepointSynchronize::_current_jni_active_count = 0;
1291N/Along SafepointSynchronize::_end_of_last_safepoint = 0;
0N/Astatic volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
0N/Astatic volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
0N/Astatic bool timeout_error_printed = false;
0N/A
0N/A// Roll all threads forward to a safepoint and suspend them all
0N/Avoid SafepointSynchronize::begin() {
0N/A
0N/A Thread* myThread = Thread::current();
0N/A assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
0N/A
1291N/A if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
1291N/A _safepoint_begin_time = os::javaTimeNanos();
1291N/A _ts_of_current_safepoint = tty->time_stamp().seconds();
1291N/A }
0N/A
0N/A#ifndef SERIALGC
0N/A if (UseConcMarkSweepGC) {
0N/A // In the future we should investigate whether CMS can use the
0N/A // more-general mechanism below. DLD (01/05).
0N/A ConcurrentMarkSweepThread::synchronize(false);
845N/A } else if (UseG1GC) {
0N/A ConcurrentGCThread::safepoint_synchronize();
0N/A }
0N/A#endif // SERIALGC
0N/A
0N/A // By getting the Threads_lock, we assure that no threads are about to start or
0N/A // exit. It is released again in SafepointSynchronize::end().
0N/A Threads_lock->lock();
0N/A
0N/A assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
0N/A
0N/A int nof_threads = Threads::number_of_threads();
0N/A
0N/A if (TraceSafepoint) {
0N/A tty->print_cr("Safepoint synchronization initiated. (%d)", nof_threads);
0N/A }
0N/A
0N/A RuntimeService::record_safepoint_begin();
0N/A
0N/A {
0N/A MutexLocker mu(Safepoint_lock);
0N/A
3152N/A // Reset the count of active JNI critical threads
3152N/A _current_jni_active_count = 0;
3152N/A
0N/A // Set number of threads to wait for, before we initiate the callbacks
0N/A _waiting_to_block = nof_threads;
0N/A TryingToBlock = 0 ;
0N/A int still_running = nof_threads;
0N/A
0N/A // Save the starting time, so that it can be compared to see if this has taken
0N/A // too long to complete.
0N/A jlong safepoint_limit_time;
0N/A timeout_error_printed = false;
0N/A
1003N/A // PrintSafepointStatisticsTimeout can be specified separately. When
1003N/A // specified, PrintSafepointStatistics will be set to true in
1003N/A // deferred_initialize_stat method. The initialization has to be done
1003N/A // early enough to avoid any races. See bug 6880029 for details.
1003N/A if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
1003N/A deferred_initialize_stat();
1003N/A }
1003N/A
0N/A // Begin the process of bringing the system to a safepoint.
0N/A // Java threads can be in several different states and are
0N/A // stopped by different mechanisms:
0N/A //
0N/A // 1. Running interpreted
0N/A // The interpeter dispatch table is changed to force it to
0N/A // check for a safepoint condition between bytecodes.
0N/A // 2. Running in native code
0N/A // When returning from the native code, a Java thread must check
0N/A // the safepoint _state to see if we must block. If the
0N/A // VM thread sees a Java thread in native, it does
0N/A // not wait for this thread to block. The order of the memory
0N/A // writes and reads of both the safepoint state and the Java
0N/A // threads state is critical. In order to guarantee that the
0N/A // memory writes are serialized with respect to each other,
0N/A // the VM thread issues a memory barrier instruction
0N/A // (on MP systems). In order to avoid the overhead of issuing
0N/A // a memory barrier for each Java thread making native calls, each Java
0N/A // thread performs a write to a single memory page after changing
0N/A // the thread state. The VM thread performs a sequence of
0N/A // mprotect OS calls which forces all previous writes from all
0N/A // Java threads to be serialized. This is done in the
0N/A // os::serialize_thread_states() call. This has proven to be
0N/A // much more efficient than executing a membar instruction
0N/A // on every call to native code.
0N/A // 3. Running compiled Code
0N/A // Compiled code reads a global (Safepoint Polling) page that
0N/A // is set to fault if we are trying to get to a safepoint.
0N/A // 4. Blocked
0N/A // A thread which is blocked will not be allowed to return from the
0N/A // block condition until the safepoint operation is complete.
0N/A // 5. In VM or Transitioning between states
0N/A // If a Java thread is currently running in the VM or transitioning
0N/A // between states, the safepointing code will wait for the thread to
0N/A // block itself when it attempts transitions to a new state.
0N/A //
0N/A _state = _synchronizing;
0N/A OrderAccess::fence();
0N/A
0N/A // Flush all thread states to memory
0N/A if (!UseMembar) {
0N/A os::serialize_thread_states();
0N/A }
0N/A
0N/A // Make interpreter safepoint aware
0N/A Interpreter::notice_safepoints();
0N/A
0N/A if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
0N/A // Make polling safepoint aware
0N/A guarantee (PageArmed == 0, "invariant") ;
0N/A PageArmed = 1 ;
0N/A os::make_polling_page_unreadable();
0N/A }
0N/A
0N/A // Consider using active_processor_count() ... but that call is expensive.
0N/A int ncpus = os::processor_count() ;
0N/A
0N/A#ifdef ASSERT
0N/A for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
0N/A assert(cur->safepoint_state()->is_running(), "Illegal initial state");
0N/A }
0N/A#endif // ASSERT
0N/A
0N/A if (SafepointTimeout)
0N/A safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
0N/A
0N/A // Iterate through all threads until it have been determined how to stop them all at a safepoint
0N/A unsigned int iterations = 0;
0N/A int steps = 0 ;
0N/A while(still_running > 0) {
0N/A for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
0N/A assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
0N/A ThreadSafepointState *cur_state = cur->safepoint_state();
0N/A if (cur_state->is_running()) {
0N/A cur_state->examine_state_of_thread();
0N/A if (!cur_state->is_running()) {
0N/A still_running--;
0N/A // consider adjusting steps downward:
0N/A // steps = 0
0N/A // steps -= NNN
0N/A // steps >>= 1
0N/A // steps = MIN(steps, 2000-100)
0N/A // if (iterations != 0) steps -= NNN
0N/A }
0N/A if (TraceSafepoint && Verbose) cur_state->print();
0N/A }
0N/A }
0N/A
1003N/A if (PrintSafepointStatistics && iterations == 0) {
0N/A begin_statistics(nof_threads, still_running);
0N/A }
0N/A
0N/A if (still_running > 0) {
0N/A // Check for if it takes to long
0N/A if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
0N/A print_safepoint_timeout(_spinning_timeout);
0N/A }
0N/A
0N/A // Spin to avoid context switching.
0N/A // There's a tension between allowing the mutators to run (and rendezvous)
0N/A // vs spinning. As the VM thread spins, wasting cycles, it consumes CPU that
0N/A // a mutator might otherwise use profitably to reach a safepoint. Excessive
0N/A // spinning by the VM thread on a saturated system can increase rendezvous latency.
0N/A // Blocking or yielding incur their own penalties in the form of context switching
0N/A // and the resultant loss of $ residency.
0N/A //
0N/A // Further complicating matters is that yield() does not work as naively expected
0N/A // on many platforms -- yield() does not guarantee that any other ready threads
0N/A // will run. As such we revert yield_all() after some number of iterations.
0N/A // Yield_all() is implemented as a short unconditional sleep on some platforms.
0N/A // Typical operating systems round a "short" sleep period up to 10 msecs, so sleeping
0N/A // can actually increase the time it takes the VM thread to detect that a system-wide
0N/A // stop-the-world safepoint has been reached. In a pathological scenario such as that
0N/A // described in CR6415670 the VMthread may sleep just before the mutator(s) become safe.
0N/A // In that case the mutators will be stalled waiting for the safepoint to complete and the
0N/A // the VMthread will be sleeping, waiting for the mutators to rendezvous. The VMthread
0N/A // will eventually wake up and detect that all mutators are safe, at which point
0N/A // we'll again make progress.
0N/A //
0N/A // Beware too that that the VMThread typically runs at elevated priority.
0N/A // Its default priority is higher than the default mutator priority.
0N/A // Obviously, this complicates spinning.
0N/A //
0N/A // Note too that on Windows XP SwitchThreadTo() has quite different behavior than Sleep(0).
0N/A // Sleep(0) will _not yield to lower priority threads, while SwitchThreadTo() will.
0N/A //
0N/A // See the comments in synchronizer.cpp for additional remarks on spinning.
0N/A //
0N/A // In the future we might:
0N/A // 1. Modify the safepoint scheme to avoid potentally unbounded spinning.
0N/A // This is tricky as the path used by a thread exiting the JVM (say on
0N/A // on JNI call-out) simply stores into its state field. The burden
0N/A // is placed on the VM thread, which must poll (spin).
0N/A // 2. Find something useful to do while spinning. If the safepoint is GC-related
0N/A // we might aggressively scan the stacks of threads that are already safe.
0N/A // 3. Use Solaris schedctl to examine the state of the still-running mutators.
0N/A // If all the mutators are ONPROC there's no reason to sleep or yield.
0N/A // 4. YieldTo() any still-running mutators that are ready but OFFPROC.
0N/A // 5. Check system saturation. If the system is not fully saturated then
0N/A // simply spin and avoid sleep/yield.
0N/A // 6. As still-running mutators rendezvous they could unpark the sleeping
0N/A // VMthread. This works well for still-running mutators that become
0N/A // safe. The VMthread must still poll for mutators that call-out.
0N/A // 7. Drive the policy on time-since-begin instead of iterations.
0N/A // 8. Consider making the spin duration a function of the # of CPUs:
0N/A // Spin = (((ncpus-1) * M) + K) + F(still_running)
0N/A // Alternately, instead of counting iterations of the outer loop
0N/A // we could count the # of threads visited in the inner loop, above.
0N/A // 9. On windows consider using the return value from SwitchThreadTo()
0N/A // to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.
0N/A
0N/A if (UseCompilerSafepoints && int(iterations) == DeferPollingPageLoopCount) {
0N/A guarantee (PageArmed == 0, "invariant") ;
0N/A PageArmed = 1 ;
0N/A os::make_polling_page_unreadable();
0N/A }
0N/A
0N/A // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
0N/A // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
0N/A ++steps ;
0N/A if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
0N/A SpinPause() ; // MP-Polite spin
0N/A } else
0N/A if (steps < DeferThrSuspendLoopCount) {
0N/A os::NakedYield() ;
0N/A } else {
0N/A os::yield_all(steps) ;
0N/A // Alternately, the VM thread could transiently depress its scheduling priority or
0N/A // transiently increase the priority of the tardy mutator(s).
0N/A }
0N/A
0N/A iterations ++ ;
0N/A }
0N/A assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
0N/A }
0N/A assert(still_running == 0, "sanity check");
0N/A
0N/A if (PrintSafepointStatistics) {
0N/A update_statistics_on_spin_end();
0N/A }
0N/A
0N/A // wait until all threads are stopped
0N/A while (_waiting_to_block > 0) {
0N/A if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block);
0N/A if (!SafepointTimeout || timeout_error_printed) {
0N/A Safepoint_lock->wait(true); // true, means with no safepoint checks
0N/A } else {
0N/A // Compute remaining time
0N/A jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
0N/A
0N/A // If there is no remaining time, then there is an error
0N/A if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
0N/A print_safepoint_timeout(_blocking_timeout);
0N/A }
0N/A }
0N/A }
0N/A assert(_waiting_to_block == 0, "sanity check");
0N/A
0N/A#ifndef PRODUCT
0N/A if (SafepointTimeout) {
0N/A jlong current_time = os::javaTimeNanos();
0N/A if (safepoint_limit_time < current_time) {
0N/A tty->print_cr("# SafepointSynchronize: Finished after "
0N/A INT64_FORMAT_W(6) " ms",
0N/A ((current_time - safepoint_limit_time) / MICROUNITS +
0N/A SafepointTimeoutDelay));
0N/A }
0N/A }
0N/A#endif
0N/A
0N/A assert((_safepoint_counter & 0x1) == 0, "must be even");
0N/A assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
0N/A _safepoint_counter ++;
0N/A
0N/A // Record state
0N/A _state = _synchronized;
0N/A
0N/A OrderAccess::fence();
0N/A
3152N/A // Update the count of active JNI critical regions
3152N/A GC_locker::set_jni_lock_count(_current_jni_active_count);
3152N/A
0N/A if (TraceSafepoint) {
0N/A VM_Operation *op = VMThread::vm_operation();
0N/A tty->print_cr("Entering safepoint region: %s", (op != NULL) ? op->name() : "no vm operation");
0N/A }
0N/A
0N/A RuntimeService::record_safepoint_synchronized();
0N/A if (PrintSafepointStatistics) {
0N/A update_statistics_on_sync_end(os::javaTimeNanos());
0N/A }
0N/A
0N/A // Call stuff that needs to be run when a safepoint is just about to be completed
0N/A do_cleanup_tasks();
1291N/A
1291N/A if (PrintSafepointStatistics) {
1291N/A // Record how much time spend on the above cleanup tasks
1291N/A update_statistics_on_cleanup_end(os::javaTimeNanos());
1291N/A }
0N/A }
0N/A}
0N/A
0N/A// Wake up all threads, so they are ready to resume execution after the safepoint
0N/A// operation has been carried out
0N/Avoid SafepointSynchronize::end() {
0N/A
0N/A assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
0N/A assert((_safepoint_counter & 0x1) == 1, "must be odd");
0N/A _safepoint_counter ++;
0N/A // memory fence isn't required here since an odd _safepoint_counter
0N/A // value can do no harm and a fence is issued below anyway.
0N/A
0N/A DEBUG_ONLY(Thread* myThread = Thread::current();)
0N/A assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
0N/A
0N/A if (PrintSafepointStatistics) {
0N/A end_statistics(os::javaTimeNanos());
0N/A }
0N/A
0N/A#ifdef ASSERT
0N/A // A pending_exception cannot be installed during a safepoint. The threads
0N/A // may install an async exception after they come back from a safepoint into
0N/A // pending_exception after they unblock. But that should happen later.
0N/A for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
0N/A assert (!(cur->has_pending_exception() &&
0N/A cur->safepoint_state()->is_at_poll_safepoint()),
0N/A "safepoint installed a pending exception");
0N/A }
0N/A#endif // ASSERT
0N/A
0N/A if (PageArmed) {
0N/A // Make polling safepoint aware
0N/A os::make_polling_page_readable();
0N/A PageArmed = 0 ;
0N/A }
0N/A
0N/A // Remove safepoint check from interpreter
0N/A Interpreter::ignore_safepoints();
0N/A
0N/A {
0N/A MutexLocker mu(Safepoint_lock);
0N/A
0N/A assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
0N/A
0N/A // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
0N/A // when they get restarted.
0N/A _state = _not_synchronized;
0N/A OrderAccess::fence();
0N/A
0N/A if (TraceSafepoint) {
0N/A tty->print_cr("Leaving safepoint region");
0N/A }
0N/A
0N/A // Start suspended threads
0N/A for(JavaThread *current = Threads::first(); current; current = current->next()) {
605N/A // A problem occurring on Solaris is when attempting to restart threads
0N/A // the first #cpus - 1 go well, but then the VMThread is preempted when we get
0N/A // to the next one (since it has been running the longest). We then have
0N/A // to wait for a cpu to become available before we can continue restarting
0N/A // threads.
0N/A // FIXME: This causes the performance of the VM to degrade when active and with
0N/A // large numbers of threads. Apparently this is due to the synchronous nature
0N/A // of suspending threads.
0N/A //
0N/A // TODO-FIXME: the comments above are vestigial and no longer apply.
0N/A // Furthermore, using solaris' schedctl in this particular context confers no benefit
0N/A if (VMThreadHintNoPreempt) {
0N/A os::hint_no_preempt();
0N/A }
0N/A ThreadSafepointState* cur_state = current->safepoint_state();
0N/A assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
0N/A cur_state->restart();
0N/A assert(cur_state->is_running(), "safepoint state has not been reset");
0N/A }
0N/A
0N/A RuntimeService::record_safepoint_end();
0N/A
0N/A // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
0N/A // blocked in signal_thread_blocked
0N/A Threads_lock->unlock();
0N/A
0N/A }
0N/A#ifndef SERIALGC
0N/A // If there are any concurrent GC threads resume them.
0N/A if (UseConcMarkSweepGC) {
0N/A ConcurrentMarkSweepThread::desynchronize(false);
845N/A } else if (UseG1GC) {
0N/A ConcurrentGCThread::safepoint_desynchronize();
0N/A }
0N/A#endif // SERIALGC
1291N/A // record this time so VMThread can keep track how much time has elasped
1291N/A // since last safepoint.
1291N/A _end_of_last_safepoint = os::javaTimeMillis();
0N/A}
0N/A
0N/Abool SafepointSynchronize::is_cleanup_needed() {
0N/A // Need a safepoint if some inline cache buffers is non-empty
0N/A if (!InlineCacheBuffer::is_empty()) return true;
0N/A return false;
0N/A}
0N/A
0N/A
0N/A
0N/A// Various cleaning tasks that should be done periodically at safepoints
0N/Avoid SafepointSynchronize::do_cleanup_tasks() {
1291N/A {
1321N/A TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime);
1291N/A ObjectSynchronizer::deflate_idle_monitors();
0N/A }
0N/A
1291N/A {
1321N/A TraceTime t2("updating inline caches", TraceSafepointCleanupTime);
1291N/A InlineCacheBuffer::update_inline_caches();
1291N/A }
1703N/A {
1703N/A TraceTime t3("compilation policy safepoint handler", TraceSafepointCleanupTime);
1703N/A CompilationPolicy::policy()->do_safepoint_work();
0N/A }
1291N/A
1321N/A TraceTime t4("sweeping nmethods", TraceSafepointCleanupTime);
1458N/A NMethodSweeper::scan_stacks();
2592N/A
2592N/A // rotate log files?
2592N/A if (UseGCLogFileRotation) {
2592N/A gclog_or_tty->rotate_log();
2592N/A }
0N/A}
0N/A
0N/A
0N/Abool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
0N/A switch(state) {
0N/A case _thread_in_native:
0N/A // native threads are safe if they have no java stack or have walkable stack
0N/A return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
0N/A
0N/A // blocked threads should have already have walkable stack
0N/A case _thread_blocked:
0N/A assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
0N/A return true;
0N/A
0N/A default:
0N/A return false;
0N/A }
0N/A}
0N/A
0N/A
0N/A// -------------------------------------------------------------------------------------------------------
0N/A// Implementation of Safepoint callback point
0N/A
0N/Avoid SafepointSynchronize::block(JavaThread *thread) {
0N/A assert(thread != NULL, "thread must be set");
0N/A assert(thread->is_Java_thread(), "not a Java thread");
0N/A
0N/A // Threads shouldn't block if they are in the middle of printing, but...
0N/A ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
0N/A
0N/A // Only bail from the block() call if the thread is gone from the
0N/A // thread list; starting to exit should still block.
0N/A if (thread->is_terminated()) {
0N/A // block current thread if we come here from native code when VM is gone
0N/A thread->block_if_vm_exited();
0N/A
0N/A // otherwise do nothing
0N/A return;
0N/A }
0N/A
0N/A JavaThreadState state = thread->thread_state();
0N/A thread->frame_anchor()->make_walkable(thread);
0N/A
0N/A // Check that we have a valid thread_state at this point
0N/A switch(state) {
0N/A case _thread_in_vm_trans:
0N/A case _thread_in_Java: // From compiled code
0N/A
0N/A // We are highly likely to block on the Safepoint_lock. In order to avoid blocking in this case,
0N/A // we pretend we are still in the VM.
0N/A thread->set_thread_state(_thread_in_vm);
0N/A
0N/A if (is_synchronizing()) {
0N/A Atomic::inc (&TryingToBlock) ;
0N/A }
0N/A
0N/A // We will always be holding the Safepoint_lock when we are examine the state
0N/A // of a thread. Hence, the instructions between the Safepoint_lock->lock() and
0N/A // Safepoint_lock->unlock() are happening atomic with regards to the safepoint code
0N/A Safepoint_lock->lock_without_safepoint_check();
0N/A if (is_synchronizing()) {
0N/A // Decrement the number of threads to wait for and signal vm thread
0N/A assert(_waiting_to_block > 0, "sanity check");
0N/A _waiting_to_block--;
0N/A thread->safepoint_state()->set_has_called_back(true);
0N/A
3152N/A if (thread->in_critical()) {
3152N/A // Notice that this thread is in a critical section
3152N/A increment_jni_active_count();
3152N/A }
3152N/A
0N/A // Consider (_waiting_to_block < 2) to pipeline the wakeup of the VM thread
0N/A if (_waiting_to_block == 0) {
0N/A Safepoint_lock->notify_all();
0N/A }
0N/A }
0N/A
0N/A // We transition the thread to state _thread_blocked here, but
0N/A // we can't do our usual check for external suspension and then
0N/A // self-suspend after the lock_without_safepoint_check() call
0N/A // below because we are often called during transitions while
0N/A // we hold different locks. That would leave us suspended while
0N/A // holding a resource which results in deadlocks.
0N/A thread->set_thread_state(_thread_blocked);
0N/A Safepoint_lock->unlock();
0N/A
0N/A // We now try to acquire the threads lock. Since this lock is hold by the VM thread during
0N/A // the entire safepoint, the threads will all line up here during the safepoint.
0N/A Threads_lock->lock_without_safepoint_check();
0N/A // restore original state. This is important if the thread comes from compiled code, so it
0N/A // will continue to execute with the _thread_in_Java state.
0N/A thread->set_thread_state(state);
0N/A Threads_lock->unlock();
0N/A break;
0N/A
0N/A case _thread_in_native_trans:
0N/A case _thread_blocked_trans:
0N/A case _thread_new_trans:
0N/A if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back) {
0N/A thread->print_thread_state();
0N/A fatal("Deadlock in safepoint code. "
0N/A "Should have called back to the VM before blocking.");
0N/A }
0N/A
0N/A // We transition the thread to state _thread_blocked here, but
0N/A // we can't do our usual check for external suspension and then
0N/A // self-suspend after the lock_without_safepoint_check() call
0N/A // below because we are often called during transitions while
0N/A // we hold different locks. That would leave us suspended while
0N/A // holding a resource which results in deadlocks.
0N/A thread->set_thread_state(_thread_blocked);
0N/A
0N/A // It is not safe to suspend a thread if we discover it is in _thread_in_native_trans. Hence,
0N/A // the safepoint code might still be waiting for it to block. We need to change the state here,
0N/A // so it can see that it is at a safepoint.
0N/A
0N/A // Block until the safepoint operation is completed.
0N/A Threads_lock->lock_without_safepoint_check();
0N/A
0N/A // Restore state
0N/A thread->set_thread_state(state);
0N/A
0N/A Threads_lock->unlock();
0N/A break;
0N/A
0N/A default:
1410N/A fatal(err_msg("Illegal threadstate encountered: %d", state));
0N/A }
0N/A
0N/A // Check for pending. async. exceptions or suspends - except if the
0N/A // thread was blocked inside the VM. has_special_runtime_exit_condition()
0N/A // is called last since it grabs a lock and we only want to do that when
0N/A // we must.
0N/A //
0N/A // Note: we never deliver an async exception at a polling point as the
0N/A // compiler may not have an exception handler for it. The polling
0N/A // code will notice the async and deoptimize and the exception will
0N/A // be delivered. (Polling at a return point is ok though). Sure is
0N/A // a lot of bother for a deprecated feature...
0N/A //
0N/A // We don't deliver an async exception if the thread state is
0N/A // _thread_in_native_trans so JNI functions won't be called with
0N/A // a surprising pending exception. If the thread state is going back to java,
0N/A // async exception is checked in check_special_condition_for_native_trans().
0N/A
0N/A if (state != _thread_blocked_trans &&
0N/A state != _thread_in_vm_trans &&
0N/A thread->has_special_runtime_exit_condition()) {
0N/A thread->handle_special_runtime_exit_condition(
0N/A !thread->is_at_poll_safepoint() && (state != _thread_in_native_trans));
0N/A }
0N/A}
0N/A
0N/A// ------------------------------------------------------------------------------------------------------
0N/A// Exception handlers
0N/A
0N/A#ifndef PRODUCT
0N/A#ifdef _LP64
0N/A#define PTR_PAD ""
0N/A#else
0N/A#define PTR_PAD " "
0N/A#endif
0N/A
0N/Astatic void print_ptrs(intptr_t oldptr, intptr_t newptr, bool wasoop) {
0N/A bool is_oop = newptr ? ((oop)newptr)->is_oop() : false;
0N/A tty->print_cr(PTR_FORMAT PTR_PAD " %s %c " PTR_FORMAT PTR_PAD " %s %s",
0N/A oldptr, wasoop?"oop":" ", oldptr == newptr ? ' ' : '!',
0N/A newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" "));
0N/A}
0N/A
0N/Astatic void print_longs(jlong oldptr, jlong newptr, bool wasoop) {
0N/A bool is_oop = newptr ? ((oop)(intptr_t)newptr)->is_oop() : false;
0N/A tty->print_cr(PTR64_FORMAT " %s %c " PTR64_FORMAT " %s %s",
0N/A oldptr, wasoop?"oop":" ", oldptr == newptr ? ' ' : '!',
0N/A newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" "));
0N/A}
0N/A
0N/A#ifdef SPARC
0N/Astatic void print_me(intptr_t *new_sp, intptr_t *old_sp, bool *was_oops) {
0N/A#ifdef _LP64
0N/A tty->print_cr("--------+------address-----+------before-----------+-------after----------+");
0N/A const int incr = 1; // Increment to skip a long, in units of intptr_t
0N/A#else
0N/A tty->print_cr("--------+--address-+------before-----------+-------after----------+");
0N/A const int incr = 2; // Increment to skip a long, in units of intptr_t
0N/A#endif
0N/A tty->print_cr("---SP---|");
0N/A for( int i=0; i<16; i++ ) {
0N/A tty->print("blob %c%d |"PTR_FORMAT" ","LO"[i>>3],i&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
0N/A tty->print_cr("--------|");
0N/A for( int i1=0; i1<frame::memory_parameter_word_sp_offset-16; i1++ ) {
0N/A tty->print("argv pad|"PTR_FORMAT" ",new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
0N/A tty->print(" pad|"PTR_FORMAT" ",new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++);
0N/A tty->print_cr("--------|");
0N/A 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;
0N/A 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;
0N/A 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;
0N/A 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;
0N/A tty->print_cr(" FSR |"PTR_FORMAT" "PTR64_FORMAT" "PTR64_FORMAT,new_sp,*(jlong*)old_sp,*(jlong*)new_sp);
0N/A old_sp += incr; new_sp += incr; was_oops += incr;
0N/A // Skip the floats
0N/A tty->print_cr("--Float-|"PTR_FORMAT,new_sp);
0N/A tty->print_cr("---FP---|");
0N/A old_sp += incr*32; new_sp += incr*32; was_oops += incr*32;
0N/A for( int i2=0; i2<16; i2++ ) {
0N/A tty->print("call %c%d |"PTR_FORMAT" ","LI"[i2>>3],i2&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
0N/A tty->print_cr("");
0N/A}
0N/A#endif // SPARC
0N/A#endif // PRODUCT
0N/A
0N/A
0N/Avoid SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
0N/A assert(thread->is_Java_thread(), "polling reference encountered by VM thread");
0N/A assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
0N/A assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
0N/A
0N/A // Uncomment this to get some serious before/after printing of the
0N/A // Sparc safepoint-blob frame structure.
0N/A /*
0N/A intptr_t* sp = thread->last_Java_sp();
0N/A intptr_t stack_copy[150];
0N/A for( int i=0; i<150; i++ ) stack_copy[i] = sp[i];
0N/A bool was_oops[150];
0N/A for( int i=0; i<150; i++ )
0N/A was_oops[i] = stack_copy[i] ? ((oop)stack_copy[i])->is_oop() : false;
0N/A */
0N/A
0N/A if (ShowSafepointMsgs) {
0N/A tty->print("handle_polling_page_exception: ");
0N/A }
0N/A
0N/A if (PrintSafepointStatistics) {
0N/A inc_page_trap_count();
0N/A }
0N/A
0N/A ThreadSafepointState* state = thread->safepoint_state();
0N/A
0N/A state->handle_polling_page_exception();
0N/A // print_me(sp,stack_copy,was_oops);
0N/A}
0N/A
0N/A
0N/Avoid SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
0N/A if (!timeout_error_printed) {
0N/A timeout_error_printed = true;
0N/A // Print out the thread infor which didn't reach the safepoint for debugging
0N/A // purposes (useful when there are lots of threads in the debugger).
0N/A tty->print_cr("");
0N/A tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
0N/A if (reason == _spinning_timeout) {
0N/A tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
0N/A } else if (reason == _blocking_timeout) {
0N/A tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
0N/A }
0N/A
0N/A tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
0N/A ThreadSafepointState *cur_state;
0N/A ResourceMark rm;
0N/A for(JavaThread *cur_thread = Threads::first(); cur_thread;
0N/A cur_thread = cur_thread->next()) {
0N/A cur_state = cur_thread->safepoint_state();
0N/A
0N/A if (cur_thread->thread_state() != _thread_blocked &&
0N/A ((reason == _spinning_timeout && cur_state->is_running()) ||
0N/A (reason == _blocking_timeout && !cur_state->has_called_back()))) {
0N/A tty->print("# ");
0N/A cur_thread->print();
0N/A tty->print_cr("");
0N/A }
0N/A }
0N/A tty->print_cr("# SafepointSynchronize::begin: (End of list)");
0N/A }
0N/A
0N/A // To debug the long safepoint, specify both DieOnSafepointTimeout &
0N/A // ShowMessageBoxOnError.
0N/A if (DieOnSafepointTimeout) {
0N/A char msg[1024];
0N/A VM_Operation *op = VMThread::vm_operation();
513N/A sprintf(msg, "Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
0N/A SafepointTimeoutDelay,
0N/A op != NULL ? op->name() : "no vm operation");
0N/A fatal(msg);
0N/A }
0N/A}
0N/A
0N/A
0N/A// -------------------------------------------------------------------------------------------------------
0N/A// Implementation of ThreadSafepointState
0N/A
0N/AThreadSafepointState::ThreadSafepointState(JavaThread *thread) {
0N/A _thread = thread;
0N/A _type = _running;
0N/A _has_called_back = false;
0N/A _at_poll_safepoint = false;
0N/A}
0N/A
0N/Avoid ThreadSafepointState::create(JavaThread *thread) {
0N/A ThreadSafepointState *state = new ThreadSafepointState(thread);
0N/A thread->set_safepoint_state(state);
0N/A}
0N/A
0N/Avoid ThreadSafepointState::destroy(JavaThread *thread) {
0N/A if (thread->safepoint_state()) {
0N/A delete(thread->safepoint_state());
0N/A thread->set_safepoint_state(NULL);
0N/A }
0N/A}
0N/A
0N/Avoid ThreadSafepointState::examine_state_of_thread() {
0N/A assert(is_running(), "better be running or just have hit safepoint poll");
0N/A
0N/A JavaThreadState state = _thread->thread_state();
0N/A
1647N/A // Save the state at the start of safepoint processing.
1647N/A _orig_thread_state = state;
1647N/A
0N/A // Check for a thread that is suspended. Note that thread resume tries
0N/A // to grab the Threads_lock which we own here, so a thread cannot be
0N/A // resumed during safepoint synchronization.
0N/A
979N/A // We check to see if this thread is suspended without locking to
979N/A // avoid deadlocking with a third thread that is waiting for this
979N/A // thread to be suspended. The third thread can notice the safepoint
979N/A // that we're trying to start at the beginning of its SR_lock->wait()
979N/A // call. If that happens, then the third thread will block on the
979N/A // safepoint while still holding the underlying SR_lock. We won't be
979N/A // able to get the SR_lock and we'll deadlock.
979N/A //
979N/A // We don't need to grab the SR_lock here for two reasons:
979N/A // 1) The suspend flags are both volatile and are set with an
979N/A // Atomic::cmpxchg() call so we should see the suspended
979N/A // state right away.
979N/A // 2) We're being called from the safepoint polling loop; if
979N/A // we don't see the suspended state on this iteration, then
979N/A // we'll come around again.
979N/A //
979N/A bool is_suspended = _thread->is_ext_suspended();
0N/A if (is_suspended) {
0N/A roll_forward(_at_safepoint);
0N/A return;
0N/A }
0N/A
0N/A // Some JavaThread states have an initial safepoint state of
0N/A // running, but are actually at a safepoint. We will happily
0N/A // agree and update the safepoint state here.
0N/A if (SafepointSynchronize::safepoint_safe(_thread, state)) {
3152N/A roll_forward(_at_safepoint);
3152N/A if (_thread->in_critical()) {
3152N/A // Notice that this thread is in a critical section
3152N/A SafepointSynchronize::increment_jni_active_count();
3152N/A }
3152N/A return;
0N/A }
0N/A
0N/A if (state == _thread_in_vm) {
0N/A roll_forward(_call_back);
0N/A return;
0N/A }
0N/A
0N/A // All other thread states will continue to run until they
0N/A // transition and self-block in state _blocked
0N/A // Safepoint polling in compiled code causes the Java threads to do the same.
0N/A // Note: new threads may require a malloc so they must be allowed to finish
0N/A
0N/A assert(is_running(), "examine_state_of_thread on non-running thread");
0N/A return;
0N/A}
0N/A
0N/A// Returns true is thread could not be rolled forward at present position.
0N/Avoid ThreadSafepointState::roll_forward(suspend_type type) {
0N/A _type = type;
0N/A
0N/A switch(_type) {
0N/A case _at_safepoint:
0N/A SafepointSynchronize::signal_thread_at_safepoint();
0N/A break;
0N/A
0N/A case _call_back:
0N/A set_has_called_back(false);
0N/A break;
0N/A
0N/A case _running:
0N/A default:
0N/A ShouldNotReachHere();
0N/A }
0N/A}
0N/A
0N/Avoid ThreadSafepointState::restart() {
0N/A switch(type()) {
0N/A case _at_safepoint:
0N/A case _call_back:
0N/A break;
0N/A
0N/A case _running:
0N/A default:
0N/A tty->print_cr("restart thread "INTPTR_FORMAT" with state %d",
0N/A _thread, _type);
0N/A _thread->print();
0N/A ShouldNotReachHere();
0N/A }
0N/A _type = _running;
0N/A set_has_called_back(false);
0N/A}
0N/A
0N/A
0N/Avoid ThreadSafepointState::print_on(outputStream *st) const {
0N/A const char *s;
0N/A
0N/A switch(_type) {
0N/A case _running : s = "_running"; break;
0N/A case _at_safepoint : s = "_at_safepoint"; break;
0N/A case _call_back : s = "_call_back"; break;
0N/A default:
0N/A ShouldNotReachHere();
0N/A }
0N/A
0N/A st->print_cr("Thread: " INTPTR_FORMAT
0N/A " [0x%2x] State: %s _has_called_back %d _at_poll_safepoint %d",
0N/A _thread, _thread->osthread()->thread_id(), s, _has_called_back,
0N/A _at_poll_safepoint);
0N/A
0N/A _thread->print_thread_state_on(st);
0N/A}
0N/A
0N/A
0N/A// ---------------------------------------------------------------------------------------------------------------------
0N/A
0N/A// Block the thread at the safepoint poll or poll return.
0N/Avoid ThreadSafepointState::handle_polling_page_exception() {
0N/A
0N/A // Check state. block() will set thread state to thread_in_vm which will
0N/A // cause the safepoint state _type to become _call_back.
0N/A assert(type() == ThreadSafepointState::_running,
0N/A "polling page exception on thread not running state");
0N/A
0N/A // Step 1: Find the nmethod from the return address
0N/A if (ShowSafepointMsgs && Verbose) {
0N/A tty->print_cr("Polling page exception at " INTPTR_FORMAT, thread()->saved_exception_pc());
0N/A }
0N/A address real_return_addr = thread()->saved_exception_pc();
0N/A
0N/A CodeBlob *cb = CodeCache::find_blob(real_return_addr);
0N/A assert(cb != NULL && cb->is_nmethod(), "return address should be in nmethod");
0N/A nmethod* nm = (nmethod*)cb;
0N/A
0N/A // Find frame of caller
0N/A frame stub_fr = thread()->last_frame();
0N/A CodeBlob* stub_cb = stub_fr.cb();
0N/A assert(stub_cb->is_safepoint_stub(), "must be a safepoint stub");
0N/A RegisterMap map(thread(), true);
0N/A frame caller_fr = stub_fr.sender(&map);
0N/A
0N/A // Should only be poll_return or poll
0N/A assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" );
0N/A
0N/A // This is a poll immediately before a return. The exception handling code
0N/A // has already had the effect of causing the return to occur, so the execution
0N/A // will continue immediately after the call. In addition, the oopmap at the
0N/A // return point does not mark the return value as an oop (if it is), so
0N/A // it needs a handle here to be updated.
0N/A if( nm->is_at_poll_return(real_return_addr) ) {
0N/A // See if return type is an oop.
0N/A bool return_oop = nm->method()->is_returning_oop();
0N/A Handle return_value;
0N/A if (return_oop) {
0N/A // The oop result has been saved on the stack together with all
0N/A // the other registers. In order to preserve it over GCs we need
0N/A // to keep it in a handle.
0N/A oop result = caller_fr.saved_oop_result(&map);
0N/A assert(result == NULL || result->is_oop(), "must be oop");
0N/A return_value = Handle(thread(), result);
0N/A assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
0N/A }
0N/A
0N/A // Block the thread
0N/A SafepointSynchronize::block(thread());
0N/A
0N/A // restore oop result, if any
0N/A if (return_oop) {
0N/A caller_fr.set_saved_oop_result(&map, return_value());
0N/A }
0N/A }
0N/A
0N/A // This is a safepoint poll. Verify the return address and block.
0N/A else {
0N/A set_at_poll_safepoint(true);
0N/A
0N/A // verify the blob built the "return address" correctly
0N/A assert(real_return_addr == caller_fr.pc(), "must match");
0N/A
0N/A // Block the thread
0N/A SafepointSynchronize::block(thread());
0N/A set_at_poll_safepoint(false);
0N/A
0N/A // If we have a pending async exception deoptimize the frame
0N/A // as otherwise we may never deliver it.
0N/A if (thread()->has_async_condition()) {
0N/A ThreadInVMfromJavaNoAsyncException __tiv(thread());
1825N/A Deoptimization::deoptimize_frame(thread(), caller_fr.id());
0N/A }
0N/A
0N/A // If an exception has been installed we must check for a pending deoptimization
0N/A // Deoptimize frame if exception has been thrown.
0N/A
0N/A if (thread()->has_pending_exception() ) {
0N/A RegisterMap map(thread(), true);
0N/A frame caller_fr = stub_fr.sender(&map);
0N/A if (caller_fr.is_deoptimized_frame()) {
0N/A // The exception patch will destroy registers that are still
0N/A // live and will be needed during deoptimization. Defer the
0N/A // Async exception should have defered the exception until the
0N/A // next safepoint which will be detected when we get into
0N/A // the interpreter so if we have an exception now things
0N/A // are messed up.
0N/A
0N/A fatal("Exception installed and deoptimization is pending");
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A//
0N/A// Statistics & Instrumentations
0N/A//
0N/ASafepointSynchronize::SafepointStats* SafepointSynchronize::_safepoint_stats = NULL;
1291N/Ajlong SafepointSynchronize::_safepoint_begin_time = 0;
0N/Aint SafepointSynchronize::_cur_stat_index = 0;
0N/Ajulong SafepointSynchronize::_safepoint_reasons[VM_Operation::VMOp_Terminating];
0N/Ajulong SafepointSynchronize::_coalesced_vmop_count = 0;
0N/Ajlong SafepointSynchronize::_max_sync_time = 0;
1291N/Ajlong SafepointSynchronize::_max_vmop_time = 0;
1291N/Afloat SafepointSynchronize::_ts_of_current_safepoint = 0.0f;
0N/A
1291N/Astatic jlong cleanup_end_time = 0;
0N/Astatic bool need_to_track_page_armed_status = false;
0N/Astatic bool init_done = false;
0N/A
1291N/A// Helper method to print the header.
1291N/Astatic void print_header() {
1291N/A tty->print(" vmop "
1291N/A "[threads: total initially_running wait_to_block] ");
1291N/A tty->print("[time: spin block sync cleanup vmop] ");
1291N/A
1291N/A // no page armed status printed out if it is always armed.
1291N/A if (need_to_track_page_armed_status) {
1291N/A tty->print("page_armed ");
1291N/A }
1291N/A
1291N/A tty->print_cr("page_trap_count");
1291N/A}
1291N/A
0N/Avoid SafepointSynchronize::deferred_initialize_stat() {
0N/A if (init_done) return;
0N/A
0N/A if (PrintSafepointStatisticsCount <= 0) {
0N/A fatal("Wrong PrintSafepointStatisticsCount");
0N/A }
0N/A
0N/A // If PrintSafepointStatisticsTimeout is specified, the statistics data will
0N/A // be printed right away, in which case, _safepoint_stats will regress to
0N/A // a single element array. Otherwise, it is a circular ring buffer with default
0N/A // size of PrintSafepointStatisticsCount.
0N/A int stats_array_size;
0N/A if (PrintSafepointStatisticsTimeout > 0) {
0N/A stats_array_size = 1;
0N/A PrintSafepointStatistics = true;
0N/A } else {
0N/A stats_array_size = PrintSafepointStatisticsCount;
0N/A }
0N/A _safepoint_stats = (SafepointStats*)os::malloc(stats_array_size
0N/A * sizeof(SafepointStats));
0N/A guarantee(_safepoint_stats != NULL,
0N/A "not enough memory for safepoint instrumentation data");
0N/A
0N/A if (UseCompilerSafepoints && DeferPollingPageLoopCount >= 0) {
0N/A need_to_track_page_armed_status = true;
0N/A }
0N/A init_done = true;
0N/A}
0N/A
0N/Avoid SafepointSynchronize::begin_statistics(int nof_threads, int nof_running) {
1003N/A assert(init_done, "safepoint statistics array hasn't been initialized");
0N/A SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
0N/A
1291N/A spstat->_time_stamp = _ts_of_current_safepoint;
1291N/A
0N/A VM_Operation *op = VMThread::vm_operation();
0N/A spstat->_vmop_type = (op != NULL ? op->type() : -1);
0N/A if (op != NULL) {
0N/A _safepoint_reasons[spstat->_vmop_type]++;
0N/A }
0N/A
0N/A spstat->_nof_total_threads = nof_threads;
0N/A spstat->_nof_initial_running_threads = nof_running;
0N/A spstat->_nof_threads_hit_page_trap = 0;
0N/A
0N/A // Records the start time of spinning. The real time spent on spinning
0N/A // will be adjusted when spin is done. Same trick is applied for time
0N/A // spent on waiting for threads to block.
0N/A if (nof_running != 0) {
0N/A spstat->_time_to_spin = os::javaTimeNanos();
0N/A } else {
0N/A spstat->_time_to_spin = 0;
0N/A }
0N/A}
0N/A
0N/Avoid SafepointSynchronize::update_statistics_on_spin_end() {
0N/A SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
0N/A
0N/A jlong cur_time = os::javaTimeNanos();
0N/A
0N/A spstat->_nof_threads_wait_to_block = _waiting_to_block;
0N/A if (spstat->_nof_initial_running_threads != 0) {
0N/A spstat->_time_to_spin = cur_time - spstat->_time_to_spin;
0N/A }
0N/A
0N/A if (need_to_track_page_armed_status) {
0N/A spstat->_page_armed = (PageArmed == 1);
0N/A }
0N/A
0N/A // Records the start time of waiting for to block. Updated when block is done.
0N/A if (_waiting_to_block != 0) {
0N/A spstat->_time_to_wait_to_block = cur_time;
0N/A } else {
0N/A spstat->_time_to_wait_to_block = 0;
0N/A }
0N/A}
0N/A
0N/Avoid SafepointSynchronize::update_statistics_on_sync_end(jlong end_time) {
0N/A SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
0N/A
0N/A if (spstat->_nof_threads_wait_to_block != 0) {
0N/A spstat->_time_to_wait_to_block = end_time -
0N/A spstat->_time_to_wait_to_block;
0N/A }
0N/A
0N/A // Records the end time of sync which will be used to calculate the total
0N/A // vm operation time. Again, the real time spending in syncing will be deducted
0N/A // from the start of the sync time later when end_statistics is called.
1291N/A spstat->_time_to_sync = end_time - _safepoint_begin_time;
0N/A if (spstat->_time_to_sync > _max_sync_time) {
0N/A _max_sync_time = spstat->_time_to_sync;
0N/A }
1291N/A
1291N/A spstat->_time_to_do_cleanups = end_time;
1291N/A}
1291N/A
1291N/Avoid SafepointSynchronize::update_statistics_on_cleanup_end(jlong end_time) {
1291N/A SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
1291N/A
1291N/A // Record how long spent in cleanup tasks.
1291N/A spstat->_time_to_do_cleanups = end_time - spstat->_time_to_do_cleanups;
1291N/A
1291N/A cleanup_end_time = end_time;
0N/A}
0N/A
0N/Avoid SafepointSynchronize::end_statistics(jlong vmop_end_time) {
0N/A SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
0N/A
0N/A // Update the vm operation time.
1291N/A spstat->_time_to_exec_vmop = vmop_end_time - cleanup_end_time;
1291N/A if (spstat->_time_to_exec_vmop > _max_vmop_time) {
1291N/A _max_vmop_time = spstat->_time_to_exec_vmop;
1291N/A }
0N/A // Only the sync time longer than the specified
0N/A // PrintSafepointStatisticsTimeout will be printed out right away.
0N/A // By default, it is -1 meaning all samples will be put into the list.
0N/A if ( PrintSafepointStatisticsTimeout > 0) {
0N/A if (spstat->_time_to_sync > PrintSafepointStatisticsTimeout * MICROUNITS) {
0N/A print_statistics();
0N/A }
0N/A } else {
0N/A // The safepoint statistics will be printed out when the _safepoin_stats
0N/A // array fills up.
1291N/A if (_cur_stat_index == PrintSafepointStatisticsCount - 1) {
0N/A print_statistics();
0N/A _cur_stat_index = 0;
1291N/A } else {
1291N/A _cur_stat_index++;
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid SafepointSynchronize::print_statistics() {
0N/A SafepointStats* sstats = _safepoint_stats;
0N/A
1291N/A for (int index = 0; index <= _cur_stat_index; index++) {
1291N/A if (index % 30 == 0) {
1291N/A print_header();
1291N/A }
0N/A sstats = &_safepoint_stats[index];
1291N/A tty->print("%.3f: ", sstats->_time_stamp);
1291N/A tty->print("%-26s ["
0N/A INT32_FORMAT_W(8)INT32_FORMAT_W(11)INT32_FORMAT_W(15)
1291N/A " ] ",
0N/A sstats->_vmop_type == -1 ? "no vm operation" :
0N/A VM_Operation::name(sstats->_vmop_type),
0N/A sstats->_nof_total_threads,
0N/A sstats->_nof_initial_running_threads,
0N/A sstats->_nof_threads_wait_to_block);
0N/A // "/ MICROUNITS " is to convert the unit from nanos to millis.
1291N/A tty->print(" ["
1291N/A INT64_FORMAT_W(6)INT64_FORMAT_W(6)
1291N/A INT64_FORMAT_W(6)INT64_FORMAT_W(6)
1291N/A INT64_FORMAT_W(6)" ] ",
0N/A sstats->_time_to_spin / MICROUNITS,
0N/A sstats->_time_to_wait_to_block / MICROUNITS,
0N/A sstats->_time_to_sync / MICROUNITS,
1291N/A sstats->_time_to_do_cleanups / MICROUNITS,
1291N/A sstats->_time_to_exec_vmop / MICROUNITS);
0N/A
0N/A if (need_to_track_page_armed_status) {
0N/A tty->print(INT32_FORMAT" ", sstats->_page_armed);
0N/A }
0N/A tty->print_cr(INT32_FORMAT" ", sstats->_nof_threads_hit_page_trap);
0N/A }
0N/A}
0N/A
0N/A// This method will be called when VM exits. It will first call
0N/A// print_statistics to print out the rest of the sampling. Then
0N/A// it tries to summarize the sampling.
0N/Avoid SafepointSynchronize::print_stat_on_exit() {
0N/A if (_safepoint_stats == NULL) return;
0N/A
0N/A SafepointStats *spstat = &_safepoint_stats[_cur_stat_index];
0N/A
0N/A // During VM exit, end_statistics may not get called and in that
0N/A // case, if the sync time is less than PrintSafepointStatisticsTimeout,
0N/A // don't print it out.
0N/A // Approximate the vm op time.
0N/A _safepoint_stats[_cur_stat_index]._time_to_exec_vmop =
1291N/A os::javaTimeNanos() - cleanup_end_time;
0N/A
0N/A if ( PrintSafepointStatisticsTimeout < 0 ||
0N/A spstat->_time_to_sync > PrintSafepointStatisticsTimeout * MICROUNITS) {
0N/A print_statistics();
0N/A }
0N/A tty->print_cr("");
0N/A
0N/A // Print out polling page sampling status.
0N/A if (!need_to_track_page_armed_status) {
0N/A if (UseCompilerSafepoints) {
0N/A tty->print_cr("Polling page always armed");
0N/A }
0N/A } else {
0N/A tty->print_cr("Defer polling page loop count = %d\n",
0N/A DeferPollingPageLoopCount);
0N/A }
0N/A
0N/A for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) {
0N/A if (_safepoint_reasons[index] != 0) {
0N/A tty->print_cr("%-26s"UINT64_FORMAT_W(10), VM_Operation::name(index),
0N/A _safepoint_reasons[index]);
0N/A }
0N/A }
0N/A
0N/A tty->print_cr(UINT64_FORMAT_W(5)" VM operations coalesced during safepoint",
0N/A _coalesced_vmop_count);
0N/A tty->print_cr("Maximum sync time "INT64_FORMAT_W(5)" ms",
0N/A _max_sync_time / MICROUNITS);
1291N/A tty->print_cr("Maximum vm operation time (except for Exit VM operation) "
1291N/A INT64_FORMAT_W(5)" ms",
1291N/A _max_vmop_time / MICROUNITS);
0N/A}
0N/A
0N/A// ------------------------------------------------------------------------------------------------
0N/A// Non-product code
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Avoid SafepointSynchronize::print_state() {
0N/A if (_state == _not_synchronized) {
0N/A tty->print_cr("not synchronized");
0N/A } else if (_state == _synchronizing || _state == _synchronized) {
0N/A tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
0N/A "synchronized");
0N/A
0N/A for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
0N/A cur->safepoint_state()->print();
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid SafepointSynchronize::safepoint_msg(const char* format, ...) {
0N/A if (ShowSafepointMsgs) {
0N/A va_list ap;
0N/A va_start(ap, format);
0N/A tty->vprint_cr(format, ap);
0N/A va_end(ap);
0N/A }
0N/A}
0N/A
0N/A#endif // !PRODUCT