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