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