Thu, 12 Oct 2017 21:27:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@1 | 25 | /* |
aoqi@1 | 26 | * This file has been modified by Loongson Technology in 2015. These |
aoqi@1 | 27 | * modifications are Copyright (c) 2015 Loongson Technology, and are made |
aoqi@1 | 28 | * available on the same license terms set forth above. |
aoqi@1 | 29 | */ |
aoqi@1 | 30 | |
aoqi@0 | 31 | #include "precompiled.hpp" |
aoqi@0 | 32 | #include "classfile/symbolTable.hpp" |
aoqi@0 | 33 | #include "classfile/systemDictionary.hpp" |
aoqi@0 | 34 | #include "code/codeCache.hpp" |
aoqi@0 | 35 | #include "code/icBuffer.hpp" |
aoqi@0 | 36 | #include "code/nmethod.hpp" |
aoqi@0 | 37 | #include "code/pcDesc.hpp" |
aoqi@0 | 38 | #include "code/scopeDesc.hpp" |
aoqi@0 | 39 | #include "gc_interface/collectedHeap.hpp" |
aoqi@0 | 40 | #include "interpreter/interpreter.hpp" |
aoqi@0 | 41 | #include "memory/resourceArea.hpp" |
aoqi@0 | 42 | #include "memory/universe.inline.hpp" |
aoqi@0 | 43 | #include "oops/oop.inline.hpp" |
aoqi@0 | 44 | #include "oops/symbol.hpp" |
aoqi@0 | 45 | #include "runtime/compilationPolicy.hpp" |
aoqi@0 | 46 | #include "runtime/deoptimization.hpp" |
aoqi@0 | 47 | #include "runtime/frame.inline.hpp" |
aoqi@0 | 48 | #include "runtime/interfaceSupport.hpp" |
aoqi@0 | 49 | #include "runtime/mutexLocker.hpp" |
goetz@6911 | 50 | #include "runtime/orderAccess.inline.hpp" |
aoqi@0 | 51 | #include "runtime/osThread.hpp" |
aoqi@0 | 52 | #include "runtime/safepoint.hpp" |
aoqi@0 | 53 | #include "runtime/signature.hpp" |
aoqi@0 | 54 | #include "runtime/stubCodeGenerator.hpp" |
aoqi@0 | 55 | #include "runtime/stubRoutines.hpp" |
aoqi@0 | 56 | #include "runtime/sweeper.hpp" |
aoqi@0 | 57 | #include "runtime/synchronizer.hpp" |
aoqi@0 | 58 | #include "runtime/thread.inline.hpp" |
aoqi@0 | 59 | #include "services/runtimeService.hpp" |
aoqi@0 | 60 | #include "utilities/events.hpp" |
aoqi@0 | 61 | #include "utilities/macros.hpp" |
aoqi@0 | 62 | #ifdef TARGET_ARCH_x86 |
aoqi@0 | 63 | # include "nativeInst_x86.hpp" |
aoqi@0 | 64 | # include "vmreg_x86.inline.hpp" |
aoqi@0 | 65 | #endif |
aoqi@0 | 66 | #ifdef TARGET_ARCH_sparc |
aoqi@0 | 67 | # include "nativeInst_sparc.hpp" |
aoqi@0 | 68 | # include "vmreg_sparc.inline.hpp" |
aoqi@0 | 69 | #endif |
aoqi@0 | 70 | #ifdef TARGET_ARCH_zero |
aoqi@0 | 71 | # include "nativeInst_zero.hpp" |
aoqi@0 | 72 | # include "vmreg_zero.inline.hpp" |
aoqi@0 | 73 | #endif |
aoqi@0 | 74 | #ifdef TARGET_ARCH_arm |
aoqi@0 | 75 | # include "nativeInst_arm.hpp" |
aoqi@0 | 76 | # include "vmreg_arm.inline.hpp" |
aoqi@0 | 77 | #endif |
aoqi@0 | 78 | #ifdef TARGET_ARCH_ppc |
aoqi@0 | 79 | # include "nativeInst_ppc.hpp" |
aoqi@0 | 80 | # include "vmreg_ppc.inline.hpp" |
aoqi@0 | 81 | #endif |
aoqi@1 | 82 | #ifdef TARGET_ARCH_mips |
aoqi@1 | 83 | # include "nativeInst_mips.hpp" |
aoqi@1 | 84 | # include "vmreg_mips.inline.hpp" |
aoqi@1 | 85 | #endif |
aoqi@0 | 86 | #if INCLUDE_ALL_GCS |
aoqi@0 | 87 | #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp" |
pliden@6906 | 88 | #include "gc_implementation/shared/suspendibleThreadSet.hpp" |
aoqi@0 | 89 | #endif // INCLUDE_ALL_GCS |
aoqi@0 | 90 | #ifdef COMPILER1 |
aoqi@0 | 91 | #include "c1/c1_globals.hpp" |
aoqi@0 | 92 | #endif |
aoqi@0 | 93 | |
aoqi@0 | 94 | PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
aoqi@0 | 95 | |
aoqi@0 | 96 | // -------------------------------------------------------------------------------------------------- |
aoqi@0 | 97 | // Implementation of Safepoint begin/end |
aoqi@0 | 98 | |
aoqi@0 | 99 | SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized; |
aoqi@0 | 100 | volatile int SafepointSynchronize::_waiting_to_block = 0; |
aoqi@0 | 101 | volatile int SafepointSynchronize::_safepoint_counter = 0; |
aoqi@0 | 102 | int SafepointSynchronize::_current_jni_active_count = 0; |
aoqi@0 | 103 | long SafepointSynchronize::_end_of_last_safepoint = 0; |
aoqi@0 | 104 | static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE |
aoqi@0 | 105 | static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only |
aoqi@0 | 106 | static bool timeout_error_printed = false; |
aoqi@0 | 107 | |
aoqi@0 | 108 | // Roll all threads forward to a safepoint and suspend them all |
aoqi@0 | 109 | void SafepointSynchronize::begin() { |
aoqi@0 | 110 | |
aoqi@0 | 111 | Thread* myThread = Thread::current(); |
aoqi@0 | 112 | assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint"); |
aoqi@0 | 113 | |
aoqi@0 | 114 | if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) { |
aoqi@0 | 115 | _safepoint_begin_time = os::javaTimeNanos(); |
aoqi@0 | 116 | _ts_of_current_safepoint = tty->time_stamp().seconds(); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | #if INCLUDE_ALL_GCS |
aoqi@0 | 120 | if (UseConcMarkSweepGC) { |
aoqi@0 | 121 | // In the future we should investigate whether CMS can use the |
aoqi@0 | 122 | // more-general mechanism below. DLD (01/05). |
aoqi@0 | 123 | ConcurrentMarkSweepThread::synchronize(false); |
aoqi@0 | 124 | } else if (UseG1GC) { |
pliden@6906 | 125 | SuspendibleThreadSet::synchronize(); |
aoqi@0 | 126 | } |
aoqi@0 | 127 | #endif // INCLUDE_ALL_GCS |
aoqi@0 | 128 | |
aoqi@0 | 129 | // By getting the Threads_lock, we assure that no threads are about to start or |
aoqi@0 | 130 | // exit. It is released again in SafepointSynchronize::end(). |
aoqi@0 | 131 | Threads_lock->lock(); |
aoqi@0 | 132 | |
aoqi@0 | 133 | assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state"); |
aoqi@0 | 134 | |
aoqi@0 | 135 | int nof_threads = Threads::number_of_threads(); |
aoqi@0 | 136 | |
aoqi@0 | 137 | if (TraceSafepoint) { |
aoqi@0 | 138 | tty->print_cr("Safepoint synchronization initiated. (%d)", nof_threads); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | |
aoqi@0 | 141 | RuntimeService::record_safepoint_begin(); |
aoqi@0 | 142 | |
aoqi@0 | 143 | MutexLocker mu(Safepoint_lock); |
aoqi@0 | 144 | |
aoqi@0 | 145 | // Reset the count of active JNI critical threads |
aoqi@0 | 146 | _current_jni_active_count = 0; |
aoqi@0 | 147 | |
aoqi@0 | 148 | // Set number of threads to wait for, before we initiate the callbacks |
aoqi@0 | 149 | _waiting_to_block = nof_threads; |
aoqi@0 | 150 | TryingToBlock = 0 ; |
aoqi@0 | 151 | int still_running = nof_threads; |
aoqi@0 | 152 | |
aoqi@0 | 153 | // Save the starting time, so that it can be compared to see if this has taken |
aoqi@0 | 154 | // too long to complete. |
aoqi@0 | 155 | jlong safepoint_limit_time; |
aoqi@0 | 156 | timeout_error_printed = false; |
aoqi@0 | 157 | |
aoqi@0 | 158 | // PrintSafepointStatisticsTimeout can be specified separately. When |
aoqi@0 | 159 | // specified, PrintSafepointStatistics will be set to true in |
aoqi@0 | 160 | // deferred_initialize_stat method. The initialization has to be done |
aoqi@0 | 161 | // early enough to avoid any races. See bug 6880029 for details. |
aoqi@0 | 162 | if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) { |
aoqi@0 | 163 | deferred_initialize_stat(); |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | // Begin the process of bringing the system to a safepoint. |
aoqi@0 | 167 | // Java threads can be in several different states and are |
aoqi@0 | 168 | // stopped by different mechanisms: |
aoqi@0 | 169 | // |
aoqi@0 | 170 | // 1. Running interpreted |
aoqi@0 | 171 | // The interpeter dispatch table is changed to force it to |
aoqi@0 | 172 | // check for a safepoint condition between bytecodes. |
aoqi@0 | 173 | // 2. Running in native code |
aoqi@0 | 174 | // When returning from the native code, a Java thread must check |
aoqi@0 | 175 | // the safepoint _state to see if we must block. If the |
aoqi@0 | 176 | // VM thread sees a Java thread in native, it does |
aoqi@0 | 177 | // not wait for this thread to block. The order of the memory |
aoqi@0 | 178 | // writes and reads of both the safepoint state and the Java |
aoqi@0 | 179 | // threads state is critical. In order to guarantee that the |
aoqi@0 | 180 | // memory writes are serialized with respect to each other, |
aoqi@0 | 181 | // the VM thread issues a memory barrier instruction |
aoqi@0 | 182 | // (on MP systems). In order to avoid the overhead of issuing |
aoqi@0 | 183 | // a memory barrier for each Java thread making native calls, each Java |
aoqi@0 | 184 | // thread performs a write to a single memory page after changing |
aoqi@0 | 185 | // the thread state. The VM thread performs a sequence of |
aoqi@0 | 186 | // mprotect OS calls which forces all previous writes from all |
aoqi@0 | 187 | // Java threads to be serialized. This is done in the |
aoqi@0 | 188 | // os::serialize_thread_states() call. This has proven to be |
aoqi@0 | 189 | // much more efficient than executing a membar instruction |
aoqi@0 | 190 | // on every call to native code. |
aoqi@0 | 191 | // 3. Running compiled Code |
aoqi@0 | 192 | // Compiled code reads a global (Safepoint Polling) page that |
aoqi@0 | 193 | // is set to fault if we are trying to get to a safepoint. |
aoqi@0 | 194 | // 4. Blocked |
aoqi@0 | 195 | // A thread which is blocked will not be allowed to return from the |
aoqi@0 | 196 | // block condition until the safepoint operation is complete. |
aoqi@0 | 197 | // 5. In VM or Transitioning between states |
aoqi@0 | 198 | // If a Java thread is currently running in the VM or transitioning |
aoqi@0 | 199 | // between states, the safepointing code will wait for the thread to |
aoqi@0 | 200 | // block itself when it attempts transitions to a new state. |
aoqi@0 | 201 | // |
aoqi@0 | 202 | _state = _synchronizing; |
aoqi@0 | 203 | OrderAccess::fence(); |
aoqi@0 | 204 | |
aoqi@0 | 205 | // Flush all thread states to memory |
aoqi@0 | 206 | if (!UseMembar) { |
aoqi@0 | 207 | os::serialize_thread_states(); |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | // Make interpreter safepoint aware |
aoqi@0 | 211 | Interpreter::notice_safepoints(); |
aoqi@0 | 212 | |
aoqi@0 | 213 | if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) { |
aoqi@0 | 214 | // Make polling safepoint aware |
aoqi@0 | 215 | guarantee (PageArmed == 0, "invariant") ; |
aoqi@0 | 216 | PageArmed = 1 ; |
aoqi@0 | 217 | os::make_polling_page_unreadable(); |
aoqi@0 | 218 | } |
aoqi@0 | 219 | |
aoqi@0 | 220 | // Consider using active_processor_count() ... but that call is expensive. |
aoqi@0 | 221 | int ncpus = os::processor_count() ; |
aoqi@0 | 222 | |
aoqi@0 | 223 | #ifdef ASSERT |
aoqi@0 | 224 | for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) { |
aoqi@0 | 225 | assert(cur->safepoint_state()->is_running(), "Illegal initial state"); |
aoqi@0 | 226 | // Clear the visited flag to ensure that the critical counts are collected properly. |
aoqi@0 | 227 | cur->set_visited_for_critical_count(false); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | #endif // ASSERT |
aoqi@0 | 230 | |
aoqi@0 | 231 | if (SafepointTimeout) |
aoqi@0 | 232 | safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS; |
aoqi@0 | 233 | |
aoqi@0 | 234 | // Iterate through all threads until it have been determined how to stop them all at a safepoint |
aoqi@0 | 235 | unsigned int iterations = 0; |
aoqi@0 | 236 | int steps = 0 ; |
aoqi@0 | 237 | while(still_running > 0) { |
aoqi@0 | 238 | for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) { |
aoqi@0 | 239 | assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended"); |
aoqi@0 | 240 | ThreadSafepointState *cur_state = cur->safepoint_state(); |
aoqi@0 | 241 | if (cur_state->is_running()) { |
aoqi@0 | 242 | cur_state->examine_state_of_thread(); |
aoqi@0 | 243 | if (!cur_state->is_running()) { |
aoqi@0 | 244 | still_running--; |
aoqi@0 | 245 | // consider adjusting steps downward: |
aoqi@0 | 246 | // steps = 0 |
aoqi@0 | 247 | // steps -= NNN |
aoqi@0 | 248 | // steps >>= 1 |
aoqi@0 | 249 | // steps = MIN(steps, 2000-100) |
aoqi@0 | 250 | // if (iterations != 0) steps -= NNN |
aoqi@0 | 251 | } |
aoqi@0 | 252 | if (TraceSafepoint && Verbose) cur_state->print(); |
aoqi@0 | 253 | } |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | if (PrintSafepointStatistics && iterations == 0) { |
aoqi@0 | 257 | begin_statistics(nof_threads, still_running); |
aoqi@0 | 258 | } |
aoqi@0 | 259 | |
aoqi@0 | 260 | if (still_running > 0) { |
aoqi@0 | 261 | // Check for if it takes to long |
aoqi@0 | 262 | if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) { |
aoqi@0 | 263 | print_safepoint_timeout(_spinning_timeout); |
aoqi@0 | 264 | } |
aoqi@0 | 265 | |
aoqi@0 | 266 | // Spin to avoid context switching. |
aoqi@0 | 267 | // There's a tension between allowing the mutators to run (and rendezvous) |
aoqi@0 | 268 | // vs spinning. As the VM thread spins, wasting cycles, it consumes CPU that |
aoqi@0 | 269 | // a mutator might otherwise use profitably to reach a safepoint. Excessive |
aoqi@0 | 270 | // spinning by the VM thread on a saturated system can increase rendezvous latency. |
aoqi@0 | 271 | // Blocking or yielding incur their own penalties in the form of context switching |
aoqi@0 | 272 | // and the resultant loss of $ residency. |
aoqi@0 | 273 | // |
aoqi@0 | 274 | // Further complicating matters is that yield() does not work as naively expected |
aoqi@0 | 275 | // on many platforms -- yield() does not guarantee that any other ready threads |
aoqi@0 | 276 | // will run. As such we revert yield_all() after some number of iterations. |
aoqi@0 | 277 | // Yield_all() is implemented as a short unconditional sleep on some platforms. |
aoqi@0 | 278 | // Typical operating systems round a "short" sleep period up to 10 msecs, so sleeping |
aoqi@0 | 279 | // can actually increase the time it takes the VM thread to detect that a system-wide |
aoqi@0 | 280 | // stop-the-world safepoint has been reached. In a pathological scenario such as that |
aoqi@0 | 281 | // described in CR6415670 the VMthread may sleep just before the mutator(s) become safe. |
aoqi@0 | 282 | // In that case the mutators will be stalled waiting for the safepoint to complete and the |
aoqi@0 | 283 | // the VMthread will be sleeping, waiting for the mutators to rendezvous. The VMthread |
aoqi@0 | 284 | // will eventually wake up and detect that all mutators are safe, at which point |
aoqi@0 | 285 | // we'll again make progress. |
aoqi@0 | 286 | // |
aoqi@0 | 287 | // Beware too that that the VMThread typically runs at elevated priority. |
aoqi@0 | 288 | // Its default priority is higher than the default mutator priority. |
aoqi@0 | 289 | // Obviously, this complicates spinning. |
aoqi@0 | 290 | // |
aoqi@0 | 291 | // Note too that on Windows XP SwitchThreadTo() has quite different behavior than Sleep(0). |
aoqi@0 | 292 | // Sleep(0) will _not yield to lower priority threads, while SwitchThreadTo() will. |
aoqi@0 | 293 | // |
aoqi@0 | 294 | // See the comments in synchronizer.cpp for additional remarks on spinning. |
aoqi@0 | 295 | // |
aoqi@0 | 296 | // In the future we might: |
aoqi@0 | 297 | // 1. Modify the safepoint scheme to avoid potentally unbounded spinning. |
aoqi@0 | 298 | // This is tricky as the path used by a thread exiting the JVM (say on |
aoqi@0 | 299 | // on JNI call-out) simply stores into its state field. The burden |
aoqi@0 | 300 | // is placed on the VM thread, which must poll (spin). |
aoqi@0 | 301 | // 2. Find something useful to do while spinning. If the safepoint is GC-related |
aoqi@0 | 302 | // we might aggressively scan the stacks of threads that are already safe. |
aoqi@0 | 303 | // 3. Use Solaris schedctl to examine the state of the still-running mutators. |
aoqi@0 | 304 | // If all the mutators are ONPROC there's no reason to sleep or yield. |
aoqi@0 | 305 | // 4. YieldTo() any still-running mutators that are ready but OFFPROC. |
aoqi@0 | 306 | // 5. Check system saturation. If the system is not fully saturated then |
aoqi@0 | 307 | // simply spin and avoid sleep/yield. |
aoqi@0 | 308 | // 6. As still-running mutators rendezvous they could unpark the sleeping |
aoqi@0 | 309 | // VMthread. This works well for still-running mutators that become |
aoqi@0 | 310 | // safe. The VMthread must still poll for mutators that call-out. |
aoqi@0 | 311 | // 7. Drive the policy on time-since-begin instead of iterations. |
aoqi@0 | 312 | // 8. Consider making the spin duration a function of the # of CPUs: |
aoqi@0 | 313 | // Spin = (((ncpus-1) * M) + K) + F(still_running) |
aoqi@0 | 314 | // Alternately, instead of counting iterations of the outer loop |
aoqi@0 | 315 | // we could count the # of threads visited in the inner loop, above. |
aoqi@0 | 316 | // 9. On windows consider using the return value from SwitchThreadTo() |
aoqi@0 | 317 | // to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions. |
aoqi@0 | 318 | |
aoqi@0 | 319 | if (UseCompilerSafepoints && int(iterations) == DeferPollingPageLoopCount) { |
aoqi@0 | 320 | guarantee (PageArmed == 0, "invariant") ; |
aoqi@0 | 321 | PageArmed = 1 ; |
aoqi@0 | 322 | os::make_polling_page_unreadable(); |
aoqi@0 | 323 | } |
aoqi@0 | 324 | |
aoqi@0 | 325 | // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or |
aoqi@0 | 326 | // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus) |
aoqi@0 | 327 | ++steps ; |
aoqi@0 | 328 | if (ncpus > 1 && steps < SafepointSpinBeforeYield) { |
aoqi@0 | 329 | SpinPause() ; // MP-Polite spin |
aoqi@0 | 330 | } else |
aoqi@0 | 331 | if (steps < DeferThrSuspendLoopCount) { |
aoqi@0 | 332 | os::NakedYield() ; |
aoqi@0 | 333 | } else { |
aoqi@0 | 334 | os::yield_all(steps) ; |
aoqi@0 | 335 | // Alternately, the VM thread could transiently depress its scheduling priority or |
aoqi@0 | 336 | // transiently increase the priority of the tardy mutator(s). |
aoqi@0 | 337 | } |
aoqi@0 | 338 | |
aoqi@0 | 339 | iterations ++ ; |
aoqi@0 | 340 | } |
aoqi@0 | 341 | assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long"); |
aoqi@0 | 342 | } |
aoqi@0 | 343 | assert(still_running == 0, "sanity check"); |
aoqi@0 | 344 | |
aoqi@0 | 345 | if (PrintSafepointStatistics) { |
aoqi@0 | 346 | update_statistics_on_spin_end(); |
aoqi@0 | 347 | } |
aoqi@0 | 348 | |
aoqi@0 | 349 | // wait until all threads are stopped |
aoqi@0 | 350 | while (_waiting_to_block > 0) { |
aoqi@0 | 351 | if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block); |
aoqi@0 | 352 | if (!SafepointTimeout || timeout_error_printed) { |
aoqi@0 | 353 | Safepoint_lock->wait(true); // true, means with no safepoint checks |
aoqi@0 | 354 | } else { |
aoqi@0 | 355 | // Compute remaining time |
aoqi@0 | 356 | jlong remaining_time = safepoint_limit_time - os::javaTimeNanos(); |
aoqi@0 | 357 | |
aoqi@0 | 358 | // If there is no remaining time, then there is an error |
aoqi@0 | 359 | if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) { |
aoqi@0 | 360 | print_safepoint_timeout(_blocking_timeout); |
aoqi@0 | 361 | } |
aoqi@0 | 362 | } |
aoqi@0 | 363 | } |
aoqi@0 | 364 | assert(_waiting_to_block == 0, "sanity check"); |
aoqi@0 | 365 | |
aoqi@0 | 366 | #ifndef PRODUCT |
aoqi@0 | 367 | if (SafepointTimeout) { |
aoqi@0 | 368 | jlong current_time = os::javaTimeNanos(); |
aoqi@0 | 369 | if (safepoint_limit_time < current_time) { |
aoqi@0 | 370 | tty->print_cr("# SafepointSynchronize: Finished after " |
aoqi@0 | 371 | INT64_FORMAT_W(6) " ms", |
aoqi@0 | 372 | ((current_time - safepoint_limit_time) / MICROUNITS + |
aoqi@0 | 373 | SafepointTimeoutDelay)); |
aoqi@0 | 374 | } |
aoqi@0 | 375 | } |
aoqi@0 | 376 | #endif |
aoqi@0 | 377 | |
aoqi@0 | 378 | assert((_safepoint_counter & 0x1) == 0, "must be even"); |
aoqi@0 | 379 | assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); |
aoqi@0 | 380 | _safepoint_counter ++; |
aoqi@0 | 381 | |
aoqi@0 | 382 | // Record state |
aoqi@0 | 383 | _state = _synchronized; |
aoqi@0 | 384 | |
aoqi@0 | 385 | OrderAccess::fence(); |
aoqi@0 | 386 | |
aoqi@0 | 387 | #ifdef ASSERT |
aoqi@0 | 388 | for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) { |
aoqi@0 | 389 | // make sure all the threads were visited |
aoqi@0 | 390 | assert(cur->was_visited_for_critical_count(), "missed a thread"); |
aoqi@0 | 391 | } |
aoqi@0 | 392 | #endif // ASSERT |
aoqi@0 | 393 | |
aoqi@0 | 394 | // Update the count of active JNI critical regions |
aoqi@0 | 395 | GC_locker::set_jni_lock_count(_current_jni_active_count); |
aoqi@0 | 396 | |
aoqi@0 | 397 | if (TraceSafepoint) { |
aoqi@0 | 398 | VM_Operation *op = VMThread::vm_operation(); |
aoqi@0 | 399 | tty->print_cr("Entering safepoint region: %s", (op != NULL) ? op->name() : "no vm operation"); |
aoqi@0 | 400 | } |
aoqi@0 | 401 | |
aoqi@0 | 402 | RuntimeService::record_safepoint_synchronized(); |
aoqi@0 | 403 | if (PrintSafepointStatistics) { |
aoqi@0 | 404 | update_statistics_on_sync_end(os::javaTimeNanos()); |
aoqi@0 | 405 | } |
aoqi@0 | 406 | |
aoqi@0 | 407 | // Call stuff that needs to be run when a safepoint is just about to be completed |
aoqi@0 | 408 | do_cleanup_tasks(); |
aoqi@0 | 409 | |
aoqi@0 | 410 | if (PrintSafepointStatistics) { |
aoqi@0 | 411 | // Record how much time spend on the above cleanup tasks |
aoqi@0 | 412 | update_statistics_on_cleanup_end(os::javaTimeNanos()); |
aoqi@0 | 413 | } |
aoqi@0 | 414 | } |
aoqi@0 | 415 | |
aoqi@0 | 416 | // Wake up all threads, so they are ready to resume execution after the safepoint |
aoqi@0 | 417 | // operation has been carried out |
aoqi@0 | 418 | void SafepointSynchronize::end() { |
aoqi@0 | 419 | |
aoqi@0 | 420 | assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); |
aoqi@0 | 421 | assert((_safepoint_counter & 0x1) == 1, "must be odd"); |
aoqi@0 | 422 | _safepoint_counter ++; |
aoqi@0 | 423 | // memory fence isn't required here since an odd _safepoint_counter |
aoqi@0 | 424 | // value can do no harm and a fence is issued below anyway. |
aoqi@0 | 425 | |
aoqi@0 | 426 | DEBUG_ONLY(Thread* myThread = Thread::current();) |
aoqi@0 | 427 | assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint"); |
aoqi@0 | 428 | |
aoqi@0 | 429 | if (PrintSafepointStatistics) { |
aoqi@0 | 430 | end_statistics(os::javaTimeNanos()); |
aoqi@0 | 431 | } |
aoqi@0 | 432 | |
aoqi@0 | 433 | #ifdef ASSERT |
aoqi@0 | 434 | // A pending_exception cannot be installed during a safepoint. The threads |
aoqi@0 | 435 | // may install an async exception after they come back from a safepoint into |
aoqi@0 | 436 | // pending_exception after they unblock. But that should happen later. |
aoqi@0 | 437 | for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) { |
aoqi@0 | 438 | assert (!(cur->has_pending_exception() && |
aoqi@0 | 439 | cur->safepoint_state()->is_at_poll_safepoint()), |
aoqi@0 | 440 | "safepoint installed a pending exception"); |
aoqi@0 | 441 | } |
aoqi@0 | 442 | #endif // ASSERT |
aoqi@0 | 443 | |
aoqi@0 | 444 | if (PageArmed) { |
aoqi@0 | 445 | // Make polling safepoint aware |
aoqi@0 | 446 | os::make_polling_page_readable(); |
aoqi@0 | 447 | PageArmed = 0 ; |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | // Remove safepoint check from interpreter |
aoqi@0 | 451 | Interpreter::ignore_safepoints(); |
aoqi@0 | 452 | |
aoqi@0 | 453 | { |
aoqi@0 | 454 | MutexLocker mu(Safepoint_lock); |
aoqi@0 | 455 | |
aoqi@0 | 456 | assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization"); |
aoqi@0 | 457 | |
aoqi@0 | 458 | // Set to not synchronized, so the threads will not go into the signal_thread_blocked method |
aoqi@0 | 459 | // when they get restarted. |
aoqi@0 | 460 | _state = _not_synchronized; |
aoqi@0 | 461 | OrderAccess::fence(); |
aoqi@0 | 462 | |
aoqi@0 | 463 | if (TraceSafepoint) { |
aoqi@0 | 464 | tty->print_cr("Leaving safepoint region"); |
aoqi@0 | 465 | } |
aoqi@0 | 466 | |
aoqi@0 | 467 | // Start suspended threads |
aoqi@0 | 468 | for(JavaThread *current = Threads::first(); current; current = current->next()) { |
aoqi@0 | 469 | // A problem occurring on Solaris is when attempting to restart threads |
aoqi@0 | 470 | // the first #cpus - 1 go well, but then the VMThread is preempted when we get |
aoqi@0 | 471 | // to the next one (since it has been running the longest). We then have |
aoqi@0 | 472 | // to wait for a cpu to become available before we can continue restarting |
aoqi@0 | 473 | // threads. |
aoqi@0 | 474 | // FIXME: This causes the performance of the VM to degrade when active and with |
aoqi@0 | 475 | // large numbers of threads. Apparently this is due to the synchronous nature |
aoqi@0 | 476 | // of suspending threads. |
aoqi@0 | 477 | // |
aoqi@0 | 478 | // TODO-FIXME: the comments above are vestigial and no longer apply. |
aoqi@0 | 479 | // Furthermore, using solaris' schedctl in this particular context confers no benefit |
aoqi@0 | 480 | if (VMThreadHintNoPreempt) { |
aoqi@0 | 481 | os::hint_no_preempt(); |
aoqi@0 | 482 | } |
aoqi@0 | 483 | ThreadSafepointState* cur_state = current->safepoint_state(); |
aoqi@0 | 484 | assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint"); |
aoqi@0 | 485 | cur_state->restart(); |
aoqi@0 | 486 | assert(cur_state->is_running(), "safepoint state has not been reset"); |
aoqi@0 | 487 | } |
aoqi@0 | 488 | |
aoqi@0 | 489 | RuntimeService::record_safepoint_end(); |
aoqi@0 | 490 | |
aoqi@0 | 491 | // Release threads lock, so threads can be created/destroyed again. It will also starts all threads |
aoqi@0 | 492 | // blocked in signal_thread_blocked |
aoqi@0 | 493 | Threads_lock->unlock(); |
aoqi@0 | 494 | |
aoqi@0 | 495 | } |
aoqi@0 | 496 | #if INCLUDE_ALL_GCS |
aoqi@0 | 497 | // If there are any concurrent GC threads resume them. |
aoqi@0 | 498 | if (UseConcMarkSweepGC) { |
aoqi@0 | 499 | ConcurrentMarkSweepThread::desynchronize(false); |
aoqi@0 | 500 | } else if (UseG1GC) { |
pliden@6906 | 501 | SuspendibleThreadSet::desynchronize(); |
aoqi@0 | 502 | } |
aoqi@0 | 503 | #endif // INCLUDE_ALL_GCS |
aoqi@0 | 504 | // record this time so VMThread can keep track how much time has elasped |
aoqi@0 | 505 | // since last safepoint. |
aoqi@0 | 506 | _end_of_last_safepoint = os::javaTimeMillis(); |
aoqi@0 | 507 | } |
aoqi@0 | 508 | |
aoqi@0 | 509 | bool SafepointSynchronize::is_cleanup_needed() { |
aoqi@0 | 510 | // Need a safepoint if some inline cache buffers is non-empty |
aoqi@0 | 511 | if (!InlineCacheBuffer::is_empty()) return true; |
aoqi@0 | 512 | return false; |
aoqi@0 | 513 | } |
aoqi@0 | 514 | |
aoqi@0 | 515 | |
aoqi@0 | 516 | |
aoqi@0 | 517 | // Various cleaning tasks that should be done periodically at safepoints |
aoqi@0 | 518 | void SafepointSynchronize::do_cleanup_tasks() { |
aoqi@0 | 519 | { |
aoqi@0 | 520 | TraceTime t1("deflating idle monitors", TraceSafepointCleanupTime); |
aoqi@0 | 521 | ObjectSynchronizer::deflate_idle_monitors(); |
aoqi@0 | 522 | } |
aoqi@0 | 523 | |
aoqi@0 | 524 | { |
aoqi@0 | 525 | TraceTime t2("updating inline caches", TraceSafepointCleanupTime); |
aoqi@0 | 526 | InlineCacheBuffer::update_inline_caches(); |
aoqi@0 | 527 | } |
aoqi@0 | 528 | { |
aoqi@0 | 529 | TraceTime t3("compilation policy safepoint handler", TraceSafepointCleanupTime); |
aoqi@0 | 530 | CompilationPolicy::policy()->do_safepoint_work(); |
aoqi@0 | 531 | } |
aoqi@0 | 532 | |
aoqi@0 | 533 | { |
aoqi@0 | 534 | TraceTime t4("mark nmethods", TraceSafepointCleanupTime); |
aoqi@0 | 535 | NMethodSweeper::mark_active_nmethods(); |
aoqi@0 | 536 | } |
aoqi@0 | 537 | |
aoqi@0 | 538 | if (SymbolTable::needs_rehashing()) { |
aoqi@0 | 539 | TraceTime t5("rehashing symbol table", TraceSafepointCleanupTime); |
aoqi@0 | 540 | SymbolTable::rehash_table(); |
aoqi@0 | 541 | } |
aoqi@0 | 542 | |
aoqi@0 | 543 | if (StringTable::needs_rehashing()) { |
aoqi@0 | 544 | TraceTime t6("rehashing string table", TraceSafepointCleanupTime); |
aoqi@0 | 545 | StringTable::rehash_table(); |
aoqi@0 | 546 | } |
aoqi@0 | 547 | |
aoqi@0 | 548 | // rotate log files? |
aoqi@0 | 549 | if (UseGCLogFileRotation) { |
aoqi@0 | 550 | gclog_or_tty->rotate_log(false); |
aoqi@0 | 551 | } |
aoqi@0 | 552 | |
aoqi@0 | 553 | { |
aoqi@0 | 554 | // CMS delays purging the CLDG until the beginning of the next safepoint and to |
aoqi@0 | 555 | // make sure concurrent sweep is done |
aoqi@0 | 556 | TraceTime t7("purging class loader data graph", TraceSafepointCleanupTime); |
aoqi@0 | 557 | ClassLoaderDataGraph::purge_if_needed(); |
aoqi@0 | 558 | } |
aoqi@0 | 559 | } |
aoqi@0 | 560 | |
aoqi@0 | 561 | |
aoqi@0 | 562 | bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) { |
aoqi@0 | 563 | switch(state) { |
aoqi@0 | 564 | case _thread_in_native: |
aoqi@0 | 565 | // native threads are safe if they have no java stack or have walkable stack |
aoqi@0 | 566 | return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable(); |
aoqi@0 | 567 | |
aoqi@0 | 568 | // blocked threads should have already have walkable stack |
aoqi@0 | 569 | case _thread_blocked: |
aoqi@0 | 570 | assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable"); |
aoqi@0 | 571 | return true; |
aoqi@0 | 572 | |
aoqi@0 | 573 | default: |
aoqi@0 | 574 | return false; |
aoqi@0 | 575 | } |
aoqi@0 | 576 | } |
aoqi@0 | 577 | |
aoqi@0 | 578 | |
aoqi@0 | 579 | // See if the thread is running inside a lazy critical native and |
aoqi@0 | 580 | // update the thread critical count if so. Also set a suspend flag to |
aoqi@0 | 581 | // cause the native wrapper to return into the JVM to do the unlock |
aoqi@0 | 582 | // once the native finishes. |
aoqi@0 | 583 | void SafepointSynchronize::check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state) { |
aoqi@0 | 584 | if (state == _thread_in_native && |
aoqi@0 | 585 | thread->has_last_Java_frame() && |
aoqi@0 | 586 | thread->frame_anchor()->walkable()) { |
aoqi@0 | 587 | // This thread might be in a critical native nmethod so look at |
aoqi@0 | 588 | // the top of the stack and increment the critical count if it |
aoqi@0 | 589 | // is. |
aoqi@0 | 590 | frame wrapper_frame = thread->last_frame(); |
aoqi@0 | 591 | CodeBlob* stub_cb = wrapper_frame.cb(); |
aoqi@0 | 592 | if (stub_cb != NULL && |
aoqi@0 | 593 | stub_cb->is_nmethod() && |
aoqi@0 | 594 | stub_cb->as_nmethod_or_null()->is_lazy_critical_native()) { |
aoqi@0 | 595 | // A thread could potentially be in a critical native across |
aoqi@0 | 596 | // more than one safepoint, so only update the critical state on |
aoqi@0 | 597 | // the first one. When it returns it will perform the unlock. |
aoqi@0 | 598 | if (!thread->do_critical_native_unlock()) { |
aoqi@0 | 599 | #ifdef ASSERT |
aoqi@0 | 600 | if (!thread->in_critical()) { |
aoqi@0 | 601 | GC_locker::increment_debug_jni_lock_count(); |
aoqi@0 | 602 | } |
aoqi@0 | 603 | #endif |
aoqi@0 | 604 | thread->enter_critical(); |
aoqi@0 | 605 | // Make sure the native wrapper calls back on return to |
aoqi@0 | 606 | // perform the needed critical unlock. |
aoqi@0 | 607 | thread->set_critical_native_unlock(); |
aoqi@0 | 608 | } |
aoqi@0 | 609 | } |
aoqi@0 | 610 | } |
aoqi@0 | 611 | } |
aoqi@0 | 612 | |
aoqi@0 | 613 | |
aoqi@0 | 614 | |
aoqi@0 | 615 | // ------------------------------------------------------------------------------------------------------- |
aoqi@0 | 616 | // Implementation of Safepoint callback point |
aoqi@0 | 617 | |
aoqi@0 | 618 | void SafepointSynchronize::block(JavaThread *thread) { |
aoqi@0 | 619 | assert(thread != NULL, "thread must be set"); |
aoqi@0 | 620 | assert(thread->is_Java_thread(), "not a Java thread"); |
aoqi@0 | 621 | |
aoqi@0 | 622 | // Threads shouldn't block if they are in the middle of printing, but... |
aoqi@0 | 623 | ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id()); |
aoqi@0 | 624 | |
aoqi@0 | 625 | // Only bail from the block() call if the thread is gone from the |
aoqi@0 | 626 | // thread list; starting to exit should still block. |
aoqi@0 | 627 | if (thread->is_terminated()) { |
aoqi@0 | 628 | // block current thread if we come here from native code when VM is gone |
aoqi@0 | 629 | thread->block_if_vm_exited(); |
aoqi@0 | 630 | |
aoqi@0 | 631 | // otherwise do nothing |
aoqi@0 | 632 | return; |
aoqi@0 | 633 | } |
aoqi@0 | 634 | |
aoqi@0 | 635 | JavaThreadState state = thread->thread_state(); |
aoqi@0 | 636 | thread->frame_anchor()->make_walkable(thread); |
aoqi@0 | 637 | |
aoqi@0 | 638 | // Check that we have a valid thread_state at this point |
aoqi@0 | 639 | switch(state) { |
aoqi@0 | 640 | case _thread_in_vm_trans: |
aoqi@0 | 641 | case _thread_in_Java: // From compiled code |
aoqi@0 | 642 | |
aoqi@0 | 643 | // We are highly likely to block on the Safepoint_lock. In order to avoid blocking in this case, |
aoqi@0 | 644 | // we pretend we are still in the VM. |
aoqi@0 | 645 | thread->set_thread_state(_thread_in_vm); |
aoqi@0 | 646 | |
aoqi@0 | 647 | if (is_synchronizing()) { |
aoqi@0 | 648 | Atomic::inc (&TryingToBlock) ; |
aoqi@0 | 649 | } |
aoqi@0 | 650 | |
aoqi@0 | 651 | // We will always be holding the Safepoint_lock when we are examine the state |
aoqi@0 | 652 | // of a thread. Hence, the instructions between the Safepoint_lock->lock() and |
aoqi@0 | 653 | // Safepoint_lock->unlock() are happening atomic with regards to the safepoint code |
aoqi@0 | 654 | Safepoint_lock->lock_without_safepoint_check(); |
aoqi@0 | 655 | if (is_synchronizing()) { |
aoqi@0 | 656 | // Decrement the number of threads to wait for and signal vm thread |
aoqi@0 | 657 | assert(_waiting_to_block > 0, "sanity check"); |
aoqi@0 | 658 | _waiting_to_block--; |
aoqi@0 | 659 | thread->safepoint_state()->set_has_called_back(true); |
aoqi@0 | 660 | |
aoqi@0 | 661 | DEBUG_ONLY(thread->set_visited_for_critical_count(true)); |
aoqi@0 | 662 | if (thread->in_critical()) { |
aoqi@0 | 663 | // Notice that this thread is in a critical section |
aoqi@0 | 664 | increment_jni_active_count(); |
aoqi@0 | 665 | } |
aoqi@0 | 666 | |
aoqi@0 | 667 | // Consider (_waiting_to_block < 2) to pipeline the wakeup of the VM thread |
aoqi@0 | 668 | if (_waiting_to_block == 0) { |
aoqi@0 | 669 | Safepoint_lock->notify_all(); |
aoqi@0 | 670 | } |
aoqi@0 | 671 | } |
aoqi@0 | 672 | |
aoqi@0 | 673 | // We transition the thread to state _thread_blocked here, but |
aoqi@0 | 674 | // we can't do our usual check for external suspension and then |
aoqi@0 | 675 | // self-suspend after the lock_without_safepoint_check() call |
aoqi@0 | 676 | // below because we are often called during transitions while |
aoqi@0 | 677 | // we hold different locks. That would leave us suspended while |
aoqi@0 | 678 | // holding a resource which results in deadlocks. |
aoqi@0 | 679 | thread->set_thread_state(_thread_blocked); |
aoqi@0 | 680 | Safepoint_lock->unlock(); |
aoqi@0 | 681 | |
aoqi@0 | 682 | // We now try to acquire the threads lock. Since this lock is hold by the VM thread during |
aoqi@0 | 683 | // the entire safepoint, the threads will all line up here during the safepoint. |
aoqi@0 | 684 | Threads_lock->lock_without_safepoint_check(); |
aoqi@0 | 685 | // restore original state. This is important if the thread comes from compiled code, so it |
aoqi@0 | 686 | // will continue to execute with the _thread_in_Java state. |
aoqi@0 | 687 | thread->set_thread_state(state); |
aoqi@0 | 688 | Threads_lock->unlock(); |
aoqi@0 | 689 | break; |
aoqi@0 | 690 | |
aoqi@0 | 691 | case _thread_in_native_trans: |
aoqi@0 | 692 | case _thread_blocked_trans: |
aoqi@0 | 693 | case _thread_new_trans: |
aoqi@0 | 694 | if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back) { |
aoqi@0 | 695 | thread->print_thread_state(); |
aoqi@0 | 696 | fatal("Deadlock in safepoint code. " |
aoqi@0 | 697 | "Should have called back to the VM before blocking."); |
aoqi@0 | 698 | } |
aoqi@0 | 699 | |
aoqi@0 | 700 | // We transition the thread to state _thread_blocked here, but |
aoqi@0 | 701 | // we can't do our usual check for external suspension and then |
aoqi@0 | 702 | // self-suspend after the lock_without_safepoint_check() call |
aoqi@0 | 703 | // below because we are often called during transitions while |
aoqi@0 | 704 | // we hold different locks. That would leave us suspended while |
aoqi@0 | 705 | // holding a resource which results in deadlocks. |
aoqi@0 | 706 | thread->set_thread_state(_thread_blocked); |
aoqi@0 | 707 | |
aoqi@0 | 708 | // It is not safe to suspend a thread if we discover it is in _thread_in_native_trans. Hence, |
aoqi@0 | 709 | // the safepoint code might still be waiting for it to block. We need to change the state here, |
aoqi@0 | 710 | // so it can see that it is at a safepoint. |
aoqi@0 | 711 | |
aoqi@0 | 712 | // Block until the safepoint operation is completed. |
aoqi@0 | 713 | Threads_lock->lock_without_safepoint_check(); |
aoqi@0 | 714 | |
aoqi@0 | 715 | // Restore state |
aoqi@0 | 716 | thread->set_thread_state(state); |
aoqi@0 | 717 | |
aoqi@0 | 718 | Threads_lock->unlock(); |
aoqi@0 | 719 | break; |
aoqi@0 | 720 | |
aoqi@0 | 721 | default: |
aoqi@0 | 722 | fatal(err_msg("Illegal threadstate encountered: %d", state)); |
aoqi@0 | 723 | } |
aoqi@0 | 724 | |
aoqi@0 | 725 | // Check for pending. async. exceptions or suspends - except if the |
aoqi@0 | 726 | // thread was blocked inside the VM. has_special_runtime_exit_condition() |
aoqi@0 | 727 | // is called last since it grabs a lock and we only want to do that when |
aoqi@0 | 728 | // we must. |
aoqi@0 | 729 | // |
aoqi@0 | 730 | // Note: we never deliver an async exception at a polling point as the |
aoqi@0 | 731 | // compiler may not have an exception handler for it. The polling |
aoqi@0 | 732 | // code will notice the async and deoptimize and the exception will |
aoqi@0 | 733 | // be delivered. (Polling at a return point is ok though). Sure is |
aoqi@0 | 734 | // a lot of bother for a deprecated feature... |
aoqi@0 | 735 | // |
aoqi@0 | 736 | // We don't deliver an async exception if the thread state is |
aoqi@0 | 737 | // _thread_in_native_trans so JNI functions won't be called with |
aoqi@0 | 738 | // a surprising pending exception. If the thread state is going back to java, |
aoqi@0 | 739 | // async exception is checked in check_special_condition_for_native_trans(). |
aoqi@0 | 740 | |
aoqi@0 | 741 | if (state != _thread_blocked_trans && |
aoqi@0 | 742 | state != _thread_in_vm_trans && |
aoqi@0 | 743 | thread->has_special_runtime_exit_condition()) { |
aoqi@0 | 744 | thread->handle_special_runtime_exit_condition( |
aoqi@0 | 745 | !thread->is_at_poll_safepoint() && (state != _thread_in_native_trans)); |
aoqi@0 | 746 | } |
aoqi@0 | 747 | } |
aoqi@0 | 748 | |
aoqi@0 | 749 | // ------------------------------------------------------------------------------------------------------ |
aoqi@0 | 750 | // Exception handlers |
aoqi@0 | 751 | |
aoqi@0 | 752 | #ifndef PRODUCT |
aoqi@0 | 753 | |
aoqi@0 | 754 | #ifdef SPARC |
aoqi@0 | 755 | |
aoqi@0 | 756 | #ifdef _LP64 |
aoqi@0 | 757 | #define PTR_PAD "" |
aoqi@0 | 758 | #else |
aoqi@0 | 759 | #define PTR_PAD " " |
aoqi@0 | 760 | #endif |
aoqi@0 | 761 | |
aoqi@0 | 762 | static void print_ptrs(intptr_t oldptr, intptr_t newptr, bool wasoop) { |
aoqi@0 | 763 | bool is_oop = newptr ? (cast_to_oop(newptr))->is_oop() : false; |
aoqi@0 | 764 | tty->print_cr(PTR_FORMAT PTR_PAD " %s %c " PTR_FORMAT PTR_PAD " %s %s", |
aoqi@0 | 765 | oldptr, wasoop?"oop":" ", oldptr == newptr ? ' ' : '!', |
aoqi@0 | 766 | newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" ")); |
aoqi@0 | 767 | } |
aoqi@0 | 768 | |
aoqi@0 | 769 | static void print_longs(jlong oldptr, jlong newptr, bool wasoop) { |
aoqi@0 | 770 | bool is_oop = newptr ? (cast_to_oop(newptr))->is_oop() : false; |
aoqi@0 | 771 | tty->print_cr(PTR64_FORMAT " %s %c " PTR64_FORMAT " %s %s", |
aoqi@0 | 772 | oldptr, wasoop?"oop":" ", oldptr == newptr ? ' ' : '!', |
aoqi@0 | 773 | newptr, is_oop?"oop":" ", (wasoop && !is_oop) ? "STALE" : ((wasoop==false&&is_oop==false&&oldptr !=newptr)?"STOMP":" ")); |
aoqi@0 | 774 | } |
aoqi@0 | 775 | |
aoqi@0 | 776 | static void print_me(intptr_t *new_sp, intptr_t *old_sp, bool *was_oops) { |
aoqi@0 | 777 | #ifdef _LP64 |
aoqi@0 | 778 | tty->print_cr("--------+------address-----+------before-----------+-------after----------+"); |
aoqi@0 | 779 | const int incr = 1; // Increment to skip a long, in units of intptr_t |
aoqi@0 | 780 | #else |
aoqi@0 | 781 | tty->print_cr("--------+--address-+------before-----------+-------after----------+"); |
aoqi@0 | 782 | const int incr = 2; // Increment to skip a long, in units of intptr_t |
aoqi@0 | 783 | #endif |
aoqi@0 | 784 | tty->print_cr("---SP---|"); |
aoqi@0 | 785 | for( int i=0; i<16; i++ ) { |
aoqi@0 | 786 | tty->print("blob %c%d |"PTR_FORMAT" ","LO"[i>>3],i&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); } |
aoqi@0 | 787 | tty->print_cr("--------|"); |
aoqi@0 | 788 | for( int i1=0; i1<frame::memory_parameter_word_sp_offset-16; i1++ ) { |
aoqi@0 | 789 | tty->print("argv pad|"PTR_FORMAT" ",new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); } |
aoqi@0 | 790 | tty->print(" pad|"PTR_FORMAT" ",new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); |
aoqi@0 | 791 | tty->print_cr("--------|"); |
aoqi@0 | 792 | 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; |
aoqi@0 | 793 | 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; |
aoqi@0 | 794 | 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; |
aoqi@0 | 795 | 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; |
aoqi@0 | 796 | tty->print_cr(" FSR |"PTR_FORMAT" "PTR64_FORMAT" "PTR64_FORMAT,new_sp,*(jlong*)old_sp,*(jlong*)new_sp); |
aoqi@0 | 797 | old_sp += incr; new_sp += incr; was_oops += incr; |
aoqi@0 | 798 | // Skip the floats |
aoqi@0 | 799 | tty->print_cr("--Float-|"PTR_FORMAT,new_sp); |
aoqi@0 | 800 | tty->print_cr("---FP---|"); |
aoqi@0 | 801 | old_sp += incr*32; new_sp += incr*32; was_oops += incr*32; |
aoqi@0 | 802 | for( int i2=0; i2<16; i2++ ) { |
aoqi@0 | 803 | tty->print("call %c%d |"PTR_FORMAT" ","LI"[i2>>3],i2&7,new_sp); print_ptrs(*old_sp++,*new_sp++,*was_oops++); } |
aoqi@0 | 804 | tty->cr(); |
aoqi@0 | 805 | } |
aoqi@0 | 806 | #endif // SPARC |
aoqi@0 | 807 | #endif // PRODUCT |
aoqi@0 | 808 | |
aoqi@0 | 809 | |
aoqi@0 | 810 | void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) { |
aoqi@0 | 811 | assert(thread->is_Java_thread(), "polling reference encountered by VM thread"); |
aoqi@0 | 812 | assert(thread->thread_state() == _thread_in_Java, "should come from Java code"); |
aoqi@0 | 813 | assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization"); |
aoqi@0 | 814 | |
aoqi@0 | 815 | // Uncomment this to get some serious before/after printing of the |
aoqi@0 | 816 | // Sparc safepoint-blob frame structure. |
aoqi@0 | 817 | /* |
aoqi@0 | 818 | intptr_t* sp = thread->last_Java_sp(); |
aoqi@0 | 819 | intptr_t stack_copy[150]; |
aoqi@0 | 820 | for( int i=0; i<150; i++ ) stack_copy[i] = sp[i]; |
aoqi@0 | 821 | bool was_oops[150]; |
aoqi@0 | 822 | for( int i=0; i<150; i++ ) |
aoqi@0 | 823 | was_oops[i] = stack_copy[i] ? ((oop)stack_copy[i])->is_oop() : false; |
aoqi@0 | 824 | */ |
aoqi@0 | 825 | |
aoqi@0 | 826 | if (ShowSafepointMsgs) { |
aoqi@0 | 827 | tty->print("handle_polling_page_exception: "); |
aoqi@0 | 828 | } |
aoqi@0 | 829 | |
aoqi@0 | 830 | if (PrintSafepointStatistics) { |
aoqi@0 | 831 | inc_page_trap_count(); |
aoqi@0 | 832 | } |
aoqi@0 | 833 | |
aoqi@0 | 834 | ThreadSafepointState* state = thread->safepoint_state(); |
aoqi@0 | 835 | |
aoqi@0 | 836 | state->handle_polling_page_exception(); |
aoqi@0 | 837 | // print_me(sp,stack_copy,was_oops); |
aoqi@0 | 838 | } |
aoqi@0 | 839 | |
aoqi@0 | 840 | |
aoqi@0 | 841 | void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) { |
aoqi@0 | 842 | if (!timeout_error_printed) { |
aoqi@0 | 843 | timeout_error_printed = true; |
aoqi@0 | 844 | // Print out the thread infor which didn't reach the safepoint for debugging |
aoqi@0 | 845 | // purposes (useful when there are lots of threads in the debugger). |
aoqi@0 | 846 | tty->cr(); |
aoqi@0 | 847 | tty->print_cr("# SafepointSynchronize::begin: Timeout detected:"); |
aoqi@0 | 848 | if (reason == _spinning_timeout) { |
aoqi@0 | 849 | tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint."); |
aoqi@0 | 850 | } else if (reason == _blocking_timeout) { |
aoqi@0 | 851 | tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop."); |
aoqi@0 | 852 | } |
aoqi@0 | 853 | |
aoqi@0 | 854 | tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:"); |
aoqi@0 | 855 | ThreadSafepointState *cur_state; |
aoqi@0 | 856 | ResourceMark rm; |
aoqi@0 | 857 | for(JavaThread *cur_thread = Threads::first(); cur_thread; |
aoqi@0 | 858 | cur_thread = cur_thread->next()) { |
aoqi@0 | 859 | cur_state = cur_thread->safepoint_state(); |
aoqi@0 | 860 | |
aoqi@0 | 861 | if (cur_thread->thread_state() != _thread_blocked && |
aoqi@0 | 862 | ((reason == _spinning_timeout && cur_state->is_running()) || |
aoqi@0 | 863 | (reason == _blocking_timeout && !cur_state->has_called_back()))) { |
aoqi@0 | 864 | tty->print("# "); |
aoqi@0 | 865 | cur_thread->print(); |
aoqi@0 | 866 | tty->cr(); |
aoqi@0 | 867 | } |
aoqi@0 | 868 | } |
aoqi@0 | 869 | tty->print_cr("# SafepointSynchronize::begin: (End of list)"); |
aoqi@0 | 870 | } |
aoqi@0 | 871 | |
aoqi@0 | 872 | // To debug the long safepoint, specify both DieOnSafepointTimeout & |
aoqi@0 | 873 | // ShowMessageBoxOnError. |
aoqi@0 | 874 | if (DieOnSafepointTimeout) { |
aoqi@0 | 875 | char msg[1024]; |
aoqi@0 | 876 | VM_Operation *op = VMThread::vm_operation(); |
aoqi@0 | 877 | sprintf(msg, "Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.", |
aoqi@0 | 878 | SafepointTimeoutDelay, |
aoqi@0 | 879 | op != NULL ? op->name() : "no vm operation"); |
aoqi@0 | 880 | fatal(msg); |
aoqi@0 | 881 | } |
aoqi@0 | 882 | } |
aoqi@0 | 883 | |
aoqi@0 | 884 | |
aoqi@0 | 885 | // ------------------------------------------------------------------------------------------------------- |
aoqi@0 | 886 | // Implementation of ThreadSafepointState |
aoqi@0 | 887 | |
aoqi@0 | 888 | ThreadSafepointState::ThreadSafepointState(JavaThread *thread) { |
aoqi@0 | 889 | _thread = thread; |
aoqi@0 | 890 | _type = _running; |
aoqi@0 | 891 | _has_called_back = false; |
aoqi@0 | 892 | _at_poll_safepoint = false; |
aoqi@0 | 893 | } |
aoqi@0 | 894 | |
aoqi@0 | 895 | void ThreadSafepointState::create(JavaThread *thread) { |
aoqi@0 | 896 | ThreadSafepointState *state = new ThreadSafepointState(thread); |
aoqi@0 | 897 | thread->set_safepoint_state(state); |
aoqi@0 | 898 | } |
aoqi@0 | 899 | |
aoqi@0 | 900 | void ThreadSafepointState::destroy(JavaThread *thread) { |
aoqi@0 | 901 | if (thread->safepoint_state()) { |
aoqi@0 | 902 | delete(thread->safepoint_state()); |
aoqi@0 | 903 | thread->set_safepoint_state(NULL); |
aoqi@0 | 904 | } |
aoqi@0 | 905 | } |
aoqi@0 | 906 | |
aoqi@0 | 907 | void ThreadSafepointState::examine_state_of_thread() { |
aoqi@0 | 908 | assert(is_running(), "better be running or just have hit safepoint poll"); |
aoqi@0 | 909 | |
aoqi@0 | 910 | JavaThreadState state = _thread->thread_state(); |
aoqi@0 | 911 | |
aoqi@0 | 912 | // Save the state at the start of safepoint processing. |
aoqi@0 | 913 | _orig_thread_state = state; |
aoqi@0 | 914 | |
aoqi@0 | 915 | // Check for a thread that is suspended. Note that thread resume tries |
aoqi@0 | 916 | // to grab the Threads_lock which we own here, so a thread cannot be |
aoqi@0 | 917 | // resumed during safepoint synchronization. |
aoqi@0 | 918 | |
aoqi@0 | 919 | // We check to see if this thread is suspended without locking to |
aoqi@0 | 920 | // avoid deadlocking with a third thread that is waiting for this |
aoqi@0 | 921 | // thread to be suspended. The third thread can notice the safepoint |
aoqi@0 | 922 | // that we're trying to start at the beginning of its SR_lock->wait() |
aoqi@0 | 923 | // call. If that happens, then the third thread will block on the |
aoqi@0 | 924 | // safepoint while still holding the underlying SR_lock. We won't be |
aoqi@0 | 925 | // able to get the SR_lock and we'll deadlock. |
aoqi@0 | 926 | // |
aoqi@0 | 927 | // We don't need to grab the SR_lock here for two reasons: |
aoqi@0 | 928 | // 1) The suspend flags are both volatile and are set with an |
aoqi@0 | 929 | // Atomic::cmpxchg() call so we should see the suspended |
aoqi@0 | 930 | // state right away. |
aoqi@0 | 931 | // 2) We're being called from the safepoint polling loop; if |
aoqi@0 | 932 | // we don't see the suspended state on this iteration, then |
aoqi@0 | 933 | // we'll come around again. |
aoqi@0 | 934 | // |
aoqi@0 | 935 | bool is_suspended = _thread->is_ext_suspended(); |
aoqi@0 | 936 | if (is_suspended) { |
aoqi@0 | 937 | roll_forward(_at_safepoint); |
aoqi@0 | 938 | return; |
aoqi@0 | 939 | } |
aoqi@0 | 940 | |
aoqi@0 | 941 | // Some JavaThread states have an initial safepoint state of |
aoqi@0 | 942 | // running, but are actually at a safepoint. We will happily |
aoqi@0 | 943 | // agree and update the safepoint state here. |
aoqi@0 | 944 | if (SafepointSynchronize::safepoint_safe(_thread, state)) { |
aoqi@0 | 945 | SafepointSynchronize::check_for_lazy_critical_native(_thread, state); |
aoqi@0 | 946 | roll_forward(_at_safepoint); |
aoqi@0 | 947 | return; |
aoqi@0 | 948 | } |
aoqi@0 | 949 | |
aoqi@0 | 950 | if (state == _thread_in_vm) { |
aoqi@0 | 951 | roll_forward(_call_back); |
aoqi@0 | 952 | return; |
aoqi@0 | 953 | } |
aoqi@0 | 954 | |
aoqi@0 | 955 | // All other thread states will continue to run until they |
aoqi@0 | 956 | // transition and self-block in state _blocked |
aoqi@0 | 957 | // Safepoint polling in compiled code causes the Java threads to do the same. |
aoqi@0 | 958 | // Note: new threads may require a malloc so they must be allowed to finish |
aoqi@0 | 959 | |
aoqi@0 | 960 | assert(is_running(), "examine_state_of_thread on non-running thread"); |
aoqi@0 | 961 | return; |
aoqi@0 | 962 | } |
aoqi@0 | 963 | |
aoqi@0 | 964 | // Returns true is thread could not be rolled forward at present position. |
aoqi@0 | 965 | void ThreadSafepointState::roll_forward(suspend_type type) { |
aoqi@0 | 966 | _type = type; |
aoqi@0 | 967 | |
aoqi@0 | 968 | switch(_type) { |
aoqi@0 | 969 | case _at_safepoint: |
aoqi@0 | 970 | SafepointSynchronize::signal_thread_at_safepoint(); |
aoqi@0 | 971 | DEBUG_ONLY(_thread->set_visited_for_critical_count(true)); |
aoqi@0 | 972 | if (_thread->in_critical()) { |
aoqi@0 | 973 | // Notice that this thread is in a critical section |
aoqi@0 | 974 | SafepointSynchronize::increment_jni_active_count(); |
aoqi@0 | 975 | } |
aoqi@0 | 976 | break; |
aoqi@0 | 977 | |
aoqi@0 | 978 | case _call_back: |
aoqi@0 | 979 | set_has_called_back(false); |
aoqi@0 | 980 | break; |
aoqi@0 | 981 | |
aoqi@0 | 982 | case _running: |
aoqi@0 | 983 | default: |
aoqi@0 | 984 | ShouldNotReachHere(); |
aoqi@0 | 985 | } |
aoqi@0 | 986 | } |
aoqi@0 | 987 | |
aoqi@0 | 988 | void ThreadSafepointState::restart() { |
aoqi@0 | 989 | switch(type()) { |
aoqi@0 | 990 | case _at_safepoint: |
aoqi@0 | 991 | case _call_back: |
aoqi@0 | 992 | break; |
aoqi@0 | 993 | |
aoqi@0 | 994 | case _running: |
aoqi@0 | 995 | default: |
aoqi@0 | 996 | tty->print_cr("restart thread "INTPTR_FORMAT" with state %d", |
aoqi@0 | 997 | _thread, _type); |
aoqi@0 | 998 | _thread->print(); |
aoqi@0 | 999 | ShouldNotReachHere(); |
aoqi@0 | 1000 | } |
aoqi@0 | 1001 | _type = _running; |
aoqi@0 | 1002 | set_has_called_back(false); |
aoqi@0 | 1003 | } |
aoqi@0 | 1004 | |
aoqi@0 | 1005 | |
aoqi@0 | 1006 | void ThreadSafepointState::print_on(outputStream *st) const { |
aoqi@0 | 1007 | const char *s; |
aoqi@0 | 1008 | |
aoqi@0 | 1009 | switch(_type) { |
aoqi@0 | 1010 | case _running : s = "_running"; break; |
aoqi@0 | 1011 | case _at_safepoint : s = "_at_safepoint"; break; |
aoqi@0 | 1012 | case _call_back : s = "_call_back"; break; |
aoqi@0 | 1013 | default: |
aoqi@0 | 1014 | ShouldNotReachHere(); |
aoqi@0 | 1015 | } |
aoqi@0 | 1016 | |
aoqi@0 | 1017 | st->print_cr("Thread: " INTPTR_FORMAT |
aoqi@0 | 1018 | " [0x%2x] State: %s _has_called_back %d _at_poll_safepoint %d", |
aoqi@0 | 1019 | _thread, _thread->osthread()->thread_id(), s, _has_called_back, |
aoqi@0 | 1020 | _at_poll_safepoint); |
aoqi@0 | 1021 | |
aoqi@0 | 1022 | _thread->print_thread_state_on(st); |
aoqi@0 | 1023 | } |
aoqi@0 | 1024 | |
aoqi@0 | 1025 | |
aoqi@0 | 1026 | // --------------------------------------------------------------------------------------------------------------------- |
aoqi@0 | 1027 | |
aoqi@0 | 1028 | // Block the thread at the safepoint poll or poll return. |
aoqi@0 | 1029 | void ThreadSafepointState::handle_polling_page_exception() { |
aoqi@0 | 1030 | |
aoqi@0 | 1031 | // Check state. block() will set thread state to thread_in_vm which will |
aoqi@0 | 1032 | // cause the safepoint state _type to become _call_back. |
aoqi@0 | 1033 | assert(type() == ThreadSafepointState::_running, |
aoqi@0 | 1034 | "polling page exception on thread not running state"); |
aoqi@0 | 1035 | |
aoqi@0 | 1036 | // Step 1: Find the nmethod from the return address |
aoqi@0 | 1037 | if (ShowSafepointMsgs && Verbose) { |
aoqi@0 | 1038 | tty->print_cr("Polling page exception at " INTPTR_FORMAT, thread()->saved_exception_pc()); |
aoqi@0 | 1039 | } |
aoqi@0 | 1040 | address real_return_addr = thread()->saved_exception_pc(); |
aoqi@0 | 1041 | |
aoqi@0 | 1042 | CodeBlob *cb = CodeCache::find_blob(real_return_addr); |
aoqi@0 | 1043 | assert(cb != NULL && cb->is_nmethod(), "return address should be in nmethod"); |
aoqi@0 | 1044 | nmethod* nm = (nmethod*)cb; |
aoqi@0 | 1045 | |
aoqi@0 | 1046 | // Find frame of caller |
aoqi@0 | 1047 | frame stub_fr = thread()->last_frame(); |
aoqi@0 | 1048 | CodeBlob* stub_cb = stub_fr.cb(); |
aoqi@0 | 1049 | assert(stub_cb->is_safepoint_stub(), "must be a safepoint stub"); |
aoqi@0 | 1050 | RegisterMap map(thread(), true); |
aoqi@0 | 1051 | frame caller_fr = stub_fr.sender(&map); |
aoqi@0 | 1052 | |
aoqi@0 | 1053 | // Should only be poll_return or poll |
aoqi@0 | 1054 | assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" ); |
aoqi@0 | 1055 | |
aoqi@0 | 1056 | // This is a poll immediately before a return. The exception handling code |
aoqi@0 | 1057 | // has already had the effect of causing the return to occur, so the execution |
aoqi@0 | 1058 | // will continue immediately after the call. In addition, the oopmap at the |
aoqi@0 | 1059 | // return point does not mark the return value as an oop (if it is), so |
aoqi@0 | 1060 | // it needs a handle here to be updated. |
aoqi@0 | 1061 | if( nm->is_at_poll_return(real_return_addr) ) { |
aoqi@0 | 1062 | // See if return type is an oop. |
aoqi@0 | 1063 | bool return_oop = nm->method()->is_returning_oop(); |
aoqi@0 | 1064 | Handle return_value; |
aoqi@0 | 1065 | if (return_oop) { |
aoqi@0 | 1066 | // The oop result has been saved on the stack together with all |
aoqi@0 | 1067 | // the other registers. In order to preserve it over GCs we need |
aoqi@0 | 1068 | // to keep it in a handle. |
aoqi@0 | 1069 | oop result = caller_fr.saved_oop_result(&map); |
aoqi@0 | 1070 | assert(result == NULL || result->is_oop(), "must be oop"); |
aoqi@0 | 1071 | return_value = Handle(thread(), result); |
aoqi@0 | 1072 | assert(Universe::heap()->is_in_or_null(result), "must be heap pointer"); |
aoqi@0 | 1073 | } |
aoqi@0 | 1074 | |
aoqi@0 | 1075 | // Block the thread |
aoqi@0 | 1076 | SafepointSynchronize::block(thread()); |
aoqi@0 | 1077 | |
aoqi@0 | 1078 | // restore oop result, if any |
aoqi@0 | 1079 | if (return_oop) { |
aoqi@0 | 1080 | caller_fr.set_saved_oop_result(&map, return_value()); |
aoqi@0 | 1081 | } |
aoqi@0 | 1082 | } |
aoqi@0 | 1083 | |
aoqi@0 | 1084 | // This is a safepoint poll. Verify the return address and block. |
aoqi@0 | 1085 | else { |
aoqi@0 | 1086 | set_at_poll_safepoint(true); |
aoqi@0 | 1087 | |
aoqi@0 | 1088 | // verify the blob built the "return address" correctly |
aoqi@0 | 1089 | assert(real_return_addr == caller_fr.pc(), "must match"); |
aoqi@0 | 1090 | |
aoqi@0 | 1091 | // Block the thread |
aoqi@0 | 1092 | SafepointSynchronize::block(thread()); |
aoqi@0 | 1093 | set_at_poll_safepoint(false); |
aoqi@0 | 1094 | |
aoqi@0 | 1095 | // If we have a pending async exception deoptimize the frame |
aoqi@0 | 1096 | // as otherwise we may never deliver it. |
aoqi@0 | 1097 | if (thread()->has_async_condition()) { |
aoqi@0 | 1098 | ThreadInVMfromJavaNoAsyncException __tiv(thread()); |
aoqi@0 | 1099 | Deoptimization::deoptimize_frame(thread(), caller_fr.id()); |
aoqi@0 | 1100 | } |
aoqi@0 | 1101 | |
aoqi@0 | 1102 | // If an exception has been installed we must check for a pending deoptimization |
aoqi@0 | 1103 | // Deoptimize frame if exception has been thrown. |
aoqi@0 | 1104 | |
aoqi@0 | 1105 | if (thread()->has_pending_exception() ) { |
aoqi@0 | 1106 | RegisterMap map(thread(), true); |
aoqi@0 | 1107 | frame caller_fr = stub_fr.sender(&map); |
aoqi@0 | 1108 | if (caller_fr.is_deoptimized_frame()) { |
aoqi@0 | 1109 | // The exception patch will destroy registers that are still |
aoqi@0 | 1110 | // live and will be needed during deoptimization. Defer the |
aoqi@0 | 1111 | // Async exception should have defered the exception until the |
aoqi@0 | 1112 | // next safepoint which will be detected when we get into |
aoqi@0 | 1113 | // the interpreter so if we have an exception now things |
aoqi@0 | 1114 | // are messed up. |
aoqi@0 | 1115 | |
aoqi@0 | 1116 | fatal("Exception installed and deoptimization is pending"); |
aoqi@0 | 1117 | } |
aoqi@0 | 1118 | } |
aoqi@0 | 1119 | } |
aoqi@0 | 1120 | } |
aoqi@0 | 1121 | |
aoqi@0 | 1122 | |
aoqi@0 | 1123 | // |
aoqi@0 | 1124 | // Statistics & Instrumentations |
aoqi@0 | 1125 | // |
aoqi@0 | 1126 | SafepointSynchronize::SafepointStats* SafepointSynchronize::_safepoint_stats = NULL; |
aoqi@0 | 1127 | jlong SafepointSynchronize::_safepoint_begin_time = 0; |
aoqi@0 | 1128 | int SafepointSynchronize::_cur_stat_index = 0; |
aoqi@0 | 1129 | julong SafepointSynchronize::_safepoint_reasons[VM_Operation::VMOp_Terminating]; |
aoqi@0 | 1130 | julong SafepointSynchronize::_coalesced_vmop_count = 0; |
aoqi@0 | 1131 | jlong SafepointSynchronize::_max_sync_time = 0; |
aoqi@0 | 1132 | jlong SafepointSynchronize::_max_vmop_time = 0; |
aoqi@0 | 1133 | float SafepointSynchronize::_ts_of_current_safepoint = 0.0f; |
aoqi@0 | 1134 | |
aoqi@0 | 1135 | static jlong cleanup_end_time = 0; |
aoqi@0 | 1136 | static bool need_to_track_page_armed_status = false; |
aoqi@0 | 1137 | static bool init_done = false; |
aoqi@0 | 1138 | |
aoqi@0 | 1139 | // Helper method to print the header. |
aoqi@0 | 1140 | static void print_header() { |
aoqi@0 | 1141 | tty->print(" vmop " |
aoqi@0 | 1142 | "[threads: total initially_running wait_to_block] "); |
aoqi@0 | 1143 | tty->print("[time: spin block sync cleanup vmop] "); |
aoqi@0 | 1144 | |
aoqi@0 | 1145 | // no page armed status printed out if it is always armed. |
aoqi@0 | 1146 | if (need_to_track_page_armed_status) { |
aoqi@0 | 1147 | tty->print("page_armed "); |
aoqi@0 | 1148 | } |
aoqi@0 | 1149 | |
aoqi@0 | 1150 | tty->print_cr("page_trap_count"); |
aoqi@0 | 1151 | } |
aoqi@0 | 1152 | |
aoqi@0 | 1153 | void SafepointSynchronize::deferred_initialize_stat() { |
aoqi@0 | 1154 | if (init_done) return; |
aoqi@0 | 1155 | |
aoqi@0 | 1156 | if (PrintSafepointStatisticsCount <= 0) { |
aoqi@0 | 1157 | fatal("Wrong PrintSafepointStatisticsCount"); |
aoqi@0 | 1158 | } |
aoqi@0 | 1159 | |
aoqi@0 | 1160 | // If PrintSafepointStatisticsTimeout is specified, the statistics data will |
aoqi@0 | 1161 | // be printed right away, in which case, _safepoint_stats will regress to |
aoqi@0 | 1162 | // a single element array. Otherwise, it is a circular ring buffer with default |
aoqi@0 | 1163 | // size of PrintSafepointStatisticsCount. |
aoqi@0 | 1164 | int stats_array_size; |
aoqi@0 | 1165 | if (PrintSafepointStatisticsTimeout > 0) { |
aoqi@0 | 1166 | stats_array_size = 1; |
aoqi@0 | 1167 | PrintSafepointStatistics = true; |
aoqi@0 | 1168 | } else { |
aoqi@0 | 1169 | stats_array_size = PrintSafepointStatisticsCount; |
aoqi@0 | 1170 | } |
aoqi@0 | 1171 | _safepoint_stats = (SafepointStats*)os::malloc(stats_array_size |
aoqi@0 | 1172 | * sizeof(SafepointStats), mtInternal); |
aoqi@0 | 1173 | guarantee(_safepoint_stats != NULL, |
aoqi@0 | 1174 | "not enough memory for safepoint instrumentation data"); |
aoqi@0 | 1175 | |
aoqi@0 | 1176 | if (UseCompilerSafepoints && DeferPollingPageLoopCount >= 0) { |
aoqi@0 | 1177 | need_to_track_page_armed_status = true; |
aoqi@0 | 1178 | } |
aoqi@0 | 1179 | init_done = true; |
aoqi@0 | 1180 | } |
aoqi@0 | 1181 | |
aoqi@0 | 1182 | void SafepointSynchronize::begin_statistics(int nof_threads, int nof_running) { |
aoqi@0 | 1183 | assert(init_done, "safepoint statistics array hasn't been initialized"); |
aoqi@0 | 1184 | SafepointStats *spstat = &_safepoint_stats[_cur_stat_index]; |
aoqi@0 | 1185 | |
aoqi@0 | 1186 | spstat->_time_stamp = _ts_of_current_safepoint; |
aoqi@0 | 1187 | |
aoqi@0 | 1188 | VM_Operation *op = VMThread::vm_operation(); |
aoqi@0 | 1189 | spstat->_vmop_type = (op != NULL ? op->type() : -1); |
aoqi@0 | 1190 | if (op != NULL) { |
aoqi@0 | 1191 | _safepoint_reasons[spstat->_vmop_type]++; |
aoqi@0 | 1192 | } |
aoqi@0 | 1193 | |
aoqi@0 | 1194 | spstat->_nof_total_threads = nof_threads; |
aoqi@0 | 1195 | spstat->_nof_initial_running_threads = nof_running; |
aoqi@0 | 1196 | spstat->_nof_threads_hit_page_trap = 0; |
aoqi@0 | 1197 | |
aoqi@0 | 1198 | // Records the start time of spinning. The real time spent on spinning |
aoqi@0 | 1199 | // will be adjusted when spin is done. Same trick is applied for time |
aoqi@0 | 1200 | // spent on waiting for threads to block. |
aoqi@0 | 1201 | if (nof_running != 0) { |
aoqi@0 | 1202 | spstat->_time_to_spin = os::javaTimeNanos(); |
aoqi@0 | 1203 | } else { |
aoqi@0 | 1204 | spstat->_time_to_spin = 0; |
aoqi@0 | 1205 | } |
aoqi@0 | 1206 | } |
aoqi@0 | 1207 | |
aoqi@0 | 1208 | void SafepointSynchronize::update_statistics_on_spin_end() { |
aoqi@0 | 1209 | SafepointStats *spstat = &_safepoint_stats[_cur_stat_index]; |
aoqi@0 | 1210 | |
aoqi@0 | 1211 | jlong cur_time = os::javaTimeNanos(); |
aoqi@0 | 1212 | |
aoqi@0 | 1213 | spstat->_nof_threads_wait_to_block = _waiting_to_block; |
aoqi@0 | 1214 | if (spstat->_nof_initial_running_threads != 0) { |
aoqi@0 | 1215 | spstat->_time_to_spin = cur_time - spstat->_time_to_spin; |
aoqi@0 | 1216 | } |
aoqi@0 | 1217 | |
aoqi@0 | 1218 | if (need_to_track_page_armed_status) { |
aoqi@0 | 1219 | spstat->_page_armed = (PageArmed == 1); |
aoqi@0 | 1220 | } |
aoqi@0 | 1221 | |
aoqi@0 | 1222 | // Records the start time of waiting for to block. Updated when block is done. |
aoqi@0 | 1223 | if (_waiting_to_block != 0) { |
aoqi@0 | 1224 | spstat->_time_to_wait_to_block = cur_time; |
aoqi@0 | 1225 | } else { |
aoqi@0 | 1226 | spstat->_time_to_wait_to_block = 0; |
aoqi@0 | 1227 | } |
aoqi@0 | 1228 | } |
aoqi@0 | 1229 | |
aoqi@0 | 1230 | void SafepointSynchronize::update_statistics_on_sync_end(jlong end_time) { |
aoqi@0 | 1231 | SafepointStats *spstat = &_safepoint_stats[_cur_stat_index]; |
aoqi@0 | 1232 | |
aoqi@0 | 1233 | if (spstat->_nof_threads_wait_to_block != 0) { |
aoqi@0 | 1234 | spstat->_time_to_wait_to_block = end_time - |
aoqi@0 | 1235 | spstat->_time_to_wait_to_block; |
aoqi@0 | 1236 | } |
aoqi@0 | 1237 | |
aoqi@0 | 1238 | // Records the end time of sync which will be used to calculate the total |
aoqi@0 | 1239 | // vm operation time. Again, the real time spending in syncing will be deducted |
aoqi@0 | 1240 | // from the start of the sync time later when end_statistics is called. |
aoqi@0 | 1241 | spstat->_time_to_sync = end_time - _safepoint_begin_time; |
aoqi@0 | 1242 | if (spstat->_time_to_sync > _max_sync_time) { |
aoqi@0 | 1243 | _max_sync_time = spstat->_time_to_sync; |
aoqi@0 | 1244 | } |
aoqi@0 | 1245 | |
aoqi@0 | 1246 | spstat->_time_to_do_cleanups = end_time; |
aoqi@0 | 1247 | } |
aoqi@0 | 1248 | |
aoqi@0 | 1249 | void SafepointSynchronize::update_statistics_on_cleanup_end(jlong end_time) { |
aoqi@0 | 1250 | SafepointStats *spstat = &_safepoint_stats[_cur_stat_index]; |
aoqi@0 | 1251 | |
aoqi@0 | 1252 | // Record how long spent in cleanup tasks. |
aoqi@0 | 1253 | spstat->_time_to_do_cleanups = end_time - spstat->_time_to_do_cleanups; |
aoqi@0 | 1254 | |
aoqi@0 | 1255 | cleanup_end_time = end_time; |
aoqi@0 | 1256 | } |
aoqi@0 | 1257 | |
aoqi@0 | 1258 | void SafepointSynchronize::end_statistics(jlong vmop_end_time) { |
aoqi@0 | 1259 | SafepointStats *spstat = &_safepoint_stats[_cur_stat_index]; |
aoqi@0 | 1260 | |
aoqi@0 | 1261 | // Update the vm operation time. |
aoqi@0 | 1262 | spstat->_time_to_exec_vmop = vmop_end_time - cleanup_end_time; |
aoqi@0 | 1263 | if (spstat->_time_to_exec_vmop > _max_vmop_time) { |
aoqi@0 | 1264 | _max_vmop_time = spstat->_time_to_exec_vmop; |
aoqi@0 | 1265 | } |
aoqi@0 | 1266 | // Only the sync time longer than the specified |
aoqi@0 | 1267 | // PrintSafepointStatisticsTimeout will be printed out right away. |
aoqi@0 | 1268 | // By default, it is -1 meaning all samples will be put into the list. |
aoqi@0 | 1269 | if ( PrintSafepointStatisticsTimeout > 0) { |
aoqi@0 | 1270 | if (spstat->_time_to_sync > PrintSafepointStatisticsTimeout * MICROUNITS) { |
aoqi@0 | 1271 | print_statistics(); |
aoqi@0 | 1272 | } |
aoqi@0 | 1273 | } else { |
aoqi@0 | 1274 | // The safepoint statistics will be printed out when the _safepoin_stats |
aoqi@0 | 1275 | // array fills up. |
aoqi@0 | 1276 | if (_cur_stat_index == PrintSafepointStatisticsCount - 1) { |
aoqi@0 | 1277 | print_statistics(); |
aoqi@0 | 1278 | _cur_stat_index = 0; |
aoqi@0 | 1279 | } else { |
aoqi@0 | 1280 | _cur_stat_index++; |
aoqi@0 | 1281 | } |
aoqi@0 | 1282 | } |
aoqi@0 | 1283 | } |
aoqi@0 | 1284 | |
aoqi@0 | 1285 | void SafepointSynchronize::print_statistics() { |
aoqi@0 | 1286 | SafepointStats* sstats = _safepoint_stats; |
aoqi@0 | 1287 | |
aoqi@0 | 1288 | for (int index = 0; index <= _cur_stat_index; index++) { |
aoqi@0 | 1289 | if (index % 30 == 0) { |
aoqi@0 | 1290 | print_header(); |
aoqi@0 | 1291 | } |
aoqi@0 | 1292 | sstats = &_safepoint_stats[index]; |
aoqi@0 | 1293 | tty->print("%.3f: ", sstats->_time_stamp); |
aoqi@0 | 1294 | tty->print("%-26s [" |
aoqi@0 | 1295 | INT32_FORMAT_W(8)INT32_FORMAT_W(11)INT32_FORMAT_W(15) |
aoqi@0 | 1296 | " ] ", |
aoqi@0 | 1297 | sstats->_vmop_type == -1 ? "no vm operation" : |
aoqi@0 | 1298 | VM_Operation::name(sstats->_vmop_type), |
aoqi@0 | 1299 | sstats->_nof_total_threads, |
aoqi@0 | 1300 | sstats->_nof_initial_running_threads, |
aoqi@0 | 1301 | sstats->_nof_threads_wait_to_block); |
aoqi@0 | 1302 | // "/ MICROUNITS " is to convert the unit from nanos to millis. |
aoqi@0 | 1303 | tty->print(" [" |
aoqi@0 | 1304 | INT64_FORMAT_W(6)INT64_FORMAT_W(6) |
aoqi@0 | 1305 | INT64_FORMAT_W(6)INT64_FORMAT_W(6) |
aoqi@0 | 1306 | INT64_FORMAT_W(6)" ] ", |
aoqi@0 | 1307 | sstats->_time_to_spin / MICROUNITS, |
aoqi@0 | 1308 | sstats->_time_to_wait_to_block / MICROUNITS, |
aoqi@0 | 1309 | sstats->_time_to_sync / MICROUNITS, |
aoqi@0 | 1310 | sstats->_time_to_do_cleanups / MICROUNITS, |
aoqi@0 | 1311 | sstats->_time_to_exec_vmop / MICROUNITS); |
aoqi@0 | 1312 | |
aoqi@0 | 1313 | if (need_to_track_page_armed_status) { |
aoqi@0 | 1314 | tty->print(INT32_FORMAT" ", sstats->_page_armed); |
aoqi@0 | 1315 | } |
aoqi@0 | 1316 | tty->print_cr(INT32_FORMAT" ", sstats->_nof_threads_hit_page_trap); |
aoqi@0 | 1317 | } |
aoqi@0 | 1318 | } |
aoqi@0 | 1319 | |
aoqi@0 | 1320 | // This method will be called when VM exits. It will first call |
aoqi@0 | 1321 | // print_statistics to print out the rest of the sampling. Then |
aoqi@0 | 1322 | // it tries to summarize the sampling. |
aoqi@0 | 1323 | void SafepointSynchronize::print_stat_on_exit() { |
aoqi@0 | 1324 | if (_safepoint_stats == NULL) return; |
aoqi@0 | 1325 | |
aoqi@0 | 1326 | SafepointStats *spstat = &_safepoint_stats[_cur_stat_index]; |
aoqi@0 | 1327 | |
aoqi@0 | 1328 | // During VM exit, end_statistics may not get called and in that |
aoqi@0 | 1329 | // case, if the sync time is less than PrintSafepointStatisticsTimeout, |
aoqi@0 | 1330 | // don't print it out. |
aoqi@0 | 1331 | // Approximate the vm op time. |
aoqi@0 | 1332 | _safepoint_stats[_cur_stat_index]._time_to_exec_vmop = |
aoqi@0 | 1333 | os::javaTimeNanos() - cleanup_end_time; |
aoqi@0 | 1334 | |
aoqi@0 | 1335 | if ( PrintSafepointStatisticsTimeout < 0 || |
aoqi@0 | 1336 | spstat->_time_to_sync > PrintSafepointStatisticsTimeout * MICROUNITS) { |
aoqi@0 | 1337 | print_statistics(); |
aoqi@0 | 1338 | } |
aoqi@0 | 1339 | tty->cr(); |
aoqi@0 | 1340 | |
aoqi@0 | 1341 | // Print out polling page sampling status. |
aoqi@0 | 1342 | if (!need_to_track_page_armed_status) { |
aoqi@0 | 1343 | if (UseCompilerSafepoints) { |
aoqi@0 | 1344 | tty->print_cr("Polling page always armed"); |
aoqi@0 | 1345 | } |
aoqi@0 | 1346 | } else { |
aoqi@0 | 1347 | tty->print_cr("Defer polling page loop count = %d\n", |
aoqi@0 | 1348 | DeferPollingPageLoopCount); |
aoqi@0 | 1349 | } |
aoqi@0 | 1350 | |
aoqi@0 | 1351 | for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) { |
aoqi@0 | 1352 | if (_safepoint_reasons[index] != 0) { |
aoqi@0 | 1353 | tty->print_cr("%-26s"UINT64_FORMAT_W(10), VM_Operation::name(index), |
aoqi@0 | 1354 | _safepoint_reasons[index]); |
aoqi@0 | 1355 | } |
aoqi@0 | 1356 | } |
aoqi@0 | 1357 | |
aoqi@0 | 1358 | tty->print_cr(UINT64_FORMAT_W(5)" VM operations coalesced during safepoint", |
aoqi@0 | 1359 | _coalesced_vmop_count); |
aoqi@0 | 1360 | tty->print_cr("Maximum sync time "INT64_FORMAT_W(5)" ms", |
aoqi@0 | 1361 | _max_sync_time / MICROUNITS); |
aoqi@0 | 1362 | tty->print_cr("Maximum vm operation time (except for Exit VM operation) " |
aoqi@0 | 1363 | INT64_FORMAT_W(5)" ms", |
aoqi@0 | 1364 | _max_vmop_time / MICROUNITS); |
aoqi@0 | 1365 | } |
aoqi@0 | 1366 | |
aoqi@0 | 1367 | // ------------------------------------------------------------------------------------------------ |
aoqi@0 | 1368 | // Non-product code |
aoqi@0 | 1369 | |
aoqi@0 | 1370 | #ifndef PRODUCT |
aoqi@0 | 1371 | |
aoqi@0 | 1372 | void SafepointSynchronize::print_state() { |
aoqi@0 | 1373 | if (_state == _not_synchronized) { |
aoqi@0 | 1374 | tty->print_cr("not synchronized"); |
aoqi@0 | 1375 | } else if (_state == _synchronizing || _state == _synchronized) { |
aoqi@0 | 1376 | tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" : |
aoqi@0 | 1377 | "synchronized"); |
aoqi@0 | 1378 | |
aoqi@0 | 1379 | for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) { |
aoqi@0 | 1380 | cur->safepoint_state()->print(); |
aoqi@0 | 1381 | } |
aoqi@0 | 1382 | } |
aoqi@0 | 1383 | } |
aoqi@0 | 1384 | |
aoqi@0 | 1385 | void SafepointSynchronize::safepoint_msg(const char* format, ...) { |
aoqi@0 | 1386 | if (ShowSafepointMsgs) { |
aoqi@0 | 1387 | va_list ap; |
aoqi@0 | 1388 | va_start(ap, format); |
aoqi@0 | 1389 | tty->vprint_cr(format, ap); |
aoqi@0 | 1390 | va_end(ap); |
aoqi@0 | 1391 | } |
aoqi@0 | 1392 | } |
aoqi@0 | 1393 | |
aoqi@0 | 1394 | #endif // !PRODUCT |