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