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