< prev index next >

src/share/vm/runtime/safepoint.cpp

Print this page




  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/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/nmethod.hpp"
  31 #include "code/pcDesc.hpp"
  32 #include "code/scopeDesc.hpp"
  33 #include "gc_interface/collectedHeap.hpp"
  34 #include "interpreter/interpreter.hpp"

  35 #include "jfr/jfrEvents.hpp"

  36 #include "memory/resourceArea.hpp"
  37 #include "memory/universe.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "oops/symbol.hpp"
  40 #include "runtime/compilationPolicy.hpp"
  41 #include "runtime/deoptimization.hpp"
  42 #include "runtime/frame.inline.hpp"
  43 #include "runtime/interfaceSupport.hpp"
  44 #include "runtime/mutexLocker.hpp"
  45 #include "runtime/orderAccess.inline.hpp"
  46 #include "runtime/osThread.hpp"
  47 #include "runtime/safepoint.hpp"
  48 #include "runtime/signature.hpp"
  49 #include "runtime/stubCodeGenerator.hpp"
  50 #include "runtime/stubRoutines.hpp"
  51 #include "runtime/sweeper.hpp"
  52 #include "runtime/synchronizer.hpp"
  53 #include "runtime/thread.inline.hpp"
  54 #include "services/runtimeService.hpp"
  55 #include "utilities/events.hpp"


  73 #ifdef TARGET_ARCH_ppc
  74 # include "nativeInst_ppc.hpp"
  75 # include "vmreg_ppc.inline.hpp"
  76 #endif
  77 #if INCLUDE_ALL_GCS
  78 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
  79 #include "gc_implementation/shared/suspendibleThreadSet.hpp"
  80 #endif // INCLUDE_ALL_GCS
  81 #ifdef COMPILER1
  82 #include "c1/c1_globals.hpp"
  83 #endif
  84 
  85 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  86 
  87 template <typename E>
  88 static void set_current_safepoint_id(E* event, int adjustment = 0) {
  89   assert(event != NULL, "invariant");
  90   event->set_safepointId(SafepointSynchronize::safepoint_counter() + adjustment);
  91 }
  92 

  93 static void post_safepoint_begin_event(EventSafepointBegin* event,
  94                                        int thread_count,
  95                                        int critical_thread_count) {
  96   assert(event != NULL, "invariant");
  97   assert(event->should_commit(), "invariant");
  98   set_current_safepoint_id(event);
  99   event->set_totalThreadCount(thread_count);
 100   event->set_jniCriticalThreadCount(critical_thread_count);
 101   event->commit();
 102 }
 103 
 104 static void post_safepoint_cleanup_event(EventSafepointCleanup* event) {
 105   assert(event != NULL, "invariant");
 106   assert(event->should_commit(), "invariant");
 107   set_current_safepoint_id(event);
 108   event->commit();
 109 }
 110 
 111 static void post_safepoint_synchronize_event(EventSafepointStateSynchronization* event,
 112                                              int initial_number_of_threads,


 133 }
 134 
 135 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask* event,
 136                                               const char* name) {
 137   assert(event != NULL, "invariant");
 138   if (event->should_commit()) {
 139     set_current_safepoint_id(event);
 140     event->set_name(name);
 141     event->commit();
 142   }
 143 }
 144 
 145 static void post_safepoint_end_event(EventSafepointEnd* event) {
 146   assert(event != NULL, "invariant");
 147   if (event->should_commit()) {
 148     // Group this event together with the ones committed before the counter increased
 149     set_current_safepoint_id(event, -1);
 150     event->commit();
 151   }
 152 }

 153 
 154 // --------------------------------------------------------------------------------------------------
 155 // Implementation of Safepoint begin/end
 156 
 157 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
 158 volatile int  SafepointSynchronize::_waiting_to_block = 0;
 159 volatile int SafepointSynchronize::_safepoint_counter = 0;
 160 int SafepointSynchronize::_current_jni_active_count = 0;
 161 long  SafepointSynchronize::_end_of_last_safepoint = 0;
 162 static volatile int PageArmed = 0 ;        // safepoint polling page is RO|RW vs PROT_NONE
 163 static volatile int TryingToBlock = 0 ;    // proximate value -- for advisory use only
 164 static bool timeout_error_printed = false;
 165 
 166 // Roll all threads forward to a safepoint and suspend them all
 167 void SafepointSynchronize::begin() {

 168   EventSafepointBegin begin_event;

 169   Thread* myThread = Thread::current();
 170   assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
 171 
 172   if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
 173     _safepoint_begin_time = os::javaTimeNanos();
 174     _ts_of_current_safepoint = tty->time_stamp().seconds();
 175   }
 176 
 177 #if INCLUDE_ALL_GCS
 178   if (UseConcMarkSweepGC) {
 179     // In the future we should investigate whether CMS can use the
 180     // more-general mechanism below.  DLD (01/05).
 181     ConcurrentMarkSweepThread::synchronize(false);
 182   } else if (UseG1GC) {
 183     SuspendibleThreadSet::synchronize();
 184   }
 185 #endif // INCLUDE_ALL_GCS
 186 
 187   // By getting the Threads_lock, we assure that no threads are about to start or
 188   // exit. It is released again in SafepointSynchronize::end().


 240   //     (on MP systems).  In order to avoid the overhead of issuing
 241   //     a memory barrier for each Java thread making native calls, each Java
 242   //     thread performs a write to a single memory page after changing
 243   //     the thread state.  The VM thread performs a sequence of
 244   //     mprotect OS calls which forces all previous writes from all
 245   //     Java threads to be serialized.  This is done in the
 246   //     os::serialize_thread_states() call.  This has proven to be
 247   //     much more efficient than executing a membar instruction
 248   //     on every call to native code.
 249   //  3. Running compiled Code
 250   //     Compiled code reads a global (Safepoint Polling) page that
 251   //     is set to fault if we are trying to get to a safepoint.
 252   //  4. Blocked
 253   //     A thread which is blocked will not be allowed to return from the
 254   //     block condition until the safepoint operation is complete.
 255   //  5. In VM or Transitioning between states
 256   //     If a Java thread is currently running in the VM or transitioning
 257   //     between states, the safepointing code will wait for the thread to
 258   //     block itself when it attempts transitions to a new state.
 259   //

 260   EventSafepointStateSynchronization sync_event;

 261   int initial_running = 0;
 262 
 263   _state            = _synchronizing;
 264   OrderAccess::fence();
 265 
 266   // Flush all thread states to memory
 267   if (!UseMembar) {
 268     os::serialize_thread_states();
 269   }
 270 
 271   // Make interpreter safepoint aware
 272   Interpreter::notice_safepoints();
 273 
 274   if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
 275     // Make polling safepoint aware
 276     guarantee (PageArmed == 0, "invariant") ;
 277     PageArmed = 1 ;
 278     os::make_polling_page_unreadable();
 279   }
 280 


 393         SpinPause() ;     // MP-Polite spin
 394       } else
 395       if (steps < DeferThrSuspendLoopCount) {
 396         os::NakedYield() ;
 397       } else {
 398         os::yield_all(steps) ;
 399         // Alternately, the VM thread could transiently depress its scheduling priority or
 400         // transiently increase the priority of the tardy mutator(s).
 401       }
 402 
 403       iterations ++ ;
 404     }
 405     assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
 406   }
 407   assert(still_running == 0, "sanity check");
 408 
 409   if (PrintSafepointStatistics) {
 410     update_statistics_on_spin_end();
 411   }
 412 

 413   if (sync_event.should_commit()) {
 414     post_safepoint_synchronize_event(&sync_event, initial_running, _waiting_to_block, iterations);
 415   }

 416 
 417   // wait until all threads are stopped
 418   {

 419     EventSafepointWaitBlocked wait_blocked_event;

 420     int initial_waiting_to_block = _waiting_to_block;
 421 
 422     while (_waiting_to_block > 0) {
 423       if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block);
 424       if (!SafepointTimeout || timeout_error_printed) {
 425         Safepoint_lock->wait(true);  // true, means with no safepoint checks
 426       } else {
 427         // Compute remaining time
 428         jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
 429 
 430         // If there is no remaining time, then there is an error
 431         if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
 432           print_safepoint_timeout(_blocking_timeout);
 433         }
 434       }
 435     }
 436     assert(_waiting_to_block == 0, "sanity check");
 437 
 438 #ifndef PRODUCT
 439     if (SafepointTimeout) {
 440       jlong current_time = os::javaTimeNanos();
 441       if (safepoint_limit_time < current_time) {
 442         tty->print_cr("# SafepointSynchronize: Finished after "
 443                       INT64_FORMAT_W(6) " ms",
 444                       ((current_time - safepoint_limit_time) / MICROUNITS +
 445                        SafepointTimeoutDelay));
 446       }
 447     }
 448 #endif
 449 
 450     assert((_safepoint_counter & 0x1) == 0, "must be even");
 451     assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 452     _safepoint_counter ++;
 453 
 454     // Record state
 455     _state = _synchronized;
 456 
 457     OrderAccess::fence();
 458 

 459     if (wait_blocked_event.should_commit()) {
 460       post_safepoint_wait_blocked_event(&wait_blocked_event, initial_waiting_to_block);
 461     }

 462   }
 463 
 464 #ifdef ASSERT
 465   for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
 466     // make sure all the threads were visited
 467     assert(cur->was_visited_for_critical_count(), "missed a thread");
 468   }
 469 #endif // ASSERT
 470 
 471   // Update the count of active JNI critical regions
 472   GC_locker::set_jni_lock_count(_current_jni_active_count);
 473 
 474   if (TraceSafepoint) {
 475     VM_Operation *op = VMThread::vm_operation();
 476     tty->print_cr("Entering safepoint region: %s", (op != NULL) ? op->name() : "no vm operation");
 477   }
 478 
 479   RuntimeService::record_safepoint_synchronized();
 480   if (PrintSafepointStatistics) {
 481     update_statistics_on_sync_end(os::javaTimeNanos());
 482   }
 483 
 484   // Call stuff that needs to be run when a safepoint is just about to be completed
 485   {

 486     EventSafepointCleanup cleanup_event;

 487     do_cleanup_tasks();

 488     if (cleanup_event.should_commit()) {
 489       post_safepoint_cleanup_event(&cleanup_event);
 490     }

 491   }
 492 
 493   if (PrintSafepointStatistics) {
 494     // Record how much time spend on the above cleanup tasks
 495     update_statistics_on_cleanup_end(os::javaTimeNanos());
 496   }
 497 

 498   if (begin_event.should_commit()) {
 499     post_safepoint_begin_event(&begin_event, nof_threads, _current_jni_active_count);
 500   }

 501 }
 502 
 503 // Wake up all threads, so they are ready to resume execution after the safepoint
 504 // operation has been carried out
 505 void SafepointSynchronize::end() {
 506 
 507   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 508   assert((_safepoint_counter & 0x1) == 1, "must be odd");

 509   EventSafepointEnd event;

 510   _safepoint_counter ++;
 511   // memory fence isn't required here since an odd _safepoint_counter
 512   // value can do no harm and a fence is issued below anyway.
 513 
 514   DEBUG_ONLY(Thread* myThread = Thread::current();)
 515   assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
 516 
 517   if (PrintSafepointStatistics) {
 518     end_statistics(os::javaTimeNanos());
 519   }
 520 
 521 #ifdef ASSERT
 522   // A pending_exception cannot be installed during a safepoint.  The threads
 523   // may install an async exception after they come back from a safepoint into
 524   // pending_exception after they unblock.  But that should happen later.
 525   for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
 526     assert (!(cur->has_pending_exception() &&
 527               cur->safepoint_state()->is_at_poll_safepoint()),
 528             "safepoint installed a pending exception");
 529   }


 575     }
 576 
 577     RuntimeService::record_safepoint_end();
 578 
 579     // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
 580     // blocked in signal_thread_blocked
 581     Threads_lock->unlock();
 582 
 583   }
 584 #if INCLUDE_ALL_GCS
 585   // If there are any concurrent GC threads resume them.
 586   if (UseConcMarkSweepGC) {
 587     ConcurrentMarkSweepThread::desynchronize(false);
 588   } else if (UseG1GC) {
 589     SuspendibleThreadSet::desynchronize();
 590   }
 591 #endif // INCLUDE_ALL_GCS
 592   // record this time so VMThread can keep track how much time has elasped
 593   // since last safepoint.
 594   _end_of_last_safepoint = os::javaTimeMillis();

 595   if (event.should_commit()) {
 596     post_safepoint_end_event(&event);
 597   }

 598 }
 599 
 600 bool SafepointSynchronize::is_cleanup_needed() {
 601   // Need a safepoint if some inline cache buffers is non-empty
 602   if (!InlineCacheBuffer::is_empty()) return true;
 603   return false;
 604 }
 605 
 606 
 607 
 608 // Various cleaning tasks that should be done periodically at safepoints
 609 void SafepointSynchronize::do_cleanup_tasks() {
 610   {
 611     const char* name = "deflating idle monitors";

 612     EventSafepointCleanupTask event;

 613     TraceTime t1(name, TraceSafepointCleanupTime);
 614     ObjectSynchronizer::deflate_idle_monitors();

 615     if (event.should_commit()) {
 616       post_safepoint_cleanup_task_event(&event, name);
 617     }

 618   }
 619 
 620   {
 621     const char* name = "updating inline caches";

 622     EventSafepointCleanupTask event;

 623     TraceTime t2(name, TraceSafepointCleanupTime);
 624     InlineCacheBuffer::update_inline_caches();

 625     if (event.should_commit()) {
 626       post_safepoint_cleanup_task_event(&event, name);
 627     }

 628   }
 629   {
 630     const char* name = "compilation policy safepoint handler";

 631     EventSafepointCleanupTask event;

 632     TraceTime t3(name, TraceSafepointCleanupTime);
 633     CompilationPolicy::policy()->do_safepoint_work();

 634     if (event.should_commit()) {
 635       post_safepoint_cleanup_task_event(&event, name);
 636     }

 637   }
 638 
 639   {
 640     const char* name = "mark nmethods";

 641     EventSafepointCleanupTask event;

 642     TraceTime t4(name, TraceSafepointCleanupTime);
 643     NMethodSweeper::mark_active_nmethods();

 644     if (event.should_commit()) {
 645       post_safepoint_cleanup_task_event(&event, name);
 646     }

 647   }
 648 
 649   if (SymbolTable::needs_rehashing()) {
 650     const char* name = "rehashing symbol table";

 651     EventSafepointCleanupTask event;

 652     TraceTime t5(name, TraceSafepointCleanupTime);
 653     SymbolTable::rehash_table();

 654     if (event.should_commit()) {
 655       post_safepoint_cleanup_task_event(&event, name);
 656     }

 657   }
 658 
 659   if (StringTable::needs_rehashing()) {
 660     const char* name = "rehashing string table";

 661     EventSafepointCleanupTask event;

 662     TraceTime t6(name, TraceSafepointCleanupTime);
 663     StringTable::rehash_table();

 664     if (event.should_commit()) {
 665       post_safepoint_cleanup_task_event(&event, name);
 666     }

 667   }
 668 
 669   // rotate log files?
 670   if (UseGCLogFileRotation) {
 671     gclog_or_tty->rotate_log(false);
 672   }
 673 
 674   {
 675     // CMS delays purging the CLDG until the beginning of the next safepoint and to
 676     // make sure concurrent sweep is done
 677     TraceTime t7("purging class loader data graph", TraceSafepointCleanupTime);
 678     ClassLoaderDataGraph::purge_if_needed();
 679   }
 680 }
 681 
 682 
 683 bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
 684   switch(state) {
 685   case _thread_in_native:
 686     // native threads are safe if they have no java stack or have walkable stack




  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/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/nmethod.hpp"
  31 #include "code/pcDesc.hpp"
  32 #include "code/scopeDesc.hpp"
  33 #include "gc_interface/collectedHeap.hpp"
  34 #include "interpreter/interpreter.hpp"
  35 #if INCLUDE_JFR
  36 #include "jfr/jfrEvents.hpp"
  37 #endif
  38 #include "memory/resourceArea.hpp"
  39 #include "memory/universe.inline.hpp"
  40 #include "oops/oop.inline.hpp"
  41 #include "oops/symbol.hpp"
  42 #include "runtime/compilationPolicy.hpp"
  43 #include "runtime/deoptimization.hpp"
  44 #include "runtime/frame.inline.hpp"
  45 #include "runtime/interfaceSupport.hpp"
  46 #include "runtime/mutexLocker.hpp"
  47 #include "runtime/orderAccess.inline.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/safepoint.hpp"
  50 #include "runtime/signature.hpp"
  51 #include "runtime/stubCodeGenerator.hpp"
  52 #include "runtime/stubRoutines.hpp"
  53 #include "runtime/sweeper.hpp"
  54 #include "runtime/synchronizer.hpp"
  55 #include "runtime/thread.inline.hpp"
  56 #include "services/runtimeService.hpp"
  57 #include "utilities/events.hpp"


  75 #ifdef TARGET_ARCH_ppc
  76 # include "nativeInst_ppc.hpp"
  77 # include "vmreg_ppc.inline.hpp"
  78 #endif
  79 #if INCLUDE_ALL_GCS
  80 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
  81 #include "gc_implementation/shared/suspendibleThreadSet.hpp"
  82 #endif // INCLUDE_ALL_GCS
  83 #ifdef COMPILER1
  84 #include "c1/c1_globals.hpp"
  85 #endif
  86 
  87 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  88 
  89 template <typename E>
  90 static void set_current_safepoint_id(E* event, int adjustment = 0) {
  91   assert(event != NULL, "invariant");
  92   event->set_safepointId(SafepointSynchronize::safepoint_counter() + adjustment);
  93 }
  94 
  95 #if INCLUDE_JFR
  96 static void post_safepoint_begin_event(EventSafepointBegin* event,
  97                                        int thread_count,
  98                                        int critical_thread_count) {
  99   assert(event != NULL, "invariant");
 100   assert(event->should_commit(), "invariant");
 101   set_current_safepoint_id(event);
 102   event->set_totalThreadCount(thread_count);
 103   event->set_jniCriticalThreadCount(critical_thread_count);
 104   event->commit();
 105 }
 106 
 107 static void post_safepoint_cleanup_event(EventSafepointCleanup* event) {
 108   assert(event != NULL, "invariant");
 109   assert(event->should_commit(), "invariant");
 110   set_current_safepoint_id(event);
 111   event->commit();
 112 }
 113 
 114 static void post_safepoint_synchronize_event(EventSafepointStateSynchronization* event,
 115                                              int initial_number_of_threads,


 136 }
 137 
 138 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask* event,
 139                                               const char* name) {
 140   assert(event != NULL, "invariant");
 141   if (event->should_commit()) {
 142     set_current_safepoint_id(event);
 143     event->set_name(name);
 144     event->commit();
 145   }
 146 }
 147 
 148 static void post_safepoint_end_event(EventSafepointEnd* event) {
 149   assert(event != NULL, "invariant");
 150   if (event->should_commit()) {
 151     // Group this event together with the ones committed before the counter increased
 152     set_current_safepoint_id(event, -1);
 153     event->commit();
 154   }
 155 }
 156 #endif
 157 
 158 // --------------------------------------------------------------------------------------------------
 159 // Implementation of Safepoint begin/end
 160 
 161 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
 162 volatile int  SafepointSynchronize::_waiting_to_block = 0;
 163 volatile int SafepointSynchronize::_safepoint_counter = 0;
 164 int SafepointSynchronize::_current_jni_active_count = 0;
 165 long  SafepointSynchronize::_end_of_last_safepoint = 0;
 166 static volatile int PageArmed = 0 ;        // safepoint polling page is RO|RW vs PROT_NONE
 167 static volatile int TryingToBlock = 0 ;    // proximate value -- for advisory use only
 168 static bool timeout_error_printed = false;
 169 
 170 // Roll all threads forward to a safepoint and suspend them all
 171 void SafepointSynchronize::begin() {
 172 #if INCLUDE_JFR
 173   EventSafepointBegin begin_event;
 174 #endif
 175   Thread* myThread = Thread::current();
 176   assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
 177 
 178   if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
 179     _safepoint_begin_time = os::javaTimeNanos();
 180     _ts_of_current_safepoint = tty->time_stamp().seconds();
 181   }
 182 
 183 #if INCLUDE_ALL_GCS
 184   if (UseConcMarkSweepGC) {
 185     // In the future we should investigate whether CMS can use the
 186     // more-general mechanism below.  DLD (01/05).
 187     ConcurrentMarkSweepThread::synchronize(false);
 188   } else if (UseG1GC) {
 189     SuspendibleThreadSet::synchronize();
 190   }
 191 #endif // INCLUDE_ALL_GCS
 192 
 193   // By getting the Threads_lock, we assure that no threads are about to start or
 194   // exit. It is released again in SafepointSynchronize::end().


 246   //     (on MP systems).  In order to avoid the overhead of issuing
 247   //     a memory barrier for each Java thread making native calls, each Java
 248   //     thread performs a write to a single memory page after changing
 249   //     the thread state.  The VM thread performs a sequence of
 250   //     mprotect OS calls which forces all previous writes from all
 251   //     Java threads to be serialized.  This is done in the
 252   //     os::serialize_thread_states() call.  This has proven to be
 253   //     much more efficient than executing a membar instruction
 254   //     on every call to native code.
 255   //  3. Running compiled Code
 256   //     Compiled code reads a global (Safepoint Polling) page that
 257   //     is set to fault if we are trying to get to a safepoint.
 258   //  4. Blocked
 259   //     A thread which is blocked will not be allowed to return from the
 260   //     block condition until the safepoint operation is complete.
 261   //  5. In VM or Transitioning between states
 262   //     If a Java thread is currently running in the VM or transitioning
 263   //     between states, the safepointing code will wait for the thread to
 264   //     block itself when it attempts transitions to a new state.
 265   //
 266 #if INCLUDE_JFR
 267   EventSafepointStateSynchronization sync_event;
 268 #endif
 269   int initial_running = 0;
 270 
 271   _state            = _synchronizing;
 272   OrderAccess::fence();
 273 
 274   // Flush all thread states to memory
 275   if (!UseMembar) {
 276     os::serialize_thread_states();
 277   }
 278 
 279   // Make interpreter safepoint aware
 280   Interpreter::notice_safepoints();
 281 
 282   if (UseCompilerSafepoints && DeferPollingPageLoopCount < 0) {
 283     // Make polling safepoint aware
 284     guarantee (PageArmed == 0, "invariant") ;
 285     PageArmed = 1 ;
 286     os::make_polling_page_unreadable();
 287   }
 288 


 401         SpinPause() ;     // MP-Polite spin
 402       } else
 403       if (steps < DeferThrSuspendLoopCount) {
 404         os::NakedYield() ;
 405       } else {
 406         os::yield_all(steps) ;
 407         // Alternately, the VM thread could transiently depress its scheduling priority or
 408         // transiently increase the priority of the tardy mutator(s).
 409       }
 410 
 411       iterations ++ ;
 412     }
 413     assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
 414   }
 415   assert(still_running == 0, "sanity check");
 416 
 417   if (PrintSafepointStatistics) {
 418     update_statistics_on_spin_end();
 419   }
 420 
 421 #if INCLUDE_JFR
 422   if (sync_event.should_commit()) {
 423     post_safepoint_synchronize_event(&sync_event, initial_running, _waiting_to_block, iterations);
 424   }
 425 #endif
 426 
 427   // wait until all threads are stopped
 428   {
 429 #if INCLUDE_JFR
 430     EventSafepointWaitBlocked wait_blocked_event;
 431 #endif
 432     int initial_waiting_to_block = _waiting_to_block;
 433 
 434     while (_waiting_to_block > 0) {
 435       if (TraceSafepoint) tty->print_cr("Waiting for %d thread(s) to block", _waiting_to_block);
 436       if (!SafepointTimeout || timeout_error_printed) {
 437         Safepoint_lock->wait(true);  // true, means with no safepoint checks
 438       } else {
 439         // Compute remaining time
 440         jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
 441 
 442         // If there is no remaining time, then there is an error
 443         if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
 444           print_safepoint_timeout(_blocking_timeout);
 445         }
 446       }
 447     }
 448     assert(_waiting_to_block == 0, "sanity check");
 449 
 450 #ifndef PRODUCT
 451     if (SafepointTimeout) {
 452       jlong current_time = os::javaTimeNanos();
 453       if (safepoint_limit_time < current_time) {
 454         tty->print_cr("# SafepointSynchronize: Finished after "
 455                       INT64_FORMAT_W(6) " ms",
 456                       ((current_time - safepoint_limit_time) / MICROUNITS +
 457                        SafepointTimeoutDelay));
 458       }
 459     }
 460 #endif
 461 
 462     assert((_safepoint_counter & 0x1) == 0, "must be even");
 463     assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 464     _safepoint_counter ++;
 465 
 466     // Record state
 467     _state = _synchronized;
 468 
 469     OrderAccess::fence();
 470 
 471 #if INCLUDE_JFR
 472     if (wait_blocked_event.should_commit()) {
 473       post_safepoint_wait_blocked_event(&wait_blocked_event, initial_waiting_to_block);
 474     }
 475 #endif
 476   }
 477 
 478 #ifdef ASSERT
 479   for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
 480     // make sure all the threads were visited
 481     assert(cur->was_visited_for_critical_count(), "missed a thread");
 482   }
 483 #endif // ASSERT
 484 
 485   // Update the count of active JNI critical regions
 486   GC_locker::set_jni_lock_count(_current_jni_active_count);
 487 
 488   if (TraceSafepoint) {
 489     VM_Operation *op = VMThread::vm_operation();
 490     tty->print_cr("Entering safepoint region: %s", (op != NULL) ? op->name() : "no vm operation");
 491   }
 492 
 493   RuntimeService::record_safepoint_synchronized();
 494   if (PrintSafepointStatistics) {
 495     update_statistics_on_sync_end(os::javaTimeNanos());
 496   }
 497 
 498   // Call stuff that needs to be run when a safepoint is just about to be completed
 499   {
 500 #if INCLUDE_JFR
 501     EventSafepointCleanup cleanup_event;
 502 #endif
 503     do_cleanup_tasks();
 504 #if INCLUDE_JFR
 505     if (cleanup_event.should_commit()) {
 506       post_safepoint_cleanup_event(&cleanup_event);
 507     }
 508 #endif
 509   }
 510 
 511   if (PrintSafepointStatistics) {
 512     // Record how much time spend on the above cleanup tasks
 513     update_statistics_on_cleanup_end(os::javaTimeNanos());
 514   }
 515 
 516 #if INCLUDE_JFR
 517   if (begin_event.should_commit()) {
 518     post_safepoint_begin_event(&begin_event, nof_threads, _current_jni_active_count);
 519   }
 520 #endif
 521 }
 522 
 523 // Wake up all threads, so they are ready to resume execution after the safepoint
 524 // operation has been carried out
 525 void SafepointSynchronize::end() {
 526 
 527   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 528   assert((_safepoint_counter & 0x1) == 1, "must be odd");
 529 #if INCLUDE_JFR
 530   EventSafepointEnd event;
 531 #endif
 532   _safepoint_counter ++;
 533   // memory fence isn't required here since an odd _safepoint_counter
 534   // value can do no harm and a fence is issued below anyway.
 535 
 536   DEBUG_ONLY(Thread* myThread = Thread::current();)
 537   assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
 538 
 539   if (PrintSafepointStatistics) {
 540     end_statistics(os::javaTimeNanos());
 541   }
 542 
 543 #ifdef ASSERT
 544   // A pending_exception cannot be installed during a safepoint.  The threads
 545   // may install an async exception after they come back from a safepoint into
 546   // pending_exception after they unblock.  But that should happen later.
 547   for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
 548     assert (!(cur->has_pending_exception() &&
 549               cur->safepoint_state()->is_at_poll_safepoint()),
 550             "safepoint installed a pending exception");
 551   }


 597     }
 598 
 599     RuntimeService::record_safepoint_end();
 600 
 601     // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
 602     // blocked in signal_thread_blocked
 603     Threads_lock->unlock();
 604 
 605   }
 606 #if INCLUDE_ALL_GCS
 607   // If there are any concurrent GC threads resume them.
 608   if (UseConcMarkSweepGC) {
 609     ConcurrentMarkSweepThread::desynchronize(false);
 610   } else if (UseG1GC) {
 611     SuspendibleThreadSet::desynchronize();
 612   }
 613 #endif // INCLUDE_ALL_GCS
 614   // record this time so VMThread can keep track how much time has elasped
 615   // since last safepoint.
 616   _end_of_last_safepoint = os::javaTimeMillis();
 617 #if INCLUDE_JFR
 618   if (event.should_commit()) {
 619     post_safepoint_end_event(&event);
 620   }
 621 #endif
 622 }
 623 
 624 bool SafepointSynchronize::is_cleanup_needed() {
 625   // Need a safepoint if some inline cache buffers is non-empty
 626   if (!InlineCacheBuffer::is_empty()) return true;
 627   return false;
 628 }
 629 
 630 
 631 
 632 // Various cleaning tasks that should be done periodically at safepoints
 633 void SafepointSynchronize::do_cleanup_tasks() {
 634   {
 635     const char* name = "deflating idle monitors";
 636 #if INCLUDE_JFR
 637     EventSafepointCleanupTask event;
 638 #endif
 639     TraceTime t1(name, TraceSafepointCleanupTime);
 640     ObjectSynchronizer::deflate_idle_monitors();
 641 #if INCLUDE_JFR
 642     if (event.should_commit()) {
 643       post_safepoint_cleanup_task_event(&event, name);
 644     }
 645 #endif
 646   }
 647 
 648   {
 649     const char* name = "updating inline caches";
 650 #if INCLUDE_JFR
 651     EventSafepointCleanupTask event;
 652 #endif
 653     TraceTime t2(name, TraceSafepointCleanupTime);
 654     InlineCacheBuffer::update_inline_caches();
 655 #if INCLUDE_JFR
 656     if (event.should_commit()) {
 657       post_safepoint_cleanup_task_event(&event, name);
 658     }
 659 #endif
 660   }
 661   {
 662     const char* name = "compilation policy safepoint handler";
 663 #if INCLUDE_JFR
 664     EventSafepointCleanupTask event;
 665 #endif
 666     TraceTime t3(name, TraceSafepointCleanupTime);
 667     CompilationPolicy::policy()->do_safepoint_work();
 668 #if INCLUDE_JFR
 669     if (event.should_commit()) {
 670       post_safepoint_cleanup_task_event(&event, name);
 671     }
 672 #endif
 673   }
 674 
 675   {
 676     const char* name = "mark nmethods";
 677 #if INCLUDE_JFR
 678     EventSafepointCleanupTask event;
 679 #endif
 680     TraceTime t4(name, TraceSafepointCleanupTime);
 681     NMethodSweeper::mark_active_nmethods();
 682 #if INCLUDE_JFR
 683     if (event.should_commit()) {
 684       post_safepoint_cleanup_task_event(&event, name);
 685     }
 686 #endif
 687   }
 688 
 689   if (SymbolTable::needs_rehashing()) {
 690     const char* name = "rehashing symbol table";
 691 #if INCLUDE_JFR
 692     EventSafepointCleanupTask event;
 693 #endif
 694     TraceTime t5(name, TraceSafepointCleanupTime);
 695     SymbolTable::rehash_table();
 696 #if INCLUDE_JFR
 697     if (event.should_commit()) {
 698       post_safepoint_cleanup_task_event(&event, name);
 699     }
 700 #endif
 701   }
 702 
 703   if (StringTable::needs_rehashing()) {
 704     const char* name = "rehashing string table";
 705 #if INCLUDE_JFR
 706     EventSafepointCleanupTask event;
 707 #endif
 708     TraceTime t6(name, TraceSafepointCleanupTime);
 709     StringTable::rehash_table();
 710 #if INCLUDE_JFR
 711     if (event.should_commit()) {
 712       post_safepoint_cleanup_task_event(&event, name);
 713     }
 714 #endif
 715   }
 716 
 717   // rotate log files?
 718   if (UseGCLogFileRotation) {
 719     gclog_or_tty->rotate_log(false);
 720   }
 721 
 722   {
 723     // CMS delays purging the CLDG until the beginning of the next safepoint and to
 724     // make sure concurrent sweep is done
 725     TraceTime t7("purging class loader data graph", TraceSafepointCleanupTime);
 726     ClassLoaderDataGraph::purge_if_needed();
 727   }
 728 }
 729 
 730 
 731 bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
 732   switch(state) {
 733   case _thread_in_native:
 734     // native threads are safe if they have no java stack or have walkable stack


< prev index next >