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