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 |
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 |
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 } |