1 /* 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "classfile/vmSymbols.hpp" 27 #include "memory/resourceArea.hpp" 28 #include "oops/markOop.hpp" 29 #include "oops/oop.inline.hpp" 30 #include "runtime/atomic.inline.hpp" 31 #include "runtime/biasedLocking.hpp" 32 #include "runtime/handles.inline.hpp" 33 #include "runtime/interfaceSupport.hpp" 34 #include "runtime/mutexLocker.hpp" 35 #include "runtime/objectMonitor.hpp" 36 #include "runtime/objectMonitor.inline.hpp" 37 #include "runtime/osThread.hpp" 38 #include "runtime/stubRoutines.hpp" 39 #include "runtime/synchronizer.hpp" 40 #include "runtime/thread.inline.hpp" 41 #include "utilities/dtrace.hpp" 42 #include "utilities/events.hpp" 43 #include "utilities/preserveException.hpp" 44 #ifdef TARGET_OS_FAMILY_linux 45 # include "os_linux.inline.hpp" 46 #endif 47 #ifdef TARGET_OS_FAMILY_solaris 48 # include "os_solaris.inline.hpp" 49 #endif 50 #ifdef TARGET_OS_FAMILY_windows 51 # include "os_windows.inline.hpp" 52 #endif 53 #ifdef TARGET_OS_FAMILY_bsd 54 # include "os_bsd.inline.hpp" 55 #endif 56 57 #if defined(__GNUC__) && !defined(PPC64) 58 // Need to inhibit inlining for older versions of GCC to avoid build-time failures 59 #define ATTR __attribute__((noinline)) 60 #else 61 #define ATTR 62 #endif 63 64 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 65 66 // The "core" versions of monitor enter and exit reside in this file. 67 // The interpreter and compilers contain specialized transliterated 68 // variants of the enter-exit fast-path operations. See i486.ad fast_lock(), 69 // for instance. If you make changes here, make sure to modify the 70 // interpreter, and both C1 and C2 fast-path inline locking code emission. 71 // 72 // 73 // ----------------------------------------------------------------------------- 74 75 #ifdef DTRACE_ENABLED 76 77 // Only bother with this argument setup if dtrace is available 78 // TODO-FIXME: probes should not fire when caller is _blocked. assert() accordingly. 79 80 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \ 81 char* bytes = NULL; \ 82 int len = 0; \ 83 jlong jtid = SharedRuntime::get_java_tid(thread); \ 84 Symbol* klassname = ((oop)(obj))->klass()->name(); \ 85 if (klassname != NULL) { \ 86 bytes = (char*)klassname->bytes(); \ 87 len = klassname->utf8_length(); \ 88 } 89 90 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \ 91 { \ 92 if (DTraceMonitorProbes) { \ 93 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 94 HOTSPOT_MONITOR_WAIT(jtid, \ 95 (uintptr_t)(monitor), bytes, len, (millis)); \ 96 } \ 97 } 98 99 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED 100 101 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \ 102 { \ 103 if (DTraceMonitorProbes) { \ 104 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \ 105 HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */ \ 106 (uintptr_t)(monitor), bytes, len); \ 107 } \ 108 } 109 110 #else // ndef DTRACE_ENABLED 111 112 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;} 113 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;} 114 115 #endif // ndef DTRACE_ENABLED 116 117 // This exists only as a workaround of dtrace bug 6254741 118 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) { 119 DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr); 120 return 0; 121 } 122 123 #define NINFLATIONLOCKS 256 124 static volatile intptr_t InflationLocks [NINFLATIONLOCKS] ; 125 126 ObjectMonitor * ObjectSynchronizer::gBlockList = NULL ; 127 ObjectMonitor * volatile ObjectSynchronizer::gFreeList = NULL ; 128 ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList = NULL ; 129 int ObjectSynchronizer::gOmInUseCount = 0; 130 static volatile intptr_t ListLock = 0 ; // protects global monitor free-list cache 131 static volatile int MonitorFreeCount = 0 ; // # on gFreeList 132 static volatile int MonitorPopulation = 0 ; // # Extant -- in circulation 133 #define CHAINMARKER (cast_to_oop<intptr_t>(-1)) 134 135 // ----------------------------------------------------------------------------- 136 // Fast Monitor Enter/Exit 137 // This the fast monitor enter. The interpreter and compiler use 138 // some assembly copies of this code. Make sure update those code 139 // if the following function is changed. The implementation is 140 // extremely sensitive to race condition. Be careful. 141 142 void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock, bool attempt_rebias, TRAPS) { 143 if (UseBiasedLocking) { 144 if (!SafepointSynchronize::is_at_safepoint()) { 145 BiasedLocking::Condition cond = BiasedLocking::revoke_and_rebias(obj, attempt_rebias, THREAD); 146 if (cond == BiasedLocking::BIAS_REVOKED_AND_REBIASED) { 147 return; 148 } 149 } else { 150 assert(!attempt_rebias, "can not rebias toward VM thread"); 151 BiasedLocking::revoke_at_safepoint(obj); 152 } 153 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 154 } 155 156 slow_enter (obj, lock, THREAD) ; 157 } 158 159 void ObjectSynchronizer::fast_exit(oop object, BasicLock* lock, TRAPS) { 160 assert(!object->mark()->has_bias_pattern(), "should not see bias pattern here"); 161 // if displaced header is null, the previous enter is recursive enter, no-op 162 markOop dhw = lock->displaced_header(); 163 markOop mark ; 164 if (dhw == NULL) { 165 // Recursive stack-lock. 166 // Diagnostics -- Could be: stack-locked, inflating, inflated. 167 mark = object->mark() ; 168 assert (!mark->is_neutral(), "invariant") ; 169 if (mark->has_locker() && mark != markOopDesc::INFLATING()) { 170 assert(THREAD->is_lock_owned((address)mark->locker()), "invariant") ; 171 } 172 if (mark->has_monitor()) { 173 ObjectMonitor * m = mark->monitor() ; 174 assert(((oop)(m->object()))->mark() == mark, "invariant") ; 175 assert(m->is_entered(THREAD), "invariant") ; 176 } 177 return ; 178 } 179 180 mark = object->mark() ; 181 182 // If the object is stack-locked by the current thread, try to 183 // swing the displaced header from the box back to the mark. 184 if (mark == (markOop) lock) { 185 assert (dhw->is_neutral(), "invariant") ; 186 if ((markOop) Atomic::cmpxchg_ptr (dhw, object->mark_addr(), mark) == mark) { 187 TEVENT (fast_exit: release stacklock) ; 188 return; 189 } 190 } 191 192 ObjectSynchronizer::inflate(THREAD, object)->exit (true, THREAD) ; 193 } 194 195 // ----------------------------------------------------------------------------- 196 // Interpreter/Compiler Slow Case 197 // This routine is used to handle interpreter/compiler slow case 198 // We don't need to use fast path here, because it must have been 199 // failed in the interpreter/compiler code. 200 void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) { 201 markOop mark = obj->mark(); 202 assert(!mark->has_bias_pattern(), "should not see bias pattern here"); 203 204 if (mark->is_neutral()) { 205 // Anticipate successful CAS -- the ST of the displaced mark must 206 // be visible <= the ST performed by the CAS. 207 lock->set_displaced_header(mark); 208 if (mark == (markOop) Atomic::cmpxchg_ptr(lock, obj()->mark_addr(), mark)) { 209 TEVENT (slow_enter: release stacklock) ; 210 return ; 211 } 212 // Fall through to inflate() ... 213 } else 214 if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) { 215 assert(lock != mark->locker(), "must not re-lock the same lock"); 216 assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock"); 217 lock->set_displaced_header(NULL); 218 return; 219 } 220 221 #if 0 222 // The following optimization isn't particularly useful. 223 if (mark->has_monitor() && mark->monitor()->is_entered(THREAD)) { 224 lock->set_displaced_header (NULL) ; 225 return ; 226 } 227 #endif 228 229 // The object header will never be displaced to this lock, 230 // so it does not matter what the value is, except that it 231 // must be non-zero to avoid looking like a re-entrant lock, 232 // and must not look locked either. 233 lock->set_displaced_header(markOopDesc::unused_mark()); 234 ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD); 235 } 236 237 // This routine is used to handle interpreter/compiler slow case 238 // We don't need to use fast path here, because it must have 239 // failed in the interpreter/compiler code. Simply use the heavy 240 // weight monitor should be ok, unless someone find otherwise. 241 void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) { 242 fast_exit (object, lock, THREAD) ; 243 } 244 245 // ----------------------------------------------------------------------------- 246 // Class Loader support to workaround deadlocks on the class loader lock objects 247 // Also used by GC 248 // complete_exit()/reenter() are used to wait on a nested lock 249 // i.e. to give up an outer lock completely and then re-enter 250 // Used when holding nested locks - lock acquisition order: lock1 then lock2 251 // 1) complete_exit lock1 - saving recursion count 252 // 2) wait on lock2 253 // 3) when notified on lock2, unlock lock2 254 // 4) reenter lock1 with original recursion count 255 // 5) lock lock2 256 // NOTE: must use heavy weight monitor to handle complete_exit/reenter() 257 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) { 258 TEVENT (complete_exit) ; 259 if (UseBiasedLocking) { 260 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 261 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 262 } 263 264 ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj()); 265 266 return monitor->complete_exit(THREAD); 267 } 268 269 // NOTE: must use heavy weight monitor to handle complete_exit/reenter() 270 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) { 271 TEVENT (reenter) ; 272 if (UseBiasedLocking) { 273 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 274 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 275 } 276 277 ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj()); 278 279 monitor->reenter(recursion, THREAD); 280 } 281 // ----------------------------------------------------------------------------- 282 // JNI locks on java objects 283 // NOTE: must use heavy weight monitor to handle jni monitor enter 284 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) { // possible entry from jni enter 285 // the current locking is from JNI instead of Java code 286 TEVENT (jni_enter) ; 287 if (UseBiasedLocking) { 288 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 289 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 290 } 291 THREAD->set_current_pending_monitor_is_from_java(false); 292 ObjectSynchronizer::inflate(THREAD, obj())->enter(THREAD); 293 THREAD->set_current_pending_monitor_is_from_java(true); 294 } 295 296 // NOTE: must use heavy weight monitor to handle jni monitor enter 297 bool ObjectSynchronizer::jni_try_enter(Handle obj, Thread* THREAD) { 298 if (UseBiasedLocking) { 299 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 300 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 301 } 302 303 ObjectMonitor* monitor = ObjectSynchronizer::inflate_helper(obj()); 304 return monitor->try_enter(THREAD); 305 } 306 307 308 // NOTE: must use heavy weight monitor to handle jni monitor exit 309 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) { 310 TEVENT (jni_exit) ; 311 if (UseBiasedLocking) { 312 Handle h_obj(THREAD, obj); 313 BiasedLocking::revoke_and_rebias(h_obj, false, THREAD); 314 obj = h_obj(); 315 } 316 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 317 318 ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj); 319 // If this thread has locked the object, exit the monitor. Note: can't use 320 // monitor->check(CHECK); must exit even if an exception is pending. 321 if (monitor->check(THREAD)) { 322 monitor->exit(true, THREAD); 323 } 324 } 325 326 // ----------------------------------------------------------------------------- 327 // Internal VM locks on java objects 328 // standard constructor, allows locking failures 329 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) { 330 _dolock = doLock; 331 _thread = thread; 332 debug_only(if (StrictSafepointChecks) _thread->check_for_valid_safepoint_state(false);) 333 _obj = obj; 334 335 if (_dolock) { 336 TEVENT (ObjectLocker) ; 337 338 ObjectSynchronizer::fast_enter(_obj, &_lock, false, _thread); 339 } 340 } 341 342 ObjectLocker::~ObjectLocker() { 343 if (_dolock) { 344 ObjectSynchronizer::fast_exit(_obj(), &_lock, _thread); 345 } 346 } 347 348 349 // ----------------------------------------------------------------------------- 350 // Wait/Notify/NotifyAll 351 // NOTE: must use heavy weight monitor to handle wait() 352 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) { 353 if (UseBiasedLocking) { 354 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 355 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 356 } 357 if (millis < 0) { 358 TEVENT (wait - throw IAX) ; 359 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); 360 } 361 ObjectMonitor* monitor = ObjectSynchronizer::inflate(THREAD, obj()); 362 DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis); 363 monitor->wait(millis, true, THREAD); 364 365 // This dummy call is in place to get around dtrace bug 6254741. Once 366 // that's fixed we can uncomment the following line, remove the call 367 // and change this function back into a "void" func. 368 // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD); 369 return dtrace_waited_probe(monitor, obj, THREAD); 370 } 371 372 void ObjectSynchronizer::waitUninterruptibly (Handle obj, jlong millis, TRAPS) { 373 if (UseBiasedLocking) { 374 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 375 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 376 } 377 if (millis < 0) { 378 TEVENT (wait - throw IAX) ; 379 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); 380 } 381 ObjectSynchronizer::inflate(THREAD, obj()) -> wait(millis, false, THREAD) ; 382 } 383 384 void ObjectSynchronizer::notify(Handle obj, TRAPS) { 385 if (UseBiasedLocking) { 386 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 387 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 388 } 389 390 markOop mark = obj->mark(); 391 if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) { 392 return; 393 } 394 ObjectSynchronizer::inflate(THREAD, obj())->notify(THREAD); 395 } 396 397 // NOTE: see comment of notify() 398 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) { 399 if (UseBiasedLocking) { 400 BiasedLocking::revoke_and_rebias(obj, false, THREAD); 401 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 402 } 403 404 markOop mark = obj->mark(); 405 if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) { 406 return; 407 } 408 ObjectSynchronizer::inflate(THREAD, obj())->notifyAll(THREAD); 409 } 410 411 // ----------------------------------------------------------------------------- 412 // Hash Code handling 413 // 414 // Performance concern: 415 // OrderAccess::storestore() calls release() which STs 0 into the global volatile 416 // OrderAccess::Dummy variable. This store is unnecessary for correctness. 417 // Many threads STing into a common location causes considerable cache migration 418 // or "sloshing" on large SMP system. As such, I avoid using OrderAccess::storestore() 419 // until it's repaired. In some cases OrderAccess::fence() -- which incurs local 420 // latency on the executing processor -- is a better choice as it scales on SMP 421 // systems. See http://blogs.sun.com/dave/entry/biased_locking_in_hotspot for a 422 // discussion of coherency costs. Note that all our current reference platforms 423 // provide strong ST-ST order, so the issue is moot on IA32, x64, and SPARC. 424 // 425 // As a general policy we use "volatile" to control compiler-based reordering 426 // and explicit fences (barriers) to control for architectural reordering performed 427 // by the CPU(s) or platform. 428 429 struct SharedGlobals { 430 // These are highly shared mostly-read variables. 431 // To avoid false-sharing they need to be the sole occupants of a $ line. 432 double padPrefix [8]; 433 volatile int stwRandom ; 434 volatile int stwCycle ; 435 436 // Hot RW variables -- Sequester to avoid false-sharing 437 double padSuffix [16]; 438 volatile int hcSequence ; 439 double padFinal [8] ; 440 } ; 441 442 static SharedGlobals GVars ; 443 static int MonitorScavengeThreshold = 1000000 ; 444 static volatile int ForceMonitorScavenge = 0 ; // Scavenge required and pending 445 446 static markOop ReadStableMark (oop obj) { 447 markOop mark = obj->mark() ; 448 if (!mark->is_being_inflated()) { 449 return mark ; // normal fast-path return 450 } 451 452 int its = 0 ; 453 for (;;) { 454 markOop mark = obj->mark() ; 455 if (!mark->is_being_inflated()) { 456 return mark ; // normal fast-path return 457 } 458 459 // The object is being inflated by some other thread. 460 // The caller of ReadStableMark() must wait for inflation to complete. 461 // Avoid live-lock 462 // TODO: consider calling SafepointSynchronize::do_call_back() while 463 // spinning to see if there's a safepoint pending. If so, immediately 464 // yielding or blocking would be appropriate. Avoid spinning while 465 // there is a safepoint pending. 466 // TODO: add inflation contention performance counters. 467 // TODO: restrict the aggregate number of spinners. 468 469 ++its ; 470 if (its > 10000 || !os::is_MP()) { 471 if (its & 1) { 472 os::NakedYield() ; 473 TEVENT (Inflate: INFLATING - yield) ; 474 } else { 475 // Note that the following code attenuates the livelock problem but is not 476 // a complete remedy. A more complete solution would require that the inflating 477 // thread hold the associated inflation lock. The following code simply restricts 478 // the number of spinners to at most one. We'll have N-2 threads blocked 479 // on the inflationlock, 1 thread holding the inflation lock and using 480 // a yield/park strategy, and 1 thread in the midst of inflation. 481 // A more refined approach would be to change the encoding of INFLATING 482 // to allow encapsulation of a native thread pointer. Threads waiting for 483 // inflation to complete would use CAS to push themselves onto a singly linked 484 // list rooted at the markword. Once enqueued, they'd loop, checking a per-thread flag 485 // and calling park(). When inflation was complete the thread that accomplished inflation 486 // would detach the list and set the markword to inflated with a single CAS and 487 // then for each thread on the list, set the flag and unpark() the thread. 488 // This is conceptually similar to muxAcquire-muxRelease, except that muxRelease 489 // wakes at most one thread whereas we need to wake the entire list. 490 int ix = (cast_from_oop<intptr_t>(obj) >> 5) & (NINFLATIONLOCKS-1) ; 491 int YieldThenBlock = 0 ; 492 assert (ix >= 0 && ix < NINFLATIONLOCKS, "invariant") ; 493 assert ((NINFLATIONLOCKS & (NINFLATIONLOCKS-1)) == 0, "invariant") ; 494 Thread::muxAcquire (InflationLocks + ix, "InflationLock") ; 495 while (obj->mark() == markOopDesc::INFLATING()) { 496 // Beware: NakedYield() is advisory and has almost no effect on some platforms 497 // so we periodically call Self->_ParkEvent->park(1). 498 // We use a mixed spin/yield/block mechanism. 499 if ((YieldThenBlock++) >= 16) { 500 Thread::current()->_ParkEvent->park(1) ; 501 } else { 502 os::NakedYield() ; 503 } 504 } 505 Thread::muxRelease (InflationLocks + ix ) ; 506 TEVENT (Inflate: INFLATING - yield/park) ; 507 } 508 } else { 509 SpinPause() ; // SMP-polite spinning 510 } 511 } 512 } 513 514 // hashCode() generation : 515 // 516 // Possibilities: 517 // * MD5Digest of {obj,stwRandom} 518 // * CRC32 of {obj,stwRandom} or any linear-feedback shift register function. 519 // * A DES- or AES-style SBox[] mechanism 520 // * One of the Phi-based schemes, such as: 521 // 2654435761 = 2^32 * Phi (golden ratio) 522 // HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stwRandom ; 523 // * A variation of Marsaglia's shift-xor RNG scheme. 524 // * (obj ^ stwRandom) is appealing, but can result 525 // in undesirable regularity in the hashCode values of adjacent objects 526 // (objects allocated back-to-back, in particular). This could potentially 527 // result in hashtable collisions and reduced hashtable efficiency. 528 // There are simple ways to "diffuse" the middle address bits over the 529 // generated hashCode values: 530 // 531 532 static inline intptr_t get_next_hash(Thread * Self, oop obj) { 533 intptr_t value = 0 ; 534 if (hashCode == 0) { 535 // This form uses an unguarded global Park-Miller RNG, 536 // so it's possible for two threads to race and generate the same RNG. 537 // On MP system we'll have lots of RW access to a global, so the 538 // mechanism induces lots of coherency traffic. 539 value = os::random() ; 540 } else 541 if (hashCode == 1) { 542 // This variation has the property of being stable (idempotent) 543 // between STW operations. This can be useful in some of the 1-0 544 // synchronization schemes. 545 intptr_t addrBits = cast_from_oop<intptr_t>(obj) >> 3 ; 546 value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ; 547 } else 548 if (hashCode == 2) { 549 value = 1 ; // for sensitivity testing 550 } else 551 if (hashCode == 3) { 552 value = ++GVars.hcSequence ; 553 } else 554 if (hashCode == 4) { 555 value = cast_from_oop<intptr_t>(obj) ; 556 } else { 557 // Marsaglia's xor-shift scheme with thread-specific state 558 // This is probably the best overall implementation -- we'll 559 // likely make this the default in future releases. 560 unsigned t = Self->_hashStateX ; 561 t ^= (t << 11) ; 562 Self->_hashStateX = Self->_hashStateY ; 563 Self->_hashStateY = Self->_hashStateZ ; 564 Self->_hashStateZ = Self->_hashStateW ; 565 unsigned v = Self->_hashStateW ; 566 v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ; 567 Self->_hashStateW = v ; 568 value = v ; 569 } 570 571 value &= markOopDesc::hash_mask; 572 if (value == 0) value = 0xBAD ; 573 assert (value != markOopDesc::no_hash, "invariant") ; 574 TEVENT (hashCode: GENERATE) ; 575 return value; 576 } 577 // 578 intptr_t ObjectSynchronizer::FastHashCode (Thread * Self, oop obj) { 579 if (UseBiasedLocking) { 580 // NOTE: many places throughout the JVM do not expect a safepoint 581 // to be taken here, in particular most operations on perm gen 582 // objects. However, we only ever bias Java instances and all of 583 // the call sites of identity_hash that might revoke biases have 584 // been checked to make sure they can handle a safepoint. The 585 // added check of the bias pattern is to avoid useless calls to 586 // thread-local storage. 587 if (obj->mark()->has_bias_pattern()) { 588 // Box and unbox the raw reference just in case we cause a STW safepoint. 589 Handle hobj (Self, obj) ; 590 // Relaxing assertion for bug 6320749. 591 assert (Universe::verify_in_progress() || 592 !SafepointSynchronize::is_at_safepoint(), 593 "biases should not be seen by VM thread here"); 594 BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current()); 595 obj = hobj() ; 596 assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 597 } 598 } 599 600 // hashCode() is a heap mutator ... 601 // Relaxing assertion for bug 6320749. 602 assert (Universe::verify_in_progress() || 603 !SafepointSynchronize::is_at_safepoint(), "invariant") ; 604 assert (Universe::verify_in_progress() || 605 Self->is_Java_thread() , "invariant") ; 606 assert (Universe::verify_in_progress() || 607 ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant") ; 608 609 ObjectMonitor* monitor = NULL; 610 markOop temp, test; 611 intptr_t hash; 612 markOop mark = ReadStableMark (obj); 613 614 // object should remain ineligible for biased locking 615 assert (!mark->has_bias_pattern(), "invariant") ; 616 617 if (mark->is_neutral()) { 618 hash = mark->hash(); // this is a normal header 619 if (hash) { // if it has hash, just return it 620 return hash; 621 } 622 hash = get_next_hash(Self, obj); // allocate a new hash code 623 temp = mark->copy_set_hash(hash); // merge the hash code into header 624 // use (machine word version) atomic operation to install the hash 625 test = (markOop) Atomic::cmpxchg_ptr(temp, obj->mark_addr(), mark); 626 if (test == mark) { 627 return hash; 628 } 629 // If atomic operation failed, we must inflate the header 630 // into heavy weight monitor. We could add more code here 631 // for fast path, but it does not worth the complexity. 632 } else if (mark->has_monitor()) { 633 monitor = mark->monitor(); 634 temp = monitor->header(); 635 assert (temp->is_neutral(), "invariant") ; 636 hash = temp->hash(); 637 if (hash) { 638 return hash; 639 } 640 // Skip to the following code to reduce code size 641 } else if (Self->is_lock_owned((address)mark->locker())) { 642 temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned 643 assert (temp->is_neutral(), "invariant") ; 644 hash = temp->hash(); // by current thread, check if the displaced 645 if (hash) { // header contains hash code 646 return hash; 647 } 648 // WARNING: 649 // The displaced header is strictly immutable. 650 // It can NOT be changed in ANY cases. So we have 651 // to inflate the header into heavyweight monitor 652 // even the current thread owns the lock. The reason 653 // is the BasicLock (stack slot) will be asynchronously 654 // read by other threads during the inflate() function. 655 // Any change to stack may not propagate to other threads 656 // correctly. 657 } 658 659 // Inflate the monitor to set hash code 660 monitor = ObjectSynchronizer::inflate(Self, obj); 661 // Load displaced header and check it has hash code 662 mark = monitor->header(); 663 assert (mark->is_neutral(), "invariant") ; 664 hash = mark->hash(); 665 if (hash == 0) { 666 hash = get_next_hash(Self, obj); 667 temp = mark->copy_set_hash(hash); // merge hash code into header 668 assert (temp->is_neutral(), "invariant") ; 669 test = (markOop) Atomic::cmpxchg_ptr(temp, monitor, mark); 670 if (test != mark) { 671 // The only update to the header in the monitor (outside GC) 672 // is install the hash code. If someone add new usage of 673 // displaced header, please update this code 674 hash = test->hash(); 675 assert (test->is_neutral(), "invariant") ; 676 assert (hash != 0, "Trivial unexpected object/monitor header usage."); 677 } 678 } 679 // We finally get the hash 680 return hash; 681 } 682 683 // Deprecated -- use FastHashCode() instead. 684 685 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) { 686 return FastHashCode (Thread::current(), obj()) ; 687 } 688 689 690 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread, 691 Handle h_obj) { 692 if (UseBiasedLocking) { 693 BiasedLocking::revoke_and_rebias(h_obj, false, thread); 694 assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 695 } 696 697 assert(thread == JavaThread::current(), "Can only be called on current thread"); 698 oop obj = h_obj(); 699 700 markOop mark = ReadStableMark (obj) ; 701 702 // Uncontended case, header points to stack 703 if (mark->has_locker()) { 704 return thread->is_lock_owned((address)mark->locker()); 705 } 706 // Contended case, header points to ObjectMonitor (tagged pointer) 707 if (mark->has_monitor()) { 708 ObjectMonitor* monitor = mark->monitor(); 709 return monitor->is_entered(thread) != 0 ; 710 } 711 // Unlocked case, header in place 712 assert(mark->is_neutral(), "sanity check"); 713 return false; 714 } 715 716 // Be aware of this method could revoke bias of the lock object. 717 // This method queries the ownership of the lock handle specified by 'h_obj'. 718 // If the current thread owns the lock, it returns owner_self. If no 719 // thread owns the lock, it returns owner_none. Otherwise, it will return 720 // owner_other. 721 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership 722 (JavaThread *self, Handle h_obj) { 723 // The caller must beware this method can revoke bias, and 724 // revocation can result in a safepoint. 725 assert (!SafepointSynchronize::is_at_safepoint(), "invariant") ; 726 assert (self->thread_state() != _thread_blocked , "invariant") ; 727 728 // Possible mark states: neutral, biased, stack-locked, inflated 729 730 if (UseBiasedLocking && h_obj()->mark()->has_bias_pattern()) { 731 // CASE: biased 732 BiasedLocking::revoke_and_rebias(h_obj, false, self); 733 assert(!h_obj->mark()->has_bias_pattern(), 734 "biases should be revoked by now"); 735 } 736 737 assert(self == JavaThread::current(), "Can only be called on current thread"); 738 oop obj = h_obj(); 739 markOop mark = ReadStableMark (obj) ; 740 741 // CASE: stack-locked. Mark points to a BasicLock on the owner's stack. 742 if (mark->has_locker()) { 743 return self->is_lock_owned((address)mark->locker()) ? 744 owner_self : owner_other; 745 } 746 747 // CASE: inflated. Mark (tagged pointer) points to an objectMonitor. 748 // The Object:ObjectMonitor relationship is stable as long as we're 749 // not at a safepoint. 750 if (mark->has_monitor()) { 751 void * owner = mark->monitor()->_owner ; 752 if (owner == NULL) return owner_none ; 753 return (owner == self || 754 self->is_lock_owned((address)owner)) ? owner_self : owner_other; 755 } 756 757 // CASE: neutral 758 assert(mark->is_neutral(), "sanity check"); 759 return owner_none ; // it's unlocked 760 } 761 762 // FIXME: jvmti should call this 763 JavaThread* ObjectSynchronizer::get_lock_owner(Handle h_obj, bool doLock) { 764 if (UseBiasedLocking) { 765 if (SafepointSynchronize::is_at_safepoint()) { 766 BiasedLocking::revoke_at_safepoint(h_obj); 767 } else { 768 BiasedLocking::revoke_and_rebias(h_obj, false, JavaThread::current()); 769 } 770 assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now"); 771 } 772 773 oop obj = h_obj(); 774 address owner = NULL; 775 776 markOop mark = ReadStableMark (obj) ; 777 778 // Uncontended case, header points to stack 779 if (mark->has_locker()) { 780 owner = (address) mark->locker(); 781 } 782 783 // Contended case, header points to ObjectMonitor (tagged pointer) 784 if (mark->has_monitor()) { 785 ObjectMonitor* monitor = mark->monitor(); 786 assert(monitor != NULL, "monitor should be non-null"); 787 owner = (address) monitor->owner(); 788 } 789 790 if (owner != NULL) { 791 // owning_thread_from_monitor_owner() may also return NULL here 792 return Threads::owning_thread_from_monitor_owner(owner, doLock); 793 } 794 795 // Unlocked case, header in place 796 // Cannot have assertion since this object may have been 797 // locked by another thread when reaching here. 798 // assert(mark->is_neutral(), "sanity check"); 799 800 return NULL; 801 } 802 // Visitors ... 803 804 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) { 805 ObjectMonitor* block = gBlockList; 806 ObjectMonitor* mid; 807 while (block) { 808 assert(block->object() == CHAINMARKER, "must be a block header"); 809 for (int i = _BLOCKSIZE - 1; i > 0; i--) { 810 mid = block + i; 811 oop object = (oop) mid->object(); 812 if (object != NULL) { 813 closure->do_monitor(mid); 814 } 815 } 816 block = (ObjectMonitor*) block->FreeNext; 817 } 818 } 819 820 // Get the next block in the block list. 821 static inline ObjectMonitor* next(ObjectMonitor* block) { 822 assert(block->object() == CHAINMARKER, "must be a block header"); 823 block = block->FreeNext ; 824 assert(block == NULL || block->object() == CHAINMARKER, "must be a block header"); 825 return block; 826 } 827 828 829 void ObjectSynchronizer::oops_do(OopClosure* f) { 830 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 831 for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) { 832 assert(block->object() == CHAINMARKER, "must be a block header"); 833 for (int i = 1; i < _BLOCKSIZE; i++) { 834 ObjectMonitor* mid = &block[i]; 835 if (mid->object() != NULL) { 836 f->do_oop((oop*)mid->object_addr()); 837 } 838 } 839 } 840 } 841 842 843 // ----------------------------------------------------------------------------- 844 // ObjectMonitor Lifecycle 845 // ----------------------- 846 // Inflation unlinks monitors from the global gFreeList and 847 // associates them with objects. Deflation -- which occurs at 848 // STW-time -- disassociates idle monitors from objects. Such 849 // scavenged monitors are returned to the gFreeList. 850 // 851 // The global list is protected by ListLock. All the critical sections 852 // are short and operate in constant-time. 853 // 854 // ObjectMonitors reside in type-stable memory (TSM) and are immortal. 855 // 856 // Lifecycle: 857 // -- unassigned and on the global free list 858 // -- unassigned and on a thread's private omFreeList 859 // -- assigned to an object. The object is inflated and the mark refers 860 // to the objectmonitor. 861 // 862 863 864 // Constraining monitor pool growth via MonitorBound ... 865 // 866 // The monitor pool is grow-only. We scavenge at STW safepoint-time, but the 867 // the rate of scavenging is driven primarily by GC. As such, we can find 868 // an inordinate number of monitors in circulation. 869 // To avoid that scenario we can artificially induce a STW safepoint 870 // if the pool appears to be growing past some reasonable bound. 871 // Generally we favor time in space-time tradeoffs, but as there's no 872 // natural back-pressure on the # of extant monitors we need to impose some 873 // type of limit. Beware that if MonitorBound is set to too low a value 874 // we could just loop. In addition, if MonitorBound is set to a low value 875 // we'll incur more safepoints, which are harmful to performance. 876 // See also: GuaranteedSafepointInterval 877 // 878 // The current implementation uses asynchronous VM operations. 879 // 880 881 static void InduceScavenge (Thread * Self, const char * Whence) { 882 // Induce STW safepoint to trim monitors 883 // Ultimately, this results in a call to deflate_idle_monitors() in the near future. 884 // More precisely, trigger an asynchronous STW safepoint as the number 885 // of active monitors passes the specified threshold. 886 // TODO: assert thread state is reasonable 887 888 if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) { 889 if (ObjectMonitor::Knob_Verbose) { 890 ::printf ("Monitor scavenge - Induced STW @%s (%d)\n", Whence, ForceMonitorScavenge) ; 891 ::fflush(stdout) ; 892 } 893 // Induce a 'null' safepoint to scavenge monitors 894 // Must VM_Operation instance be heap allocated as the op will be enqueue and posted 895 // to the VMthread and have a lifespan longer than that of this activation record. 896 // The VMThread will delete the op when completed. 897 VMThread::execute (new VM_ForceAsyncSafepoint()) ; 898 899 if (ObjectMonitor::Knob_Verbose) { 900 ::printf ("Monitor scavenge - STW posted @%s (%d)\n", Whence, ForceMonitorScavenge) ; 901 ::fflush(stdout) ; 902 } 903 } 904 } 905 /* Too slow for general assert or debug 906 void ObjectSynchronizer::verifyInUse (Thread *Self) { 907 ObjectMonitor* mid; 908 int inusetally = 0; 909 for (mid = Self->omInUseList; mid != NULL; mid = mid->FreeNext) { 910 inusetally ++; 911 } 912 assert(inusetally == Self->omInUseCount, "inuse count off"); 913 914 int freetally = 0; 915 for (mid = Self->omFreeList; mid != NULL; mid = mid->FreeNext) { 916 freetally ++; 917 } 918 assert(freetally == Self->omFreeCount, "free count off"); 919 } 920 */ 921 ObjectMonitor * ATTR ObjectSynchronizer::omAlloc (Thread * Self) { 922 // A large MAXPRIVATE value reduces both list lock contention 923 // and list coherency traffic, but also tends to increase the 924 // number of objectMonitors in circulation as well as the STW 925 // scavenge costs. As usual, we lean toward time in space-time 926 // tradeoffs. 927 const int MAXPRIVATE = 1024 ; 928 for (;;) { 929 ObjectMonitor * m ; 930 931 // 1: try to allocate from the thread's local omFreeList. 932 // Threads will attempt to allocate first from their local list, then 933 // from the global list, and only after those attempts fail will the thread 934 // attempt to instantiate new monitors. Thread-local free lists take 935 // heat off the ListLock and improve allocation latency, as well as reducing 936 // coherency traffic on the shared global list. 937 m = Self->omFreeList ; 938 if (m != NULL) { 939 Self->omFreeList = m->FreeNext ; 940 Self->omFreeCount -- ; 941 // CONSIDER: set m->FreeNext = BAD -- diagnostic hygiene 942 guarantee (m->object() == NULL, "invariant") ; 943 if (MonitorInUseLists) { 944 m->FreeNext = Self->omInUseList; 945 Self->omInUseList = m; 946 Self->omInUseCount ++; 947 // verifyInUse(Self); 948 } else { 949 m->FreeNext = NULL; 950 } 951 return m ; 952 } 953 954 // 2: try to allocate from the global gFreeList 955 // CONSIDER: use muxTry() instead of muxAcquire(). 956 // If the muxTry() fails then drop immediately into case 3. 957 // If we're using thread-local free lists then try 958 // to reprovision the caller's free list. 959 if (gFreeList != NULL) { 960 // Reprovision the thread's omFreeList. 961 // Use bulk transfers to reduce the allocation rate and heat 962 // on various locks. 963 Thread::muxAcquire (&ListLock, "omAlloc") ; 964 for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL; ) { 965 MonitorFreeCount --; 966 ObjectMonitor * take = gFreeList ; 967 gFreeList = take->FreeNext ; 968 guarantee (take->object() == NULL, "invariant") ; 969 guarantee (!take->is_busy(), "invariant") ; 970 take->Recycle() ; 971 omRelease (Self, take, false) ; 972 } 973 Thread::muxRelease (&ListLock) ; 974 Self->omFreeProvision += 1 + (Self->omFreeProvision/2) ; 975 if (Self->omFreeProvision > MAXPRIVATE ) Self->omFreeProvision = MAXPRIVATE ; 976 TEVENT (omFirst - reprovision) ; 977 978 const int mx = MonitorBound ; 979 if (mx > 0 && (MonitorPopulation-MonitorFreeCount) > mx) { 980 // We can't safely induce a STW safepoint from omAlloc() as our thread 981 // state may not be appropriate for such activities and callers may hold 982 // naked oops, so instead we defer the action. 983 InduceScavenge (Self, "omAlloc") ; 984 } 985 continue; 986 } 987 988 // 3: allocate a block of new ObjectMonitors 989 // Both the local and global free lists are empty -- resort to malloc(). 990 // In the current implementation objectMonitors are TSM - immortal. 991 assert (_BLOCKSIZE > 1, "invariant") ; 992 ObjectMonitor * temp = new ObjectMonitor[_BLOCKSIZE]; 993 994 // NOTE: (almost) no way to recover if allocation failed. 995 // We might be able to induce a STW safepoint and scavenge enough 996 // objectMonitors to permit progress. 997 if (temp == NULL) { 998 vm_exit_out_of_memory (sizeof (ObjectMonitor[_BLOCKSIZE]), OOM_MALLOC_ERROR, 999 "Allocate ObjectMonitors"); 1000 } 1001 1002 // Format the block. 1003 // initialize the linked list, each monitor points to its next 1004 // forming the single linked free list, the very first monitor 1005 // will points to next block, which forms the block list. 1006 // The trick of using the 1st element in the block as gBlockList 1007 // linkage should be reconsidered. A better implementation would 1008 // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; } 1009 1010 for (int i = 1; i < _BLOCKSIZE ; i++) { 1011 temp[i].FreeNext = &temp[i+1]; 1012 } 1013 1014 // terminate the last monitor as the end of list 1015 temp[_BLOCKSIZE - 1].FreeNext = NULL ; 1016 1017 // Element [0] is reserved for global list linkage 1018 temp[0].set_object(CHAINMARKER); 1019 1020 // Consider carving out this thread's current request from the 1021 // block in hand. This avoids some lock traffic and redundant 1022 // list activity. 1023 1024 // Acquire the ListLock to manipulate BlockList and FreeList. 1025 // An Oyama-Taura-Yonezawa scheme might be more efficient. 1026 Thread::muxAcquire (&ListLock, "omAlloc [2]") ; 1027 MonitorPopulation += _BLOCKSIZE-1; 1028 MonitorFreeCount += _BLOCKSIZE-1; 1029 1030 // Add the new block to the list of extant blocks (gBlockList). 1031 // The very first objectMonitor in a block is reserved and dedicated. 1032 // It serves as blocklist "next" linkage. 1033 temp[0].FreeNext = gBlockList; 1034 gBlockList = temp; 1035 1036 // Add the new string of objectMonitors to the global free list 1037 temp[_BLOCKSIZE - 1].FreeNext = gFreeList ; 1038 gFreeList = temp + 1; 1039 Thread::muxRelease (&ListLock) ; 1040 TEVENT (Allocate block of monitors) ; 1041 } 1042 } 1043 1044 // Place "m" on the caller's private per-thread omFreeList. 1045 // In practice there's no need to clamp or limit the number of 1046 // monitors on a thread's omFreeList as the only time we'll call 1047 // omRelease is to return a monitor to the free list after a CAS 1048 // attempt failed. This doesn't allow unbounded #s of monitors to 1049 // accumulate on a thread's free list. 1050 // 1051 1052 void ObjectSynchronizer::omRelease (Thread * Self, ObjectMonitor * m, bool fromPerThreadAlloc) { 1053 guarantee (m->object() == NULL, "invariant") ; 1054 1055 // Remove from omInUseList 1056 if (MonitorInUseLists && fromPerThreadAlloc) { 1057 ObjectMonitor* curmidinuse = NULL; 1058 for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; ) { 1059 if (m == mid) { 1060 // extract from per-thread in-use-list 1061 if (mid == Self->omInUseList) { 1062 Self->omInUseList = mid->FreeNext; 1063 } else if (curmidinuse != NULL) { 1064 curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist 1065 } 1066 Self->omInUseCount --; 1067 // verifyInUse(Self); 1068 break; 1069 } else { 1070 curmidinuse = mid; 1071 mid = mid->FreeNext; 1072 } 1073 } 1074 } 1075 1076 // FreeNext is used for both onInUseList and omFreeList, so clear old before setting new 1077 m->FreeNext = Self->omFreeList ; 1078 Self->omFreeList = m ; 1079 Self->omFreeCount ++ ; 1080 } 1081 1082 // Return the monitors of a moribund thread's local free list to 1083 // the global free list. Typically a thread calls omFlush() when 1084 // it's dying. We could also consider having the VM thread steal 1085 // monitors from threads that have not run java code over a few 1086 // consecutive STW safepoints. Relatedly, we might decay 1087 // omFreeProvision at STW safepoints. 1088 // 1089 // Also return the monitors of a moribund thread"s omInUseList to 1090 // a global gOmInUseList under the global list lock so these 1091 // will continue to be scanned. 1092 // 1093 // We currently call omFlush() from the Thread:: dtor _after the thread 1094 // has been excised from the thread list and is no longer a mutator. 1095 // That means that omFlush() can run concurrently with a safepoint and 1096 // the scavenge operator. Calling omFlush() from JavaThread::exit() might 1097 // be a better choice as we could safely reason that that the JVM is 1098 // not at a safepoint at the time of the call, and thus there could 1099 // be not inopportune interleavings between omFlush() and the scavenge 1100 // operator. 1101 1102 void ObjectSynchronizer::omFlush (Thread * Self) { 1103 ObjectMonitor * List = Self->omFreeList ; // Null-terminated SLL 1104 Self->omFreeList = NULL ; 1105 ObjectMonitor * Tail = NULL ; 1106 int Tally = 0; 1107 if (List != NULL) { 1108 ObjectMonitor * s ; 1109 for (s = List ; s != NULL ; s = s->FreeNext) { 1110 Tally ++ ; 1111 Tail = s ; 1112 guarantee (s->object() == NULL, "invariant") ; 1113 guarantee (!s->is_busy(), "invariant") ; 1114 s->set_owner (NULL) ; // redundant but good hygiene 1115 TEVENT (omFlush - Move one) ; 1116 } 1117 guarantee (Tail != NULL && List != NULL, "invariant") ; 1118 } 1119 1120 ObjectMonitor * InUseList = Self->omInUseList; 1121 ObjectMonitor * InUseTail = NULL ; 1122 int InUseTally = 0; 1123 if (InUseList != NULL) { 1124 Self->omInUseList = NULL; 1125 ObjectMonitor *curom; 1126 for (curom = InUseList; curom != NULL; curom = curom->FreeNext) { 1127 InUseTail = curom; 1128 InUseTally++; 1129 } 1130 // TODO debug 1131 assert(Self->omInUseCount == InUseTally, "inuse count off"); 1132 Self->omInUseCount = 0; 1133 guarantee (InUseTail != NULL && InUseList != NULL, "invariant"); 1134 } 1135 1136 Thread::muxAcquire (&ListLock, "omFlush") ; 1137 if (Tail != NULL) { 1138 Tail->FreeNext = gFreeList ; 1139 gFreeList = List ; 1140 MonitorFreeCount += Tally; 1141 } 1142 1143 if (InUseTail != NULL) { 1144 InUseTail->FreeNext = gOmInUseList; 1145 gOmInUseList = InUseList; 1146 gOmInUseCount += InUseTally; 1147 } 1148 1149 Thread::muxRelease (&ListLock) ; 1150 TEVENT (omFlush) ; 1151 } 1152 1153 // Fast path code shared by multiple functions 1154 ObjectMonitor* ObjectSynchronizer::inflate_helper(oop obj) { 1155 markOop mark = obj->mark(); 1156 if (mark->has_monitor()) { 1157 assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid"); 1158 assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header"); 1159 return mark->monitor(); 1160 } 1161 return ObjectSynchronizer::inflate(Thread::current(), obj); 1162 } 1163 1164 1165 // Note that we could encounter some performance loss through false-sharing as 1166 // multiple locks occupy the same $ line. Padding might be appropriate. 1167 1168 1169 ObjectMonitor * ATTR ObjectSynchronizer::inflate (Thread * Self, oop object) { 1170 // Inflate mutates the heap ... 1171 // Relaxing assertion for bug 6320749. 1172 assert (Universe::verify_in_progress() || 1173 !SafepointSynchronize::is_at_safepoint(), "invariant") ; 1174 1175 for (;;) { 1176 const markOop mark = object->mark() ; 1177 assert (!mark->has_bias_pattern(), "invariant") ; 1178 1179 // The mark can be in one of the following states: 1180 // * Inflated - just return 1181 // * Stack-locked - coerce it to inflated 1182 // * INFLATING - busy wait for conversion to complete 1183 // * Neutral - aggressively inflate the object. 1184 // * BIASED - Illegal. We should never see this 1185 1186 // CASE: inflated 1187 if (mark->has_monitor()) { 1188 ObjectMonitor * inf = mark->monitor() ; 1189 assert (inf->header()->is_neutral(), "invariant"); 1190 assert (inf->object() == object, "invariant") ; 1191 assert (ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid"); 1192 return inf ; 1193 } 1194 1195 // CASE: inflation in progress - inflating over a stack-lock. 1196 // Some other thread is converting from stack-locked to inflated. 1197 // Only that thread can complete inflation -- other threads must wait. 1198 // The INFLATING value is transient. 1199 // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish. 1200 // We could always eliminate polling by parking the thread on some auxiliary list. 1201 if (mark == markOopDesc::INFLATING()) { 1202 TEVENT (Inflate: spin while INFLATING) ; 1203 ReadStableMark(object) ; 1204 continue ; 1205 } 1206 1207 // CASE: stack-locked 1208 // Could be stack-locked either by this thread or by some other thread. 1209 // 1210 // Note that we allocate the objectmonitor speculatively, _before_ attempting 1211 // to install INFLATING into the mark word. We originally installed INFLATING, 1212 // allocated the objectmonitor, and then finally STed the address of the 1213 // objectmonitor into the mark. This was correct, but artificially lengthened 1214 // the interval in which INFLATED appeared in the mark, thus increasing 1215 // the odds of inflation contention. 1216 // 1217 // We now use per-thread private objectmonitor free lists. 1218 // These list are reprovisioned from the global free list outside the 1219 // critical INFLATING...ST interval. A thread can transfer 1220 // multiple objectmonitors en-mass from the global free list to its local free list. 1221 // This reduces coherency traffic and lock contention on the global free list. 1222 // Using such local free lists, it doesn't matter if the omAlloc() call appears 1223 // before or after the CAS(INFLATING) operation. 1224 // See the comments in omAlloc(). 1225 1226 if (mark->has_locker()) { 1227 ObjectMonitor * m = omAlloc (Self) ; 1228 // Optimistically prepare the objectmonitor - anticipate successful CAS 1229 // We do this before the CAS in order to minimize the length of time 1230 // in which INFLATING appears in the mark. 1231 m->Recycle(); 1232 m->_Responsible = NULL ; 1233 m->OwnerIsThread = 0 ; 1234 m->_recursions = 0 ; 1235 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ; // Consider: maintain by type/class 1236 1237 markOop cmp = (markOop) Atomic::cmpxchg_ptr (markOopDesc::INFLATING(), object->mark_addr(), mark) ; 1238 if (cmp != mark) { 1239 omRelease (Self, m, true) ; 1240 continue ; // Interference -- just retry 1241 } 1242 1243 // We've successfully installed INFLATING (0) into the mark-word. 1244 // This is the only case where 0 will appear in a mark-work. 1245 // Only the singular thread that successfully swings the mark-word 1246 // to 0 can perform (or more precisely, complete) inflation. 1247 // 1248 // Why do we CAS a 0 into the mark-word instead of just CASing the 1249 // mark-word from the stack-locked value directly to the new inflated state? 1250 // Consider what happens when a thread unlocks a stack-locked object. 1251 // It attempts to use CAS to swing the displaced header value from the 1252 // on-stack basiclock back into the object header. Recall also that the 1253 // header value (hashcode, etc) can reside in (a) the object header, or 1254 // (b) a displaced header associated with the stack-lock, or (c) a displaced 1255 // header in an objectMonitor. The inflate() routine must copy the header 1256 // value from the basiclock on the owner's stack to the objectMonitor, all 1257 // the while preserving the hashCode stability invariants. If the owner 1258 // decides to release the lock while the value is 0, the unlock will fail 1259 // and control will eventually pass from slow_exit() to inflate. The owner 1260 // will then spin, waiting for the 0 value to disappear. Put another way, 1261 // the 0 causes the owner to stall if the owner happens to try to 1262 // drop the lock (restoring the header from the basiclock to the object) 1263 // while inflation is in-progress. This protocol avoids races that might 1264 // would otherwise permit hashCode values to change or "flicker" for an object. 1265 // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable. 1266 // 0 serves as a "BUSY" inflate-in-progress indicator. 1267 1268 1269 // fetch the displaced mark from the owner's stack. 1270 // The owner can't die or unwind past the lock while our INFLATING 1271 // object is in the mark. Furthermore the owner can't complete 1272 // an unlock on the object, either. 1273 markOop dmw = mark->displaced_mark_helper() ; 1274 assert (dmw->is_neutral(), "invariant") ; 1275 1276 // Setup monitor fields to proper values -- prepare the monitor 1277 m->set_header(dmw) ; 1278 1279 // Optimization: if the mark->locker stack address is associated 1280 // with this thread we could simply set m->_owner = Self and 1281 // m->OwnerIsThread = 1. Note that a thread can inflate an object 1282 // that it has stack-locked -- as might happen in wait() -- directly 1283 // with CAS. That is, we can avoid the xchg-NULL .... ST idiom. 1284 m->set_owner(mark->locker()); 1285 m->set_object(object); 1286 // TODO-FIXME: assert BasicLock->dhw != 0. 1287 1288 // Must preserve store ordering. The monitor state must 1289 // be stable at the time of publishing the monitor address. 1290 guarantee (object->mark() == markOopDesc::INFLATING(), "invariant") ; 1291 object->release_set_mark(markOopDesc::encode(m)); 1292 1293 // Hopefully the performance counters are allocated on distinct cache lines 1294 // to avoid false sharing on MP systems ... 1295 if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ; 1296 TEVENT(Inflate: overwrite stacklock) ; 1297 if (TraceMonitorInflation) { 1298 if (object->is_instance()) { 1299 ResourceMark rm; 1300 tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s", 1301 (void *) object, (intptr_t) object->mark(), 1302 object->klass()->external_name()); 1303 } 1304 } 1305 return m ; 1306 } 1307 1308 // CASE: neutral 1309 // TODO-FIXME: for entry we currently inflate and then try to CAS _owner. 1310 // If we know we're inflating for entry it's better to inflate by swinging a 1311 // pre-locked objectMonitor pointer into the object header. A successful 1312 // CAS inflates the object *and* confers ownership to the inflating thread. 1313 // In the current implementation we use a 2-step mechanism where we CAS() 1314 // to inflate and then CAS() again to try to swing _owner from NULL to Self. 1315 // An inflateTry() method that we could call from fast_enter() and slow_enter() 1316 // would be useful. 1317 1318 assert (mark->is_neutral(), "invariant"); 1319 ObjectMonitor * m = omAlloc (Self) ; 1320 // prepare m for installation - set monitor to initial state 1321 m->Recycle(); 1322 m->set_header(mark); 1323 m->set_owner(NULL); 1324 m->set_object(object); 1325 m->OwnerIsThread = 1 ; 1326 m->_recursions = 0 ; 1327 m->_Responsible = NULL ; 1328 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit ; // consider: keep metastats by type/class 1329 1330 if (Atomic::cmpxchg_ptr (markOopDesc::encode(m), object->mark_addr(), mark) != mark) { 1331 m->set_object (NULL) ; 1332 m->set_owner (NULL) ; 1333 m->OwnerIsThread = 0 ; 1334 m->Recycle() ; 1335 omRelease (Self, m, true) ; 1336 m = NULL ; 1337 continue ; 1338 // interference - the markword changed - just retry. 1339 // The state-transitions are one-way, so there's no chance of 1340 // live-lock -- "Inflated" is an absorbing state. 1341 } 1342 1343 // Hopefully the performance counters are allocated on distinct 1344 // cache lines to avoid false sharing on MP systems ... 1345 if (ObjectMonitor::_sync_Inflations != NULL) ObjectMonitor::_sync_Inflations->inc() ; 1346 TEVENT(Inflate: overwrite neutral) ; 1347 if (TraceMonitorInflation) { 1348 if (object->is_instance()) { 1349 ResourceMark rm; 1350 tty->print_cr("Inflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s", 1351 (void *) object, (intptr_t) object->mark(), 1352 object->klass()->external_name()); 1353 } 1354 } 1355 return m ; 1356 } 1357 } 1358 1359 // Note that we could encounter some performance loss through false-sharing as 1360 // multiple locks occupy the same $ line. Padding might be appropriate. 1361 1362 1363 // Deflate_idle_monitors() is called at all safepoints, immediately 1364 // after all mutators are stopped, but before any objects have moved. 1365 // It traverses the list of known monitors, deflating where possible. 1366 // The scavenged monitor are returned to the monitor free list. 1367 // 1368 // Beware that we scavenge at *every* stop-the-world point. 1369 // Having a large number of monitors in-circulation negatively 1370 // impacts the performance of some applications (e.g., PointBase). 1371 // Broadly, we want to minimize the # of monitors in circulation. 1372 // 1373 // We have added a flag, MonitorInUseLists, which creates a list 1374 // of active monitors for each thread. deflate_idle_monitors() 1375 // only scans the per-thread inuse lists. omAlloc() puts all 1376 // assigned monitors on the per-thread list. deflate_idle_monitors() 1377 // returns the non-busy monitors to the global free list. 1378 // When a thread dies, omFlush() adds the list of active monitors for 1379 // that thread to a global gOmInUseList acquiring the 1380 // global list lock. deflate_idle_monitors() acquires the global 1381 // list lock to scan for non-busy monitors to the global free list. 1382 // An alternative could have used a single global inuse list. The 1383 // downside would have been the additional cost of acquiring the global list lock 1384 // for every omAlloc(). 1385 // 1386 // Perversely, the heap size -- and thus the STW safepoint rate -- 1387 // typically drives the scavenge rate. Large heaps can mean infrequent GC, 1388 // which in turn can mean large(r) numbers of objectmonitors in circulation. 1389 // This is an unfortunate aspect of this design. 1390 // 1391 1392 enum ManifestConstants { 1393 ClearResponsibleAtSTW = 0, 1394 MaximumRecheckInterval = 1000 1395 } ; 1396 1397 // Deflate a single monitor if not in use 1398 // Return true if deflated, false if in use 1399 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj, 1400 ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) { 1401 bool deflated; 1402 // Normal case ... The monitor is associated with obj. 1403 guarantee (obj->mark() == markOopDesc::encode(mid), "invariant") ; 1404 guarantee (mid == obj->mark()->monitor(), "invariant"); 1405 guarantee (mid->header()->is_neutral(), "invariant"); 1406 1407 if (mid->is_busy()) { 1408 if (ClearResponsibleAtSTW) mid->_Responsible = NULL ; 1409 deflated = false; 1410 } else { 1411 // Deflate the monitor if it is no longer being used 1412 // It's idle - scavenge and return to the global free list 1413 // plain old deflation ... 1414 TEVENT (deflate_idle_monitors - scavenge1) ; 1415 if (TraceMonitorInflation) { 1416 if (obj->is_instance()) { 1417 ResourceMark rm; 1418 tty->print_cr("Deflating object " INTPTR_FORMAT " , mark " INTPTR_FORMAT " , type %s", 1419 (void *) obj, (intptr_t) obj->mark(), obj->klass()->external_name()); 1420 } 1421 } 1422 1423 // Restore the header back to obj 1424 obj->release_set_mark(mid->header()); 1425 mid->clear(); 1426 1427 assert (mid->object() == NULL, "invariant") ; 1428 1429 // Move the object to the working free list defined by FreeHead,FreeTail. 1430 if (*FreeHeadp == NULL) *FreeHeadp = mid; 1431 if (*FreeTailp != NULL) { 1432 ObjectMonitor * prevtail = *FreeTailp; 1433 assert(prevtail->FreeNext == NULL, "cleaned up deflated?"); // TODO KK 1434 prevtail->FreeNext = mid; 1435 } 1436 *FreeTailp = mid; 1437 deflated = true; 1438 } 1439 return deflated; 1440 } 1441 1442 // Caller acquires ListLock 1443 int ObjectSynchronizer::walk_monitor_list(ObjectMonitor** listheadp, 1444 ObjectMonitor** FreeHeadp, ObjectMonitor** FreeTailp) { 1445 ObjectMonitor* mid; 1446 ObjectMonitor* next; 1447 ObjectMonitor* curmidinuse = NULL; 1448 int deflatedcount = 0; 1449 1450 for (mid = *listheadp; mid != NULL; ) { 1451 oop obj = (oop) mid->object(); 1452 bool deflated = false; 1453 if (obj != NULL) { 1454 deflated = deflate_monitor(mid, obj, FreeHeadp, FreeTailp); 1455 } 1456 if (deflated) { 1457 // extract from per-thread in-use-list 1458 if (mid == *listheadp) { 1459 *listheadp = mid->FreeNext; 1460 } else if (curmidinuse != NULL) { 1461 curmidinuse->FreeNext = mid->FreeNext; // maintain the current thread inuselist 1462 } 1463 next = mid->FreeNext; 1464 mid->FreeNext = NULL; // This mid is current tail in the FreeHead list 1465 mid = next; 1466 deflatedcount++; 1467 } else { 1468 curmidinuse = mid; 1469 mid = mid->FreeNext; 1470 } 1471 } 1472 return deflatedcount; 1473 } 1474 1475 void ObjectSynchronizer::deflate_idle_monitors() { 1476 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); 1477 int nInuse = 0 ; // currently associated with objects 1478 int nInCirculation = 0 ; // extant 1479 int nScavenged = 0 ; // reclaimed 1480 bool deflated = false; 1481 1482 ObjectMonitor * FreeHead = NULL ; // Local SLL of scavenged monitors 1483 ObjectMonitor * FreeTail = NULL ; 1484 1485 TEVENT (deflate_idle_monitors) ; 1486 // Prevent omFlush from changing mids in Thread dtor's during deflation 1487 // And in case the vm thread is acquiring a lock during a safepoint 1488 // See e.g. 6320749 1489 Thread::muxAcquire (&ListLock, "scavenge - return") ; 1490 1491 if (MonitorInUseLists) { 1492 int inUse = 0; 1493 for (JavaThread* cur = Threads::first(); cur != NULL; cur = cur->next()) { 1494 nInCirculation+= cur->omInUseCount; 1495 int deflatedcount = walk_monitor_list(cur->omInUseList_addr(), &FreeHead, &FreeTail); 1496 cur->omInUseCount-= deflatedcount; 1497 // verifyInUse(cur); 1498 nScavenged += deflatedcount; 1499 nInuse += cur->omInUseCount; 1500 } 1501 1502 // For moribund threads, scan gOmInUseList 1503 if (gOmInUseList) { 1504 nInCirculation += gOmInUseCount; 1505 int deflatedcount = walk_monitor_list((ObjectMonitor **)&gOmInUseList, &FreeHead, &FreeTail); 1506 gOmInUseCount-= deflatedcount; 1507 nScavenged += deflatedcount; 1508 nInuse += gOmInUseCount; 1509 } 1510 1511 } else for (ObjectMonitor* block = gBlockList; block != NULL; block = next(block)) { 1512 // Iterate over all extant monitors - Scavenge all idle monitors. 1513 assert(block->object() == CHAINMARKER, "must be a block header"); 1514 nInCirculation += _BLOCKSIZE ; 1515 for (int i = 1 ; i < _BLOCKSIZE; i++) { 1516 ObjectMonitor* mid = &block[i]; 1517 oop obj = (oop) mid->object(); 1518 1519 if (obj == NULL) { 1520 // The monitor is not associated with an object. 1521 // The monitor should either be a thread-specific private 1522 // free list or the global free list. 1523 // obj == NULL IMPLIES mid->is_busy() == 0 1524 guarantee (!mid->is_busy(), "invariant") ; 1525 continue ; 1526 } 1527 deflated = deflate_monitor(mid, obj, &FreeHead, &FreeTail); 1528 1529 if (deflated) { 1530 mid->FreeNext = NULL ; 1531 nScavenged ++ ; 1532 } else { 1533 nInuse ++; 1534 } 1535 } 1536 } 1537 1538 MonitorFreeCount += nScavenged; 1539 1540 // Consider: audit gFreeList to ensure that MonitorFreeCount and list agree. 1541 1542 if (ObjectMonitor::Knob_Verbose) { 1543 ::printf ("Deflate: InCirc=%d InUse=%d Scavenged=%d ForceMonitorScavenge=%d : pop=%d free=%d\n", 1544 nInCirculation, nInuse, nScavenged, ForceMonitorScavenge, 1545 MonitorPopulation, MonitorFreeCount) ; 1546 ::fflush(stdout) ; 1547 } 1548 1549 ForceMonitorScavenge = 0; // Reset 1550 1551 // Move the scavenged monitors back to the global free list. 1552 if (FreeHead != NULL) { 1553 guarantee (FreeTail != NULL && nScavenged > 0, "invariant") ; 1554 assert (FreeTail->FreeNext == NULL, "invariant") ; 1555 // constant-time list splice - prepend scavenged segment to gFreeList 1556 FreeTail->FreeNext = gFreeList ; 1557 gFreeList = FreeHead ; 1558 } 1559 Thread::muxRelease (&ListLock) ; 1560 1561 if (ObjectMonitor::_sync_Deflations != NULL) ObjectMonitor::_sync_Deflations->inc(nScavenged) ; 1562 if (ObjectMonitor::_sync_MonExtant != NULL) ObjectMonitor::_sync_MonExtant ->set_value(nInCirculation); 1563 1564 // TODO: Add objectMonitor leak detection. 1565 // Audit/inventory the objectMonitors -- make sure they're all accounted for. 1566 GVars.stwRandom = os::random() ; 1567 GVars.stwCycle ++ ; 1568 } 1569 1570 // Monitor cleanup on JavaThread::exit 1571 1572 // Iterate through monitor cache and attempt to release thread's monitors 1573 // Gives up on a particular monitor if an exception occurs, but continues 1574 // the overall iteration, swallowing the exception. 1575 class ReleaseJavaMonitorsClosure: public MonitorClosure { 1576 private: 1577 TRAPS; 1578 1579 public: 1580 ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {} 1581 void do_monitor(ObjectMonitor* mid) { 1582 if (mid->owner() == THREAD) { 1583 (void)mid->complete_exit(CHECK); 1584 } 1585 } 1586 }; 1587 1588 // Release all inflated monitors owned by THREAD. Lightweight monitors are 1589 // ignored. This is meant to be called during JNI thread detach which assumes 1590 // all remaining monitors are heavyweight. All exceptions are swallowed. 1591 // Scanning the extant monitor list can be time consuming. 1592 // A simple optimization is to add a per-thread flag that indicates a thread 1593 // called jni_monitorenter() during its lifetime. 1594 // 1595 // Instead of No_Savepoint_Verifier it might be cheaper to 1596 // use an idiom of the form: 1597 // auto int tmp = SafepointSynchronize::_safepoint_counter ; 1598 // <code that must not run at safepoint> 1599 // guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ; 1600 // Since the tests are extremely cheap we could leave them enabled 1601 // for normal product builds. 1602 1603 void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) { 1604 assert(THREAD == JavaThread::current(), "must be current Java thread"); 1605 No_Safepoint_Verifier nsv ; 1606 ReleaseJavaMonitorsClosure rjmc(THREAD); 1607 Thread::muxAcquire(&ListLock, "release_monitors_owned_by_thread"); 1608 ObjectSynchronizer::monitors_iterate(&rjmc); 1609 Thread::muxRelease(&ListLock); 1610 THREAD->clear_pending_exception(); 1611 } 1612 1613 //------------------------------------------------------------------------------ 1614 // Non-product code 1615 1616 #ifndef PRODUCT 1617 1618 // Verify all monitors in the monitor cache, the verification is weak. 1619 void ObjectSynchronizer::verify() { 1620 ObjectMonitor* block = gBlockList; 1621 ObjectMonitor* mid; 1622 while (block) { 1623 assert(block->object() == CHAINMARKER, "must be a block header"); 1624 for (int i = 1; i < _BLOCKSIZE; i++) { 1625 mid = block + i; 1626 oop object = (oop) mid->object(); 1627 if (object != NULL) { 1628 mid->verify(); 1629 } 1630 } 1631 block = (ObjectMonitor*) block->FreeNext; 1632 } 1633 } 1634 1635 // Check if monitor belongs to the monitor cache 1636 // The list is grow-only so it's *relatively* safe to traverse 1637 // the list of extant blocks without taking a lock. 1638 1639 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) { 1640 ObjectMonitor* block = gBlockList; 1641 1642 while (block) { 1643 assert(block->object() == CHAINMARKER, "must be a block header"); 1644 if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) { 1645 address mon = (address) monitor; 1646 address blk = (address) block; 1647 size_t diff = mon - blk; 1648 assert((diff % sizeof(ObjectMonitor)) == 0, "check"); 1649 return 1; 1650 } 1651 block = (ObjectMonitor*) block->FreeNext; 1652 } 1653 return 0; 1654 } 1655 1656 #endif