< prev index next >

src/hotspot/share/runtime/safepoint.cpp

Print this page
rev 47862 : imported patch 10.07.open.rebase_20171110.dcubed
rev 47863 : imported patch 10.08.open.rebase_20171114.rehn


  42 #include "memory/resourceArea.hpp"
  43 #include "memory/universe.inline.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "oops/symbol.hpp"
  46 #include "runtime/atomic.hpp"
  47 #include "runtime/compilationPolicy.hpp"
  48 #include "runtime/deoptimization.hpp"
  49 #include "runtime/frame.inline.hpp"
  50 #include "runtime/interfaceSupport.hpp"
  51 #include "runtime/mutexLocker.hpp"
  52 #include "runtime/orderAccess.inline.hpp"
  53 #include "runtime/osThread.hpp"
  54 #include "runtime/safepoint.hpp"
  55 #include "runtime/safepointMechanism.inline.hpp"
  56 #include "runtime/signature.hpp"
  57 #include "runtime/stubCodeGenerator.hpp"
  58 #include "runtime/stubRoutines.hpp"
  59 #include "runtime/sweeper.hpp"
  60 #include "runtime/synchronizer.hpp"
  61 #include "runtime/thread.inline.hpp"

  62 #include "runtime/timerTrace.hpp"
  63 #include "services/runtimeService.hpp"
  64 #include "trace/tracing.hpp"
  65 #include "trace/traceMacros.hpp"
  66 #include "utilities/events.hpp"
  67 #include "utilities/macros.hpp"
  68 #ifdef COMPILER1
  69 #include "c1/c1_globals.hpp"
  70 #endif
  71 
  72 // --------------------------------------------------------------------------------------------------
  73 // Implementation of Safepoint begin/end
  74 
  75 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
  76 volatile int  SafepointSynchronize::_waiting_to_block = 0;
  77 volatile int SafepointSynchronize::_safepoint_counter = 0;
  78 int SafepointSynchronize::_current_jni_active_count = 0;
  79 long  SafepointSynchronize::_end_of_last_safepoint = 0;
  80 static volatile int PageArmed = 0 ;        // safepoint polling page is RO|RW vs PROT_NONE
  81 static volatile int TryingToBlock = 0 ;    // proximate value -- for advisory use only


 157   //  3. Running compiled Code
 158   //     Compiled code reads a global (Safepoint Polling) page that
 159   //     is set to fault if we are trying to get to a safepoint.
 160   //  4. Blocked
 161   //     A thread which is blocked will not be allowed to return from the
 162   //     block condition until the safepoint operation is complete.
 163   //  5. In VM or Transitioning between states
 164   //     If a Java thread is currently running in the VM or transitioning
 165   //     between states, the safepointing code will wait for the thread to
 166   //     block itself when it attempts transitions to a new state.
 167   //
 168   {
 169     EventSafepointStateSynchronization sync_event;
 170     int initial_running = 0;
 171 
 172     _state            = _synchronizing;
 173 
 174     if (SafepointMechanism::uses_thread_local_poll()) {
 175       // Arming the per thread poll while having _state != _not_synchronized means safepointing
 176       log_trace(safepoint)("Setting thread local yield flag for threads");
 177       for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
 178         // Make sure the threads start polling, it is time to yield.
 179         SafepointMechanism::arm_local_poll(cur); // release store, global state -> local state
 180       }
 181     }
 182     OrderAccess::fence(); // storestore|storeload, global state -> local state
 183 
 184     // Flush all thread states to memory
 185     if (!UseMembar) {
 186       os::serialize_thread_states();
 187     }
 188 
 189     if (SafepointMechanism::uses_global_page_poll()) {
 190       // Make interpreter safepoint aware
 191       Interpreter::notice_safepoints();
 192 
 193       if (DeferPollingPageLoopCount < 0) {
 194         // Make polling safepoint aware
 195         guarantee (PageArmed == 0, "invariant") ;
 196         PageArmed = 1 ;
 197         os::make_polling_page_unreadable();
 198       }
 199     }
 200 
 201     // Consider using active_processor_count() ... but that call is expensive.
 202     int ncpus = os::processor_count() ;

 203 


 204 #ifdef ASSERT
 205     for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
 206       assert(cur->safepoint_state()->is_running(), "Illegal initial state");
 207       // Clear the visited flag to ensure that the critical counts are collected properly.
 208       cur->set_visited_for_critical_count(false);
 209     }
 210 #endif // ASSERT
 211 
 212     if (SafepointTimeout)
 213       safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
 214 
 215     // Iterate through all threads until it have been determined how to stop them all at a safepoint
 216     unsigned int iterations = 0;
 217     int steps = 0 ;
 218     while(still_running > 0) {
 219       for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {

 220         assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
 221         ThreadSafepointState *cur_state = cur->safepoint_state();
 222         if (cur_state->is_running()) {
 223           cur_state->examine_state_of_thread();
 224           if (!cur_state->is_running()) {
 225             still_running--;
 226             // consider adjusting steps downward:
 227             //   steps = 0
 228             //   steps -= NNN
 229             //   steps >>= 1
 230             //   steps = MIN(steps, 2000-100)
 231             //   if (iterations != 0) steps -= NNN
 232           }
 233           LogTarget(Trace, safepoint) lt;
 234           if (lt.is_enabled()) {
 235             ResourceMark rm;
 236             LogStream ls(lt);
 237             cur_state->print_on(&ls);
 238           }
 239         }


 310           PageArmed = 1 ;
 311           os::make_polling_page_unreadable();
 312         }
 313 
 314         // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
 315         // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
 316         ++steps ;
 317         if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
 318           SpinPause() ;     // MP-Polite spin
 319         } else
 320           if (steps < DeferThrSuspendLoopCount) {
 321             os::naked_yield() ;
 322           } else {
 323             os::naked_short_sleep(1);
 324           }
 325 
 326         iterations ++ ;
 327       }
 328       assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
 329     }

 330     assert(still_running == 0, "sanity check");
 331 
 332     if (PrintSafepointStatistics) {
 333       update_statistics_on_spin_end();
 334     }
 335 
 336     if (sync_event.should_commit()) {
 337       // Group this event together with the ones committed after the counter is increased
 338       sync_event.set_safepointId(safepoint_counter() + 1);
 339       sync_event.set_initialThreadCount(initial_running);
 340       sync_event.set_runningThreadCount(_waiting_to_block);
 341       sync_event.set_iterations(iterations);
 342       sync_event.commit();
 343     }
 344   } //EventSafepointStateSync
 345 
 346   // wait until all threads are stopped
 347   {
 348     EventSafepointWaitBlocked wait_blocked_event;
 349     int initial_waiting_to_block = _waiting_to_block;
 350 
 351     while (_waiting_to_block > 0) {
 352       log_debug(safepoint)("Waiting for %d thread(s) to block", _waiting_to_block);
 353       if (!SafepointTimeout || timeout_error_printed) {
 354         Safepoint_lock->wait(true);  // true, means with no safepoint checks
 355       } else {
 356         // Compute remaining time
 357         jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
 358 
 359         // If there is no remaining time, then there is an error
 360         if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
 361           print_safepoint_timeout(_blocking_timeout);
 362         }
 363       }
 364     }


 376     }
 377 #endif
 378 
 379     assert((_safepoint_counter & 0x1) == 0, "must be even");
 380     assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 381     _safepoint_counter ++;
 382 
 383     // Record state
 384     _state = _synchronized;
 385 
 386     OrderAccess::fence();
 387 
 388     if (wait_blocked_event.should_commit()) {
 389       wait_blocked_event.set_safepointId(safepoint_counter());
 390       wait_blocked_event.set_runningThreadCount(initial_waiting_to_block);
 391       wait_blocked_event.commit();
 392     }
 393   } // EventSafepointWaitBlocked
 394 
 395 #ifdef ASSERT
 396   for (JavaThread *cur = Threads::first(); cur != NULL; cur = cur->next()) {
 397     // make sure all the threads were visited
 398     assert(cur->was_visited_for_critical_count(), "missed a thread");
 399   }
 400 #endif // ASSERT
 401 
 402   // Update the count of active JNI critical regions
 403   GCLocker::set_jni_lock_count(_current_jni_active_count);
 404 
 405   if (log_is_enabled(Debug, safepoint)) {
 406     log_debug(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
 407   }
 408 
 409   RuntimeService::record_safepoint_synchronized();
 410   if (PrintSafepointStatistics) {
 411     update_statistics_on_sync_end(os::javaTimeNanos());
 412   }
 413 
 414   // Call stuff that needs to be run when a safepoint is just about to be completed
 415   {
 416     EventSafepointCleanup cleanup_event;
 417     do_cleanup_tasks();


 435 
 436 // Wake up all threads, so they are ready to resume execution after the safepoint
 437 // operation has been carried out
 438 void SafepointSynchronize::end() {
 439   EventSafepointEnd event;
 440   int safepoint_id = safepoint_counter(); // Keep the odd counter as "id"
 441 
 442   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 443   assert((_safepoint_counter & 0x1) == 1, "must be odd");
 444   _safepoint_counter ++;
 445   // memory fence isn't required here since an odd _safepoint_counter
 446   // value can do no harm and a fence is issued below anyway.
 447 
 448   DEBUG_ONLY(Thread* myThread = Thread::current();)
 449   assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
 450 
 451   if (PrintSafepointStatistics) {
 452     end_statistics(os::javaTimeNanos());
 453   }
 454 


 455 #ifdef ASSERT
 456   // A pending_exception cannot be installed during a safepoint.  The threads
 457   // may install an async exception after they come back from a safepoint into
 458   // pending_exception after they unblock.  But that should happen later.
 459   for (JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
 460     assert (!(cur->has_pending_exception() &&
 461               cur->safepoint_state()->is_at_poll_safepoint()),
 462             "safepoint installed a pending exception");
 463   }
 464 #endif // ASSERT
 465 
 466   if (PageArmed) {
 467     assert(SafepointMechanism::uses_global_page_poll(), "sanity");
 468     // Make polling safepoint aware
 469     os::make_polling_page_readable();
 470     PageArmed = 0 ;
 471   }
 472 
 473   if (SafepointMechanism::uses_global_page_poll()) {
 474     // Remove safepoint check from interpreter
 475     Interpreter::ignore_safepoints();
 476   }
 477 
 478   {
 479     MutexLocker mu(Safepoint_lock);
 480 
 481     assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
 482 
 483     if (SafepointMechanism::uses_thread_local_poll()) {
 484       _state = _not_synchronized;
 485       OrderAccess::storestore(); // global state -> local state
 486       for (JavaThread *current = Threads::first(); current; current = current->next()) {

 487         ThreadSafepointState* cur_state = current->safepoint_state();
 488         cur_state->restart(); // TSS _running
 489         SafepointMechanism::disarm_local_poll(current); // release store, local state -> polling page
 490       }
 491       log_debug(safepoint)("Leaving safepoint region");
 492     } else {
 493       // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
 494       // when they get restarted.
 495       _state = _not_synchronized;
 496       OrderAccess::fence();
 497 
 498       log_debug(safepoint)("Leaving safepoint region");
 499 
 500       // Start suspended threads
 501       for (JavaThread *current = Threads::first(); current; current = current->next()) {

 502         // A problem occurring on Solaris is when attempting to restart threads
 503         // the first #cpus - 1 go well, but then the VMThread is preempted when we get
 504         // to the next one (since it has been running the longest).  We then have
 505         // to wait for a cpu to become available before we can continue restarting
 506         // threads.
 507         // FIXME: This causes the performance of the VM to degrade when active and with
 508         // large numbers of threads.  Apparently this is due to the synchronous nature
 509         // of suspending threads.
 510         //
 511         // TODO-FIXME: the comments above are vestigial and no longer apply.
 512         // Furthermore, using solaris' schedctl in this particular context confers no benefit
 513         if (VMThreadHintNoPreempt) {
 514           os::hint_no_preempt();
 515         }
 516         ThreadSafepointState* cur_state = current->safepoint_state();
 517         assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
 518         cur_state->restart();
 519         assert(cur_state->is_running(), "safepoint state has not been reset");
 520       }
 521     }
 522 
 523     RuntimeService::record_safepoint_end();
 524 
 525     // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
 526     // blocked in signal_thread_blocked
 527     Threads_lock->unlock();
 528 
 529   }


 530   Universe::heap()->safepoint_synchronize_end();
 531   // record this time so VMThread can keep track how much time has elapsed
 532   // since last safepoint.
 533   _end_of_last_safepoint = os::javaTimeMillis();
 534 
 535   if (event.should_commit()) {
 536     event.set_safepointId(safepoint_id);
 537     event.commit();
 538   }
 539 }
 540 
 541 bool SafepointSynchronize::is_cleanup_needed() {
 542   // Need a safepoint if there are many monitors to deflate.
 543   if (ObjectSynchronizer::is_cleanup_needed()) return true;
 544   // Need a safepoint if some inline cache buffers is non-empty
 545   if (!InlineCacheBuffer::is_empty()) return true;
 546   return false;
 547 }
 548 
 549 static void event_safepoint_cleanup_task_commit(EventSafepointCleanupTask& event, const char* name) {


 898   state->handle_polling_page_exception();
 899 }
 900 
 901 
 902 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
 903   if (!timeout_error_printed) {
 904     timeout_error_printed = true;
 905     // Print out the thread info which didn't reach the safepoint for debugging
 906     // purposes (useful when there are lots of threads in the debugger).
 907     tty->cr();
 908     tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
 909     if (reason ==  _spinning_timeout) {
 910       tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
 911     } else if (reason == _blocking_timeout) {
 912       tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
 913     }
 914 
 915     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
 916     ThreadSafepointState *cur_state;
 917     ResourceMark rm;
 918     for (JavaThread *cur_thread = Threads::first(); cur_thread;
 919         cur_thread = cur_thread->next()) {
 920       cur_state = cur_thread->safepoint_state();
 921 
 922       if (cur_thread->thread_state() != _thread_blocked &&
 923           ((reason == _spinning_timeout && cur_state->is_running()) ||
 924            (reason == _blocking_timeout && !cur_state->has_called_back()))) {
 925         tty->print("# ");
 926         cur_thread->print();
 927         tty->cr();
 928       }
 929     }
 930     tty->print_cr("# SafepointSynchronize::begin: (End of list)");
 931   }
 932 
 933   // To debug the long safepoint, specify both DieOnSafepointTimeout &
 934   // ShowMessageBoxOnError.
 935   if (DieOnSafepointTimeout) {
 936     fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
 937           SafepointTimeoutDelay, VMThread::vm_safepoint_description());
 938   }
 939 }


