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