< prev index next >

src/hotspot/share/runtime/safepoint.cpp

Print this page
rev 47287 : Port 09.17.Thread_SMR_logging_update from JDK9 to JDK10
rev 47289 : eosterlund, stefank CR - refactor code into threadSMR.cpp and threadSMR.hpp
rev 47292 : stefank, coleenp CR - refactor most JavaThreadIterator usage to use JavaThreadIteratorWithHandle.


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

  60 #include "runtime/timerTrace.hpp"
  61 #include "services/runtimeService.hpp"
  62 #include "trace/tracing.hpp"
  63 #include "trace/traceMacros.hpp"
  64 #include "utilities/events.hpp"
  65 #include "utilities/macros.hpp"
  66 #if INCLUDE_ALL_GCS
  67 #include "gc/cms/concurrentMarkSweepThread.hpp"
  68 #include "gc/g1/suspendibleThreadSet.hpp"
  69 #endif // INCLUDE_ALL_GCS
  70 #ifdef COMPILER1
  71 #include "c1/c1_globals.hpp"
  72 #endif
  73 
  74 // --------------------------------------------------------------------------------------------------
  75 // Implementation of Safepoint begin/end
  76 
  77 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
  78 volatile int  SafepointSynchronize::_waiting_to_block = 0;
  79 volatile int SafepointSynchronize::_safepoint_counter = 0;


 182     _state            = _synchronizing;
 183     OrderAccess::fence();
 184 
 185     // Flush all thread states to memory
 186     if (!UseMembar) {
 187       os::serialize_thread_states();
 188     }
 189 
 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     // Consider using active_processor_count() ... but that call is expensive.
 201     int ncpus = os::processor_count() ;

 202 


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

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


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

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


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


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


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

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


 514 #if INCLUDE_ALL_GCS
 515   // If there are any concurrent GC threads resume them.
 516   if (UseConcMarkSweepGC) {
 517     ConcurrentMarkSweepThread::desynchronize(false);
 518   } else if (UseG1GC) {
 519     SuspendibleThreadSet::desynchronize();
 520   }
 521 #endif // INCLUDE_ALL_GCS
 522   // record this time so VMThread can keep track how much time has elapsed
 523   // since last safepoint.
 524   _end_of_last_safepoint = os::javaTimeMillis();
 525 
 526   if (event.should_commit()) {
 527     event.set_safepointId(safepoint_id);
 528     event.commit();
 529   }
 530 }
 531 
 532 bool SafepointSynchronize::is_cleanup_needed() {
 533   // Need a safepoint if there are many monitors to deflate.


 879   state->handle_polling_page_exception();
 880 }
 881 
 882 
 883 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
 884   if (!timeout_error_printed) {
 885     timeout_error_printed = true;
 886     // Print out the thread info which didn't reach the safepoint for debugging
 887     // purposes (useful when there are lots of threads in the debugger).
 888     tty->cr();
 889     tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
 890     if (reason ==  _spinning_timeout) {
 891       tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
 892     } else if (reason == _blocking_timeout) {
 893       tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
 894     }
 895 
 896     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
 897     ThreadSafepointState *cur_state;
 898     ResourceMark rm;
 899     for(JavaThread *cur_thread = Threads::first(); cur_thread;
 900         cur_thread = cur_thread->next()) {
 901       cur_state = cur_thread->safepoint_state();
 902 
 903       if (cur_thread->thread_state() != _thread_blocked &&
 904           ((reason == _spinning_timeout && cur_state->is_running()) ||
 905            (reason == _blocking_timeout && !cur_state->has_called_back()))) {
 906         tty->print("# ");
 907         cur_thread->print();
 908         tty->cr();
 909       }
 910     }
 911     tty->print_cr("# SafepointSynchronize::begin: (End of list)");
 912   }
 913 
 914   // To debug the long safepoint, specify both DieOnSafepointTimeout &
 915   // ShowMessageBoxOnError.
 916   if (DieOnSafepointTimeout) {
 917     fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
 918           SafepointTimeoutDelay, VMThread::vm_safepoint_description());
 919   }
 920 }


