src/hotspot/share/runtime/safepoint.cpp
changeset 53895 b22d8ae270a2
parent 53775 5d20b085d893
child 54009 13acb4339895
equal deleted inserted replaced
53894:bf1133e7dfba 53895:b22d8ae270a2
   101     event.set_iterations(iterations);
   101     event.set_iterations(iterations);
   102     event.commit();
   102     event.commit();
   103   }
   103   }
   104 }
   104 }
   105 
   105 
   106 static void post_safepoint_wait_blocked_event(EventSafepointWaitBlocked& event,
       
   107                                               uint64_t safepoint_id,
       
   108                                               int initial_threads_waiting_to_block) {
       
   109   if (event.should_commit()) {
       
   110     event.set_safepointId(safepoint_id);
       
   111     event.set_runningThreadCount(initial_threads_waiting_to_block);
       
   112     event.commit();
       
   113   }
       
   114 }
       
   115 
       
   116 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask& event,
   106 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask& event,
   117                                               uint64_t safepoint_id,
   107                                               uint64_t safepoint_id,
   118                                               const char* name) {
   108                                               const char* name) {
   119   if (event.should_commit()) {
   109   if (event.should_commit()) {
   120     event.set_safepointId(safepoint_id);
   110     event.set_safepointId(safepoint_id);
   136 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
   126 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
   137 int SafepointSynchronize::_waiting_to_block = 0;
   127 int SafepointSynchronize::_waiting_to_block = 0;
   138 volatile uint64_t SafepointSynchronize::_safepoint_counter = 0;
   128 volatile uint64_t SafepointSynchronize::_safepoint_counter = 0;
   139 const uint64_t SafepointSynchronize::InactiveSafepointCounter = 0;
   129 const uint64_t SafepointSynchronize::InactiveSafepointCounter = 0;
   140 int SafepointSynchronize::_current_jni_active_count = 0;
   130 int SafepointSynchronize::_current_jni_active_count = 0;
   141 long SafepointSynchronize::_end_of_last_safepoint = 0;
       
   142 
   131 
   143 WaitBarrier* SafepointSynchronize::_wait_barrier;
   132 WaitBarrier* SafepointSynchronize::_wait_barrier;
   144 
       
   145 // We need a place to save the desc since it is released before we need it.
       
   146 static char stopped_description[64] = "";
       
   147 static bool _vm_is_waiting = false;
       
   148 
   133 
   149 static volatile bool PageArmed = false;        // safepoint polling page is RO|RW vs PROT_NONE
   134 static volatile bool PageArmed = false;        // safepoint polling page is RO|RW vs PROT_NONE
   150 static bool timeout_error_printed = false;
   135 static bool timeout_error_printed = false;
   151 
   136 
   152 // Statistic related
   137 // Statistic related
   153 julong SafepointSynchronize::_coalesced_vmop_count = 0;
       
   154 static jlong _safepoint_begin_time = 0;
   138 static jlong _safepoint_begin_time = 0;
   155 static float _ts_of_current_safepoint = 0.0f;
       
   156 static volatile int _nof_threads_hit_polling_page = 0;
   139 static volatile int _nof_threads_hit_polling_page = 0;
   157 
   140 
   158 void SafepointSynchronize::init(Thread* vmthread) {
   141 void SafepointSynchronize::init(Thread* vmthread) {
   159   // WaitBarrier should never be destroyed since we will have
   142   // WaitBarrier should never be destroyed since we will have
   160   // threads waiting on it while exiting.
   143   // threads waiting on it while exiting.
   161   _wait_barrier = new WaitBarrier(vmthread);
   144   _wait_barrier = new WaitBarrier(vmthread);
       
   145   SafepointTracing::init();
   162 }
   146 }
   163 
   147 
   164 void SafepointSynchronize::increment_jni_active_count() {
   148 void SafepointSynchronize::increment_jni_active_count() {
   165   assert(Thread::current()->is_VM_thread(), "Only VM thread may increment");
   149   assert(Thread::current()->is_VM_thread(), "Only VM thread may increment");
   166   ++_current_jni_active_count;
   150   ++_current_jni_active_count;
   242   *p_prev = NULL;
   226   *p_prev = NULL;
   243 
   227 
   244   DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);)
   228   DEBUG_ONLY(assert_list_is_valid(tss_head, still_running);)
   245 
   229 
   246   *initial_running = still_running;
   230   *initial_running = still_running;
   247   if (log_is_enabled(Debug, safepoint, stats)) {
       
   248     begin_statistics(nof_threads, still_running);
       
   249   }
       
   250 
   231 
   251   int iterations = 1; // The first iteration is above.
   232   int iterations = 1; // The first iteration is above.
   252 
   233 
   253   while (still_running > 0) {
   234   while (still_running > 0) {
   254     // Check if this has taken too long:
   235     // Check if this has taken too long:
   255     if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
   236     if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
   256       print_safepoint_timeout(_spinning_timeout);
   237       print_safepoint_timeout();
   257     }
   238     }
   258     if (int(iterations) == -1) { // overflow - something is wrong.
   239     if (int(iterations) == -1) { // overflow - something is wrong.
   259       // We can only overflow here when we are using global
   240       // We can only overflow here when we are using global
   260       // polling pages. We keep this guarantee in its original
   241       // polling pages. We keep this guarantee in its original
   261       // form so that searches of the bug database for this
   242       // form so that searches of the bug database for this
   289     iterations++;
   270     iterations++;
   290   }
   271   }
   291 
   272 
   292   assert(tss_head == NULL, "Must be empty");
   273   assert(tss_head == NULL, "Must be empty");
   293 
   274 
   294   if (log_is_enabled(Debug, safepoint, stats)) {
       
   295     update_statistics_on_spin_end();
       
   296   }
       
   297   return iterations;
   275   return iterations;
   298 }
   276 }
   299 
   277 
   300 void SafepointSynchronize::arm_safepoint() {
   278 void SafepointSynchronize::arm_safepoint() {
   301   // Begin the process of bringing the system to a safepoint.
   279   // Begin the process of bringing the system to a safepoint.
   302   // Java threads can be in several different states and are
   280   // Java threads can be in several different states and are
   303   // stopped by different mechanisms:
   281   // stopped by different mechanisms:
   304   //
   282   //
   305   //  1. Running interpreted
   283   //  1. Running interpreted
   306   //     The interpreter dispatch table is changed to force it to
   284   //     When executing branching/returning byte codes interpreter
   307   //     check for a safepoint condition between bytecodes.
   285   //     checks if the poll is armed, if so blocks in SS::block().
       
   286   //     When using global polling the interpreter dispatch table
       
   287   //     is changed to force it to check for a safepoint condition
       
   288   //     between bytecodes.
   308   //  2. Running in native code
   289   //  2. Running in native code
   309   //     When returning from the native code, a Java thread must check
   290   //     When returning from the native code, a Java thread must check
   310   //     the safepoint _state to see if we must block.  If the
   291   //     the safepoint _state to see if we must block.  If the
   311   //     VM thread sees a Java thread in native, it does
   292   //     VM thread sees a Java thread in native, it does
   312   //     not wait for this thread to block.  The order of the memory
   293   //     not wait for this thread to block.  The order of the memory
   320   //  4. Blocked
   301   //  4. Blocked
   321   //     A thread which is blocked will not be allowed to return from the
   302   //     A thread which is blocked will not be allowed to return from the
   322   //     block condition until the safepoint operation is complete.
   303   //     block condition until the safepoint operation is complete.
   323   //  5. In VM or Transitioning between states
   304   //  5. In VM or Transitioning between states
   324   //     If a Java thread is currently running in the VM or transitioning
   305   //     If a Java thread is currently running in the VM or transitioning
   325   //     between states, the safepointing code will wait for the thread to
   306   //     between states, the safepointing code will poll the thread state
   326   //     block itself when it attempts transitions to a new state.
   307   //     until the thread blocks itself when it attempts transitions to a
   327   //
   308   //     new state or locking a safepoint checked monitor.
   328 
   309 
   329   // We must never miss a thread with correct safepoint id, so we must make sure we arm
   310   // We must never miss a thread with correct safepoint id, so we must make sure we arm
   330   // the wait barrier for the next safepoint id/counter.
   311   // the wait barrier for the next safepoint id/counter.
   331   // Arming must be done after resetting _current_jni_active_count, _waiting_to_block.
   312   // Arming must be done after resetting _current_jni_active_count, _waiting_to_block.
   332   _wait_barrier->arm(static_cast<int>(_safepoint_counter + 1));
   313   _wait_barrier->arm(static_cast<int>(_safepoint_counter + 1));
   361   }
   342   }
   362 }
   343 }
   363 
   344 
   364 // Roll all threads forward to a safepoint and suspend them all
   345 // Roll all threads forward to a safepoint and suspend them all
   365 void SafepointSynchronize::begin() {
   346 void SafepointSynchronize::begin() {
       
   347   assert(Thread::current()->is_VM_thread(), "Only VM thread may execute a safepoint");
       
   348 
   366   EventSafepointBegin begin_event;
   349   EventSafepointBegin begin_event;
   367   assert(Thread::current()->is_VM_thread(), "Only VM thread may execute a safepoint");
   350   SafepointTracing::begin(VMThread::vm_op_type());
   368 
       
   369   strncpy(stopped_description, VMThread::vm_safepoint_description(), sizeof(stopped_description) - 1);
       
   370   stopped_description[sizeof(stopped_description) - 1] = '\0';
       
   371 
       
   372   if (log_is_enabled(Debug, safepoint, stats)) {
       
   373     _safepoint_begin_time = os::javaTimeNanos();
       
   374     _ts_of_current_safepoint = tty->time_stamp().seconds();
       
   375     _nof_threads_hit_polling_page = 0;
       
   376   }
       
   377 
   351 
   378   Universe::heap()->safepoint_synchronize_begin();
   352   Universe::heap()->safepoint_synchronize_begin();
   379 
   353 
   380   // By getting the Threads_lock, we assure that no threads are about to start or
   354   // By getting the Threads_lock, we assure that no threads are about to start or
   381   // exit. It is released again in SafepointSynchronize::end().
   355   // exit. It is released again in SafepointSynchronize::end().
   383 
   357 
   384   assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
   358   assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
   385 
   359 
   386   int nof_threads = Threads::number_of_threads();
   360   int nof_threads = Threads::number_of_threads();
   387 
   361 
       
   362   _nof_threads_hit_polling_page = 0;
       
   363 
   388   log_debug(safepoint)("Safepoint synchronization initiated using %s wait barrier. (%d threads)", _wait_barrier->description(), nof_threads);
   364   log_debug(safepoint)("Safepoint synchronization initiated using %s wait barrier. (%d threads)", _wait_barrier->description(), nof_threads);
   389 
       
   390   RuntimeService::record_safepoint_begin();
       
   391 
   365 
   392   // Reset the count of active JNI critical threads
   366   // Reset the count of active JNI critical threads
   393   _current_jni_active_count = 0;
   367   _current_jni_active_count = 0;
   394 
   368 
   395   // Set number of threads to wait for
   369   // Set number of threads to wait for
   397 
   371 
   398   jlong safepoint_limit_time = 0;
   372   jlong safepoint_limit_time = 0;
   399   if (SafepointTimeout) {
   373   if (SafepointTimeout) {
   400     // Set the limit time, so that it can be compared to see if this has taken
   374     // Set the limit time, so that it can be compared to see if this has taken
   401     // too long to complete.
   375     // too long to complete.
   402     safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
   376     safepoint_limit_time = SafepointTracing::start_of_safepoint() + (jlong)SafepointTimeoutDelay * (NANOUNITS / MILLIUNITS);
   403   }
   377     timeout_error_printed = false;
   404   timeout_error_printed = false;
   378   }
   405 
   379 
   406   EventSafepointStateSynchronization sync_event;
   380   EventSafepointStateSynchronization sync_event;
   407   int initial_running = 0;
   381   int initial_running = 0;
   408 
   382 
   409   // Arms the safepoint, _current_jni_active_count and _waiting_to_block must be set before.
   383   // Arms the safepoint, _current_jni_active_count and _waiting_to_block must be set before.
   411 
   385 
   412   // Will spin until all threads are safe.
   386   // Will spin until all threads are safe.
   413   int iterations = synchronize_threads(safepoint_limit_time, nof_threads, &initial_running);
   387   int iterations = synchronize_threads(safepoint_limit_time, nof_threads, &initial_running);
   414   assert(_waiting_to_block == 0, "No thread should be running");
   388   assert(_waiting_to_block == 0, "No thread should be running");
   415 
   389 
   416   post_safepoint_synchronize_event(sync_event, _safepoint_counter, initial_running,
       
   417                                    _waiting_to_block, iterations);
       
   418 
       
   419   // Keep event from now.
       
   420   EventSafepointWaitBlocked wait_blocked_event;
       
   421 
       
   422 #ifndef PRODUCT
   390 #ifndef PRODUCT
   423   if (SafepointTimeout) {
   391   if (safepoint_limit_time != 0) {
   424     jlong current_time = os::javaTimeNanos();
   392     jlong current_time = os::javaTimeNanos();
   425     if (safepoint_limit_time < current_time) {
   393     if (safepoint_limit_time < current_time) {
   426       log_warning(safepoint)("# SafepointSynchronize: Finished after "
   394       log_warning(safepoint)("# SafepointSynchronize: Finished after "
   427                     INT64_FORMAT_W(6) " ms",
   395                     INT64_FORMAT_W(6) " ms",
   428                     (int64_t)((current_time - safepoint_limit_time) / MICROUNITS +
   396                     (int64_t)(current_time - SafepointTracing::start_of_safepoint()) / (NANOUNITS / MILLIUNITS));
   429                               (jlong)SafepointTimeoutDelay));
       
   430     }
   397     }
   431   }
   398   }
   432 #endif
   399 #endif
   433 
   400 
   434   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
   401   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
   435 
   402 
   436   // Record state
   403   // Record state
   437   _state = _synchronized;
   404   _state = _synchronized;
   438 
   405 
   439   OrderAccess::fence();
   406   OrderAccess::fence();
   440 
       
   441   post_safepoint_wait_blocked_event(wait_blocked_event, _safepoint_counter, 0);
       
   442 
   407 
   443 #ifdef ASSERT
   408 #ifdef ASSERT
   444   // Make sure all the threads were visited.
   409   // Make sure all the threads were visited.
   445   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
   410   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
   446     assert(cur->was_visited_for_critical_count(_safepoint_counter), "missed a thread");
   411     assert(cur->was_visited_for_critical_count(_safepoint_counter), "missed a thread");
   448 #endif // ASSERT
   413 #endif // ASSERT
   449 
   414 
   450   // Update the count of active JNI critical regions
   415   // Update the count of active JNI critical regions
   451   GCLocker::set_jni_lock_count(_current_jni_active_count);
   416   GCLocker::set_jni_lock_count(_current_jni_active_count);
   452 
   417 
   453   log_info(safepoint)("Entering safepoint region: %s", stopped_description);
   418   post_safepoint_synchronize_event(sync_event,
   454 
   419                                    _safepoint_counter,
   455   RuntimeService::record_safepoint_synchronized();
   420                                    initial_running,
   456   if (log_is_enabled(Debug, safepoint, stats)) {
   421                                    _waiting_to_block, iterations);
   457     update_statistics_on_sync_end(os::javaTimeNanos());
   422 
   458   }
   423   SafepointTracing::synchronized(nof_threads, initial_running, _nof_threads_hit_polling_page);
   459 
   424 
   460   // We do the safepoint cleanup first since a GC related safepoint
   425   // We do the safepoint cleanup first since a GC related safepoint
   461   // needs cleanup to be completed before running the GC op.
   426   // needs cleanup to be completed before running the GC op.
   462   EventSafepointCleanup cleanup_event;
   427   EventSafepointCleanup cleanup_event;
   463   do_cleanup_tasks();
   428   do_cleanup_tasks();
   464   post_safepoint_cleanup_event(cleanup_event, _safepoint_counter);
   429   post_safepoint_cleanup_event(cleanup_event, _safepoint_counter);
   465 
   430 
   466   if (log_is_enabled(Debug, safepoint, stats)) {
       
   467     // Record how much time spend on the above cleanup tasks
       
   468     update_statistics_on_cleanup_end(os::javaTimeNanos());
       
   469   }
       
   470 
       
   471   post_safepoint_begin_event(begin_event, _safepoint_counter, nof_threads, _current_jni_active_count);
   431   post_safepoint_begin_event(begin_event, _safepoint_counter, nof_threads, _current_jni_active_count);
       
   432   SafepointTracing::cleanup();
   472 }
   433 }
   473 
   434 
   474 void SafepointSynchronize::disarm_safepoint() {
   435 void SafepointSynchronize::disarm_safepoint() {
   475   uint64_t safepoint_id = _safepoint_counter;
   436   uint64_t safepoint_id = _safepoint_counter;
   476   {
   437   {
   518       assert(cur_state->is_running(), "safepoint state has not been reset");
   479       assert(cur_state->is_running(), "safepoint state has not been reset");
   519       SafepointMechanism::disarm_local_poll(current);
   480       SafepointMechanism::disarm_local_poll(current);
   520     }
   481     }
   521   } // ~JavaThreadIteratorWithHandle
   482   } // ~JavaThreadIteratorWithHandle
   522 
   483 
   523   log_info(safepoint)("Leaving safepoint region");
       
   524 
       
   525   RuntimeService::record_safepoint_end();
       
   526 
       
   527   // Release threads lock, so threads can be created/destroyed again.
   484   // Release threads lock, so threads can be created/destroyed again.
   528   Threads_lock->unlock();
   485   Threads_lock->unlock();
   529 
   486 
   530   // Wake threads after local state is correctly set.
   487   // Wake threads after local state is correctly set.
   531   _wait_barrier->disarm();
   488   _wait_barrier->disarm();
   537   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
   494   assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
   538   EventSafepointEnd event;
   495   EventSafepointEnd event;
   539   uint64_t safepoint_id = _safepoint_counter;
   496   uint64_t safepoint_id = _safepoint_counter;
   540   assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint");
   497   assert(Thread::current()->is_VM_thread(), "Only VM thread can execute a safepoint");
   541 
   498 
   542   if (log_is_enabled(Debug, safepoint, stats)) {
       
   543     end_statistics(os::javaTimeNanos());
       
   544   }
       
   545 
       
   546   disarm_safepoint();
   499   disarm_safepoint();
   547 
   500 
   548   RuntimeService::record_safepoint_epilog(stopped_description);
       
   549 
       
   550   Universe::heap()->safepoint_synchronize_end();
   501   Universe::heap()->safepoint_synchronize_end();
   551 
   502 
   552   // record this time so VMThread can keep track how much time has elapsed
   503   SafepointTracing::end();
   553   // since last safepoint.
       
   554   _end_of_last_safepoint = os::javaTimeMillis();
       
   555 
   504 
   556   post_safepoint_end_event(event, safepoint_id);
   505   post_safepoint_end_event(event, safepoint_id);
   557 }
   506 }
   558 
   507 
   559 bool SafepointSynchronize::is_cleanup_needed() {
   508 bool SafepointSynchronize::is_cleanup_needed() {
   913   assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
   862   assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
   914   if (!ThreadLocalHandshakes) {
   863   if (!ThreadLocalHandshakes) {
   915     assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
   864     assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
   916   }
   865   }
   917 
   866 
   918   if (log_is_enabled(Debug, safepoint, stats)) {
   867   if (log_is_enabled(Info, safepoint, stats)) {
   919     Atomic::inc(&_nof_threads_hit_polling_page);
   868     Atomic::inc(&_nof_threads_hit_polling_page);
   920   }
   869   }
   921 
   870 
   922   ThreadSafepointState* state = thread->safepoint_state();
   871   ThreadSafepointState* state = thread->safepoint_state();
   923 
   872 
   924   state->handle_polling_page_exception();
   873   state->handle_polling_page_exception();
   925 }
   874 }
   926 
   875 
   927 
   876 
   928 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
   877 void SafepointSynchronize::print_safepoint_timeout() {
   929   if (!timeout_error_printed) {
   878   if (!timeout_error_printed) {
   930     timeout_error_printed = true;
   879     timeout_error_printed = true;
   931     // Print out the thread info which didn't reach the safepoint for debugging
   880     // Print out the thread info which didn't reach the safepoint for debugging
   932     // purposes (useful when there are lots of threads in the debugger).
   881     // purposes (useful when there are lots of threads in the debugger).
   933     LogTarget(Warning, safepoint) lt;
   882     LogTarget(Warning, safepoint) lt;
   935       ResourceMark rm;
   884       ResourceMark rm;
   936       LogStream ls(lt);
   885       LogStream ls(lt);
   937 
   886 
   938       ls.cr();
   887       ls.cr();
   939       ls.print_cr("# SafepointSynchronize::begin: Timeout detected:");
   888       ls.print_cr("# SafepointSynchronize::begin: Timeout detected:");
   940       if (reason ==  _spinning_timeout) {
   889       ls.print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
   941         ls.print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
       
   942       } else if (reason == _blocking_timeout) {
       
   943         ls.print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
       
   944       }
       
   945 
       
   946       ls.print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
   890       ls.print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
   947       ThreadSafepointState *cur_state;
       
   948       for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {
   891       for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {
   949         cur_state = cur_thread->safepoint_state();
   892         if (cur_thread->safepoint_state()->is_running()) {
   950 
       
   951         if (cur_thread->thread_state() != _thread_blocked &&
       
   952           ((reason == _spinning_timeout && cur_state->is_running()) ||
       
   953              (reason == _blocking_timeout))) {
       
   954           ls.print("# ");
   893           ls.print("# ");
   955           cur_thread->print_on(&ls);
   894           cur_thread->print_on(&ls);
   956           ls.cr();
   895           ls.cr();
   957         }
   896         }
   958       }
   897       }
   962 
   901 
   963   // To debug the long safepoint, specify both AbortVMOnSafepointTimeout &
   902   // To debug the long safepoint, specify both AbortVMOnSafepointTimeout &
   964   // ShowMessageBoxOnError.
   903   // ShowMessageBoxOnError.
   965   if (AbortVMOnSafepointTimeout) {
   904   if (AbortVMOnSafepointTimeout) {
   966     fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
   905     fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
   967           SafepointTimeoutDelay, VMThread::vm_safepoint_description());
   906           SafepointTimeoutDelay, VMThread::vm_operation()->name());
   968   }
   907   }
   969 }
   908 }
   970 
       
   971 
   909 
   972 // -------------------------------------------------------------------------------------------------------
   910 // -------------------------------------------------------------------------------------------------------
   973 // Implementation of ThreadSafepointState
   911 // Implementation of ThreadSafepointState
   974 
   912 
   975 ThreadSafepointState::ThreadSafepointState(JavaThread *thread)
   913 ThreadSafepointState::ThreadSafepointState(JavaThread *thread)
  1174     }
  1112     }
  1175   }
  1113   }
  1176 }
  1114 }
  1177 
  1115 
  1178 
  1116 
  1179 //
  1117 // -------------------------------------------------------------------------------------------------------
  1180 //                     Statistics & Instrumentations
  1118 // Implementation of SafepointTracing
  1181 //
  1119 
  1182 struct SafepointStats {
  1120 jlong SafepointTracing::_last_safepoint_begin_time_ns = 0;
  1183     float  _time_stamp;                        // record when the current safepoint occurs in seconds
  1121 jlong SafepointTracing::_last_safepoint_sync_time_ns = 0;
  1184     int    _vmop_type;                         // tyep of VM operation triggers the safepoint
  1122 jlong SafepointTracing::_last_safepoint_cleanup_time_ns = 0;
  1185     int    _nof_total_threads;                 // total number of Java threads
  1123 jlong SafepointTracing::_last_safepoint_end_time_ns = 0;
  1186     int    _nof_initial_running_threads;       // total number of initially seen running threads
  1124 jlong SafepointTracing::_last_app_time_ns = 0;
  1187     int    _nof_threads_wait_to_block;         // total number of threads waiting for to block
  1125 int SafepointTracing::_nof_threads = 0;
  1188     bool   _page_armed;                        // true if polling page is armed, false otherwise
  1126 int SafepointTracing::_nof_running = 0;
  1189     int _nof_threads_hit_page_trap;            // total number of threads hitting the page trap
  1127 int SafepointTracing::_page_trap = 0;
  1190     jlong  _time_to_spin;                      // total time in millis spent in spinning
  1128 VM_Operation::VMOp_Type SafepointTracing::_current_type;
  1191     jlong  _time_to_wait_to_block;             // total time in millis spent in waiting for to block
  1129 jlong     SafepointTracing::_max_sync_time = 0;
  1192     jlong  _time_to_do_cleanups;               // total time in millis spent in performing cleanups
  1130 jlong     SafepointTracing::_max_vmop_time = 0;
  1193     jlong  _time_to_sync;                      // total time in millis spent in getting to _synchronized
  1131 uint64_t  SafepointTracing::_op_count[VM_Operation::VMOp_Terminating] = {0};
  1194     jlong  _time_to_exec_vmop;                 // total time in millis spent in vm operation itself
  1132 
  1195 };
  1133 void SafepointTracing::init() {
  1196 
  1134   // Application start
  1197 static const int _statistics_header_count = 30;
  1135   _last_safepoint_end_time_ns = os::javaTimeNanos();
  1198 static int _cur_stat_index = 0;
       
  1199 static SafepointStats safepoint_stats = {0};  // zero initialize
       
  1200 static SafepointStats* spstat = &safepoint_stats;
       
  1201 
       
  1202 static julong _safepoint_reasons[VM_Operation::VMOp_Terminating];
       
  1203 static jlong  _max_sync_time = 0;
       
  1204 static jlong  _max_vmop_time = 0;
       
  1205 
       
  1206 static jlong  cleanup_end_time = 0;
       
  1207 
       
  1208 void SafepointSynchronize::begin_statistics(int nof_threads, int nof_running) {
       
  1209 
       
  1210   spstat->_time_stamp = _ts_of_current_safepoint;
       
  1211 
       
  1212   VM_Operation *op = VMThread::vm_operation();
       
  1213   spstat->_vmop_type = op != NULL ? op->type() : VM_Operation::VMOp_None;
       
  1214   _safepoint_reasons[spstat->_vmop_type]++;
       
  1215 
       
  1216   spstat->_nof_total_threads = nof_threads;
       
  1217   spstat->_nof_initial_running_threads = nof_running;
       
  1218 
       
  1219   // Records the start time of spinning. The real time spent on spinning
       
  1220   // will be adjusted when spin is done. Same trick is applied for time
       
  1221   // spent on waiting for threads to block.
       
  1222   if (nof_running != 0) {
       
  1223     spstat->_time_to_spin = os::javaTimeNanos();
       
  1224   }  else {
       
  1225     spstat->_time_to_spin = 0;
       
  1226   }
       
  1227 }
       
  1228 
       
  1229 void SafepointSynchronize::update_statistics_on_spin_end() {
       
  1230   jlong cur_time = os::javaTimeNanos();
       
  1231 
       
  1232   spstat->_nof_threads_wait_to_block = _waiting_to_block;
       
  1233   if (spstat->_nof_initial_running_threads != 0) {
       
  1234     spstat->_time_to_spin = cur_time - spstat->_time_to_spin;
       
  1235   }
       
  1236 
       
  1237   // Records the start time of waiting for to block. Updated when block is done.
       
  1238   if (_waiting_to_block != 0) {
       
  1239     spstat->_time_to_wait_to_block = cur_time;
       
  1240   } else {
       
  1241     spstat->_time_to_wait_to_block = 0;
       
  1242   }
       
  1243 }
       
  1244 
       
  1245 void SafepointSynchronize::update_statistics_on_sync_end(jlong end_time) {
       
  1246 
       
  1247   if (spstat->_nof_threads_wait_to_block != 0) {
       
  1248     spstat->_time_to_wait_to_block = end_time -
       
  1249       spstat->_time_to_wait_to_block;
       
  1250   }
       
  1251 
       
  1252   // Records the end time of sync which will be used to calculate the total
       
  1253   // vm operation time. Again, the real time spending in syncing will be deducted
       
  1254   // from the start of the sync time later when end_statistics is called.
       
  1255   spstat->_time_to_sync = end_time - _safepoint_begin_time;
       
  1256   if (spstat->_time_to_sync > _max_sync_time) {
       
  1257     _max_sync_time = spstat->_time_to_sync;
       
  1258   }
       
  1259 
       
  1260   spstat->_time_to_do_cleanups = end_time;
       
  1261 }
       
  1262 
       
  1263 void SafepointSynchronize::update_statistics_on_cleanup_end(jlong end_time) {
       
  1264 
       
  1265   // Record how long spent in cleanup tasks.
       
  1266   spstat->_time_to_do_cleanups = end_time - spstat->_time_to_do_cleanups;
       
  1267   cleanup_end_time = end_time;
       
  1268 }
       
  1269 
       
  1270 void SafepointSynchronize::end_statistics(jlong vmop_end_time) {
       
  1271 
       
  1272   // Update the vm operation time.
       
  1273   spstat->_time_to_exec_vmop = vmop_end_time -  cleanup_end_time;
       
  1274   if (spstat->_time_to_exec_vmop > _max_vmop_time) {
       
  1275     _max_vmop_time = spstat->_time_to_exec_vmop;
       
  1276   }
       
  1277 
       
  1278   spstat->_nof_threads_hit_page_trap = _nof_threads_hit_polling_page;
       
  1279 
       
  1280   print_statistics();
       
  1281 }
  1136 }
  1282 
  1137 
  1283 // Helper method to print the header.
  1138 // Helper method to print the header.
  1284 static void print_header(outputStream* st) {
  1139 static void print_header(outputStream* st) {
  1285   // The number of spaces is significant here, and should match the format
  1140   // The number of spaces is significant here, and should match the format
  1286   // specifiers in print_statistics().
  1141   // specifiers in print_statistics().
  1287 
  1142 
  1288   st->print("          vmop                            "
  1143   st->print("VM Operation                 "
  1289             "[ threads:    total initially_running wait_to_block ]"
  1144             "[ threads: total initial_running ]"
  1290             "[ time:    spin   block    sync cleanup    vmop ] ");
  1145             "[ time:       sync    cleanup       vmop      total ]");
  1291 
  1146 
  1292   st->print_cr("page_trap_count");
  1147   st->print_cr(" page_trap_count");
  1293 }
  1148 }
  1294 
  1149 
  1295 // This prints a nice table.  To get the statistics to not shift due to the logging uptime
  1150 // This prints a nice table.  To get the statistics to not shift due to the logging uptime
  1296 // decorator, use the option as: -Xlog:safepoint+stats=debug:[outputfile]:none
  1151 // decorator, use the option as: -Xlog:safepoint+stats:[outputfile]:none
  1297 void SafepointSynchronize::print_statistics() {
  1152 void SafepointTracing::statistics_log() {
  1298   LogTarget(Debug, safepoint, stats) lt;
  1153   LogTarget(Info, safepoint, stats) lt;
  1299   assert (lt.is_enabled(), "should only be called when printing statistics is enabled");
  1154   assert (lt.is_enabled(), "should only be called when printing statistics is enabled");
  1300   LogStream ls(lt);
  1155   LogStream ls(lt);
  1301 
  1156 
       
  1157   static int _cur_stat_index = 0;
       
  1158 
  1302   // Print header every 30 entries
  1159   // Print header every 30 entries
  1303   if ((_cur_stat_index % _statistics_header_count) == 0) {
  1160   if ((_cur_stat_index % 30) == 0) {
  1304     print_header(&ls);
  1161     print_header(&ls);
  1305     _cur_stat_index = 1;  // wrap
  1162     _cur_stat_index = 1;  // wrap
  1306   } else {
  1163   } else {
  1307     _cur_stat_index++;
  1164     _cur_stat_index++;
  1308   }
  1165   }
  1309 
  1166 
  1310   ls.print("%8.3f: ", spstat->_time_stamp);
  1167   ls.print("%-28s [       "
  1311   ls.print("%-28s  [          "
  1168            INT32_FORMAT_W(8) "        " INT32_FORMAT_W(8) " "
  1312            INT32_FORMAT_W(8) " " INT32_FORMAT_W(17) " " INT32_FORMAT_W(13) " "
       
  1313            "]",
  1169            "]",
  1314            VM_Operation::name(spstat->_vmop_type),
  1170            VM_Operation::name(_current_type),
  1315            spstat->_nof_total_threads,
  1171            _nof_threads,
  1316            spstat->_nof_initial_running_threads,
  1172            _nof_running);
  1317            spstat->_nof_threads_wait_to_block);
       
  1318   // "/ MICROUNITS " is to convert the unit from nanos to millis.
       
  1319   ls.print("[       "
  1173   ls.print("[       "
  1320            INT64_FORMAT_W(7) " " INT64_FORMAT_W(7) " "
  1174            INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " "
  1321            INT64_FORMAT_W(7) " " INT64_FORMAT_W(7) " "
  1175            INT64_FORMAT_W(10) " " INT64_FORMAT_W(10) " ]",
  1322            INT64_FORMAT_W(7) " ] ",
  1176            (int64_t)(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns),
  1323            (int64_t)(spstat->_time_to_spin / MICROUNITS),
  1177            (int64_t)(_last_safepoint_cleanup_time_ns - _last_safepoint_sync_time_ns),
  1324            (int64_t)(spstat->_time_to_wait_to_block / MICROUNITS),
  1178            (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns),
  1325            (int64_t)(spstat->_time_to_sync / MICROUNITS),
  1179            (int64_t)(_last_safepoint_end_time_ns - _last_safepoint_begin_time_ns));
  1326            (int64_t)(spstat->_time_to_do_cleanups / MICROUNITS),
  1180 
  1327            (int64_t)(spstat->_time_to_exec_vmop / MICROUNITS));
  1181   ls.print_cr(INT32_FORMAT_W(16), _page_trap);
  1328 
       
  1329   ls.print_cr(INT32_FORMAT_W(15) " ", spstat->_nof_threads_hit_page_trap);
       
  1330 }
  1182 }
  1331 
  1183 
  1332 // This method will be called when VM exits. This tries to summarize the sampling.
  1184 // This method will be called when VM exits. This tries to summarize the sampling.
  1333 // Current thread may already be deleted, so don't use ResourceMark.
  1185 // Current thread may already be deleted, so don't use ResourceMark.
  1334 void SafepointSynchronize::print_stat_on_exit() {
  1186 void SafepointTracing::statistics_exit_log() {
  1335 
  1187   if (!log_is_enabled(Info, safepoint, stats)) {
       
  1188     return;
       
  1189   }
  1336   for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) {
  1190   for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) {
  1337     if (_safepoint_reasons[index] != 0) {
  1191     if (_op_count[index] != 0) {
  1338       log_debug(safepoint, stats)("%-28s" UINT64_FORMAT_W(10), VM_Operation::name(index),
  1192       log_info(safepoint, stats)("%-28s" UINT64_FORMAT_W(10), VM_Operation::name(index),
  1339                 _safepoint_reasons[index]);
  1193                _op_count[index]);
  1340     }
  1194     }
  1341   }
  1195   }
  1342 
  1196 
  1343   log_debug(safepoint, stats)("VM operations coalesced during safepoint " INT64_FORMAT,
  1197   log_info(safepoint, stats)("VM operations coalesced during safepoint " INT64_FORMAT,
  1344                               _coalesced_vmop_count);
  1198                               VMThread::get_coalesced_count());
  1345   log_debug(safepoint, stats)("Maximum sync time  " INT64_FORMAT" ms",
  1199   log_info(safepoint, stats)("Maximum sync time  " INT64_FORMAT" ns",
  1346                               (int64_t)(_max_sync_time / MICROUNITS));
  1200                               (int64_t)(_max_sync_time));
  1347   log_debug(safepoint, stats)("Maximum vm operation time (except for Exit VM operation)  "
  1201   log_info(safepoint, stats)("Maximum vm operation time (except for Exit VM operation)  "
  1348                               INT64_FORMAT " ms",
  1202                               INT64_FORMAT " ns",
  1349                               (int64_t)(_max_vmop_time / MICROUNITS));
  1203                               (int64_t)(_max_vmop_time));
  1350 }
  1204 }
       
  1205 
       
  1206 void SafepointTracing::begin(VM_Operation::VMOp_Type type) {
       
  1207   _op_count[type]++;
       
  1208   _current_type = type;
       
  1209 
       
  1210   // update the time stamp to begin recording safepoint time
       
  1211   _last_safepoint_begin_time_ns = os::javaTimeNanos();
       
  1212   _last_safepoint_sync_time_ns = 0;
       
  1213   _last_safepoint_cleanup_time_ns = 0;
       
  1214 
       
  1215   _last_app_time_ns = _last_safepoint_begin_time_ns - _last_safepoint_end_time_ns;
       
  1216   _last_safepoint_end_time_ns = 0;
       
  1217 
       
  1218   RuntimeService::record_safepoint_begin(_last_app_time_ns);
       
  1219 }
       
  1220 
       
  1221 void SafepointTracing::synchronized(int nof_threads, int nof_running, int traps) {
       
  1222   _last_safepoint_sync_time_ns = os::javaTimeNanos();
       
  1223   _nof_threads = nof_threads;
       
  1224   _nof_running = nof_running;
       
  1225   _page_trap   = traps;
       
  1226   RuntimeService::record_safepoint_synchronized(_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns);
       
  1227 }
       
  1228 
       
  1229 void SafepointTracing::cleanup() {
       
  1230   _last_safepoint_cleanup_time_ns = os::javaTimeNanos();
       
  1231 }
       
  1232 
       
  1233 void SafepointTracing::end() {
       
  1234   _last_safepoint_end_time_ns = os::javaTimeNanos();
       
  1235 
       
  1236   if (_max_sync_time < (_last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns)) {
       
  1237     _max_sync_time = _last_safepoint_sync_time_ns - _last_safepoint_begin_time_ns;
       
  1238   }
       
  1239   if (_max_vmop_time < (_last_safepoint_end_time_ns - _last_safepoint_sync_time_ns)) {
       
  1240     _max_vmop_time = _last_safepoint_end_time_ns - _last_safepoint_sync_time_ns;
       
  1241   }
       
  1242   if (log_is_enabled(Info, safepoint, stats)) {
       
  1243     statistics_log();
       
  1244   }
       
  1245 
       
  1246   log_info(safepoint)(
       
  1247      "Safepoint \"%s\", "
       
  1248      "Time since last: " JLONG_FORMAT " ns, "
       
  1249      "Reaching safepoint: " JLONG_FORMAT " ns, "
       
  1250      "At safepoint: " JLONG_FORMAT " ns, "
       
  1251      "Total: " JLONG_FORMAT " ns",
       
  1252       VM_Operation::name(_current_type),
       
  1253       _last_app_time_ns,
       
  1254       _last_safepoint_cleanup_time_ns - _last_safepoint_begin_time_ns,
       
  1255       _last_safepoint_end_time_ns     - _last_safepoint_cleanup_time_ns,
       
  1256       _last_safepoint_end_time_ns     - _last_safepoint_begin_time_ns
       
  1257      );
       
  1258 
       
  1259   RuntimeService::record_safepoint_end(_last_safepoint_end_time_ns - _last_safepoint_cleanup_time_ns);
       
  1260 }