1 /*
   2  * Copyright (c) 1998, 2024, 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 "jfr/jfrEvents.hpp"
  28 #include "gc/shared/suspendibleThreadSet.hpp"
  29 #include "logging/log.hpp"
  30 #include "logging/logStream.hpp"
  31 #include "memory/allocation.inline.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/globals.hpp"
  40 #include "runtime/handles.inline.hpp"
  41 #include "runtime/handshake.hpp"
  42 #include "runtime/interfaceSupport.inline.hpp"
  43 #include "runtime/lockStack.inline.hpp"
  44 #include "runtime/mutexLocker.hpp"
  45 #include "runtime/objectMonitor.hpp"
  46 #include "runtime/objectMonitor.inline.hpp"
  47 #include "runtime/os.inline.hpp"
  48 #include "runtime/osThread.hpp"
  49 #include "runtime/perfData.hpp"
  50 #include "runtime/safepointMechanism.inline.hpp"
  51 #include "runtime/safepointVerifiers.hpp"
  52 #include "runtime/sharedRuntime.hpp"
  53 #include "runtime/stubRoutines.hpp"
  54 #include "runtime/synchronizer.hpp"
  55 #include "runtime/thread.inline.hpp"
  56 #include "runtime/timer.hpp"
  57 #include "runtime/trimNativeHeap.hpp"
  58 #include "runtime/vframe.hpp"
  59 #include "runtime/vmThread.hpp"
  60 #include "utilities/align.hpp"
  61 #include "utilities/dtrace.hpp"
  62 #include "utilities/events.hpp"
  63 #include "utilities/globalDefinitions.hpp"
  64 #include "utilities/preserveException.hpp"
  65 
  66 void MonitorList::add(ObjectMonitor* m) {
  67   ObjectMonitor* head;
  68   do {
  69     head = Atomic::load(&_head);
  70     m->set_next_om(head);
  71   } while (Atomic::cmpxchg(&_head, head, m) != head);
  72 
  73   size_t count = Atomic::add(&_count, 1u);
  74   if (count > max()) {
  75     Atomic::inc(&_max);
  76   }
  77 }
  78 
  79 size_t MonitorList::count() const {
  80   return Atomic::load(&_count);
  81 }
  82 
  83 size_t MonitorList::max() const {
  84   return Atomic::load(&_max);
  85 }
  86 
  87 // Walk the in-use list and unlink (at most MonitorDeflationMax) deflated
  88 // ObjectMonitors. Returns the number of unlinked ObjectMonitors.
  89 size_t MonitorList::unlink_deflated(Thread* current, LogStream* ls,
  90                                     elapsedTimer* timer_p,
  91                                     GrowableArray<ObjectMonitor*>* unlinked_list) {
  92   size_t unlinked_count = 0;
  93   ObjectMonitor* prev = NULL;
  94   ObjectMonitor* head = Atomic::load_acquire(&_head);
  95   ObjectMonitor* m = head;
  96   // The in-use list head can be NULL during the final audit.
  97   while (m != NULL) {
  98     if (m->is_being_async_deflated()) {
  99       // Find next live ObjectMonitor.
 100       ObjectMonitor* next = m;
 101       do {
 102         ObjectMonitor* next_next = next->next_om();
 103         unlinked_count++;
 104         unlinked_list->append(next);
 105         next = next_next;
 106         if (unlinked_count >= (size_t)MonitorDeflationMax) {
 107           // Reached the max so bail out on the gathering loop.
 108           break;
 109         }
 110       } while (next != NULL && next->is_being_async_deflated());
 111       if (prev == NULL) {
 112         ObjectMonitor* prev_head = Atomic::cmpxchg(&_head, head, next);
 113         if (prev_head != head) {
 114           // Find new prev ObjectMonitor that just got inserted.
 115           for (ObjectMonitor* n = prev_head; n != m; n = n->next_om()) {
 116             prev = n;
 117           }
 118           prev->set_next_om(next);
 119         }
 120       } else {
 121         prev->set_next_om(next);
 122       }
 123       if (unlinked_count >= (size_t)MonitorDeflationMax) {
 124         // Reached the max so bail out on the searching loop.
 125         break;
 126       }
 127       m = next;
 128     } else {
 129       prev = m;
 130       m = m->next_om();
 131     }
 132 
 133     if (current->is_Java_thread()) {
 134       // A JavaThread must check for a safepoint/handshake and honor it.
 135       ObjectSynchronizer::chk_for_block_req(current->as_Java_thread(), "unlinking",
 136                                             "unlinked_count", unlinked_count,
 137                                             ls, timer_p);
 138     }
 139   }
 140   Atomic::sub(&_count, unlinked_count);
 141   return unlinked_count;
 142 }
 143 
 144 MonitorList::Iterator MonitorList::iterator() const {
 145   return Iterator(Atomic::load_acquire(&_head));
 146 }
 147 
 148 ObjectMonitor* MonitorList::Iterator::next() {
 149   ObjectMonitor* current = _current;
 150   _current = current->next_om();
 151   return current;
 152 }
 153 
 154 // The "core" versions of monitor enter and exit reside in this file.
 155 // The interpreter and compilers contain specialized transliterated
 156 // variants of the enter-exit fast-path operations.  See c2_MacroAssembler_x86.cpp
 157 // fast_lock(...) for instance.  If you make changes here, make sure to modify the
 158 // interpreter, and both C1 and C2 fast-path inline locking code emission.
 159 //
 160 // -----------------------------------------------------------------------------
 161 
 162 #ifdef DTRACE_ENABLED
 163 
 164 // Only bother with this argument setup if dtrace is available
 165 // TODO-FIXME: probes should not fire when caller is _blocked.  assert() accordingly.
 166 
 167 #define DTRACE_MONITOR_PROBE_COMMON(obj, thread)                           \
 168   char* bytes = NULL;                                                      \
 169   int len = 0;                                                             \
 170   jlong jtid = SharedRuntime::get_java_tid(thread);                        \
 171   Symbol* klassname = obj->klass()->name();                                \
 172   if (klassname != NULL) {                                                 \
 173     bytes = (char*)klassname->bytes();                                     \
 174     len = klassname->utf8_length();                                        \
 175   }
 176 
 177 #define DTRACE_MONITOR_WAIT_PROBE(monitor, obj, thread, millis)            \
 178   {                                                                        \
 179     if (DTraceMonitorProbes) {                                             \
 180       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 181       HOTSPOT_MONITOR_WAIT(jtid,                                           \
 182                            (uintptr_t)(monitor), bytes, len, (millis));    \
 183     }                                                                      \
 184   }
 185 
 186 #define HOTSPOT_MONITOR_PROBE_notify HOTSPOT_MONITOR_NOTIFY
 187 #define HOTSPOT_MONITOR_PROBE_notifyAll HOTSPOT_MONITOR_NOTIFYALL
 188 #define HOTSPOT_MONITOR_PROBE_waited HOTSPOT_MONITOR_WAITED
 189 
 190 #define DTRACE_MONITOR_PROBE(probe, monitor, obj, thread)                  \
 191   {                                                                        \
 192     if (DTraceMonitorProbes) {                                             \
 193       DTRACE_MONITOR_PROBE_COMMON(obj, thread);                            \
 194       HOTSPOT_MONITOR_PROBE_##probe(jtid, /* probe = waited */             \
 195                                     (uintptr_t)(monitor), bytes, len);     \
 196     }                                                                      \
 197   }
 198 
 199 #else //  ndef DTRACE_ENABLED
 200 
 201 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
 202 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
 203 
 204 #endif // ndef DTRACE_ENABLED
 205 
 206 // This exists only as a workaround of dtrace bug 6254741
 207 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
 208   DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
 209   return 0;
 210 }
 211 
 212 static const int NINFLATIONLOCKS = 256;
 213 static os::PlatformMutex* gInflationLocks[NINFLATIONLOCKS];
 214 
 215 void ObjectSynchronizer::initialize() {
 216   for (int i = 0; i < NINFLATIONLOCKS; i++) {
 217     gInflationLocks[i] = new os::PlatformMutex();
 218   }
 219   // Start the ceiling with the estimate for one thread.
 220   set_in_use_list_ceiling(AvgMonitorsPerThreadEstimate);
 221 
 222   // Start the timer for deflations, so it does not trigger immediately.
 223   _last_async_deflation_time_ns = os::javaTimeNanos();
 224 }
 225 
 226 MonitorList ObjectSynchronizer::_in_use_list;
 227 // monitors_used_above_threshold() policy is as follows:
 228 //
 229 // The ratio of the current _in_use_list count to the ceiling is used
 230 // to determine if we are above MonitorUsedDeflationThreshold and need
 231 // to do an async monitor deflation cycle. The ceiling is increased by
 232 // AvgMonitorsPerThreadEstimate when a thread is added to the system
 233 // and is decreased by AvgMonitorsPerThreadEstimate when a thread is
 234 // removed from the system.
 235 //
 236 // Note: If the _in_use_list max exceeds the ceiling, then
 237 // monitors_used_above_threshold() will use the in_use_list max instead
 238 // of the thread count derived ceiling because we have used more
 239 // ObjectMonitors than the estimated average.
 240 //
 241 // Note: If deflate_idle_monitors() has NoAsyncDeflationProgressMax
 242 // no-progress async monitor deflation cycles in a row, then the ceiling
 243 // is adjusted upwards by monitors_used_above_threshold().
 244 //
 245 // Start the ceiling with the estimate for one thread in initialize()
 246 // which is called after cmd line options are processed.
 247 static size_t _in_use_list_ceiling = 0;
 248 bool volatile ObjectSynchronizer::_is_async_deflation_requested = false;
 249 bool volatile ObjectSynchronizer::_is_final_audit = false;
 250 jlong ObjectSynchronizer::_last_async_deflation_time_ns = 0;
 251 static uintx _no_progress_cnt = 0;
 252 static bool _no_progress_skip_increment = false;
 253 
 254 // =====================> Quick functions
 255 
 256 // The quick_* forms are special fast-path variants used to improve
 257 // performance.  In the simplest case, a "quick_*" implementation could
 258 // simply return false, in which case the caller will perform the necessary
 259 // state transitions and call the slow-path form.
 260 // The fast-path is designed to handle frequently arising cases in an efficient
 261 // manner and is just a degenerate "optimistic" variant of the slow-path.
 262 // returns true  -- to indicate the call was satisfied.
 263 // returns false -- to indicate the call needs the services of the slow-path.
 264 // A no-loitering ordinance is in effect for code in the quick_* family
 265 // operators: safepoints or indefinite blocking (blocking that might span a
 266 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon
 267 // entry.
 268 //
 269 // Consider: An interesting optimization is to have the JIT recognize the
 270 // following common idiom:
 271 //   synchronized (someobj) { .... ; notify(); }
 272 // That is, we find a notify() or notifyAll() call that immediately precedes
 273 // the monitorexit operation.  In that case the JIT could fuse the operations
 274 // into a single notifyAndExit() runtime primitive.
 275 
 276 bool ObjectSynchronizer::quick_notify(oopDesc* obj, JavaThread* current, bool all) {
 277   assert(current->thread_state() == _thread_in_Java, "invariant");
 278   NoSafepointVerifier nsv;
 279   if (obj == NULL) return false;  // slow-path for invalid obj
 280   const markWord mark = obj->mark();
 281 
 282   if (LockingMode == LM_LIGHTWEIGHT) {
 283     if (mark.is_fast_locked() && current->lock_stack().contains(cast_to_oop(obj))) {
 284       // Degenerate notify
 285       // fast-locked by caller so by definition the implied waitset is empty.
 286       return true;
 287     }
 288   } else if (LockingMode == LM_LEGACY) {
 289     if (mark.has_locker() && current->is_lock_owned((address)mark.locker())) {
 290       // Degenerate notify
 291       // stack-locked by caller so by definition the implied waitset is empty.
 292       return true;
 293     }
 294   }
 295 
 296   if (mark.has_monitor()) {
 297     ObjectMonitor* const mon = mark.monitor();
 298     assert(mon->object() == oop(obj), "invariant");
 299     if (mon->owner() != current) return false;  // slow-path for IMS exception
 300 
 301     if (mon->first_waiter() != NULL) {
 302       // We have one or more waiters. Since this is an inflated monitor
 303       // that we own, we can transfer one or more threads from the waitset
 304       // to the entrylist here and now, avoiding the slow-path.
 305       if (all) {
 306         DTRACE_MONITOR_PROBE(notifyAll, mon, obj, current);
 307       } else {
 308         DTRACE_MONITOR_PROBE(notify, mon, obj, current);
 309       }
 310       int free_count = 0;
 311       do {
 312         mon->INotify(current);
 313         ++free_count;
 314       } while (mon->first_waiter() != NULL && all);
 315       OM_PERFDATA_OP(Notifications, inc(free_count));
 316     }
 317     return true;
 318   }
 319 
 320   // biased locking and any other IMS exception states take the slow-path
 321   return false;
 322 }
 323 
 324 
 325 // The LockNode emitted directly at the synchronization site would have
 326 // been too big if it were to have included support for the cases of inflated
 327 // recursive enter and exit, so they go here instead.
 328 // Note that we can't safely call AsyncPrintJavaStack() from within
 329 // quick_enter() as our thread state remains _in_Java.
 330 
 331 bool ObjectSynchronizer::quick_enter(oop obj, JavaThread* current,
 332                                      BasicLock * lock) {
 333   assert(current->thread_state() == _thread_in_Java, "invariant");
 334   NoSafepointVerifier nsv;
 335   if (obj == NULL) return false;       // Need to throw NPE
 336 
 337   if (obj->klass()->is_value_based()) {
 338     return false;
 339   }
 340 
 341   if (LockingMode == LM_LIGHTWEIGHT) {
 342     LockStack& lock_stack = current->lock_stack();
 343     if (lock_stack.is_full()) {
 344       // Always go into runtime if the lock stack is full.
 345       return false;
 346     }
 347     if (lock_stack.try_recursive_enter(obj)) {
 348       // Recursive lock successful.
 349       return true;
 350     }
 351   }
 352 
 353   const markWord mark = obj->mark();
 354 
 355   if (mark.has_monitor()) {
 356     ObjectMonitor* const m = mark.monitor();
 357     // An async deflation or GC can race us before we manage to make
 358     // the ObjectMonitor busy by setting the owner below. If we detect
 359     // that race we just bail out to the slow-path here.
 360     if (m->object_peek() == NULL) {
 361       return false;
 362     }
 363     JavaThread* const owner = (JavaThread*) m->owner_raw();
 364 
 365     // Lock contention and Transactional Lock Elision (TLE) diagnostics
 366     // and observability
 367     // Case: light contention possibly amenable to TLE
 368     // Case: TLE inimical operations such as nested/recursive synchronization
 369 
 370     if (owner == current) {
 371       m->_recursions++;
 372       return true;
 373     }
 374 
 375     if (LockingMode != LM_LIGHTWEIGHT) {
 376       // This Java Monitor is inflated so obj's header will never be
 377       // displaced to this thread's BasicLock. Make the displaced header
 378       // non-NULL so this BasicLock is not seen as recursive nor as
 379       // being locked. We do this unconditionally so that this thread's
 380       // BasicLock cannot be mis-interpreted by any stack walkers. For
 381       // performance reasons, stack walkers generally first check for
 382       // Biased Locking in the object's header, the second check is for
 383       // stack-locking in the object's header, the third check is for
 384       // recursive stack-locking in the displaced header in the BasicLock,
 385       // and last are the inflated Java Monitor (ObjectMonitor) checks.
 386       lock->set_displaced_header(markWord::unused_mark());
 387     }
 388 
 389     if (owner == NULL && m->try_set_owner_from(NULL, current) == NULL) {
 390       assert(m->_recursions == 0, "invariant");
 391       return true;
 392     }
 393   }
 394 
 395   // Note that we could inflate in quick_enter.
 396   // This is likely a useful optimization
 397   // Critically, in quick_enter() we must not:
 398   // -- perform bias revocation, or
 399   // -- block indefinitely, or
 400   // -- reach a safepoint
 401 
 402   return false;        // revert to slow-path
 403 }
 404 
 405 // Handle notifications when synchronizing on value based classes
 406 void ObjectSynchronizer::handle_sync_on_value_based_class(Handle obj, JavaThread* locking_thread) {
 407   assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
 408   frame last_frame = locking_thread->last_frame();
 409   bool bcp_was_adjusted = false;
 410   // Don't decrement bcp if it points to the frame's first instruction.  This happens when
 411   // handle_sync_on_value_based_class() is called because of a synchronized method.  There
 412   // is no actual monitorenter instruction in the byte code in this case.
 413   if (last_frame.is_interpreted_frame() &&
 414       (last_frame.interpreter_frame_method()->code_base() < last_frame.interpreter_frame_bcp())) {
 415     // adjust bcp to point back to monitorenter so that we print the correct line numbers
 416     last_frame.interpreter_frame_set_bcp(last_frame.interpreter_frame_bcp() - 1);
 417     bcp_was_adjusted = true;
 418   }
 419 
 420   if (DiagnoseSyncOnValueBasedClasses == FATAL_EXIT) {
 421     ResourceMark rm;
 422     stringStream ss;
 423     locking_thread->print_stack_on(&ss);
 424     char* base = (char*)strstr(ss.base(), "at");
 425     char* newline = (char*)strchr(ss.base(), '\n');
 426     if (newline != NULL) {
 427       *newline = '\0';
 428     }
 429     fatal("Synchronizing on object " INTPTR_FORMAT " of klass %s %s", p2i(obj()), obj->klass()->external_name(), base);
 430   } else {
 431     assert(DiagnoseSyncOnValueBasedClasses == LOG_WARNING, "invalid value for DiagnoseSyncOnValueBasedClasses");
 432     ResourceMark rm;
 433     Log(valuebasedclasses) vblog;
 434 
 435     vblog.info("Synchronizing on object " INTPTR_FORMAT " of klass %s", p2i(obj()), obj->klass()->external_name());
 436     if (locking_thread->has_last_Java_frame()) {
 437       LogStream info_stream(vblog.info());
 438       locking_thread->print_stack_on(&info_stream);
 439     } else {
 440       vblog.info("Cannot find the last Java frame");
 441     }
 442 
 443     EventSyncOnValueBasedClass event;
 444     if (event.should_commit()) {
 445       event.set_valueBasedClass(obj->klass());
 446       event.commit();
 447     }
 448   }
 449 
 450   if (bcp_was_adjusted) {
 451     last_frame.interpreter_frame_set_bcp(last_frame.interpreter_frame_bcp() + 1);
 452   }
 453 }
 454 
 455 static bool useHeavyMonitors() {
 456 #if defined(X86) || defined(AARCH64) || defined(PPC64) || defined(RISCV64)
 457   return LockingMode == LM_MONITOR;
 458 #else
 459   return false;
 460 #endif
 461 }
 462 
 463 // -----------------------------------------------------------------------------
 464 // Monitor Enter/Exit
 465 
 466 void ObjectSynchronizer::enter_for(Handle obj, BasicLock* lock, JavaThread* locking_thread) {
 467   // When called with locking_thread != Thread::current() some mechanism must synchronize
 468   // the locking_thread with respect to the current thread. Currently only used when
 469   // deoptimizing and re-locking locks. See Deoptimization::relock_objects
 470   assert(locking_thread == Thread::current() || locking_thread->is_obj_deopt_suspend(), "must be");
 471   if (!enter_fast_impl(obj, lock, locking_thread)) {
 472     // Inflated ObjectMonitor::enter_for is required
 473 
 474     // An async deflation can race after the inflate_for() call and before
 475     // enter_for() can make the ObjectMonitor busy. enter_for() returns false
 476     // if we have lost the race to async deflation and we simply try again.
 477     while (true) {
 478       ObjectMonitor* monitor = inflate_for(locking_thread, obj(), inflate_cause_monitor_enter);
 479       if (monitor->enter_for(locking_thread)) {
 480         return;
 481       }
 482       assert(monitor->is_being_async_deflated(), "must be");
 483     }
 484   }
 485 }
 486 
 487 void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, JavaThread* current) {
 488   assert(current == Thread::current(), "must be");
 489   if (!enter_fast_impl(obj, lock, current)) {
 490     // Inflated ObjectMonitor::enter is required
 491 
 492     // An async deflation can race after the inflate() call and before
 493     // enter() can make the ObjectMonitor busy. enter() returns false if
 494     // we have lost the race to async deflation and we simply try again.
 495     while (true) {
 496       ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_monitor_enter);
 497       if (monitor->enter(current)) {
 498         return;
 499       }
 500     }
 501   }
 502 }
 503 
 504 // The interpreter and compiler assembly code tries to lock using the fast path
 505 // of this algorithm. Make sure to update that code if the following function is
 506 // changed. The implementation is extremely sensitive to race condition. Be careful.
 507 bool ObjectSynchronizer::enter_fast_impl(Handle obj, BasicLock* lock, JavaThread* locking_thread) {
 508 
 509   if (obj->klass()->is_value_based()) {
 510     handle_sync_on_value_based_class(obj, locking_thread);
 511   }
 512 
 513   if (!useHeavyMonitors()) {
 514     if (LockingMode == LM_LIGHTWEIGHT) {
 515       // Fast-locking does not use the 'lock' argument.
 516       LockStack& lock_stack = locking_thread->lock_stack();
 517       if (lock_stack.is_full()) {
 518         // We unconditionally make room on the lock stack by inflating
 519         // the least recently locked object on the lock stack.
 520 
 521         // About the choice to inflate least recently locked object.
 522         // First we must chose to inflate a lock, either some lock on
 523         // the lock-stack or the lock that is currently being entered
 524         // (which may or may not be on the lock-stack).
 525         // Second the best lock to inflate is a lock which is entered
 526         // in a control flow where there are only a very few locks being
 527         // used, as the costly part of inflated locking is inflation,
 528         // not locking. But this property is entirely program dependent.
 529         // Third inflating the lock currently being entered on when it
 530         // is not present on the lock-stack will result in a still full
 531         // lock-stack. This creates a scenario where every deeper nested
 532         // monitorenter must call into the runtime.
 533         // The rational here is as follows:
 534         // Because we cannot (currently) figure out the second, and want
 535         // to avoid the third, we inflate a lock on the lock-stack.
 536         // The least recently locked lock is chosen as it is the lock
 537         // with the longest critical section.
 538 
 539         log_info(monitorinflation)("LockStack capacity exceeded, inflating.");
 540         ObjectMonitor* monitor = inflate_for(locking_thread, lock_stack.bottom(), inflate_cause_vm_internal);
 541         assert(monitor->owner() == Thread::current(), "must be owner=" PTR_FORMAT " current=" PTR_FORMAT " mark=" PTR_FORMAT,
 542                p2i(monitor->owner()), p2i(Thread::current()), monitor->object()->mark_acquire().value());
 543         assert(!lock_stack.is_full(), "must have made room here");
 544       }
 545 
 546       markWord mark = obj()->mark_acquire();
 547       while (mark.is_neutral()) {
 548         // Retry until a lock state change has been observed. cas_set_mark() may collide with non lock bits modifications.
 549         // Try to swing into 'fast-locked' state.
 550         assert(!lock_stack.contains(obj()), "thread must not already hold the lock");
 551         const markWord locked_mark = mark.set_fast_locked();
 552         const markWord old_mark = obj()->cas_set_mark(locked_mark, mark);
 553         if (old_mark == mark) {
 554           // Successfully fast-locked, push object to lock-stack and return.
 555           lock_stack.push(obj());
 556           return true;
 557         }
 558         mark = old_mark;
 559       }
 560 
 561       if (mark.is_fast_locked() && lock_stack.try_recursive_enter(obj())) {
 562         // Recursive lock successful.
 563         return true;
 564       }
 565 
 566       // Failed to fast lock.
 567       return false;
 568     } else if (LockingMode == LM_LEGACY) {
 569       if (UseBiasedLocking) {
 570         BiasedLocking::revoke(locking_thread, obj);
 571       }
 572 
 573       markWord mark = obj->mark();
 574       if (mark.is_neutral()) {
 575         // Anticipate successful CAS -- the ST of the displaced mark must
 576         // be visible <= the ST performed by the CAS.
 577         lock->set_displaced_header(mark);
 578         if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) {
 579           return true;
 580         }
 581       } else if (mark.has_locker() &&
 582                  locking_thread->is_lock_owned((address) mark.locker())) {
 583         assert(lock != mark.locker(), "must not re-lock the same lock");
 584         assert(lock != (BasicLock*) obj->mark().value(), "don't relock with same BasicLock");
 585         lock->set_displaced_header(markWord::from_pointer(NULL));
 586         return true;
 587       }
 588 
 589       // The object header will never be displaced to this lock,
 590       // so it does not matter what the value is, except that it
 591       // must be non-zero to avoid looking like a re-entrant lock,
 592       // and must not look locked either.
 593       lock->set_displaced_header(markWord::unused_mark());
 594 
 595       // Failed to fast lock.
 596       return false;
 597     }
 598   }
 599 
 600   return false;
 601 }
 602 
 603 void ObjectSynchronizer::exit(oop object, BasicLock* lock, JavaThread* current) {
 604   if (!useHeavyMonitors()) {
 605     markWord mark = object->mark();
 606     if (LockingMode == LM_LIGHTWEIGHT) {
 607       // Fast-locking does not use the 'lock' argument.
 608       LockStack& lock_stack = current->lock_stack();
 609       if (mark.is_fast_locked() && lock_stack.try_recursive_exit(object)) {
 610         // Recursively unlocked.
 611         return;
 612       }
 613 
 614       if (mark.is_fast_locked() && lock_stack.is_recursive(object)) {
 615         // This lock is recursive but is not at the top of the lock stack so we're
 616         // doing an unbalanced exit. We have to fall thru to inflation below and
 617         // let ObjectMonitor::exit() do the unlock.
 618       } else {
 619         while (mark.is_fast_locked()) {
 620           // Retry until a lock state change has been observed. cas_set_mark() may collide with non lock bits modifications.
 621           const markWord unlocked_mark = mark.set_unlocked();
 622           const markWord old_mark = object->cas_set_mark(unlocked_mark, mark);
 623           if (old_mark == mark) {
 624             size_t recursions = lock_stack.remove(object) - 1;
 625             assert(recursions == 0, "must not be recursive here");
 626             return;
 627           }
 628           mark = old_mark;
 629         }
 630       }
 631     } else if (LockingMode == LM_LEGACY) {
 632       markWord dhw = lock->displaced_header();
 633       if (dhw.value() == 0) {
 634         // If the displaced header is NULL, then this exit matches up with
 635         // a recursive enter. No real work to do here except for diagnostics.
 636 #ifndef PRODUCT
 637         if (mark != markWord::INFLATING()) {
 638           // Only do diagnostics if we are not racing an inflation. Simply
 639           // exiting a recursive enter of a Java Monitor that is being
 640           // inflated is safe; see the has_monitor() comment below.
 641           assert(!mark.is_neutral(), "invariant");
 642           assert(!mark.has_locker() ||
 643                  current->is_lock_owned((address)mark.locker()), "invariant");
 644           if (mark.has_monitor()) {
 645             // The BasicLock's displaced_header is marked as a recursive
 646             // enter and we have an inflated Java Monitor (ObjectMonitor).
 647             // This is a special case where the Java Monitor was inflated
 648             // after this thread entered the stack-lock recursively. When a
 649             // Java Monitor is inflated, we cannot safely walk the Java
 650             // Monitor owner's stack and update the BasicLocks because a
 651             // Java Monitor can be asynchronously inflated by a thread that
 652             // does not own the Java Monitor.
 653             ObjectMonitor* m = mark.monitor();
 654             assert(m->object()->mark() == mark, "invariant");
 655             assert(m->is_entered(current), "invariant");
 656           }
 657         }
 658 #endif
 659         return;
 660       }
 661 
 662       if (mark == markWord::from_pointer(lock)) {
 663         // If the object is stack-locked by the current thread, try to
 664         // swing the displaced header from the BasicLock back to the mark.
 665         assert(dhw.is_neutral(), "invariant");
 666         if (object->cas_set_mark(dhw, mark) == mark) {
 667           return;
 668         }
 669       }
 670     }
 671   }
 672 
 673   // We have to take the slow-path of possible inflation and then exit.
 674   // The ObjectMonitor* can't be async deflated until ownership is
 675   // dropped inside exit() and the ObjectMonitor* must be !is_busy().
 676   ObjectMonitor* monitor = inflate(current, object, inflate_cause_vm_internal);
 677   assert(!monitor->is_owner_anonymous(), "must not be");
 678   monitor->exit(current);
 679 }
 680 
 681 // -----------------------------------------------------------------------------
 682 // Class Loader  support to workaround deadlocks on the class loader lock objects
 683 // Also used by GC
 684 // complete_exit()/reenter() are used to wait on a nested lock
 685 // i.e. to give up an outer lock completely and then re-enter
 686 // Used when holding nested locks - lock acquisition order: lock1 then lock2
 687 //  1) complete_exit lock1 - saving recursion count
 688 //  2) wait on lock2
 689 //  3) when notified on lock2, unlock lock2
 690 //  4) reenter lock1 with original recursion count
 691 //  5) lock lock2
 692 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 693 intx ObjectSynchronizer::complete_exit(Handle obj, JavaThread* current) {
 694   if (UseBiasedLocking) {
 695     BiasedLocking::revoke(current, obj);
 696     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 697   }
 698 
 699   // The ObjectMonitor* can't be async deflated until ownership is
 700   // dropped inside exit() and the ObjectMonitor* must be !is_busy().
 701   ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_vm_internal);
 702   intptr_t ret_code = monitor->complete_exit(current);
 703   return ret_code;
 704 }
 705 
 706 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 707 void ObjectSynchronizer::reenter(Handle obj, intx recursions, JavaThread* current) {
 708   if (UseBiasedLocking) {
 709     BiasedLocking::revoke(current, obj);
 710     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 711   }
 712 
 713   // An async deflation can race after the inflate() call and before
 714   // reenter() -> enter() can make the ObjectMonitor busy. reenter() ->
 715   // enter() returns false if we have lost the race to async deflation
 716   // and we simply try again.
 717   while (true) {
 718     ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_vm_internal);
 719     if (monitor->reenter(recursions, current)) {
 720       return;
 721     }
 722   }
 723 }
 724 
 725 // -----------------------------------------------------------------------------
 726 // JNI locks on java objects
 727 // NOTE: must use heavy weight monitor to handle jni monitor enter
 728 void ObjectSynchronizer::jni_enter(Handle obj, JavaThread* current) {
 729   if (obj->klass()->is_value_based()) {
 730     handle_sync_on_value_based_class(obj, current);
 731   }
 732 
 733   // the current locking is from JNI instead of Java code
 734   if (UseBiasedLocking) {
 735     BiasedLocking::revoke(current, obj);
 736     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 737   }
 738   current->set_current_pending_monitor_is_from_java(false);
 739   // An async deflation can race after the inflate() call and before
 740   // enter() can make the ObjectMonitor busy. enter() returns false if
 741   // we have lost the race to async deflation and we simply try again.
 742   while (true) {
 743     ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_jni_enter);
 744     if (monitor->enter(current)) {
 745       break;
 746     }
 747   }
 748   current->set_current_pending_monitor_is_from_java(true);
 749 }
 750 
 751 // NOTE: must use heavy weight monitor to handle jni monitor exit
 752 void ObjectSynchronizer::jni_exit(oop obj, TRAPS) {
 753   JavaThread* current = THREAD;
 754   if (UseBiasedLocking) {
 755     Handle h_obj(current, obj);
 756     BiasedLocking::revoke(current, h_obj);
 757     obj = h_obj();
 758   }
 759   assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 760 
 761   // The ObjectMonitor* can't be async deflated until ownership is
 762   // dropped inside exit() and the ObjectMonitor* must be !is_busy().
 763   ObjectMonitor* monitor = inflate(current, obj, inflate_cause_jni_exit);
 764   // If this thread has locked the object, exit the monitor. We
 765   // intentionally do not use CHECK on check_owner because we must exit the
 766   // monitor even if an exception was already pending.
 767   if (monitor->check_owner(THREAD)) {
 768     monitor->exit(current);
 769   }
 770 }
 771 
 772 // -----------------------------------------------------------------------------
 773 // Internal VM locks on java objects
 774 // standard constructor, allows locking failures
 775 ObjectLocker::ObjectLocker(Handle obj, JavaThread* thread) {
 776   _thread = thread;
 777   _thread->check_for_valid_safepoint_state();
 778   _obj = obj;
 779 
 780   if (_obj() != NULL) {
 781     ObjectSynchronizer::enter(_obj, &_lock, _thread);
 782   }
 783 }
 784 
 785 ObjectLocker::~ObjectLocker() {
 786   if (_obj() != NULL) {
 787     ObjectSynchronizer::exit(_obj(), &_lock, _thread);
 788   }
 789 }
 790 
 791 
 792 // -----------------------------------------------------------------------------
 793 //  Wait/Notify/NotifyAll
 794 // NOTE: must use heavy weight monitor to handle wait()
 795 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
 796   JavaThread* current = THREAD;
 797   if (UseBiasedLocking) {
 798     BiasedLocking::revoke(current, obj);
 799     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 800   }
 801   if (millis < 0) {
 802     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 803   }
 804   // The ObjectMonitor* can't be async deflated because the _waiters
 805   // field is incremented before ownership is dropped and decremented
 806   // after ownership is regained.
 807   ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_wait);
 808 
 809   DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), current, millis);
 810   monitor->wait(millis, true, THREAD); // Not CHECK as we need following code
 811 
 812   // This dummy call is in place to get around dtrace bug 6254741.  Once
 813   // that's fixed we can uncomment the following line, remove the call
 814   // and change this function back into a "void" func.
 815   // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
 816   int ret_code = dtrace_waited_probe(monitor, obj, THREAD);
 817   return ret_code;
 818 }
 819 
 820 // No exception are possible in this case as we only use this internally when locking is
 821 // correct and we have to wait until notified - so no interrupts or timeouts.
 822 void ObjectSynchronizer::wait_uninterruptibly(Handle obj, JavaThread* current) {
 823   if (UseBiasedLocking) {
 824     BiasedLocking::revoke(current, obj);
 825     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 826   }
 827   // The ObjectMonitor* can't be async deflated because the _waiters
 828   // field is incremented before ownership is dropped and decremented
 829   // after ownership is regained.
 830   ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_wait);
 831   monitor->wait(0 /* wait-forever */, false /* not interruptible */, current);
 832 }
 833 
 834 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
 835   JavaThread* current = THREAD;
 836   if (UseBiasedLocking) {
 837     BiasedLocking::revoke(current, obj);
 838     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 839   }
 840 
 841   markWord mark = obj->mark();
 842   if (LockingMode == LM_LIGHTWEIGHT) {
 843     if ((mark.is_fast_locked() && current->lock_stack().contains(obj()))) {
 844       // Not inflated so there can't be any waiters to notify.
 845       return;
 846     }
 847   } else if (LockingMode == LM_LEGACY) {
 848     if (mark.has_locker() && current->is_lock_owned((address)mark.locker())) {
 849       // Not inflated so there can't be any waiters to notify.
 850       return;
 851     }
 852   }
 853   // The ObjectMonitor* can't be async deflated until ownership is
 854   // dropped by the calling thread.
 855   ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_notify);
 856   monitor->notify(CHECK);
 857 }
 858 
 859 // NOTE: see comment of notify()
 860 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
 861   JavaThread* current = THREAD;
 862   if (UseBiasedLocking) {
 863     BiasedLocking::revoke(current, obj);
 864     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 865   }
 866 
 867   markWord mark = obj->mark();
 868   if (LockingMode == LM_LIGHTWEIGHT) {
 869     if ((mark.is_fast_locked() && current->lock_stack().contains(obj()))) {
 870       // Not inflated so there can't be any waiters to notify.
 871       return;
 872     }
 873   } else if (LockingMode == LM_LEGACY) {
 874     if (mark.has_locker() && current->is_lock_owned((address)mark.locker())) {
 875       // Not inflated so there can't be any waiters to notify.
 876       return;
 877     }
 878   }
 879   // The ObjectMonitor* can't be async deflated until ownership is
 880   // dropped by the calling thread.
 881   ObjectMonitor* monitor = inflate(current, obj(), inflate_cause_notify);
 882   monitor->notifyAll(CHECK);
 883 }
 884 
 885 // -----------------------------------------------------------------------------
 886 // Hash Code handling
 887 
 888 struct SharedGlobals {
 889   char         _pad_prefix[OM_CACHE_LINE_SIZE];
 890   // This is a highly shared mostly-read variable.
 891   // To avoid false-sharing it needs to be the sole occupant of a cache line.
 892   volatile int stw_random;
 893   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 894   // Hot RW variable -- Sequester to avoid false-sharing
 895   volatile int hc_sequence;
 896   DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 897 };
 898 
 899 static SharedGlobals GVars;
 900 
 901 static markWord read_stable_mark(oop obj) {
 902   markWord mark = obj->mark_acquire();
 903   if (!mark.is_being_inflated() || LockingMode == LM_LIGHTWEIGHT) {
 904     // New lightweight locking does not use the markWord::INFLATING() protocol.
 905     return mark;       // normal fast-path return
 906   }
 907 
 908   int its = 0;
 909   for (;;) {
 910     markWord mark = obj->mark_acquire();
 911     if (!mark.is_being_inflated()) {
 912       return mark;    // normal fast-path return
 913     }
 914 
 915     // The object is being inflated by some other thread.
 916     // The caller of read_stable_mark() must wait for inflation to complete.
 917     // Avoid live-lock.
 918 
 919     ++its;
 920     if (its > 10000 || !os::is_MP()) {
 921       if (its & 1) {
 922         os::naked_yield();
 923       } else {
 924         // Note that the following code attenuates the livelock problem but is not
 925         // a complete remedy.  A more complete solution would require that the inflating
 926         // thread hold the associated inflation lock.  The following code simply restricts
 927         // the number of spinners to at most one.  We'll have N-2 threads blocked
 928         // on the inflationlock, 1 thread holding the inflation lock and using
 929         // a yield/park strategy, and 1 thread in the midst of inflation.
 930         // A more refined approach would be to change the encoding of INFLATING
 931         // to allow encapsulation of a native thread pointer.  Threads waiting for
 932         // inflation to complete would use CAS to push themselves onto a singly linked
 933         // list rooted at the markword.  Once enqueued, they'd loop, checking a per-thread flag
 934         // and calling park().  When inflation was complete the thread that accomplished inflation
 935         // would detach the list and set the markword to inflated with a single CAS and
 936         // then for each thread on the list, set the flag and unpark() the thread.
 937 
 938         // Index into the lock array based on the current object address.
 939         static_assert(is_power_of_2(NINFLATIONLOCKS), "must be");
 940         int ix = (cast_from_oop<intptr_t>(obj) >> 5) & (NINFLATIONLOCKS-1);
 941         int YieldThenBlock = 0;
 942         assert(ix >= 0 && ix < NINFLATIONLOCKS, "invariant");
 943         gInflationLocks[ix]->lock();
 944         while (obj->mark_acquire() == markWord::INFLATING()) {
 945           // Beware: naked_yield() is advisory and has almost no effect on some platforms
 946           // so we periodically call current->_ParkEvent->park(1).
 947           // We use a mixed spin/yield/block mechanism.
 948           if ((YieldThenBlock++) >= 16) {
 949             Thread::current()->_ParkEvent->park(1);
 950           } else {
 951             os::naked_yield();
 952           }
 953         }
 954         gInflationLocks[ix]->unlock();
 955       }
 956     } else {
 957       SpinPause();       // SMP-polite spinning
 958     }
 959   }
 960 }
 961 
 962 // hashCode() generation :
 963 //
 964 // Possibilities:
 965 // * MD5Digest of {obj,stw_random}
 966 // * CRC32 of {obj,stw_random} or any linear-feedback shift register function.
 967 // * A DES- or AES-style SBox[] mechanism
 968 // * One of the Phi-based schemes, such as:
 969 //   2654435761 = 2^32 * Phi (golden ratio)
 970 //   HashCodeValue = ((uintptr_t(obj) >> 3) * 2654435761) ^ GVars.stw_random ;
 971 // * A variation of Marsaglia's shift-xor RNG scheme.
 972 // * (obj ^ stw_random) is appealing, but can result
 973 //   in undesirable regularity in the hashCode values of adjacent objects
 974 //   (objects allocated back-to-back, in particular).  This could potentially
 975 //   result in hashtable collisions and reduced hashtable efficiency.
 976 //   There are simple ways to "diffuse" the middle address bits over the
 977 //   generated hashCode values:
 978 
 979 static inline intptr_t get_next_hash(Thread* current, oop obj) {
 980   intptr_t value = 0;
 981   if (hashCode == 0) {
 982     // This form uses global Park-Miller RNG.
 983     // On MP system we'll have lots of RW access to a global, so the
 984     // mechanism induces lots of coherency traffic.
 985     value = os::random();
 986   } else if (hashCode == 1) {
 987     // This variation has the property of being stable (idempotent)
 988     // between STW operations.  This can be useful in some of the 1-0
 989     // synchronization schemes.
 990     intptr_t addr_bits = cast_from_oop<intptr_t>(obj) >> 3;
 991     value = addr_bits ^ (addr_bits >> 5) ^ GVars.stw_random;
 992   } else if (hashCode == 2) {
 993     value = 1;            // for sensitivity testing
 994   } else if (hashCode == 3) {
 995     value = ++GVars.hc_sequence;
 996   } else if (hashCode == 4) {
 997     value = cast_from_oop<intptr_t>(obj);
 998   } else {
 999     // Marsaglia's xor-shift scheme with thread-specific state
1000     // This is probably the best overall implementation -- we'll
1001     // likely make this the default in future releases.
1002     unsigned t = current->_hashStateX;
1003     t ^= (t << 11);
1004     current->_hashStateX = current->_hashStateY;
1005     current->_hashStateY = current->_hashStateZ;
1006     current->_hashStateZ = current->_hashStateW;
1007     unsigned v = current->_hashStateW;
1008     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8));
1009     current->_hashStateW = v;
1010     value = v;
1011   }
1012 
1013   value &= UseCompactObjectHeaders ? markWord::hash_mask_compact : markWord::hash_mask;
1014   if (value == 0) value = 0xBAD;
1015   assert(value != markWord::no_hash, "invariant");
1016   return value;
1017 }
1018 
1019 intptr_t ObjectSynchronizer::FastHashCode(Thread* current, oop obj) {
1020   if (UseBiasedLocking) {
1021     // NOTE: many places throughout the JVM do not expect a safepoint
1022     // to be taken here. However, we only ever bias Java instances and all
1023     // of the call sites of identity_hash that might revoke biases have
1024     // been checked to make sure they can handle a safepoint. The
1025     // added check of the bias pattern is to avoid useless calls to
1026     // thread-local storage.
1027     if (obj->mark().has_bias_pattern()) {
1028       // Handle for oop obj in case of STW safepoint
1029       Handle hobj(current, obj);
1030       if (SafepointSynchronize::is_at_safepoint()) {
1031         BiasedLocking::revoke_at_safepoint(hobj);
1032       } else {
1033         BiasedLocking::revoke(current->as_Java_thread(), hobj);
1034       }
1035       obj = hobj();
1036       assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
1037     }
1038   }
1039 
1040   while (true) {
1041     ObjectMonitor* monitor = NULL;
1042     markWord temp, test;
1043     intptr_t hash;
1044     markWord mark = read_stable_mark(obj);
1045 
1046     // object should remain ineligible for biased locking
1047     assert(!mark.has_bias_pattern(), "invariant");
1048 
1049     if (mark.is_neutral() || (LockingMode == LM_LIGHTWEIGHT && mark.is_fast_locked())) {
1050       hash = mark.hash();
1051       if (hash != 0) {                     // if it has a hash, just return it
1052         return hash;
1053       }
1054       hash = get_next_hash(current, obj);  // get a new hash
1055       temp = mark.copy_set_hash(hash);     // merge the hash into header
1056                                            // try to install the hash
1057       test = obj->cas_set_mark(temp, mark);
1058       if (test == mark) {                  // if the hash was installed, return it
1059         return hash;
1060       }
1061       if (LockingMode == LM_LIGHTWEIGHT) {
1062         // CAS failed, retry
1063         continue;
1064       }
1065       // Failed to install the hash. It could be that another thread
1066       // installed the hash just before our attempt or inflation has
1067       // occurred or... so we fall thru to inflate the monitor for
1068       // stability and then install the hash.
1069     } else if (mark.has_monitor()) {
1070       monitor = mark.monitor();
1071       temp = monitor->header();
1072       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1073       hash = temp.hash();
1074       if (hash != 0) {
1075         // It has a hash.
1076 
1077         // Separate load of dmw/header above from the loads in
1078         // is_being_async_deflated().
1079 
1080         // dmw/header and _contentions may get written by different threads.
1081         // Make sure to observe them in the same order when having several observers.
1082         OrderAccess::loadload_for_IRIW();
1083 
1084         if (monitor->is_being_async_deflated()) {
1085           // But we can't safely use the hash if we detect that async
1086           // deflation has occurred. So we attempt to restore the
1087           // header/dmw to the object's header so that we only retry
1088           // once if the deflater thread happens to be slow.
1089           monitor->install_displaced_markword_in_object(obj);
1090           continue;
1091         }
1092         return hash;
1093       }
1094       // Fall thru so we only have one place that installs the hash in
1095       // the ObjectMonitor.
1096     } else if (LockingMode == LM_LEGACY && mark.has_locker() && current->is_lock_owned((address)mark.locker())) {
1097       // This is a stack lock owned by the calling thread so fetch the
1098       // displaced markWord from the BasicLock on the stack.
1099       temp = mark.displaced_mark_helper();
1100       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1101       hash = temp.hash();
1102       if (hash != 0) {                  // if it has a hash, just return it
1103         return hash;
1104       }
1105       // WARNING:
1106       // The displaced header in the BasicLock on a thread's stack
1107       // is strictly immutable. It CANNOT be changed in ANY cases.
1108       // So we have to inflate the stack lock into an ObjectMonitor
1109       // even if the current thread owns the lock. The BasicLock on
1110       // a thread's stack can be asynchronously read by other threads
1111       // during an inflate() call so any change to that stack memory
1112       // may not propagate to other threads correctly.
1113     }
1114 
1115     // Inflate the monitor to set the hash.
1116 
1117     // An async deflation can race after the inflate() call and before we
1118     // can update the ObjectMonitor's header with the hash value below.
1119     monitor = inflate(current, obj, inflate_cause_hash_code);
1120     // Load ObjectMonitor's header/dmw field and see if it has a hash.
1121     mark = monitor->header();
1122     assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1123     hash = mark.hash();
1124     if (hash == 0) {                       // if it does not have a hash
1125       hash = get_next_hash(current, obj);  // get a new hash
1126       temp = mark.copy_set_hash(hash)   ;  // merge the hash into header
1127       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1128       uintptr_t v = Atomic::cmpxchg((volatile uintptr_t*)monitor->header_addr(), mark.value(), temp.value());
1129       test = markWord(v);
1130       if (test != mark) {
1131         // The attempt to update the ObjectMonitor's header/dmw field
1132         // did not work. This can happen if another thread managed to
1133         // merge in the hash just before our cmpxchg().
1134         // If we add any new usages of the header/dmw field, this code
1135         // will need to be updated.
1136         hash = test.hash();
1137         assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
1138         assert(hash != 0, "should only have lost the race to a thread that set a non-zero hash");
1139       }
1140       if (monitor->is_being_async_deflated()) {
1141         // If we detect that async deflation has occurred, then we
1142         // attempt to restore the header/dmw to the object's header
1143         // so that we only retry once if the deflater thread happens
1144         // to be slow.
1145         monitor->install_displaced_markword_in_object(obj);
1146         continue;
1147       }
1148     }
1149     // We finally get the hash.
1150     return hash;
1151   }
1152 }
1153 
1154 // Deprecated -- use FastHashCode() instead.
1155 
1156 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
1157   return FastHashCode(Thread::current(), obj());
1158 }
1159 
1160 
1161 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* current,
1162                                                    Handle h_obj) {
1163   if (UseBiasedLocking) {
1164     BiasedLocking::revoke(current, h_obj);
1165     assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
1166   }
1167 
1168   assert(current == JavaThread::current(), "Can only be called on current thread");
1169   oop obj = h_obj();
1170 
1171   markWord mark = read_stable_mark(obj);
1172 
1173   if (LockingMode == LM_LEGACY && mark.has_locker()) {
1174     // stack-locked case, header points into owner's stack
1175     return current->is_lock_owned((address)mark.locker());
1176   }
1177 
1178   if (LockingMode == LM_LIGHTWEIGHT && mark.is_fast_locked()) {
1179     // fast-locking case, see if lock is in current's lock stack
1180     return current->lock_stack().contains(h_obj());
1181   }
1182 
1183   // Contended case, header points to ObjectMonitor (tagged pointer)
1184   if (mark.has_monitor()) {
1185     // The first stage of async deflation does not affect any field
1186     // used by this comparison so the ObjectMonitor* is usable here.
1187     ObjectMonitor* monitor = mark.monitor();
1188     return monitor->is_entered(current) != 0;
1189   }
1190   // Unlocked case, header in place
1191   assert(mark.is_neutral(), "sanity check");
1192   return false;
1193 }
1194 
1195 // FIXME: jvmti should call this
1196 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
1197   if (UseBiasedLocking) {
1198     if (SafepointSynchronize::is_at_safepoint()) {
1199       BiasedLocking::revoke_at_safepoint(h_obj);
1200     } else {
1201       BiasedLocking::revoke(JavaThread::current(), h_obj);
1202     }
1203     assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
1204   }
1205 
1206   oop obj = h_obj();
1207   markWord mark = read_stable_mark(obj);
1208 
1209   if (LockingMode == LM_LEGACY && mark.has_locker()) {
1210     // stack-locked so header points into owner's stack.
1211     // owning_thread_from_monitor_owner() may also return null here:
1212     return Threads::owning_thread_from_monitor_owner(t_list, (address) mark.locker());
1213   }
1214 
1215   if (LockingMode == LM_LIGHTWEIGHT && mark.is_fast_locked()) {
1216     // fast-locked so get owner from the object.
1217     // owning_thread_from_object() may also return null here:
1218     return Threads::owning_thread_from_object(t_list, h_obj());
1219   }
1220 
1221   // Contended case, header points to ObjectMonitor (tagged pointer)
1222   if (mark.has_monitor()) {
1223     // The first stage of async deflation does not affect any field
1224     // used by this comparison so the ObjectMonitor* is usable here.
1225     ObjectMonitor* monitor = mark.monitor();
1226     assert(monitor != NULL, "monitor should be non-null");
1227     // owning_thread_from_monitor() may also return null here:
1228     return Threads::owning_thread_from_monitor(t_list, monitor);
1229   }
1230 
1231   // Unlocked case, header in place
1232   // Cannot have assertion since this object may have been
1233   // locked by another thread when reaching here.
1234   // assert(mark.is_neutral(), "sanity check");
1235 
1236   return NULL;
1237 }
1238 
1239 // Visitors ...
1240 
1241 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure, JavaThread* thread) {
1242   MonitorList::Iterator iter = _in_use_list.iterator();
1243   while (iter.has_next()) {
1244     ObjectMonitor* mid = iter.next();
1245     if (mid->owner() != thread) {
1246       continue;
1247     }
1248     if (!mid->is_being_async_deflated() && mid->object_peek() != NULL) {
1249       // Only process with closure if the object is set.
1250 
1251       // monitors_iterate() is only called at a safepoint or when the
1252       // target thread is suspended or when the target thread is
1253       // operating on itself. The current closures in use today are
1254       // only interested in an owned ObjectMonitor and ownership
1255       // cannot be dropped under the calling contexts so the
1256       // ObjectMonitor cannot be async deflated.
1257       closure->do_monitor(mid);
1258     }
1259   }
1260 }
1261 
1262 static bool monitors_used_above_threshold(MonitorList* list) {
1263   if (MonitorUsedDeflationThreshold == 0) {  // disabled case is easy
1264     return false;
1265   }
1266   // Start with ceiling based on a per-thread estimate:
1267   size_t ceiling = ObjectSynchronizer::in_use_list_ceiling();
1268   size_t old_ceiling = ceiling;
1269   if (ceiling < list->max()) {
1270     // The max used by the system has exceeded the ceiling so use that:
1271     ceiling = list->max();
1272   }
1273   size_t monitors_used = list->count();
1274   if (monitors_used == 0) {  // empty list is easy
1275     return false;
1276   }
1277   if (NoAsyncDeflationProgressMax != 0 &&
1278       _no_progress_cnt >= NoAsyncDeflationProgressMax) {
1279     float remainder = (100.0 - MonitorUsedDeflationThreshold) / 100.0;
1280     size_t new_ceiling = ceiling + (ceiling * remainder) + 1;
1281     ObjectSynchronizer::set_in_use_list_ceiling(new_ceiling);
1282     log_info(monitorinflation)("Too many deflations without progress; "
1283                                "bumping in_use_list_ceiling from " SIZE_FORMAT
1284                                " to " SIZE_FORMAT, old_ceiling, new_ceiling);
1285     _no_progress_cnt = 0;
1286     ceiling = new_ceiling;
1287   }
1288 
1289   // Check if our monitor usage is above the threshold:
1290   size_t monitor_usage = (monitors_used * 100LL) / ceiling;
1291   if (int(monitor_usage) > MonitorUsedDeflationThreshold) {
1292     log_info(monitorinflation)("monitors_used=" SIZE_FORMAT ", ceiling=" SIZE_FORMAT
1293                                ", monitor_usage=" SIZE_FORMAT ", threshold=" INTX_FORMAT,
1294                                monitors_used, ceiling, monitor_usage, MonitorUsedDeflationThreshold);
1295     return true;
1296   }
1297 
1298   return false;
1299 }
1300 
1301 size_t ObjectSynchronizer::in_use_list_ceiling() {
1302   return _in_use_list_ceiling;
1303 }
1304 
1305 void ObjectSynchronizer::dec_in_use_list_ceiling() {
1306   Atomic::sub(&_in_use_list_ceiling, AvgMonitorsPerThreadEstimate);
1307 }
1308 
1309 void ObjectSynchronizer::inc_in_use_list_ceiling() {
1310   Atomic::add(&_in_use_list_ceiling, AvgMonitorsPerThreadEstimate);
1311 }
1312 
1313 void ObjectSynchronizer::set_in_use_list_ceiling(size_t new_value) {
1314   _in_use_list_ceiling = new_value;
1315 }
1316 
1317 bool ObjectSynchronizer::is_async_deflation_needed() {
1318   if (is_async_deflation_requested()) {
1319     // Async deflation request.
1320     log_info(monitorinflation)("Async deflation needed: explicit request");
1321     return true;
1322   }
1323 
1324   jlong time_since_last = time_since_last_async_deflation_ms();
1325 
1326   if (AsyncDeflationInterval > 0 &&
1327       time_since_last > AsyncDeflationInterval &&
1328       monitors_used_above_threshold(&_in_use_list)) {
1329     // It's been longer than our specified deflate interval and there
1330     // are too many monitors in use. We don't deflate more frequently
1331     // than AsyncDeflationInterval (unless is_async_deflation_requested)
1332     // in order to not swamp the MonitorDeflationThread.
1333     log_info(monitorinflation)("Async deflation needed: monitors used are above the threshold");
1334     return true;
1335   }
1336 
1337   if (GuaranteedAsyncDeflationInterval > 0 &&
1338       time_since_last > GuaranteedAsyncDeflationInterval) {
1339     // It's been longer than our specified guaranteed deflate interval.
1340     // We need to clean up the used monitors even if the threshold is
1341     // not reached, to keep the memory utilization at bay when many threads
1342     // touched many monitors.
1343     log_info(monitorinflation)("Async deflation needed: guaranteed interval (" INTX_FORMAT " ms) "
1344                                "is greater than time since last deflation (" JLONG_FORMAT " ms)",
1345                                GuaranteedAsyncDeflationInterval, time_since_last);
1346 
1347     // If this deflation has no progress, then it should not affect the no-progress
1348     // tracking, otherwise threshold heuristics would think it was triggered, experienced
1349     // no progress, and needs to backoff more aggressively. In this "no progress" case,
1350     // the generic code would bump the no-progress counter, and we compensate for that
1351     // by telling it to skip the update.
1352     //
1353     // If this deflation has progress, then it should let non-progress tracking
1354     // know about this, otherwise the threshold heuristics would kick in, potentially
1355     // experience no-progress due to aggressive cleanup by this deflation, and think
1356     // it is still in no-progress stride. In this "progress" case, the generic code would
1357     // zero the counter, and we allow it to happen.
1358     _no_progress_skip_increment = true;
1359 
1360     return true;
1361   }
1362 
1363   return false;
1364 }
1365 
1366 bool ObjectSynchronizer::request_deflate_idle_monitors() {
1367   JavaThread* current = JavaThread::current();
1368   bool ret_code = false;
1369 
1370   jlong last_time = last_async_deflation_time_ns();
1371   set_is_async_deflation_requested(true);
1372   {
1373     MonitorLocker ml(MonitorDeflation_lock, Mutex::_no_safepoint_check_flag);
1374     ml.notify_all();
1375   }
1376   const int N_CHECKS = 5;
1377   for (int i = 0; i < N_CHECKS; i++) {  // sleep for at most 5 seconds
1378     if (last_async_deflation_time_ns() > last_time) {
1379       log_info(monitorinflation)("Async Deflation happened after %d check(s).", i);
1380       ret_code = true;
1381       break;
1382     }
1383     {
1384       // JavaThread has to honor the blocking protocol.
1385       ThreadBlockInVM tbivm(current);
1386       os::naked_short_sleep(999);  // sleep for almost 1 second
1387     }
1388   }
1389   if (!ret_code) {
1390     log_info(monitorinflation)("Async Deflation DID NOT happen after %d checks.", N_CHECKS);
1391   }
1392 
1393   return ret_code;
1394 }
1395 
1396 jlong ObjectSynchronizer::time_since_last_async_deflation_ms() {
1397   return (os::javaTimeNanos() - last_async_deflation_time_ns()) / (NANOUNITS / MILLIUNITS);
1398 }
1399 
1400 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1401                                        const oop obj,
1402                                        ObjectSynchronizer::InflateCause cause) {
1403   assert(event != NULL, "invariant");
1404   event->set_monitorClass(obj->klass());
1405   event->set_address((uintptr_t)(void*)obj);
1406   event->set_cause((u1)cause);
1407   event->commit();
1408 }
1409 
1410 // Fast path code shared by multiple functions
1411 void ObjectSynchronizer::inflate_helper(oop obj) {
1412   markWord mark = obj->mark_acquire();
1413   if (mark.has_monitor()) {
1414     ObjectMonitor* monitor = mark.monitor();
1415     markWord dmw = monitor->header();
1416     assert(dmw.is_neutral(), "sanity check: header=" INTPTR_FORMAT, dmw.value());
1417     return;
1418   }
1419   (void)inflate(Thread::current(), obj, inflate_cause_vm_internal);
1420 }
1421 
1422 ObjectMonitor* ObjectSynchronizer::inflate(Thread* current, oop obj, const InflateCause cause) {
1423   assert(current == Thread::current(), "must be");
1424   if (LockingMode == LM_LIGHTWEIGHT && current->is_Java_thread()) {
1425     return inflate_impl(current->as_Java_thread(), obj, cause);
1426   }
1427   return inflate_impl(nullptr, obj, cause);
1428 }
1429 
1430 ObjectMonitor* ObjectSynchronizer::inflate_for(JavaThread* thread, oop obj, const InflateCause cause) {
1431   assert(thread == Thread::current() || thread->is_obj_deopt_suspend(), "must be");
1432   return inflate_impl(thread, obj, cause);
1433 }
1434 
1435 ObjectMonitor* ObjectSynchronizer::inflate_impl(JavaThread* inflating_thread, oop object, const InflateCause cause) {
1436   // The JavaThread* inflating_thread parameter is only used by LM_LIGHTWEIGHT and requires
1437   // that the inflating_thread == Thread::current() or is suspended throughout the call by
1438   // some other mechanism.
1439   // Even with LM_LIGHTWEIGHT the thread might be nullptr when called from a non
1440   // JavaThread. (As may still be the case from FastHashCode). However it is only
1441   // important for the correctness of the LM_LIGHTWEIGHT algorithm that the thread
1442   // is set when called from ObjectSynchronizer::enter from the owning thread,
1443   // ObjectSynchronizer::enter_for from any thread, or ObjectSynchronizer::exit.
1444   EventJavaMonitorInflate event;
1445 
1446   for (;;) {
1447     const markWord mark = object->mark_acquire();
1448     assert(!mark.has_bias_pattern(), "invariant");
1449 
1450     // The mark can be in one of the following states:
1451     // *  inflated     - Just return if using stack-locking.
1452     //                   If using fast-locking and the ObjectMonitor owner
1453     //                   is anonymous and the inflating_thread owns the
1454     //                   object lock, then we make the inflating_thread
1455     //                   the ObjectMonitor owner and remove the lock from
1456     //                   the inflating_thread's lock stack.
1457     // *  fast-locked  - Coerce it to inflated from fast-locked.
1458     // *  stack-locked - Coerce it to inflated from stack-locked.
1459     // *  INFLATING    - busy wait for conversion to complete
1460     // *  Neutral      - aggressively inflate the object.
1461     // *  BIASED       - Illegal.  We should never see this
1462 
1463     // CASE: inflated
1464     if (mark.has_monitor()) {
1465       ObjectMonitor* inf = mark.monitor();
1466       markWord dmw = inf->header();
1467       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1468       if (LockingMode == LM_LIGHTWEIGHT && inf->is_owner_anonymous() &&
1469           inflating_thread != nullptr && inflating_thread->lock_stack().contains(object)) {
1470         inf->set_owner_from_anonymous(inflating_thread);
1471         size_t removed = inflating_thread->lock_stack().remove(object);
1472         inf->set_recursions(removed - 1);
1473       }
1474       return inf;
1475     }
1476 
1477     // CASE: inflation in progress - inflating over a stack-lock.
1478     // Some other thread is converting from stack-locked to inflated.
1479     // Only that thread can complete inflation -- other threads must wait.
1480     // The INFLATING value is transient.
1481     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1482     // We could always eliminate polling by parking the thread on some auxiliary list.
1483     if (LockingMode != LM_LIGHTWEIGHT) {
1484       // New lightweight locking does not use INFLATING.
1485       // CASE: inflation in progress - inflating over a stack-lock.
1486       // Some other thread is converting from stack-locked to inflated.
1487       // Only that thread can complete inflation -- other threads must wait.
1488       // The INFLATING value is transient.
1489       // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1490       // We could always eliminate polling by parking the thread on some auxiliary list.
1491       if (mark == markWord::INFLATING()) {
1492         read_stable_mark(object);
1493         continue;
1494       }
1495     }
1496 
1497     // CASE: fast-locked
1498     // Could be fast-locked either by the inflating_thread or by some other thread.
1499     //
1500     // Note that we allocate the ObjectMonitor speculatively, _before_
1501     // attempting to set the object's mark to the new ObjectMonitor. If
1502     // the inflating_thread owns the monitor, then we set the ObjectMonitor's
1503     // owner to the inflating_thread. Otherwise, we set the ObjectMonitor's owner
1504     // to anonymous. If we lose the race to set the object's mark to the
1505     // new ObjectMonitor, then we just delete it and loop around again.
1506     //
1507     LogStreamHandle(Trace, monitorinflation) lsh;
1508     if (LockingMode == LM_LIGHTWEIGHT && mark.is_fast_locked()) {
1509       ObjectMonitor* monitor = new ObjectMonitor(object);
1510       monitor->set_header(mark.set_unlocked());
1511       bool own = inflating_thread != nullptr && inflating_thread->lock_stack().contains(object);
1512       if (own) {
1513         // Owned by us.
1514         monitor->set_owner_from(NULL, inflating_thread);
1515       } else {
1516         // Owned by somebody else.
1517         monitor->set_owner_anonymous();
1518       }
1519       markWord monitor_mark = markWord::encode(monitor);
1520       markWord old_mark = object->cas_set_mark(monitor_mark, mark);
1521       if (old_mark == mark) {
1522         // Success! Return inflated monitor.
1523         if (own) {
1524           size_t removed = inflating_thread->lock_stack().remove(object);
1525           monitor->set_recursions(removed - 1);
1526         }
1527         // Once the ObjectMonitor is configured and object is associated
1528         // with the ObjectMonitor, it is safe to allow async deflation:
1529         _in_use_list.add(monitor);
1530 
1531         // Hopefully the performance counters are allocated on distinct
1532         // cache lines to avoid false sharing on MP systems ...
1533         OM_PERFDATA_OP(Inflations, inc());
1534         if (log_is_enabled(Trace, monitorinflation)) {
1535           ResourceMark rm;
1536           lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1537                        INTPTR_FORMAT ", type='%s'", p2i(object),
1538                        object->mark().value(), object->klass()->external_name());
1539         }
1540         if (event.should_commit()) {
1541           post_monitor_inflate_event(&event, object, cause);
1542         }
1543         return monitor;
1544       } else {
1545         delete monitor;
1546         continue;  // Interference -- just retry
1547       }
1548     }
1549 
1550     // CASE: stack-locked
1551     // Could be stack-locked either by this thread or by some other thread.
1552     //
1553     // Note that we allocate the ObjectMonitor speculatively, _before_ attempting
1554     // to install INFLATING into the mark word.  We originally installed INFLATING,
1555     // allocated the ObjectMonitor, and then finally STed the address of the
1556     // ObjectMonitor into the mark.  This was correct, but artificially lengthened
1557     // the interval in which INFLATING appeared in the mark, thus increasing
1558     // the odds of inflation contention.
1559 
1560     if (LockingMode == LM_LEGACY && mark.has_locker()) {
1561       assert(LockingMode != LM_LIGHTWEIGHT, "cannot happen with new lightweight locking");
1562       ObjectMonitor* m = new ObjectMonitor(object);
1563       // Optimistically prepare the ObjectMonitor - anticipate successful CAS
1564       // We do this before the CAS in order to minimize the length of time
1565       // in which INFLATING appears in the mark.
1566 
1567       markWord cmp = object->cas_set_mark(markWord::INFLATING(), mark);
1568       if (cmp != mark) {
1569         delete m;
1570         continue;       // Interference -- just retry
1571       }
1572 
1573       // We've successfully installed INFLATING (0) into the mark-word.
1574       // This is the only case where 0 will appear in a mark-word.
1575       // Only the singular thread that successfully swings the mark-word
1576       // to 0 can perform (or more precisely, complete) inflation.
1577       //
1578       // Why do we CAS a 0 into the mark-word instead of just CASing the
1579       // mark-word from the stack-locked value directly to the new inflated state?
1580       // Consider what happens when a thread unlocks a stack-locked object.
1581       // It attempts to use CAS to swing the displaced header value from the
1582       // on-stack BasicLock back into the object header.  Recall also that the
1583       // header value (hash code, etc) can reside in (a) the object header, or
1584       // (b) a displaced header associated with the stack-lock, or (c) a displaced
1585       // header in an ObjectMonitor.  The inflate() routine must copy the header
1586       // value from the BasicLock on the owner's stack to the ObjectMonitor, all
1587       // the while preserving the hashCode stability invariants.  If the owner
1588       // decides to release the lock while the value is 0, the unlock will fail
1589       // and control will eventually pass from slow_exit() to inflate.  The owner
1590       // will then spin, waiting for the 0 value to disappear.   Put another way,
1591       // the 0 causes the owner to stall if the owner happens to try to
1592       // drop the lock (restoring the header from the BasicLock to the object)
1593       // while inflation is in-progress.  This protocol avoids races that might
1594       // would otherwise permit hashCode values to change or "flicker" for an object.
1595       // Critically, while object->mark is 0 mark.displaced_mark_helper() is stable.
1596       // 0 serves as a "BUSY" inflate-in-progress indicator.
1597 
1598 
1599       // fetch the displaced mark from the owner's stack.
1600       // The owner can't die or unwind past the lock while our INFLATING
1601       // object is in the mark.  Furthermore the owner can't complete
1602       // an unlock on the object, either.
1603       markWord dmw = mark.displaced_mark_helper();
1604       // Catch if the object's header is not neutral (not locked and
1605       // not marked is what we care about here).
1606       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1607 
1608       // Setup monitor fields to proper values -- prepare the monitor
1609       m->set_header(dmw);
1610 
1611       // Optimization: if the mark.locker stack address is associated
1612       // with this thread we could simply set m->_owner = current.
1613       // Note that a thread can inflate an object
1614       // that it has stack-locked -- as might happen in wait() -- directly
1615       // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1616       m->set_owner_from(NULL, mark.locker());
1617       // TODO-FIXME: assert BasicLock->dhw != 0.
1618 
1619       // Must preserve store ordering. The monitor state must
1620       // be stable at the time of publishing the monitor address.
1621       guarantee(object->mark() == markWord::INFLATING(), "invariant");
1622       // Release semantics so that above set_object() is seen first.
1623       object->release_set_mark(markWord::encode(m));
1624 
1625       // Once ObjectMonitor is configured and the object is associated
1626       // with the ObjectMonitor, it is safe to allow async deflation:
1627       _in_use_list.add(m);
1628 
1629       // Hopefully the performance counters are allocated on distinct cache lines
1630       // to avoid false sharing on MP systems ...
1631       OM_PERFDATA_OP(Inflations, inc());
1632       if (log_is_enabled(Trace, monitorinflation)) {
1633         ResourceMark rm;
1634         lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1635                      INTPTR_FORMAT ", type='%s'", p2i(object),
1636                      object->mark().value(), object->klass()->external_name());
1637       }
1638       if (event.should_commit()) {
1639         post_monitor_inflate_event(&event, object, cause);
1640       }
1641       return m;
1642     }
1643 
1644     // CASE: neutral
1645     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1646     // If we know we're inflating for entry it's better to inflate by swinging a
1647     // pre-locked ObjectMonitor pointer into the object header.   A successful
1648     // CAS inflates the object *and* confers ownership to the inflating thread.
1649     // In the current implementation we use a 2-step mechanism where we CAS()
1650     // to inflate and then CAS() again to try to swing _owner from NULL to current.
1651     // An inflateTry() method that we could call from enter() would be useful.
1652 
1653     // Catch if the object's header is not neutral (not locked and
1654     // not marked is what we care about here).
1655     assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1656     ObjectMonitor* m = new ObjectMonitor(object);
1657     // prepare m for installation - set monitor to initial state
1658     m->set_header(mark);
1659 
1660     if (object->cas_set_mark(markWord::encode(m), mark) != mark) {
1661       delete m;
1662       m = NULL;
1663       continue;
1664       // interference - the markword changed - just retry.
1665       // The state-transitions are one-way, so there's no chance of
1666       // live-lock -- "Inflated" is an absorbing state.
1667     }
1668 
1669     // Once the ObjectMonitor is configured and object is associated
1670     // with the ObjectMonitor, it is safe to allow async deflation:
1671     _in_use_list.add(m);
1672 
1673     // Hopefully the performance counters are allocated on distinct
1674     // cache lines to avoid false sharing on MP systems ...
1675     OM_PERFDATA_OP(Inflations, inc());
1676     if (log_is_enabled(Trace, monitorinflation)) {
1677       ResourceMark rm;
1678       lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
1679                    INTPTR_FORMAT ", type='%s'", p2i(object),
1680                    object->mark().value(), object->klass()->external_name());
1681     }
1682     if (event.should_commit()) {
1683       post_monitor_inflate_event(&event, object, cause);
1684     }
1685     return m;
1686   }
1687 }
1688 
1689 void ObjectSynchronizer::chk_for_block_req(JavaThread* current, const char* op_name,
1690                                            const char* cnt_name, size_t cnt,
1691                                            LogStream* ls, elapsedTimer* timer_p) {
1692   if (!SafepointMechanism::should_process(current)) {
1693     return;
1694   }
1695 
1696   // A safepoint/handshake has started.
1697   if (ls != NULL) {
1698     timer_p->stop();
1699     ls->print_cr("pausing %s: %s=" SIZE_FORMAT ", in_use_list stats: ceiling="
1700                  SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1701                  op_name, cnt_name, cnt, in_use_list_ceiling(),
1702                  _in_use_list.count(), _in_use_list.max());
1703   }
1704 
1705   {
1706     // Honor block request.
1707     ThreadBlockInVM tbivm(current);
1708   }
1709 
1710   if (ls != NULL) {
1711     ls->print_cr("resuming %s: in_use_list stats: ceiling=" SIZE_FORMAT
1712                  ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT, op_name,
1713                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1714     timer_p->start();
1715   }
1716 }
1717 
1718 // Walk the in-use list and deflate (at most MonitorDeflationMax) idle
1719 // ObjectMonitors. Returns the number of deflated ObjectMonitors.
1720 size_t ObjectSynchronizer::deflate_monitor_list(Thread* current, LogStream* ls,
1721                                                 elapsedTimer* timer_p) {
1722   MonitorList::Iterator iter = _in_use_list.iterator();
1723   size_t deflated_count = 0;
1724 
1725   while (iter.has_next()) {
1726     if (deflated_count >= (size_t)MonitorDeflationMax) {
1727       break;
1728     }
1729     ObjectMonitor* mid = iter.next();
1730     if (mid->deflate_monitor()) {
1731       deflated_count++;
1732     }
1733 
1734     if (current->is_Java_thread()) {
1735       // A JavaThread must check for a safepoint/handshake and honor it.
1736       chk_for_block_req(current->as_Java_thread(), "deflation", "deflated_count",
1737                         deflated_count, ls, timer_p);
1738     }
1739   }
1740 
1741   return deflated_count;
1742 }
1743 
1744 class HandshakeForDeflation : public HandshakeClosure {
1745  public:
1746   HandshakeForDeflation() : HandshakeClosure("HandshakeForDeflation") {}
1747 
1748   void do_thread(Thread* thread) {
1749     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
1750                                 INTPTR_FORMAT, p2i(thread));
1751   }
1752 };
1753 
1754 class VM_RendezvousGCThreads : public VM_Operation {
1755 public:
1756   bool evaluate_at_safepoint() const override { return false; }
1757   VMOp_Type type() const override { return VMOp_RendezvousGCThreads; }
1758   void doit() override {
1759     SuspendibleThreadSet::synchronize();
1760     SuspendibleThreadSet::desynchronize();
1761   };
1762 };
1763 
1764 // This function is called by the MonitorDeflationThread to deflate
1765 // ObjectMonitors. It is also called via do_final_audit_and_print_stats()
1766 // by the VMThread.
1767 size_t ObjectSynchronizer::deflate_idle_monitors() {
1768   Thread* current = Thread::current();
1769   if (current->is_Java_thread()) {
1770     // The async deflation request has been processed.
1771     _last_async_deflation_time_ns = os::javaTimeNanos();
1772     set_is_async_deflation_requested(false);
1773   }
1774 
1775   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1776   LogStreamHandle(Info, monitorinflation) lsh_info;
1777   LogStream* ls = NULL;
1778   if (log_is_enabled(Debug, monitorinflation)) {
1779     ls = &lsh_debug;
1780   } else if (log_is_enabled(Info, monitorinflation)) {
1781     ls = &lsh_info;
1782   }
1783 
1784   elapsedTimer timer;
1785   if (ls != NULL) {
1786     ls->print_cr("begin deflating: in_use_list stats: ceiling=" SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1787                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1788     timer.start();
1789   }
1790 
1791   // Deflate some idle ObjectMonitors.
1792   size_t deflated_count = deflate_monitor_list(current, ls, &timer);
1793   if (deflated_count > 0 || is_final_audit()) {
1794     // There are ObjectMonitors that have been deflated or this is the
1795     // final audit and all the remaining ObjectMonitors have been
1796     // deflated, BUT the MonitorDeflationThread blocked for the final
1797     // safepoint during unlinking.
1798 
1799     // Unlink deflated ObjectMonitors from the in-use list.
1800     ResourceMark rm;
1801     GrowableArray<ObjectMonitor*> delete_list((int)deflated_count);
1802     size_t unlinked_count = _in_use_list.unlink_deflated(current, ls, &timer,
1803                                                          &delete_list);
1804     if (current->is_Java_thread()) {
1805       if (ls != NULL) {
1806         timer.stop();
1807         ls->print_cr("before handshaking: unlinked_count=" SIZE_FORMAT
1808                      ", in_use_list stats: ceiling=" SIZE_FORMAT ", count="
1809                      SIZE_FORMAT ", max=" SIZE_FORMAT,
1810                      unlinked_count, in_use_list_ceiling(),
1811                      _in_use_list.count(), _in_use_list.max());
1812       }
1813 
1814       // A JavaThread needs to handshake in order to safely free the
1815       // ObjectMonitors that were deflated in this cycle.
1816       // Also, we sync and desync GC threads around the handshake, so that they can
1817       // safely read the mark-word and look-through to the object-monitor, without
1818       // being afraid that the object-monitor is going away.
1819       HandshakeForDeflation hfd_hc;
1820       Handshake::execute(&hfd_hc);
1821       VM_RendezvousGCThreads sync_gc;
1822       VMThread::execute(&sync_gc);
1823 
1824       if (ls != NULL) {
1825         ls->print_cr("after handshaking: in_use_list stats: ceiling="
1826                      SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1827                      in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1828         timer.start();
1829       }
1830     }
1831 
1832     NativeHeapTrimmer::SuspendMark sm("monitor deletion");
1833 
1834     // After the handshake, safely free the ObjectMonitors that were
1835     // deflated in this cycle.
1836     size_t deleted_count = 0;
1837     for (ObjectMonitor* monitor: delete_list) {
1838       delete monitor;
1839       deleted_count++;
1840 
1841       if (current->is_Java_thread()) {
1842         // A JavaThread must check for a safepoint/handshake and honor it.
1843         chk_for_block_req(current->as_Java_thread(), "deletion", "deleted_count",
1844                           deleted_count, ls, &timer);
1845       }
1846     }
1847   }
1848 
1849   if (ls != NULL) {
1850     timer.stop();
1851     if (deflated_count != 0 || log_is_enabled(Debug, monitorinflation)) {
1852       ls->print_cr("deflated " SIZE_FORMAT " monitors in %3.7f secs",
1853                    deflated_count, timer.seconds());
1854     }
1855     ls->print_cr("end deflating: in_use_list stats: ceiling=" SIZE_FORMAT ", count=" SIZE_FORMAT ", max=" SIZE_FORMAT,
1856                  in_use_list_ceiling(), _in_use_list.count(), _in_use_list.max());
1857   }
1858 
1859   OM_PERFDATA_OP(MonExtant, set_value(_in_use_list.count()));
1860   OM_PERFDATA_OP(Deflations, inc(deflated_count));
1861 
1862   GVars.stw_random = os::random();
1863 
1864   if (deflated_count != 0) {
1865     _no_progress_cnt = 0;
1866   } else if (_no_progress_skip_increment) {
1867     _no_progress_skip_increment = false;
1868   } else {
1869     _no_progress_cnt++;
1870   }
1871 
1872   return deflated_count;
1873 }
1874 
1875 // Monitor cleanup on JavaThread::exit
1876 
1877 // Iterate through monitor cache and attempt to release thread's monitors
1878 class ReleaseJavaMonitorsClosure: public MonitorClosure {
1879  private:
1880   JavaThread* _thread;
1881 
1882  public:
1883   ReleaseJavaMonitorsClosure(JavaThread* thread) : _thread(thread) {}
1884   void do_monitor(ObjectMonitor* mid) {
1885     (void)mid->complete_exit(_thread);
1886   }
1887 };
1888 
1889 // Release all inflated monitors owned by current thread.  Lightweight monitors are
1890 // ignored.  This is meant to be called during JNI thread detach which assumes
1891 // all remaining monitors are heavyweight.  All exceptions are swallowed.
1892 // Scanning the extant monitor list can be time consuming.
1893 // A simple optimization is to add a per-thread flag that indicates a thread
1894 // called jni_monitorenter() during its lifetime.
1895 //
1896 // Instead of NoSafepointVerifier it might be cheaper to
1897 // use an idiom of the form:
1898 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
1899 //   <code that must not run at safepoint>
1900 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1901 // Since the tests are extremely cheap we could leave them enabled
1902 // for normal product builds.
1903 
1904 void ObjectSynchronizer::release_monitors_owned_by_thread(JavaThread* current) {
1905   assert(current == JavaThread::current(), "must be current Java thread");
1906   NoSafepointVerifier nsv;
1907   ReleaseJavaMonitorsClosure rjmc(current);
1908   ObjectSynchronizer::monitors_iterate(&rjmc, current);
1909   assert(!current->has_pending_exception(), "Should not be possible");
1910   current->clear_pending_exception();
1911 }
1912 
1913 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) {
1914   switch (cause) {
1915     case inflate_cause_vm_internal:    return "VM Internal";
1916     case inflate_cause_monitor_enter:  return "Monitor Enter";
1917     case inflate_cause_wait:           return "Monitor Wait";
1918     case inflate_cause_notify:         return "Monitor Notify";
1919     case inflate_cause_hash_code:      return "Monitor Hash Code";
1920     case inflate_cause_jni_enter:      return "JNI Monitor Enter";
1921     case inflate_cause_jni_exit:       return "JNI Monitor Exit";
1922     default:
1923       ShouldNotReachHere();
1924   }
1925   return "Unknown";
1926 }
1927 
1928 //------------------------------------------------------------------------------
1929 // Debugging code
1930 
1931 u_char* ObjectSynchronizer::get_gvars_addr() {
1932   return (u_char*)&GVars;
1933 }
1934 
1935 u_char* ObjectSynchronizer::get_gvars_hc_sequence_addr() {
1936   return (u_char*)&GVars.hc_sequence;
1937 }
1938 
1939 size_t ObjectSynchronizer::get_gvars_size() {
1940   return sizeof(SharedGlobals);
1941 }
1942 
1943 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() {
1944   return (u_char*)&GVars.stw_random;
1945 }
1946 
1947 // Do the final audit and print of ObjectMonitor stats; must be done
1948 // by the VMThread at VM exit time.
1949 void ObjectSynchronizer::do_final_audit_and_print_stats() {
1950   assert(Thread::current()->is_VM_thread(), "sanity check");
1951 
1952   if (is_final_audit()) {  // Only do the audit once.
1953     return;
1954   }
1955   set_is_final_audit();
1956   log_info(monitorinflation)("Starting the final audit.");
1957 
1958   if (log_is_enabled(Info, monitorinflation)) {
1959     // Do a deflation in order to reduce the in-use monitor population
1960     // that is reported by ObjectSynchronizer::log_in_use_monitor_details()
1961     // which is called by ObjectSynchronizer::audit_and_print_stats().
1962     while (ObjectSynchronizer::deflate_idle_monitors() != 0) {
1963       ; // empty
1964     }
1965     // The other audit_and_print_stats() call is done at the Debug
1966     // level at a safepoint in SafepointSynchronize::do_cleanup_tasks.
1967     ObjectSynchronizer::audit_and_print_stats(true /* on_exit */);
1968   }
1969 }
1970 
1971 // This function can be called at a safepoint or it can be called when
1972 // we are trying to exit the VM. When we are trying to exit the VM, the
1973 // list walker functions can run in parallel with the other list
1974 // operations so spin-locking is used for safety.
1975 //
1976 // Calls to this function can be added in various places as a debugging
1977 // aid; pass 'true' for the 'on_exit' parameter to have in-use monitor
1978 // details logged at the Info level and 'false' for the 'on_exit'
1979 // parameter to have in-use monitor details logged at the Trace level.
1980 //
1981 void ObjectSynchronizer::audit_and_print_stats(bool on_exit) {
1982   assert(on_exit || SafepointSynchronize::is_at_safepoint(), "invariant");
1983 
1984   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1985   LogStreamHandle(Info, monitorinflation) lsh_info;
1986   LogStreamHandle(Trace, monitorinflation) lsh_trace;
1987   LogStream* ls = NULL;
1988   if (log_is_enabled(Trace, monitorinflation)) {
1989     ls = &lsh_trace;
1990   } else if (log_is_enabled(Debug, monitorinflation)) {
1991     ls = &lsh_debug;
1992   } else if (log_is_enabled(Info, monitorinflation)) {
1993     ls = &lsh_info;
1994   }
1995   assert(ls != NULL, "sanity check");
1996 
1997   int error_cnt = 0;
1998 
1999   ls->print_cr("Checking in_use_list:");
2000   chk_in_use_list(ls, &error_cnt);
2001 
2002   if (error_cnt == 0) {
2003     ls->print_cr("No errors found in in_use_list checks.");
2004   } else {
2005     log_error(monitorinflation)("found in_use_list errors: error_cnt=%d", error_cnt);
2006   }
2007 
2008   if ((on_exit && log_is_enabled(Info, monitorinflation)) ||
2009       (!on_exit && log_is_enabled(Trace, monitorinflation))) {
2010     // When exiting this log output is at the Info level. When called
2011     // at a safepoint, this log output is at the Trace level since
2012     // there can be a lot of it.
2013     log_in_use_monitor_details(ls);
2014   }
2015 
2016   ls->flush();
2017 
2018   guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt);
2019 }
2020 
2021 // Check the in_use_list; log the results of the checks.
2022 void ObjectSynchronizer::chk_in_use_list(outputStream* out, int *error_cnt_p) {
2023   size_t l_in_use_count = _in_use_list.count();
2024   size_t l_in_use_max = _in_use_list.max();
2025   out->print_cr("count=" SIZE_FORMAT ", max=" SIZE_FORMAT, l_in_use_count,
2026                 l_in_use_max);
2027 
2028   size_t ck_in_use_count = 0;
2029   MonitorList::Iterator iter = _in_use_list.iterator();
2030   while (iter.has_next()) {
2031     ObjectMonitor* mid = iter.next();
2032     chk_in_use_entry(mid, out, error_cnt_p);
2033     ck_in_use_count++;
2034   }
2035 
2036   if (l_in_use_count == ck_in_use_count) {
2037     out->print_cr("in_use_count=" SIZE_FORMAT " equals ck_in_use_count="
2038                   SIZE_FORMAT, l_in_use_count, ck_in_use_count);
2039   } else {
2040     out->print_cr("WARNING: in_use_count=" SIZE_FORMAT " is not equal to "
2041                   "ck_in_use_count=" SIZE_FORMAT, l_in_use_count,
2042                   ck_in_use_count);
2043   }
2044 
2045   size_t ck_in_use_max = _in_use_list.max();
2046   if (l_in_use_max == ck_in_use_max) {
2047     out->print_cr("in_use_max=" SIZE_FORMAT " equals ck_in_use_max="
2048                   SIZE_FORMAT, l_in_use_max, ck_in_use_max);
2049   } else {
2050     out->print_cr("WARNING: in_use_max=" SIZE_FORMAT " is not equal to "
2051                   "ck_in_use_max=" SIZE_FORMAT, l_in_use_max, ck_in_use_max);
2052   }
2053 }
2054 
2055 // Check an in-use monitor entry; log any errors.
2056 void ObjectSynchronizer::chk_in_use_entry(ObjectMonitor* n, outputStream* out,
2057                                           int* error_cnt_p) {
2058   if (n->owner_is_DEFLATER_MARKER()) {
2059     // This should not happen, but if it does, it is not fatal.
2060     out->print_cr("WARNING: monitor=" INTPTR_FORMAT ": in-use monitor is "
2061                   "deflated.", p2i(n));
2062     return;
2063   }
2064   if (n->header().value() == 0) {
2065     out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor must "
2066                   "have non-NULL _header field.", p2i(n));
2067     *error_cnt_p = *error_cnt_p + 1;
2068   }
2069   const oop obj = n->object_peek();
2070   if (obj != NULL) {
2071     const markWord mark = obj->mark();
2072     if (!mark.has_monitor()) {
2073       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
2074                     "object does not think it has a monitor: obj="
2075                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT, p2i(n),
2076                     p2i(obj), mark.value());
2077       *error_cnt_p = *error_cnt_p + 1;
2078     }
2079     ObjectMonitor* const obj_mon = mark.monitor();
2080     if (n != obj_mon) {
2081       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use monitor's "
2082                     "object does not refer to the same monitor: obj="
2083                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
2084                     INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
2085       *error_cnt_p = *error_cnt_p + 1;
2086     }
2087   }
2088 }
2089 
2090 // Log details about ObjectMonitors on the in_use_list. The 'BHL'
2091 // flags indicate why the entry is in-use, 'object' and 'object type'
2092 // indicate the associated object and its type.
2093 void ObjectSynchronizer::log_in_use_monitor_details(outputStream* out) {
2094   stringStream ss;
2095   if (_in_use_list.count() > 0) {
2096     out->print_cr("In-use monitor info:");
2097     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2098     out->print_cr("%18s  %s  %18s  %18s",
2099                   "monitor", "BHL", "object", "object type");
2100     out->print_cr("==================  ===  ==================  ==================");
2101     MonitorList::Iterator iter = _in_use_list.iterator();
2102     while (iter.has_next()) {
2103       ObjectMonitor* mid = iter.next();
2104       const oop obj = mid->object_peek();
2105       const markWord mark = mid->header();
2106       ResourceMark rm;
2107       out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(mid),
2108                  mid->is_busy(), mark.hash() != 0, mid->owner() != NULL,
2109                  p2i(obj), obj == NULL ? "" : obj->klass()->external_name());
2110       if (mid->is_busy()) {
2111         out->print(" (%s)", mid->is_busy_to_string(&ss));
2112         ss.reset();
2113       }
2114       out->cr();
2115     }
2116   }
2117 
2118   out->flush();
2119 }