1410                 _coalesced_vmop_count);
1411   tty->print_cr("Maximum sync time  " INT64_FORMAT_W(5) " ms",
1412                 (int64_t)(_max_sync_time / MICROUNITS));
1413   tty->print_cr("Maximum vm operation time (except for Exit VM operation)  "
1414                 INT64_FORMAT_W(5) " ms",
1415                 (int64_t)(_max_vmop_time / MICROUNITS));
1416 }
1417 
1418 // ------------------------------------------------------------------------------------------------
1419 // Non-product code
1420 
1421 #ifndef PRODUCT
1422 
1423 void SafepointSynchronize::print_state() {
1424   if (_state == _not_synchronized) {
1425     tty->print_cr("not synchronized");
1426   } else if (_state == _synchronizing || _state == _synchronized) {
1427     tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
1428                   "synchronized");
1429 
1430     for (JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
1431        cur->safepoint_state()->print();
1432     }
1433   }
1434 }
1435 
1436 void SafepointSynchronize::safepoint_msg(const char* format, ...) {
1437   if (ShowSafepointMsgs) {
1438     va_list ap;
1439     va_start(ap, format);
1440     tty->vprint_cr(format, ap);
1441     va_end(ap);
1442   }
1443 }
1444 
1445 #endif // !PRODUCT


  42 #include "memory/resourceArea.hpp"
  43 #include "memory/universe.inline.hpp"
  44 #include "oops/oop.inline.hpp"
  45 #include "oops/symbol.hpp"
  46 #include "runtime/atomic.hpp"
  47 #include "runtime/compilationPolicy.hpp"
  48 #include "runtime/deoptimization.hpp"
  49 #include "runtime/frame.inline.hpp"
  50 #include "runtime/interfaceSupport.hpp"
  51 #include "runtime/mutexLocker.hpp"
  52 #include "runtime/orderAccess.inline.hpp"
  53 #include "runtime/osThread.hpp"
  54 #include "runtime/safepoint.hpp"
  55 #include "runtime/safepointMechanism.inline.hpp"
  56 #include "runtime/signature.hpp"
  57 #include "runtime/stubCodeGenerator.hpp"
  58 #include "runtime/stubRoutines.hpp"
  59 #include "runtime/sweeper.hpp"
  60 #include "runtime/synchronizer.hpp"
  61 #include "runtime/thread.inline.hpp"
  62 #include "runtime/threadSMR.hpp"
  63 #include "runtime/timerTrace.hpp"
  64 #include "services/runtimeService.hpp"
  65 #include "trace/tracing.hpp"
  66 #include "trace/traceMacros.hpp"
  67 #include "utilities/events.hpp"
  68 #include "utilities/macros.hpp"
  69 #ifdef COMPILER1
  70 #include "c1/c1_globals.hpp"
  71 #endif
  72 
  73 // --------------------------------------------------------------------------------------------------
  74 // Implementation of Safepoint begin/end
  75 
  76 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
  77 volatile int  SafepointSynchronize::_waiting_to_block = 0;
  78 volatile int SafepointSynchronize::_safepoint_counter = 0;
  79 int SafepointSynchronize::_current_jni_active_count = 0;
  80 long  SafepointSynchronize::_end_of_last_safepoint = 0;
  81 static volatile int PageArmed = 0 ;        // safepoint polling page is RO|RW vs PROT_NONE
  82 static volatile int TryingToBlock = 0 ;    // proximate value -- for advisory use only


 158   //  3. Running compiled Code
 159   //     Compiled code reads a global (Safepoint Polling) page that
 160   //     is set to fault if we are trying to get to a safepoint.
 161   //  4. Blocked
 162   //     A thread which is blocked will not be allowed to return from the
 163   //     block condition until the safepoint operation is complete.
 164   //  5. In VM or Transitioning between states
 165   //     If a Java thread is currently running in the VM or transitioning
 166   //     between states, the safepointing code will wait for the thread to
 167   //     block itself when it attempts transitions to a new state.
 168   //
 169   {
 170     EventSafepointStateSynchronization sync_event;
 171     int initial_running = 0;
 172 
 173     _state            = _synchronizing;
 174 
 175     if (SafepointMechanism::uses_thread_local_poll()) {
 176       // Arming the per thread poll while having _state != _not_synchronized means safepointing
 177       log_trace(safepoint)("Setting thread local yield flag for threads");
 178       for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
 179         // Make sure the threads start polling, it is time to yield.
 180         SafepointMechanism::arm_local_poll(cur); // release store, global state -> local state
 181       }
 182     }
 183     OrderAccess::fence(); // storestore|storeload, global state -> local state
 184 
 185     // Flush all thread states to memory
 186     if (!UseMembar) {
 187       os::serialize_thread_states();
 188     }
 189 
 190     if (SafepointMechanism::uses_global_page_poll()) {
 191       // Make interpreter safepoint aware
 192       Interpreter::notice_safepoints();
 193 
 194       if (DeferPollingPageLoopCount < 0) {
 195         // Make polling safepoint aware
 196         guarantee (PageArmed == 0, "invariant") ;
 197         PageArmed = 1 ;
 198         os::make_polling_page_unreadable();
 199       }
 200     }
 201 
 202     // Consider using active_processor_count() ... but that call is expensive.
 203     int ncpus = os::processor_count() ;
 204     unsigned int iterations = 0;
 205 
 206     {
 207       JavaThreadIteratorWithHandle jtiwh;
 208 #ifdef ASSERT
 209       for (; JavaThread *cur = jtiwh.next(); ) {
 210         assert(cur->safepoint_state()->is_running(), "Illegal initial state");
 211         // Clear the visited flag to ensure that the critical counts are collected properly.
 212         cur->set_visited_for_critical_count(false);
 213       }
 214 #endif // ASSERT
 215 
 216       if (SafepointTimeout)
 217         safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
 218 
 219       // Iterate through all threads until it have been determined how to stop them all at a safepoint

 220       int steps = 0 ;
 221       while(still_running > 0) {
 222         jtiwh.rewind();
 223         for (; JavaThread *cur = jtiwh.next(); ) {
 224           assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
 225           ThreadSafepointState *cur_state = cur->safepoint_state();
 226           if (cur_state->is_running()) {
 227             cur_state->examine_state_of_thread();
 228             if (!cur_state->is_running()) {
 229               still_running--;
 230               // consider adjusting steps downward:
 231               //   steps = 0
 232               //   steps -= NNN
 233               //   steps >>= 1
 234               //   steps = MIN(steps, 2000-100)
 235               //   if (iterations != 0) steps -= NNN
 236             }
 237             LogTarget(Trace, safepoint) lt;
 238             if (lt.is_enabled()) {
 239               ResourceMark rm;
 240               LogStream ls(lt);
 241               cur_state->print_on(&ls);
 242             }
 243           }


 314             PageArmed = 1 ;
 315             os::make_polling_page_unreadable();
 316           }
 317 
 318           // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
 319           // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
 320           ++steps ;
 321           if (ncpus > 1 && steps < SafepointSpinBeforeYield) {
 322             SpinPause() ;     // MP-Polite spin
 323           } else
 324             if (steps < DeferThrSuspendLoopCount) {
 325               os::naked_yield() ;
 326             } else {
 327               os::naked_short_sleep(1);
 328             }
 329 
 330           iterations ++ ;
 331         }
 332         assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
 333       }
 334     } // ThreadsListHandle destroyed here.
 335     assert(still_running == 0, "sanity check");
 336 
 337     if (PrintSafepointStatistics) {
 338       update_statistics_on_spin_end();
 339     }
 340 
 341     if (sync_event.should_commit()) {
 342       // Group this event together with the ones committed after the counter is increased
 343       sync_event.set_safepointId(safepoint_counter() + 1);
 344       sync_event.set_initialThreadCount(initial_running);
 345       sync_event.set_runningThreadCount(_waiting_to_block);
 346       sync_event.set_iterations(iterations);
 347       sync_event.commit();
 348     }
 349   } // EventSafepointStateSynchronization destroyed here.
 350 
 351   // wait until all threads are stopped
 352   {
 353     EventSafepointWaitBlocked wait_blocked_event;
 354     int initial_waiting_to_block = _waiting_to_block;
 355 
 356     while (_waiting_to_block > 0) {
 357       log_debug(safepoint)("Waiting for %d thread(s) to block", _waiting_to_block);
 358       if (!SafepointTimeout || timeout_error_printed) {
 359         Safepoint_lock->wait(true);  // true, means with no safepoint checks
 360       } else {
 361         // Compute remaining time
 362         jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
 363 
 364         // If there is no remaining time, then there is an error
 365         if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
 366           print_safepoint_timeout(_blocking_timeout);
 367         }
 368       }
 369     }


 381     }
 382 #endif
 383 
 384     assert((_safepoint_counter & 0x1) == 0, "must be even");
 385     assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 386     _safepoint_counter ++;
 387 
 388     // Record state
 389     _state = _synchronized;
 390 
 391     OrderAccess::fence();
 392 
 393     if (wait_blocked_event.should_commit()) {
 394       wait_blocked_event.set_safepointId(safepoint_counter());
 395       wait_blocked_event.set_runningThreadCount(initial_waiting_to_block);
 396       wait_blocked_event.commit();
 397     }
 398   } // EventSafepointWaitBlocked
 399 
 400 #ifdef ASSERT
 401   // Make sure all the threads were visited.
 402   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
 403     assert(cur->was_visited_for_critical_count(), "missed a thread");
 404   }
 405 #endif // ASSERT
 406 
 407   // Update the count of active JNI critical regions
 408   GCLocker::set_jni_lock_count(_current_jni_active_count);
 409 
 410   if (log_is_enabled(Debug, safepoint)) {
 411     log_debug(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
 412   }
 413 
 414   RuntimeService::record_safepoint_synchronized();
 415   if (PrintSafepointStatistics) {
 416     update_statistics_on_sync_end(os::javaTimeNanos());
 417   }
 418 
 419   // Call stuff that needs to be run when a safepoint is just about to be completed
 420   {
 421     EventSafepointCleanup cleanup_event;
 422     do_cleanup_tasks();


 440 
 441 // Wake up all threads, so they are ready to resume execution after the safepoint
 442 // operation has been carried out
 443 void SafepointSynchronize::end() {
 444   EventSafepointEnd event;
 445   int safepoint_id = safepoint_counter(); // Keep the odd counter as "id"
 446 
 447   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 448   assert((_safepoint_counter & 0x1) == 1, "must be odd");
 449   _safepoint_counter ++;
 450   // memory fence isn't required here since an odd _safepoint_counter
 451   // value can do no harm and a fence is issued below anyway.
 452 
 453   DEBUG_ONLY(Thread* myThread = Thread::current();)
 454   assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
 455 
 456   if (PrintSafepointStatistics) {
 457     end_statistics(os::javaTimeNanos());
 458   }
 459 
 460   {
 461     JavaThreadIteratorWithHandle jtiwh;
 462 #ifdef ASSERT
 463     // A pending_exception cannot be installed during a safepoint.  The threads
 464     // may install an async exception after they come back from a safepoint into
 465     // pending_exception after they unblock.  But that should happen later.
 466     for (; JavaThread *cur = jtiwh.next(); ) {
 467       assert (!(cur->has_pending_exception() &&
 468                 cur->safepoint_state()->is_at_poll_safepoint()),
 469               "safepoint installed a pending exception");
 470     }
 471 #endif // ASSERT
 472 
 473     if (PageArmed) {
 474       assert(SafepointMechanism::uses_global_page_poll(), "sanity");
 475       // Make polling safepoint aware
 476       os::make_polling_page_readable();
 477       PageArmed = 0 ;
 478     }
 479 
 480     if (SafepointMechanism::uses_global_page_poll()) {
 481       // Remove safepoint check from interpreter
 482       Interpreter::ignore_safepoints();
 483     }
 484 
 485     {
 486       MutexLocker mu(Safepoint_lock);
 487 
 488       assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
 489 
 490       if (SafepointMechanism::uses_thread_local_poll()) {
 491         _state = _not_synchronized;
 492         OrderAccess::storestore(); // global state -> local state
 493         jtiwh.rewind();
 494         for (; JavaThread *current = jtiwh.next(); ) {
 495           ThreadSafepointState* cur_state = current->safepoint_state();
 496           cur_state->restart(); // TSS _running
 497           SafepointMechanism::disarm_local_poll(current); // release store, local state -> polling page
 498         }
 499         log_debug(safepoint)("Leaving safepoint region");
 500       } else {
 501         // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
 502         // when they get restarted.
 503         _state = _not_synchronized;
 504         OrderAccess::fence();
 505 
 506         log_debug(safepoint)("Leaving safepoint region");
 507 
 508         // Start suspended threads
 509         jtiwh.rewind();
 510         for (; JavaThread *current = jtiwh.next(); ) {
 511           // A problem occurring on Solaris is when attempting to restart threads
 512           // the first #cpus - 1 go well, but then the VMThread is preempted when we get
 513           // to the next one (since it has been running the longest).  We then have
 514           // to wait for a cpu to become available before we can continue restarting
 515           // threads.
 516           // FIXME: This causes the performance of the VM to degrade when active and with
 517           // large numbers of threads.  Apparently this is due to the synchronous nature
 518           // of suspending threads.
 519           //
 520           // TODO-FIXME: the comments above are vestigial and no longer apply.
 521           // Furthermore, using solaris' schedctl in this particular context confers no benefit
 522           if (VMThreadHintNoPreempt) {
 523             os::hint_no_preempt();
 524           }
 525           ThreadSafepointState* cur_state = current->safepoint_state();
 526           assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
 527           cur_state->restart();
 528           assert(cur_state->is_running(), "safepoint state has not been reset");
 529         }
 530       }
 531 
 532       RuntimeService::record_safepoint_end();
 533 
 534       // Release threads lock, so threads can be created/destroyed again.
 535       // It will also release all threads blocked in signal_thread_blocked.
 536       Threads_lock->unlock();

 537     }
 538   } // ThreadsListHandle destroyed here.
 539 
 540   Universe::heap()->safepoint_synchronize_end();
 541   // record this time so VMThread can keep track how much time has elapsed
 542   // since last safepoint.
 543   _end_of_last_safepoint = os::javaTimeMillis();
 544 
 545   if (event.should_commit()) {
 546     event.set_safepointId(safepoint_id);
 547     event.commit();
 548   }
 549 }
 550 
 551 bool SafepointSynchronize::is_cleanup_needed() {
 552   // Need a safepoint if there are many monitors to deflate.
 553   if (ObjectSynchronizer::is_cleanup_needed()) return true;
 554   // Need a safepoint if some inline cache buffers is non-empty
 555   if (!InlineCacheBuffer::is_empty()) return true;
 556   return false;
 557 }
 558 
 559 static void event_safepoint_cleanup_task_commit(EventSafepointCleanupTask& event, const char* name) {


 908   state->handle_polling_page_exception();
 909 }
 910 
 911 
 912 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
 913   if (!timeout_error_printed) {
 914     timeout_error_printed = true;
 915     // Print out the thread info which didn't reach the safepoint for debugging
 916     // purposes (useful when there are lots of threads in the debugger).
 917     tty->cr();
 918     tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
 919     if (reason ==  _spinning_timeout) {
 920       tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
 921     } else if (reason == _blocking_timeout) {
 922       tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
 923     }
 924 
 925     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
 926     ThreadSafepointState *cur_state;
 927     ResourceMark rm;
 928     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {

 929       cur_state = cur_thread->safepoint_state();
 930 
 931       if (cur_thread->thread_state() != _thread_blocked &&
 932         ((reason == _spinning_timeout && cur_state->is_running()) ||
 933            (reason == _blocking_timeout && !cur_state->has_called_back()))) {
 934         tty->print("# ");
 935         cur_thread->print();
 936         tty->cr();
 937       }
 938     }
 939     tty->print_cr("# SafepointSynchronize::begin: (End of list)");
 940   }
 941 
 942   // To debug the long safepoint, specify both DieOnSafepointTimeout &
 943   // ShowMessageBoxOnError.
 944   if (DieOnSafepointTimeout) {
 945     fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
 946           SafepointTimeoutDelay, VMThread::vm_safepoint_description());
 947   }
 948 }


