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


 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       ThreadsListHandle tlh;
 207       JavaThreadIterator jti(tlh.list());
 208 #ifdef ASSERT
 209       for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.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         for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.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           }


 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   {
 400     // Make sure all the threads were visited.
 401     ThreadsListHandle tlh;
 402     JavaThreadIterator jti(tlh.list());
 403     for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) {
 404       assert(cur->was_visited_for_critical_count(), "missed a thread");
 405     }
 406   }
 407 #endif // ASSERT
 408 
 409   // Update the count of active JNI critical regions
 410   GCLocker::set_jni_lock_count(_current_jni_active_count);
 411 
 412   if (log_is_enabled(Debug, safepoint)) {
 413     log_debug(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
 414   }
 415 
 416   RuntimeService::record_safepoint_synchronized();
 417   if (PrintSafepointStatistics) {
 418     update_statistics_on_sync_end(os::javaTimeNanos());
 419   }
 420 
 421   // Call stuff that needs to be run when a safepoint is just about to be completed
 422   {
 423     EventSafepointCleanup cleanup_event;
 424     do_cleanup_tasks();
 425     if (cleanup_event.should_commit()) {
 426       cleanup_event.set_safepointId(safepoint_counter());


 443 // Wake up all threads, so they are ready to resume execution after the safepoint
 444 // operation has been carried out
 445 void SafepointSynchronize::end() {
 446   EventSafepointEnd event;
 447   int safepoint_id = safepoint_counter(); // Keep the odd counter as "id"
 448 
 449   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
 450   assert((_safepoint_counter & 0x1) == 1, "must be odd");
 451   _safepoint_counter ++;
 452   // memory fence isn't required here since an odd _safepoint_counter
 453   // value can do no harm and a fence is issued below anyway.
 454 
 455   DEBUG_ONLY(Thread* myThread = Thread::current();)
 456   assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
 457 
 458   if (PrintSafepointStatistics) {
 459     end_statistics(os::javaTimeNanos());
 460   }
 461 
 462   {
 463     ThreadsListHandle tlh;
 464     JavaThreadIterator jti(tlh.list());
 465 #ifdef ASSERT
 466     // A pending_exception cannot be installed during a safepoint.  The threads
 467     // may install an async exception after they come back from a safepoint into
 468     // pending_exception after they unblock.  But that should happen later.
 469     for (JavaThread *cur = jti.first(); cur != NULL; cur = jti.next()) {
 470       assert (!(cur->has_pending_exception() &&
 471                 cur->safepoint_state()->is_at_poll_safepoint()),
 472               "safepoint installed a pending exception");
 473     }
 474 #endif // ASSERT
 475 
 476     if (PageArmed) {
 477       // Make polling safepoint aware
 478       os::make_polling_page_readable();
 479       PageArmed = 0 ;
 480     }
 481 
 482     // Remove safepoint check from interpreter
 483     Interpreter::ignore_safepoints();
 484 
 485     {
 486       MutexLocker mu(Safepoint_lock);
 487 
 488       assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
 489 
 490       // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
 491       // when they get restarted.
 492       _state = _not_synchronized;
 493       OrderAccess::fence();
 494 
 495       log_debug(safepoint)("Leaving safepoint region");
 496 
 497       // Start suspended threads
 498       for (JavaThread *current = jti.first(); current != NULL; current = jti.next()) {

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


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


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


 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           }


 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();
 421     if (cleanup_event.should_commit()) {
 422       cleanup_event.set_safepointId(safepoint_counter());


 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 


 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 }
 929 
 930 
 931 // -------------------------------------------------------------------------------------------------------
 932 // Implementation of ThreadSafepointState
 933 
 934 ThreadSafepointState::ThreadSafepointState(JavaThread *thread) {
 935   _thread = thread;
 936   _type   = _running;
 937   _has_called_back = false;
 938   _at_poll_safepoint = false;


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 >