src/hotspot/share/runtime/safepoint.cpp
changeset 47881 0ce0ac68ace7
parent 47774 69c081ca110a
child 48105 8d15b1369c7a
equal deleted inserted replaced
47824:cf127be65014 47881:0ce0ac68ace7
    50 #include "runtime/interfaceSupport.hpp"
    50 #include "runtime/interfaceSupport.hpp"
    51 #include "runtime/mutexLocker.hpp"
    51 #include "runtime/mutexLocker.hpp"
    52 #include "runtime/orderAccess.inline.hpp"
    52 #include "runtime/orderAccess.inline.hpp"
    53 #include "runtime/osThread.hpp"
    53 #include "runtime/osThread.hpp"
    54 #include "runtime/safepoint.hpp"
    54 #include "runtime/safepoint.hpp"
       
    55 #include "runtime/safepointMechanism.inline.hpp"
    55 #include "runtime/signature.hpp"
    56 #include "runtime/signature.hpp"
    56 #include "runtime/stubCodeGenerator.hpp"
    57 #include "runtime/stubCodeGenerator.hpp"
    57 #include "runtime/stubRoutines.hpp"
    58 #include "runtime/stubRoutines.hpp"
    58 #include "runtime/sweeper.hpp"
    59 #include "runtime/sweeper.hpp"
    59 #include "runtime/synchronizer.hpp"
    60 #include "runtime/synchronizer.hpp"
   167   {
   168   {
   168     EventSafepointStateSynchronization sync_event;
   169     EventSafepointStateSynchronization sync_event;
   169     int initial_running = 0;
   170     int initial_running = 0;
   170 
   171 
   171     _state            = _synchronizing;
   172     _state            = _synchronizing;
   172     OrderAccess::fence();
   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
   173 
   183 
   174     // Flush all thread states to memory
   184     // Flush all thread states to memory
   175     if (!UseMembar) {
   185     if (!UseMembar) {
   176       os::serialize_thread_states();
   186       os::serialize_thread_states();
   177     }
   187     }
   178 
   188 
   179     // Make interpreter safepoint aware
   189     if (SafepointMechanism::uses_global_page_poll()) {
   180     Interpreter::notice_safepoints();
   190       // Make interpreter safepoint aware
   181 
   191       Interpreter::notice_safepoints();
   182     if (DeferPollingPageLoopCount < 0) {
   192 
   183       // Make polling safepoint aware
   193       if (DeferPollingPageLoopCount < 0) {
   184       guarantee (PageArmed == 0, "invariant") ;
   194         // Make polling safepoint aware
   185       PageArmed = 1 ;
   195         guarantee (PageArmed == 0, "invariant") ;
   186       os::make_polling_page_unreadable();
   196         PageArmed = 1 ;
       
   197         os::make_polling_page_unreadable();
       
   198       }
   187     }
   199     }
   188 
   200 
   189     // Consider using active_processor_count() ... but that call is expensive.
   201     // Consider using active_processor_count() ... but that call is expensive.
   190     int ncpus = os::processor_count() ;
   202     int ncpus = os::processor_count() ;
   191 
   203 
   291         //    Alternately, instead of counting iterations of the outer loop
   303         //    Alternately, instead of counting iterations of the outer loop
   292         //    we could count the # of threads visited in the inner loop, above.
   304         //    we could count the # of threads visited in the inner loop, above.
   293         // 9. On windows consider using the return value from SwitchThreadTo()
   305         // 9. On windows consider using the return value from SwitchThreadTo()
   294         //    to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.
   306         //    to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.
   295 
   307 
   296         if (int(iterations) == DeferPollingPageLoopCount) {
   308         if (SafepointMechanism::uses_global_page_poll() && int(iterations) == DeferPollingPageLoopCount) {
   297           guarantee (PageArmed == 0, "invariant") ;
   309           guarantee (PageArmed == 0, "invariant") ;
   298           PageArmed = 1 ;
   310           PageArmed = 1 ;
   299           os::make_polling_page_unreadable();
   311           os::make_polling_page_unreadable();
   300         }
   312         }
   301 
   313 
   442 
   454 
   443 #ifdef ASSERT
   455 #ifdef ASSERT
   444   // A pending_exception cannot be installed during a safepoint.  The threads
   456   // A pending_exception cannot be installed during a safepoint.  The threads
   445   // 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
   446   // pending_exception after they unblock.  But that should happen later.
   458   // pending_exception after they unblock.  But that should happen later.
   447   for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
   459   for (JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
   448     assert (!(cur->has_pending_exception() &&
   460     assert (!(cur->has_pending_exception() &&
   449               cur->safepoint_state()->is_at_poll_safepoint()),
   461               cur->safepoint_state()->is_at_poll_safepoint()),
   450             "safepoint installed a pending exception");
   462             "safepoint installed a pending exception");
   451   }
   463   }
   452 #endif // ASSERT
   464 #endif // ASSERT
   453 
   465 
   454   if (PageArmed) {
   466   if (PageArmed) {
       
   467     assert(SafepointMechanism::uses_global_page_poll(), "sanity");
   455     // Make polling safepoint aware
   468     // Make polling safepoint aware
   456     os::make_polling_page_readable();
   469     os::make_polling_page_readable();
   457     PageArmed = 0 ;
   470     PageArmed = 0 ;
   458   }
   471   }
   459 
   472 
   460   // Remove safepoint check from interpreter
   473   if (SafepointMechanism::uses_global_page_poll()) {
   461   Interpreter::ignore_safepoints();
   474     // Remove safepoint check from interpreter
       
   475     Interpreter::ignore_safepoints();
       
   476   }
   462 
   477 
   463   {
   478   {
   464     MutexLocker mu(Safepoint_lock);
   479     MutexLocker mu(Safepoint_lock);
   465 
   480 
   466     assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
   481     assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
   467 
   482 
   468     // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
   483     if (SafepointMechanism::uses_thread_local_poll()) {
   469     // when they get restarted.
   484       _state = _not_synchronized;
   470     _state = _not_synchronized;
   485       OrderAccess::storestore(); // global state -> local state
   471     OrderAccess::fence();
   486       for (JavaThread *current = Threads::first(); current; current = current->next()) {
   472 
   487         ThreadSafepointState* cur_state = current->safepoint_state();
   473     log_debug(safepoint)("Leaving safepoint region");
   488         cur_state->restart(); // TSS _running
   474 
   489         SafepointMechanism::disarm_local_poll(current); // release store, local state -> polling page
   475     // Start suspended threads
   490       }
   476     for(JavaThread *current = Threads::first(); current; current = current->next()) {
   491       log_debug(safepoint)("Leaving safepoint region");
   477       // A problem occurring on Solaris is when attempting to restart threads
   492     } else {
   478       // the first #cpus - 1 go well, but then the VMThread is preempted when we get
   493       // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
   479       // to the next one (since it has been running the longest).  We then have
   494       // when they get restarted.
   480       // to wait for a cpu to become available before we can continue restarting
   495       _state = _not_synchronized;
   481       // threads.
   496       OrderAccess::fence();
   482       // FIXME: This causes the performance of the VM to degrade when active and with
   497 
   483       // large numbers of threads.  Apparently this is due to the synchronous nature
   498       log_debug(safepoint)("Leaving safepoint region");
   484       // of suspending threads.
   499 
   485       //
   500       // Start suspended threads
   486       // TODO-FIXME: the comments above are vestigial and no longer apply.
   501       for (JavaThread *current = Threads::first(); current; current = current->next()) {
   487       // Furthermore, using solaris' schedctl in this particular context confers no benefit
   502         // A problem occurring on Solaris is when attempting to restart threads
   488       if (VMThreadHintNoPreempt) {
   503         // the first #cpus - 1 go well, but then the VMThread is preempted when we get
   489         os::hint_no_preempt();
   504         // to the next one (since it has been running the longest).  We then have
   490       }
   505         // to wait for a cpu to become available before we can continue restarting
   491       ThreadSafepointState* cur_state = current->safepoint_state();
   506         // threads.
   492       assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
   507         // FIXME: This causes the performance of the VM to degrade when active and with
   493       cur_state->restart();
   508         // large numbers of threads.  Apparently this is due to the synchronous nature
   494       assert(cur_state->is_running(), "safepoint state has not been reset");
   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       }
   495     }
   521     }
   496 
   522 
   497     RuntimeService::record_safepoint_end();
   523     RuntimeService::record_safepoint_end();
   498 
   524 
   499     // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
   525     // Release threads lock, so threads can be created/destroyed again. It will also starts all threads
   853 
   879 
   854 
   880 
   855 void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
   881 void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
   856   assert(thread->is_Java_thread(), "polling reference encountered by VM thread");
   882   assert(thread->is_Java_thread(), "polling reference encountered by VM thread");
   857   assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
   883   assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
   858   assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
   884   if (!ThreadLocalHandshakes) {
       
   885     assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
       
   886   }
   859 
   887 
   860   if (ShowSafepointMsgs) {
   888   if (ShowSafepointMsgs) {
   861     tty->print("handle_polling_page_exception: ");
   889     tty->print("handle_polling_page_exception: ");
   862   }
   890   }
   863 
   891 
   885     }
   913     }
   886 
   914 
   887     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
   915     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
   888     ThreadSafepointState *cur_state;
   916     ThreadSafepointState *cur_state;
   889     ResourceMark rm;
   917     ResourceMark rm;
   890     for(JavaThread *cur_thread = Threads::first(); cur_thread;
   918     for (JavaThread *cur_thread = Threads::first(); cur_thread;
   891         cur_thread = cur_thread->next()) {
   919         cur_thread = cur_thread->next()) {
   892       cur_state = cur_thread->safepoint_state();
   920       cur_state = cur_thread->safepoint_state();
   893 
   921 
   894       if (cur_thread->thread_state() != _thread_blocked &&
   922       if (cur_thread->thread_state() != _thread_blocked &&
   895           ((reason == _spinning_timeout && cur_state->is_running()) ||
   923           ((reason == _spinning_timeout && cur_state->is_running()) ||
  1051   _thread->print_thread_state_on(st);
  1079   _thread->print_thread_state_on(st);
  1052 }
  1080 }
  1053 
  1081 
  1054 // ---------------------------------------------------------------------------------------------------------------------
  1082 // ---------------------------------------------------------------------------------------------------------------------
  1055 
  1083 
  1056 // Block the thread at the safepoint poll or poll return.
  1084 // Block the thread at poll or poll return for safepoint/handshake.
  1057 void ThreadSafepointState::handle_polling_page_exception() {
  1085 void ThreadSafepointState::handle_polling_page_exception() {
  1058 
  1086 
  1059   // Check state.  block() will set thread state to thread_in_vm which will
  1087   // Check state.  block() will set thread state to thread_in_vm which will
  1060   // cause the safepoint state _type to become _call_back.
  1088   // cause the safepoint state _type to become _call_back.
  1061   assert(type() == ThreadSafepointState::_running,
  1089   suspend_type t = type();
  1062          "polling page exception on thread not running state");
  1090   assert(!SafepointMechanism::uses_global_page_poll() || t == ThreadSafepointState::_running,
       
  1091          "polling page exception on thread not running state: %u", uint(t));
  1063 
  1092 
  1064   // Step 1: Find the nmethod from the return address
  1093   // Step 1: Find the nmethod from the return address
  1065   if (ShowSafepointMsgs && Verbose) {
  1094   if (ShowSafepointMsgs && Verbose) {
  1066     tty->print_cr("Polling page exception at " INTPTR_FORMAT, p2i(thread()->saved_exception_pc()));
  1095     tty->print_cr("Polling page exception at " INTPTR_FORMAT, p2i(thread()->saved_exception_pc()));
  1067   }
  1096   }
  1099       return_value = Handle(thread(), result);
  1128       return_value = Handle(thread(), result);
  1100       assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
  1129       assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
  1101     }
  1130     }
  1102 
  1131 
  1103     // Block the thread
  1132     // Block the thread
  1104     SafepointSynchronize::block(thread());
  1133     SafepointMechanism::block_if_requested(thread());
  1105 
  1134 
  1106     // restore oop result, if any
  1135     // restore oop result, if any
  1107     if (return_oop) {
  1136     if (return_oop) {
  1108       caller_fr.set_saved_oop_result(&map, return_value());
  1137       caller_fr.set_saved_oop_result(&map, return_value());
  1109     }
  1138     }
  1115 
  1144 
  1116     // verify the blob built the "return address" correctly
  1145     // verify the blob built the "return address" correctly
  1117     assert(real_return_addr == caller_fr.pc(), "must match");
  1146     assert(real_return_addr == caller_fr.pc(), "must match");
  1118 
  1147 
  1119     // Block the thread
  1148     // Block the thread
  1120     SafepointSynchronize::block(thread());
  1149     SafepointMechanism::block_if_requested(thread());
  1121     set_at_poll_safepoint(false);
  1150     set_at_poll_safepoint(false);
  1122 
  1151 
  1123     // If we have a pending async exception deoptimize the frame
  1152     // If we have a pending async exception deoptimize the frame
  1124     // as otherwise we may never deliver it.
  1153     // as otherwise we may never deliver it.
  1125     if (thread()->has_async_condition()) {
  1154     if (thread()->has_async_condition()) {
  1396     tty->print_cr("not synchronized");
  1425     tty->print_cr("not synchronized");
  1397   } else if (_state == _synchronizing || _state == _synchronized) {
  1426   } else if (_state == _synchronizing || _state == _synchronized) {
  1398     tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
  1427     tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
  1399                   "synchronized");
  1428                   "synchronized");
  1400 
  1429 
  1401     for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
  1430     for (JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
  1402        cur->safepoint_state()->print();
  1431        cur->safepoint_state()->print();
  1403     }
  1432     }
  1404   }
  1433   }
  1405 }
  1434 }
  1406 
  1435