annotate src/hotspot/share/runtime/safepoint.cpp @ 52088:181e6a03249b

8164683: Solaris: JVM abuses thread preemption control Summary: Complete removal of preemption control and command line arguments (were deprecated in 11). Reviewed-by: hseigel, pchilanomate, dholmes
author coleenp
date Fri, 10 Aug 2018 09:36:01 -0400
parents aa3bfacc912c
children c88019b32bc4
rev   line source
duke@1 1 /*
coleenp@49492 2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
coleenp@52039 26 #include "classfile/classLoaderData.inline.hpp"
gziemski@24426 27 #include "classfile/stringTable.hpp"
mgronlun@34666 28 #include "classfile/symbolTable.hpp"
stefank@7397 29 #include "classfile/systemDictionary.hpp"
stefank@7397 30 #include "code/codeCache.hpp"
stefank@7397 31 #include "code/icBuffer.hpp"
stefank@7397 32 #include "code/nmethod.hpp"
stefank@7397 33 #include "code/pcDesc.hpp"
stefank@7397 34 #include "code/scopeDesc.hpp"
pliden@30764 35 #include "gc/shared/collectedHeap.hpp"
stefank@50089 36 #include "gc/shared/gcLocker.hpp"
rkennke@46702 37 #include "gc/shared/strongRootsScope.hpp"
rkennke@46702 38 #include "gc/shared/workgroup.hpp"
stefank@7397 39 #include "interpreter/interpreter.hpp"
egahlin@50662 40 #include "jfr/jfrEvents.hpp"
rprotacio@33763 41 #include "logging/log.hpp"
stuefe@46701 42 #include "logging/logStream.hpp"
stefank@7397 43 #include "memory/resourceArea.hpp"
stefank@49824 44 #include "memory/universe.hpp"
stefank@7397 45 #include "oops/oop.inline.hpp"
coleenp@8076 46 #include "oops/symbol.hpp"
dholmes@40655 47 #include "runtime/atomic.hpp"
stefank@7397 48 #include "runtime/compilationPolicy.hpp"
stefank@7397 49 #include "runtime/deoptimization.hpp"
stefank@7397 50 #include "runtime/frame.inline.hpp"
coleenp@49914 51 #include "runtime/interfaceSupport.inline.hpp"
stefank@7397 52 #include "runtime/mutexLocker.hpp"
coleenp@51033 53 #include "runtime/orderAccess.hpp"
stefank@7397 54 #include "runtime/osThread.hpp"
stefank@7397 55 #include "runtime/safepoint.hpp"
rehn@48069 56 #include "runtime/safepointMechanism.inline.hpp"
stefank@7397 57 #include "runtime/signature.hpp"
stefank@7397 58 #include "runtime/stubCodeGenerator.hpp"
stefank@7397 59 #include "runtime/stubRoutines.hpp"
stefank@7397 60 #include "runtime/sweeper.hpp"
stefank@7397 61 #include "runtime/synchronizer.hpp"
stefank@14583 62 #include "runtime/thread.inline.hpp"
dcubed@48321 63 #include "runtime/threadSMR.hpp"
rehn@37161 64 #include "runtime/timerTrace.hpp"
stefank@7397 65 #include "services/runtimeService.hpp"
stefank@7397 66 #include "utilities/events.hpp"
jprovino@15482 67 #include "utilities/macros.hpp"
stefank@7397 68 #ifdef COMPILER1
stefank@7397 69 #include "c1/c1_globals.hpp"
stefank@7397 70 #endif
duke@1 71
egahlin@50662 72 template <typename E>
egahlin@50662 73 static void set_current_safepoint_id(E* event, int adjustment = 0) {
egahlin@50662 74 assert(event != NULL, "invariant");
egahlin@50662 75 event->set_safepointId(SafepointSynchronize::safepoint_counter() + adjustment);
egahlin@50662 76 }
egahlin@50662 77
egahlin@50662 78 static void post_safepoint_begin_event(EventSafepointBegin* event,
egahlin@50662 79 int thread_count,
egahlin@50662 80 int critical_thread_count) {
egahlin@50662 81 assert(event != NULL, "invariant");
egahlin@50662 82 assert(event->should_commit(), "invariant");
egahlin@50662 83 set_current_safepoint_id(event);
egahlin@50662 84 event->set_totalThreadCount(thread_count);
egahlin@50662 85 event->set_jniCriticalThreadCount(critical_thread_count);
egahlin@50662 86 event->commit();
egahlin@50662 87 }
egahlin@50662 88
egahlin@50662 89 static void post_safepoint_cleanup_event(EventSafepointCleanup* event) {
egahlin@50662 90 assert(event != NULL, "invariant");
egahlin@50662 91 assert(event->should_commit(), "invariant");
egahlin@50662 92 set_current_safepoint_id(event);
egahlin@50662 93 event->commit();
egahlin@50662 94 }
egahlin@50662 95
egahlin@50662 96 static void post_safepoint_synchronize_event(EventSafepointStateSynchronization* event,
egahlin@50662 97 int initial_number_of_threads,
egahlin@50662 98 int threads_waiting_to_block,
egahlin@50662 99 unsigned int iterations) {
egahlin@50662 100 assert(event != NULL, "invariant");
egahlin@50662 101 if (event->should_commit()) {
egahlin@50662 102 // Group this event together with the ones committed after the counter is increased
egahlin@50662 103 set_current_safepoint_id(event, 1);
egahlin@50662 104 event->set_initialThreadCount(initial_number_of_threads);
egahlin@50662 105 event->set_runningThreadCount(threads_waiting_to_block);
egahlin@50662 106 event->set_iterations(iterations);
egahlin@50662 107 event->commit();
egahlin@50662 108 }
egahlin@50662 109 }
egahlin@50662 110
egahlin@50662 111 static void post_safepoint_wait_blocked_event(EventSafepointWaitBlocked* event,
egahlin@50662 112 int initial_threads_waiting_to_block) {
egahlin@50662 113 assert(event != NULL, "invariant");
egahlin@50662 114 assert(event->should_commit(), "invariant");
egahlin@50662 115 set_current_safepoint_id(event);
egahlin@50662 116 event->set_runningThreadCount(initial_threads_waiting_to_block);
egahlin@50662 117 event->commit();
egahlin@50662 118 }
egahlin@50662 119
egahlin@50662 120 static void post_safepoint_cleanup_task_event(EventSafepointCleanupTask* event,
egahlin@50662 121 const char* name) {
egahlin@50662 122 assert(event != NULL, "invariant");
egahlin@50662 123 if (event->should_commit()) {
egahlin@50662 124 set_current_safepoint_id(event);
egahlin@50662 125 event->set_name(name);
egahlin@50662 126 event->commit();
egahlin@50662 127 }
egahlin@50662 128 }
egahlin@50662 129
egahlin@50662 130 static void post_safepoint_end_event(EventSafepointEnd* event) {
egahlin@50662 131 assert(event != NULL, "invariant");
egahlin@50662 132 if (event->should_commit()) {
egahlin@50662 133 // Group this event together with the ones committed before the counter increased
egahlin@50662 134 set_current_safepoint_id(event, -1);
egahlin@50662 135 event->commit();
egahlin@50662 136 }
egahlin@50662 137 }
egahlin@50662 138
duke@1 139 // --------------------------------------------------------------------------------------------------
duke@1 140 // Implementation of Safepoint begin/end
duke@1 141
duke@1 142 SafepointSynchronize::SynchronizeState volatile SafepointSynchronize::_state = SafepointSynchronize::_not_synchronized;
duke@1 143 volatile int SafepointSynchronize::_waiting_to_block = 0;
duke@1 144 volatile int SafepointSynchronize::_safepoint_counter = 0;
never@11631 145 int SafepointSynchronize::_current_jni_active_count = 0;
xlu@5042 146 long SafepointSynchronize::_end_of_last_safepoint = 0;
hseigel@49600 147 int SafepointSynchronize::_defer_thr_suspend_loop_count = 4000;
hseigel@49600 148 static const int safepoint_spin_before_yield = 2000;
duke@1 149 static volatile int PageArmed = 0 ; // safepoint polling page is RO|RW vs PROT_NONE
duke@1 150 static volatile int TryingToBlock = 0 ; // proximate value -- for advisory use only
duke@1 151 static bool timeout_error_printed = false;
duke@1 152
coleenp@51627 153
coleenp@51627 154 // Statistic related statics
coleenp@51627 155 julong SafepointSynchronize::_coalesced_vmop_count = 0;
coleenp@51627 156 static jlong _safepoint_begin_time = 0;
coleenp@51627 157 static float _ts_of_current_safepoint = 0.0f;
coleenp@51627 158 static volatile int _nof_threads_hit_polling_page = 0;
coleenp@51627 159
duke@1 160 // Roll all threads forward to a safepoint and suspend them all
duke@1 161 void SafepointSynchronize::begin() {
dsimms@37176 162 EventSafepointBegin begin_event;
duke@1 163 Thread* myThread = Thread::current();
duke@1 164 assert(myThread->is_VM_thread(), "Only VM thread may execute a safepoint");
duke@1 165
coleenp@51627 166 if (log_is_enabled(Debug, safepoint, stats)) {
xlu@5042 167 _safepoint_begin_time = os::javaTimeNanos();
xlu@5042 168 _ts_of_current_safepoint = tty->time_stamp().seconds();
coleenp@51627 169 _nof_threads_hit_polling_page = 0;
xlu@5042 170 }
duke@1 171
eosterlund@47791 172 Universe::heap()->safepoint_synchronize_begin();
duke@1 173
duke@1 174 // By getting the Threads_lock, we assure that no threads are about to start or
duke@1 175 // exit. It is released again in SafepointSynchronize::end().
duke@1 176 Threads_lock->lock();
duke@1 177
duke@1 178 assert( _state == _not_synchronized, "trying to safepoint synchronize with wrong state");
duke@1 179
duke@1 180 int nof_threads = Threads::number_of_threads();
duke@1 181
coleenp@48775 182 log_debug(safepoint)("Safepoint synchronization initiated. (%d threads)", nof_threads);
duke@1 183
duke@1 184 RuntimeService::record_safepoint_begin();
duke@1 185
duke@1 186 MutexLocker mu(Safepoint_lock);
duke@1 187
never@11631 188 // Reset the count of active JNI critical threads
never@11631 189 _current_jni_active_count = 0;
never@11631 190
duke@1 191 // Set number of threads to wait for, before we initiate the callbacks
duke@1 192 _waiting_to_block = nof_threads;
duke@1 193 TryingToBlock = 0 ;
duke@1 194 int still_running = nof_threads;
duke@1 195
duke@1 196 // Save the starting time, so that it can be compared to see if this has taken
duke@1 197 // too long to complete.
goetz@33589 198 jlong safepoint_limit_time = 0;
duke@1 199 timeout_error_printed = false;
duke@1 200
duke@1 201 // Begin the process of bringing the system to a safepoint.
duke@1 202 // Java threads can be in several different states and are
duke@1 203 // stopped by different mechanisms:
duke@1 204 //
duke@1 205 // 1. Running interpreted
jwilhelm@22551 206 // The interpreter dispatch table is changed to force it to
duke@1 207 // check for a safepoint condition between bytecodes.
duke@1 208 // 2. Running in native code
duke@1 209 // When returning from the native code, a Java thread must check
duke@1 210 // the safepoint _state to see if we must block. If the
duke@1 211 // VM thread sees a Java thread in native, it does
duke@1 212 // not wait for this thread to block. The order of the memory
duke@1 213 // writes and reads of both the safepoint state and the Java
duke@1 214 // threads state is critical. In order to guarantee that the
duke@1 215 // memory writes are serialized with respect to each other,
duke@1 216 // the VM thread issues a memory barrier instruction
duke@1 217 // (on MP systems). In order to avoid the overhead of issuing
duke@1 218 // a memory barrier for each Java thread making native calls, each Java
duke@1 219 // thread performs a write to a single memory page after changing
duke@1 220 // the thread state. The VM thread performs a sequence of
duke@1 221 // mprotect OS calls which forces all previous writes from all
duke@1 222 // Java threads to be serialized. This is done in the
duke@1 223 // os::serialize_thread_states() call. This has proven to be
duke@1 224 // much more efficient than executing a membar instruction
duke@1 225 // on every call to native code.
duke@1 226 // 3. Running compiled Code
duke@1 227 // Compiled code reads a global (Safepoint Polling) page that
duke@1 228 // is set to fault if we are trying to get to a safepoint.
duke@1 229 // 4. Blocked
duke@1 230 // A thread which is blocked will not be allowed to return from the
duke@1 231 // block condition until the safepoint operation is complete.
duke@1 232 // 5. In VM or Transitioning between states
duke@1 233 // If a Java thread is currently running in the VM or transitioning
duke@1 234 // between states, the safepointing code will wait for the thread to
duke@1 235 // block itself when it attempts transitions to a new state.
duke@1 236 //
dsimms@37176 237 {
egahlin@40664 238 EventSafepointStateSynchronization sync_event;
dsimms@37176 239 int initial_running = 0;
duke@1 240
dsimms@37176 241 _state = _synchronizing;
rehn@48069 242
rehn@48069 243 if (SafepointMechanism::uses_thread_local_poll()) {
rehn@48069 244 // Arming the per thread poll while having _state != _not_synchronized means safepointing
rehn@48069 245 log_trace(safepoint)("Setting thread local yield flag for threads");
mdoerr@51582 246 OrderAccess::storestore(); // storestore, global state -> local state
dcubed@48321 247 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
rehn@48069 248 // Make sure the threads start polling, it is time to yield.
mdoerr@51582 249 SafepointMechanism::arm_local_poll(cur);
rehn@48069 250 }
rehn@48069 251 }
rehn@48069 252 OrderAccess::fence(); // storestore|storeload, global state -> local state
duke@1 253
dsimms@37176 254 // Flush all thread states to memory
dsimms@37176 255 if (!UseMembar) {
dsimms@37176 256 os::serialize_thread_states();
dsimms@37176 257 }
duke@1 258
rehn@48069 259 if (SafepointMechanism::uses_global_page_poll()) {
rehn@48069 260 // Make interpreter safepoint aware
rehn@48069 261 Interpreter::notice_safepoints();
duke@1 262
hseigel@49600 263 // Make polling safepoint aware
hseigel@49600 264 guarantee (PageArmed == 0, "invariant") ;
hseigel@49600 265 PageArmed = 1 ;
hseigel@49600 266 os::make_polling_page_unreadable();
dsimms@37176 267 }
dsimms@37176 268
dsimms@37176 269 // Consider using active_processor_count() ... but that call is expensive.
dsimms@37176 270 int ncpus = os::processor_count() ;
dcubed@48321 271 unsigned int iterations = 0;
duke@1 272
dcubed@48321 273 {
dcubed@48321 274 JavaThreadIteratorWithHandle jtiwh;
duke@1 275 #ifdef ASSERT
dcubed@48321 276 for (; JavaThread *cur = jtiwh.next(); ) {
dcubed@48321 277 assert(cur->safepoint_state()->is_running(), "Illegal initial state");
dcubed@48321 278 // Clear the visited flag to ensure that the critical counts are collected properly.
dcubed@48321 279 cur->set_visited_for_critical_count(false);
dcubed@48321 280 }
duke@1 281 #endif // ASSERT
duke@1 282
dcubed@48321 283 if (SafepointTimeout)
dcubed@48321 284 safepoint_limit_time = os::javaTimeNanos() + (jlong)SafepointTimeoutDelay * MICROUNITS;
duke@1 285
dcubed@48321 286 // Iterate through all threads until it have been determined how to stop them all at a safepoint
dcubed@48321 287 int steps = 0 ;
dcubed@48321 288 while(still_running > 0) {
dcubed@48321 289 jtiwh.rewind();
dcubed@48321 290 for (; JavaThread *cur = jtiwh.next(); ) {
dcubed@48321 291 assert(!cur->is_ConcurrentGC_thread(), "A concurrent GC thread is unexpectly being suspended");
dcubed@48321 292 ThreadSafepointState *cur_state = cur->safepoint_state();
dcubed@48321 293 if (cur_state->is_running()) {
dcubed@48321 294 cur_state->examine_state_of_thread();
dcubed@48321 295 if (!cur_state->is_running()) {
dcubed@48321 296 still_running--;
dcubed@48321 297 // consider adjusting steps downward:
dcubed@48321 298 // steps = 0
dcubed@48321 299 // steps -= NNN
dcubed@48321 300 // steps >>= 1
dcubed@48321 301 // steps = MIN(steps, 2000-100)
dcubed@48321 302 // if (iterations != 0) steps -= NNN
dcubed@48321 303 }
dcubed@48321 304 LogTarget(Trace, safepoint) lt;
dcubed@48321 305 if (lt.is_enabled()) {
dcubed@48321 306 ResourceMark rm;
dcubed@48321 307 LogStream ls(lt);
dcubed@48321 308 cur_state->print_on(&ls);
dcubed@48321 309 }
dsimms@37176 310 }
duke@1 311 }
dsimms@37176 312
dcubed@48321 313 if (iterations == 0) {
dcubed@48321 314 initial_running = still_running;
coleenp@51627 315 if (log_is_enabled(Debug, safepoint, stats)) {
dcubed@48321 316 begin_statistics(nof_threads, still_running);
dcubed@48321 317 }
dsimms@37176 318 }
dsimms@37176 319
dcubed@48321 320 if (still_running > 0) {
dcubed@48321 321 // Check for if it takes to long
dcubed@48321 322 if (SafepointTimeout && safepoint_limit_time < os::javaTimeNanos()) {
dcubed@48321 323 print_safepoint_timeout(_spinning_timeout);
dsimms@37176 324 }
dsimms@37176 325
dcubed@48321 326 // Spin to avoid context switching.
dcubed@48321 327 // There's a tension between allowing the mutators to run (and rendezvous)
dcubed@48321 328 // vs spinning. As the VM thread spins, wasting cycles, it consumes CPU that
dcubed@48321 329 // a mutator might otherwise use profitably to reach a safepoint. Excessive
dcubed@48321 330 // spinning by the VM thread on a saturated system can increase rendezvous latency.
dcubed@48321 331 // Blocking or yielding incur their own penalties in the form of context switching
dcubed@48321 332 // and the resultant loss of $ residency.
dcubed@48321 333 //
dcubed@48321 334 // Further complicating matters is that yield() does not work as naively expected
dcubed@48321 335 // on many platforms -- yield() does not guarantee that any other ready threads
dcubed@48321 336 // will run. As such we revert to naked_short_sleep() after some number of iterations.
dcubed@48321 337 // nakes_short_sleep() is implemented as a short unconditional sleep.
dcubed@48321 338 // Typical operating systems round a "short" sleep period up to 10 msecs, so sleeping
dcubed@48321 339 // can actually increase the time it takes the VM thread to detect that a system-wide
dcubed@48321 340 // stop-the-world safepoint has been reached. In a pathological scenario such as that
dcubed@48321 341 // described in CR6415670 the VMthread may sleep just before the mutator(s) become safe.
dcubed@48321 342 // In that case the mutators will be stalled waiting for the safepoint to complete and the
dcubed@48321 343 // the VMthread will be sleeping, waiting for the mutators to rendezvous. The VMthread
dcubed@48321 344 // will eventually wake up and detect that all mutators are safe, at which point
dcubed@48321 345 // we'll again make progress.
dcubed@48321 346 //
dcubed@48321 347 // Beware too that that the VMThread typically runs at elevated priority.
dcubed@48321 348 // Its default priority is higher than the default mutator priority.
dcubed@48321 349 // Obviously, this complicates spinning.
dcubed@48321 350 //
dcubed@48321 351 // Note too that on Windows XP SwitchThreadTo() has quite different behavior than Sleep(0).
dcubed@48321 352 // Sleep(0) will _not yield to lower priority threads, while SwitchThreadTo() will.
dcubed@48321 353 //
dcubed@48321 354 // See the comments in synchronizer.cpp for additional remarks on spinning.
dcubed@48321 355 //
dcubed@48321 356 // In the future we might:
coleenp@52088 357 // -- Modify the safepoint scheme to avoid potentially unbounded spinning.
dcubed@48321 358 // This is tricky as the path used by a thread exiting the JVM (say on
dcubed@48321 359 // on JNI call-out) simply stores into its state field. The burden
dcubed@48321 360 // is placed on the VM thread, which must poll (spin).
coleenp@52088 361 // -- Find something useful to do while spinning. If the safepoint is GC-related
dcubed@48321 362 // we might aggressively scan the stacks of threads that are already safe.
coleenp@52088 363 // -- YieldTo() any still-running mutators that are ready but OFFPROC.
coleenp@52088 364 // -- Check system saturation. If the system is not fully saturated then
dcubed@48321 365 // simply spin and avoid sleep/yield.
coleenp@52088 366 // -- As still-running mutators rendezvous they could unpark the sleeping
dcubed@48321 367 // VMthread. This works well for still-running mutators that become
dcubed@48321 368 // safe. The VMthread must still poll for mutators that call-out.
coleenp@52088 369 // -- Drive the policy on time-since-begin instead of iterations.
coleenp@52088 370 // -- Consider making the spin duration a function of the # of CPUs:
dcubed@48321 371 // Spin = (((ncpus-1) * M) + K) + F(still_running)
dcubed@48321 372 // Alternately, instead of counting iterations of the outer loop
dcubed@48321 373 // we could count the # of threads visited in the inner loop, above.
coleenp@52088 374 // -- On windows consider using the return value from SwitchThreadTo()
dcubed@48321 375 // to drive subsequent spin/SwitchThreadTo()/Sleep(N) decisions.
dcubed@48321 376
hseigel@49600 377 if (int(iterations) == -1) { // overflow - something is wrong.
hseigel@49600 378 // We can only overflow here when we are using global
hseigel@49600 379 // polling pages. We keep this guarantee in its original
hseigel@49600 380 // form so that searches of the bug database for this
hseigel@49600 381 // failure mode find the right bugs.
hseigel@49600 382 guarantee (PageArmed == 0, "invariant");
dcubed@48321 383 }
dcubed@48321 384
dcubed@48321 385 // Instead of (ncpus > 1) consider either (still_running < (ncpus + EPSILON)) or
dcubed@48321 386 // ((still_running + _waiting_to_block - TryingToBlock)) < ncpus)
dcubed@48321 387 ++steps ;
hseigel@49600 388 if (ncpus > 1 && steps < safepoint_spin_before_yield) {
dcubed@48321 389 SpinPause() ; // MP-Polite spin
dcubed@48321 390 } else
hseigel@49600 391 if (steps < _defer_thr_suspend_loop_count) {
dcubed@48321 392 os::naked_yield() ;
dcubed@48321 393 } else {
dcubed@48321 394 os::naked_short_sleep(1);
dcubed@48321 395 }
dcubed@48321 396
dcubed@48321 397 iterations ++ ;
dcubed@48321 398 }
dcubed@48321 399 assert(iterations < (uint)max_jint, "We have been iterating in the safepoint loop too long");
dsimms@37176 400 }
dcubed@48321 401 } // ThreadsListHandle destroyed here.
dsimms@37176 402 assert(still_running == 0, "sanity check");
dsimms@37176 403
coleenp@51627 404 if (log_is_enabled(Debug, safepoint, stats)) {
dsimms@37176 405 update_statistics_on_spin_end();
dsimms@37176 406 }
dsimms@37176 407 if (sync_event.should_commit()) {
egahlin@50662 408 post_safepoint_synchronize_event(&sync_event, initial_running, _waiting_to_block, iterations);
dsimms@37176 409 }
egahlin@50662 410 }
dsimms@37176 411
dsimms@37176 412 // wait until all threads are stopped
dsimms@37176 413 {
dsimms@37176 414 EventSafepointWaitBlocked wait_blocked_event;
dsimms@37176 415 int initial_waiting_to_block = _waiting_to_block;
dsimms@37176 416
dsimms@37176 417 while (_waiting_to_block > 0) {
dsimms@37176 418 log_debug(safepoint)("Waiting for %d thread(s) to block", _waiting_to_block);
dsimms@37176 419 if (!SafepointTimeout || timeout_error_printed) {
dsimms@37176 420 Safepoint_lock->wait(true); // true, means with no safepoint checks
dsimms@37176 421 } else {
dsimms@37176 422 // Compute remaining time
dsimms@37176 423 jlong remaining_time = safepoint_limit_time - os::javaTimeNanos();
dsimms@37176 424
dsimms@37176 425 // If there is no remaining time, then there is an error
dsimms@37176 426 if (remaining_time < 0 || Safepoint_lock->wait(true, remaining_time / MICROUNITS)) {
dsimms@37176 427 print_safepoint_timeout(_blocking_timeout);
rprotacio@33763 428 }
duke@1 429 }
duke@1 430 }
dsimms@37176 431 assert(_waiting_to_block == 0, "sanity check");
duke@1 432
dsimms@37176 433 #ifndef PRODUCT
dsimms@37176 434 if (SafepointTimeout) {
dsimms@37176 435 jlong current_time = os::javaTimeNanos();
dsimms@37176 436 if (safepoint_limit_time < current_time) {
coleenp@51627 437 log_warning(safepoint)("# SafepointSynchronize: Finished after "
dsimms@37176 438 INT64_FORMAT_W(6) " ms",
jwilhelm@46630 439 (int64_t)((current_time - safepoint_limit_time) / MICROUNITS +
jwilhelm@46630 440 (jlong)SafepointTimeoutDelay));
duke@1 441 }
duke@1 442 }
duke@1 443 #endif
duke@1 444
dsimms@37176 445 assert((_safepoint_counter & 0x1) == 0, "must be even");
dsimms@37176 446 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
dsimms@37176 447 _safepoint_counter ++;
duke@1 448
dsimms@37176 449 // Record state
dsimms@37176 450 _state = _synchronized;
duke@1 451
dsimms@37176 452 OrderAccess::fence();
dsimms@37176 453 if (wait_blocked_event.should_commit()) {
egahlin@50662 454 post_safepoint_wait_blocked_event(&wait_blocked_event, initial_waiting_to_block);
dsimms@37176 455 }
egahlin@50662 456 }
duke@1 457
never@12108 458 #ifdef ASSERT
dcubed@48321 459 // Make sure all the threads were visited.
dcubed@48321 460 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur = jtiwh.next(); ) {
never@12108 461 assert(cur->was_visited_for_critical_count(), "missed a thread");
never@12108 462 }
never@12108 463 #endif // ASSERT
never@12108 464
never@11631 465 // Update the count of active JNI critical regions
david@35492 466 GCLocker::set_jni_lock_count(_current_jni_active_count);
never@11631 467
coleenp@48775 468 log_info(safepoint)("Entering safepoint region: %s", VMThread::vm_safepoint_description());
duke@1 469
duke@1 470 RuntimeService::record_safepoint_synchronized();
coleenp@51627 471 if (log_is_enabled(Debug, safepoint, stats)) {
duke@1 472 update_statistics_on_sync_end(os::javaTimeNanos());
duke@1 473 }
duke@1 474
duke@1 475 // Call stuff that needs to be run when a safepoint is just about to be completed
dsimms@37176 476 {
dsimms@37176 477 EventSafepointCleanup cleanup_event;
dsimms@37176 478 do_cleanup_tasks();
dsimms@37176 479 if (cleanup_event.should_commit()) {
egahlin@50662 480 post_safepoint_cleanup_event(&cleanup_event);
dsimms@37176 481 }
dsimms@37176 482 }
xlu@5042 483
coleenp@51627 484 if (log_is_enabled(Debug, safepoint, stats)) {
xlu@5042 485 // Record how much time spend on the above cleanup tasks
xlu@5042 486 update_statistics_on_cleanup_end(os::javaTimeNanos());
xlu@5042 487 }
egahlin@50662 488
dsimms@37176 489 if (begin_event.should_commit()) {
egahlin@50662 490 post_safepoint_begin_event(&begin_event, nof_threads, _current_jni_active_count);
dsimms@37176 491 }
duke@1 492 }
duke@1 493
duke@1 494 // Wake up all threads, so they are ready to resume execution after the safepoint
duke@1 495 // operation has been carried out
duke@1 496 void SafepointSynchronize::end() {
duke@1 497 assert(Threads_lock->owned_by_self(), "must hold Threads_lock");
duke@1 498 assert((_safepoint_counter & 0x1) == 1, "must be odd");
egahlin@50662 499 EventSafepointEnd event;
duke@1 500 _safepoint_counter ++;
duke@1 501 // memory fence isn't required here since an odd _safepoint_counter
duke@1 502 // value can do no harm and a fence is issued below anyway.
duke@1 503
duke@1 504 DEBUG_ONLY(Thread* myThread = Thread::current();)
duke@1 505 assert(myThread->is_VM_thread(), "Only VM thread can execute a safepoint");
duke@1 506
coleenp@51627 507 if (log_is_enabled(Debug, safepoint, stats)) {
duke@1 508 end_statistics(os::javaTimeNanos());
duke@1 509 }
duke@1 510
dcubed@48321 511 {
dcubed@48321 512 JavaThreadIteratorWithHandle jtiwh;
duke@1 513 #ifdef ASSERT
dcubed@48321 514 // A pending_exception cannot be installed during a safepoint. The threads
dcubed@48321 515 // may install an async exception after they come back from a safepoint into
dcubed@48321 516 // pending_exception after they unblock. But that should happen later.
dcubed@48321 517 for (; JavaThread *cur = jtiwh.next(); ) {
dcubed@48321 518 assert (!(cur->has_pending_exception() &&
dcubed@48321 519 cur->safepoint_state()->is_at_poll_safepoint()),
dcubed@48321 520 "safepoint installed a pending exception");
dcubed@48321 521 }
duke@1 522 #endif // ASSERT
duke@1 523
dcubed@48321 524 if (PageArmed) {
dcubed@48321 525 assert(SafepointMechanism::uses_global_page_poll(), "sanity");
dcubed@48321 526 // Make polling safepoint aware
dcubed@48321 527 os::make_polling_page_readable();
dcubed@48321 528 PageArmed = 0 ;
duke@1 529 }
duke@1 530
dcubed@48321 531 if (SafepointMechanism::uses_global_page_poll()) {
dcubed@48321 532 // Remove safepoint check from interpreter
dcubed@48321 533 Interpreter::ignore_safepoints();
dcubed@48321 534 }
duke@1 535
dcubed@48321 536 {
dcubed@48321 537 MutexLocker mu(Safepoint_lock);
duke@1 538
dcubed@48321 539 assert(_state == _synchronized, "must be synchronized before ending safepoint synchronization");
dcubed@48321 540
dcubed@48321 541 if (SafepointMechanism::uses_thread_local_poll()) {
dcubed@48321 542 _state = _not_synchronized;
dcubed@48321 543 OrderAccess::storestore(); // global state -> local state
dcubed@48321 544 jtiwh.rewind();
dcubed@48321 545 for (; JavaThread *current = jtiwh.next(); ) {
dcubed@48321 546 ThreadSafepointState* cur_state = current->safepoint_state();
dcubed@48321 547 cur_state->restart(); // TSS _running
mdoerr@51582 548 SafepointMechanism::disarm_local_poll(current);
dcubed@48321 549 }
coleenp@48775 550 log_info(safepoint)("Leaving safepoint region");
dcubed@48321 551 } else {
dcubed@48321 552 // Set to not synchronized, so the threads will not go into the signal_thread_blocked method
dcubed@48321 553 // when they get restarted.
dcubed@48321 554 _state = _not_synchronized;
dcubed@48321 555 OrderAccess::fence();
dcubed@48321 556
coleenp@48775 557 log_info(safepoint)("Leaving safepoint region");
dcubed@48321 558
dcubed@48321 559 // Start suspended threads
dcubed@48321 560 jtiwh.rewind();
dcubed@48321 561 for (; JavaThread *current = jtiwh.next(); ) {
dcubed@48321 562 ThreadSafepointState* cur_state = current->safepoint_state();
dcubed@48321 563 assert(cur_state->type() != ThreadSafepointState::_running, "Thread not suspended at safepoint");
dcubed@48321 564 cur_state->restart();
dcubed@48321 565 assert(cur_state->is_running(), "safepoint state has not been reset");
dcubed@48321 566 }
dcubed@48321 567 }
dcubed@48321 568
dcubed@48321 569 RuntimeService::record_safepoint_end();
dcubed@48321 570
dcubed@48321 571 // Release threads lock, so threads can be created/destroyed again.
dcubed@48321 572 // It will also release all threads blocked in signal_thread_blocked.
dcubed@48321 573 Threads_lock->unlock();
dcubed@48321 574 }
dcubed@48321 575 } // ThreadsListHandle destroyed here.
dcubed@48321 576
eosterlund@47791 577 Universe::heap()->safepoint_synchronize_end();
jwilhelm@22551 578 // record this time so VMThread can keep track how much time has elapsed
xlu@5042 579 // since last safepoint.
xlu@5042 580 _end_of_last_safepoint = os::javaTimeMillis();
dsimms@37176 581 if (event.should_commit()) {
egahlin@50662 582 post_safepoint_end_event(&event);
dsimms@37176 583 }
duke@1 584 }
duke@1 585
duke@1 586 bool SafepointSynchronize::is_cleanup_needed() {
rehn@46541 587 // Need a safepoint if there are many monitors to deflate.
rehn@46541 588 if (ObjectSynchronizer::is_cleanup_needed()) return true;
duke@1 589 // Need a safepoint if some inline cache buffers is non-empty
duke@1 590 if (!InlineCacheBuffer::is_empty()) return true;
duke@1 591 return false;
duke@1 592 }
duke@1 593
rkennke@46702 594 class ParallelSPCleanupThreadClosure : public ThreadClosure {
rkennke@46702 595 private:
rkennke@46702 596 CodeBlobClosure* _nmethod_cl;
rkennke@46702 597 DeflateMonitorCounters* _counters;
rkennke@46702 598
rkennke@46702 599 public:
rkennke@46702 600 ParallelSPCleanupThreadClosure(DeflateMonitorCounters* counters) :
tschatzl@52035 601 _nmethod_cl(NMethodSweeper::prepare_mark_active_nmethods()), _counters(counters) {}
rkennke@46702 602
rkennke@46702 603 void do_thread(Thread* thread) {
rkennke@46702 604 ObjectSynchronizer::deflate_thread_local_monitors(thread, _counters);
rkennke@46702 605 if (_nmethod_cl != NULL && thread->is_Java_thread() &&
rkennke@46702 606 ! thread->is_Code_cache_sweeper_thread()) {
rkennke@46702 607 JavaThread* jt = (JavaThread*) thread;
rkennke@46702 608 jt->nmethods_do(_nmethod_cl);
rkennke@46702 609 }
rkennke@46702 610 }
rkennke@46702 611 };
rkennke@46702 612
rkennke@46702 613 class ParallelSPCleanupTask : public AbstractGangTask {
rkennke@46702 614 private:
rkennke@46702 615 SubTasksDone _subtasks;
rkennke@46702 616 ParallelSPCleanupThreadClosure _cleanup_threads_cl;
rkennke@46702 617 uint _num_workers;
rkennke@46702 618 DeflateMonitorCounters* _counters;
rkennke@46702 619 public:
rkennke@46702 620 ParallelSPCleanupTask(uint num_workers, DeflateMonitorCounters* counters) :
rkennke@46702 621 AbstractGangTask("Parallel Safepoint Cleanup"),
tschatzl@52035 622 _subtasks(SubTasksDone(SafepointSynchronize::SAFEPOINT_CLEANUP_NUM_TASKS)),
rkennke@46702 623 _cleanup_threads_cl(ParallelSPCleanupThreadClosure(counters)),
rkennke@46702 624 _num_workers(num_workers),
rkennke@46702 625 _counters(counters) {}
rkennke@46702 626
rkennke@46702 627 void work(uint worker_id) {
rkennke@46702 628 // All threads deflate monitors and mark nmethods (if necessary).
rkennke@47792 629 Threads::possibly_parallel_threads_do(true, &_cleanup_threads_cl);
rkennke@46702 630
rkennke@46702 631 if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_DEFLATE_MONITORS)) {
rkennke@46702 632 const char* name = "deflating idle monitors";
rkennke@46702 633 EventSafepointCleanupTask event;
rkennke@46702 634 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
rkennke@46702 635 ObjectSynchronizer::deflate_idle_monitors(_counters);
egahlin@50662 636 if (event.should_commit()) {
egahlin@50662 637 post_safepoint_cleanup_task_event(&event, name);
egahlin@50662 638 }
rkennke@46702 639 }
rkennke@46702 640
rkennke@46702 641 if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_UPDATE_INLINE_CACHES)) {
rkennke@46702 642 const char* name = "updating inline caches";
rkennke@46702 643 EventSafepointCleanupTask event;
rkennke@46702 644 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
rkennke@46702 645 InlineCacheBuffer::update_inline_caches();
egahlin@50662 646 if (event.should_commit()) {
egahlin@50662 647 post_safepoint_cleanup_task_event(&event, name);
egahlin@50662 648 }
rkennke@46702 649 }
rkennke@46702 650
rkennke@46702 651 if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_COMPILATION_POLICY)) {
rkennke@46702 652 const char* name = "compilation policy safepoint handler";
rkennke@46702 653 EventSafepointCleanupTask event;
rkennke@46702 654 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
rkennke@46702 655 CompilationPolicy::policy()->do_safepoint_work();
egahlin@50662 656 if (event.should_commit()) {
egahlin@50662 657 post_safepoint_cleanup_task_event(&event, name);
egahlin@50662 658 }
rkennke@46702 659 }
rkennke@46702 660
rkennke@46702 661 if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_SYMBOL_TABLE_REHASH)) {
rkennke@46702 662 if (SymbolTable::needs_rehashing()) {
rkennke@46702 663 const char* name = "rehashing symbol table";
rkennke@46702 664 EventSafepointCleanupTask event;
rkennke@46702 665 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
rkennke@46702 666 SymbolTable::rehash_table();
egahlin@50662 667 if (event.should_commit()) {
egahlin@50662 668 post_safepoint_cleanup_task_event(&event, name);
egahlin@50662 669 }
rkennke@46702 670 }
rkennke@46702 671 }
rkennke@46702 672
rkennke@46702 673 if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_STRING_TABLE_REHASH)) {
rkennke@46702 674 if (StringTable::needs_rehashing()) {
rkennke@46702 675 const char* name = "rehashing string table";
rkennke@46702 676 EventSafepointCleanupTask event;
rkennke@46702 677 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
rkennke@46702 678 StringTable::rehash_table();
egahlin@50662 679 if (event.should_commit()) {
egahlin@50662 680 post_safepoint_cleanup_task_event(&event, name);
egahlin@50662 681 }
rkennke@46702 682 }
rkennke@46702 683 }
rkennke@46702 684
rkennke@46702 685 if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_CLD_PURGE)) {
rkennke@46702 686 // CMS delays purging the CLDG until the beginning of the next safepoint and to
rkennke@46702 687 // make sure concurrent sweep is done
rkennke@46702 688 const char* name = "purging class loader data graph";
rkennke@46702 689 EventSafepointCleanupTask event;
rkennke@46702 690 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
rkennke@46702 691 ClassLoaderDataGraph::purge_if_needed();
egahlin@50662 692 if (event.should_commit()) {
egahlin@50662 693 post_safepoint_cleanup_task_event(&event, name);
egahlin@50662 694 }
rkennke@46702 695 }
gziemski@47955 696
gziemski@47955 697 if (!_subtasks.is_task_claimed(SafepointSynchronize::SAFEPOINT_CLEANUP_SYSTEM_DICTIONARY_RESIZE)) {
gziemski@47955 698 const char* name = "resizing system dictionaries";
gziemski@47955 699 EventSafepointCleanupTask event;
gziemski@47955 700 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
gziemski@47955 701 ClassLoaderDataGraph::resize_if_needed();
egahlin@50662 702 if (event.should_commit()) {
egahlin@50662 703 post_safepoint_cleanup_task_event(&event, name);
egahlin@50662 704 }
gziemski@47955 705 }
coleenp@52039 706
rkennke@46702 707 _subtasks.all_tasks_completed(_num_workers);
rkennke@46702 708 }
rkennke@46702 709 };
rkennke@46702 710
rkennke@46702 711 // Various cleaning tasks that should be done periodically at safepoints.
duke@1 712 void SafepointSynchronize::do_cleanup_tasks() {
rkennke@46702 713
rkennke@46702 714 TraceTime timer("safepoint cleanup tasks", TRACETIME_LOG(Info, safepoint, cleanup));
rkennke@46702 715
rkennke@46702 716 // Prepare for monitor deflation.
rkennke@46702 717 DeflateMonitorCounters deflate_counters;
rkennke@46702 718 ObjectSynchronizer::prepare_deflate_idle_monitors(&deflate_counters);
rkennke@46702 719
rkennke@46702 720 CollectedHeap* heap = Universe::heap();
rkennke@46702 721 assert(heap != NULL, "heap not initialized yet?");
rkennke@46702 722 WorkGang* cleanup_workers = heap->get_safepoint_workers();
rkennke@46702 723 if (cleanup_workers != NULL) {
rkennke@46702 724 // Parallel cleanup using GC provided thread pool.
rkennke@46702 725 uint num_cleanup_workers = cleanup_workers->active_workers();
rkennke@46702 726 ParallelSPCleanupTask cleanup(num_cleanup_workers, &deflate_counters);
rkennke@46702 727 StrongRootsScope srs(num_cleanup_workers);
rkennke@46702 728 cleanup_workers->run_task(&cleanup);
rkennke@46702 729 } else {
rkennke@46702 730 // Serial cleanup using VMThread.
rkennke@46702 731 ParallelSPCleanupTask cleanup(1, &deflate_counters);
rkennke@46702 732 StrongRootsScope srs(1);
rkennke@46702 733 cleanup.work(0);
duke@1 734 }
duke@1 735
coleenp@52039 736 // Needs to be done single threaded by the VMThread. This walks
coleenp@52039 737 // the thread stacks looking for references to metadata before
coleenp@52039 738 // deciding to remove it from the metaspaces.
coleenp@52039 739 if (ClassLoaderDataGraph::should_clean_metaspaces_and_reset()) {
coleenp@52039 740 const char* name = "cleanup live ClassLoaderData metaspaces";
coleenp@52039 741 TraceTime timer(name, TRACETIME_LOG(Info, safepoint, cleanup));
coleenp@52039 742 ClassLoaderDataGraph::walk_metadata_and_clean_metaspaces();
coleenp@52039 743 }
coleenp@52039 744
rkennke@46702 745 // Finish monitor deflation.
rkennke@46702 746 ObjectSynchronizer::finish_deflate_idle_monitors(&deflate_counters);
coleenp@52039 747
duke@1 748 }
duke@1 749
duke@1 750
duke@1 751 bool SafepointSynchronize::safepoint_safe(JavaThread *thread, JavaThreadState state) {
duke@1 752 switch(state) {
duke@1 753 case _thread_in_native:
duke@1 754 // native threads are safe if they have no java stack or have walkable stack
duke@1 755 return !thread->has_last_Java_frame() || thread->frame_anchor()->walkable();
duke@1 756
duke@1 757 // blocked threads should have already have walkable stack
duke@1 758 case _thread_blocked:
duke@1 759 assert(!thread->has_last_Java_frame() || thread->frame_anchor()->walkable(), "blocked and not walkable");
duke@1 760 return true;
duke@1 761
duke@1 762 default:
duke@1 763 return false;
duke@1 764 }
duke@1 765 }
duke@1 766
duke@1 767
never@11637 768 // See if the thread is running inside a lazy critical native and
never@11637 769 // update the thread critical count if so. Also set a suspend flag to
never@11637 770 // cause the native wrapper to return into the JVM to do the unlock
never@11637 771 // once the native finishes.
never@11637 772 void SafepointSynchronize::check_for_lazy_critical_native(JavaThread *thread, JavaThreadState state) {
never@11637 773 if (state == _thread_in_native &&
never@11637 774 thread->has_last_Java_frame() &&
never@11637 775 thread->frame_anchor()->walkable()) {
never@11637 776 // This thread might be in a critical native nmethod so look at
never@11637 777 // the top of the stack and increment the critical count if it
never@11637 778 // is.
never@11637 779 frame wrapper_frame = thread->last_frame();
never@11637 780 CodeBlob* stub_cb = wrapper_frame.cb();
never@11637 781 if (stub_cb != NULL &&
never@11637 782 stub_cb->is_nmethod() &&
never@11637 783 stub_cb->as_nmethod_or_null()->is_lazy_critical_native()) {
never@11637 784 // A thread could potentially be in a critical native across
never@11637 785 // more than one safepoint, so only update the critical state on
never@11637 786 // the first one. When it returns it will perform the unlock.
never@11637 787 if (!thread->do_critical_native_unlock()) {
never@11637 788 #ifdef ASSERT
never@11637 789 if (!thread->in_critical()) {
david@35492 790 GCLocker::increment_debug_jni_lock_count();
never@11637 791 }
never@11637 792 #endif
never@11637 793 thread->enter_critical();
never@11637 794 // Make sure the native wrapper calls back on return to
never@11637 795 // perform the needed critical unlock.
never@11637 796 thread->set_critical_native_unlock();
never@11637 797 }
never@11637 798 }
never@11637 799 }
never@11637 800 }
never@11637 801
never@11637 802
never@11637 803
duke@1 804 // -------------------------------------------------------------------------------------------------------
duke@1 805 // Implementation of Safepoint callback point
duke@1 806
duke@1 807 void SafepointSynchronize::block(JavaThread *thread) {
duke@1 808 assert(thread != NULL, "thread must be set");
duke@1 809 assert(thread->is_Java_thread(), "not a Java thread");
duke@1 810
duke@1 811 // Threads shouldn't block if they are in the middle of printing, but...
duke@1 812 ttyLocker::break_tty_lock_for_safepoint(os::current_thread_id());
duke@1 813
duke@1 814 // Only bail from the block() call if the thread is gone from the
duke@1 815 // thread list; starting to exit should still block.
duke@1 816 if (thread->is_terminated()) {
duke@1 817 // block current thread if we come here from native code when VM is gone
duke@1 818 thread->block_if_vm_exited();
duke@1 819
duke@1 820 // otherwise do nothing
duke@1 821 return;
duke@1 822 }
duke@1 823
duke@1 824 JavaThreadState state = thread->thread_state();
duke@1 825 thread->frame_anchor()->make_walkable(thread);
duke@1 826
duke@1 827 // Check that we have a valid thread_state at this point
duke@1 828 switch(state) {
duke@1 829 case _thread_in_vm_trans:
duke@1 830 case _thread_in_Java: // From compiled code
duke@1 831
duke@1 832 // We are highly likely to block on the Safepoint_lock. In order to avoid blocking in this case,
duke@1 833 // we pretend we are still in the VM.
duke@1 834 thread->set_thread_state(_thread_in_vm);
duke@1 835
duke@1 836 if (is_synchronizing()) {
duke@1 837 Atomic::inc (&TryingToBlock) ;
duke@1 838 }
duke@1 839
duke@1 840 // We will always be holding the Safepoint_lock when we are examine the state
duke@1 841 // of a thread. Hence, the instructions between the Safepoint_lock->lock() and
duke@1 842 // Safepoint_lock->unlock() are happening atomic with regards to the safepoint code
duke@1 843 Safepoint_lock->lock_without_safepoint_check();
duke@1 844 if (is_synchronizing()) {
duke@1 845 // Decrement the number of threads to wait for and signal vm thread
duke@1 846 assert(_waiting_to_block > 0, "sanity check");
duke@1 847 _waiting_to_block--;
duke@1 848 thread->safepoint_state()->set_has_called_back(true);
duke@1 849
never@12108 850 DEBUG_ONLY(thread->set_visited_for_critical_count(true));
never@11631 851 if (thread->in_critical()) {
never@11631 852 // Notice that this thread is in a critical section
never@11631 853 increment_jni_active_count();
never@11631 854 }
never@11631 855
duke@1 856 // Consider (_waiting_to_block < 2) to pipeline the wakeup of the VM thread
duke@1 857 if (_waiting_to_block == 0) {
duke@1 858 Safepoint_lock->notify_all();
duke@1 859 }
duke@1 860 }
duke@1 861
duke@1 862 // We transition the thread to state _thread_blocked here, but
duke@1 863 // we can't do our usual check for external suspension and then
duke@1 864 // self-suspend after the lock_without_safepoint_check() call
duke@1 865 // below because we are often called during transitions while
duke@1 866 // we hold different locks. That would leave us suspended while
duke@1 867 // holding a resource which results in deadlocks.
duke@1 868 thread->set_thread_state(_thread_blocked);
duke@1 869 Safepoint_lock->unlock();
duke@1 870
duke@1 871 // We now try to acquire the threads lock. Since this lock is hold by the VM thread during
duke@1 872 // the entire safepoint, the threads will all line up here during the safepoint.
duke@1 873 Threads_lock->lock_without_safepoint_check();
duke@1 874 // restore original state. This is important if the thread comes from compiled code, so it
duke@1 875 // will continue to execute with the _thread_in_Java state.
duke@1 876 thread->set_thread_state(state);
duke@1 877 Threads_lock->unlock();
duke@1 878 break;
duke@1 879
duke@1 880 case _thread_in_native_trans:
duke@1 881 case _thread_blocked_trans:
duke@1 882 case _thread_new_trans:
duke@1 883 if (thread->safepoint_state()->type() == ThreadSafepointState::_call_back) {
duke@1 884 thread->print_thread_state();
duke@1 885 fatal("Deadlock in safepoint code. "
duke@1 886 "Should have called back to the VM before blocking.");
duke@1 887 }
duke@1 888
duke@1 889 // We transition the thread to state _thread_blocked here, but
duke@1 890 // we can't do our usual check for external suspension and then
duke@1 891 // self-suspend after the lock_without_safepoint_check() call
duke@1 892 // below because we are often called during transitions while
duke@1 893 // we hold different locks. That would leave us suspended while
duke@1 894 // holding a resource which results in deadlocks.
duke@1 895 thread->set_thread_state(_thread_blocked);
duke@1 896
duke@1 897 // It is not safe to suspend a thread if we discover it is in _thread_in_native_trans. Hence,
duke@1 898 // the safepoint code might still be waiting for it to block. We need to change the state here,
duke@1 899 // so it can see that it is at a safepoint.
duke@1 900
duke@1 901 // Block until the safepoint operation is completed.
duke@1 902 Threads_lock->lock_without_safepoint_check();
duke@1 903
duke@1 904 // Restore state
duke@1 905 thread->set_thread_state(state);
duke@1 906
duke@1 907 Threads_lock->unlock();
duke@1 908 break;
duke@1 909
duke@1 910 default:
david@33105 911 fatal("Illegal threadstate encountered: %d", state);
duke@1 912 }
duke@1 913
duke@1 914 // Check for pending. async. exceptions or suspends - except if the
duke@1 915 // thread was blocked inside the VM. has_special_runtime_exit_condition()
duke@1 916 // is called last since it grabs a lock and we only want to do that when
duke@1 917 // we must.
duke@1 918 //
duke@1 919 // Note: we never deliver an async exception at a polling point as the
duke@1 920 // compiler may not have an exception handler for it. The polling
duke@1 921 // code will notice the async and deoptimize and the exception will
duke@1 922 // be delivered. (Polling at a return point is ok though). Sure is
duke@1 923 // a lot of bother for a deprecated feature...
duke@1 924 //
duke@1 925 // We don't deliver an async exception if the thread state is
duke@1 926 // _thread_in_native_trans so JNI functions won't be called with
duke@1 927 // a surprising pending exception. If the thread state is going back to java,
duke@1 928 // async exception is checked in check_special_condition_for_native_trans().
duke@1 929
duke@1 930 if (state != _thread_blocked_trans &&
duke@1 931 state != _thread_in_vm_trans &&
duke@1 932 thread->has_special_runtime_exit_condition()) {
duke@1 933 thread->handle_special_runtime_exit_condition(
duke@1 934 !thread->is_at_poll_safepoint() && (state != _thread_in_native_trans));
duke@1 935 }
duke@1 936 }
duke@1 937
duke@1 938 // ------------------------------------------------------------------------------------------------------
duke@1 939 // Exception handlers
duke@1 940
duke@1 941
duke@1 942 void SafepointSynchronize::handle_polling_page_exception(JavaThread *thread) {
duke@1 943 assert(thread->is_Java_thread(), "polling reference encountered by VM thread");
duke@1 944 assert(thread->thread_state() == _thread_in_Java, "should come from Java code");
rehn@48069 945 if (!ThreadLocalHandshakes) {
rehn@48069 946 assert(SafepointSynchronize::is_synchronizing(), "polling encountered outside safepoint synchronization");
rehn@48069 947 }
duke@1 948
coleenp@51627 949 if (log_is_enabled(Debug, safepoint, stats)) {
coleenp@51627 950 Atomic::inc(&_nof_threads_hit_polling_page);
duke@1 951 }
duke@1 952
duke@1 953 ThreadSafepointState* state = thread->safepoint_state();
duke@1 954
duke@1 955 state->handle_polling_page_exception();
duke@1 956 }
duke@1 957
duke@1 958
duke@1 959 void SafepointSynchronize::print_safepoint_timeout(SafepointTimeoutReason reason) {
duke@1 960 if (!timeout_error_printed) {
duke@1 961 timeout_error_printed = true;
jwilhelm@22551 962 // Print out the thread info which didn't reach the safepoint for debugging
duke@1 963 // purposes (useful when there are lots of threads in the debugger).
coleenp@51627 964 LogTarget(Warning, safepoint) lt;
coleenp@51627 965 if (lt.is_enabled()) {
coleenp@51627 966 ResourceMark rm;
coleenp@51627 967 LogStream ls(lt);
coleenp@51627 968
coleenp@51627 969 ls.cr();
coleenp@51627 970 ls.print_cr("# SafepointSynchronize::begin: Timeout detected:");
coleenp@51627 971 if (reason == _spinning_timeout) {
coleenp@51627 972 ls.print_cr("# SafepointSynchronize::begin: Timed out while spinning to reach a safepoint.");
coleenp@51627 973 } else if (reason == _blocking_timeout) {
coleenp@51627 974 ls.print_cr("# SafepointSynchronize::begin: Timed out while waiting for threads to stop.");
coleenp@51627 975 }
coleenp@51627 976
coleenp@51627 977 ls.print_cr("# SafepointSynchronize::begin: Threads which did not reach the safepoint:");
coleenp@51627 978 ThreadSafepointState *cur_state;
coleenp@51627 979 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *cur_thread = jtiwh.next(); ) {
coleenp@51627 980 cur_state = cur_thread->safepoint_state();
coleenp@51627 981
coleenp@51627 982 if (cur_thread->thread_state() != _thread_blocked &&
coleenp@51627 983 ((reason == _spinning_timeout && cur_state->is_running()) ||
coleenp@51627 984 (reason == _blocking_timeout && !cur_state->has_called_back()))) {
coleenp@51627 985 ls.print("# ");
coleenp@51627 986 cur_thread->print_on(&ls);
coleenp@51627 987 ls.cr();
coleenp@51627 988 }
coleenp@51627 989 }
coleenp@51627 990 ls.print_cr("# SafepointSynchronize::begin: (End of list)");
duke@1 991 }
duke@1 992 }
duke@1 993
duke@1 994 // To debug the long safepoint, specify both DieOnSafepointTimeout &
duke@1 995 // ShowMessageBoxOnError.
duke@1 996 if (DieOnSafepointTimeout) {
david@33105 997 fatal("Safepoint sync time longer than " INTX_FORMAT "ms detected when executing %s.",
rehn@46496 998 SafepointTimeoutDelay, VMThread::vm_safepoint_description());
duke@1 999 }
duke@1 1000 }
duke@1 1001
duke@1 1002
duke@1 1003 // -------------------------------------------------------------------------------------------------------
duke@1 1004 // Implementation of ThreadSafepointState
duke@1 1005
duke@1 1006 ThreadSafepointState::ThreadSafepointState(JavaThread *thread) {
duke@1 1007 _thread = thread;
duke@1 1008 _type = _running;
duke@1 1009 _has_called_back = false;
duke@1 1010 _at_poll_safepoint = false;
duke@1 1011 }
duke@1 1012
duke@1 1013 void ThreadSafepointState::create(JavaThread *thread) {
duke@1 1014 ThreadSafepointState *state = new ThreadSafepointState(thread);
duke@1 1015 thread->set_safepoint_state(state);
duke@1 1016 }
duke@1 1017
duke@1 1018 void ThreadSafepointState::destroy(JavaThread *thread) {
duke@1 1019 if (thread->safepoint_state()) {
duke@1 1020 delete(thread->safepoint_state());
duke@1 1021 thread->set_safepoint_state(NULL);
duke@1 1022 }
duke@1 1023 }
duke@1 1024
duke@1 1025 void ThreadSafepointState::examine_state_of_thread() {
duke@1 1026 assert(is_running(), "better be running or just have hit safepoint poll");
duke@1 1027
duke@1 1028 JavaThreadState state = _thread->thread_state();
duke@1 1029
never@6269 1030 // Save the state at the start of safepoint processing.
never@6269 1031 _orig_thread_state = state;
never@6269 1032
duke@1 1033 // Check for a thread that is suspended. Note that thread resume tries
duke@1 1034 // to grab the Threads_lock which we own here, so a thread cannot be
duke@1 1035 // resumed during safepoint synchronization.
duke@1 1036
dcubed@3826 1037 // We check to see if this thread is suspended without locking to
dcubed@3826 1038 // avoid deadlocking with a third thread that is waiting for this
dcubed@3826 1039 // thread to be suspended. The third thread can notice the safepoint
dcubed@3826 1040 // that we're trying to start at the beginning of its SR_lock->wait()
dcubed@3826 1041 // call. If that happens, then the third thread will block on the
dcubed@3826 1042 // safepoint while still holding the underlying SR_lock. We won't be
dcubed@3826 1043 // able to get the SR_lock and we'll deadlock.
dcubed@3826 1044 //
dcubed@3826 1045 // We don't need to grab the SR_lock here for two reasons:
dcubed@3826 1046 // 1) The suspend flags are both volatile and are set with an
dcubed@3826 1047 // Atomic::cmpxchg() call so we should see the suspended
dcubed@3826 1048 // state right away.
dcubed@3826 1049 // 2) We're being called from the safepoint polling loop; if
dcubed@3826 1050 // we don't see the suspended state on this iteration, then
dcubed@3826 1051 // we'll come around again.
dcubed@3826 1052 //
dcubed@3826 1053 bool is_suspended = _thread->is_ext_suspended();
duke@1 1054 if (is_suspended) {
duke@1 1055 roll_forward(_at_safepoint);
duke@1 1056 return;
duke@1 1057 }
duke@1 1058
duke@1 1059 // Some JavaThread states have an initial safepoint state of
duke@1 1060 // running, but are actually at a safepoint. We will happily
duke@1 1061 // agree and update the safepoint state here.
duke@1 1062 if (SafepointSynchronize::safepoint_safe(_thread, state)) {
never@12108 1063 SafepointSynchronize::check_for_lazy_critical_native(_thread, state);
never@11631 1064 roll_forward(_at_safepoint);
never@11631 1065 return;
duke@1 1066 }
duke@1 1067
duke@1 1068 if (state == _thread_in_vm) {
duke@1 1069 roll_forward(_call_back);
duke@1 1070 return;
duke@1 1071 }
duke@1 1072
duke@1 1073 // All other thread states will continue to run until they
duke@1 1074 // transition and self-block in state _blocked
duke@1 1075 // Safepoint polling in compiled code causes the Java threads to do the same.
duke@1 1076 // Note: new threads may require a malloc so they must be allowed to finish
duke@1 1077
duke@1 1078 assert(is_running(), "examine_state_of_thread on non-running thread");
duke@1 1079 return;
duke@1 1080 }
duke@1 1081
duke@1 1082 // Returns true is thread could not be rolled forward at present position.
duke@1 1083 void ThreadSafepointState::roll_forward(suspend_type type) {
duke@1 1084 _type = type;
duke@1 1085
duke@1 1086 switch(_type) {
duke@1 1087 case _at_safepoint:
duke@1 1088 SafepointSynchronize::signal_thread_at_safepoint();
never@12108 1089 DEBUG_ONLY(_thread->set_visited_for_critical_count(true));
never@12108 1090 if (_thread->in_critical()) {
never@12108 1091 // Notice that this thread is in a critical section
never@12108 1092 SafepointSynchronize::increment_jni_active_count();
never@12108 1093 }
duke@1 1094 break;
duke@1 1095
duke@1 1096 case _call_back:
duke@1 1097 set_has_called_back(false);
duke@1 1098 break;
duke@1 1099
duke@1 1100 case _running:
duke@1 1101 default:
duke@1 1102 ShouldNotReachHere();
duke@1 1103 }
duke@1 1104 }
duke@1 1105
duke@1 1106 void ThreadSafepointState::restart() {
duke@1 1107 switch(type()) {
duke@1 1108 case _at_safepoint:
duke@1 1109 case _call_back:
duke@1 1110 break;
duke@1 1111
duke@1 1112 case _running:
duke@1 1113 default:
bpittore@31592 1114 tty->print_cr("restart thread " INTPTR_FORMAT " with state %d",
david@33148 1115 p2i(_thread), _type);
duke@1 1116 _thread->print();
duke@1 1117 ShouldNotReachHere();
duke@1 1118 }
duke@1 1119 _type = _running;
duke@1 1120 set_has_called_back(false);
duke@1 1121 }
duke@1 1122
duke@1 1123
duke@1 1124 void ThreadSafepointState::print_on(outputStream *st) const {
goetz@33589 1125 const char *s = NULL;
duke@1 1126
duke@1 1127 switch(_type) {
duke@1 1128 case _running : s = "_running"; break;
duke@1 1129 case _at_safepoint : s = "_at_safepoint"; break;
duke@1 1130 case _call_back : s = "_call_back"; break;
duke@1 1131 default:
duke@1 1132 ShouldNotReachHere();
duke@1 1133 }
duke@1 1134
duke@1 1135 st->print_cr("Thread: " INTPTR_FORMAT
duke@1 1136 " [0x%2x] State: %s _has_called_back %d _at_poll_safepoint %d",
david@33148 1137 p2i(_thread), _thread->osthread()->thread_id(), s, _has_called_back,
duke@1 1138 _at_poll_safepoint);
duke@1 1139
duke@1 1140 _thread->print_thread_state_on(st);
duke@1 1141 }
duke@1 1142
duke@1 1143 // ---------------------------------------------------------------------------------------------------------------------
duke@1 1144
rehn@48069 1145 // Block the thread at poll or poll return for safepoint/handshake.
duke@1 1146 void ThreadSafepointState::handle_polling_page_exception() {
duke@1 1147
duke@1 1148 // Check state. block() will set thread state to thread_in_vm which will
duke@1 1149 // cause the safepoint state _type to become _call_back.
rehn@48069 1150 suspend_type t = type();
rehn@48069 1151 assert(!SafepointMechanism::uses_global_page_poll() || t == ThreadSafepointState::_running,
rehn@48069 1152 "polling page exception on thread not running state: %u", uint(t));
duke@1 1153
duke@1 1154 // Step 1: Find the nmethod from the return address
duke@1 1155 address real_return_addr = thread()->saved_exception_pc();
duke@1 1156
duke@1 1157 CodeBlob *cb = CodeCache::find_blob(real_return_addr);
rbackman@38133 1158 assert(cb != NULL && cb->is_compiled(), "return address should be in nmethod");
rbackman@38133 1159 CompiledMethod* nm = (CompiledMethod*)cb;
duke@1 1160
duke@1 1161 // Find frame of caller
duke@1 1162 frame stub_fr = thread()->last_frame();
duke@1 1163 CodeBlob* stub_cb = stub_fr.cb();
duke@1 1164 assert(stub_cb->is_safepoint_stub(), "must be a safepoint stub");
duke@1 1165 RegisterMap map(thread(), true);
duke@1 1166 frame caller_fr = stub_fr.sender(&map);
duke@1 1167
duke@1 1168 // Should only be poll_return or poll
duke@1 1169 assert( nm->is_at_poll_or_poll_return(real_return_addr), "should not be at call" );
duke@1 1170
duke@1 1171 // This is a poll immediately before a return. The exception handling code
duke@1 1172 // has already had the effect of causing the return to occur, so the execution
duke@1 1173 // will continue immediately after the call. In addition, the oopmap at the
duke@1 1174 // return point does not mark the return value as an oop (if it is), so
duke@1 1175 // it needs a handle here to be updated.
duke@1 1176 if( nm->is_at_poll_return(real_return_addr) ) {
duke@1 1177 // See if return type is an oop.
duke@1 1178 bool return_oop = nm->method()->is_returning_oop();
duke@1 1179 Handle return_value;
duke@1 1180 if (return_oop) {
duke@1 1181 // The oop result has been saved on the stack together with all
duke@1 1182 // the other registers. In order to preserve it over GCs we need
duke@1 1183 // to keep it in a handle.
duke@1 1184 oop result = caller_fr.saved_oop_result(&map);
coleenp@46968 1185 assert(oopDesc::is_oop_or_null(result), "must be oop");
duke@1 1186 return_value = Handle(thread(), result);
duke@1 1187 assert(Universe::heap()->is_in_or_null(result), "must be heap pointer");
duke@1 1188 }
duke@1 1189
duke@1 1190 // Block the thread
rehn@48069 1191 SafepointMechanism::block_if_requested(thread());
duke@1 1192
duke@1 1193 // restore oop result, if any
duke@1 1194 if (return_oop) {
duke@1 1195 caller_fr.set_saved_oop_result(&map, return_value());
duke@1 1196 }
duke@1 1197 }
duke@1 1198
duke@1 1199 // This is a safepoint poll. Verify the return address and block.
duke@1 1200 else {
duke@1 1201 set_at_poll_safepoint(true);
duke@1 1202
duke@1 1203 // verify the blob built the "return address" correctly
duke@1 1204 assert(real_return_addr == caller_fr.pc(), "must match");
duke@1 1205
duke@1 1206 // Block the thread
rehn@48069 1207 SafepointMechanism::block_if_requested(thread());
duke@1 1208 set_at_poll_safepoint(false);
duke@1 1209
duke@1 1210 // If we have a pending async exception deoptimize the frame
duke@1 1211 // as otherwise we may never deliver it.
duke@1 1212 if (thread()->has_async_condition()) {
duke@1 1213 ThreadInVMfromJavaNoAsyncException __tiv(thread());
never@7106 1214 Deoptimization::deoptimize_frame(thread(), caller_fr.id());
duke@1 1215 }
duke@1 1216
duke@1 1217 // If an exception has been installed we must check for a pending deoptimization
duke@1 1218 // Deoptimize frame if exception has been thrown.
duke@1 1219
duke@1 1220 if (thread()->has_pending_exception() ) {
duke@1 1221 RegisterMap map(thread(), true);
duke@1 1222 frame caller_fr = stub_fr.sender(&map);
duke@1 1223 if (caller_fr.is_deoptimized_frame()) {
duke@1 1224 // The exception patch will destroy registers that are still
duke@1 1225 // live and will be needed during deoptimization. Defer the
jwilhelm@22551 1226 // Async exception should have deferred the exception until the
duke@1 1227 // next safepoint which will be detected when we get into
duke@1 1228 // the interpreter so if we have an exception now things
duke@1 1229 // are messed up.
duke@1 1230
duke@1 1231 fatal("Exception installed and deoptimization is pending");
duke@1 1232 }
duke@1 1233 }
duke@1 1234 }
duke@1 1235 }
duke@1 1236
duke@1 1237
duke@1 1238 //
duke@1 1239 // Statistics & Instrumentations
duke@1 1240 //
coleenp@51627 1241 struct SafepointStats {
coleenp@51627 1242 float _time_stamp; // record when the current safepoint occurs in seconds
coleenp@51627 1243 int _vmop_type; // tyep of VM operation triggers the safepoint
coleenp@51627 1244 int _nof_total_threads; // total number of Java threads
coleenp@51627 1245 int _nof_initial_running_threads; // total number of initially seen running threads
coleenp@51627 1246 int _nof_threads_wait_to_block; // total number of threads waiting for to block
coleenp@51627 1247 bool _page_armed; // true if polling page is armed, false otherwise
coleenp@51627 1248 int _nof_threads_hit_page_trap; // total number of threads hitting the page trap
coleenp@51627 1249 jlong _time_to_spin; // total time in millis spent in spinning
coleenp@51627 1250 jlong _time_to_wait_to_block; // total time in millis spent in waiting for to block
coleenp@51627 1251 jlong _time_to_do_cleanups; // total time in millis spent in performing cleanups
coleenp@51627 1252 jlong _time_to_sync; // total time in millis spent in getting to _synchronized
coleenp@51627 1253 jlong _time_to_exec_vmop; // total time in millis spent in vm operation itself
coleenp@51627 1254 };
coleenp@51627 1255
coleenp@51627 1256 static const int _statistics_header_count = 30;
coleenp@51627 1257 static int _cur_stat_index = 0;
coleenp@51627 1258 static SafepointStats safepoint_stats = {0}; // zero initialize
coleenp@51627 1259 static SafepointStats* spstat = &safepoint_stats;
coleenp@51627 1260
coleenp@51627 1261 static julong _safepoint_reasons[VM_Operation::VMOp_Terminating];
coleenp@51627 1262 static jlong _max_sync_time = 0;
coleenp@51627 1263 static jlong _max_vmop_time = 0;
duke@1 1264
xlu@5042 1265 static jlong cleanup_end_time = 0;
duke@1 1266
duke@1 1267 void SafepointSynchronize::begin_statistics(int nof_threads, int nof_running) {
duke@1 1268
xlu@5042 1269 spstat->_time_stamp = _ts_of_current_safepoint;
xlu@5042 1270
duke@1 1271 VM_Operation *op = VMThread::vm_operation();
coleenp@51627 1272 spstat->_vmop_type = op != NULL ? op->type() : VM_Operation::VMOp_None;
coleenp@51627 1273 _safepoint_reasons[spstat->_vmop_type]++;
duke@1 1274
duke@1 1275 spstat->_nof_total_threads = nof_threads;
duke@1 1276 spstat->_nof_initial_running_threads = nof_running;
duke@1 1277
duke@1 1278 // Records the start time of spinning. The real time spent on spinning
duke@1 1279 // will be adjusted when spin is done. Same trick is applied for time
duke@1 1280 // spent on waiting for threads to block.
duke@1 1281 if (nof_running != 0) {
duke@1 1282 spstat->_time_to_spin = os::javaTimeNanos();
duke@1 1283 } else {
duke@1 1284 spstat->_time_to_spin = 0;
duke@1 1285 }
duke@1 1286 }
duke@1 1287
duke@1 1288 void SafepointSynchronize::update_statistics_on_spin_end() {
duke@1 1289 jlong cur_time = os::javaTimeNanos();
duke@1 1290
duke@1 1291 spstat->_nof_threads_wait_to_block = _waiting_to_block;
duke@1 1292 if (spstat->_nof_initial_running_threads != 0) {
duke@1 1293 spstat->_time_to_spin = cur_time - spstat->_time_to_spin;
duke@1 1294 }
duke@1 1295
duke@1 1296 // Records the start time of waiting for to block. Updated when block is done.
duke@1 1297 if (_waiting_to_block != 0) {
duke@1 1298 spstat->_time_to_wait_to_block = cur_time;
duke@1 1299 } else {
duke@1 1300 spstat->_time_to_wait_to_block = 0;
duke@1 1301 }
duke@1 1302 }
duke@1 1303
duke@1 1304 void SafepointSynchronize::update_statistics_on_sync_end(jlong end_time) {
duke@1 1305
duke@1 1306 if (spstat->_nof_threads_wait_to_block != 0) {
duke@1 1307 spstat->_time_to_wait_to_block = end_time -
duke@1 1308 spstat->_time_to_wait_to_block;
duke@1 1309 }
duke@1 1310
duke@1 1311 // Records the end time of sync which will be used to calculate the total
duke@1 1312 // vm operation time. Again, the real time spending in syncing will be deducted
duke@1 1313 // from the start of the sync time later when end_statistics is called.
xlu@5042 1314 spstat->_time_to_sync = end_time - _safepoint_begin_time;
duke@1 1315 if (spstat->_time_to_sync > _max_sync_time) {
duke@1 1316 _max_sync_time = spstat->_time_to_sync;
duke@1 1317 }
xlu@5042 1318
xlu@5042 1319 spstat->_time_to_do_cleanups = end_time;
xlu@5042 1320 }
xlu@5042 1321
xlu@5042 1322 void SafepointSynchronize::update_statistics_on_cleanup_end(jlong end_time) {
xlu@5042 1323
xlu@5042 1324 // Record how long spent in cleanup tasks.
xlu@5042 1325 spstat->_time_to_do_cleanups = end_time - spstat->_time_to_do_cleanups;
xlu@5042 1326 cleanup_end_time = end_time;
duke@1 1327 }
duke@1 1328
duke@1 1329 void SafepointSynchronize::end_statistics(jlong vmop_end_time) {
duke@1 1330
duke@1 1331 // Update the vm operation time.
xlu@5042 1332 spstat->_time_to_exec_vmop = vmop_end_time - cleanup_end_time;
xlu@5042 1333 if (spstat->_time_to_exec_vmop > _max_vmop_time) {
xlu@5042 1334 _max_vmop_time = spstat->_time_to_exec_vmop;
xlu@5042 1335 }
coleenp@51627 1336
coleenp@51627 1337 spstat->_nof_threads_hit_page_trap = _nof_threads_hit_polling_page;
coleenp@51627 1338
coleenp@51627 1339 print_statistics();
duke@1 1340 }
duke@1 1341
coleenp@51627 1342 // Helper method to print the header.
coleenp@51627 1343 static void print_header(outputStream* st) {
coleenp@51627 1344 // The number of spaces is significant here, and should match the format
coleenp@51627 1345 // specifiers in print_statistics().
duke@1 1346
coleenp@51627 1347 st->print(" vmop "
coleenp@51627 1348 "[ threads: total initially_running wait_to_block ]"
coleenp@51627 1349 "[ time: spin block sync cleanup vmop ] ");
coleenp@51627 1350
coleenp@51627 1351 st->print_cr("page_trap_count");
duke@1 1352 }
duke@1 1353
coleenp@51627 1354 // This prints a nice table. To get the statistics to not shift due to the logging uptime
coleenp@51627 1355 // decorator, use the option as: -Xlog:safepoint+stats=debug:[outputfile]:none
coleenp@51627 1356 void SafepointSynchronize::print_statistics() {
coleenp@51627 1357 LogTarget(Debug, safepoint, stats) lt;
coleenp@51627 1358 assert (lt.is_enabled(), "should only be called when printing statistics is enabled");
coleenp@51627 1359 LogStream ls(lt);
coleenp@51627 1360
coleenp@51627 1361 // Print header every 30 entries
coleenp@51627 1362 if ((_cur_stat_index % _statistics_header_count) == 0) {
coleenp@51627 1363 print_header(&ls);
coleenp@51627 1364 _cur_stat_index = 1; // wrap
coleenp@51627 1365 } else {
coleenp@51627 1366 _cur_stat_index++;
coleenp@51627 1367 }
coleenp@51627 1368
coleenp@51627 1369 ls.print("%8.3f: ", spstat->_time_stamp);
coleenp@51627 1370 ls.print("%-28s [ "
coleenp@51627 1371 INT32_FORMAT_W(8) " " INT32_FORMAT_W(17) " " INT32_FORMAT_W(13) " "
coleenp@51627 1372 "]",
coleenp@51627 1373 VM_Operation::name(spstat->_vmop_type),
coleenp@51627 1374 spstat->_nof_total_threads,
coleenp@51627 1375 spstat->_nof_initial_running_threads,
coleenp@51627 1376 spstat->_nof_threads_wait_to_block);
coleenp@51627 1377 // "/ MICROUNITS " is to convert the unit from nanos to millis.
coleenp@51627 1378 ls.print("[ "
coleenp@51627 1379 INT64_FORMAT_W(7) " " INT64_FORMAT_W(7) " "
coleenp@51627 1380 INT64_FORMAT_W(7) " " INT64_FORMAT_W(7) " "
coleenp@51627 1381 INT64_FORMAT_W(7) " ] ",
coleenp@51627 1382 (int64_t)(spstat->_time_to_spin / MICROUNITS),
coleenp@51627 1383 (int64_t)(spstat->_time_to_wait_to_block / MICROUNITS),
coleenp@51627 1384 (int64_t)(spstat->_time_to_sync / MICROUNITS),
coleenp@51627 1385 (int64_t)(spstat->_time_to_do_cleanups / MICROUNITS),
coleenp@51627 1386 (int64_t)(spstat->_time_to_exec_vmop / MICROUNITS));
coleenp@51627 1387
coleenp@51627 1388 ls.print_cr(INT32_FORMAT_W(15) " ", spstat->_nof_threads_hit_page_trap);
coleenp@51627 1389 }
coleenp@51627 1390
coleenp@51627 1391 // This method will be called when VM exits. This tries to summarize the sampling.
coleenp@51627 1392 // Current thread may already be deleted, so don't use ResourceMark.
duke@1 1393 void SafepointSynchronize::print_stat_on_exit() {
duke@1 1394
duke@1 1395 for (int index = 0; index < VM_Operation::VMOp_Terminating; index++) {
duke@1 1396 if (_safepoint_reasons[index] != 0) {
coleenp@51627 1397 log_debug(safepoint, stats)("%-28s" UINT64_FORMAT_W(10), VM_Operation::name(index),
coleenp@51627 1398 _safepoint_reasons[index]);
duke@1 1399 }
duke@1 1400 }
duke@1 1401
coleenp@51627 1402 log_debug(safepoint, stats)("VM operations coalesced during safepoint " INT64_FORMAT,
coleenp@51627 1403 _coalesced_vmop_count);
coleenp@51627 1404 log_debug(safepoint, stats)("Maximum sync time " INT64_FORMAT" ms",
coleenp@51627 1405 (int64_t)(_max_sync_time / MICROUNITS));
coleenp@51627 1406 log_debug(safepoint, stats)("Maximum vm operation time (except for Exit VM operation) "
coleenp@51627 1407 INT64_FORMAT " ms",
coleenp@51627 1408 (int64_t)(_max_vmop_time / MICROUNITS));
duke@1 1409 }