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