86 void SafepointSynchronize::begin() {
87 EventSafepointBegin begin_event;
88 Thread* myThread = Thread::current();
89 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
90
91 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
92 _safepoint_begin_time = os::javaTimeNanos();
93 _ts_of_current_safepoint = tty->time_stamp().seconds();
94 }
95
96 Universe::heap()->safepoint_synchronize_begin();
97
98 // By getting the Threads_lock, we assure that no threads are about to start or
99 // exit. It is released again in SafepointSynchronize::end().
100 Threads_lock->lock();
101
102 assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
103
104 int nof_threads = Threads::number_of_threads();
105
106 log_debug(safepoint)("Safepoint synchronization initiated. (%d)", nof_threads);
107
108 RuntimeService::record_safepoint_begin();
109
110 MutexLocker mu(Safepoint_lock);
111
112 // Reset the count of active JNI critical threads
113 _current_jni_active_count = 0;
114
115 // Set number of threads to wait for, before we initiate the callbacks
116 _waiting_to_block = nof_threads;
117 TryingToBlock = 0 ;
118 int still_running = nof_threads;
119
120 // Save the starting time, so that it can be compared to see if this has taken
121 // too long to complete.
122 jlong safepoint_limit_time = 0;
123 timeout_error_printed = false;
124
125 // PrintSafepointStatisticsTimeout can be specified separately. When
126 // specified, PrintSafepointStatistics will be set to true in
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();
423 if (cleanup_event.should_commit()) {
424 cleanup_event.set_safepointId(safepoint_counter());
425 cleanup_event.commit();
426 }
427 }
428
429 if (PrintSafepointStatistics) {
430 // Record how much time spend on the above cleanup tasks
431 update_statistics_on_cleanup_end(os::javaTimeNanos());
432 }
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");
|
86 void SafepointSynchronize::begin() {
87 EventSafepointBegin begin_event;
88 Thread* myThread = Thread::current();
89 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
90
91 if (PrintSafepointStatistics || PrintSafepointStatisticsTimeout > 0) {
92 _safepoint_begin_time = os::javaTimeNanos();
93 _ts_of_current_safepoint = tty->time_stamp().seconds();
94 }
95
96 Universe::heap()->safepoint_synchronize_begin();
97
98 // By getting the Threads_lock, we assure that no threads are about to start or
99 // exit. It is released again in SafepointSynchronize::end().
100 Threads_lock->lock();
101
102 assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
103
104 int nof_threads = Threads::number_of_threads();
105
106 log_debug(safepoint)("Safepoint synchronization initiated. (%d threads)", nof_threads);
107
108 RuntimeService::record_safepoint_begin();
109
110 MutexLocker mu(Safepoint_lock);
111
112 // Reset the count of active JNI critical threads
113 _current_jni_active_count = 0;
114
115 // Set number of threads to wait for, before we initiate the callbacks
116 _waiting_to_block = nof_threads;
117 TryingToBlock = 0 ;
118 int still_running = nof_threads;
119
120 // Save the starting time, so that it can be compared to see if this has taken
121 // too long to complete.
122 jlong safepoint_limit_time = 0;
123 timeout_error_printed = false;
124
125 // PrintSafepointStatisticsTimeout can be specified separately. When
126 // specified, PrintSafepointStatistics will be set to true in
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 log_info(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
411
412 RuntimeService::record_safepoint_synchronized();
413 if (PrintSafepointStatistics) {
414 update_statistics_on_sync_end(os::javaTimeNanos());
415 }
416
417 // Call stuff that needs to be run when a safepoint is just about to be completed
418 {
419 EventSafepointCleanup cleanup_event;
420 do_cleanup_tasks();
421 if (cleanup_event.should_commit()) {
422 cleanup_event.set_safepointId(safepoint_counter());
423 cleanup_event.commit();
424 }
425 }
426
427 if (PrintSafepointStatistics) {
428 // Record how much time spend on the above cleanup tasks
429 update_statistics_on_cleanup_end(os::javaTimeNanos());
430 }
477
478 if (SafepointMechanism::uses_global_page_poll()) {
479 // Remove safepoint check from interpreter
480 Interpreter::ignore_safepoints();
481 }
482
483 {
484 MutexLocker mu(Safepoint_lock);
485
486 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
487
488 if (SafepointMechanism::uses_thread_local_poll()) {
489 _state = _not_synchronized;
490 OrderAccess::storestore(); // global state -> local state
491 jtiwh.rewind();
492 for (; JavaThread *current = jtiwh.next(); ) {
493 ThreadSafepointState* cur_state = current->safepoint_state();
494 cur_state->restart(); // TSS _running
495 SafepointMechanism::disarm_local_poll(current); // release store, local state -> polling page
496 }
497 log_info(safepoint)("Leaving safepoint region");
498 } else {
499 // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
500 // when they get restarted.
501 _state = _not_synchronized;
502 OrderAccess::fence();
503
504 log_info(safepoint)("Leaving safepoint region");
505
506 // Start suspended threads
507 jtiwh.rewind();
508 for (; JavaThread *current = jtiwh.next(); ) {
509 // A problem occurring on Solaris is when attempting to restart threads
510 // the first #cpus - 1 go well, but then the VMThread is preempted when we get
511 // to the next one (since it has been running the longest). We then have
512 // to wait for a cpu to become available before we can continue restarting
513 // threads.
514 // FIXME: This causes the performance of the VM to degrade when active and with
515 // large numbers of threads. Apparently this is due to the synchronous nature
516 // of suspending threads.
517 //
518 // TODO-FIXME: the comments above are vestigial and no longer apply.
519 // Furthermore, using solaris' schedctl in this particular context confers no benefit
520 if (VMThreadHintNoPreempt) {
521 os::hint_no_preempt();
522 }
523 ThreadSafepointState* cur_state = current->safepoint_state();
524 assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
|