1390                 _coalesced_vmop_count);
1391   tty->print_cr("Maximum sync time  " INT64_FORMAT_W(5) " ms",
1392                 (int64_t)(_max_sync_time / MICROUNITS));
1393   tty->print_cr("Maximum vm operation time (except for Exit VM operation)  "
1394                 INT64_FORMAT_W(5) " ms",
1395                 (int64_t)(_max_vmop_time / MICROUNITS));
1396 }
1397 
1398 // ------------------------------------------------------------------------------------------------
1399 // Non-product code
1400 
1401 #ifndef PRODUCT
1402 
1403 void SafepointSynchronize::print_state() {
1404   if (_state == _not_synchronized) {
1405     tty->print_cr("not synchronized");
1406   } else if (_state == _synchronizing || _state == _synchronized) {
1407     tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
1408                   "synchronized");
1409 
1410     for(JavaThread *cur = Threads::first(); cur; cur = cur->next()) {
1411        cur->safepoint_state()->print();
1412     }
1413   }
1414 }
1415 
1416 void SafepointSynchronize::safepoint_msg(const char* format, ...) {
1417   if (ShowSafepointMsgs) {
1418     va_list ap;
1419     va_start(ap, format);
1420     tty->vprint_cr(format, ap);
1421     va_end(ap);
1422   }
1423 }
1424 
1425 #endif // !PRODUCT


  40 #include "logging/logStream.hpp"
  41 #include "memory/resourceArea.hpp"
  42 #include "memory/universe.inline.hpp"
  43 #include "oops/oop.inline.hpp"
  44 #include "oops/symbol.hpp"
  45 #include "runtime/atomic.hpp"
  46 #include "runtime/compilationPolicy.hpp"
  47 #include "runtime/deoptimization.hpp"
  48 #include "runtime/frame.inline.hpp"
  49 #include "runtime/interfaceSupport.hpp"
  50 #include "runtime/mutexLocker.hpp"
  51 #include "runtime/orderAccess.inline.hpp"
  52 #include "runtime/osThread.hpp"
  53 #include "runtime/safepoint.hpp"
  54 #include "runtime/signature.hpp"
  55 #include "runtime/stubCodeGenerator.hpp"
  56 #include "runtime/stubRoutines.hpp"
  57 #include "runtime/sweeper.hpp"
  58 #include "runtime/synchronizer.hpp"
  59 #include "runtime/thread.inline.hpp"
  60 #include "runtime/threadSMR.hpp"
  61 #include "runtime/timerTrace.hpp"
  62 #include "services/runtimeService.hpp"
  63 #include "trace/tracing.hpp"
  64 #include "trace/traceMacros.hpp"
  65 #include "utilities/events.hpp"
  66 #include "utilities/macros.hpp"
  67 #if INCLUDE_ALL_GCS
  68 #include "gc/cms/concurrentMarkSweepThread.hpp"
  69 #include "gc/g1/suspendibleThreadSet.hpp"
  70 #endif // INCLUDE_ALL_GCS
  71 #ifdef COMPILER1
  72 #include "c1/c1_globals.hpp"
  73 #endif
  74 
  75 // --------------------------------------------------------------------------------------------------
  76 // Implementation of Safepoint begin/end
  77 
  78 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
  79 volatile int  SafepointSynchronize::_waiting_to_block = 0;
  80 volatile int SafepointSynchronize::_safepoint_counter = 0;


 183     _state            = _synchronizing;
 184     OrderAccess::fence();
 185 
 186     // Flush all thread states to memory
 187     if (!UseMembar) {
 188       os::serialize_thread_states();
 189     }
 190 
 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     // Consider using active_processor_count() ... but that call is expensive.
 202     int ncpus = os::processor_count() ;
 203     unsigned int iterations = 0;
 204 
 205     {
 206       JavaThreadIteratorWithHandle jtiwh;
 207 #ifdef ASSERT
 208       for (; JavaThread *cur = jtiwh.next(); ) {
 209         assert(cur->safepoint_state()->is_running(), "Illegal initial state");
 210         // Clear the visited flag to ensure that the critical counts are collected properly.
 211         cur->set_visited_for_critical_count(false);
 212       }
 213 #endif // ASSERT
 214 
 215       if (SafepointTimeout)
 216         safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
 217 
 218       // Iterate through all threads until it have been determined how to stop them all at a safepoint

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


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


 379     }
 380 #endif
 381 
 382     assert((_safepoint_counter & 0x1) == 0, "must be even");
 383     assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 384     _safepoint_counter ++;
 385 
 386     // Record state
 387     _state = _synchronized;
 388 
 389     OrderAccess::fence();
 390 
 391     if (wait_blocked_event.should_commit()) {
 392       wait_blocked_event.set_safepointId(safepoint_counter());
 393       wait_blocked_event.set_runningThreadCount(initial_waiting_to_block);
 394       wait_blocked_event.commit();
 395     }
 396   } // EventSafepointWaitBlocked
 397 
 398 #ifdef ASSERT
 399   // Make sure all the threads were visited.
 400   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
 401     assert(cur->was_visited_for_critical_count(), "missed a thread");
 402   }
 403 #endif // ASSERT
 404 
 405   // Update the count of active JNI critical regions
 406   GCLocker::set_jni_lock_count(_current_jni_active_count);
 407 
 408   if (log_is_enabled(Debug, safepoint)) {
 409     log_debug(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
 410   }
 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();


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

 520     }
 521   } // ThreadsListHandle destroyed here.
 522 
 523 #if INCLUDE_ALL_GCS
 524   // If there are any concurrent GC threads resume them.
 525   if (UseConcMarkSweepGC) {
 526     ConcurrentMarkSweepThread::desynchronize(false);
 527   } else if (UseG1GC) {
 528     SuspendibleThreadSet::desynchronize();
 529   }
 530 #endif // INCLUDE_ALL_GCS
 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.


 888   state->handle_polling_page_exception();
 889 }
 890 
 891 
 892 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
 893   if (!timeout_error_printed) {
 894     timeout_error_printed = true;
 895     // Print out the thread info which didn't reach the safepoint for debugging
 896     // purposes (useful when there are lots of threads in the debugger).
 897     tty->cr();
 898     tty->print_cr("# SafepointSynchronize::begin: Timeout detected:");
 899     if (reason ==  _spinning_timeout) {
 900       tty->print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
 901     } else if (reason == _blocking_timeout) {
 902       tty->print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
 903     }
 904 
 905     tty->print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
 906     ThreadSafepointState *cur_state;
 907     ResourceMark rm;
 908     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {

 909       cur_state = cur_thread->safepoint_state();
 910 
 911       if (cur_thread->thread_state() != _thread_blocked &&
 912         ((reason == _spinning_timeout && cur_state->is_running()) ||
 913            (reason == _blocking_timeout && !cur_state->has_called_back()))) {
 914         tty->print("# ");
 915         cur_thread->print();
 916         tty->cr();
 917       }
 918     }
 919     tty->print_cr("# SafepointSynchronize::begin: (End of list)");
 920   }
 921 
 922   // To debug the long safepoint, specify both DieOnSafepointTimeout &
 923   // ShowMessageBoxOnError.
 924   if (DieOnSafepointTimeout) {
 925     fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
 926           SafepointTimeoutDelay, VMThread::vm_safepoint_description());
 927   }
 928 }


