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