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