src/hotspot/share/runtime/safepoint.cpp
changeset 55514 03468b206457
parent 54982 b18c8301b8c2
child 55569 8e3a0ebf3497
equal deleted inserted replaced
55505:0bf9477626ef 55514:03468b206457
   116     event.set_safepointId(safepoint_id);
   116     event.set_safepointId(safepoint_id);
   117     event.commit();
   117     event.commit();
   118   }
   118   }
   119 }
   119 }
   120 
   120 
       
   121 // SafepointCheck
       
   122 SafepointStateTracker::SafepointStateTracker(uint64_t safepoint_id, bool at_safepoint)
       
   123   : _safepoint_id(safepoint_id), _at_safepoint(at_safepoint) {}
       
   124 
       
   125 bool SafepointStateTracker::safepoint_state_changed() {
       
   126   return _safepoint_id != SafepointSynchronize::safepoint_id() ||
       
   127     _at_safepoint != SafepointSynchronize::is_at_safepoint();
       
   128 }
       
   129 
   121 // --------------------------------------------------------------------------------------------------
   130 // --------------------------------------------------------------------------------------------------
   122 // Implementation of Safepoint begin/end
   131 // Implementation of Safepoint begin/end
   123 
   132 
   124 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
   133 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
   125 int SafepointSynchronize::_waiting_to_block = 0;
   134 int SafepointSynchronize::_waiting_to_block = 0;
   126 volatile uint64_t SafepointSynchronize::_safepoint_counter = 0;
   135 volatile uint64_t SafepointSynchronize::_safepoint_counter = 0;
       
   136 uint64_t SafepointSynchronize::_safepoint_id = 0;
   127 const uint64_t SafepointSynchronize::InactiveSafepointCounter = 0;
   137 const uint64_t SafepointSynchronize::InactiveSafepointCounter = 0;
   128 int SafepointSynchronize::_current_jni_active_count = 0;
   138 int SafepointSynchronize::_current_jni_active_count = 0;
   129 
   139 
   130 WaitBarrier* SafepointSynchronize::_wait_barrier;
   140 WaitBarrier* SafepointSynchronize::_wait_barrier;
   131 
   141 
   152   assert(_waiting_to_block > 0, "sanity check");
   162   assert(_waiting_to_block > 0, "sanity check");
   153   assert(Thread::current()->is_VM_thread(), "Only VM thread may decrement");
   163   assert(Thread::current()->is_VM_thread(), "Only VM thread may decrement");
   154   --_waiting_to_block;
   164   --_waiting_to_block;
   155 }
   165 }
   156 
   166 
   157 static bool thread_not_running(ThreadSafepointState *cur_state) {
   167 bool SafepointSynchronize::thread_not_running(ThreadSafepointState *cur_state) {
   158   if (!cur_state->is_running()) {
   168   if (!cur_state->is_running()) {
   159     return true;
   169     return true;
   160   }
   170   }
   161   cur_state->examine_state_of_thread(SafepointSynchronize::safepoint_counter());
   171   cur_state->examine_state_of_thread(SafepointSynchronize::safepoint_counter());
   162   if (!cur_state->is_running()) {
   172   if (!cur_state->is_running()) {
   406   // Record state
   416   // Record state
   407   _state = _synchronized;
   417   _state = _synchronized;
   408 
   418 
   409   OrderAccess::fence();
   419   OrderAccess::fence();
   410 
   420 
       
   421   // Set the new id
       
   422   ++_safepoint_id;
       
   423 
   411 #ifdef ASSERT
   424 #ifdef ASSERT
   412   // Make sure all the threads were visited.
   425   // Make sure all the threads were visited.
   413   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
   426   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
   414     assert(cur->was_visited_for_critical_count(_safepoint_counter), "missed a thread");
   427     assert(cur->was_visited_for_critical_count(_safepoint_counter), "missed a thread");
   415   }
   428   }
   417 
   430 
   418   // Update the count of active JNI critical regions
   431   // Update the count of active JNI critical regions
   419   GCLocker::set_jni_lock_count(_current_jni_active_count);
   432   GCLocker::set_jni_lock_count(_current_jni_active_count);
   420 
   433 
   421   post_safepoint_synchronize_event(sync_event,
   434   post_safepoint_synchronize_event(sync_event,
   422                                    _safepoint_counter,
   435                                    _safepoint_id,
   423                                    initial_running,
   436                                    initial_running,
   424                                    _waiting_to_block, iterations);
   437                                    _waiting_to_block, iterations);
   425 
   438 
   426   SafepointTracing::synchronized(nof_threads, initial_running, _nof_threads_hit_polling_page);
   439   SafepointTracing::synchronized(nof_threads, initial_running, _nof_threads_hit_polling_page);
   427 
   440 
   428   // We do the safepoint cleanup first since a GC related safepoint
   441   // We do the safepoint cleanup first since a GC related safepoint
   429   // needs cleanup to be completed before running the GC op.
   442   // needs cleanup to be completed before running the GC op.
   430   EventSafepointCleanup cleanup_event;
   443   EventSafepointCleanup cleanup_event;
   431   do_cleanup_tasks();
   444   do_cleanup_tasks();
   432   post_safepoint_cleanup_event(cleanup_event, _safepoint_counter);
   445   post_safepoint_cleanup_event(cleanup_event, _safepoint_id);
   433 
   446 
   434   post_safepoint_begin_event(begin_event, _safepoint_counter, nof_threads, _current_jni_active_count);
   447   post_safepoint_begin_event(begin_event, _safepoint_id, nof_threads, _current_jni_active_count);
   435   SafepointTracing::cleanup();
   448   SafepointTracing::cleanup();
   436 }
   449 }
   437 
   450 
   438 void SafepointSynchronize::disarm_safepoint() {
   451 void SafepointSynchronize::disarm_safepoint() {
   439   uint64_t safepoint_id = _safepoint_counter;
   452   uint64_t active_safepoint_counter = _safepoint_counter;
   440   {
   453   {
   441     JavaThreadIteratorWithHandle jtiwh;
   454     JavaThreadIteratorWithHandle jtiwh;
   442 #ifdef ASSERT
   455 #ifdef ASSERT
   443     // A pending_exception cannot be installed during a safepoint.  The threads
   456     // A pending_exception cannot be installed during a safepoint.  The threads
   444     // may install an async exception after they come back from a safepoint into
   457     // may install an async exception after they come back from a safepoint into
   473     OrderAccess::fence(); // Keep the local state from floating up.
   486     OrderAccess::fence(); // Keep the local state from floating up.
   474 
   487 
   475     jtiwh.rewind();
   488     jtiwh.rewind();
   476     for (; JavaThread *current = jtiwh.next(); ) {
   489     for (; JavaThread *current = jtiwh.next(); ) {
   477       // Clear the visited flag to ensure that the critical counts are collected properly.
   490       // Clear the visited flag to ensure that the critical counts are collected properly.
   478       DEBUG_ONLY(current->reset_visited_for_critical_count(safepoint_id);)
   491       DEBUG_ONLY(current->reset_visited_for_critical_count(active_safepoint_counter);)
   479       ThreadSafepointState* cur_state = current->safepoint_state();
   492       ThreadSafepointState* cur_state = current->safepoint_state();
   480       assert(!cur_state->is_running(), "Thread not suspended at safepoint");
   493       assert(!cur_state->is_running(), "Thread not suspended at safepoint");
   481       cur_state->restart(); // TSS _running
   494       cur_state->restart(); // TSS _running
   482       assert(cur_state->is_running(), "safepoint state has not been reset");
   495       assert(cur_state->is_running(), "safepoint state has not been reset");
   483 
   496 
   495 // Wake up all threads, so they are ready to resume execution after the safepoint
   508 // Wake up all threads, so they are ready to resume execution after the safepoint
   496 // operation has been carried out
   509 // operation has been carried out
   497 void SafepointSynchronize::end() {
   510 void SafepointSynchronize::end() {
   498   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
   511   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
   499   EventSafepointEnd event;
   512   EventSafepointEnd event;
   500   uint64_t safepoint_id = _safepoint_counter;
       
   501   assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint");
   513   assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint");
   502 
   514 
   503   disarm_safepoint();
   515   disarm_safepoint();
   504 
   516 
   505   Universe::heap()->safepoint_synchronize_end();
   517   Universe::heap()->safepoint_synchronize_end();
   506 
   518 
   507   SafepointTracing::end();
   519   SafepointTracing::end();
   508 
   520 
   509   post_safepoint_end_event(event, safepoint_id);
   521   post_safepoint_end_event(event, safepoint_id());
   510 }
   522 }
   511 
   523 
   512 bool SafepointSynchronize::is_cleanup_needed() {
   524 bool SafepointSynchronize::is_cleanup_needed() {
   513   // Need a safepoint if there are many monitors to deflate.
   525   // Need a safepoint if there are many monitors to deflate.
   514   if (ObjectSynchronizer::is_cleanup_needed()) return true;
   526   if (ObjectSynchronizer::is_cleanup_needed()) return true;
   552     _cleanup_threads_cl(ParallelSPCleanupThreadClosure(counters)),
   564     _cleanup_threads_cl(ParallelSPCleanupThreadClosure(counters)),
   553     _num_workers(num_workers),
   565     _num_workers(num_workers),
   554     _counters(counters) {}
   566     _counters(counters) {}
   555 
   567 
   556   void work(uint worker_id) {
   568   void work(uint worker_id) {
   557     uint64_t safepoint_id = SafepointSynchronize::safepoint_counter();
   569     uint64_t safepoint_id = SafepointSynchronize::safepoint_id();
   558     // All threads deflate monitors and mark nmethods (if necessary).
   570     // All threads deflate monitors and mark nmethods (if necessary).
   559     Threads::possibly_parallel_threads_do(true, &_cleanup_threads_cl);
   571     Threads::possibly_parallel_threads_do(true, &_cleanup_threads_cl);
   560 
   572 
   561     if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_DEFLATE_MONITORS)) {
   573     if (_subtasks.try_claim_task(SafepointSynchronize::SAFEPOINT_CLEANUP_DEFLATE_MONITORS)) {
   562       const char* name = "deflating global idle monitors";
   574       const char* name = "deflating global idle monitors";