src/share/vm/runtime/synchronizer.cpp

Tue, 29 May 2018 20:20:25 +0800

author
aoqi
date
Tue, 29 May 2018 20:20:25 +0800
changeset 9122
024be04bb151
parent 8856
ac27a9c85bea
child 9852
70aa912cebe5
permissions
-rw-r--r--

Merge

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

mercurial