1398                 _coalesced_vmop_count);
1399   tty->print_cr("Maximum sync time  " INT64_FORMAT_W(5) " ms",
1400                 (int64_t)(_max_sync_time / MICROUNITS));
1401   tty->print_cr("Maximum vm operation time (except for Exit VM operation)  "
1402                 INT64_FORMAT_W(5) " ms",
1403                 (int64_t)(_max_vmop_time / MICROUNITS));
1404 }
1405 
1406 // ------------------------------------------------------------------------------------------------
1407 // Non-product code
1408 
1409 #ifndef PRODUCT
1410 
1411 void SafepointSynchronize::print_state() {
1412   if (_state == _not_synchronized) {
1413     tty->print_cr("not synchronized");
1414   } else if (_state == _synchronizing || _state == _synchronized) {
1415     tty->print_cr("State: %s", (_state == _synchronizing) ? "synchronizing" :
1416                   "synchronized");
1417 
1418     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
1419        cur->safepoint_state()->print();
1420     }
1421   }
1422 }
1423 
1424 void SafepointSynchronize::safepoint_msg(const char* format, ...) {
1425   if (ShowSafepointMsgs) {
1426     va_list ap;
1427     va_start(ap, format);
1428     tty->vprint_cr(format, ap);
1429     va_end(ap);
1430   }
1431 }
1432 
1433 #endif // !PRODUCT
< prev index next >