1 /* 2 * Copyright (c) 1997, 2023, 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.hpp" 27 #include "classfile/stringTable.hpp" 28 #include "classfile/symbolTable.hpp" 29 #include "code/codeCache.hpp" 30 #include "code/nmethod.hpp" 31 #include "code/pcDesc.hpp" 32 #include "code/scopeDesc.hpp" 33 #include "compiler/compilationPolicy.hpp" 34 #include "gc/shared/collectedHeap.hpp" 35 #include "gc/shared/gcLocker.hpp" 36 #include "gc/shared/oopStorage.hpp" 37 #include "gc/shared/strongRootsScope.hpp" 38 #include "gc/shared/workerThread.hpp" 39 #include "gc/shared/workerUtils.hpp" 40 #include "interpreter/interpreter.hpp" 41 #include "jfr/jfrEvents.hpp" 42 #include "logging/log.hpp" 43 #include "logging/logStream.hpp" 44 #include "memory/resourceArea.hpp" 45 #include "memory/universe.hpp" 46 #include "oops/oop.inline.hpp" 47 #include "oops/symbol.hpp" 48 #include "oops/inlineKlass.hpp" 49 #include "runtime/atomic.hpp" 50 #include "runtime/deoptimization.hpp" 51 #include "runtime/frame.inline.hpp" 52 #include "runtime/globals.hpp" 53 #include "runtime/handles.inline.hpp" 54 #include "runtime/interfaceSupport.inline.hpp" 55 #include "runtime/javaThread.inline.hpp" 56 #include "runtime/mutexLocker.hpp" 57 #include "runtime/orderAccess.hpp" 58 #include "runtime/osThread.hpp" 59 #include "runtime/safepoint.hpp" 60 #include "runtime/safepointMechanism.inline.hpp" 61 #include "runtime/signature.hpp" 62 #include "runtime/stackWatermarkSet.inline.hpp" 63 #include "runtime/stubCodeGenerator.hpp" 64 #include "runtime/stubRoutines.hpp" 65 #include "runtime/synchronizer.hpp" 66 #include "runtime/threads.hpp" 67 #include "runtime/threadSMR.hpp" 68 #include "runtime/threadWXSetters.inline.hpp" 69 #include "runtime/timerTrace.hpp" 70 #include "services/runtimeService.hpp" 71 #include "utilities/events.hpp" 72 #include "utilities/macros.hpp" 73 #include "utilities/systemMemoryBarrier.hpp" 74 75 static void post_safepoint_begin_event(EventSafepointBegin& event, 76 uint64_t safepoint_id, 77 int thread_count, 78 int critical_thread_count) { 79 if (event.should_commit()) { 80 event.set_safepointId(safepoint_id); 81 event.set_totalThreadCount(thread_count); 82 event.set_jniCriticalThreadCount(critical_thread_count); 83 event.commit(); 84 } 85 } 86 87 static void post_safepoint_cleanup_event(EventSafepointCleanup& event, uint64_t safepoint_id) { 88 if (event.should_commit()) { 89 event.set_safepointId(safepoint_id); 90 event.commit(); 91 } 92 } 93 94 static void post_safepoint_synchronize_event(EventSafepointStateSynchronization& event, 95 uint64_t safepoint_id, 96 int initial_number_of_threads, 97 int threads_waiting_to_block, 98 int iterations) { 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(checked_cast<u4>(iterations)); 104 event.commit(); 105 } 106 } 107 108 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask& event, 109 uint64_t safepoint_id, 110 const char* name) { 111 if (event.should_commit()) { 112 event.set_safepointId(safepoint_id); 113 event.set_name(name); 114 event.commit(); 115 } 116 } 117 118 static void post_safepoint_end_event(EventSafepointEnd& event, uint64_t safepoint_id) { 119 if (event.should_commit()) { 120 event.set_safepointId(safepoint_id); 121 event.commit(); 122 } 123 } 124 125 // SafepointCheck 126 SafepointStateTracker::SafepointStateTracker(uint64_t safepoint_id, bool at_safepoint) 127 : _safepoint_id(safepoint_id), _at_safepoint(at_safepoint) {} 128 129 bool SafepointStateTracker::safepoint_state_changed() { 130 return _safepoint_id != SafepointSynchronize::safepoint_id() || 131 _at_safepoint != SafepointSynchronize::is_at_safepoint(); 132 } 133 134 // -------------------------------------------------------------------------------------------------- 135 // Implementation of Safepoint begin/end 136 137 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized; 138 int SafepointSynchronize::_waiting_to_block = 0; 139 volatile uint64_t SafepointSynchronize::_safepoint_counter = 0; 140 uint64_t SafepointSynchronize::_safepoint_id = 0; 141 const uint64_t SafepointSynchronize::InactiveSafepointCounter = 0; 142 int SafepointSynchronize::_current_jni_active_count = 0; 143 144 WaitBarrier* SafepointSynchronize::_wait_barrier; 145 146 static bool timeout_error_printed = false; 147 148 // Statistic related 149 static jlong _safepoint_begin_time = 0; 150 static volatile int _nof_threads_hit_polling_page = 0; 151 152 void SafepointSynchronize::init(Thread* vmthread) { 153 // WaitBarrier should never be destroyed since we will have 154 // threads waiting on it while exiting. 155 _wait_barrier = new WaitBarrier(vmthread); 156 SafepointTracing::init(); 157 } 158 159 void SafepointSynchronize::increment_jni_active_count() { 160 assert(Thread::current()->is_VM_thread(), "Only VM thread may increment"); 161 ++_current_jni_active_count; 162 } 163 164 void SafepointSynchronize::decrement_waiting_to_block() { 165 assert(_waiting_to_block > 0, "sanity check"); 166 assert(Thread::current()->is_VM_thread(), "Only VM thread may decrement"); 167 --_waiting_to_block; 168 } 169 170 bool SafepointSynchronize::thread_not_running(ThreadSafepointState *cur_state) { 171 if (!cur_state->is_running()) { 172 // Robustness: asserted in the caller, but handle/tolerate it for release bits. 173 LogTarget(Error, safepoint) lt; 174 if (lt.is_enabled()) { 175 ResourceMark rm; 176 LogStream ls(lt); 177 ls.print("Illegal initial state detected: "); 178 cur_state->print_on(&ls); 179 } 180 return true; 181 } 182 cur_state->examine_state_of_thread(SafepointSynchronize::safepoint_counter()); 183 if (!cur_state->is_running()) { 184 return true; 185 } 186 LogTarget(Trace, safepoint) lt; 187 if (lt.is_enabled()) { 188 ResourceMark rm; 189 LogStream ls(lt); 190 cur_state->print_on(&ls); 191 } 192 return false; 193 } 194 195 #ifdef ASSERT 196 static void assert_list_is_valid(const ThreadSafepointState* tss_head, int still_running) { 197 int a = 0; 198 const ThreadSafepointState *tmp_tss = tss_head; 199 while (tmp_tss != nullptr) { 200 ++a; 201 assert(tmp_tss->is_running(), "Illegal initial state"); 202 tmp_tss = tmp_tss->get_next(); 203 } 204 assert(a == still_running, "Must be the same"); 205 } 206 #endif // ASSERT 207 208 static void back_off(int64_t start_time) { 209 // We start with fine-grained nanosleeping until a millisecond has 210 // passed, at which point we resort to plain naked_short_sleep. 211 if (os::javaTimeNanos() - start_time < NANOSECS_PER_MILLISEC) { 212 os::naked_short_nanosleep(10 * (NANOUNITS / MICROUNITS)); 213 } else { 214 os::naked_short_sleep(1); 215 } 216 } 217 218 int SafepointSynchronize::synchronize_threads(jlong safepoint_limit_time, int nof_threads, int* initial_running) 219 { 220 JavaThreadIteratorWithHandle jtiwh; 221 222 #ifdef ASSERT 223 for (; JavaThread *cur = jtiwh.next(); ) { 224 assert(cur->safepoint_state()->is_running(), "Illegal initial state"); 225 } 226 jtiwh.rewind(); 227 #endif // ASSERT 228 229 // Iterate through all threads until it has been determined how to stop them all at a safepoint. 230 int still_running = nof_threads; 231 ThreadSafepointState *tss_head = nullptr; 232 ThreadSafepointState **p_prev = &tss_head; 233 for (; JavaThread *cur = jtiwh.next(); ) { 234 ThreadSafepointState *cur_tss = cur->safepoint_state(); 235 assert(cur_tss->get_next() == nullptr, "Must be null"); 236 if (thread_not_running(cur_tss)) { 237 --still_running; 238 } else { 239 *p_prev = cur_tss; 240 p_prev = cur_tss->next_ptr(); 241 } 242 } 243 *p_prev = nullptr; 244 245 DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);) 246 247 *initial_running = still_running; 248 249 // If there is no thread still running, we are already done. 250 if (still_running <= 0) { 251 assert(tss_head == nullptr, "Must be empty"); 252 return 1; 253 } 254 255 int iterations = 1; // The first iteration is above. 256 int64_t start_time = os::javaTimeNanos(); 257 258 do { 259 // Check if this has taken too long: 260 if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) { 261 print_safepoint_timeout(); 262 } 263 264 p_prev = &tss_head; 265 ThreadSafepointState *cur_tss = tss_head; 266 while (cur_tss != nullptr) { 267 assert(cur_tss->is_running(), "Illegal initial state"); 268 if (thread_not_running(cur_tss)) { 269 --still_running; 270 *p_prev = nullptr; 271 ThreadSafepointState *tmp = cur_tss; 272 cur_tss = cur_tss->get_next(); 273 tmp->set_next(nullptr); 274 } else { 275 *p_prev = cur_tss; 276 p_prev = cur_tss->next_ptr(); 277 cur_tss = cur_tss->get_next(); 278 } 279 } 280 281 DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);) 282 283 if (still_running > 0) { 284 back_off(start_time); 285 } 286 287 iterations++; 288 } while (still_running > 0); 289 290 assert(tss_head == nullptr, "Must be empty"); 291 292 return iterations; 293 } 294 295 void SafepointSynchronize::arm_safepoint() { 296 // Begin the process of bringing the system to a safepoint. 297 // Java threads can be in several different states and are 298 // stopped by different mechanisms: 299 // 300 // 1. Running interpreted 301 // When executing branching/returning byte codes interpreter 302 // checks if the poll is armed, if so blocks in SS::block(). 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 // Arming the per thread poll while having _state != _not_synchronized means safepointing 338 log_trace(safepoint)("Setting thread local yield flag for threads"); 339 OrderAccess::storestore(); // storestore, global state -> local state 340 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) { 341 // Make sure the threads start polling, it is time to yield. 342 SafepointMechanism::arm_local_poll(cur); 343 } 344 if (UseSystemMemoryBarrier) { 345 SystemMemoryBarrier::emit(); // storestore|storeload, global state -> local state 346 } else { 347 OrderAccess::fence(); // storestore|storeload, global state -> local state 348 } 349 } 350 351 // Roll all threads forward to a safepoint and suspend them all 352 void SafepointSynchronize::begin() { 353 assert(Thread::current()->is_VM_thread(), "Only VM thread may execute a safepoint"); 354 355 EventSafepointBegin begin_event; 356 SafepointTracing::begin(VMThread::vm_op_type()); 357 358 Universe::heap()->safepoint_synchronize_begin(); 359 360 // By getting the Threads_lock, we assure that no threads are about to start or 361 // exit. It is released again in SafepointSynchronize::end(). 362 Threads_lock->lock(); 363 364 assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state"); 365 366 int nof_threads = Threads::number_of_threads(); 367 368 _nof_threads_hit_polling_page = 0; 369 370 log_debug(safepoint)("Safepoint synchronization initiated using %s wait barrier. (%d threads)", _wait_barrier->description(), nof_threads); 371 372 // Reset the count of active JNI critical threads 373 _current_jni_active_count = 0; 374 375 // Set number of threads to wait for 376 _waiting_to_block = nof_threads; 377 378 jlong safepoint_limit_time = 0; 379 if (SafepointTimeout) { 380 // Set the limit time, so that it can be compared to see if this has taken 381 // too long to complete. 382 safepoint_limit_time = SafepointTracing::start_of_safepoint() + (jlong)(SafepointTimeoutDelay * NANOSECS_PER_MILLISEC); 383 timeout_error_printed = false; 384 } 385 386 EventSafepointStateSynchronization sync_event; 387 int initial_running = 0; 388 389 // Arms the safepoint, _current_jni_active_count and _waiting_to_block must be set before. 390 arm_safepoint(); 391 392 // Will spin until all threads are safe. 393 int iterations = synchronize_threads(safepoint_limit_time, nof_threads, &initial_running); 394 assert(_waiting_to_block == 0, "No thread should be running"); 395 396 #ifndef PRODUCT 397 // Mark all threads 398 if (VerifyCrossModifyFence) { 399 JavaThreadIteratorWithHandle jtiwh; 400 for (; JavaThread *cur = jtiwh.next(); ) { 401 cur->set_requires_cross_modify_fence(true); 402 } 403 } 404 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 OrderAccess::fence(); // keep read and write of _state from floating up 468 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization"); 469 470 // Change state first to _not_synchronized. 471 // No threads should see _synchronized when running. 472 _state = _not_synchronized; 473 474 // Set the next dormant (even) safepoint id. 475 assert((_safepoint_counter & 0x1) == 1, "must be odd"); 476 Atomic::release_store(&_safepoint_counter, _safepoint_counter + 1); 477 478 OrderAccess::fence(); // Keep the local state from floating up. 479 480 jtiwh.rewind(); 481 for (; JavaThread *current = jtiwh.next(); ) { 482 // Clear the visited flag to ensure that the critical counts are collected properly. 483 DEBUG_ONLY(current->reset_visited_for_critical_count(active_safepoint_counter);) 484 ThreadSafepointState* cur_state = current->safepoint_state(); 485 assert(!cur_state->is_running(), "Thread not suspended at safepoint"); 486 cur_state->restart(); // TSS _running 487 assert(cur_state->is_running(), "safepoint state has not been reset"); 488 } 489 } // ~JavaThreadIteratorWithHandle 490 491 // Release threads lock, so threads can be created/destroyed again. 492 Threads_lock->unlock(); 493 494 // Wake threads after local state is correctly set. 495 _wait_barrier->disarm(); 496 } 497 498 // Wake up all threads, so they are ready to resume execution after the safepoint 499 // operation has been carried out 500 void SafepointSynchronize::end() { 501 assert(Threads_lock->owned_by_self(), "must hold Threads_lock"); 502 EventSafepointEnd event; 503 assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint"); 504 505 disarm_safepoint(); 506 507 Universe::heap()->safepoint_synchronize_end(); 508 509 SafepointTracing::end(); 510 511 post_safepoint_end_event(event, safepoint_id()); 512 } 513 514 bool SafepointSynchronize::is_cleanup_needed() { 515 // Need a safepoint if some inline cache buffers is non-empty 516 if (StringTable::needs_rehashing()) return true; 517 if (SymbolTable::needs_rehashing()) return true; 518 return false; 519 } 520 521 class ParallelCleanupTask : public WorkerTask { 522 private: 523 SubTasksDone _subtasks; 524 bool _do_lazy_roots; 525 526 class Tracer { 527 private: 528 const char* _name; 529 EventSafepointCleanupTask _event; 530 TraceTime _timer; 531 532 public: 533 Tracer(const char* name) : 534 _name(name), 535 _event(), 536 _timer(name, TRACETIME_LOG(Info, safepoint, cleanup)) {} 537 ~Tracer() { 538 post_safepoint_cleanup_task_event(_event, SafepointSynchronize::safepoint_id(), _name); 539 } 540 }; 541 542 public: 543 ParallelCleanupTask() : 544 WorkerTask("Parallel Safepoint Cleanup"), 545 _subtasks(SafepointSynchronize::SAFEPOINT_CLEANUP_NUM_TASKS), 546 _do_lazy_roots(!VMThread::vm_operation()->skip_thread_oop_barriers() && 547 Universe::heap()->uses_stack_watermark_barrier()) {} 548 549 uint expected_num_workers() const { 550 uint workers = 0; 551 552 if (SymbolTable::rehash_table_expects_safepoint_rehashing()) { 553 workers++; 554 } 555 556 if (StringTable::rehash_table_expects_safepoint_rehashing()) { 557 workers++; 558 } 559 560 if (_do_lazy_roots) { 561 workers++; 562 } 563 564 return MAX2<uint>(1, workers); 565 } 566 567 void work(uint worker_id) { 568 // These tasks are ordered by relative length of time to execute so that potentially longer tasks start first. 569 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH)) { 570 if (SymbolTable::needs_rehashing()) { 571 Tracer t("rehashing symbol table"); 572 SymbolTable::rehash_table(); 573 } 574 } 575 576 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_STRING_TABLE_REHASH)) { 577 if (StringTable::needs_rehashing()) { 578 Tracer t("rehashing string table"); 579 StringTable::rehash_table(); 580 } 581 } 582 583 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_LAZY_ROOT_PROCESSING)) { 584 if (_do_lazy_roots) { 585 Tracer t("lazy partial thread root processing"); 586 class LazyRootClosure : public ThreadClosure { 587 public: 588 void do_thread(Thread* thread) { 589 StackWatermarkSet::start_processing(JavaThread::cast(thread), StackWatermarkKind::gc); 590 } 591 }; 592 LazyRootClosure cl; 593 Threads::java_threads_do(&cl); 594 } 595 } 596 597 if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_REQUEST_OOPSTORAGE_CLEANUP)) { 598 // Don't bother reporting event or time for this very short operation. 599 // To have any utility we'd also want to report whether needed. 600 OopStorage::trigger_cleanup_if_needed(); 601 } 602 603 _subtasks.all_tasks_claimed(); 604 } 605 }; 606 607 // Various cleaning tasks that should be done periodically at safepoints. 608 void SafepointSynchronize::do_cleanup_tasks() { 609 610 TraceTime timer("safepoint cleanup tasks", TRACETIME_LOG(Info, safepoint, cleanup)); 611 612 CollectedHeap* heap = Universe::heap(); 613 assert(heap != nullptr, "heap not initialized yet?"); 614 ParallelCleanupTask cleanup; 615 WorkerThreads* cleanup_workers = heap->safepoint_workers(); 616 const uint expected_num_workers = cleanup.expected_num_workers(); 617 if (cleanup_workers != nullptr && expected_num_workers > 1) { 618 // Parallel cleanup using GC provided thread pool. 619 const uint num_workers = MIN2(expected_num_workers, cleanup_workers->active_workers()); 620 cleanup_workers->run_task(&cleanup, num_workers); 621 } else { 622 // Serial cleanup using VMThread. 623 cleanup.work(0); 624 } 625 626 if (log_is_enabled(Debug, monitorinflation)) { 627 // The VMThread calls do_final_audit_and_print_stats() which calls 628 // audit_and_print_stats() at the Info level at VM exit time. 629 ObjectSynchronizer::audit_and_print_stats(false /* on_exit */); 630 } 631 } 632 633 // Methods for determining if a JavaThread is safepoint safe. 634 635 // False means unsafe with undetermined state. 636 // True means a determined state, but it may be an unsafe state. 637 // If called from a non-safepoint context safepoint_count MUST be InactiveSafepointCounter. 638 bool SafepointSynchronize::try_stable_load_state(JavaThreadState *state, JavaThread *thread, uint64_t safepoint_count) { 639 assert((safepoint_count != InactiveSafepointCounter && 640 Thread::current() == (Thread*)VMThread::vm_thread() && 641 SafepointSynchronize::_state != _not_synchronized) 642 || safepoint_count == InactiveSafepointCounter, "Invalid check"); 643 644 // To handle the thread_blocked state on the backedge of the WaitBarrier from 645 // previous safepoint and reading the reset value (0/InactiveSafepointCounter) we 646 // re-read state after we read thread safepoint id. The JavaThread changes its 647 // thread state from thread_blocked before resetting safepoint id to 0. 648 // This guarantees the second read will be from an updated thread state. It can 649 // either be different state making this an unsafe state or it can see blocked 650 // again. When we see blocked twice with a 0 safepoint id, either: 651 // - It is normally blocked, e.g. on Mutex, TBIVM. 652 // - It was in SS:block(), looped around to SS:block() and is blocked on the WaitBarrier. 653 // - It was in SS:block() but now on a Mutex. 654 // All of these cases are safe. 655 656 *state = thread->thread_state(); 657 OrderAccess::loadload(); 658 uint64_t sid = thread->safepoint_state()->get_safepoint_id(); // Load acquire 659 if (sid != InactiveSafepointCounter && sid != safepoint_count) { 660 // In an old safepoint, state not relevant. 661 return false; 662 } 663 return *state == thread->thread_state(); 664 } 665 666 static bool safepoint_safe_with(JavaThread *thread, JavaThreadState state) { 667 switch(state) { 668 case _thread_in_native: 669 // native threads are safe if they have no java stack or have walkable stack 670 return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable(); 671 672 case _thread_blocked: 673 // On wait_barrier or blocked. 674 // Blocked threads should already have walkable stack. 675 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable"); 676 return true; 677 678 default: 679 return false; 680 } 681 } 682 683 bool SafepointSynchronize::handshake_safe(JavaThread *thread) { 684 if (thread->is_terminated()) { 685 return true; 686 } 687 JavaThreadState stable_state; 688 if (try_stable_load_state(&stable_state, thread, InactiveSafepointCounter)) { 689 return safepoint_safe_with(thread, stable_state); 690 } 691 return false; 692 } 693 694 695 // ------------------------------------------------------------------------------------------------------- 696 // Implementation of Safepoint blocking point 697 698 void SafepointSynchronize::block(JavaThread *thread) { 699 assert(thread != nullptr, "thread must be set"); 700 701 // Threads shouldn't block if they are in the middle of printing, but... 702 ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id()); 703 704 // Only bail from the block() call if the thread is gone from the 705 // thread list; starting to exit should still block. 706 if (thread->is_terminated()) { 707 // block current thread if we come here from native code when VM is gone 708 thread->block_if_vm_exited(); 709 710 // otherwise do nothing 711 return; 712 } 713 714 JavaThreadState state = thread->thread_state(); 715 thread->frame_anchor()->make_walkable(); 716 717 uint64_t safepoint_id = SafepointSynchronize::safepoint_counter(); 718 719 // We have no idea where the VMThread is, it might even be at next safepoint. 720 // So we can miss this poll, but stop at next. 721 722 // Load dependent store, it must not pass loading of safepoint_id. 723 thread->safepoint_state()->set_safepoint_id(safepoint_id); // Release store 724 725 // This part we can skip if we notice we miss or are in a future safepoint. 726 OrderAccess::storestore(); 727 // Load in wait barrier should not float up 728 thread->set_thread_state_fence(_thread_blocked); 729 730 _wait_barrier->wait(static_cast<int>(safepoint_id)); 731 assert(_state != _synchronized, "Can't be"); 732 733 // If barrier is disarmed stop store from floating above loads in barrier. 734 OrderAccess::loadstore(); 735 thread->set_thread_state(state); 736 737 // Then we reset the safepoint id to inactive. 738 thread->safepoint_state()->reset_safepoint_id(); // Release store 739 740 OrderAccess::fence(); 741 742 guarantee(thread->safepoint_state()->get_safepoint_id() == InactiveSafepointCounter, 743 "The safepoint id should be set only in block path"); 744 745 // cross_modify_fence is done by SafepointMechanism::process_if_requested 746 // which is the only caller here. 747 } 748 749 // ------------------------------------------------------------------------------------------------------ 750 // Exception handlers 751 752 753 void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) { 754 assert(thread->thread_state() == _thread_in_Java, "should come from Java code"); 755 thread->set_thread_state(_thread_in_vm); 756 757 // Enable WXWrite: the function is called implicitly from java code. 758 MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread)); 759 760 if (log_is_enabled(Info, safepoint, stats)) { 761 Atomic::inc(&_nof_threads_hit_polling_page); 762 } 763 764 ThreadSafepointState* state = thread->safepoint_state(); 765 766 state->handle_polling_page_exception(); 767 768 thread->set_thread_state(_thread_in_Java); 769 } 770 771 772 void SafepointSynchronize::print_safepoint_timeout() { 773 if (!timeout_error_printed) { 774 timeout_error_printed = true; 775 // Print out the thread info which didn't reach the safepoint for debugging 776 // purposes (useful when there are lots of threads in the debugger). 777 LogTarget(Warning, safepoint) lt; 778 if (lt.is_enabled()) { 779 ResourceMark rm; 780 LogStream ls(lt); 781 782 ls.cr(); 783 ls.print_cr("# SafepointSynchronize::begin: Timeout detected:"); 784 ls.print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint."); 785 ls.print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:"); 786 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) { 787 if (cur_thread->safepoint_state()->is_running()) { 788 ls.print("# "); 789 cur_thread->print_on(&ls); 790 ls.cr(); 791 } 792 } 793 ls.print_cr("# SafepointSynchronize::begin: (End of list)"); 794 } 795 } 796 797 // To debug the long safepoint, specify both AbortVMOnSafepointTimeout & 798 // ShowMessageBoxOnError. 799 if (AbortVMOnSafepointTimeout && (os::elapsedTime() * MILLIUNITS > AbortVMOnSafepointTimeoutDelay)) { 800 // Send the blocking thread a signal to terminate and write an error file. 801 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) { 802 if (cur_thread->safepoint_state()->is_running()) { 803 if (!os::signal_thread(cur_thread, SIGILL, "blocking a safepoint")) { 804 break; // Could not send signal. Report fatal error. 805 } 806 // Give cur_thread a chance to report the error and terminate the VM. 807 os::naked_sleep(3000); 808 } 809 } 810 fatal("Safepoint sync time longer than %.6f ms detected when executing %s.", 811 SafepointTimeoutDelay, VMThread::vm_operation()->name()); 812 } 813 } 814 815 // ------------------------------------------------------------------------------------------------------- 816 // Implementation of ThreadSafepointState 817 818 ThreadSafepointState::ThreadSafepointState(JavaThread *thread) 819 : _at_poll_safepoint(false), _thread(thread), _safepoint_safe(false), 820 _safepoint_id(SafepointSynchronize::InactiveSafepointCounter), _next(nullptr) { 821 } 822 823 void ThreadSafepointState::create(JavaThread *thread) { 824 ThreadSafepointState *state = new ThreadSafepointState(thread); 825 thread->set_safepoint_state(state); 826 } 827 828 void ThreadSafepointState::destroy(JavaThread *thread) { 829 if (thread->safepoint_state()) { 830 delete(thread->safepoint_state()); 831 thread->set_safepoint_state(nullptr); 832 } 833 } 834 835 uint64_t ThreadSafepointState::get_safepoint_id() const { 836 return Atomic::load_acquire(&_safepoint_id); 837 } 838 839 void ThreadSafepointState::reset_safepoint_id() { 840 Atomic::release_store(&_safepoint_id, SafepointSynchronize::InactiveSafepointCounter); 841 } 842 843 void ThreadSafepointState::set_safepoint_id(uint64_t safepoint_id) { 844 Atomic::release_store(&_safepoint_id, safepoint_id); 845 } 846 847 void ThreadSafepointState::examine_state_of_thread(uint64_t safepoint_count) { 848 assert(is_running(), "better be running or just have hit safepoint poll"); 849 850 JavaThreadState stable_state; 851 if (!SafepointSynchronize::try_stable_load_state(&stable_state, _thread, safepoint_count)) { 852 // We could not get stable state of the JavaThread. 853 // Consider it running and just return. 854 return; 855 } 856 857 if (safepoint_safe_with(_thread, stable_state)) { 858 account_safe_thread(); 859 return; 860 } 861 862 // All other thread states will continue to run until they 863 // transition and self-block in state _blocked 864 // Safepoint polling in compiled code causes the Java threads to do the same. 865 // Note: new threads may require a malloc so they must be allowed to finish 866 867 assert(is_running(), "examine_state_of_thread on non-running thread"); 868 return; 869 } 870 871 void ThreadSafepointState::account_safe_thread() { 872 SafepointSynchronize::decrement_waiting_to_block(); 873 if (_thread->in_critical()) { 874 // Notice that this thread is in a critical section 875 SafepointSynchronize::increment_jni_active_count(); 876 } 877 DEBUG_ONLY(_thread->set_visited_for_critical_count(SafepointSynchronize::safepoint_counter());) 878 assert(!_safepoint_safe, "Must be unsafe before safe"); 879 _safepoint_safe = true; 880 } 881 882 void ThreadSafepointState::restart() { 883 assert(_safepoint_safe, "Must be safe before unsafe"); 884 _safepoint_safe = false; 885 } 886 887 void ThreadSafepointState::print_on(outputStream *st) const { 888 const char *s = _safepoint_safe ? "_at_safepoint" : "_running"; 889 890 st->print_cr("Thread: " INTPTR_FORMAT 891 " [0x%2x] State: %s _at_poll_safepoint %d", 892 p2i(_thread), _thread->osthread()->thread_id(), s, _at_poll_safepoint); 893 894 _thread->print_thread_state_on(st); 895 } 896 897 // --------------------------------------------------------------------------------------------------------------------- 898 899 // Process pending operation. 900 void ThreadSafepointState::handle_polling_page_exception() { 901 JavaThread* self = thread(); 902 assert(self == JavaThread::current(), "must be self"); 903 904 // Step 1: Find the nmethod from the return address 905 address real_return_addr = self->saved_exception_pc(); 906 907 CodeBlob *cb = CodeCache::find_blob(real_return_addr); 908 assert(cb != nullptr && cb->is_compiled(), "return address should be in nmethod"); 909 CompiledMethod* nm = (CompiledMethod*)cb; 910 911 // Find frame of caller 912 frame stub_fr = self->last_frame(); 913 CodeBlob* stub_cb = stub_fr.cb(); 914 assert(stub_cb->is_safepoint_stub(), "must be a safepoint stub"); 915 RegisterMap map(self, 916 RegisterMap::UpdateMap::include, 917 RegisterMap::ProcessFrames::skip, 918 RegisterMap::WalkContinuation::skip); 919 frame caller_fr = stub_fr.sender(&map); 920 921 // Should only be poll_return or poll 922 assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" ); 923 924 // This is a poll immediately before a return. The exception handling code 925 // has already had the effect of causing the return to occur, so the execution 926 // will continue immediately after the call. In addition, the oopmap at the 927 // return point does not mark the return value as an oop (if it is), so 928 // it needs a handle here to be updated. 929 if( nm->is_at_poll_return(real_return_addr) ) { 930 ResourceMark rm; 931 // See if return type is an oop. 932 Method* method = nm->method(); 933 bool return_oop = method->is_returning_oop(); 934 HandleMark hm(self); 935 GrowableArray<Handle> return_values; 936 InlineKlass* vk = nullptr; 937 if (return_oop && InlineTypeReturnedAsFields && 938 (method->result_type() == T_OBJECT)) { 939 // Check if an inline type is returned as fields 940 vk = InlineKlass::returned_inline_klass(map); 941 if (vk != nullptr) { 942 // We're at a safepoint at the return of a method that returns 943 // multiple values. We must make sure we preserve the oop values 944 // across the safepoint. 945 assert(vk == method->returns_inline_type(thread()), "bad inline klass"); 946 vk->save_oop_fields(map, return_values); 947 return_oop = false; 948 } 949 } 950 951 if (return_oop) { 952 // The oop result has been saved on the stack together with all 953 // the other registers. In order to preserve it over GCs we need 954 // to keep it in a handle. 955 oop result = caller_fr.saved_oop_result(&map); 956 assert(oopDesc::is_oop_or_null(result), "must be oop"); 957 return_values.push(Handle(self, result)); 958 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer"); 959 } 960 961 // We get here if compiled return polls found a reason to call into the VM. 962 // One condition for that is that the top frame is not yet safe to use. 963 // The following stack watermark barrier poll will catch such situations. 964 StackWatermarkSet::after_unwind(self); 965 966 // Process pending operation 967 SafepointMechanism::process_if_requested_with_exit_check(self, true /* check asyncs */); 968 969 // restore oop result, if any 970 if (return_oop) { 971 assert(return_values.length() == 1, "only one return value"); 972 caller_fr.set_saved_oop_result(&map, return_values.pop()()); 973 } else if (vk != nullptr) { 974 vk->restore_oop_results(map, return_values); 975 } 976 } 977 978 // This is a safepoint poll. Verify the return address and block. 979 else { 980 981 // verify the blob built the "return address" correctly 982 assert(real_return_addr == caller_fr.pc(), "must match"); 983 984 set_at_poll_safepoint(true); 985 // Process pending operation 986 // We never deliver an async exception at a polling point as the 987 // compiler may not have an exception handler for it (polling at 988 // a return point is ok though). We will check for a pending async 989 // exception below and deoptimize if needed. We also cannot deoptimize 990 // and still install the exception here because live registers needed 991 // during deoptimization are clobbered by the exception path. The 992 // exception will just be delivered once we get into the interpreter. 993 SafepointMechanism::process_if_requested_with_exit_check(self, false /* check asyncs */); 994 set_at_poll_safepoint(false); 995 996 if (self->has_async_exception_condition()) { 997 Deoptimization::deoptimize_frame(self, caller_fr.id()); 998 log_info(exceptions)("deferred async exception at compiled safepoint"); 999 } 1000 1001 // If an exception has been installed we must verify that the top frame wasn't deoptimized. 1002 if (self->has_pending_exception() ) { 1003 RegisterMap map(self, 1004 RegisterMap::UpdateMap::include, 1005 RegisterMap::ProcessFrames::skip, 1006 RegisterMap::WalkContinuation::skip); 1007 frame caller_fr = stub_fr.sender(&map); 1008 if (caller_fr.is_deoptimized_frame()) { 1009 // The exception path will destroy registers that are still 1010 // live and will be needed during deoptimization, so if we 1011 // have an exception now things are messed up. We only check 1012 // at this scope because for a poll return it is ok to deoptimize 1013 // while having a pending exception since the call we are returning 1014 // from already collides with exception handling registers and 1015 // so there is no issue (the exception handling path kills call 1016 // result registers but this is ok since the exception kills 1017 // the result anyway). 1018 fatal("Exception installed and deoptimization is pending"); 1019 } 1020 } 1021 } 1022 } 1023 1024 1025 // ------------------------------------------------------------------------------------------------------- 1026 // Implementation of SafepointTracing 1027 1028 jlong SafepointTracing::_last_safepoint_begin_time_ns = 0; 1029 jlong SafepointTracing::_last_safepoint_sync_time_ns = 0; 1030 jlong SafepointTracing::_last_safepoint_cleanup_time_ns = 0; 1031 jlong SafepointTracing::_last_safepoint_end_time_ns = 0; 1032 jlong SafepointTracing::_last_app_time_ns = 0; 1033 int SafepointTracing::_nof_threads = 0; 1034 int SafepointTracing::_nof_running = 0; 1035 int SafepointTracing::_page_trap = 0; 1036 VM_Operation::VMOp_Type SafepointTracing::_current_type; 1037 jlong SafepointTracing::_max_sync_time = 0; 1038 jlong SafepointTracing::_max_cleanup_time = 0; 1039 jlong SafepointTracing::_max_vmop_time = 0; 1040 uint64_t SafepointTracing::_op_count[VM_Operation::VMOp_Terminating] = {0}; 1041 1042 void SafepointTracing::init() { 1043 // Application start 1044 _last_safepoint_end_time_ns = os::javaTimeNanos(); 1045 } 1046 1047 // Helper method to print the header. 1048 static void print_header(outputStream* st) { 1049 // The number of spaces is significant here, and should match the format 1050 // specifiers in print_statistics(). 1051 1052 st->print("VM Operation " 1053 "[ threads: total initial_running ]" 1054 "[ time: sync cleanup vmop total ]"); 1055 1056 st->print_cr(" page_trap_count"); 1057 } 1058 1059 // This prints a nice table. To get the statistics to not shift due to the logging uptime 1060 // decorator, use the option as: -Xlog:safepoint+stats:[outputfile]:none 1061 void SafepointTracing::statistics_log() { 1062 LogTarget(Info, safepoint, stats) lt; 1063 assert (lt.is_enabled(), "should only be called when printing statistics is enabled"); 1064 LogStream ls(lt); 1065 1066 static int _cur_stat_index = 0; 1067 1068 // Print header every 30 entries 1069 if ((_cur_stat_index % 30) == 0) { 1070 print_header(&ls); 1071 _cur_stat_index = 1; // wrap 1072 } else { 1073 _cur_stat_index++; 1074 } 1075 1076 ls.print("%-28s [ " 1077 INT32_FORMAT_W(8) " " INT32_FORMAT_W(8) " " 1078 "]", 1079 VM_Operation::name(_current_type), 1080 _nof_threads, 1081 _nof_running); 1082 ls.print("[ " 1083 INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " " 1084 INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " ]", 1085 (int64_t)(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns), 1086 (int64_t)(_last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns), 1087 (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns), 1088 (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_begin_time_ns)); 1089 1090 ls.print_cr(INT32_FORMAT_W(16), _page_trap); 1091 } 1092 1093 // This method will be called when VM exits. This tries to summarize the sampling. 1094 // Current thread may already be deleted, so don't use ResourceMark. 1095 void SafepointTracing::statistics_exit_log() { 1096 if (!log_is_enabled(Info, safepoint, stats)) { 1097 return; 1098 } 1099 for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) { 1100 if (_op_count[index] != 0) { 1101 log_info(safepoint, stats)("%-28s" UINT64_FORMAT_W(10), VM_Operation::name(index), 1102 _op_count[index]); 1103 } 1104 } 1105 1106 log_info(safepoint, stats)("Maximum sync time " INT64_FORMAT" ns", 1107 (int64_t)(_max_sync_time)); 1108 log_info(safepoint, stats)("Maximum cleanup time " INT64_FORMAT" ns", 1109 (int64_t)(_max_cleanup_time)); 1110 log_info(safepoint, stats)("Maximum vm operation time (except for Exit VM operation) " 1111 INT64_FORMAT " ns", 1112 (int64_t)(_max_vmop_time)); 1113 } 1114 1115 void SafepointTracing::begin(VM_Operation::VMOp_Type type) { 1116 _op_count[type]++; 1117 _current_type = type; 1118 1119 // update the time stamp to begin recording safepoint time 1120 _last_safepoint_begin_time_ns = os::javaTimeNanos(); 1121 _last_safepoint_sync_time_ns = 0; 1122 _last_safepoint_cleanup_time_ns = 0; 1123 1124 _last_app_time_ns = _last_safepoint_begin_time_ns - _last_safepoint_end_time_ns; 1125 _last_safepoint_end_time_ns = 0; 1126 1127 RuntimeService::record_safepoint_begin(_last_app_time_ns); 1128 } 1129 1130 void SafepointTracing::synchronized(int nof_threads, int nof_running, int traps) { 1131 _last_safepoint_sync_time_ns = os::javaTimeNanos(); 1132 _nof_threads = nof_threads; 1133 _nof_running = nof_running; 1134 _page_trap = traps; 1135 RuntimeService::record_safepoint_synchronized(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns); 1136 } 1137 1138 void SafepointTracing::cleanup() { 1139 _last_safepoint_cleanup_time_ns = os::javaTimeNanos(); 1140 } 1141 1142 void SafepointTracing::end() { 1143 _last_safepoint_end_time_ns = os::javaTimeNanos(); 1144 1145 if (_max_sync_time < (_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns)) { 1146 _max_sync_time = _last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns; 1147 } 1148 if (_max_cleanup_time < (_last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns)) { 1149 _max_cleanup_time = _last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns; 1150 } 1151 if (_max_vmop_time < (_last_safepoint_end_time_ns - _last_safepoint_sync_time_ns)) { 1152 _max_vmop_time = _last_safepoint_end_time_ns - _last_safepoint_sync_time_ns; 1153 } 1154 if (log_is_enabled(Info, safepoint, stats)) { 1155 statistics_log(); 1156 } 1157 1158 log_info(safepoint)( 1159 "Safepoint \"%s\", " 1160 "Time since last: " JLONG_FORMAT " ns, " 1161 "Reaching safepoint: " JLONG_FORMAT " ns, " 1162 "Cleanup: " JLONG_FORMAT " ns, " 1163 "At safepoint: " JLONG_FORMAT " ns, " 1164 "Total: " JLONG_FORMAT " ns", 1165 VM_Operation::name(_current_type), 1166 _last_app_time_ns, 1167 _last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns, 1168 _last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns, 1169 _last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns, 1170 _last_safepoint_end_time_ns - _last_safepoint_begin_time_ns 1171 ); 1172 1173 RuntimeService::record_safepoint_end(_last_safepoint_end_time_ns - _last_safepoint_sync_time_ns); 1174 }