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