1 /*
2 * Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/vmSymbols.hpp"
27 #include "logging/log.hpp"
28 #include "logging/logStream.hpp"
29 #include "jfr/jfrEvents.hpp"
30 #include "memory/allocation.inline.hpp"
31 #include "memory/metaspaceShared.hpp"
32 #include "memory/padded.hpp"
33 #include "memory/resourceArea.hpp"
34 #include "memory/universe.hpp"
35 #include "oops/markWord.hpp"
36 #include "oops/oop.inline.hpp"
37 #include "runtime/atomic.hpp"
38 #include "runtime/biasedLocking.hpp"
39 #include "runtime/handles.inline.hpp"
40 #include "runtime/interfaceSupport.inline.hpp"
41 #include "runtime/mutexLocker.hpp"
42 #include "runtime/objectMonitor.hpp"
43 #include "runtime/objectMonitor.inline.hpp"
44 #include "runtime/osThread.hpp"
45 #include "runtime/safepointVerifiers.hpp"
46 #include "runtime/sharedRuntime.hpp"
47 #include "runtime/stubRoutines.hpp"
48 #include "runtime/synchronizer.hpp"
49 #include "runtime/thread.inline.hpp"
50 #include "runtime/timer.hpp"
51 #include "runtime/vframe.hpp"
52 #include "runtime/vmThread.hpp"
53 #include "utilities/align.hpp"
54 #include "utilities/dtrace.hpp"
55 #include "utilities/events.hpp"
56 #include "utilities/preserveException.hpp"
57
58 // The "core" versions of monitor enter and exit reside in this file.
59 // The interpreter and compilers contain specialized transliterated
60 // variants of the enter-exit fast-path operations. See i486.ad fast_lock(),
61 // for instance. If you make changes here, make sure to modify the
62 // interpreter, and both C1 and C2 fast-path inline locking code emission.
63 //
64 // -----------------------------------------------------------------------------
65
66 #ifdef DTRACE_ENABLED
67
68 // Only bother with this argument setup if dtrace is available
69 // TODO-FIXME: probes should not fire when caller is _blocked. assert() accordingly.
70
71 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread) \
72 char* bytes = NULL; \
73 int len = 0; \
74 jlong jtid = SharedRuntime::get_java_tid(thread); \
75 Symbol* klassname = ((oop)(obj))->klass()->name(); \
76 if (klassname != NULL) { \
77 bytes = (char*)klassname->bytes(); \
78 len = klassname->utf8_length(); \
79 }
80
81 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis) \
82 { \
83 if (DTraceMonitorProbes) { \
84 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \
85 HOTSPOT_MONITOR_WAIT(jtid, \
86 (uintptr_t)(monitor), bytes, len, (millis)); \
87 } \
88 }
89
90 #define HOTSPOT_MONITOR_PROBE_notify HOTSPOT_MONITOR_NOTIFY
91 #define HOTSPOT_MONITOR_PROBE_notifyAll HOTSPOT_MONITOR_NOTIFYALL
92 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED
93
94 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread) \
95 { \
96 if (DTraceMonitorProbes) { \
97 DTRACE_MONITOR_PROBE_COMMON(obj, thread); \
98 HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */ \
99 (uintptr_t)(monitor), bytes, len); \
100 } \
101 }
102
103 #else // ndef DTRACE_ENABLED
104
105 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon) {;}
106 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon) {;}
107
108 #endif // ndef DTRACE_ENABLED
109
110 // This exists only as a workaround of dtrace bug 6254741
111 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
112 DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
113 return 0;
114 }
115
116 #define NINFLATIONLOCKS 256
117 static volatile intptr_t gInflationLocks[NINFLATIONLOCKS];
118
119 // global list of blocks of monitors
120 PaddedObjectMonitor* ObjectSynchronizer::g_block_list = NULL;
121
122 struct ObjectMonitorListGlobals {
123 char _pad_prefix[OM_CACHE_LINE_SIZE];
124 // These are highly shared list related variables.
125 // To avoid false-sharing they need to be the sole occupants of a cache line.
126
127 // Global ObjectMonitor free list. Newly allocated and deflated
128 // ObjectMonitors are prepended here.
129 ObjectMonitor* _free_list;
130 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(ObjectMonitor*));
131
132 // Global ObjectMonitor in-use list. When a JavaThread is exiting,
133 // ObjectMonitors on its per-thread in-use list are prepended here.
134 ObjectMonitor* _in_use_list;
135 DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(ObjectMonitor*));
136
137 int _free_count; // # on free_list
138 DEFINE_PAD_MINUS_SIZE(3, OM_CACHE_LINE_SIZE, sizeof(int));
139
140 int _in_use_count; // # on in_use_list
141 DEFINE_PAD_MINUS_SIZE(4, OM_CACHE_LINE_SIZE, sizeof(int));
142
143 int _population; // # Extant -- in circulation
144 DEFINE_PAD_MINUS_SIZE(5, OM_CACHE_LINE_SIZE, sizeof(int));
145 };
146 static ObjectMonitorListGlobals om_list_globals;
147
148 #define CHAINMARKER (cast_to_oop<intptr_t>(-1))
149
150
151 // =====================> Spin-lock functions
152
153 // ObjectMonitors are not lockable outside of this file. We use spin-locks
154 // implemented using a bit in the _next_om field instead of the heavier
155 // weight locking mechanisms for faster list management.
156
157 #define OM_LOCK_BIT 0x1
158
159 // Return true if the ObjectMonitor is locked.
160 // Otherwise returns false.
161 static bool is_locked(ObjectMonitor* om) {
162 return ((intptr_t)om->next_om() & OM_LOCK_BIT) == OM_LOCK_BIT;
163 }
164
165 // Mark an ObjectMonitor* with OM_LOCK_BIT and return it.
166 static ObjectMonitor* mark_om_ptr(ObjectMonitor* om) {
167 return (ObjectMonitor*)((intptr_t)om | OM_LOCK_BIT);
168 }
169
170 // Return the unmarked next field in an ObjectMonitor. Note: the next
171 // field may or may not have been marked with OM_LOCK_BIT originally.
172 static ObjectMonitor* unmarked_next(ObjectMonitor* om) {
173 return (ObjectMonitor*)((intptr_t)om->next_om() & ~OM_LOCK_BIT);
174 }
175
176 // Try to lock an ObjectMonitor. Returns true if locking was successful.
177 // Otherwise returns false.
178 static bool try_om_lock(ObjectMonitor* om) {
179 // Get current next field without any OM_LOCK_BIT value.
180 ObjectMonitor* next = unmarked_next(om);
181 if (om->try_set_next_om(next, mark_om_ptr(next)) != next) {
182 return false; // Cannot lock the ObjectMonitor.
183 }
184 return true;
185 }
186
187 // Lock an ObjectMonitor.
188 static void om_lock(ObjectMonitor* om) {
189 while (true) {
190 if (try_om_lock(om)) {
191 return;
192 }
193 }
194 }
195
196 // Unlock an ObjectMonitor.
197 static void om_unlock(ObjectMonitor* om) {
198 ObjectMonitor* next = om->next_om();
199 guarantee(((intptr_t)next & OM_LOCK_BIT) == OM_LOCK_BIT, "next=" INTPTR_FORMAT
200 " must have OM_LOCK_BIT=%x set.", p2i(next), OM_LOCK_BIT);
201
202 next = (ObjectMonitor*)((intptr_t)next & ~OM_LOCK_BIT); // Clear OM_LOCK_BIT.
203 om->set_next_om(next);
204 }
205
206 // Get the list head after locking it. Returns the list head or NULL
207 // if the list is empty.
208 static ObjectMonitor* get_list_head_locked(ObjectMonitor** list_p) {
209 while (true) {
210 ObjectMonitor* mid = Atomic::load(list_p);
211 if (mid == NULL) {
212 return NULL; // The list is empty.
213 }
214 if (try_om_lock(mid)) {
215 if (Atomic::load(list_p) != mid) {
216 // The list head changed before we could lock it so we have to retry.
217 om_unlock(mid);
218 continue;
219 }
220 return mid;
221 }
222 }
223 }
224
225 #undef OM_LOCK_BIT
226
227
228 // =====================> List Management functions
229
230 // Prepend a list of ObjectMonitors to the specified *list_p. 'tail' is
231 // the last ObjectMonitor in the list and there are 'count' on the list.
232 // Also updates the specified *count_p.
233 static void prepend_list_to_common(ObjectMonitor* list, ObjectMonitor* tail,
234 int count, ObjectMonitor** list_p,
235 int* count_p) {
236 while (true) {
237 ObjectMonitor* cur = Atomic::load(list_p);
238 // Prepend list to *list_p.
239 if (!try_om_lock(tail)) {
240 // Failed to lock tail due to a list walker so try it all again.
241 continue;
242 }
243 tail->set_next_om(cur); // tail now points to cur (and unlocks tail)
244 if (cur == NULL) {
245 // No potential race with takers or other prependers since
246 // *list_p is empty.
247 if (Atomic::cmpxchg(list_p, cur, list) == cur) {
248 // Successfully switched *list_p to the list value.
249 Atomic::add(count_p, count);
250 break;
251 }
252 // Implied else: try it all again
253 } else {
254 if (!try_om_lock(cur)) {
255 continue; // failed to lock cur so try it all again
256 }
257 // We locked cur so try to switch *list_p to the list value.
258 if (Atomic::cmpxchg(list_p, cur, list) != cur) {
259 // The list head has changed so unlock cur and try again:
260 om_unlock(cur);
261 continue;
262 }
263 Atomic::add(count_p, count);
264 om_unlock(cur);
265 break;
266 }
267 }
268 }
269
270 // Prepend a newly allocated block of ObjectMonitors to g_block_list and
271 // om_list_globals._free_list. Also updates om_list_globals._population
272 // and om_list_globals._free_count.
273 void ObjectSynchronizer::prepend_block_to_lists(PaddedObjectMonitor* new_blk) {
274 // First we handle g_block_list:
275 while (true) {
276 PaddedObjectMonitor* cur = Atomic::load(&g_block_list);
277 // Prepend new_blk to g_block_list. The first ObjectMonitor in
278 // a block is reserved for use as linkage to the next block.
279 new_blk[0].set_next_om(cur);
280 if (Atomic::cmpxchg(&g_block_list, cur, new_blk) == cur) {
281 // Successfully switched g_block_list to the new_blk value.
282 Atomic::add(&om_list_globals._population, _BLOCKSIZE - 1);
283 break;
284 }
285 // Implied else: try it all again
286 }
287
288 // Second we handle om_list_globals._free_list:
289 prepend_list_to_common(new_blk + 1, &new_blk[_BLOCKSIZE - 1], _BLOCKSIZE - 1,
290 &om_list_globals._free_list, &om_list_globals._free_count);
291 }
292
293 // Prepend a list of ObjectMonitors to om_list_globals._free_list.
294 // 'tail' is the last ObjectMonitor in the list and there are 'count'
295 // on the list. Also updates om_list_globals._free_count.
296 static void prepend_list_to_global_free_list(ObjectMonitor* list,
297 ObjectMonitor* tail, int count) {
298 prepend_list_to_common(list, tail, count, &om_list_globals._free_list,
299 &om_list_globals._free_count);
300 }
301
302 // Prepend a list of ObjectMonitors to om_list_globals._in_use_list.
303 // 'tail' is the last ObjectMonitor in the list and there are 'count'
304 // on the list. Also updates om_list_globals._in_use_list.
305 static void prepend_list_to_global_in_use_list(ObjectMonitor* list,
306 ObjectMonitor* tail, int count) {
307 prepend_list_to_common(list, tail, count, &om_list_globals._in_use_list,
308 &om_list_globals._in_use_count);
309 }
310
311 // Prepend an ObjectMonitor to the specified list. Also updates
312 // the specified counter.
313 static void prepend_to_common(ObjectMonitor* m, ObjectMonitor** list_p,
314 int* count_p) {
315 while (true) {
316 om_lock(m); // Lock m so we can safely update its next field.
317 ObjectMonitor* cur = NULL;
318 // Lock the list head to guard against races with a list walker
319 // thread:
320 if ((cur = get_list_head_locked(list_p)) != NULL) {
321 // List head is now locked so we can safely switch it.
322 m->set_next_om(cur); // m now points to cur (and unlocks m)
323 Atomic::store(list_p, m); // Switch list head to unlocked m.
324 om_unlock(cur);
325 break;
326 }
327 // The list is empty so try to set the list head.
328 assert(cur == NULL, "cur must be NULL: cur=" INTPTR_FORMAT, p2i(cur));
329 m->set_next_om(cur); // m now points to NULL (and unlocks m)
330 if (Atomic::cmpxchg(list_p, cur, m) == cur) {
331 // List head is now unlocked m.
332 break;
333 }
334 // Implied else: try it all again
335 }
336 Atomic::inc(count_p);
337 }
338
339 // Prepend an ObjectMonitor to a per-thread om_free_list.
340 // Also updates the per-thread om_free_count.
341 static void prepend_to_om_free_list(Thread* self, ObjectMonitor* m) {
342 prepend_to_common(m, &self->om_free_list, &self->om_free_count);
343 }
344
345 // Prepend an ObjectMonitor to a per-thread om_in_use_list.
346 // Also updates the per-thread om_in_use_count.
347 static void prepend_to_om_in_use_list(Thread* self, ObjectMonitor* m) {
348 prepend_to_common(m, &self->om_in_use_list, &self->om_in_use_count);
349 }
350
351 // Take an ObjectMonitor from the start of the specified list. Also
352 // decrements the specified counter. Returns NULL if none are available.
353 static ObjectMonitor* take_from_start_of_common(ObjectMonitor** list_p,
354 int* count_p) {
355 ObjectMonitor* take = NULL;
356 // Lock the list head to guard against races with a list walker
357 // thread:
358 if ((take = get_list_head_locked(list_p)) == NULL) {
359 return NULL; // None are available.
360 }
361 ObjectMonitor* next = unmarked_next(take);
362 // Switch locked list head to next (which unlocks the list head, but
363 // leaves take locked):
364 Atomic::store(list_p, next);
365 Atomic::dec(count_p);
366 // Unlock take, but leave the next value for any lagging list
367 // walkers. It will get cleaned up when take is prepended to
368 // the in-use list:
369 om_unlock(take);
370 return take;
371 }
372
373 // Take an ObjectMonitor from the start of the om_list_globals._free_list.
374 // Also updates om_list_globals._free_count. Returns NULL if none are
375 // available.
376 static ObjectMonitor* take_from_start_of_global_free_list() {
377 return take_from_start_of_common(&om_list_globals._free_list,
378 &om_list_globals._free_count);
379 }
380
381 // Take an ObjectMonitor from the start of a per-thread free-list.
382 // Also updates om_free_count. Returns NULL if none are available.
383 static ObjectMonitor* take_from_start_of_om_free_list(Thread* self) {
384 return take_from_start_of_common(&self->om_free_list, &self->om_free_count);
385 }
386
387
388 // =====================> Quick functions
389
390 // The quick_* forms are special fast-path variants used to improve
391 // performance. In the simplest case, a "quick_*" implementation could
392 // simply return false, in which case the caller will perform the necessary
393 // state transitions and call the slow-path form.
394 // The fast-path is designed to handle frequently arising cases in an efficient
395 // manner and is just a degenerate "optimistic" variant of the slow-path.
396 // returns true -- to indicate the call was satisfied.
397 // returns false -- to indicate the call needs the services of the slow-path.
398 // A no-loitering ordinance is in effect for code in the quick_* family
399 // operators: safepoints or indefinite blocking (blocking that might span a
400 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon
401 // entry.
402 //
403 // Consider: An interesting optimization is to have the JIT recognize the
404 // following common idiom:
405 // synchronized (someobj) { .... ; notify(); }
406 // That is, we find a notify() or notifyAll() call that immediately precedes
407 // the monitorexit operation. In that case the JIT could fuse the operations
408 // into a single notifyAndExit() runtime primitive.
409
410 bool ObjectSynchronizer::quick_notify(oopDesc* obj, Thread* self, bool all) {
411 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
412 assert(self->is_Java_thread(), "invariant");
413 assert(((JavaThread *) self)->thread_state() == _thread_in_Java, "invariant");
414 NoSafepointVerifier nsv;
415 if (obj == NULL) return false; // slow-path for invalid obj
416 const markWord mark = obj->mark();
417
418 if (mark.has_locker() && self->is_lock_owned((address)mark.locker())) {
419 // Degenerate notify
420 // stack-locked by caller so by definition the implied waitset is empty.
421 return true;
422 }
423
424 if (mark.has_monitor()) {
425 ObjectMonitor* const mon = mark.monitor();
426 assert(mon->object() == obj, "invariant");
427 if (mon->owner() != self) return false; // slow-path for IMS exception
428
429 if (mon->first_waiter() != NULL) {
430 // We have one or more waiters. Since this is an inflated monitor
431 // that we own, we can transfer one or more threads from the waitset
432 // to the entrylist here and now, avoiding the slow-path.
433 if (all) {
434 DTRACE_MONITOR_PROBE(notifyAll, mon, obj, self);
435 } else {
436 DTRACE_MONITOR_PROBE(notify, mon, obj, self);
437 }
438 int free_count = 0;
439 do {
440 mon->INotify(self);
441 ++free_count;
442 } while (mon->first_waiter() != NULL && all);
443 OM_PERFDATA_OP(Notifications, inc(free_count));
444 }
445 return true;
446 }
447
448 // biased locking and any other IMS exception states take the slow-path
449 return false;
450 }
451
452
453 // The LockNode emitted directly at the synchronization site would have
454 // been too big if it were to have included support for the cases of inflated
455 // recursive enter and exit, so they go here instead.
456 // Note that we can't safely call AsyncPrintJavaStack() from within
457 // quick_enter() as our thread state remains _in_Java.
458
459 bool ObjectSynchronizer::quick_enter(oop obj, Thread* self,
460 BasicLock * lock) {
461 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
462 assert(self->is_Java_thread(), "invariant");
463 assert(((JavaThread *) self)->thread_state() == _thread_in_Java, "invariant");
464 NoSafepointVerifier nsv;
465 if (obj == NULL) return false; // Need to throw NPE
466 const markWord mark = obj->mark();
467
468 if (mark.has_monitor()) {
469 ObjectMonitor* const m = mark.monitor();
470 assert(m->object() == obj, "invariant");
471 Thread* const owner = (Thread *) m->_owner;
472
473 // Lock contention and Transactional Lock Elision (TLE) diagnostics
474 // and observability
475 // Case: light contention possibly amenable to TLE
476 // Case: TLE inimical operations such as nested/recursive synchronization
477
478 if (owner == self) {
479 m->_recursions++;
480 return true;
481 }
482
483 // This Java Monitor is inflated so obj's header will never be
484 // displaced to this thread's BasicLock. Make the displaced header
485 // non-NULL so this BasicLock is not seen as recursive nor as
486 // being locked. We do this unconditionally so that this thread's
487 // BasicLock cannot be mis-interpreted by any stack walkers. For
488 // performance reasons, stack walkers generally first check for
489 // Biased Locking in the object's header, the second check is for
490 // stack-locking in the object's header, the third check is for
491 // recursive stack-locking in the displaced header in the BasicLock,
492 // and last are the inflated Java Monitor (ObjectMonitor) checks.
493 lock->set_displaced_header(markWord::unused_mark());
494
495 if (owner == NULL && m->try_set_owner_from(NULL, self) == NULL) {
496 assert(m->_recursions == 0, "invariant");
497 return true;
498 }
499 }
500
501 // Note that we could inflate in quick_enter.
502 // This is likely a useful optimization
503 // Critically, in quick_enter() we must not:
504 // -- perform bias revocation, or
505 // -- block indefinitely, or
506 // -- reach a safepoint
507
508 return false; // revert to slow-path
509 }
510
511 // -----------------------------------------------------------------------------
512 // Monitor Enter/Exit
513 // The interpreter and compiler assembly code tries to lock using the fast path
514 // of this algorithm. Make sure to update that code if the following function is
515 // changed. The implementation is extremely sensitive to race condition. Be careful.
516
517 void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, TRAPS) {
518 if (UseBiasedLocking) {
519 if (!SafepointSynchronize::is_at_safepoint()) {
520 BiasedLocking::revoke(obj, THREAD);
521 } else {
522 BiasedLocking::revoke_at_safepoint(obj);
523 }
524 }
525
526 markWord mark = obj->mark();
527 assert(!mark.has_bias_pattern(), "should not see bias pattern here");
528
529 if (mark.is_neutral()) {
530 // Anticipate successful CAS -- the ST of the displaced mark must
531 // be visible <= the ST performed by the CAS.
532 lock->set_displaced_header(mark);
533 if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) {
534 return;
535 }
536 // Fall through to inflate() ...
537 } else if (mark.has_locker() &&
538 THREAD->is_lock_owned((address)mark.locker())) {
539 assert(lock != mark.locker(), "must not re-lock the same lock");
540 assert(lock != (BasicLock*)obj->mark().value(), "don't relock with same BasicLock");
541 lock->set_displaced_header(markWord::from_pointer(NULL));
542 return;
543 }
544
545 // The object header will never be displaced to this lock,
546 // so it does not matter what the value is, except that it
547 // must be non-zero to avoid looking like a re-entrant lock,
548 // and must not look locked either.
549 lock->set_displaced_header(markWord::unused_mark());
550 inflate(THREAD, obj(), inflate_cause_monitor_enter)->enter(THREAD);
551 }
552
553 void ObjectSynchronizer::exit(oop object, BasicLock* lock, TRAPS) {
554 markWord mark = object->mark();
555 // We cannot check for Biased Locking if we are racing an inflation.
556 assert(mark == markWord::INFLATING() ||
557 !mark.has_bias_pattern(), "should not see bias pattern here");
558
559 markWord dhw = lock->displaced_header();
560 if (dhw.value() == 0) {
561 // If the displaced header is NULL, then this exit matches up with
562 // a recursive enter. No real work to do here except for diagnostics.
563 #ifndef PRODUCT
564 if (mark != markWord::INFLATING()) {
565 // Only do diagnostics if we are not racing an inflation. Simply
566 // exiting a recursive enter of a Java Monitor that is being
567 // inflated is safe; see the has_monitor() comment below.
568 assert(!mark.is_neutral(), "invariant");
569 assert(!mark.has_locker() ||
570 THREAD->is_lock_owned((address)mark.locker()), "invariant");
571 if (mark.has_monitor()) {
572 // The BasicLock's displaced_header is marked as a recursive
573 // enter and we have an inflated Java Monitor (ObjectMonitor).
574 // This is a special case where the Java Monitor was inflated
575 // after this thread entered the stack-lock recursively. When a
576 // Java Monitor is inflated, we cannot safely walk the Java
577 // Monitor owner's stack and update the BasicLocks because a
578 // Java Monitor can be asynchronously inflated by a thread that
579 // does not own the Java Monitor.
580 ObjectMonitor* m = mark.monitor();
581 assert(((oop)(m->object()))->mark() == mark, "invariant");
582 assert(m->is_entered(THREAD), "invariant");
583 }
584 }
585 #endif
586 return;
587 }
588
589 if (mark == markWord::from_pointer(lock)) {
590 // If the object is stack-locked by the current thread, try to
591 // swing the displaced header from the BasicLock back to the mark.
592 assert(dhw.is_neutral(), "invariant");
593 if (object->cas_set_mark(dhw, mark) == mark) {
594 return;
595 }
596 }
597
598 // We have to take the slow-path of possible inflation and then exit.
599 inflate(THREAD, object, inflate_cause_vm_internal)->exit(true, THREAD);
600 }
601
602 // -----------------------------------------------------------------------------
603 // Class Loader support to workaround deadlocks on the class loader lock objects
604 // Also used by GC
605 // complete_exit()/reenter() are used to wait on a nested lock
606 // i.e. to give up an outer lock completely and then re-enter
607 // Used when holding nested locks - lock acquisition order: lock1 then lock2
608 // 1) complete_exit lock1 - saving recursion count
609 // 2) wait on lock2
610 // 3) when notified on lock2, unlock lock2
611 // 4) reenter lock1 with original recursion count
612 // 5) lock lock2
613 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
614 // NOTE(TSAN): We cannot instrument complete_exit/reenter in ObjectSynchronizer
615 // in a manner similar to wait and waitUninterruptibly, because
616 // (1) recursion count stored by inflated monitor is different from
617 // the absolute recursion count tracked by Tsan, and (2) in the
618 // general case, we cannot merely store Tsan's recursion count
619 // once: we must track it for *each invocation* of complete_exit.
620 // Hence, the best place to instrument for Tsan is at the call site
621 // for complete_exit/reenter. Luckily, there is only one call site.
622 intx ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
623 if (UseBiasedLocking) {
624 BiasedLocking::revoke(obj, THREAD);
625 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
626 }
627
628 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
629
630 return monitor->complete_exit(THREAD);
631 }
632
633 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
634 void ObjectSynchronizer::reenter(Handle obj, intx recursions, TRAPS) {
635 if (UseBiasedLocking) {
636 BiasedLocking::revoke(obj, THREAD);
637 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
638 }
639
640 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
641
642 monitor->reenter(recursions, THREAD);
643 }
644 // -----------------------------------------------------------------------------
645 // JNI locks on java objects
646 // NOTE: must use heavy weight monitor to handle jni monitor enter
647 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) {
648 // the current locking is from JNI instead of Java code
649 if (UseBiasedLocking) {
650 BiasedLocking::revoke(obj, THREAD);
651 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
652 }
653 THREAD->set_current_pending_monitor_is_from_java(false);
654 inflate(THREAD, obj(), inflate_cause_jni_enter)->enter(THREAD);
655 THREAD->set_current_pending_monitor_is_from_java(true);
656 TSAN_RUNTIME_ONLY(SharedRuntime::tsan_oop_lock(THREAD, obj()));
657 }
658
659 // NOTE: must use heavy weight monitor to handle jni monitor exit
660 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
661 if (UseBiasedLocking) {
662 Handle h_obj(THREAD, obj);
663 BiasedLocking::revoke(h_obj, THREAD);
664 obj = h_obj();
665 }
666 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
667
668 ObjectMonitor* monitor = inflate(THREAD, obj, inflate_cause_jni_exit);
669 // If this thread has locked the object, exit the monitor. We
670 // intentionally do not use CHECK here because we must exit the
671 // monitor even if an exception is pending.
672 if (monitor->check_owner(THREAD)) {
673 TSAN_RUNTIME_ONLY(SharedRuntime::tsan_oop_unlock(THREAD, obj));
674 monitor->exit(true, THREAD);
675 }
676 }
677
678 // -----------------------------------------------------------------------------
679 // Internal VM locks on java objects
680 // standard constructor, allows locking failures
681 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool do_lock) {
682 _dolock = do_lock;
683 _thread = thread;
684 _thread->check_for_valid_safepoint_state();
685 _obj = obj;
686
687 if (_dolock) {
688 ObjectSynchronizer::enter(_obj, &_lock, _thread);
689 TSAN_RUNTIME_ONLY(SharedRuntime::tsan_oop_lock(_thread, _obj()));
690 }
691 }
692
693 ObjectLocker::~ObjectLocker() {
694 if (_dolock) {
695 TSAN_RUNTIME_ONLY(SharedRuntime::tsan_oop_unlock(_thread, _obj()));
696 ObjectSynchronizer::exit(_obj(), &_lock, _thread);
697 }
698 }
699
700
701 // -----------------------------------------------------------------------------
702 // Wait/Notify/NotifyAll
703 // NOTE: must use heavy weight monitor to handle wait()
704 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
705 if (UseBiasedLocking) {
706 BiasedLocking::revoke(obj, THREAD);
707 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
708 }
709 if (millis < 0) {
710 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
711 }
712 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_wait);
713
714 DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
715
716 TSAN_ONLY(int tsan_rec = 0;)
717 TSAN_RUNTIME_ONLY(
718 tsan_rec = SharedRuntime::tsan_oop_rec_unlock(THREAD, obj());
719 assert(tsan_rec > 0, "tsan: unlocking unlocked mutex");
720 );
721
722 monitor->wait(millis, true, THREAD);
723
724 TSAN_RUNTIME_ONLY(SharedRuntime::tsan_oop_rec_lock(THREAD, obj(), tsan_rec));
725
726 // This dummy call is in place to get around dtrace bug 6254741. Once
727 // that's fixed we can uncomment the following line, remove the call
728 // and change this function back into a "void" func.
729 // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
730 return dtrace_waited_probe(monitor, obj, THREAD);
731 }
732
733 void ObjectSynchronizer::wait_uninterruptibly(Handle obj, jlong millis, TRAPS) {
734 if (UseBiasedLocking) {
735 BiasedLocking::revoke(obj, THREAD);
736 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
737 }
738 if (millis < 0) {
739 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
740 }
741 ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_wait);
742 TSAN_ONLY(int tsan_rec;)
743 TSAN_RUNTIME_ONLY(
744 tsan_rec = SharedRuntime::tsan_oop_rec_unlock(THREAD, obj());
745 assert(tsan_rec > 0, "tsan: unlocking unlocked mutex");
746 );
747 monitor->wait(millis, false, THREAD);
748 TSAN_RUNTIME_ONLY(SharedRuntime::tsan_oop_rec_lock(THREAD, obj(), tsan_rec));
749 }
750
751 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
752 if (UseBiasedLocking) {
753 BiasedLocking::revoke(obj, THREAD);
754 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
755 }
756
757 markWord mark = obj->mark();
758 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
759 return;
760 }
761 inflate(THREAD, obj(), inflate_cause_notify)->notify(THREAD);
762 }
763
764 // NOTE: see comment of notify()
765 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
766 if (UseBiasedLocking) {
767 BiasedLocking::revoke(obj, THREAD);
768 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
769 }
770
771 markWord mark = obj->mark();
772 if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
773 return;
774 }
775 inflate(THREAD, obj(), inflate_cause_notify)->notifyAll(THREAD);
776 }
777
778 // -----------------------------------------------------------------------------
779 // Hash Code handling
780 //
781 // Performance concern:
782 // OrderAccess::storestore() calls release() which at one time stored 0
783 // into the global volatile OrderAccess::dummy variable. This store was
784 // unnecessary for correctness. Many threads storing into a common location
785 // causes considerable cache migration or "sloshing" on large SMP systems.
786 // As such, I avoided using OrderAccess::storestore(). In some cases
787 // OrderAccess::fence() -- which incurs local latency on the executing
788 // processor -- is a better choice as it scales on SMP systems.
789 //
790 // See http://blogs.oracle.com/dave/entry/biased_locking_in_hotspot for
791 // a discussion of coherency costs. Note that all our current reference
792 // platforms provide strong ST-ST order, so the issue is moot on IA32,
793 // x64, and SPARC.
794 //
795 // As a general policy we use "volatile" to control compiler-based reordering
796 // and explicit fences (barriers) to control for architectural reordering
797 // performed by the CPU(s) or platform.
798
799 struct SharedGlobals {
800 char _pad_prefix[OM_CACHE_LINE_SIZE];
801 // These are highly shared mostly-read variables.
802 // To avoid false-sharing they need to be the sole occupants of a cache line.
803 volatile int stw_random;
804 volatile int stw_cycle;
805 DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(volatile int) * 2);
806 // Hot RW variable -- Sequester to avoid false-sharing
807 volatile int hc_sequence;
808 DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(volatile int));
809 };
810
811 static SharedGlobals GVars;
812 static int _forceMonitorScavenge = 0; // Scavenge required and pending
813
814 static markWord read_stable_mark(oop obj) {
815 markWord mark = obj->mark();
816 if (!mark.is_being_inflated()) {
817 return mark; // normal fast-path return
818 }
819
820 int its = 0;
821 for (;;) {
822 markWord mark = obj->mark();
823 if (!mark.is_being_inflated()) {
824 return mark; // normal fast-path return
825 }
826
827 // The object is being inflated by some other thread.
828 // The caller of read_stable_mark() must wait for inflation to complete.
829 // Avoid live-lock
830 // TODO: consider calling SafepointSynchronize::do_call_back() while
831 // spinning to see if there's a safepoint pending. If so, immediately
832 // yielding or blocking would be appropriate. Avoid spinning while
833 // there is a safepoint pending.
834 // TODO: add inflation contention performance counters.
835 // TODO: restrict the aggregate number of spinners.
836
837 ++its;
838 if (its > 10000 || !os::is_MP()) {
839 if (its & 1) {
840 os::naked_yield();
841 } else {
842 // Note that the following code attenuates the livelock problem but is not
843 // a complete remedy. A more complete solution would require that the inflating
844 // thread hold the associated inflation lock. The following code simply restricts
845 // the number of spinners to at most one. We'll have N-2 threads blocked
846 // on the inflationlock, 1 thread holding the inflation lock and using
847 // a yield/park strategy, and 1 thread in the midst of inflation.
848 // A more refined approach would be to change the encoding of INFLATING
849 // to allow encapsulation of a native thread pointer. Threads waiting for
850 // inflation to complete would use CAS to push themselves onto a singly linked
851 // list rooted at the markword. Once enqueued, they'd loop, checking a per-thread flag
852 // and calling park(). When inflation was complete the thread that accomplished inflation
853 // would detach the list and set the markword to inflated with a single CAS and
854 // then for each thread on the list, set the flag and unpark() the thread.
855 // This is conceptually similar to muxAcquire-muxRelease, except that muxRelease
856 // wakes at most one thread whereas we need to wake the entire list.
857 int ix = (cast_from_oop<intptr_t>(obj) >> 5) & (NINFLATIONLOCKS-1);
858 int YieldThenBlock = 0;
859 assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant");
860 assert((NINFLATIONLOCKS & (NINFLATIONLOCKS-1)) == 0, "invariant");
861 Thread::muxAcquire(gInflationLocks + ix, "gInflationLock");
862 while (obj->mark() == markWord::INFLATING()) {
863 // Beware: NakedYield() is advisory and has almost no effect on some platforms
864 // so we periodically call self->_ParkEvent->park(1).
865 // We use a mixed spin/yield/block mechanism.
866 if ((YieldThenBlock++) >= 16) {
867 Thread::current()->_ParkEvent->park(1);
868 } else {
869 os::naked_yield();
870 }
871 }
872 Thread::muxRelease(gInflationLocks + ix);
873 }
874 } else {
875 SpinPause(); // SMP-polite spinning
876 }
877 }
878 }
879
880 // hashCode() generation :
881 //
882 // Possibilities:
883 // * MD5Digest of {obj,stw_random}
884 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
885 // * A DES- or AES-style SBox[] mechanism
886 // * One of the Phi-based schemes, such as:
887 // 2654435761 = 2^32 * Phi (golden ratio)
888 // HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
889 // * A variation of Marsaglia's shift-xor RNG scheme.
890 // * (obj ^ stw_random) is appealing, but can result
891 // in undesirable regularity in the hashCode values of adjacent objects
892 // (objects allocated back-to-back, in particular). This could potentially
893 // result in hashtable collisions and reduced hashtable efficiency.
894 // There are simple ways to "diffuse" the middle address bits over the
895 // generated hashCode values:
896
897 static inline intptr_t get_next_hash(Thread* self, oop obj) {
898 intptr_t value = 0;
899 if (hashCode == 0) {
900 // This form uses global Park-Miller RNG.
901 // On MP system we'll have lots of RW access to a global, so the
902 // mechanism induces lots of coherency traffic.
903 value = os::random();
904 } else if (hashCode == 1) {
905 // This variation has the property of being stable (idempotent)
906 // between STW operations. This can be useful in some of the 1-0
907 // synchronization schemes.
908 intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
909 value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
910 } else if (hashCode == 2) {
911 value = 1; // for sensitivity testing
912 } else if (hashCode == 3) {
913 value = ++GVars.hc_sequence;
914 } else if (hashCode == 4) {
915 value = cast_from_oop<intptr_t>(obj);
916 } else {
917 // Marsaglia's xor-shift scheme with thread-specific state
918 // This is probably the best overall implementation -- we'll
919 // likely make this the default in future releases.
920 unsigned t = self->_hashStateX;
921 t ^= (t << 11);
922 self->_hashStateX = self->_hashStateY;
923 self->_hashStateY = self->_hashStateZ;
924 self->_hashStateZ = self->_hashStateW;
925 unsigned v = self->_hashStateW;
926 v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
927 self->_hashStateW = v;
928 value = v;
929 }
930
931 value &= markWord::hash_mask;
932 if (value == 0) value = 0xBAD;
933 assert(value != markWord::no_hash, "invariant");
934 return value;
935 }
936
937 intptr_t ObjectSynchronizer::FastHashCode(Thread* self, oop obj) {
938 if (UseBiasedLocking) {
939 // NOTE: many places throughout the JVM do not expect a safepoint
940 // to be taken here, in particular most operations on perm gen
941 // objects. However, we only ever bias Java instances and all of
942 // the call sites of identity_hash that might revoke biases have
943 // been checked to make sure they can handle a safepoint. The
944 // added check of the bias pattern is to avoid useless calls to
945 // thread-local storage.
946 if (obj->mark().has_bias_pattern()) {
947 // Handle for oop obj in case of STW safepoint
948 Handle hobj(self, obj);
949 // Relaxing assertion for bug 6320749.
950 assert(Universe::verify_in_progress() ||
951 !SafepointSynchronize::is_at_safepoint(),
952 "biases should not be seen by VM thread here");
953 BiasedLocking::revoke(hobj, JavaThread::current());
954 obj = hobj();
955 assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
956 }
957 }
958
959 // hashCode() is a heap mutator ...
960 // Relaxing assertion for bug 6320749.
961 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
962 !SafepointSynchronize::is_at_safepoint(), "invariant");
963 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
964 self->is_Java_thread() , "invariant");
965 assert(Universe::verify_in_progress() || DumpSharedSpaces ||
966 ((JavaThread *)self)->thread_state() != _thread_blocked, "invariant");
967
968 ObjectMonitor* monitor = NULL;
969 markWord temp, test;
970 intptr_t hash;
971 markWord mark = read_stable_mark(obj);
972
973 // object should remain ineligible for biased locking
974 assert(!mark.has_bias_pattern(), "invariant");
975
976 if (mark.is_neutral()) { // if this is a normal header
977 hash = mark.hash();
978 if (hash != 0) { // if it has a hash, just return it
979 return hash;
980 }
981 hash = get_next_hash(self, obj); // get a new hash
982 temp = mark.copy_set_hash(hash); // merge the hash into header
983 // try to install the hash
984 test = obj->cas_set_mark(temp, mark);
985 if (test == mark) { // if the hash was installed, return it
986 return hash;
987 }
988 // Failed to install the hash. It could be that another thread
989 // installed the hash just before our attempt or inflation has
990 // occurred or... so we fall thru to inflate the monitor for
991 // stability and then install the hash.
992 } else if (mark.has_monitor()) {
993 monitor = mark.monitor();
994 temp = monitor->header();
995 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
996 hash = temp.hash();
997 if (hash != 0) { // if it has a hash, just return it
998 return hash;
999 }
1000 // Fall thru so we only have one place that installs the hash in
1001 // the ObjectMonitor.
1002 } else if (self->is_lock_owned((address)mark.locker())) {
1003 // This is a stack lock owned by the calling thread so fetch the
1004 // displaced markWord from the BasicLock on the stack.
1005 temp = mark.displaced_mark_helper();
1006 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1007 hash = temp.hash();
1008 if (hash != 0) { // if it has a hash, just return it
1009 return hash;
1010 }
1011 // WARNING:
1012 // The displaced header in the BasicLock on a thread's stack
1013 // is strictly immutable. It CANNOT be changed in ANY cases.
1014 // So we have to inflate the stack lock into an ObjectMonitor
1015 // even if the current thread owns the lock. The BasicLock on
1016 // a thread's stack can be asynchronously read by other threads
1017 // during an inflate() call so any change to that stack memory
1018 // may not propagate to other threads correctly.
1019 }
1020
1021 // Inflate the monitor to set the hash.
1022 monitor = inflate(self, obj, inflate_cause_hash_code);
1023 // Load ObjectMonitor's header/dmw field and see if it has a hash.
1024 mark = monitor->header();
1025 assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1026 hash = mark.hash();
1027 if (hash == 0) { // if it does not have a hash
1028 hash = get_next_hash(self, obj); // get a new hash
1029 temp = mark.copy_set_hash(hash); // merge the hash into header
1030 assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1031 uintptr_t v = Atomic::cmpxchg((volatile uintptr_t*)monitor->header_addr(), mark.value(), temp.value());
1032 test = markWord(v);
1033 if (test != mark) {
1034 // The attempt to update the ObjectMonitor's header/dmw field
1035 // did not work. This can happen if another thread managed to
1036 // merge in the hash just before our cmpxchg().
1037 // If we add any new usages of the header/dmw field, this code
1038 // will need to be updated.
1039 hash = test.hash();
1040 assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
1041 assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
1042 }
1043 }
1044 // We finally get the hash.
1045 return hash;
1046 }
1047
1048 // Deprecated -- use FastHashCode() instead.
1049
1050 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
1051 return FastHashCode(Thread::current(), obj());
1052 }
1053
1054
1055 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
1056 Handle h_obj) {
1057 if (UseBiasedLocking) {
1058 BiasedLocking::revoke(h_obj, thread);
1059 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
1060 }
1061
1062 assert(thread == JavaThread::current(), "Can only be called on current thread");
1063 oop obj = h_obj();
1064
1065 markWord mark = read_stable_mark(obj);
1066
1067 // Uncontended case, header points to stack
1068 if (mark.has_locker()) {
1069 return thread->is_lock_owned((address)mark.locker());
1070 }
1071 // Contended case, header points to ObjectMonitor (tagged pointer)
1072 if (mark.has_monitor()) {
1073 ObjectMonitor* monitor = mark.monitor();
1074 return monitor->is_entered(thread) != 0;
1075 }
1076 // Unlocked case, header in place
1077 assert(mark.is_neutral(), "sanity check");
1078 return false;
1079 }
1080
1081 // Be aware of this method could revoke bias of the lock object.
1082 // This method queries the ownership of the lock handle specified by 'h_obj'.
1083 // If the current thread owns the lock, it returns owner_self. If no
1084 // thread owns the lock, it returns owner_none. Otherwise, it will return
1085 // owner_other.
1086 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
1087 (JavaThread *self, Handle h_obj) {
1088 // The caller must beware this method can revoke bias, and
1089 // revocation can result in a safepoint.
1090 assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
1091 assert(self->thread_state() != _thread_blocked, "invariant");
1092
1093 // Possible mark states: neutral, biased, stack-locked, inflated
1094
1095 if (UseBiasedLocking && h_obj()->mark().has_bias_pattern()) {
1096 // CASE: biased
1097 BiasedLocking::revoke(h_obj, self);
1098 assert(!h_obj->mark().has_bias_pattern(),
1099 "biases should be revoked by now");
1100 }
1101
1102 assert(self == JavaThread::current(), "Can only be called on current thread");
1103 oop obj = h_obj();
1104 markWord mark = read_stable_mark(obj);
1105
1106 // CASE: stack-locked. Mark points to a BasicLock on the owner's stack.
1107 if (mark.has_locker()) {
1108 return self->is_lock_owned((address)mark.locker()) ?
1109 owner_self : owner_other;
1110 }
1111
1112 // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor.
1113 // The Object:ObjectMonitor relationship is stable as long as we're
1114 // not at a safepoint.
1115 if (mark.has_monitor()) {
1116 void* owner = mark.monitor()->_owner;
1117 if (owner == NULL) return owner_none;
1118 return (owner == self ||
1119 self->is_lock_owned((address)owner)) ? owner_self : owner_other;
1120 }
1121
1122 // CASE: neutral
1123 assert(mark.is_neutral(), "sanity check");
1124 return owner_none; // it's unlocked
1125 }
1126
1127 // FIXME: jvmti should call this
1128 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
1129 if (UseBiasedLocking) {
1130 if (SafepointSynchronize::is_at_safepoint()) {
1131 BiasedLocking::revoke_at_safepoint(h_obj);
1132 } else {
1133 BiasedLocking::revoke(h_obj, JavaThread::current());
1134 }
1135 assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
1136 }
1137
1138 oop obj = h_obj();
1139 address owner = NULL;
1140
1141 markWord mark = read_stable_mark(obj);
1142
1143 // Uncontended case, header points to stack
1144 if (mark.has_locker()) {
1145 owner = (address) mark.locker();
1146 }
1147
1148 // Contended case, header points to ObjectMonitor (tagged pointer)
1149 else if (mark.has_monitor()) {
1150 ObjectMonitor* monitor = mark.monitor();
1151 assert(monitor != NULL, "monitor should be non-null");
1152 owner = (address) monitor->owner();
1153 }
1154
1155 if (owner != NULL) {
1156 // owning_thread_from_monitor_owner() may also return NULL here
1157 return Threads::owning_thread_from_monitor_owner(t_list, owner);
1158 }
1159
1160 // Unlocked case, header in place
1161 // Cannot have assertion since this object may have been
1162 // locked by another thread when reaching here.
1163 // assert(mark.is_neutral(), "sanity check");
1164
1165 return NULL;
1166 }
1167
1168 // Visitors ...
1169
1170 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
1171 PaddedObjectMonitor* block = Atomic::load(&g_block_list);
1172 while (block != NULL) {
1173 assert(block->object() == CHAINMARKER, "must be a block header");
1174 for (int i = _BLOCKSIZE - 1; i > 0; i--) {
1175 ObjectMonitor* mid = (ObjectMonitor *)(block + i);
1176 oop object = (oop)mid->object();
1177 if (object != NULL) {
1178 // Only process with closure if the object is set.
1179 closure->do_monitor(mid);
1180 }
1181 }
1182 // unmarked_next() is not needed with g_block_list (no locking
1183 // used with block linkage _next_om fields).
1184 block = (PaddedObjectMonitor*)block->next_om();
1185 }
1186 }
1187
1188 static bool monitors_used_above_threshold() {
1189 int population = Atomic::load(&om_list_globals._population);
1190 if (population == 0) {
1191 return false;
1192 }
1193 if (MonitorUsedDeflationThreshold > 0) {
1194 int monitors_used = population - Atomic::load(&om_list_globals._free_count);
1195 int monitor_usage = (monitors_used * 100LL) / population;
1196 return monitor_usage > MonitorUsedDeflationThreshold;
1197 }
1198 return false;
1199 }
1200
1201 // Returns true if MonitorBound is set (> 0) and if the specified
1202 // cnt is > MonitorBound. Otherwise returns false.
1203 static bool is_MonitorBound_exceeded(const int cnt) {
1204 const int mx = MonitorBound;
1205 return mx > 0 && cnt > mx;
1206 }
1207
1208 bool ObjectSynchronizer::is_cleanup_needed() {
1209 if (monitors_used_above_threshold()) {
1210 // Too many monitors in use.
1211 return true;
1212 }
1213 return needs_monitor_scavenge();
1214 }
1215
1216 bool ObjectSynchronizer::needs_monitor_scavenge() {
1217 if (Atomic::load(&_forceMonitorScavenge) == 1) {
1218 log_info(monitorinflation)("Monitor scavenge needed, triggering safepoint cleanup.");
1219 return true;
1220 }
1221 return false;
1222 }
1223
1224 void ObjectSynchronizer::oops_do(OopClosure* f) {
1225 // We only scan the global used list here (for moribund threads), and
1226 // the thread-local monitors in Thread::oops_do().
1227 global_used_oops_do(f);
1228 }
1229
1230 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) {
1231 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1232 list_oops_do(Atomic::load(&om_list_globals._in_use_list), f);
1233 }
1234
1235 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) {
1236 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1237 list_oops_do(thread->om_in_use_list, f);
1238 }
1239
1240 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, OopClosure* f) {
1241 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1242 // The oops_do() phase does not overlap with monitor deflation
1243 // so no need to lock ObjectMonitors for the list traversal.
1244 for (ObjectMonitor* mid = list; mid != NULL; mid = unmarked_next(mid)) {
1245 if (mid->object() != NULL) {
1246 f->do_oop((oop*)mid->object_addr());
1247 }
1248 }
1249 }
1250
1251
1252 // -----------------------------------------------------------------------------
1253 // ObjectMonitor Lifecycle
1254 // -----------------------
1255 // Inflation unlinks monitors from om_list_globals._free_list or a per-thread
1256 // free list and associates them with objects. Deflation -- which occurs at
1257 // STW-time -- disassociates idle monitors from objects.
1258 // Such scavenged monitors are returned to the om_list_globals._free_list.
1259 //
1260 // ObjectMonitors reside in type-stable memory (TSM) and are immortal.
1261 //
1262 // Lifecycle:
1263 // -- unassigned and on the om_list_globals._free_list
1264 // -- unassigned and on a per-thread free list
1265 // -- assigned to an object. The object is inflated and the mark refers
1266 // to the ObjectMonitor.
1267
1268
1269 // Constraining monitor pool growth via MonitorBound ...
1270 //
1271 // If MonitorBound is not set (<= 0), MonitorBound checks are disabled.
1272 //
1273 // The monitor pool is grow-only. We scavenge at STW safepoint-time, but the
1274 // the rate of scavenging is driven primarily by GC. As such, we can find
1275 // an inordinate number of monitors in circulation.
1276 // To avoid that scenario we can artificially induce a STW safepoint
1277 // if the pool appears to be growing past some reasonable bound.
1278 // Generally we favor time in space-time tradeoffs, but as there's no
1279 // natural back-pressure on the # of extant monitors we need to impose some
1280 // type of limit. Beware that if MonitorBound is set to too low a value
1281 // we could just loop. In addition, if MonitorBound is set to a low value
1282 // we'll incur more safepoints, which are harmful to performance.
1283 // See also: GuaranteedSafepointInterval
1284 //
1285 // If MonitorBound is set, the boundry applies to
1286 // (om_list_globals._population - om_list_globals._free_count)
1287 // i.e., if there are not enough ObjectMonitors on the global free list,
1288 // then a safepoint deflation is induced. Picking a good MonitorBound value
1289 // is non-trivial.
1290
1291 static void InduceScavenge(Thread* self, const char * Whence) {
1292 // Induce STW safepoint to trim monitors
1293 // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
1294 // More precisely, trigger a cleanup safepoint as the number
1295 // of active monitors passes the specified threshold.
1296 // TODO: assert thread state is reasonable
1297
1298 if (Atomic::xchg(&_forceMonitorScavenge, 1) == 0) {
1299 VMThread::check_for_forced_cleanup();
1300 }
1301 }
1302
1303 ObjectMonitor* ObjectSynchronizer::om_alloc(Thread* self) {
1304 // A large MAXPRIVATE value reduces both list lock contention
1305 // and list coherency traffic, but also tends to increase the
1306 // number of ObjectMonitors in circulation as well as the STW
1307 // scavenge costs. As usual, we lean toward time in space-time
1308 // tradeoffs.
1309 const int MAXPRIVATE = 1024;
1310 NoSafepointVerifier nsv;
1311
1312 stringStream ss;
1313 for (;;) {
1314 ObjectMonitor* m;
1315
1316 // 1: try to allocate from the thread's local om_free_list.
1317 // Threads will attempt to allocate first from their local list, then
1318 // from the global list, and only after those attempts fail will the
1319 // thread attempt to instantiate new monitors. Thread-local free lists
1320 // improve allocation latency, as well as reducing coherency traffic
1321 // on the shared global list.
1322 m = take_from_start_of_om_free_list(self);
1323 if (m != NULL) {
1324 guarantee(m->object() == NULL, "invariant");
1325 prepend_to_om_in_use_list(self, m);
1326 return m;
1327 }
1328
1329 // 2: try to allocate from the global om_list_globals._free_list
1330 // If we're using thread-local free lists then try
1331 // to reprovision the caller's free list.
1332 if (Atomic::load(&om_list_globals._free_list) != NULL) {
1333 // Reprovision the thread's om_free_list.
1334 // Use bulk transfers to reduce the allocation rate and heat
1335 // on various locks.
1336 for (int i = self->om_free_provision; --i >= 0;) {
1337 ObjectMonitor* take = take_from_start_of_global_free_list();
1338 if (take == NULL) {
1339 break; // No more are available.
1340 }
1341 guarantee(take->object() == NULL, "invariant");
1342 take->Recycle();
1343 om_release(self, take, false);
1344 }
1345 self->om_free_provision += 1 + (self->om_free_provision / 2);
1346 if (self->om_free_provision > MAXPRIVATE) self->om_free_provision = MAXPRIVATE;
1347
1348 if (is_MonitorBound_exceeded(Atomic::load(&om_list_globals._population) -
1349 Atomic::load(&om_list_globals._free_count))) {
1350 // Not enough ObjectMonitors on the global free list.
1351 // We can't safely induce a STW safepoint from om_alloc() as our thread
1352 // state may not be appropriate for such activities and callers may hold
1353 // naked oops, so instead we defer the action.
1354 InduceScavenge(self, "om_alloc");
1355 }
1356 continue;
1357 }
1358
1359 // 3: allocate a block of new ObjectMonitors
1360 // Both the local and global free lists are empty -- resort to malloc().
1361 // In the current implementation ObjectMonitors are TSM - immortal.
1362 // Ideally, we'd write "new ObjectMonitor[_BLOCKSIZE], but we want
1363 // each ObjectMonitor to start at the beginning of a cache line,
1364 // so we use align_up().
1365 // A better solution would be to use C++ placement-new.
1366 // BEWARE: As it stands currently, we don't run the ctors!
1367 assert(_BLOCKSIZE > 1, "invariant");
1368 size_t neededsize = sizeof(PaddedObjectMonitor) * _BLOCKSIZE;
1369 PaddedObjectMonitor* temp;
1370 size_t aligned_size = neededsize + (OM_CACHE_LINE_SIZE - 1);
1371 void* real_malloc_addr = NEW_C_HEAP_ARRAY(char, aligned_size, mtInternal);
1372 temp = (PaddedObjectMonitor*)align_up(real_malloc_addr, OM_CACHE_LINE_SIZE);
1373 (void)memset((void *) temp, 0, neededsize);
1374
1375 // Format the block.
1376 // initialize the linked list, each monitor points to its next
1377 // forming the single linked free list, the very first monitor
1378 // will points to next block, which forms the block list.
1379 // The trick of using the 1st element in the block as g_block_list
1380 // linkage should be reconsidered. A better implementation would
1381 // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
1382
1383 for (int i = 1; i < _BLOCKSIZE; i++) {
1384 temp[i].set_next_om((ObjectMonitor*)&temp[i + 1]);
1385 }
1386
1387 // terminate the last monitor as the end of list
1388 temp[_BLOCKSIZE - 1].set_next_om((ObjectMonitor*)NULL);
1389
1390 // Element [0] is reserved for global list linkage
1391 temp[0].set_object(CHAINMARKER);
1392
1393 // Consider carving out this thread's current request from the
1394 // block in hand. This avoids some lock traffic and redundant
1395 // list activity.
1396
1397 prepend_block_to_lists(temp);
1398 }
1399 }
1400
1401 // Place "m" on the caller's private per-thread om_free_list.
1402 // In practice there's no need to clamp or limit the number of
1403 // monitors on a thread's om_free_list as the only non-allocation time
1404 // we'll call om_release() is to return a monitor to the free list after
1405 // a CAS attempt failed. This doesn't allow unbounded #s of monitors to
1406 // accumulate on a thread's free list.
1407 //
1408 // Key constraint: all ObjectMonitors on a thread's free list and the global
1409 // free list must have their object field set to null. This prevents the
1410 // scavenger -- deflate_monitor_list() -- from reclaiming them while we
1411 // are trying to release them.
1412
1413 void ObjectSynchronizer::om_release(Thread* self, ObjectMonitor* m,
1414 bool from_per_thread_alloc) {
1415 guarantee(m->header().value() == 0, "invariant");
1416 guarantee(m->object() == NULL, "invariant");
1417 NoSafepointVerifier nsv;
1418
1419 stringStream ss;
1420 guarantee((m->is_busy() | m->_recursions) == 0, "freeing in-use monitor: "
1421 "%s, recursions=" INTX_FORMAT, m->is_busy_to_string(&ss),
1422 m->_recursions);
1423 // _next_om is used for both per-thread in-use and free lists so
1424 // we have to remove 'm' from the in-use list first (as needed).
1425 if (from_per_thread_alloc) {
1426 // Need to remove 'm' from om_in_use_list.
1427 ObjectMonitor* mid = NULL;
1428 ObjectMonitor* next = NULL;
1429
1430 // This list walk can only race with another list walker since
1431 // deflation can only happen at a safepoint so we don't have to
1432 // worry about an ObjectMonitor being removed from this list
1433 // while we are walking it.
1434
1435 // Lock the list head to avoid racing with another list walker.
1436 if ((mid = get_list_head_locked(&self->om_in_use_list)) == NULL) {
1437 fatal("thread=" INTPTR_FORMAT " in-use list must not be empty.", p2i(self));
1438 }
1439 next = unmarked_next(mid);
1440 if (m == mid) {
1441 // First special case:
1442 // 'm' matches mid, is the list head and is locked. Switch the list
1443 // head to next which unlocks the list head, but leaves the extracted
1444 // mid locked:
1445 Atomic::store(&self->om_in_use_list, next);
1446 } else if (m == next) {
1447 // Second special case:
1448 // 'm' matches next after the list head and we already have the list
1449 // head locked so set mid to what we are extracting:
1450 mid = next;
1451 // Lock mid to prevent races with a list walker:
1452 om_lock(mid);
1453 // Update next to what follows mid (if anything):
1454 next = unmarked_next(mid);
1455 // Switch next after the list head to new next which unlocks the
1456 // list head, but leaves the extracted mid locked:
1457 self->om_in_use_list->set_next_om(next);
1458 } else {
1459 // We have to search the list to find 'm'.
1460 om_unlock(mid); // unlock the list head
1461 guarantee(next != NULL, "thread=" INTPTR_FORMAT ": om_in_use_list=" INTPTR_FORMAT
1462 " is too short.", p2i(self), p2i(self->om_in_use_list));
1463 // Our starting anchor is next after the list head which is the
1464 // last ObjectMonitor we checked:
1465 ObjectMonitor* anchor = next;
1466 while ((mid = unmarked_next(anchor)) != NULL) {
1467 if (m == mid) {
1468 // We found 'm' on the per-thread in-use list so extract it.
1469 om_lock(anchor); // Lock the anchor so we can safely modify it.
1470 // Update next to what follows mid (if anything):
1471 next = unmarked_next(mid);
1472 // Switch next after the anchor to new next which unlocks the
1473 // anchor, but leaves the extracted mid locked:
1474 anchor->set_next_om(next);
1475 break;
1476 } else {
1477 anchor = mid;
1478 }
1479 }
1480 }
1481
1482 if (mid == NULL) {
1483 // Reached end of the list and didn't find 'm' so:
1484 fatal("thread=" INTPTR_FORMAT " must find m=" INTPTR_FORMAT "on om_in_use_list="
1485 INTPTR_FORMAT, p2i(self), p2i(m), p2i(self->om_in_use_list));
1486 }
1487
1488 // At this point mid is disconnected from the in-use list so
1489 // its lock no longer has any effects on the in-use list.
1490 Atomic::dec(&self->om_in_use_count);
1491 // Unlock mid, but leave the next value for any lagging list
1492 // walkers. It will get cleaned up when mid is prepended to
1493 // the thread's free list:
1494 om_unlock(mid);
1495 }
1496
1497 prepend_to_om_free_list(self, m);
1498 }
1499
1500 // Return ObjectMonitors on a moribund thread's free and in-use
1501 // lists to the appropriate global lists. The ObjectMonitors on the
1502 // per-thread in-use list may still be in use by other threads.
1503 //
1504 // We currently call om_flush() from Threads::remove() before the
1505 // thread has been excised from the thread list and is no longer a
1506 // mutator. This means that om_flush() cannot run concurrently with
1507 // a safepoint and interleave with deflate_idle_monitors(). In
1508 // particular, this ensures that the thread's in-use monitors are
1509 // scanned by a GC safepoint, either via Thread::oops_do() (before
1510 // om_flush() is called) or via ObjectSynchronizer::oops_do() (after
1511 // om_flush() is called).
1512
1513 void ObjectSynchronizer::om_flush(Thread* self) {
1514 // Process the per-thread in-use list first to be consistent.
1515 int in_use_count = 0;
1516 ObjectMonitor* in_use_list = NULL;
1517 ObjectMonitor* in_use_tail = NULL;
1518 NoSafepointVerifier nsv;
1519
1520 // This function can race with a list walker thread so we lock the
1521 // list head to prevent confusion.
1522 if ((in_use_list = get_list_head_locked(&self->om_in_use_list)) != NULL) {
1523 // At this point, we have locked the in-use list head so a racing
1524 // thread cannot come in after us. However, a racing thread could
1525 // be ahead of us; we'll detect that and delay to let it finish.
1526 //
1527 // The thread is going away, however the ObjectMonitors on the
1528 // om_in_use_list may still be in-use by other threads. Link
1529 // them to in_use_tail, which will be linked into the global
1530 // in-use list (om_list_globals._in_use_list) below.
1531 //
1532 // Account for the in-use list head before the loop since it is
1533 // already locked (by this thread):
1534 in_use_tail = in_use_list;
1535 in_use_count++;
1536 for (ObjectMonitor* cur_om = unmarked_next(in_use_list); cur_om != NULL; cur_om = unmarked_next(cur_om)) {
1537 if (is_locked(cur_om)) {
1538 // cur_om is locked so there must be a racing walker thread ahead
1539 // of us so we'll give it a chance to finish.
1540 while (is_locked(cur_om)) {
1541 os::naked_short_sleep(1);
1542 }
1543 }
1544 in_use_tail = cur_om;
1545 in_use_count++;
1546 }
1547 guarantee(in_use_tail != NULL, "invariant");
1548 int l_om_in_use_count = Atomic::load(&self->om_in_use_count);
1549 assert(l_om_in_use_count == in_use_count, "in-use counts don't match: "
1550 "l_om_in_use_count=%d, in_use_count=%d", l_om_in_use_count, in_use_count);
1551 Atomic::store(&self->om_in_use_count, 0);
1552 // Clear the in-use list head (which also unlocks it):
1553 Atomic::store(&self->om_in_use_list, (ObjectMonitor*)NULL);
1554 om_unlock(in_use_list);
1555 }
1556
1557 int free_count = 0;
1558 ObjectMonitor* free_list = NULL;
1559 ObjectMonitor* free_tail = NULL;
1560 // This function can race with a list walker thread so we lock the
1561 // list head to prevent confusion.
1562 if ((free_list = get_list_head_locked(&self->om_free_list)) != NULL) {
1563 // At this point, we have locked the free list head so a racing
1564 // thread cannot come in after us. However, a racing thread could
1565 // be ahead of us; we'll detect that and delay to let it finish.
1566 //
1567 // The thread is going away. Set 'free_tail' to the last per-thread free
1568 // monitor which will be linked to om_list_globals._free_list below.
1569 //
1570 // Account for the free list head before the loop since it is
1571 // already locked (by this thread):
1572 free_tail = free_list;
1573 free_count++;
1574 for (ObjectMonitor* s = unmarked_next(free_list); s != NULL; s = unmarked_next(s)) {
1575 if (is_locked(s)) {
1576 // s is locked so there must be a racing walker thread ahead
1577 // of us so we'll give it a chance to finish.
1578 while (is_locked(s)) {
1579 os::naked_short_sleep(1);
1580 }
1581 }
1582 free_tail = s;
1583 free_count++;
1584 guarantee(s->object() == NULL, "invariant");
1585 stringStream ss;
1586 guarantee(!s->is_busy(), "must be !is_busy: %s", s->is_busy_to_string(&ss));
1587 }
1588 guarantee(free_tail != NULL, "invariant");
1589 int l_om_free_count = Atomic::load(&self->om_free_count);
1590 assert(l_om_free_count == free_count, "free counts don't match: "
1591 "l_om_free_count=%d, free_count=%d", l_om_free_count, free_count);
1592 Atomic::store(&self->om_free_count, 0);
1593 Atomic::store(&self->om_free_list, (ObjectMonitor*)NULL);
1594 om_unlock(free_list);
1595 }
1596
1597 if (free_tail != NULL) {
1598 prepend_list_to_global_free_list(free_list, free_tail, free_count);
1599 }
1600
1601 if (in_use_tail != NULL) {
1602 prepend_list_to_global_in_use_list(in_use_list, in_use_tail, in_use_count);
1603 }
1604
1605 LogStreamHandle(Debug, monitorinflation) lsh_debug;
1606 LogStreamHandle(Info, monitorinflation) lsh_info;
1607 LogStream* ls = NULL;
1608 if (log_is_enabled(Debug, monitorinflation)) {
1609 ls = &lsh_debug;
1610 } else if ((free_count != 0 || in_use_count != 0) &&
1611 log_is_enabled(Info, monitorinflation)) {
1612 ls = &lsh_info;
1613 }
1614 if (ls != NULL) {
1615 ls->print_cr("om_flush: jt=" INTPTR_FORMAT ", free_count=%d"
1616 ", in_use_count=%d" ", om_free_provision=%d",
1617 p2i(self), free_count, in_use_count, self->om_free_provision);
1618 }
1619 }
1620
1621 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1622 const oop obj,
1623 ObjectSynchronizer::InflateCause cause) {
1624 assert(event != NULL, "invariant");
1625 assert(event->should_commit(), "invariant");
1626 event->set_monitorClass(obj->klass());
1627 event->set_address((uintptr_t)(void*)obj);
1628 event->set_cause((u1)cause);
1629 event->commit();
1630 }
1631
1632 // Fast path code shared by multiple functions
1633 void ObjectSynchronizer::inflate_helper(oop obj) {
1634 markWord mark = obj->mark();
1635 if (mark.has_monitor()) {
1636 assert(ObjectSynchronizer::verify_objmon_isinpool(mark.monitor()), "monitor is invalid");
1637 assert(mark.monitor()->header().is_neutral(), "monitor must record a good object header");
1638 return;
1639 }
1640 inflate(Thread::current(), obj, inflate_cause_vm_internal);
1641 }
1642
1643 ObjectMonitor* ObjectSynchronizer::inflate(Thread* self,
1644 oop object, const InflateCause cause) {
1645 // Inflate mutates the heap ...
1646 // Relaxing assertion for bug 6320749.
1647 assert(Universe::verify_in_progress() ||
1648 !SafepointSynchronize::is_at_safepoint(), "invariant");
1649
1650 EventJavaMonitorInflate event;
1651
1652 for (;;) {
1653 const markWord mark = object->mark();
1654 assert(!mark.has_bias_pattern(), "invariant");
1655
1656 // The mark can be in one of the following states:
1657 // * Inflated - just return
1658 // * Stack-locked - coerce it to inflated
1659 // * INFLATING - busy wait for conversion to complete
1660 // * Neutral - aggressively inflate the object.
1661 // * BIASED - Illegal. We should never see this
1662
1663 // CASE: inflated
1664 if (mark.has_monitor()) {
1665 ObjectMonitor* inf = mark.monitor();
1666 markWord dmw = inf->header();
1667 assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1668 assert(inf->object() == object, "invariant");
1669 assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1670 return inf;
1671 }
1672
1673 // CASE: inflation in progress - inflating over a stack-lock.
1674 // Some other thread is converting from stack-locked to inflated.
1675 // Only that thread can complete inflation -- other threads must wait.
1676 // The INFLATING value is transient.
1677 // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1678 // We could always eliminate polling by parking the thread on some auxiliary list.
1679 if (mark == markWord::INFLATING()) {
1680 read_stable_mark(object);
1681 continue;
1682 }
1683
1684 // CASE: stack-locked
1685 // Could be stack-locked either by this thread or by some other thread.
1686 //
1687 // Note that we allocate the objectmonitor speculatively, _before_ attempting
1688 // to install INFLATING into the mark word. We originally installed INFLATING,
1689 // allocated the objectmonitor, and then finally STed the address of the
1690 // objectmonitor into the mark. This was correct, but artificially lengthened
1691 // the interval in which INFLATED appeared in the mark, thus increasing
1692 // the odds of inflation contention.
1693 //
1694 // We now use per-thread private objectmonitor free lists.
1695 // These list are reprovisioned from the global free list outside the
1696 // critical INFLATING...ST interval. A thread can transfer
1697 // multiple objectmonitors en-mass from the global free list to its local free list.
1698 // This reduces coherency traffic and lock contention on the global free list.
1699 // Using such local free lists, it doesn't matter if the om_alloc() call appears
1700 // before or after the CAS(INFLATING) operation.
1701 // See the comments in om_alloc().
1702
1703 LogStreamHandle(Trace, monitorinflation) lsh;
1704
1705 if (mark.has_locker()) {
1706 ObjectMonitor* m = om_alloc(self);
1707 // Optimistically prepare the objectmonitor - anticipate successful CAS
1708 // We do this before the CAS in order to minimize the length of time
1709 // in which INFLATING appears in the mark.
1710 m->Recycle();
1711 m->_Responsible = NULL;
1712 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // Consider: maintain by type/class
1713
1714 markWord cmp = object->cas_set_mark(markWord::INFLATING(), mark);
1715 if (cmp != mark) {
1716 om_release(self, m, true);
1717 continue; // Interference -- just retry
1718 }
1719
1720 // We've successfully installed INFLATING (0) into the mark-word.
1721 // This is the only case where 0 will appear in a mark-word.
1722 // Only the singular thread that successfully swings the mark-word
1723 // to 0 can perform (or more precisely, complete) inflation.
1724 //
1725 // Why do we CAS a 0 into the mark-word instead of just CASing the
1726 // mark-word from the stack-locked value directly to the new inflated state?
1727 // Consider what happens when a thread unlocks a stack-locked object.
1728 // It attempts to use CAS to swing the displaced header value from the
1729 // on-stack BasicLock back into the object header. Recall also that the
1730 // header value (hash code, etc) can reside in (a) the object header, or
1731 // (b) a displaced header associated with the stack-lock, or (c) a displaced
1732 // header in an ObjectMonitor. The inflate() routine must copy the header
1733 // value from the BasicLock on the owner's stack to the ObjectMonitor, all
1734 // the while preserving the hashCode stability invariants. If the owner
1735 // decides to release the lock while the value is 0, the unlock will fail
1736 // and control will eventually pass from slow_exit() to inflate. The owner
1737 // will then spin, waiting for the 0 value to disappear. Put another way,
1738 // the 0 causes the owner to stall if the owner happens to try to
1739 // drop the lock (restoring the header from the BasicLock to the object)
1740 // while inflation is in-progress. This protocol avoids races that might
1741 // would otherwise permit hashCode values to change or "flicker" for an object.
1742 // Critically, while object->mark is 0 mark.displaced_mark_helper() is stable.
1743 // 0 serves as a "BUSY" inflate-in-progress indicator.
1744
1745
1746 // fetch the displaced mark from the owner's stack.
1747 // The owner can't die or unwind past the lock while our INFLATING
1748 // object is in the mark. Furthermore the owner can't complete
1749 // an unlock on the object, either.
1750 markWord dmw = mark.displaced_mark_helper();
1751 // Catch if the object's header is not neutral (not locked and
1752 // not marked is what we care about here).
1753 assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1754
1755 // Setup monitor fields to proper values -- prepare the monitor
1756 m->set_header(dmw);
1757
1758 // Optimization: if the mark.locker stack address is associated
1759 // with this thread we could simply set m->_owner = self.
1760 // Note that a thread can inflate an object
1761 // that it has stack-locked -- as might happen in wait() -- directly
1762 // with CAS. That is, we can avoid the xchg-NULL .... ST idiom.
1763 m->set_owner_from(NULL, mark.locker());
1764 m->set_object(object);
1765 // TODO-FIXME: assert BasicLock->dhw != 0.
1766
1767 // Must preserve store ordering. The monitor state must
1768 // be stable at the time of publishing the monitor address.
1769 guarantee(object->mark() == markWord::INFLATING(), "invariant");
1770 object->release_set_mark(markWord::encode(m));
1771
1772 // Hopefully the performance counters are allocated on distinct cache lines
1773 // to avoid false sharing on MP systems ...
1774 OM_PERFDATA_OP(Inflations, inc());
1775 if (log_is_enabled(Trace, monitorinflation)) {
1776 ResourceMark rm(self);
1777 lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1778 INTPTR_FORMAT ", type='%s'", p2i(object),
1779 object->mark().value(), object->klass()->external_name());
1780 }
1781 if (event.should_commit()) {
1782 post_monitor_inflate_event(&event, object, cause);
1783 }
1784 return m;
1785 }
1786
1787 // CASE: neutral
1788 // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1789 // If we know we're inflating for entry it's better to inflate by swinging a
1790 // pre-locked ObjectMonitor pointer into the object header. A successful
1791 // CAS inflates the object *and* confers ownership to the inflating thread.
1792 // In the current implementation we use a 2-step mechanism where we CAS()
1793 // to inflate and then CAS() again to try to swing _owner from NULL to self.
1794 // An inflateTry() method that we could call from enter() would be useful.
1795
1796 // Catch if the object's header is not neutral (not locked and
1797 // not marked is what we care about here).
1798 assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1799 ObjectMonitor* m = om_alloc(self);
1800 // prepare m for installation - set monitor to initial state
1801 m->Recycle();
1802 m->set_header(mark);
1803 m->set_object(object);
1804 m->_Responsible = NULL;
1805 m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // consider: keep metastats by type/class
1806
1807 if (object->cas_set_mark(markWord::encode(m), mark) != mark) {
1808 m->set_header(markWord::zero());
1809 m->set_object(NULL);
1810 m->Recycle();
1811 om_release(self, m, true);
1812 m = NULL;
1813 continue;
1814 // interference - the markword changed - just retry.
1815 // The state-transitions are one-way, so there's no chance of
1816 // live-lock -- "Inflated" is an absorbing state.
1817 }
1818
1819 // Hopefully the performance counters are allocated on distinct
1820 // cache lines to avoid false sharing on MP systems ...
1821 OM_PERFDATA_OP(Inflations, inc());
1822 if (log_is_enabled(Trace, monitorinflation)) {
1823 ResourceMark rm(self);
1824 lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
1825 INTPTR_FORMAT ", type='%s'", p2i(object),
1826 object->mark().value(), object->klass()->external_name());
1827 }
1828 if (event.should_commit()) {
1829 post_monitor_inflate_event(&event, object, cause);
1830 }
1831 return m;
1832 }
1833 }
1834
1835
1836 // We maintain a list of in-use monitors for each thread.
1837 //
1838 // deflate_thread_local_monitors() scans a single thread's in-use list, while
1839 // deflate_idle_monitors() scans only a global list of in-use monitors which
1840 // is populated only as a thread dies (see om_flush()).
1841 //
1842 // These operations are called at all safepoints, immediately after mutators
1843 // are stopped, but before any objects have moved. Collectively they traverse
1844 // the population of in-use monitors, deflating where possible. The scavenged
1845 // monitors are returned to the global monitor free list.
1846 //
1847 // Beware that we scavenge at *every* stop-the-world point. Having a large
1848 // number of monitors in-use could negatively impact performance. We also want
1849 // to minimize the total # of monitors in circulation, as they incur a small
1850 // footprint penalty.
1851 //
1852 // Perversely, the heap size -- and thus the STW safepoint rate --
1853 // typically drives the scavenge rate. Large heaps can mean infrequent GC,
1854 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
1855 // This is an unfortunate aspect of this design.
1856
1857 // Deflate a single monitor if not in-use
1858 // Return true if deflated, false if in-use
1859 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1860 ObjectMonitor** free_head_p,
1861 ObjectMonitor** free_tail_p) {
1862 bool deflated;
1863 // Normal case ... The monitor is associated with obj.
1864 const markWord mark = obj->mark();
1865 guarantee(mark == markWord::encode(mid), "should match: mark="
1866 INTPTR_FORMAT ", encoded mid=" INTPTR_FORMAT, mark.value(),
1867 markWord::encode(mid).value());
1868 // Make sure that mark.monitor() and markWord::encode() agree:
1869 guarantee(mark.monitor() == mid, "should match: monitor()=" INTPTR_FORMAT
1870 ", mid=" INTPTR_FORMAT, p2i(mark.monitor()), p2i(mid));
1871 const markWord dmw = mid->header();
1872 guarantee(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1873
1874 if (mid->is_busy()) {
1875 // Easy checks are first - the ObjectMonitor is busy so no deflation.
1876 deflated = false;
1877 } else {
1878 // Deflate the monitor if it is no longer being used
1879 // It's idle - scavenge and return to the global free list
1880 // plain old deflation ...
1881 if (log_is_enabled(Trace, monitorinflation)) {
1882 ResourceMark rm;
1883 log_trace(monitorinflation)("deflate_monitor: "
1884 "object=" INTPTR_FORMAT ", mark="
1885 INTPTR_FORMAT ", type='%s'", p2i(obj),
1886 mark.value(), obj->klass()->external_name());
1887 }
1888
1889 // Restore the header back to obj
1890 obj->release_set_mark(dmw);
1891 mid->clear();
1892
1893 assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT,
1894 p2i(mid->object()));
1895
1896 // Move the deflated ObjectMonitor to the working free list
1897 // defined by free_head_p and free_tail_p.
1898 if (*free_head_p == NULL) *free_head_p = mid;
1899 if (*free_tail_p != NULL) {
1900 // We append to the list so the caller can use mid->_next_om
1901 // to fix the linkages in its context.
1902 ObjectMonitor* prevtail = *free_tail_p;
1903 // Should have been cleaned up by the caller:
1904 // Note: Should not have to lock prevtail here since we're at a
1905 // safepoint and ObjectMonitors on the local free list should
1906 // not be accessed in parallel.
1907 #ifdef ASSERT
1908 ObjectMonitor* l_next_om = prevtail->next_om();
1909 #endif
1910 assert(l_next_om == NULL, "must be NULL: _next_om=" INTPTR_FORMAT, p2i(l_next_om));
1911 prevtail->set_next_om(mid);
1912 }
1913 *free_tail_p = mid;
1914 // At this point, mid->_next_om still refers to its current
1915 // value and another ObjectMonitor's _next_om field still
1916 // refers to this ObjectMonitor. Those linkages have to be
1917 // cleaned up by the caller who has the complete context.
1918 deflated = true;
1919 }
1920 return deflated;
1921 }
1922
1923 // Walk a given monitor list, and deflate idle monitors.
1924 // The given list could be a per-thread list or a global list.
1925 //
1926 // In the case of parallel processing of thread local monitor lists,
1927 // work is done by Threads::parallel_threads_do() which ensures that
1928 // each Java thread is processed by exactly one worker thread, and
1929 // thus avoid conflicts that would arise when worker threads would
1930 // process the same monitor lists concurrently.
1931 //
1932 // See also ParallelSPCleanupTask and
1933 // SafepointSynchronize::do_cleanup_tasks() in safepoint.cpp and
1934 // Threads::parallel_java_threads_do() in thread.cpp.
1935 int ObjectSynchronizer::deflate_monitor_list(ObjectMonitor** list_p,
1936 int* count_p,
1937 ObjectMonitor** free_head_p,
1938 ObjectMonitor** free_tail_p) {
1939 ObjectMonitor* cur_mid_in_use = NULL;
1940 ObjectMonitor* mid = NULL;
1941 ObjectMonitor* next = NULL;
1942 int deflated_count = 0;
1943
1944 // This list walk executes at a safepoint and does not race with any
1945 // other list walkers.
1946
1947 for (mid = Atomic::load(list_p); mid != NULL; mid = next) {
1948 next = unmarked_next(mid);
1949 oop obj = (oop) mid->object();
1950 if (obj != NULL && deflate_monitor(mid, obj, free_head_p, free_tail_p)) {
1951 // Deflation succeeded and already updated free_head_p and
1952 // free_tail_p as needed. Finish the move to the local free list
1953 // by unlinking mid from the global or per-thread in-use list.
1954 if (cur_mid_in_use == NULL) {
1955 // mid is the list head so switch the list head to next:
1956 Atomic::store(list_p, next);
1957 } else {
1958 // Switch cur_mid_in_use's next field to next:
1959 cur_mid_in_use->set_next_om(next);
1960 }
1961 // At this point mid is disconnected from the in-use list.
1962 deflated_count++;
1963 Atomic::dec(count_p);
1964 // mid is current tail in the free_head_p list so NULL terminate it:
1965 mid->set_next_om(NULL);
1966 } else {
1967 cur_mid_in_use = mid;
1968 }
1969 }
1970 return deflated_count;
1971 }
1972
1973 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) {
1974 counters->n_in_use = 0; // currently associated with objects
1975 counters->n_in_circulation = 0; // extant
1976 counters->n_scavenged = 0; // reclaimed (global and per-thread)
1977 counters->per_thread_scavenged = 0; // per-thread scavenge total
1978 counters->per_thread_times = 0.0; // per-thread scavenge times
1979 }
1980
1981 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) {
1982 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1983 bool deflated = false;
1984
1985 ObjectMonitor* free_head_p = NULL; // Local SLL of scavenged monitors
1986 ObjectMonitor* free_tail_p = NULL;
1987 elapsedTimer timer;
1988
1989 if (log_is_enabled(Info, monitorinflation)) {
1990 timer.start();
1991 }
1992
1993 // Note: the thread-local monitors lists get deflated in
1994 // a separate pass. See deflate_thread_local_monitors().
1995
1996 // For moribund threads, scan om_list_globals._in_use_list
1997 int deflated_count = 0;
1998 if (Atomic::load(&om_list_globals._in_use_list) != NULL) {
1999 // Update n_in_circulation before om_list_globals._in_use_count is
2000 // updated by deflation.
2001 Atomic::add(&counters->n_in_circulation,
2002 Atomic::load(&om_list_globals._in_use_count));
2003
2004 deflated_count = deflate_monitor_list(&om_list_globals._in_use_list,
2005 &om_list_globals._in_use_count,
2006 &free_head_p, &free_tail_p);
2007 Atomic::add(&counters->n_in_use, Atomic::load(&om_list_globals._in_use_count));
2008 }
2009
2010 if (free_head_p != NULL) {
2011 // Move the deflated ObjectMonitors back to the global free list.
2012 guarantee(free_tail_p != NULL && deflated_count > 0, "invariant");
2013 #ifdef ASSERT
2014 ObjectMonitor* l_next_om = free_tail_p->next_om();
2015 #endif
2016 assert(l_next_om == NULL, "must be NULL: _next_om=" INTPTR_FORMAT, p2i(l_next_om));
2017 prepend_list_to_global_free_list(free_head_p, free_tail_p, deflated_count);
2018 Atomic::add(&counters->n_scavenged, deflated_count);
2019 }
2020 timer.stop();
2021
2022 LogStreamHandle(Debug, monitorinflation) lsh_debug;
2023 LogStreamHandle(Info, monitorinflation) lsh_info;
2024 LogStream* ls = NULL;
2025 if (log_is_enabled(Debug, monitorinflation)) {
2026 ls = &lsh_debug;
2027 } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2028 ls = &lsh_info;
2029 }
2030 if (ls != NULL) {
2031 ls->print_cr("deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
2032 }
2033 }
2034
2035 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2036 // Report the cumulative time for deflating each thread's idle
2037 // monitors. Note: if the work is split among more than one
2038 // worker thread, then the reported time will likely be more
2039 // than a beginning to end measurement of the phase.
2040 log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->per_thread_times, counters->per_thread_scavenged);
2041
2042 if (log_is_enabled(Debug, monitorinflation)) {
2043 // exit_globals()'s call to audit_and_print_stats() is done
2044 // at the Info level and not at a safepoint.
2045 ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2046 } else if (log_is_enabled(Info, monitorinflation)) {
2047 log_info(monitorinflation)("global_population=%d, global_in_use_count=%d, "
2048 "global_free_count=%d",
2049 Atomic::load(&om_list_globals._population),
2050 Atomic::load(&om_list_globals._in_use_count),
2051 Atomic::load(&om_list_globals._free_count));
2052 }
2053
2054 Atomic::store(&_forceMonitorScavenge, 0); // Reset
2055
2056 OM_PERFDATA_OP(Deflations, inc(counters->n_scavenged));
2057 OM_PERFDATA_OP(MonExtant, set_value(counters->n_in_circulation));
2058
2059 GVars.stw_random = os::random();
2060 GVars.stw_cycle++;
2061 }
2062
2063 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
2064 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2065
2066 ObjectMonitor* free_head_p = NULL; // Local SLL of scavenged monitors
2067 ObjectMonitor* free_tail_p = NULL;
2068 elapsedTimer timer;
2069
2070 if (log_is_enabled(Info, safepoint, cleanup) ||
2071 log_is_enabled(Info, monitorinflation)) {
2072 timer.start();
2073 }
2074
2075 // Update n_in_circulation before om_in_use_count is updated by deflation.
2076 Atomic::add(&counters->n_in_circulation, Atomic::load(&thread->om_in_use_count));
2077
2078 int deflated_count = deflate_monitor_list(&thread->om_in_use_list, &thread->om_in_use_count, &free_head_p, &free_tail_p);
2079 Atomic::add(&counters->n_in_use, Atomic::load(&thread->om_in_use_count));
2080
2081 if (free_head_p != NULL) {
2082 // Move the deflated ObjectMonitors back to the global free list.
2083 guarantee(free_tail_p != NULL && deflated_count > 0, "invariant");
2084 #ifdef ASSERT
2085 ObjectMonitor* l_next_om = free_tail_p->next_om();
2086 #endif
2087 assert(l_next_om == NULL, "must be NULL: _next_om=" INTPTR_FORMAT, p2i(l_next_om));
2088 prepend_list_to_global_free_list(free_head_p, free_tail_p, deflated_count);
2089 Atomic::add(&counters->n_scavenged, deflated_count);
2090 Atomic::add(&counters->per_thread_scavenged, deflated_count);
2091 }
2092
2093 timer.stop();
2094 counters->per_thread_times += timer.seconds();
2095
2096 LogStreamHandle(Debug, monitorinflation) lsh_debug;
2097 LogStreamHandle(Info, monitorinflation) lsh_info;
2098 LogStream* ls = NULL;
2099 if (log_is_enabled(Debug, monitorinflation)) {
2100 ls = &lsh_debug;
2101 } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2102 ls = &lsh_info;
2103 }
2104 if (ls != NULL) {
2105 ls->print_cr("jt=" INTPTR_FORMAT ": deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(thread), timer.seconds(), deflated_count);
2106 }
2107 }
2108
2109 // Monitor cleanup on JavaThread::exit
2110
2111 // Iterate through monitor cache and attempt to release thread's monitors
2112 // Gives up on a particular monitor if an exception occurs, but continues
2113 // the overall iteration, swallowing the exception.
2114 class ReleaseJavaMonitorsClosure: public MonitorClosure {
2115 private:
2116 TRAPS;
2117
2118 public:
2119 ReleaseJavaMonitorsClosure(Thread* thread) : THREAD(thread) {}
2120 void do_monitor(ObjectMonitor* mid) {
2121 if (mid->owner() == THREAD) {
2122 // Note well -- this occurs ONLY on thread exit, and is a last ditch
2123 // effort to release all locks. Hence, we don't need to record tsan's
2124 // recursion count -- it will never be locked again.
2125 TSAN_RUNTIME_ONLY(SharedRuntime::tsan_oop_rec_unlock(THREAD, (oop)mid->object()));
2126 (void)mid->complete_exit(CHECK);
2127 }
2128 }
2129 };
2130
2131 // Release all inflated monitors owned by THREAD. Lightweight monitors are
2132 // ignored. This is meant to be called during JNI thread detach which assumes
2133 // all remaining monitors are heavyweight. All exceptions are swallowed.
2134 // Scanning the extant monitor list can be time consuming.
2135 // A simple optimization is to add a per-thread flag that indicates a thread
2136 // called jni_monitorenter() during its lifetime.
2137 //
2138 // Instead of No_Savepoint_Verifier it might be cheaper to
2139 // use an idiom of the form:
2140 // auto int tmp = SafepointSynchronize::_safepoint_counter ;
2141 // <code that must not run at safepoint>
2142 // guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
2143 // Since the tests are extremely cheap we could leave them enabled
2144 // for normal product builds.
2145
2146 void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) {
2147 assert(THREAD == JavaThread::current(), "must be current Java thread");
2148 NoSafepointVerifier nsv;
2149 ReleaseJavaMonitorsClosure rjmc(THREAD);
2150 ObjectSynchronizer::monitors_iterate(&rjmc);
2151 THREAD->clear_pending_exception();
2152 }
2153
2154 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) {
2155 switch (cause) {
2156 case inflate_cause_vm_internal: return "VM Internal";
2157 case inflate_cause_monitor_enter: return "Monitor Enter";
2158 case inflate_cause_wait: return "Monitor Wait";
2159 case inflate_cause_notify: return "Monitor Notify";
2160 case inflate_cause_hash_code: return "Monitor Hash Code";
2161 case inflate_cause_jni_enter: return "JNI Monitor Enter";
2162 case inflate_cause_jni_exit: return "JNI Monitor Exit";
2163 default:
2164 ShouldNotReachHere();
2165 }
2166 return "Unknown";
2167 }
2168
2169 //------------------------------------------------------------------------------
2170 // Debugging code
2171
2172 u_char* ObjectSynchronizer::get_gvars_addr() {
2173 return (u_char*)&GVars;
2174 }
2175
2176 u_char* ObjectSynchronizer::get_gvars_hc_sequence_addr() {
2177 return (u_char*)&GVars.hc_sequence;
2178 }
2179
2180 size_t ObjectSynchronizer::get_gvars_size() {
2181 return sizeof(SharedGlobals);
2182 }
2183
2184 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() {
2185 return (u_char*)&GVars.stw_random;
2186 }
2187
2188 // This function can be called at a safepoint or it can be called when
2189 // we are trying to exit the VM. When we are trying to exit the VM, the
2190 // list walker functions can run in parallel with the other list
2191 // operations so spin-locking is used for safety.
2192 //
2193 // Calls to this function can be added in various places as a debugging
2194 // aid; pass 'true' for the 'on_exit' parameter to have in-use monitor
2195 // details logged at the Info level and 'false' for the 'on_exit'
2196 // parameter to have in-use monitor details logged at the Trace level.
2197 // deflate_monitor_list() no longer uses spin-locking so be careful
2198 // when adding audit_and_print_stats() calls at a safepoint.
2199 //
2200 void ObjectSynchronizer::audit_and_print_stats(bool on_exit) {
2201 assert(on_exit || SafepointSynchronize::is_at_safepoint(), "invariant");
2202
2203 LogStreamHandle(Debug, monitorinflation) lsh_debug;
2204 LogStreamHandle(Info, monitorinflation) lsh_info;
2205 LogStreamHandle(Trace, monitorinflation) lsh_trace;
2206 LogStream* ls = NULL;
2207 if (log_is_enabled(Trace, monitorinflation)) {
2208 ls = &lsh_trace;
2209 } else if (log_is_enabled(Debug, monitorinflation)) {
2210 ls = &lsh_debug;
2211 } else if (log_is_enabled(Info, monitorinflation)) {
2212 ls = &lsh_info;
2213 }
2214 assert(ls != NULL, "sanity check");
2215
2216 // Log counts for the global and per-thread monitor lists:
2217 int chk_om_population = log_monitor_list_counts(ls);
2218 int error_cnt = 0;
2219
2220 ls->print_cr("Checking global lists:");
2221
2222 // Check om_list_globals._population:
2223 if (Atomic::load(&om_list_globals._population) == chk_om_population) {
2224 ls->print_cr("global_population=%d equals chk_om_population=%d",
2225 Atomic::load(&om_list_globals._population), chk_om_population);
2226 } else {
2227 // With fine grained locks on the monitor lists, it is possible for
2228 // log_monitor_list_counts() to return a value that doesn't match
2229 // om_list_globals._population. So far a higher value has been
2230 // seen in testing so something is being double counted by
2231 // log_monitor_list_counts().
2232 ls->print_cr("WARNING: global_population=%d is not equal to "
2233 "chk_om_population=%d",
2234 Atomic::load(&om_list_globals._population), chk_om_population);
2235 }
2236
2237 // Check om_list_globals._in_use_list and om_list_globals._in_use_count:
2238 chk_global_in_use_list_and_count(ls, &error_cnt);
2239
2240 // Check om_list_globals._free_list and om_list_globals._free_count:
2241 chk_global_free_list_and_count(ls, &error_cnt);
2242
2243 ls->print_cr("Checking per-thread lists:");
2244
2245 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2246 // Check om_in_use_list and om_in_use_count:
2247 chk_per_thread_in_use_list_and_count(jt, ls, &error_cnt);
2248
2249 // Check om_free_list and om_free_count:
2250 chk_per_thread_free_list_and_count(jt, ls, &error_cnt);
2251 }
2252
2253 if (error_cnt == 0) {
2254 ls->print_cr("No errors found in monitor list checks.");
2255 } else {
2256 log_error(monitorinflation)("found monitor list errors: error_cnt=%d", error_cnt);
2257 }
2258
2259 if ((on_exit && log_is_enabled(Info, monitorinflation)) ||
2260 (!on_exit && log_is_enabled(Trace, monitorinflation))) {
2261 // When exiting this log output is at the Info level. When called
2262 // at a safepoint, this log output is at the Trace level since
2263 // there can be a lot of it.
2264 log_in_use_monitor_details(ls);
2265 }
2266
2267 ls->flush();
2268
2269 guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt);
2270 }
2271
2272 // Check a free monitor entry; log any errors.
2273 void ObjectSynchronizer::chk_free_entry(JavaThread* jt, ObjectMonitor* n,
2274 outputStream * out, int *error_cnt_p) {
2275 stringStream ss;
2276 if (n->is_busy()) {
2277 if (jt != NULL) {
2278 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2279 ": free per-thread monitor must not be busy: %s", p2i(jt),
2280 p2i(n), n->is_busy_to_string(&ss));
2281 } else {
2282 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
2283 "must not be busy: %s", p2i(n), n->is_busy_to_string(&ss));
2284 }
2285 *error_cnt_p = *error_cnt_p + 1;
2286 }
2287 if (n->header().value() != 0) {
2288 if (jt != NULL) {
2289 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2290 ": free per-thread monitor must have NULL _header "
2291 "field: _header=" INTPTR_FORMAT, p2i(jt), p2i(n),
2292 n->header().value());
2293 } else {
2294 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
2295 "must have NULL _header field: _header=" INTPTR_FORMAT,
2296 p2i(n), n->header().value());
2297 }
2298 *error_cnt_p = *error_cnt_p + 1;
2299 }
2300 if (n->object() != NULL) {
2301 if (jt != NULL) {
2302 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2303 ": free per-thread monitor must have NULL _object "
2304 "field: _object=" INTPTR_FORMAT, p2i(jt), p2i(n),
2305 p2i(n->object()));
2306 } else {
2307 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
2308 "must have NULL _object field: _object=" INTPTR_FORMAT,
2309 p2i(n), p2i(n->object()));
2310 }
2311 *error_cnt_p = *error_cnt_p + 1;
2312 }
2313 }
2314
2315 // Lock the next ObjectMonitor for traversal and unlock the current
2316 // ObjectMonitor. Returns the next ObjectMonitor if there is one.
2317 // Otherwise returns NULL (after unlocking the current ObjectMonitor).
2318 // This function is used by the various list walker functions to
2319 // safely walk a list without allowing an ObjectMonitor to be moved
2320 // to another list in the middle of a walk.
2321 static ObjectMonitor* lock_next_for_traversal(ObjectMonitor* cur) {
2322 assert(is_locked(cur), "cur=" INTPTR_FORMAT " must be locked", p2i(cur));
2323 ObjectMonitor* next = unmarked_next(cur);
2324 if (next == NULL) { // Reached the end of the list.
2325 om_unlock(cur);
2326 return NULL;
2327 }
2328 om_lock(next); // Lock next before unlocking current to keep
2329 om_unlock(cur); // from being by-passed by another thread.
2330 return next;
2331 }
2332
2333 // Check the global free list and count; log the results of the checks.
2334 void ObjectSynchronizer::chk_global_free_list_and_count(outputStream * out,
2335 int *error_cnt_p) {
2336 int chk_om_free_count = 0;
2337 ObjectMonitor* cur = NULL;
2338 if ((cur = get_list_head_locked(&om_list_globals._free_list)) != NULL) {
2339 // Marked the global free list head so process the list.
2340 while (true) {
2341 chk_free_entry(NULL /* jt */, cur, out, error_cnt_p);
2342 chk_om_free_count++;
2343
2344 cur = lock_next_for_traversal(cur);
2345 if (cur == NULL) {
2346 break;
2347 }
2348 }
2349 }
2350 int l_free_count = Atomic::load(&om_list_globals._free_count);
2351 if (l_free_count == chk_om_free_count) {
2352 out->print_cr("global_free_count=%d equals chk_om_free_count=%d",
2353 l_free_count, chk_om_free_count);
2354 } else {
2355 // With fine grained locks on om_list_globals._free_list, it
2356 // is possible for an ObjectMonitor to be prepended to
2357 // om_list_globals._free_list after we started calculating
2358 // chk_om_free_count so om_list_globals._free_count may not
2359 // match anymore.
2360 out->print_cr("WARNING: global_free_count=%d is not equal to "
2361 "chk_om_free_count=%d", l_free_count, chk_om_free_count);
2362 }
2363 }
2364
2365 // Check the global in-use list and count; log the results of the checks.
2366 void ObjectSynchronizer::chk_global_in_use_list_and_count(outputStream * out,
2367 int *error_cnt_p) {
2368 int chk_om_in_use_count = 0;
2369 ObjectMonitor* cur = NULL;
2370 if ((cur = get_list_head_locked(&om_list_globals._in_use_list)) != NULL) {
2371 // Marked the global in-use list head so process the list.
2372 while (true) {
2373 chk_in_use_entry(NULL /* jt */, cur, out, error_cnt_p);
2374 chk_om_in_use_count++;
2375
2376 cur = lock_next_for_traversal(cur);
2377 if (cur == NULL) {
2378 break;
2379 }
2380 }
2381 }
2382 int l_in_use_count = Atomic::load(&om_list_globals._in_use_count);
2383 if (l_in_use_count == chk_om_in_use_count) {
2384 out->print_cr("global_in_use_count=%d equals chk_om_in_use_count=%d",
2385 l_in_use_count, chk_om_in_use_count);
2386 } else {
2387 // With fine grained locks on the monitor lists, it is possible for
2388 // an exiting JavaThread to put its in-use ObjectMonitors on the
2389 // global in-use list after chk_om_in_use_count is calculated above.
2390 out->print_cr("WARNING: global_in_use_count=%d is not equal to chk_om_in_use_count=%d",
2391 l_in_use_count, chk_om_in_use_count);
2392 }
2393 }
2394
2395 // Check an in-use monitor entry; log any errors.
2396 void ObjectSynchronizer::chk_in_use_entry(JavaThread* jt, ObjectMonitor* n,
2397 outputStream * out, int *error_cnt_p) {
2398 if (n->header().value() == 0) {
2399 if (jt != NULL) {
2400 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2401 ": in-use per-thread monitor must have non-NULL _header "
2402 "field.", p2i(jt), p2i(n));
2403 } else {
2404 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor "
2405 "must have non-NULL _header field.", p2i(n));
2406 }
2407 *error_cnt_p = *error_cnt_p + 1;
2408 }
2409 if (n->object() == NULL) {
2410 if (jt != NULL) {
2411 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2412 ": in-use per-thread monitor must have non-NULL _object "
2413 "field.", p2i(jt), p2i(n));
2414 } else {
2415 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor "
2416 "must have non-NULL _object field.", p2i(n));
2417 }
2418 *error_cnt_p = *error_cnt_p + 1;
2419 }
2420 const oop obj = (oop)n->object();
2421 const markWord mark = obj->mark();
2422 if (!mark.has_monitor()) {
2423 if (jt != NULL) {
2424 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2425 ": in-use per-thread monitor's object does not think "
2426 "it has a monitor: obj=" INTPTR_FORMAT ", mark="
2427 INTPTR_FORMAT, p2i(jt), p2i(n), p2i(obj), mark.value());
2428 } else {
2429 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
2430 "monitor's object does not think it has a monitor: obj="
2431 INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n),
2432 p2i(obj), mark.value());
2433 }
2434 *error_cnt_p = *error_cnt_p + 1;
2435 }
2436 ObjectMonitor* const obj_mon = mark.monitor();
2437 if (n != obj_mon) {
2438 if (jt != NULL) {
2439 out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2440 ": in-use per-thread monitor's object does not refer "
2441 "to the same monitor: obj=" INTPTR_FORMAT ", mark="
2442 INTPTR_FORMAT ", obj_mon=" INTPTR_FORMAT, p2i(jt),
2443 p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
2444 } else {
2445 out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
2446 "monitor's object does not refer to the same monitor: obj="
2447 INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
2448 INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
2449 }
2450 *error_cnt_p = *error_cnt_p + 1;
2451 }
2452 }
2453
2454 // Check the thread's free list and count; log the results of the checks.
2455 void ObjectSynchronizer::chk_per_thread_free_list_and_count(JavaThread *jt,
2456 outputStream * out,
2457 int *error_cnt_p) {
2458 int chk_om_free_count = 0;
2459 ObjectMonitor* cur = NULL;
2460 if ((cur = get_list_head_locked(&jt->om_free_list)) != NULL) {
2461 // Marked the per-thread free list head so process the list.
2462 while (true) {
2463 chk_free_entry(jt, cur, out, error_cnt_p);
2464 chk_om_free_count++;
2465
2466 cur = lock_next_for_traversal(cur);
2467 if (cur == NULL) {
2468 break;
2469 }
2470 }
2471 }
2472 int l_om_free_count = Atomic::load(&jt->om_free_count);
2473 if (l_om_free_count == chk_om_free_count) {
2474 out->print_cr("jt=" INTPTR_FORMAT ": om_free_count=%d equals "
2475 "chk_om_free_count=%d", p2i(jt), l_om_free_count, chk_om_free_count);
2476 } else {
2477 out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_free_count=%d is not "
2478 "equal to chk_om_free_count=%d", p2i(jt), l_om_free_count,
2479 chk_om_free_count);
2480 *error_cnt_p = *error_cnt_p + 1;
2481 }
2482 }
2483
2484 // Check the thread's in-use list and count; log the results of the checks.
2485 void ObjectSynchronizer::chk_per_thread_in_use_list_and_count(JavaThread *jt,
2486 outputStream * out,
2487 int *error_cnt_p) {
2488 int chk_om_in_use_count = 0;
2489 ObjectMonitor* cur = NULL;
2490 if ((cur = get_list_head_locked(&jt->om_in_use_list)) != NULL) {
2491 // Marked the per-thread in-use list head so process the list.
2492 while (true) {
2493 chk_in_use_entry(jt, cur, out, error_cnt_p);
2494 chk_om_in_use_count++;
2495
2496 cur = lock_next_for_traversal(cur);
2497 if (cur == NULL) {
2498 break;
2499 }
2500 }
2501 }
2502 int l_om_in_use_count = Atomic::load(&jt->om_in_use_count);
2503 if (l_om_in_use_count == chk_om_in_use_count) {
2504 out->print_cr("jt=" INTPTR_FORMAT ": om_in_use_count=%d equals "
2505 "chk_om_in_use_count=%d", p2i(jt), l_om_in_use_count,
2506 chk_om_in_use_count);
2507 } else {
2508 out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_in_use_count=%d is not "
2509 "equal to chk_om_in_use_count=%d", p2i(jt), l_om_in_use_count,
2510 chk_om_in_use_count);
2511 *error_cnt_p = *error_cnt_p + 1;
2512 }
2513 }
2514
2515 // Log details about ObjectMonitors on the in-use lists. The 'BHL'
2516 // flags indicate why the entry is in-use, 'object' and 'object type'
2517 // indicate the associated object and its type.
2518 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out) {
2519 stringStream ss;
2520 if (Atomic::load(&om_list_globals._in_use_count) > 0) {
2521 out->print_cr("In-use global monitor info:");
2522 out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2523 out->print_cr("%18s %s %18s %18s",
2524 "monitor", "BHL", "object", "object type");
2525 out->print_cr("================== === ================== ==================");
2526 ObjectMonitor* cur = NULL;
2527 if ((cur = get_list_head_locked(&om_list_globals._in_use_list)) != NULL) {
2528 // Marked the global in-use list head so process the list.
2529 while (true) {
2530 const oop obj = (oop) cur->object();
2531 const markWord mark = cur->header();
2532 ResourceMark rm;
2533 out->print(INTPTR_FORMAT " %d%d%d " INTPTR_FORMAT " %s", p2i(cur),
2534 cur->is_busy() != 0, mark.hash() != 0, cur->owner() != NULL,
2535 p2i(obj), obj->klass()->external_name());
2536 if (cur->is_busy() != 0) {
2537 out->print(" (%s)", cur->is_busy_to_string(&ss));
2538 ss.reset();
2539 }
2540 out->cr();
2541
2542 cur = lock_next_for_traversal(cur);
2543 if (cur == NULL) {
2544 break;
2545 }
2546 }
2547 }
2548 }
2549
2550 out->print_cr("In-use per-thread monitor info:");
2551 out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2552 out->print_cr("%18s %18s %s %18s %18s",
2553 "jt", "monitor", "BHL", "object", "object type");
2554 out->print_cr("================== ================== === ================== ==================");
2555 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2556 ObjectMonitor* cur = NULL;
2557 if ((cur = get_list_head_locked(&jt->om_in_use_list)) != NULL) {
2558 // Marked the global in-use list head so process the list.
2559 while (true) {
2560 const oop obj = (oop) cur->object();
2561 const markWord mark = cur->header();
2562 ResourceMark rm;
2563 out->print(INTPTR_FORMAT " " INTPTR_FORMAT " %d%d%d " INTPTR_FORMAT
2564 " %s", p2i(jt), p2i(cur), cur->is_busy() != 0,
2565 mark.hash() != 0, cur->owner() != NULL, p2i(obj),
2566 obj->klass()->external_name());
2567 if (cur->is_busy() != 0) {
2568 out->print(" (%s)", cur->is_busy_to_string(&ss));
2569 ss.reset();
2570 }
2571 out->cr();
2572
2573 cur = lock_next_for_traversal(cur);
2574 if (cur == NULL) {
2575 break;
2576 }
2577 }
2578 }
2579 }
2580
2581 out->flush();
2582 }
2583
2584 // Log counts for the global and per-thread monitor lists and return
2585 // the population count.
2586 int ObjectSynchronizer::log_monitor_list_counts(outputStream * out) {
2587 int pop_count = 0;
2588 out->print_cr("%18s %10s %10s %10s",
2589 "Global Lists:", "InUse", "Free", "Total");
2590 out->print_cr("================== ========== ========== ==========");
2591 int l_in_use_count = Atomic::load(&om_list_globals._in_use_count);
2592 int l_free_count = Atomic::load(&om_list_globals._free_count);
2593 out->print_cr("%18s %10d %10d %10d", "", l_in_use_count,
2594 l_free_count, Atomic::load(&om_list_globals._population));
2595 pop_count += l_in_use_count + l_free_count;
2596
2597 out->print_cr("%18s %10s %10s %10s",
2598 "Per-Thread Lists:", "InUse", "Free", "Provision");
2599 out->print_cr("================== ========== ========== ==========");
2600
2601 for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2602 int l_om_in_use_count = Atomic::load(&jt->om_in_use_count);
2603 int l_om_free_count = Atomic::load(&jt->om_free_count);
2604 out->print_cr(INTPTR_FORMAT " %10d %10d %10d", p2i(jt),
2605 l_om_in_use_count, l_om_free_count, jt->om_free_provision);
2606 pop_count += l_om_in_use_count + l_om_free_count;
2607 }
2608 return pop_count;
2609 }
2610
2611 #ifndef PRODUCT
2612
2613 // Check if monitor belongs to the monitor cache
2614 // The list is grow-only so it's *relatively* safe to traverse
2615 // the list of extant blocks without taking a lock.
2616
2617 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) {
2618 PaddedObjectMonitor* block = Atomic::load(&g_block_list);
2619 while (block != NULL) {
2620 assert(block->object() == CHAINMARKER, "must be a block header");
2621 if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) {
2622 address mon = (address)monitor;
2623 address blk = (address)block;
2624 size_t diff = mon - blk;
2625 assert((diff % sizeof(PaddedObjectMonitor)) == 0, "must be aligned");
2626 return 1;
2627 }
2628 // unmarked_next() is not needed with g_block_list (no locking
2629 // used with block linkage _next_om fields).
2630 block = (PaddedObjectMonitor*)block->next_om();
2631 }
2632 return 0;
2633 }
2634
2635 #endif