1419                 _coalesced_vmop_count);
1420   tty->print_cr("Maximum sync time  " INT64_FORMAT_W(5) " ms",
1421                 (int64_t)(_max_sync_time / MICROUNITS));
1422   tty->print_cr("Maximum vm operation time (except for Exit VM operation)  "
1423                 INT64_FORMAT_W(5) " ms",
1424                 (int64_t)(_max_vmop_time / MICROUNITS));
1425 }
1426 
1427 // ------------------------------------------------------------------------------------------------
1428 // Non-product code
1429 
1430 #ifndef PRODUCT
1431 
1432 void SafepointSynchronize::print_state() {
1433   if (_state == _not_synchronized) {
1434     tty->print_cr("not synchronized");
1435   } else if (_state == _synchronizing || _state == _synchronized) {
1436     tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
1437                   "synchronized");
1438 
1439     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
1440        cur->safepoint_state()->print();
1441     }
1442   }
1443 }
1444 
1445 void SafepointSynchronize::safepoint_msg(const char* format, ...) {
1446   if (ShowSafepointMsgs) {
1447     va_list ap;
1448     va_start(ap, format);
1449     tty->vprint_cr(format, ap);
1450     va_end(ap);
1451   }
1452 }
1453 
1454 #endif // !PRODUCT
< prev index next >