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