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