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/icBuffer.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "code/scopeDesc.hpp"
  34 #include "compiler/compilationPolicy.hpp"
  35 #include "gc/shared/collectedHeap.hpp"
  36 #include "gc/shared/gcLocker.hpp"
  37 #include "gc/shared/oopStorage.hpp"
  38 #include "gc/shared/strongRootsScope.hpp"
  39 #include "gc/shared/workerThread.hpp"
  40 #include "gc/shared/workerUtils.hpp"
  41 #include "interpreter/interpreter.hpp"
  42 #include "jfr/jfrEvents.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/oop.inline.hpp"
  48 #include "oops/symbol.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 (!InlineCacheBuffer::is_empty()) return true;
 517   if (StringTable::needs_rehashing()) return true;
 518   if (SymbolTable::needs_rehashing()) return true;
 519   return false;
 520 }
 521 
 522 class ParallelCleanupTask : public WorkerTask {
 523 private:
 524   SubTasksDone _subtasks;
 525   bool _do_lazy_roots;
 526 
 527   class Tracer {
 528   private:
 529     const char*               _name;
 530     EventSafepointCleanupTask _event;
 531     TraceTime                 _timer;
 532 
 533   public:
 534     Tracer(const char* name) :
 535         _name(name),
 536         _event(),
 537         _timer(name, TRACETIME_LOG(Info, safepoint, cleanup)) {}
 538     ~Tracer() {
 539       post_safepoint_cleanup_task_event(_event, SafepointSynchronize::safepoint_id(), _name);
 540     }
 541   };
 542 
 543 public:
 544   ParallelCleanupTask() :
 545     WorkerTask("Parallel Safepoint Cleanup"),
 546     _subtasks(SafepointSynchronize::SAFEPOINT_CLEANUP_NUM_TASKS),
 547     _do_lazy_roots(!VMThread::vm_operation()->skip_thread_oop_barriers() &&
 548                    Universe::heap()->uses_stack_watermark_barrier()) {}
 549 
 550   uint expected_num_workers() const {
 551     uint workers = 0;
 552 
 553     if (SymbolTable::rehash_table_expects_safepoint_rehashing()) {
 554       workers++;
 555     }
 556 
 557     if (StringTable::rehash_table_expects_safepoint_rehashing()) {
 558       workers++;
 559     }
 560 
 561     if (InlineCacheBuffer::needs_update_inline_caches()) {
 562       workers++;
 563     }
 564 
 565     if (_do_lazy_roots) {
 566       workers++;
 567     }
 568 
 569     return MAX2<uint>(1, workers);
 570   }
 571 
 572   void work(uint worker_id) {
 573     // These tasks are ordered by relative length of time to execute so that potentially longer tasks start first.
 574     if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH)) {
 575       if (SymbolTable::needs_rehashing()) {
 576         Tracer t("rehashing symbol table");
 577         SymbolTable::rehash_table();
 578       }
 579     }
 580 
 581     if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_STRING_TABLE_REHASH)) {
 582       if (StringTable::needs_rehashing()) {
 583         Tracer t("rehashing string table");
 584         StringTable::rehash_table();
 585       }
 586     }
 587 
 588     if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_LAZY_ROOT_PROCESSING)) {
 589       if (_do_lazy_roots) {
 590         Tracer t("lazy partial thread root processing");
 591         class LazyRootClosure : public ThreadClosure {
 592         public:
 593           void do_thread(Thread* thread) {
 594             StackWatermarkSet::start_processing(JavaThread::cast(thread), StackWatermarkKind::gc);
 595           }
 596         };
 597         LazyRootClosure cl;
 598         Threads::java_threads_do(&cl);
 599       }
 600     }
 601 
 602     if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_UPDATE_INLINE_CACHES)) {
 603       Tracer t("updating inline caches");
 604       InlineCacheBuffer::update_inline_caches();
 605     }
 606 
 607     if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_REQUEST_OOPSTORAGE_CLEANUP)) {
 608       // Don't bother reporting event or time for this very short operation.
 609       // To have any utility we'd also want to report whether needed.
 610       OopStorage::trigger_cleanup_if_needed();
 611     }
 612 
 613     _subtasks.all_tasks_claimed();
 614   }
 615 };
 616 
 617 // Various cleaning tasks that should be done periodically at safepoints.
 618 void SafepointSynchronize::do_cleanup_tasks() {
 619 
 620   TraceTime timer("safepoint cleanup tasks", TRACETIME_LOG(Info, safepoint, cleanup));
 621 
 622   CollectedHeap* heap = Universe::heap();
 623   assert(heap != nullptr, "heap not initialized yet?");
 624   ParallelCleanupTask cleanup;
 625   WorkerThreads* cleanup_workers = heap->safepoint_workers();
 626   const uint expected_num_workers = cleanup.expected_num_workers();
 627   if (cleanup_workers != nullptr && expected_num_workers > 1) {
 628     // Parallel cleanup using GC provided thread pool.
 629     const uint num_workers = MIN2(expected_num_workers, cleanup_workers->active_workers());
 630     cleanup_workers->run_task(&cleanup, num_workers);
 631   } else {
 632     // Serial cleanup using VMThread.
 633     cleanup.work(0);
 634   }
 635 
 636   assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
 637 
 638   if (log_is_enabled(Debug, monitorinflation)) {
 639     // The VMThread calls do_final_audit_and_print_stats() which calls
 640     // audit_and_print_stats() at the Info level at VM exit time.
 641     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
 642   }
 643 }
 644 
 645 // Methods for determining if a JavaThread is safepoint safe.
 646 
 647 // False means unsafe with undetermined state.
 648 // True means a determined state, but it may be an unsafe state.
 649 // If called from a non-safepoint context safepoint_count MUST be InactiveSafepointCounter.
 650 bool SafepointSynchronize::try_stable_load_state(JavaThreadState *state, JavaThread *thread, uint64_t safepoint_count) {
 651   assert((safepoint_count != InactiveSafepointCounter &&
 652           Thread::current() == (Thread*)VMThread::vm_thread() &&
 653           SafepointSynchronize::_state != _not_synchronized)
 654          || safepoint_count == InactiveSafepointCounter, "Invalid check");
 655 
 656   // To handle the thread_blocked state on the backedge of the WaitBarrier from
 657   // previous safepoint and reading the reset value (0/InactiveSafepointCounter) we
 658   // re-read state after we read thread safepoint id. The JavaThread changes its
 659   // thread state from thread_blocked before resetting safepoint id to 0.
 660   // This guarantees the second read will be from an updated thread state. It can
 661   // either be different state making this an unsafe state or it can see blocked
 662   // again. When we see blocked twice with a 0 safepoint id, either:
 663   // - It is normally blocked, e.g. on Mutex, TBIVM.
 664   // - It was in SS:block(), looped around to SS:block() and is blocked on the WaitBarrier.
 665   // - It was in SS:block() but now on a Mutex.
 666   // All of these cases are safe.
 667 
 668   *state = thread->thread_state();
 669   OrderAccess::loadload();
 670   uint64_t sid = thread->safepoint_state()->get_safepoint_id();  // Load acquire
 671   if (sid != InactiveSafepointCounter && sid != safepoint_count) {
 672     // In an old safepoint, state not relevant.
 673     return false;
 674   }
 675   return *state == thread->thread_state();
 676 }
 677 
 678 static bool safepoint_safe_with(JavaThread *thread, JavaThreadState state) {
 679   switch(state) {
 680   case _thread_in_native:
 681     // native threads are safe if they have no java stack or have walkable stack
 682     return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
 683 
 684   case _thread_blocked:
 685     // On wait_barrier or blocked.
 686     // Blocked threads should already have walkable stack.
 687     assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
 688     return true;
 689 
 690   default:
 691     return false;
 692   }
 693 }
 694 
 695 bool SafepointSynchronize::handshake_safe(JavaThread *thread) {
 696   if (thread->is_terminated()) {
 697     return true;
 698   }
 699   JavaThreadState stable_state;
 700   if (try_stable_load_state(&stable_state, thread, InactiveSafepointCounter)) {
 701     return safepoint_safe_with(thread, stable_state);
 702   }
 703   return false;
 704 }
 705 
 706 
 707 // -------------------------------------------------------------------------------------------------------
 708 // Implementation of Safepoint blocking point
 709 
 710 void SafepointSynchronize::block(JavaThread *thread) {
 711   assert(thread != nullptr, "thread must be set");
 712 
 713   // Threads shouldn't block if they are in the middle of printing, but...
 714   ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
 715 
 716   // Only bail from the block() call if the thread is gone from the
 717   // thread list; starting to exit should still block.
 718   if (thread->is_terminated()) {
 719      // block current thread if we come here from native code when VM is gone
 720      thread->block_if_vm_exited();
 721 
 722      // otherwise do nothing
 723      return;
 724   }
 725 
 726   JavaThreadState state = thread->thread_state();
 727   thread->frame_anchor()->make_walkable();
 728 
 729   uint64_t safepoint_id = SafepointSynchronize::safepoint_counter();
 730 
 731   // We have no idea where the VMThread is, it might even be at next safepoint.
 732   // So we can miss this poll, but stop at next.
 733 
 734   // Load dependent store, it must not pass loading of safepoint_id.
 735   thread->safepoint_state()->set_safepoint_id(safepoint_id); // Release store
 736 
 737   // This part we can skip if we notice we miss or are in a future safepoint.
 738   OrderAccess::storestore();
 739   // Load in wait barrier should not float up
 740   thread->set_thread_state_fence(_thread_blocked);
 741 
 742   _wait_barrier->wait(static_cast<int>(safepoint_id));
 743   assert(_state != _synchronized, "Can't be");
 744 
 745   // If barrier is disarmed stop store from floating above loads in barrier.
 746   OrderAccess::loadstore();
 747   thread->set_thread_state(state);
 748 
 749   // Then we reset the safepoint id to inactive.
 750   thread->safepoint_state()->reset_safepoint_id(); // Release store
 751 
 752   OrderAccess::fence();
 753 
 754   guarantee(thread->safepoint_state()->get_safepoint_id() == InactiveSafepointCounter,
 755             "The safepoint id should be set only in block path");
 756 
 757   // cross_modify_fence is done by SafepointMechanism::process_if_requested
 758   // which is the only caller here.
 759 }
 760 
 761 // ------------------------------------------------------------------------------------------------------
 762 // Exception handlers
 763 
 764 
 765 void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
 766   assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
 767   thread->set_thread_state(_thread_in_vm);
 768 
 769   // Enable WXWrite: the function is called implicitly from java code.
 770   MACOS_AARCH64_ONLY(ThreadWXEnable wx(WXWrite, thread));
 771 
 772   if (log_is_enabled(Info, safepoint, stats)) {
 773     Atomic::inc(&_nof_threads_hit_polling_page);
 774   }
 775 
 776   ThreadSafepointState* state = thread->safepoint_state();
 777 
 778   state->handle_polling_page_exception();
 779 
 780   thread->set_thread_state(_thread_in_Java);
 781 }
 782 
 783 
 784 void SafepointSynchronize::print_safepoint_timeout() {
 785   if (!timeout_error_printed) {
 786     timeout_error_printed = true;
 787     // Print out the thread info which didn't reach the safepoint for debugging
 788     // purposes (useful when there are lots of threads in the debugger).
 789     LogTarget(Warning, safepoint) lt;
 790     if (lt.is_enabled()) {
 791       ResourceMark rm;
 792       LogStream ls(lt);
 793 
 794       ls.cr();
 795       ls.print_cr("# SafepointSynchronize::begin: Timeout detected:");
 796       ls.print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
 797       ls.print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
 798       for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {
 799         if (cur_thread->safepoint_state()->is_running()) {
 800           ls.print("# ");
 801           cur_thread->print_on(&ls);
 802           ls.cr();
 803         }
 804       }
 805       ls.print_cr("# SafepointSynchronize::begin: (End of list)");
 806     }
 807   }
 808 
 809   // To debug the long safepoint, specify both AbortVMOnSafepointTimeout &
 810   // ShowMessageBoxOnError.
 811   if (AbortVMOnSafepointTimeout && (os::elapsedTime() * MILLIUNITS > AbortVMOnSafepointTimeoutDelay)) {
 812     // Send the blocking thread a signal to terminate and write an error file.
 813     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {
 814       if (cur_thread->safepoint_state()->is_running()) {
 815         if (!os::signal_thread(cur_thread, SIGILL, "blocking a safepoint")) {
 816           break; // Could not send signal. Report fatal error.
 817         }
 818         // Give cur_thread a chance to report the error and terminate the VM.
 819         os::naked_sleep(3000);
 820       }
 821     }
 822     fatal("Safepoint sync time longer than %.6f ms detected when executing %s.",
 823           SafepointTimeoutDelay, VMThread::vm_operation()->name());
 824   }
 825 }
 826 
 827 // -------------------------------------------------------------------------------------------------------
 828 // Implementation of ThreadSafepointState
 829 
 830 ThreadSafepointState::ThreadSafepointState(JavaThread *thread)
 831   : _at_poll_safepoint(false), _thread(thread), _safepoint_safe(false),
 832     _safepoint_id(SafepointSynchronize::InactiveSafepointCounter), _next(nullptr) {
 833 }
 834 
 835 void ThreadSafepointState::create(JavaThread *thread) {
 836   ThreadSafepointState *state = new ThreadSafepointState(thread);
 837   thread->set_safepoint_state(state);
 838 }
 839 
 840 void ThreadSafepointState::destroy(JavaThread *thread) {
 841   if (thread->safepoint_state()) {
 842     delete(thread->safepoint_state());
 843     thread->set_safepoint_state(nullptr);
 844   }
 845 }
 846 
 847 uint64_t ThreadSafepointState::get_safepoint_id() const {
 848   return Atomic::load_acquire(&_safepoint_id);
 849 }
 850 
 851 void ThreadSafepointState::reset_safepoint_id() {
 852   Atomic::release_store(&_safepoint_id, SafepointSynchronize::InactiveSafepointCounter);
 853 }
 854 
 855 void ThreadSafepointState::set_safepoint_id(uint64_t safepoint_id) {
 856   Atomic::release_store(&_safepoint_id, safepoint_id);
 857 }
 858 
 859 void ThreadSafepointState::examine_state_of_thread(uint64_t safepoint_count) {
 860   assert(is_running(), "better be running or just have hit safepoint poll");
 861 
 862   JavaThreadState stable_state;
 863   if (!SafepointSynchronize::try_stable_load_state(&stable_state, _thread, safepoint_count)) {
 864     // We could not get stable state of the JavaThread.
 865     // Consider it running and just return.
 866     return;
 867   }
 868 
 869   if (safepoint_safe_with(_thread, stable_state)) {
 870     account_safe_thread();
 871     return;
 872   }
 873 
 874   // All other thread states will continue to run until they
 875   // transition and self-block in state _blocked
 876   // Safepoint polling in compiled code causes the Java threads to do the same.
 877   // Note: new threads may require a malloc so they must be allowed to finish
 878 
 879   assert(is_running(), "examine_state_of_thread on non-running thread");
 880   return;
 881 }
 882 
 883 void ThreadSafepointState::account_safe_thread() {
 884   SafepointSynchronize::decrement_waiting_to_block();
 885   if (_thread->in_critical()) {
 886     // Notice that this thread is in a critical section
 887     SafepointSynchronize::increment_jni_active_count();
 888   }
 889   DEBUG_ONLY(_thread->set_visited_for_critical_count(SafepointSynchronize::safepoint_counter());)
 890   assert(!_safepoint_safe, "Must be unsafe before safe");
 891   _safepoint_safe = true;
 892 }
 893 
 894 void ThreadSafepointState::restart() {
 895   assert(_safepoint_safe, "Must be safe before unsafe");
 896   _safepoint_safe = false;
 897 }
 898 
 899 void ThreadSafepointState::print_on(outputStream *st) const {
 900   const char *s = _safepoint_safe ? "_at_safepoint" : "_running";
 901 
 902   st->print_cr("Thread: " INTPTR_FORMAT
 903               "  [0x%2x] State: %s _at_poll_safepoint %d",
 904                p2i(_thread), _thread->osthread()->thread_id(), s, _at_poll_safepoint);
 905 
 906   _thread->print_thread_state_on(st);
 907 }
 908 
 909 // ---------------------------------------------------------------------------------------------------------------------
 910 
 911 // Process pending operation.
 912 void ThreadSafepointState::handle_polling_page_exception() {
 913   JavaThread* self = thread();
 914   assert(self == JavaThread::current(), "must be self");
 915 
 916   // Step 1: Find the nmethod from the return address
 917   address real_return_addr = self->saved_exception_pc();
 918 
 919   CodeBlob *cb = CodeCache::find_blob(real_return_addr);
 920   assert(cb != nullptr && cb->is_compiled(), "return address should be in nmethod");
 921   CompiledMethod* nm = (CompiledMethod*)cb;
 922 
 923   // Find frame of caller
 924   frame stub_fr = self->last_frame();
 925   CodeBlob* stub_cb = stub_fr.cb();
 926   assert(stub_cb->is_safepoint_stub(), "must be a safepoint stub");
 927   RegisterMap map(self,
 928                   RegisterMap::UpdateMap::include,
 929                   RegisterMap::ProcessFrames::skip,
 930                   RegisterMap::WalkContinuation::skip);
 931   frame caller_fr = stub_fr.sender(&map);
 932 
 933   // Should only be poll_return or poll
 934   assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" );
 935 
 936   // This is a poll immediately before a return. The exception handling code
 937   // has already had the effect of causing the return to occur, so the execution
 938   // will continue immediately after the call. In addition, the oopmap at the
 939   // return point does not mark the return value as an oop (if it is), so
 940   // it needs a handle here to be updated.
 941   if( nm->is_at_poll_return(real_return_addr) ) {
 942     // See if return type is an oop.
 943     bool return_oop = nm->method()->is_returning_oop();
 944     HandleMark hm(self);
 945     Handle return_value;
 946     if (return_oop) {
 947       // The oop result has been saved on the stack together with all
 948       // the other registers. In order to preserve it over GCs we need
 949       // to keep it in a handle.
 950       oop result = caller_fr.saved_oop_result(&map);
 951       assert(oopDesc::is_oop_or_null(result), "must be oop");
 952       return_value = Handle(self, result);
 953       assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
 954     }
 955 
 956     // We get here if compiled return polls found a reason to call into the VM.
 957     // One condition for that is that the top frame is not yet safe to use.
 958     // The following stack watermark barrier poll will catch such situations.
 959     StackWatermarkSet::after_unwind(self);
 960 
 961     // Process pending operation
 962     SafepointMechanism::process_if_requested_with_exit_check(self, true /* check asyncs */);
 963 
 964     // restore oop result, if any
 965     if (return_oop) {
 966       caller_fr.set_saved_oop_result(&map, return_value());
 967     }
 968   }
 969 
 970   // This is a safepoint poll. Verify the return address and block.
 971   else {
 972 
 973     // verify the blob built the "return address" correctly
 974     assert(real_return_addr == caller_fr.pc(), "must match");
 975 
 976     set_at_poll_safepoint(true);
 977     // Process pending operation
 978     // We never deliver an async exception at a polling point as the
 979     // compiler may not have an exception handler for it (polling at
 980     // a return point is ok though). We will check for a pending async
 981     // exception below and deoptimize if needed. We also cannot deoptimize
 982     // and still install the exception here because live registers needed
 983     // during deoptimization are clobbered by the exception path. The
 984     // exception will just be delivered once we get into the interpreter.
 985     SafepointMechanism::process_if_requested_with_exit_check(self, false /* check asyncs */);
 986     set_at_poll_safepoint(false);
 987 
 988     if (self->has_async_exception_condition()) {
 989       Deoptimization::deoptimize_frame(self, caller_fr.id());
 990       log_info(exceptions)("deferred async exception at compiled safepoint");
 991     }
 992 
 993     // If an exception has been installed we must verify that the top frame wasn't deoptimized.
 994     if (self->has_pending_exception() ) {
 995       RegisterMap map(self,
 996                       RegisterMap::UpdateMap::include,
 997                       RegisterMap::ProcessFrames::skip,
 998                       RegisterMap::WalkContinuation::skip);
 999       frame caller_fr = stub_fr.sender(&map);
1000       if (caller_fr.is_deoptimized_frame()) {
1001         // The exception path will destroy registers that are still
1002         // live and will be needed during deoptimization, so if we
1003         // have an exception now things are messed up. We only check
1004         // at this scope because for a poll return it is ok to deoptimize
1005         // while having a pending exception since the call we are returning
1006         // from already collides with exception handling registers and
1007         // so there is no issue (the exception handling path kills call
1008         // result registers but this is ok since the exception kills
1009         // the result anyway).
1010         fatal("Exception installed and deoptimization is pending");
1011       }
1012     }
1013   }
1014 }
1015 
1016 
1017 // -------------------------------------------------------------------------------------------------------
1018 // Implementation of SafepointTracing
1019 
1020 jlong SafepointTracing::_last_safepoint_begin_time_ns = 0;
1021 jlong SafepointTracing::_last_safepoint_sync_time_ns = 0;
1022 jlong SafepointTracing::_last_safepoint_cleanup_time_ns = 0;
1023 jlong SafepointTracing::_last_safepoint_end_time_ns = 0;
1024 jlong SafepointTracing::_last_app_time_ns = 0;
1025 int SafepointTracing::_nof_threads = 0;
1026 int SafepointTracing::_nof_running = 0;
1027 int SafepointTracing::_page_trap = 0;
1028 VM_Operation::VMOp_Type SafepointTracing::_current_type;
1029 jlong     SafepointTracing::_max_sync_time = 0;
1030 jlong     SafepointTracing::_max_cleanup_time = 0;
1031 jlong     SafepointTracing::_max_vmop_time = 0;
1032 uint64_t  SafepointTracing::_op_count[VM_Operation::VMOp_Terminating] = {0};
1033 
1034 void SafepointTracing::init() {
1035   // Application start
1036   _last_safepoint_end_time_ns = os::javaTimeNanos();
1037 }
1038 
1039 // Helper method to print the header.
1040 static void print_header(outputStream* st) {
1041   // The number of spaces is significant here, and should match the format
1042   // specifiers in print_statistics().
1043 
1044   st->print("VM Operation                 "
1045             "[ threads: total initial_running ]"
1046             "[ time:       sync    cleanup       vmop      total ]");
1047 
1048   st->print_cr(" page_trap_count");
1049 }
1050 
1051 // This prints a nice table.  To get the statistics to not shift due to the logging uptime
1052 // decorator, use the option as: -Xlog:safepoint+stats:[outputfile]:none
1053 void SafepointTracing::statistics_log() {
1054   LogTarget(Info, safepoint, stats) lt;
1055   assert (lt.is_enabled(), "should only be called when printing statistics is enabled");
1056   LogStream ls(lt);
1057 
1058   static int _cur_stat_index = 0;
1059 
1060   // Print header every 30 entries
1061   if ((_cur_stat_index % 30) == 0) {
1062     print_header(&ls);
1063     _cur_stat_index = 1;  // wrap
1064   } else {
1065     _cur_stat_index++;
1066   }
1067 
1068   ls.print("%-28s [       "
1069            INT32_FORMAT_W(8) "        " INT32_FORMAT_W(8) " "
1070            "]",
1071            VM_Operation::name(_current_type),
1072            _nof_threads,
1073            _nof_running);
1074   ls.print("[       "
1075            INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " "
1076            INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " ]",
1077            (int64_t)(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns),
1078            (int64_t)(_last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns),
1079            (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns),
1080            (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_begin_time_ns));
1081 
1082   ls.print_cr(INT32_FORMAT_W(16), _page_trap);
1083 }
1084 
1085 // This method will be called when VM exits. This tries to summarize the sampling.
1086 // Current thread may already be deleted, so don't use ResourceMark.
1087 void SafepointTracing::statistics_exit_log() {
1088   if (!log_is_enabled(Info, safepoint, stats)) {
1089     return;
1090   }
1091   for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) {
1092     if (_op_count[index] != 0) {
1093       log_info(safepoint, stats)("%-28s" UINT64_FORMAT_W(10), VM_Operation::name(index),
1094                _op_count[index]);
1095     }
1096   }
1097 
1098   log_info(safepoint, stats)("Maximum sync time  " INT64_FORMAT" ns",
1099                               (int64_t)(_max_sync_time));
1100   log_info(safepoint, stats)("Maximum cleanup time  " INT64_FORMAT" ns",
1101                               (int64_t)(_max_cleanup_time));
1102   log_info(safepoint, stats)("Maximum vm operation time (except for Exit VM operation)  "
1103                               INT64_FORMAT " ns",
1104                               (int64_t)(_max_vmop_time));
1105 }
1106 
1107 void SafepointTracing::begin(VM_Operation::VMOp_Type type) {
1108   _op_count[type]++;
1109   _current_type = type;
1110 
1111   // update the time stamp to begin recording safepoint time
1112   _last_safepoint_begin_time_ns = os::javaTimeNanos();
1113   _last_safepoint_sync_time_ns = 0;
1114   _last_safepoint_cleanup_time_ns = 0;
1115 
1116   _last_app_time_ns = _last_safepoint_begin_time_ns - _last_safepoint_end_time_ns;
1117   _last_safepoint_end_time_ns = 0;
1118 
1119   RuntimeService::record_safepoint_begin(_last_app_time_ns);
1120 }
1121 
1122 void SafepointTracing::synchronized(int nof_threads, int nof_running, int traps) {
1123   _last_safepoint_sync_time_ns = os::javaTimeNanos();
1124   _nof_threads = nof_threads;
1125   _nof_running = nof_running;
1126   _page_trap   = traps;
1127   RuntimeService::record_safepoint_synchronized(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns);
1128 }
1129 
1130 void SafepointTracing::cleanup() {
1131   _last_safepoint_cleanup_time_ns = os::javaTimeNanos();
1132 }
1133 
1134 void SafepointTracing::end() {
1135   _last_safepoint_end_time_ns = os::javaTimeNanos();
1136 
1137   if (_max_sync_time < (_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns)) {
1138     _max_sync_time = _last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns;
1139   }
1140   if (_max_cleanup_time < (_last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns)) {
1141     _max_cleanup_time = _last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns;
1142   }
1143   if (_max_vmop_time < (_last_safepoint_end_time_ns - _last_safepoint_sync_time_ns)) {
1144     _max_vmop_time = _last_safepoint_end_time_ns - _last_safepoint_sync_time_ns;
1145   }
1146   if (log_is_enabled(Info, safepoint, stats)) {
1147     statistics_log();
1148   }
1149 
1150   log_info(safepoint)(
1151      "Safepoint \"%s\", "
1152      "Time since last: " JLONG_FORMAT " ns, "
1153      "Reaching safepoint: " JLONG_FORMAT " ns, "
1154      "Cleanup: " JLONG_FORMAT " ns, "
1155      "At safepoint: " JLONG_FORMAT " ns, "
1156      "Total: " JLONG_FORMAT " ns",
1157       VM_Operation::name(_current_type),
1158       _last_app_time_ns,
1159       _last_safepoint_sync_time_ns    - _last_safepoint_begin_time_ns,
1160       _last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns,
1161       _last_safepoint_end_time_ns     - _last_safepoint_cleanup_time_ns,
1162       _last_safepoint_end_time_ns     - _last_safepoint_begin_time_ns
1163      );
1164 
1165   RuntimeService::record_safepoint_end(_last_safepoint_end_time_ns - _last_safepoint_sync_time_ns);
1166 }