safepoint.cpp revision 1647
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * This code is free software; you can redistribute it and/or modify it
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * under the terms of the GNU General Public License version 2 only, as
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * published by the Free Software Foundation.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * This code is distributed in the hope that it will be useful, but WITHOUT
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * version 2 for more details (a copy is included in the LICENSE file that
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * accompanied this code).
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * You should have received a copy of the GNU General Public License version
02cbf7551685bc3b33f15f4cebfe9e9da1cae33eChristian Maeder * 2 along with this work; if not, write to the Free Software Foundation,
02cbf7551685bc3b33f15f4cebfe9e9da1cae33eChristian Maeder * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder * or visit www.oracle.com if you need additional information or have any
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder# include "incls/_precompiled.incl"
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder# include "incls/_safepoint.cpp.incl"
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder// --------------------------------------------------------------------------------------------------
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder// Implementation of Safepoint begin/end
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian MaederSafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maedervolatile int SafepointSynchronize::_waiting_to_block = 0;
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maedervolatile int SafepointSynchronize::_safepoint_counter = 0;
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maederlong SafepointSynchronize::_end_of_last_safepoint = 0;
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maederstatic volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maederstatic volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maederstatic bool timeout_error_printed = false;
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder// Roll all threads forward to a safepoint and suspend them all
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder _safepoint_begin_time = os::javaTimeNanos();
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder _ts_of_current_safepoint = tty->time_stamp().seconds();
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // In the future we should investigate whether CMS can use the
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // more-general mechanism below. DLD (01/05).
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder ConcurrentMarkSweepThread::synchronize(false);
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder ConcurrentGCThread::safepoint_synchronize();
02cbf7551685bc3b33f15f4cebfe9e9da1cae33eChristian Maeder#endif // SERIALGC
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // By getting the Threads_lock, we assure that no threads are about to start or
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // exit. It is released again in SafepointSynchronize::end().
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder int nof_threads = Threads::number_of_threads();
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder tty->print_cr("Safepoint synchronization initiated. (%d)", nof_threads);
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Set number of threads to wait for, before we initiate the callbacks
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Save the starting time, so that it can be compared to see if this has taken
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // too long to complete.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // PrintSafepointStatisticsTimeout can be specified separately. When
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // specified, PrintSafepointStatistics will be set to true in
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // deferred_initialize_stat method. The initialization has to be done
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // early enough to avoid any races. See bug 6880029 for details.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // Begin the process of bringing the system to a safepoint.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // Java threads can be in several different states and are
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // stopped by different mechanisms:
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // 1. Running interpreted
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // The interpeter dispatch table is changed to force it to
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // check for a safepoint condition between bytecodes.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 2. Running in native code
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // When returning from the native code, a Java thread must check
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // the safepoint _state to see if we must block. If the
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // VM thread sees a Java thread in native, it does
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // not wait for this thread to block. The order of the memory
02cbf7551685bc3b33f15f4cebfe9e9da1cae33eChristian Maeder // writes and reads of both the safepoint state and the Java
02cbf7551685bc3b33f15f4cebfe9e9da1cae33eChristian Maeder // threads state is critical. In order to guarantee that the
02cbf7551685bc3b33f15f4cebfe9e9da1cae33eChristian Maeder // memory writes are serialized with respect to each other,
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // the VM thread issues a memory barrier instruction
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder // (on MP systems). In order to avoid the overhead of issuing
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder // a memory barrier for each Java thread making native calls, each Java
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder // thread performs a write to a single memory page after changing
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder // the thread state. The VM thread performs a sequence of
d049e21661c3d0a2cf6339d3b94e4cd188b0188cChristian Maeder // mprotect OS calls which forces all previous writes from all
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Java threads to be serialized. This is done in the
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // os::serialize_thread_states() call. This has proven to be
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // much more efficient than executing a membar instruction
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // on every call to native code.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // 3. Running compiled Code
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // Compiled code reads a global (Safepoint Polling) page that
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // is set to fault if we are trying to get to a safepoint.
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder // 4. Blocked
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // A thread which is blocked will not be allowed to return from the
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // block condition until the safepoint operation is complete.
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder // 5. In VM or Transitioning between states
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder // If a Java thread is currently running in the VM or transitioning
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // between states, the safepointing code will wait for the thread to
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // block itself when it attempts transitions to a new state.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Flush all thread states to memory
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Make interpreter safepoint aware
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Make polling safepoint aware
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Consider using active_processor_count() ... but that call is expensive.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder assert(cur->safepoint_state()->is_running(), "Illegal initial state");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder#endif // ASSERT
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Iterate through all threads until it have been determined how to stop them all at a safepoint
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder unsigned int iterations = 0;
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder ThreadSafepointState *cur_state = cur->safepoint_state();
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // consider adjusting steps downward:
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // steps -= NNN
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // steps >>= 1
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // steps = MIN(steps, 2000-100)
710aac293d45bd34d3690944701a879828ee5dc6Christian Maeder // if (iterations != 0) steps -= NNN
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder if (TraceSafepoint && Verbose) cur_state->print();
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder if (PrintSafepointStatistics && iterations == 0) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder begin_statistics(nof_threads, still_running);
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder // Check for if it takes to long
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder print_safepoint_timeout(_spinning_timeout);
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // Spin to avoid context switching.
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // There's a tension between allowing the mutators to run (and rendezvous)
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // vs spinning. As the VM thread spins, wasting cycles, it consumes CPU that
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // a mutator might otherwise use profitably to reach a safepoint. Excessive
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // spinning by the VM thread on a saturated system can increase rendezvous latency.
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // Blocking or yielding incur their own penalties in the form of context switching
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // and the resultant loss of $ residency.
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // Further complicating matters is that yield() does not work as naively expected
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // on many platforms -- yield() does not guarantee that any other ready threads
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // will run. As such we revert yield_all() after some number of iterations.
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // Yield_all() is implemented as a short unconditional sleep on some platforms.
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // Typical operating systems round a "short" sleep period up to 10 msecs, so sleeping
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // can actually increase the time it takes the VM thread to detect that a system-wide
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // stop-the-world safepoint has been reached. In a pathological scenario such as that
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder // described in CR6415670 the VMthread may sleep just before the mutator(s) become safe.
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder // In that case the mutators will be stalled waiting for the safepoint to complete and the
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // the VMthread will be sleeping, waiting for the mutators to rendezvous. The VMthread
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // will eventually wake up and detect that all mutators are safe, at which point
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // we'll again make progress.
b1ddd2f41ea1dfac428a366c5c3d413470dcbcfaChristian Maeder // Beware too that that the VMThread typically runs at elevated priority.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Its default priority is higher than the default mutator priority.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Obviously, this complicates spinning.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Note too that on Windows XP SwitchThreadTo() has quite different behavior than Sleep(0).
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Sleep(0) will _not yield to lower priority threads, while SwitchThreadTo() will.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // See the comments in synchronizer.cpp for additional remarks on spinning.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // In the future we might:
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 1. Modify the safepoint scheme to avoid potentally unbounded spinning.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // This is tricky as the path used by a thread exiting the JVM (say on
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // on JNI call-out) simply stores into its state field. The burden
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // is placed on the VM thread, which must poll (spin).
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 2. Find something useful to do while spinning. If the safepoint is GC-related
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // we might aggressively scan the stacks of threads that are already safe.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 3. Use Solaris schedctl to examine the state of the still-running mutators.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // If all the mutators are ONPROC there's no reason to sleep or yield.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 4. YieldTo() any still-running mutators that are ready but OFFPROC.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 5. Check system saturation. If the system is not fully saturated then
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // simply spin and avoid sleep/yield.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 6. As still-running mutators rendezvous they could unpark the sleeping
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // VMthread. This works well for still-running mutators that become
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // safe. The VMthread must still poll for mutators that call-out.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 7. Drive the policy on time-since-begin instead of iterations.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 8. Consider making the spin duration a function of the # of CPUs:
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Spin = (((ncpus-1) * M) + K) + F(still_running)
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Alternately, instead of counting iterations of the outer loop
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // we could count the # of threads visited in the inner loop, above.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // 9. On windows consider using the return value from SwitchThreadTo()
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder if (UseCompilerSafepoints && int(iterations) == DeferPollingPageLoopCount) {
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Alternately, the VM thread could transiently depress its scheduling priority or
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // transiently increase the priority of the tardy mutator(s).
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder assert(still_running == 0, "sanity check");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // wait until all threads are stopped
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block);
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder if (!SafepointTimeout || timeout_error_printed) {
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder Safepoint_lock->wait(true); // true, means with no safepoint checks
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // Compute remaining time
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // If there is no remaining time, then there is an error
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder print_safepoint_timeout(_blocking_timeout);
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder assert(_waiting_to_block == 0, "sanity check");
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder tty->print_cr("# SafepointSynchronize: Finished after "
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder ((current_time - safepoint_limit_time) / MICROUNITS +
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder assert((_safepoint_counter & 0x1) == 0, "must be even");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Record state
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder VM_Operation *op = VMThread::vm_operation();
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder tty->print_cr("Entering safepoint region: %s", (op != NULL) ? op->name() : "no vm operation");
02cbf7551685bc3b33f15f4cebfe9e9da1cae33eChristian Maeder RuntimeService::record_safepoint_synchronized();
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder update_statistics_on_sync_end(os::javaTimeNanos());
e6d733e9ab4fb48cb611637528e5694d1e092de0Christian Maeder // Call stuff that needs to be run when a safepoint is just about to be completed
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder // Record how much time spend on the above cleanup tasks
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder update_statistics_on_cleanup_end(os::javaTimeNanos());
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder// Wake up all threads, so they are ready to resume execution after the safepoint
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder// operation has been carried out
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder assert((_safepoint_counter & 0x1) == 1, "must be odd");
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // memory fence isn't required here since an odd _safepoint_counter
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // value can do no harm and a fence is issued below anyway.
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder DEBUG_ONLY(Thread* myThread = Thread::current();)
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder // A pending_exception cannot be installed during a safepoint. The threads
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder // may install an async exception after they come back from a safepoint into
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder // pending_exception after they unblock. But that should happen later.
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder cur->safepoint_state()->is_at_poll_safepoint()),
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder "safepoint installed a pending exception");
4542eacf3e35ca3b52de55effecd2ffb3aef96b6Christian Maeder#endif // ASSERT
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // Make polling safepoint aware
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // Remove safepoint check from interpreter
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // when they get restarted.
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // Start suspended threads
f82f141a6b1639a50064a6e66b14f7823220b5d5Christian Maeder for(JavaThread *current = Threads::first(); current; current = current->next()) {
f82f141a6b1639a50064a6e66b14f7823220b5d5Christian Maeder // A problem occurring on Solaris is when attempting to restart threads
f82f141a6b1639a50064a6e66b14f7823220b5d5Christian Maeder // the first #cpus - 1 go well, but then the VMThread is preempted when we get
f82f141a6b1639a50064a6e66b14f7823220b5d5Christian Maeder // to the next one (since it has been running the longest). We then have
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // to wait for a cpu to become available before we can continue restarting
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder // FIXME: This causes the performance of the VM to degrade when active and with
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // large numbers of threads. Apparently this is due to the synchronous nature
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // of suspending threads.
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // TODO-FIXME: the comments above are vestigial and no longer apply.
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // Furthermore, using solaris' schedctl in this particular context confers no benefit
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder ThreadSafepointState* cur_state = current->safepoint_state();
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder assert(cur_state->is_running(), "safepoint state has not been reset");
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // blocked in signal_thread_blocked
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // If there are any concurrent GC threads resume them.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder ConcurrentMarkSweepThread::desynchronize(false);
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder ConcurrentGCThread::safepoint_desynchronize();
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder#endif // SERIALGC
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // record this time so VMThread can keep track how much time has elasped
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // since last safepoint.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder _end_of_last_safepoint = os::javaTimeMillis();
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maederbool SafepointSynchronize::is_cleanup_needed() {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // Need a safepoint if some inline cache buffers is non-empty
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder if (!InlineCacheBuffer::is_empty()) return true;
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder return false;
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // This operation is going to be performed only at the end of a safepoint
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // and hence GC's will not be going on, all Java mutators are suspended
a326c4a2e1bb141ada3b3072f5c7be86700005f2Christian Maeder // at this point and hence SystemDictionary_lock is also not needed.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(SafepointSynchronize::is_at_safepoint(), "can only be executed at a safepoint");
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder int nclasses = SystemDictionary::number_of_classes();
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder double classes_per_tick = nclasses * (CounterDecayMinIntervalLength * 1e-3 /
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder for (int i = 0; i < classes_per_tick; i++) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder klassOop k = SystemDictionary::try_get_next_class();
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder if (k != NULL && k->klass_part()->oop_is_instance()) {
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder instanceKlass::cast(k)->methods_do(do_method);
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder// Various cleaning tasks that should be done periodically at safepoints
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maedervoid SafepointSynchronize::do_cleanup_tasks() {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime);
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder ObjectSynchronizer::deflate_idle_monitors();
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder TraceTime t2("updating inline caches", TraceSafepointCleanupTime);
a326c4a2e1bb141ada3b3072f5c7be86700005f2Christian Maeder if(UseCounterDecay && CounterDecay::is_decay_needed()) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder TraceTime t3("decaying counter", TraceSafepointCleanupTime);
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder TraceTime t4("sweeping nmethods", TraceSafepointCleanupTime);
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maederbool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // native threads are safe if they have no java stack or have walkable stack
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder // blocked threads should have already have walkable stack
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder return false;
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder// -------------------------------------------------------------------------------------------------------
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder// Implementation of Safepoint callback point
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maedervoid SafepointSynchronize::block(JavaThread *thread) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(thread != NULL, "thread must be set");
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(thread->is_Java_thread(), "not a Java thread");
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // Threads shouldn't block if they are in the middle of printing, but...
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // Only bail from the block() call if the thread is gone from the
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // thread list; starting to exit should still block.
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // block current thread if we come here from native code when VM is gone
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // otherwise do nothing
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder JavaThreadState state = thread->thread_state();
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder thread->frame_anchor()->make_walkable(thread);
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // Check that we have a valid thread_state at this point
a326c4a2e1bb141ada3b3072f5c7be86700005f2Christian Maeder case _thread_in_Java: // From compiled code
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // We are highly likely to block on the Safepoint_lock. In order to avoid blocking in this case,
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder // we pretend we are still in the VM.
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // We will always be holding the Safepoint_lock when we are examine the state
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // of a thread. Hence, the instructions between the Safepoint_lock->lock() and
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // Safepoint_lock->unlock() are happening atomic with regards to the safepoint code
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder Safepoint_lock->lock_without_safepoint_check();
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // Decrement the number of threads to wait for and signal vm thread
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder assert(_waiting_to_block > 0, "sanity check");
615ef723f845e48d74cd88934738ac9ba76de0b3Christian Maeder thread->safepoint_state()->set_has_called_back(true);
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // Consider (_waiting_to_block < 2) to pipeline the wakeup of the VM thread
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // We transition the thread to state _thread_blocked here, but
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // we can't do our usual check for external suspension and then
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // self-suspend after the lock_without_safepoint_check() call
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // below because we are often called during transitions while
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // we hold different locks. That would leave us suspended while
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // holding a resource which results in deadlocks.
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // We now try to acquire the threads lock. Since this lock is hold by the VM thread during
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // the entire safepoint, the threads will all line up here during the safepoint.
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder Threads_lock->lock_without_safepoint_check();
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // restore original state. This is important if the thread comes from compiled code, so it
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // will continue to execute with the _thread_in_Java state.
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back) {
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder "Should have called back to the VM before blocking.");
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder // We transition the thread to state _thread_blocked here, but
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder // we can't do our usual check for external suspension and then
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // self-suspend after the lock_without_safepoint_check() call
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder // below because we are often called during transitions while
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // we hold different locks. That would leave us suspended while
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // holding a resource which results in deadlocks.
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // It is not safe to suspend a thread if we discover it is in _thread_in_native_trans. Hence,
a326c4a2e1bb141ada3b3072f5c7be86700005f2Christian Maeder // the safepoint code might still be waiting for it to block. We need to change the state here,
39366a8c5aa917040fcc6360841277e33da0d605Christian Maeder // so it can see that it is at a safepoint.
6e557a0d5e166ce87867876444a793bc9296b14aChristian Maeder // Block until the safepoint operation is completed.
6e557a0d5e166ce87867876444a793bc9296b14aChristian Maeder Threads_lock->lock_without_safepoint_check();
6e557a0d5e166ce87867876444a793bc9296b14aChristian Maeder // Restore state
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder fatal(err_msg("Illegal threadstate encountered: %d", state));
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder // Check for pending. async. exceptions or suspends - except if the
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder // thread was blocked inside the VM. has_special_runtime_exit_condition()
6e557a0d5e166ce87867876444a793bc9296b14aChristian Maeder // is called last since it grabs a lock and we only want to do that when
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder // Note: we never deliver an async exception at a polling point as the
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // compiler may not have an exception handler for it. The polling
9818ec2fd12dd08c36cedfb75473ccb970a5eb62Christian Maeder // code will notice the async and deoptimize and the exception will
4eef141c429d6ac5127ebcc4cd37367a5c70b0aaChristian Maeder // be delivered. (Polling at a return point is ok though). Sure is
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder // a lot of bother for a deprecated feature...
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder // We don't deliver an async exception if the thread state is
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder // _thread_in_native_trans so JNI functions won't be called with
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder // a surprising pending exception. If the thread state is going back to java,
4eef141c429d6ac5127ebcc4cd37367a5c70b0aaChristian Maeder // async exception is checked in check_special_condition_for_native_trans().
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder thread->has_special_runtime_exit_condition()) {
4eef141c429d6ac5127ebcc4cd37367a5c70b0aaChristian Maeder thread->handle_special_runtime_exit_condition(
4eef141c429d6ac5127ebcc4cd37367a5c70b0aaChristian Maeder !thread->is_at_poll_safepoint() && (state != _thread_in_native_trans));
d049e21661c3d0a2cf6339d3b94e4cd188b0188cChristian Maeder// ------------------------------------------------------------------------------------------------------
d049e21661c3d0a2cf6339d3b94e4cd188b0188cChristian Maeder// Exception handlers
d049e21661c3d0a2cf6339d3b94e4cd188b0188cChristian Maederstatic void print_ptrs(intptr_t oldptr, intptr_t newptr, bool wasoop) {
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder bool is_oop = newptr ? ((oop)newptr)->is_oop() : false;
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder tty->print_cr(PTR_FORMAT PTR_PAD " %s %c " PTR_FORMAT PTR_PAD " %s %s",
4eef141c429d6ac5127ebcc4cd37367a5c70b0aaChristian Maeder oldptr, wasoop?"oop":" ", oldptr == newptr ? ' ' : '!',
4eef141c429d6ac5127ebcc4cd37367a5c70b0aaChristian Maeder newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" "));
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maederstatic void print_longs(jlong oldptr, jlong newptr, bool wasoop) {
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder bool is_oop = newptr ? ((oop)(intptr_t)newptr)->is_oop() : false;
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder tty->print_cr(PTR64_FORMAT " %s %c " PTR64_FORMAT " %s %s",
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder oldptr, wasoop?"oop":" ", oldptr == newptr ? ' ' : '!',
710aac293d45bd34d3690944701a879828ee5dc6Christian Maeder newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" "));
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maederstatic void print_me(intptr_t *new_sp, intptr_t *old_sp, bool *was_oops) {
710aac293d45bd34d3690944701a879828ee5dc6Christian Maeder tty->print_cr("--------+------address-----+------before-----------+-------after----------+");
710aac293d45bd34d3690944701a879828ee5dc6Christian Maeder const int incr = 1; // Increment to skip a long, in units of intptr_t
710aac293d45bd34d3690944701a879828ee5dc6Christian Maeder tty->print_cr("--------+--address-+------before-----------+-------after----------+");
d049e21661c3d0a2cf6339d3b94e4cd188b0188cChristian Maeder const int incr = 2; // Increment to skip a long, in units of intptr_t
6e557a0d5e166ce87867876444a793bc9296b14aChristian Maeder for( int i=0; i<16; i++ ) {
6e557a0d5e166ce87867876444a793bc9296b14aChristian Maeder tty->print("blob %c%d |"PTR_FORMAT" ","LO"[i>>3],i&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
d049e21661c3d0a2cf6339d3b94e4cd188b0188cChristian Maeder for( int i1=0; i1<frame::memory_parameter_word_sp_offset-16; i1++ ) {
4c20d89b174474beb5f38cc4d47b1ff983c676a9Christian Maeder tty->print("argv pad|"PTR_FORMAT" ",new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder tty->print(" pad|"PTR_FORMAT" ",new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++);
710aac293d45bd34d3690944701a879828ee5dc6Christian Maeder 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;
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder 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;
a86b847d6ea0bc06d90cb01db0ac4e654d13ee98Christian Maeder 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;
d049e21661c3d0a2cf6339d3b94e4cd188b0188cChristian Maeder 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;
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder tty->print_cr(" FSR |"PTR_FORMAT" "PTR64_FORMAT" "PTR64_FORMAT,new_sp,*(jlong*)old_sp,*(jlong*)new_sp);
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder old_sp += incr; new_sp += incr; was_oops += incr;
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder // Skip the floats
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder tty->print_cr("--Float-|"PTR_FORMAT,new_sp);
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder old_sp += incr*32; new_sp += incr*32; was_oops += incr*32;
4561227a776bdf0ab679b19fb92f1eaaed8786f7Christian Maeder tty->print("call %c%d |"PTR_FORMAT" ","LI"[i2>>3],i2&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); }
f82f141a6b1639a50064a6e66b14f7823220b5d5Christian Maeder#endif // SPARC
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder#endif // PRODUCT
af48251d509031fd4ee846da43cab43844751cf7Christian Maedervoid SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder assert(thread->is_Java_thread(), "polling reference encountered by VM thread");
11a8f0b56c43688f967c4f592db6807d9f279f42Christian Maeder assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder // Uncomment this to get some serious before/after printing of the
f82f141a6b1639a50064a6e66b14f7823220b5d5Christian Maeder // Sparc safepoint-blob frame structure.
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder intptr_t* sp = thread->last_Java_sp();
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder intptr_t stack_copy[150];
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder for( int i=0; i<150; i++ ) stack_copy[i] = sp[i];
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder bool was_oops[150];
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder for( int i=0; i<150; i++ )
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder was_oops[i] = stack_copy[i] ? ((oop)stack_copy[i])->is_oop() : false;
f4a4521a8d3f171834ab351d1ac336c1898ece25Christian Maeder tty->print("handle_polling_page_exception: ");
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian Maeder ThreadSafepointState* state = thread->safepoint_state();
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian Maeder // print_me(sp,stack_copy,was_oops);
af48251d509031fd4ee846da43cab43844751cf7Christian Maedervoid SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder // Print out the thread infor which didn't reach the safepoint for debugging
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian Maeder // purposes (useful when there are lots of threads in the debugger).
a326c4a2e1bb141ada3b3072f5c7be86700005f2Christian Maeder tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder for(JavaThread *cur_thread = Threads::first(); cur_thread;
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder if (cur_thread->thread_state() != _thread_blocked &&
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder ((reason == _spinning_timeout && cur_state->is_running()) ||
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder (reason == _blocking_timeout && !cur_state->has_called_back()))) {
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder tty->print_cr("# SafepointSynchronize::begin: (End of list)");
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder // To debug the long safepoint, specify both DieOnSafepointTimeout &
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian Maeder // ShowMessageBoxOnError.
af48251d509031fd4ee846da43cab43844751cf7Christian Maeder VM_Operation *op = VMThread::vm_operation();
11a8f0b56c43688f967c4f592db6807d9f279f42Christian Maeder sprintf(msg, "Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian Maeder op != NULL ? op->name() : "no vm operation");
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian Maeder// -------------------------------------------------------------------------------------------------------
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian Maeder// Implementation of ThreadSafepointState
dd1f6a9c4c7860e85e5499dad5965e8cc4ae3814Christian MaederThreadSafepointState::ThreadSafepointState(JavaThread *thread) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maedervoid ThreadSafepointState::create(JavaThread *thread) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder ThreadSafepointState *state = new ThreadSafepointState(thread);
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maedervoid ThreadSafepointState::destroy(JavaThread *thread) {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maedervoid ThreadSafepointState::examine_state_of_thread() {
c26ff5708c4a855bf9503b3001bcc19e5fd6286fChristian Maeder assert(is_running(), "better be running or just have hit safepoint poll");
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder JavaThreadState state = _thread->thread_state();
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // Save the state at the start of safepoint processing.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // Check for a thread that is suspended. Note that thread resume tries
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // to grab the Threads_lock which we own here, so a thread cannot be
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // resumed during safepoint synchronization.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // We check to see if this thread is suspended without locking to
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // avoid deadlocking with a third thread that is waiting for this
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // thread to be suspended. The third thread can notice the safepoint
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // that we're trying to start at the beginning of its SR_lock->wait()
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // call. If that happens, then the third thread will block on the
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // safepoint while still holding the underlying SR_lock. We won't be
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // able to get the SR_lock and we'll deadlock.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // We don't need to grab the SR_lock here for two reasons:
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // 1) The suspend flags are both volatile and are set with an
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // Atomic::cmpxchg() call so we should see the suspended
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // state right away.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // 2) We're being called from the safepoint polling loop; if
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // we don't see the suspended state on this iteration, then
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // we'll come around again.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder bool is_suspended = _thread->is_ext_suspended();
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // Some JavaThread states have an initial safepoint state of
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // running, but are actually at a safepoint. We will happily
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // agree and update the safepoint state here.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder if (SafepointSynchronize::safepoint_safe(_thread, state)) {
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // All other thread states will continue to run until they
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // transition and self-block in state _blocked
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // Safepoint polling in compiled code causes the Java threads to do the same.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder // Note: new threads may require a malloc so they must be allowed to finish
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder assert(is_running(), "examine_state_of_thread on non-running thread");
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maeder// Returns true is thread could not be rolled forward at present position.
f76ecb66e7bc7b8b89384943f151465effc47942Christian Maedervoid ThreadSafepointState::roll_forward(suspend_type type) {
switch(_type) {
case _at_safepoint:
case _call_back:
set_has_called_back(false);
case _running:
switch(type()) {
case _at_safepoint:
case _call_back:
case _running:
set_has_called_back(false);
switch(_type) {
// ---------------------------------------------------------------------------------------------------------------------
if (return_oop) {
if (return_oop) {
set_at_poll_safepoint(true);
set_at_poll_safepoint(false);
static bool need_to_track_page_armed_status = false;
static bool init_done = false;
static void print_header() {
if (init_done) return;
if (PrintSafepointStatisticsCount <= 0) {
int stats_array_size;
if (PrintSafepointStatisticsTimeout > 0) {
PrintSafepointStatistics = true;
* sizeof(SafepointStats));
need_to_track_page_armed_status = true;
init_done = true;
if (nof_running != 0) {
if (_waiting_to_block != 0) {
if ( PrintSafepointStatisticsTimeout > 0) {
_cur_stat_index = 0;
print_header();
if ( PrintSafepointStatisticsTimeout < 0 ||
if (!need_to_track_page_armed_status) {
if (UseCompilerSafepoints) {
#ifndef PRODUCT
if (ShowSafepointMsgs) {