1 /* 2 * Copyright (c) 1997, 2020, 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/classLoaderDataGraph.inline.hpp" 27 #include "classfile/dictionary.hpp" 28 #include "classfile/stringTable.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionary.hpp" 31 #include "code/codeCache.hpp" 32 #include "code/icBuffer.hpp" 33 #include "code/nmethod.hpp" 34 #include "code/pcDesc.hpp" 35 #include "code/scopeDesc.hpp" 36 #include "compiler/compilationPolicy.hpp" 37 #include "gc/shared/collectedHeap.hpp" 38 #include "gc/shared/gcLocker.hpp" 39 #include "gc/shared/oopStorage.hpp" 40 #include "gc/shared/strongRootsScope.hpp" 41 #include "gc/shared/workgroup.hpp" 42 #include "interpreter/interpreter.hpp" 43 #include "jfr/jfrEvents.hpp" 44 #include "logging/log.hpp" 45 #include "logging/logStream.hpp" 46 #include "memory/resourceArea.hpp" 47 #include "memory/universe.hpp" 48 #include "oops/oop.inline.hpp" 49 #include "oops/symbol.hpp" 50 #include "runtime/atomic.hpp" 51 #include "runtime/deoptimization.hpp" 52 #include "runtime/frame.inline.hpp" 53 #include "runtime/handles.inline.hpp" 54 #include "runtime/interfaceSupport.inline.hpp" 55 #include "runtime/mutexLocker.hpp" 56 #include "runtime/orderAccess.hpp" 57 #include "runtime/osThread.hpp" 58 #include "runtime/safepoint.hpp" 59 #include "runtime/safepointMechanism.inline.hpp" 60 #include "runtime/signature.hpp" 61 #include "runtime/stubCodeGenerator.hpp" 62 #include "runtime/stubRoutines.hpp" 63 #include "runtime/sweeper.hpp" 64 #include "runtime/synchronizer.hpp" 65 #include "runtime/thread.inline.hpp" 66 #include "runtime/threadSMR.hpp" 67 #include "runtime/timerTrace.hpp" 68 #include "services/runtimeService.hpp" 69 #include "utilities/events.hpp" 70 #include "utilities/macros.hpp" 71 72 static void post_safepoint_begin_event(EventSafepointBegin& event, 73 uint64_t safepoint_id, 74 int thread_count, 75 int critical_thread_count) { 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, uint64_t safepoint_id) { 85 if (event.should_commit()) { 86 event.set_safepointId(safepoint_id); 87 event.commit(); 88 } 89 } 90 91 static void post_safepoint_synchronize_event(EventSafepointStateSynchronization& event, 92 uint64_t safepoint_id, 93 int initial_number_of_threads, 94 int threads_waiting_to_block, 95 uint64_t iterations) { 96 if (event.should_commit()) { 97 event.set_safepointId(safepoint_id); 98 event.set_initialThreadCount(initial_number_of_threads); 99 event.set_runningThreadCount(threads_waiting_to_block); 100 event.set_iterations(iterations); 101 event.commit(); 102 } 103 } 104 105 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask& event, 106 uint64_t safepoint_id, 107 const char* name) { 108 if (event.should_commit()) { 109 event.set_safepointId(safepoint_id); 110 event.set_name(name); 111 event.commit(); 112 } 113 } 114 115 static void post_safepoint_end_event(EventSafepointEnd& event, uint64_t safepoint_id) { 116 if (event.should_commit()) { 117 event.set_safepointId(safepoint_id); 118 event.commit(); 119 } 120 } 121 122 // SafepointCheck 123 SafepointStateTracker::SafepointStateTracker(uint64_t safepoint_id, bool at_safepoint) 124 : _safepoint_id(safepoint_id), _at_safepoint(at_safepoint) {} 125 126 bool SafepointStateTracker::safepoint_state_changed() { 127 return _safepoint_id != SafepointSynchronize::safepoint_id() || 128 _at_safepoint != SafepointSynchronize::is_at_safepoint(); 129 } 130 131 // -------------------------------------------------------------------------------------------------- 132 // Implementation of Safepoint begin/end 133 134 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized; 135 int SafepointSynchronize::_waiting_to_block = 0; 136 volatile uint64_t SafepointSynchronize::_safepoint_counter = 0; 137 uint64_t SafepointSynchronize::_safepoint_id = 0; 138 const uint64_t SafepointSynchronize::InactiveSafepointCounter = 0; 139 int SafepointSynchronize::_current_jni_active_count = 0; 140 141 WaitBarrier* SafepointSynchronize::_wait_barrier; 142 143 static volatile bool PageArmed = false; // safepoint polling page is RO|RW vs PROT_NONE 144 static bool timeout_error_printed = false; 145 146 // Statistic related 147 static jlong _safepoint_begin_time = 0; 148 static volatile int _nof_threads_hit_polling_page = 0; 149 150 void SafepointSynchronize::init(Thread* vmthread) { 151 // WaitBarrier should never be destroyed since we will have 152 // threads waiting on it while exiting. 153 _wait_barrier = new WaitBarrier(vmthread); 154 SafepointTracing::init(); 155 } 156 157 void SafepointSynchronize::increment_jni_active_count() { 158 assert(Thread::current()->is_VM_thread(), "Only VM thread may increment"); 159 ++_current_jni_active_count; 160 } 161 162 void SafepointSynchronize::decrement_waiting_to_block() { 163 assert(_waiting_to_block > 0, "sanity check"); 164 assert(Thread::current()->is_VM_thread(), "Only VM thread may decrement"); 165 --_waiting_to_block; 166 } 167 168 bool SafepointSynchronize::thread_not_running(ThreadSafepointState *cur_state) { 169 if (!cur_state->is_running()) { 170 return true; 171 } 172 cur_state->examine_state_of_thread(SafepointSynchronize::safepoint_counter()); 173 if (!cur_state->is_running()) { 174 return true; 175 } 176 LogTarget(Trace, safepoint) lt; 177 if (lt.is_enabled()) { 178 ResourceMark rm; 179 LogStream ls(lt); 180 cur_state->print_on(&ls); 181 } 182 return false; 183 } 184 185 #ifdef ASSERT 186 static void assert_list_is_valid(const ThreadSafepointState* tss_head, int still_running) { 187 int a = 0; 188 const ThreadSafepointState *tmp_tss = tss_head; 189 while (tmp_tss != NULL) { 190 ++a; 191 assert(tmp_tss->is_running(), "Illegal initial state"); 192 tmp_tss = tmp_tss->get_next(); 193 } 194 assert(a == still_running, "Must be the same"); 195 } 196 #endif // ASSERT 197 198 static void back_off(int64_t start_time) { 199 // We start with fine-grained nanosleeping until a millisecond has 200 // passed, at which point we resort to plain naked_short_sleep. 201 if (os::javaTimeNanos() - start_time < NANOSECS_PER_MILLISEC) { 202 os::naked_short_nanosleep(10 * (NANOUNITS / MICROUNITS)); 203 } else { 204 os::naked_short_sleep(1); 205 } 206 } 207 208 int SafepointSynchronize::synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running) 209 { 210 JavaThreadIteratorWithHandle jtiwh; 211 212 #ifdef ASSERT 213 for (; JavaThread *cur = jtiwh.next(); ) { 214 assert(cur->safepoint_state()->is_running(), "Illegal initial state"); 215 } 216 jtiwh.rewind(); 217 #endif // ASSERT 218 219 // Iterate through all threads until it has been determined how to stop them all at a safepoint. 220 int still_running = nof_threads; 221 ThreadSafepointState *tss_head = NULL; 222 ThreadSafepointState **p_prev = &tss_head; 223 for (; JavaThread *cur = jtiwh.next(); ) { 224 ThreadSafepointState *cur_tss = cur->safepoint_state(); 225 assert(cur_tss->get_next() == NULL, "Must be NULL"); 226 if (thread_not_running(cur_tss)) { 227 --still_running; 228 } else { 229 *p_prev = cur_tss; 230 p_prev = cur_tss->next_ptr(); 231 } 232 } 233 *p_prev = NULL; 234 235 DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);) 236 237 *initial_running = still_running; 238 239 // If there is no thread still running, we are already done. 240 if (still_running <= 0) { 241 assert(tss_head == NULL, "Must be empty"); 242 return 1; 243 } 244 245 int iterations = 1; // The first iteration is above. 246 int64_t start_time = os::javaTimeNanos(); 247 248 do { 249 // Check if this has taken too long: 250 if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) { 251 print_safepoint_timeout(); 252 } 253 if (int(iterations) == -1) { // overflow - something is wrong. 254 // We can only overflow here when we are using global 255 // polling pages. We keep this guarantee in its original 256 // form so that searches of the bug database for this 257 // failure mode find the right bugs. 258 guarantee (!PageArmed, "invariant"); 259 } 260 261 p_prev = &tss_head; 262 ThreadSafepointState *cur_tss = tss_head; 263 while (cur_tss != NULL) { 264 assert(cur_tss->is_running(), "Illegal initial state"); 265 if (thread_not_running(cur_tss)) { 266 --still_running; 267 *p_prev = NULL; 268 ThreadSafepointState *tmp = cur_tss; 269 cur_tss = cur_tss->get_next(); 270 tmp->set_next(NULL); 271 } else { 272 *p_prev = cur_tss; 273 p_prev = cur_tss->next_ptr(); 274 cur_tss = cur_tss->get_next(); 275 } 276 } 277 278 DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);) 279 280 if (still_running > 0) { 281 back_off(start_time); 282 } 283 284 iterations++; 285 } while (still_running > 0); 286 287 assert(tss_head == NULL, "Must be empty"); 288 289 return iterations; 290 } 291 292 void SafepointSynchronize::arm_safepoint() { 293 // Begin the process of bringing the system to a safepoint. 294 // Java threads can be in several different states and are 295 // stopped by different mechanisms: 296 // 297 // 1. Running interpreted 298 // When executing branching/returning byte codes interpreter 299 // checks if the poll is armed, if so blocks in SS::block(). 300 // When using global polling the interpreter dispatch table 301 // is changed to force it to check for a safepoint condition 302 // between bytecodes. 303 // 2. Running in native code 304 // When returning from the native code, a Java thread must check 305 // the safepoint _state to see if we must block. If the 306 // VM thread sees a Java thread in native, it does 307 // not wait for this thread to block. The order of the memory 308 // writes and reads of both the safepoint state and the Java 309 // threads state is critical. In order to guarantee that the 310 // memory writes are serialized with respect to each other, 311 // the VM thread issues a memory barrier instruction. 312 // 3. Running compiled Code 313 // Compiled code reads the local polling page that 314 // is set to fault if we are trying to get to a safepoint. 315 // 4. Blocked 316 // A thread which is blocked will not be allowed to return from the 317 // block condition until the safepoint operation is complete. 318 // 5. In VM or Transitioning between states 319 // If a Java thread is currently running in the VM or transitioning 320 // between states, the safepointing code will poll the thread state 321 // until the thread blocks itself when it attempts transitions to a 322 // new state or locking a safepoint checked monitor. 323 324 // We must never miss a thread with correct safepoint id, so we must make sure we arm 325 // the wait barrier for the next safepoint id/counter. 326 // Arming must be done after resetting _current_jni_active_count, _waiting_to_block. 327 _wait_barrier->arm(static_cast<int>(_safepoint_counter + 1)); 328 329 assert((_safepoint_counter & 0x1) == 0, "must be even"); 330 // The store to _safepoint_counter must happen after any stores in arming. 331 Atomic::release_store(&_safepoint_counter, _safepoint_counter + 1); 332 333 // We are synchronizing 334 OrderAccess::storestore(); // Ordered with _safepoint_counter 335 _state = _synchronizing; 336 337 if (SafepointMechanism::uses_thread_local_poll()) { 338 // Arming the per thread poll while having _state != _not_synchronized means safepointing 339 log_trace(safepoint)("Setting thread local yield flag for threads"); 340 OrderAccess::storestore(); // storestore, global state -> local state 341 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) { 342 // Make sure the threads start polling, it is time to yield. 343 SafepointMechanism::arm_local_poll(cur); 344 } 345 } 346 OrderAccess::fence(); // storestore|storeload, global state -> local state 347 348 if (SafepointMechanism::uses_global_page_poll()) { 349 // Make interpreter safepoint aware 350 Interpreter::notice_safepoints(); 351 352 // Make polling safepoint aware 353 guarantee (!PageArmed, "invariant") ; 354 PageArmed = true; 355 os::make_polling_page_unreadable(); 356 } 357 } 358 359 // Roll all threads forward to a safepoint and suspend them all 360 void SafepointSynchronize::begin() { 361 assert(Thread::current()->is_VM_thread(), "Only VM thread may execute a safepoint"); 362 363 EventSafepointBegin begin_event; 364 SafepointTracing::begin(VMThread::vm_op_type()); 365 366 Universe::heap()->safepoint_synchronize_begin(); 367 368 // By getting the Threads_lock, we assure that no threads are about to start or 369 // exit. It is released again in SafepointSynchronize::end(). 370 Threads_lock->lock(); 371 372 assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state"); 373 374 int nof_threads = Threads::number_of_threads(); 375 376 _nof_threads_hit_polling_page = 0; 377 378 log_debug(safepoint)("Safepoint synchronization initiated using %s wait barrier. (%d threads)", _wait_barrier->description(), nof_threads); 379 380 // Reset the count of active JNI critical threads 381 _current_jni_active_count = 0; 382 383 // Set number of threads to wait for 384 _waiting_to_block = nof_threads; 385 386 jlong safepoint_limit_time = 0; 387 if (SafepointTimeout) { 388 // Set the limit time, so that it can be compared to see if this has taken 389 // too long to complete. 390 safepoint_limit_time = SafepointTracing::start_of_safepoint() + (jlong)SafepointTimeoutDelay * (NANOUNITS / MILLIUNITS); 391 timeout_error_printed = false; 392 } 393 394 EventSafepointStateSynchronization sync_event; 395 int initial_running = 0; 396 397 // Arms the safepoint, _current_jni_active_count and _waiting_to_block must be set before. 398 arm_safepoint(); 399 400 // Will spin until all threads are safe. 401 int iterations = synchronize_threads(safepoint_limit_time, nof_threads, &initial_running); 402 assert(_waiting_to_block == 0, "No thread should be running"); 403 404 #ifndef PRODUCT 405 if (safepoint_limit_time != 0) { 406 jlong current_time = os::javaTimeNanos(); 407 if (safepoint_limit_time < current_time) { 408 log_warning(safepoint)("# SafepointSynchronize: Finished after " 409 INT64_FORMAT_W(6) " ms", 410 (int64_t)(current_time - SafepointTracing::start_of_safepoint()) / (NANOUNITS / MILLIUNITS)); 411 } 412 } 413 #endif 414 415 assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); 416 417 // Record state 418 _state = _synchronized; 419 420 OrderAccess::fence(); 421 422 // Set the new id 423 ++_safepoint_id; 424 425 #ifdef ASSERT 426 // Make sure all the threads were visited. 427 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) { 428 assert(cur->was_visited_for_critical_count(_safepoint_counter), "missed a thread"); 429 } 430 #endif // ASSERT 431 432 // Update the count of active JNI critical regions 433 GCLocker::set_jni_lock_count(_current_jni_active_count); 434 435 post_safepoint_synchronize_event(sync_event, 436 _safepoint_id, 437 initial_running, 438 _waiting_to_block, iterations); 439 440 SafepointTracing::synchronized(nof_threads, initial_running, _nof_threads_hit_polling_page); 441 442 // We do the safepoint cleanup first since a GC related safepoint 443 // needs cleanup to be completed before running the GC op. 444 EventSafepointCleanup cleanup_event; 445 do_cleanup_tasks(); 446 post_safepoint_cleanup_event(cleanup_event, _safepoint_id); 447 448 post_safepoint_begin_event(begin_event, _safepoint_id, nof_threads, _current_jni_active_count); 449 SafepointTracing::cleanup(); 450 } 451 452 void SafepointSynchronize::disarm_safepoint() { 453 uint64_t active_safepoint_counter = _safepoint_counter; 454 { 455 JavaThreadIteratorWithHandle jtiwh; 456 #ifdef ASSERT 457 // A pending_exception cannot be installed during a safepoint. The threads 458 // may install an async exception after they come back from a safepoint into 459 // pending_exception after they unblock. But that should happen later. 460 for (; JavaThread *cur = jtiwh.next(); ) { 461 assert (!(cur->has_pending_exception() && 462 cur->safepoint_state()->is_at_poll_safepoint()), 463 "safepoint installed a pending exception"); 464 } 465 #endif // ASSERT 466 467 if (SafepointMechanism::uses_global_page_poll()) { 468 guarantee (PageArmed, "invariant"); 469 // Make polling safepoint aware 470 os::make_polling_page_readable(); 471 PageArmed = false; 472 // Remove safepoint check from interpreter 473 Interpreter::ignore_safepoints(); 474 } 475 476 OrderAccess::fence(); // keep read and write of _state from floating up 477 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization"); 478 479 // Change state first to _not_synchronized. 480 // No threads should see _synchronized when running. 481 _state = _not_synchronized; 482 483 // Set the next dormant (even) safepoint id. 484 assert((_safepoint_counter & 0x1) == 1, "must be odd"); 485 Atomic::release_store(&_safepoint_counter, _safepoint_counter + 1); 486 487 OrderAccess::fence(); // Keep the local state from floating up. 488 489 jtiwh.rewind(); 490 for (; JavaThread *current = jtiwh.next(); ) { 491 // Clear the visited flag to ensure that the critical counts are collected properly. 492 DEBUG_ONLY(current->reset_visited_for_critical_count(active_safepoint_counter);) 493 ThreadSafepointState* cur_state = current->safepoint_state(); 494 assert(!cur_state->is_running(), "Thread not suspended at safepoint"); 495 cur_state->restart(); // TSS _running 496 assert(cur_state->is_running(), "safepoint state has not been reset"); 497 } 498 } // ~JavaThreadIteratorWithHandle 499 500 // Release threads lock, so threads can be created/destroyed again. 501 Threads_lock->unlock(); 502 503 // Wake threads after local state is correctly set. 504 _wait_barrier->disarm(); 505 } 506 507 // Wake up all threads, so they are ready to resume execution after the safepoint 508 // operation has been carried out 509 void SafepointSynchronize::end() { 510 assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); 511 EventSafepointEnd event; 512 assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint"); 513 514 disarm_safepoint(); 515 516 Universe::heap()->safepoint_synchronize_end(); 517 518 SafepointTracing::end(); 519 520 post_safepoint_end_event(event, safepoint_id()); 521 } 522 523 bool SafepointSynchronize::is_cleanup_needed() { 524 // Need a safepoint if there are many monitors to deflate. 525 if (ObjectSynchronizer::is_cleanup_needed()) return true; 526 // Need a safepoint if some inline cache buffers is non-empty 527 if (!InlineCacheBuffer::is_empty()) return true; 528 if (StringTable::needs_rehashing()) return true; 529 if (SymbolTable::needs_rehashing()) return true; 530 return false; 531 } 532 533 bool SafepointSynchronize::is_forced_cleanup_needed() { 534 return ObjectSynchronizer::needs_monitor_scavenge(); 535 } 536 537 class ParallelSPCleanupThreadClosure : public ThreadClosure { 538 private: 539 CodeBlobClosure* _nmethod_cl; 540 DeflateMonitorCounters* _counters; 541 542 public: 543 ParallelSPCleanupThreadClosure(DeflateMonitorCounters* counters) : 544 _nmethod_cl(UseCodeAging ? NMethodSweeper::prepare_reset_hotness_counters() : NULL), 545 _counters(counters) {} 546 547 void do_thread(Thread* thread) { 548 ObjectSynchronizer::deflate_thread_local_monitors(thread, _counters); 549 if (_nmethod_cl != NULL && thread->is_Java_thread() && 550 ! thread->is_Code_cache_sweeper_thread()) { 551 JavaThread* jt = (JavaThread*) thread; 552 jt->nmethods_do(_nmethod_cl); 553 } 554 } 555 }; 556 557 class ParallelSPCleanupTask : public AbstractGangTask { 558 private: 559 SubTasksDone _subtasks; 560 ParallelSPCleanupThreadClosure _cleanup_threads_cl; 561 uint _num_workers; 562 DeflateMonitorCounters* _counters; 563 public: 564 ParallelSPCleanupTask(uint num_workers, DeflateMonitorCounters* counters) : 565 AbstractGangTask("Parallel Safepoint Cleanup"), 566 _subtasks(SubTasksDone(SafepointSynchronize::SAFEPOINT_CLEANUP_NUM_TASKS)), 567 _cleanup_threads_cl(ParallelSPCleanupThreadClosure(counters)), 568 _num_workers(num_workers), 569 _counters(counters) {} 570 571 void work(uint worker_id) { 572 uint64_t safepoint_id = SafepointSynchronize::safepoint_id(); 573 // All threads deflate monitors and mark nmethods (if necessary). 574 Threads::possibly_parallel_threads_do(true, &_cleanup_threads_cl); 575 576 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_DEFLATE_MONITORS)) { 577 const char* name = "deflating global idle monitors"; 578 EventSafepointCleanupTask event; 579 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup)); 580 ObjectSynchronizer::deflate_idle_monitors(_counters); 581 582 post_safepoint_cleanup_task_event(event, safepoint_id, name); 583 } 584 585 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_UPDATE_INLINE_CACHES)) { 586 const char* name = "updating inline caches"; 587 EventSafepointCleanupTask event; 588 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup)); 589 InlineCacheBuffer::update_inline_caches(); 590 591 post_safepoint_cleanup_task_event(event, safepoint_id, name); 592 } 593 594 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_COMPILATION_POLICY)) { 595 const char* name = "compilation policy safepoint handler"; 596 EventSafepointCleanupTask event; 597 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup)); 598 CompilationPolicy::policy()->do_safepoint_work(); 599 600 post_safepoint_cleanup_task_event(event, safepoint_id, name); 601 } 602 603 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH)) { 604 if (SymbolTable::needs_rehashing()) { 605 const char* name = "rehashing symbol table"; 606 EventSafepointCleanupTask event; 607 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup)); 608 SymbolTable::rehash_table(); 609 610 post_safepoint_cleanup_task_event(event, safepoint_id, name); 611 } 612 } 613 614 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_STRING_TABLE_REHASH)) { 615 if (StringTable::needs_rehashing()) { 616 const char* name = "rehashing string table"; 617 EventSafepointCleanupTask event; 618 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup)); 619 StringTable::rehash_table(); 620 621 post_safepoint_cleanup_task_event(event, safepoint_id, name); 622 } 623 } 624 625 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_SYSTEM_DICTIONARY_RESIZE)) { 626 if (Dictionary::does_any_dictionary_needs_resizing()) { 627 const char* name = "resizing system dictionaries"; 628 EventSafepointCleanupTask event; 629 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup)); 630 ClassLoaderDataGraph::resize_dictionaries(); 631 632 post_safepoint_cleanup_task_event(event, safepoint_id, name); 633 } 634 } 635 636 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_REQUEST_OOPSTORAGE_CLEANUP)) { 637 // Don't bother reporting event or time for this very short operation. 638 // To have any utility we'd also want to report whether needed. 639 OopStorage::trigger_cleanup_if_needed(); 640 } 641 642 _subtasks.all_tasks_completed(_num_workers); 643 } 644 }; 645 646 // Various cleaning tasks that should be done periodically at safepoints. 647 void SafepointSynchronize::do_cleanup_tasks() { 648 649 TraceTime timer("safepoint cleanup tasks", TRACETIME_LOG(Info, safepoint, cleanup)); 650 651 // Prepare for monitor deflation. 652 DeflateMonitorCounters deflate_counters; 653 ObjectSynchronizer::prepare_deflate_idle_monitors(&deflate_counters); 654 655 CollectedHeap* heap = Universe::heap(); 656 assert(heap != NULL, "heap not initialized yet?"); 657 WorkGang* cleanup_workers = heap->get_safepoint_workers(); 658 if (cleanup_workers != NULL) { 659 // Parallel cleanup using GC provided thread pool. 660 uint num_cleanup_workers = cleanup_workers->active_workers(); 661 ParallelSPCleanupTask cleanup(num_cleanup_workers, &deflate_counters); 662 StrongRootsScope srs(num_cleanup_workers); 663 cleanup_workers->run_task(&cleanup); 664 } else { 665 // Serial cleanup using VMThread. 666 ParallelSPCleanupTask cleanup(1, &deflate_counters); 667 StrongRootsScope srs(1); 668 cleanup.work(0); 669 } 670 671 // Needs to be done single threaded by the VMThread. This walks 672 // the thread stacks looking for references to metadata before 673 // deciding to remove it from the metaspaces. 674 if (ClassLoaderDataGraph::should_clean_metaspaces_and_reset()) { 675 const char* name = "cleanup live ClassLoaderData metaspaces"; 676 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup)); 677 ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces(); 678 } 679 680 // Finish monitor deflation. 681 ObjectSynchronizer::finish_deflate_idle_monitors(&deflate_counters); 682 683 assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer"); 684 } 685 686 // Methods for determining if a JavaThread is safepoint safe. 687 688 // False means unsafe with undetermined state. 689 // True means a determined state, but it may be an unsafe state. 690 // If called from a non-safepoint context safepoint_count MUST be InactiveSafepointCounter. 691 bool SafepointSynchronize::try_stable_load_state(JavaThreadState *state, JavaThread *thread, uint64_t safepoint_count) { 692 assert((safepoint_count != InactiveSafepointCounter && 693 Thread::current() == (Thread*)VMThread::vm_thread() && 694 SafepointSynchronize::_state != _not_synchronized) 695 || safepoint_count == InactiveSafepointCounter, "Invalid check"); 696 697 // To handle the thread_blocked state on the backedge of the WaitBarrier from 698 // previous safepoint and reading the reset value (0/InactiveSafepointCounter) we 699 // re-read state after we read thread safepoint id. The JavaThread changes its 700 // thread state from thread_blocked before resetting safepoint id to 0. 701 // This guarantees the second read will be from an updated thread state. It can 702 // either be different state making this an unsafe state or it can see blocked 703 // again. When we see blocked twice with a 0 safepoint id, either: 704 // - It is normally blocked, e.g. on Mutex, TBIVM. 705 // - It was in SS:block(), looped around to SS:block() and is blocked on the WaitBarrier. 706 // - It was in SS:block() but now on a Mutex. 707 // All of these cases are safe. 708 709 *state = thread->thread_state(); 710 OrderAccess::loadload(); 711 uint64_t sid = thread->safepoint_state()->get_safepoint_id(); // Load acquire 712 if (sid != InactiveSafepointCounter && sid != safepoint_count) { 713 // In an old safepoint, state not relevant. 714 return false; 715 } 716 return *state == thread->thread_state(); 717 } 718 719 static bool safepoint_safe_with(JavaThread *thread, JavaThreadState state) { 720 switch(state) { 721 case _thread_in_native: 722 // native threads are safe if they have no java stack or have walkable stack 723 return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable(); 724 725 case _thread_blocked: 726 // On wait_barrier or blocked. 727 // Blocked threads should already have walkable stack. 728 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable"); 729 return true; 730 731 default: 732 return false; 733 } 734 } 735 736 bool SafepointSynchronize::handshake_safe(JavaThread *thread) { 737 if (thread->is_ext_suspended() || thread->is_terminated()) { 738 return true; 739 } 740 JavaThreadState stable_state; 741 if (try_stable_load_state(&stable_state, thread, InactiveSafepointCounter)) { 742 return safepoint_safe_with(thread, stable_state); 743 } 744 return false; 745 } 746 747 // See if the thread is running inside a lazy critical native and 748 // update the thread critical count if so. Also set a suspend flag to 749 // cause the native wrapper to return into the JVM to do the unlock 750 // once the native finishes. 751 static void check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state) { 752 if (state == _thread_in_native && 753 thread->has_last_Java_frame() && 754 thread->frame_anchor()->walkable()) { 755 // This thread might be in a critical native nmethod so look at 756 // the top of the stack and increment the critical count if it 757 // is. 758 frame wrapper_frame = thread->last_frame(); 759 CodeBlob* stub_cb = wrapper_frame.cb(); 760 if (stub_cb != NULL && 761 stub_cb->is_nmethod() && 762 stub_cb->as_nmethod_or_null()->is_lazy_critical_native()) { 763 // A thread could potentially be in a critical native across 764 // more than one safepoint, so only update the critical state on 765 // the first one. When it returns it will perform the unlock. 766 if (!thread->do_critical_native_unlock()) { 767 #ifdef ASSERT 768 if (!thread->in_critical()) { 769 GCLocker::increment_debug_jni_lock_count(); 770 } 771 #endif 772 thread->enter_critical(); 773 // Make sure the native wrapper calls back on return to 774 // perform the needed critical unlock. 775 thread->set_critical_native_unlock(); 776 } 777 } 778 } 779 } 780 781 // ------------------------------------------------------------------------------------------------------- 782 // Implementation of Safepoint blocking point 783 784 void SafepointSynchronize::block(JavaThread *thread) { 785 assert(thread != NULL, "thread must be set"); 786 assert(thread->is_Java_thread(), "not a Java thread"); 787 788 // Threads shouldn't block if they are in the middle of printing, but... 789 ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id()); 790 791 // Only bail from the block() call if the thread is gone from the 792 // thread list; starting to exit should still block. 793 if (thread->is_terminated()) { 794 // block current thread if we come here from native code when VM is gone 795 thread->block_if_vm_exited(); 796 797 // otherwise do nothing 798 return; 799 } 800 801 JavaThreadState state = thread->thread_state(); 802 thread->frame_anchor()->make_walkable(thread); 803 804 uint64_t safepoint_id = SafepointSynchronize::safepoint_counter(); 805 // Check that we have a valid thread_state at this point 806 switch(state) { 807 case _thread_in_vm_trans: 808 case _thread_in_Java: // From compiled code 809 case _thread_in_native_trans: 810 case _thread_blocked_trans: 811 case _thread_new_trans: 812 813 // We have no idea where the VMThread is, it might even be at next safepoint. 814 // So we can miss this poll, but stop at next. 815 816 // Load dependent store, it must not pass loading of safepoint_id. 817 thread->safepoint_state()->set_safepoint_id(safepoint_id); // Release store 818 819 // This part we can skip if we notice we miss or are in a future safepoint. 820 OrderAccess::storestore(); 821 // Load in wait barrier should not float up 822 thread->set_thread_state_fence(_thread_blocked); 823 824 _wait_barrier->wait(static_cast<int>(safepoint_id)); 825 assert(_state != _synchronized, "Can't be"); 826 827 // If barrier is disarmed stop store from floating above loads in barrier. 828 OrderAccess::loadstore(); 829 thread->set_thread_state(state); 830 831 // Then we reset the safepoint id to inactive. 832 thread->safepoint_state()->reset_safepoint_id(); // Release store 833 834 OrderAccess::fence(); 835 836 break; 837 838 default: 839 fatal("Illegal threadstate encountered: %d", state); 840 } 841 guarantee(thread->safepoint_state()->get_safepoint_id() == InactiveSafepointCounter, 842 "The safepoint id should be set only in block path"); 843 844 // Check for pending. async. exceptions or suspends - except if the 845 // thread was blocked inside the VM. has_special_runtime_exit_condition() 846 // is called last since it grabs a lock and we only want to do that when 847 // we must. 848 // 849 // Note: we never deliver an async exception at a polling point as the 850 // compiler may not have an exception handler for it. The polling 851 // code will notice the async and deoptimize and the exception will 852 // be delivered. (Polling at a return point is ok though). Sure is 853 // a lot of bother for a deprecated feature... 854 // 855 // We don't deliver an async exception if the thread state is 856 // _thread_in_native_trans so JNI functions won't be called with 857 // a surprising pending exception. If the thread state is going back to java, 858 // async exception is checked in check_special_condition_for_native_trans(). 859 860 if (state != _thread_blocked_trans && 861 state != _thread_in_vm_trans && 862 thread->has_special_runtime_exit_condition()) { 863 thread->handle_special_runtime_exit_condition( 864 !thread->is_at_poll_safepoint() && (state != _thread_in_native_trans)); 865 } 866 867 // cross_modify_fence is done by SafepointMechanism::block_if_requested_slow 868 // which is the only caller here. 869 } 870 871 // ------------------------------------------------------------------------------------------------------ 872 // Exception handlers 873 874 875 void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) { 876 assert(thread->is_Java_thread(), "polling reference encountered by VM thread"); 877 assert(thread->thread_state() == _thread_in_Java, "should come from Java code"); 878 if (!SafepointMechanism::uses_thread_local_poll()) { 879 assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization"); 880 } 881 882 if (log_is_enabled(Info, safepoint, stats)) { 883 Atomic::inc(&_nof_threads_hit_polling_page); 884 } 885 886 ThreadSafepointState* state = thread->safepoint_state(); 887 888 state->handle_polling_page_exception(); 889 } 890 891 892 void SafepointSynchronize::print_safepoint_timeout() { 893 if (!timeout_error_printed) { 894 timeout_error_printed = true; 895 // Print out the thread info which didn't reach the safepoint for debugging 896 // purposes (useful when there are lots of threads in the debugger). 897 LogTarget(Warning, safepoint) lt; 898 if (lt.is_enabled()) { 899 ResourceMark rm; 900 LogStream ls(lt); 901 902 ls.cr(); 903 ls.print_cr("# SafepointSynchronize::begin: Timeout detected:"); 904 ls.print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint."); 905 ls.print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:"); 906 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) { 907 if (cur_thread->safepoint_state()->is_running()) { 908 ls.print("# "); 909 cur_thread->print_on(&ls); 910 ls.cr(); 911 } 912 } 913 ls.print_cr("# SafepointSynchronize::begin: (End of list)"); 914 } 915 } 916 917 // To debug the long safepoint, specify both AbortVMOnSafepointTimeout & 918 // ShowMessageBoxOnError. 919 if (AbortVMOnSafepointTimeout) { 920 // Send the blocking thread a signal to terminate and write an error file. 921 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) { 922 if (cur_thread->safepoint_state()->is_running()) { 923 if (!os::signal_thread(cur_thread, SIGILL, "blocking a safepoint")) { 924 break; // Could not send signal. Report fatal error. 925 } 926 // Give cur_thread a chance to report the error and terminate the VM. 927 os::naked_sleep(3000); 928 } 929 } 930 fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.", 931 SafepointTimeoutDelay, VMThread::vm_operation()->name()); 932 } 933 } 934 935 // ------------------------------------------------------------------------------------------------------- 936 // Implementation of ThreadSafepointState 937 938 ThreadSafepointState::ThreadSafepointState(JavaThread *thread) 939 : _at_poll_safepoint(false), _thread(thread), _safepoint_safe(false), 940 _safepoint_id(SafepointSynchronize::InactiveSafepointCounter), _next(NULL) { 941 } 942 943 void ThreadSafepointState::create(JavaThread *thread) { 944 ThreadSafepointState *state = new ThreadSafepointState(thread); 945 thread->set_safepoint_state(state); 946 } 947 948 void ThreadSafepointState::destroy(JavaThread *thread) { 949 if (thread->safepoint_state()) { 950 delete(thread->safepoint_state()); 951 thread->set_safepoint_state(NULL); 952 } 953 } 954 955 uint64_t ThreadSafepointState::get_safepoint_id() const { 956 return Atomic::load_acquire(&_safepoint_id); 957 } 958 959 void ThreadSafepointState::reset_safepoint_id() { 960 Atomic::release_store(&_safepoint_id, SafepointSynchronize::InactiveSafepointCounter); 961 } 962 963 void ThreadSafepointState::set_safepoint_id(uint64_t safepoint_id) { 964 Atomic::release_store(&_safepoint_id, safepoint_id); 965 } 966 967 void ThreadSafepointState::examine_state_of_thread(uint64_t safepoint_count) { 968 assert(is_running(), "better be running or just have hit safepoint poll"); 969 970 JavaThreadState stable_state; 971 if (!SafepointSynchronize::try_stable_load_state(&stable_state, _thread, safepoint_count)) { 972 // We could not get stable state of the JavaThread. 973 // Consider it running and just return. 974 return; 975 } 976 977 // Check for a thread that is suspended. Note that thread resume tries 978 // to grab the Threads_lock which we own here, so a thread cannot be 979 // resumed during safepoint synchronization. 980 981 // We check to see if this thread is suspended without locking to 982 // avoid deadlocking with a third thread that is waiting for this 983 // thread to be suspended. The third thread can notice the safepoint 984 // that we're trying to start at the beginning of its SR_lock->wait() 985 // call. If that happens, then the third thread will block on the 986 // safepoint while still holding the underlying SR_lock. We won't be 987 // able to get the SR_lock and we'll deadlock. 988 // 989 // We don't need to grab the SR_lock here for two reasons: 990 // 1) The suspend flags are both volatile and are set with an 991 // Atomic::cmpxchg() call so we should see the suspended 992 // state right away. 993 // 2) We're being called from the safepoint polling loop; if 994 // we don't see the suspended state on this iteration, then 995 // we'll come around again. 996 // 997 bool is_suspended = _thread->is_ext_suspended(); 998 if (is_suspended) { 999 account_safe_thread(); 1000 return; 1001 } 1002 1003 if (safepoint_safe_with(_thread, stable_state)) { 1004 check_for_lazy_critical_native(_thread, stable_state); 1005 account_safe_thread(); 1006 return; 1007 } 1008 1009 // All other thread states will continue to run until they 1010 // transition and self-block in state _blocked 1011 // Safepoint polling in compiled code causes the Java threads to do the same. 1012 // Note: new threads may require a malloc so they must be allowed to finish 1013 1014 assert(is_running(), "examine_state_of_thread on non-running thread"); 1015 return; 1016 } 1017 1018 void ThreadSafepointState::account_safe_thread() { 1019 SafepointSynchronize::decrement_waiting_to_block(); 1020 if (_thread->in_critical()) { 1021 // Notice that this thread is in a critical section 1022 SafepointSynchronize::increment_jni_active_count(); 1023 } 1024 DEBUG_ONLY(_thread->set_visited_for_critical_count(SafepointSynchronize::safepoint_counter());) 1025 assert(!_safepoint_safe, "Must be unsafe before safe"); 1026 _safepoint_safe = true; 1027 } 1028 1029 void ThreadSafepointState::restart() { 1030 assert(_safepoint_safe, "Must be safe before unsafe"); 1031 _safepoint_safe = false; 1032 } 1033 1034 void ThreadSafepointState::print_on(outputStream *st) const { 1035 const char *s = _safepoint_safe ? "_at_safepoint" : "_running"; 1036 1037 st->print_cr("Thread: " INTPTR_FORMAT 1038 " [0x%2x] State: %s _at_poll_safepoint %d", 1039 p2i(_thread), _thread->osthread()->thread_id(), s, _at_poll_safepoint); 1040 1041 _thread->print_thread_state_on(st); 1042 } 1043 1044 // --------------------------------------------------------------------------------------------------------------------- 1045 1046 // Block the thread at poll or poll return for safepoint/handshake. 1047 void ThreadSafepointState::handle_polling_page_exception() { 1048 1049 // If we're using a global poll, then the thread should not be 1050 // marked as safepoint safe yet. 1051 assert(!SafepointMechanism::uses_global_page_poll() || !_safepoint_safe, 1052 "polling page exception on thread safepoint safe"); 1053 1054 // Step 1: Find the nmethod from the return address 1055 address real_return_addr = thread()->saved_exception_pc(); 1056 1057 CodeBlob *cb = CodeCache::find_blob(real_return_addr); 1058 assert(cb != NULL && cb->is_compiled(), "return address should be in nmethod"); 1059 CompiledMethod* nm = (CompiledMethod*)cb; 1060 1061 // Find frame of caller 1062 frame stub_fr = thread()->last_frame(); 1063 CodeBlob* stub_cb = stub_fr.cb(); 1064 assert(stub_cb->is_safepoint_stub(), "must be a safepoint stub"); 1065 RegisterMap map(thread(), true); 1066 frame caller_fr = stub_fr.sender(&map); 1067 1068 // Should only be poll_return or poll 1069 assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" ); 1070 1071 // This is a poll immediately before a return. The exception handling code 1072 // has already had the effect of causing the return to occur, so the execution 1073 // will continue immediately after the call. In addition, the oopmap at the 1074 // return point does not mark the return value as an oop (if it is), so 1075 // it needs a handle here to be updated. 1076 if( nm->is_at_poll_return(real_return_addr) ) { 1077 // See if return type is an oop. 1078 bool return_oop = nm->method()->is_returning_oop(); 1079 Handle return_value; 1080 if (return_oop) { 1081 // The oop result has been saved on the stack together with all 1082 // the other registers. In order to preserve it over GCs we need 1083 // to keep it in a handle. 1084 oop result = caller_fr.saved_oop_result(&map); 1085 assert(oopDesc::is_oop_or_null(result), "must be oop"); 1086 return_value = Handle(thread(), result); 1087 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer"); 1088 } 1089 1090 // Block the thread 1091 SafepointMechanism::block_if_requested(thread()); 1092 1093 // restore oop result, if any 1094 if (return_oop) { 1095 caller_fr.set_saved_oop_result(&map, return_value()); 1096 } 1097 } 1098 1099 // This is a safepoint poll. Verify the return address and block. 1100 else { 1101 set_at_poll_safepoint(true); 1102 1103 // verify the blob built the "return address" correctly 1104 assert(real_return_addr == caller_fr.pc(), "must match"); 1105 1106 // Block the thread 1107 SafepointMechanism::block_if_requested(thread()); 1108 set_at_poll_safepoint(false); 1109 1110 // If we have a pending async exception deoptimize the frame 1111 // as otherwise we may never deliver it. 1112 if (thread()->has_async_condition()) { 1113 ThreadInVMfromJavaNoAsyncException __tiv(thread()); 1114 Deoptimization::deoptimize_frame(thread(), caller_fr.id()); 1115 } 1116 1117 // If an exception has been installed we must check for a pending deoptimization 1118 // Deoptimize frame if exception has been thrown. 1119 1120 if (thread()->has_pending_exception() ) { 1121 RegisterMap map(thread(), true); 1122 frame caller_fr = stub_fr.sender(&map); 1123 if (caller_fr.is_deoptimized_frame()) { 1124 // The exception patch will destroy registers that are still 1125 // live and will be needed during deoptimization. Defer the 1126 // Async exception should have deferred the exception until the 1127 // next safepoint which will be detected when we get into 1128 // the interpreter so if we have an exception now things 1129 // are messed up. 1130 1131 fatal("Exception installed and deoptimization is pending"); 1132 } 1133 } 1134 } 1135 } 1136 1137 1138 // ------------------------------------------------------------------------------------------------------- 1139 // Implementation of SafepointTracing 1140 1141 jlong SafepointTracing::_last_safepoint_begin_time_ns = 0; 1142 jlong SafepointTracing::_last_safepoint_sync_time_ns = 0; 1143 jlong SafepointTracing::_last_safepoint_cleanup_time_ns = 0; 1144 jlong SafepointTracing::_last_safepoint_end_time_ns = 0; 1145 jlong SafepointTracing::_last_safepoint_end_time_epoch_ms = 0; 1146 jlong SafepointTracing::_last_app_time_ns = 0; 1147 int SafepointTracing::_nof_threads = 0; 1148 int SafepointTracing::_nof_running = 0; 1149 int SafepointTracing::_page_trap = 0; 1150 VM_Operation::VMOp_Type SafepointTracing::_current_type; 1151 jlong SafepointTracing::_max_sync_time = 0; 1152 jlong SafepointTracing::_max_vmop_time = 0; 1153 uint64_t SafepointTracing::_op_count[VM_Operation::VMOp_Terminating] = {0}; 1154 1155 void SafepointTracing::init() { 1156 // Application start 1157 _last_safepoint_end_time_ns = os::javaTimeNanos(); 1158 // amount of time since epoch 1159 _last_safepoint_end_time_epoch_ms = os::javaTimeMillis(); 1160 } 1161 1162 // Helper method to print the header. 1163 static void print_header(outputStream* st) { 1164 // The number of spaces is significant here, and should match the format 1165 // specifiers in print_statistics(). 1166 1167 st->print("VM Operation " 1168 "[ threads: total initial_running ]" 1169 "[ time: sync cleanup vmop total ]"); 1170 1171 st->print_cr(" page_trap_count"); 1172 } 1173 1174 // This prints a nice table. To get the statistics to not shift due to the logging uptime 1175 // decorator, use the option as: -Xlog:safepoint+stats:[outputfile]:none 1176 void SafepointTracing::statistics_log() { 1177 LogTarget(Info, safepoint, stats) lt; 1178 assert (lt.is_enabled(), "should only be called when printing statistics is enabled"); 1179 LogStream ls(lt); 1180 1181 static int _cur_stat_index = 0; 1182 1183 // Print header every 30 entries 1184 if ((_cur_stat_index % 30) == 0) { 1185 print_header(&ls); 1186 _cur_stat_index = 1; // wrap 1187 } else { 1188 _cur_stat_index++; 1189 } 1190 1191 ls.print("%-28s [ " 1192 INT32_FORMAT_W(8) " " INT32_FORMAT_W(8) " " 1193 "]", 1194 VM_Operation::name(_current_type), 1195 _nof_threads, 1196 _nof_running); 1197 ls.print("[ " 1198 INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " " 1199 INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " ]", 1200 (int64_t)(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns), 1201 (int64_t)(_last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns), 1202 (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns), 1203 (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_begin_time_ns)); 1204 1205 ls.print_cr(INT32_FORMAT_W(16), _page_trap); 1206 } 1207 1208 // This method will be called when VM exits. This tries to summarize the sampling. 1209 // Current thread may already be deleted, so don't use ResourceMark. 1210 void SafepointTracing::statistics_exit_log() { 1211 if (!log_is_enabled(Info, safepoint, stats)) { 1212 return; 1213 } 1214 for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) { 1215 if (_op_count[index] != 0) { 1216 log_info(safepoint, stats)("%-28s" UINT64_FORMAT_W(10), VM_Operation::name(index), 1217 _op_count[index]); 1218 } 1219 } 1220 1221 log_info(safepoint, stats)("VM operations coalesced during safepoint " INT64_FORMAT, 1222 VMThread::get_coalesced_count()); 1223 log_info(safepoint, stats)("Maximum sync time " INT64_FORMAT" ns", 1224 (int64_t)(_max_sync_time)); 1225 log_info(safepoint, stats)("Maximum vm operation time (except for Exit VM operation) " 1226 INT64_FORMAT " ns", 1227 (int64_t)(_max_vmop_time)); 1228 } 1229 1230 void SafepointTracing::begin(VM_Operation::VMOp_Type type) { 1231 _op_count[type]++; 1232 _current_type = type; 1233 1234 // update the time stamp to begin recording safepoint time 1235 _last_safepoint_begin_time_ns = os::javaTimeNanos(); 1236 _last_safepoint_sync_time_ns = 0; 1237 _last_safepoint_cleanup_time_ns = 0; 1238 1239 _last_app_time_ns = _last_safepoint_begin_time_ns - _last_safepoint_end_time_ns; 1240 _last_safepoint_end_time_ns = 0; 1241 1242 RuntimeService::record_safepoint_begin(_last_app_time_ns); 1243 } 1244 1245 void SafepointTracing::synchronized(int nof_threads, int nof_running, int traps) { 1246 _last_safepoint_sync_time_ns = os::javaTimeNanos(); 1247 _nof_threads = nof_threads; 1248 _nof_running = nof_running; 1249 _page_trap = traps; 1250 RuntimeService::record_safepoint_synchronized(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns); 1251 } 1252 1253 void SafepointTracing::cleanup() { 1254 _last_safepoint_cleanup_time_ns = os::javaTimeNanos(); 1255 } 1256 1257 void SafepointTracing::end() { 1258 _last_safepoint_end_time_ns = os::javaTimeNanos(); 1259 // amount of time since epoch 1260 _last_safepoint_end_time_epoch_ms = os::javaTimeMillis(); 1261 1262 if (_max_sync_time < (_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns)) { 1263 _max_sync_time = _last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns; 1264 } 1265 if (_max_vmop_time < (_last_safepoint_end_time_ns - _last_safepoint_sync_time_ns)) { 1266 _max_vmop_time = _last_safepoint_end_time_ns - _last_safepoint_sync_time_ns; 1267 } 1268 if (log_is_enabled(Info, safepoint, stats)) { 1269 statistics_log(); 1270 } 1271 1272 log_info(safepoint)( 1273 "Safepoint \"%s\", " 1274 "Time since last: " JLONG_FORMAT " ns, " 1275 "Reaching safepoint: " JLONG_FORMAT " ns, " 1276 "At safepoint: " JLONG_FORMAT " ns, " 1277 "Total: " JLONG_FORMAT " ns", 1278 VM_Operation::name(_current_type), 1279 _last_app_time_ns, 1280 _last_safepoint_cleanup_time_ns - _last_safepoint_begin_time_ns, 1281 _last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns, 1282 _last_safepoint_end_time_ns - _last_safepoint_begin_time_ns 1283 ); 1284 1285 RuntimeService::record_safepoint_end(_last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns); 1286 }