src/share/vm/runtime/safepoint.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 1
2d8a650513c2
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial