< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page
rev 56775 : imported patch 8230876.patch
rev 56776 : v2.00 -> v2.07 (CR7/v2.07/10-for-jdk14) patches combined into one; merge with 8230876.patch (2019.10.17) and jdk-14+21.
rev 56777 : See CR7-to-CR8-changes.


  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 "logging/log.hpp"
  28 #include "logging/logStream.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/metaspaceShared.hpp"
  32 #include "memory/padded.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/markWord.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "runtime/atomic.hpp"
  38 #include "runtime/biasedLocking.hpp"
  39 #include "runtime/handles.inline.hpp"

  40 #include "runtime/interfaceSupport.inline.hpp"
  41 #include "runtime/mutexLocker.hpp"
  42 #include "runtime/objectMonitor.hpp"
  43 #include "runtime/objectMonitor.inline.hpp"
  44 #include "runtime/osThread.hpp"
  45 #include "runtime/safepointVerifiers.hpp"
  46 #include "runtime/sharedRuntime.hpp"
  47 #include "runtime/stubRoutines.hpp"
  48 #include "runtime/synchronizer.hpp"
  49 #include "runtime/thread.inline.hpp"
  50 #include "runtime/timer.hpp"
  51 #include "runtime/vframe.hpp"
  52 #include "runtime/vmThread.hpp"
  53 #include "utilities/align.hpp"
  54 #include "utilities/dtrace.hpp"
  55 #include "utilities/events.hpp"
  56 #include "utilities/preserveException.hpp"
  57 
  58 // The "core" versions of monitor enter and exit reside in this file.
  59 // The interpreter and compilers contain specialized transliterated


 101   }
 102 
 103 #else //  ndef DTRACE_ENABLED
 104 
 105 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
 106 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
 107 
 108 #endif // ndef DTRACE_ENABLED
 109 
 110 // This exists only as a workaround of dtrace bug 6254741
 111 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
 112   DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
 113   return 0;
 114 }
 115 
 116 #define NINFLATIONLOCKS 256
 117 static volatile intptr_t gInflationLocks[NINFLATIONLOCKS];
 118 
 119 // global list of blocks of monitors
 120 PaddedObjectMonitor* volatile ObjectSynchronizer::g_block_list = NULL;




 121 // Global ObjectMonitor free list. Newly allocated and deflated
 122 // ObjectMonitors are prepended here.
 123 ObjectMonitor* volatile ObjectSynchronizer::g_free_list = NULL;
 124 // Global ObjectMonitor in-use list. When a JavaThread is exiting,
 125 // ObjectMonitors on its per-thread in-use list are prepended here.
 126 ObjectMonitor* volatile ObjectSynchronizer::g_om_in_use_list = NULL;
 127 int ObjectSynchronizer::g_om_in_use_count = 0;  // # on g_om_in_use_list





 128 
 129 static volatile intptr_t gListLock = 0;   // protects global monitor lists
 130 static volatile int g_om_free_count = 0;  // # on g_free_list

 131 static volatile int g_om_population = 0;  // # Extant -- in circulation

 132 
 133 #define CHAINMARKER (cast_to_oop<intptr_t>(-1))
 134 
 135 






































































































































































































































































 136 // =====================> Quick functions
 137 
 138 // The quick_* forms are special fast-path variants used to improve
 139 // performance.  In the simplest case, a "quick_*" implementation could
 140 // simply return false, in which case the caller will perform the necessary
 141 // state transitions and call the slow-path form.
 142 // The fast-path is designed to handle frequently arising cases in an efficient
 143 // manner and is just a degenerate "optimistic" variant of the slow-path.
 144 // returns true  -- to indicate the call was satisfied.
 145 // returns false -- to indicate the call needs the services of the slow-path.
 146 // A no-loitering ordinance is in effect for code in the quick_* family
 147 // operators: safepoints or indefinite blocking (blocking that might span a
 148 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon
 149 // entry.
 150 //
 151 // Consider: An interesting optimization is to have the JIT recognize the
 152 // following common idiom:
 153 //   synchronized (someobj) { .... ; notify(); }
 154 // That is, we find a notify() or notifyAll() call that immediately precedes
 155 // the monitorexit operation.  In that case the JIT could fuse the operations


 194   }
 195 
 196   // biased locking and any other IMS exception states take the slow-path
 197   return false;
 198 }
 199 
 200 
 201 // The LockNode emitted directly at the synchronization site would have
 202 // been too big if it were to have included support for the cases of inflated
 203 // recursive enter and exit, so they go here instead.
 204 // Note that we can't safely call AsyncPrintJavaStack() from within
 205 // quick_enter() as our thread state remains _in_Java.
 206 
 207 bool ObjectSynchronizer::quick_enter(oop obj, Thread* self,
 208                                      BasicLock * lock) {
 209   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 210   assert(self->is_Java_thread(), "invariant");
 211   assert(((JavaThread *) self)->thread_state() == _thread_in_Java, "invariant");
 212   NoSafepointVerifier nsv;
 213   if (obj == NULL) return false;       // Need to throw NPE


 214   const markWord mark = obj->mark();
 215 
 216   if (mark.has_monitor()) {
 217     ObjectMonitor* const m = mark.monitor();






 218     assert(m->object() == obj, "invariant");
 219     Thread* const owner = (Thread *) m->_owner;
 220 
 221     // Lock contention and Transactional Lock Elision (TLE) diagnostics
 222     // and observability
 223     // Case: light contention possibly amenable to TLE
 224     // Case: TLE inimical operations such as nested/recursive synchronization
 225 
 226     if (owner == self) {
 227       m->_recursions++;
 228       return true;
 229     }
 230 
 231     // This Java Monitor is inflated so obj's header will never be
 232     // displaced to this thread's BasicLock. Make the displaced header
 233     // non-NULL so this BasicLock is not seen as recursive nor as
 234     // being locked. We do this unconditionally so that this thread's
 235     // BasicLock cannot be mis-interpreted by any stack walkers. For
 236     // performance reasons, stack walkers generally first check for
 237     // Biased Locking in the object's header, the second check is for
 238     // stack-locking in the object's header, the third check is for
 239     // recursive stack-locking in the displaced header in the BasicLock,
 240     // and last are the inflated Java Monitor (ObjectMonitor) checks.
 241     lock->set_displaced_header(markWord::unused_mark());
 242 
 243     if (owner == NULL && Atomic::replace_if_null(self, &(m->_owner))) {










 244       assert(m->_recursions == 0, "invariant");
 245       return true;
 246     }
 247   }


 248 
 249   // Note that we could inflate in quick_enter.
 250   // This is likely a useful optimization
 251   // Critically, in quick_enter() we must not:
 252   // -- perform bias revocation, or
 253   // -- block indefinitely, or
 254   // -- reach a safepoint
 255 
 256   return false;        // revert to slow-path
 257 }
 258 
 259 // -----------------------------------------------------------------------------
 260 // Monitor Enter/Exit
 261 // The interpreter and compiler assembly code tries to lock using the fast path
 262 // of this algorithm. Make sure to update that code if the following function is
 263 // changed. The implementation is extremely sensitive to race condition. Be careful.
 264 
 265 void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, TRAPS) {
 266   if (UseBiasedLocking) {
 267     if (!SafepointSynchronize::is_at_safepoint()) {


 278     // Anticipate successful CAS -- the ST of the displaced mark must
 279     // be visible <= the ST performed by the CAS.
 280     lock->set_displaced_header(mark);
 281     if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) {
 282       return;
 283     }
 284     // Fall through to inflate() ...
 285   } else if (mark.has_locker() &&
 286              THREAD->is_lock_owned((address)mark.locker())) {
 287     assert(lock != mark.locker(), "must not re-lock the same lock");
 288     assert(lock != (BasicLock*)obj->mark().value(), "don't relock with same BasicLock");
 289     lock->set_displaced_header(markWord::from_pointer(NULL));
 290     return;
 291   }
 292 
 293   // The object header will never be displaced to this lock,
 294   // so it does not matter what the value is, except that it
 295   // must be non-zero to avoid looking like a re-entrant lock,
 296   // and must not look locked either.
 297   lock->set_displaced_header(markWord::unused_mark());
 298   inflate(THREAD, obj(), inflate_cause_monitor_enter)->enter(THREAD);


 299 }
 300 
 301 void ObjectSynchronizer::exit(oop object, BasicLock* lock, TRAPS) {
 302   markWord mark = object->mark();
 303   // We cannot check for Biased Locking if we are racing an inflation.
 304   assert(mark == markWord::INFLATING() ||
 305          !mark.has_bias_pattern(), "should not see bias pattern here");
 306 
 307   markWord dhw = lock->displaced_header();
 308   if (dhw.value() == 0) {
 309     // If the displaced header is NULL, then this exit matches up with
 310     // a recursive enter. No real work to do here except for diagnostics.
 311 #ifndef PRODUCT
 312     if (mark != markWord::INFLATING()) {
 313       // Only do diagnostics if we are not racing an inflation. Simply
 314       // exiting a recursive enter of a Java Monitor that is being
 315       // inflated is safe; see the has_monitor() comment below.
 316       assert(!mark.is_neutral(), "invariant");
 317       assert(!mark.has_locker() ||
 318              THREAD->is_lock_owned((address)mark.locker()), "invariant");


 327         // does not own the Java Monitor.
 328         ObjectMonitor* m = mark.monitor();
 329         assert(((oop)(m->object()))->mark() == mark, "invariant");
 330         assert(m->is_entered(THREAD), "invariant");
 331       }
 332     }
 333 #endif
 334     return;
 335   }
 336 
 337   if (mark == markWord::from_pointer(lock)) {
 338     // If the object is stack-locked by the current thread, try to
 339     // swing the displaced header from the BasicLock back to the mark.
 340     assert(dhw.is_neutral(), "invariant");
 341     if (object->cas_set_mark(dhw, mark) == mark) {
 342       return;
 343     }
 344   }
 345 
 346   // We have to take the slow-path of possible inflation and then exit.
 347   inflate(THREAD, object, inflate_cause_vm_internal)->exit(true, THREAD);


 348 }
 349 
 350 // -----------------------------------------------------------------------------
 351 // Class Loader  support to workaround deadlocks on the class loader lock objects
 352 // Also used by GC
 353 // complete_exit()/reenter() are used to wait on a nested lock
 354 // i.e. to give up an outer lock completely and then re-enter
 355 // Used when holding nested locks - lock acquisition order: lock1 then lock2
 356 //  1) complete_exit lock1 - saving recursion count
 357 //  2) wait on lock2
 358 //  3) when notified on lock2, unlock lock2
 359 //  4) reenter lock1 with original recursion count
 360 //  5) lock lock2
 361 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 362 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
 363   if (UseBiasedLocking) {
 364     BiasedLocking::revoke(obj, THREAD);
 365     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 366   }
 367 
 368   ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
 369 
 370   return monitor->complete_exit(THREAD);

 371 }
 372 
 373 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 374 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
 375   if (UseBiasedLocking) {
 376     BiasedLocking::revoke(obj, THREAD);
 377     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 378   }
 379 
 380   ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
 381 
 382   monitor->reenter(recursion, THREAD);
 383 }
 384 // -----------------------------------------------------------------------------
 385 // JNI locks on java objects
 386 // NOTE: must use heavy weight monitor to handle jni monitor enter
 387 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) {
 388   // the current locking is from JNI instead of Java code
 389   if (UseBiasedLocking) {
 390     BiasedLocking::revoke(obj, THREAD);
 391     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 392   }
 393   THREAD->set_current_pending_monitor_is_from_java(false);
 394   inflate(THREAD, obj(), inflate_cause_jni_enter)->enter(THREAD);


 395   THREAD->set_current_pending_monitor_is_from_java(true);
 396 }
 397 
 398 // NOTE: must use heavy weight monitor to handle jni monitor exit
 399 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
 400   if (UseBiasedLocking) {
 401     Handle h_obj(THREAD, obj);
 402     BiasedLocking::revoke(h_obj, THREAD);
 403     obj = h_obj();
 404   }
 405   assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 406 
 407   ObjectMonitor* monitor = inflate(THREAD, obj, inflate_cause_jni_exit);


 408   // If this thread has locked the object, exit the monitor. We
 409   // intentionally do not use CHECK here because we must exit the
 410   // monitor even if an exception is pending.
 411   if (monitor->check_owner(THREAD)) {
 412     monitor->exit(true, THREAD);
 413   }
 414 }
 415 
 416 // -----------------------------------------------------------------------------
 417 // Internal VM locks on java objects
 418 // standard constructor, allows locking failures
 419 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool do_lock) {
 420   _dolock = do_lock;
 421   _thread = thread;
 422   _thread->check_for_valid_safepoint_state();
 423   _obj = obj;
 424 
 425   if (_dolock) {
 426     ObjectSynchronizer::enter(_obj, &_lock, _thread);
 427   }
 428 }
 429 
 430 ObjectLocker::~ObjectLocker() {
 431   if (_dolock) {
 432     ObjectSynchronizer::exit(_obj(), &_lock, _thread);
 433   }
 434 }
 435 
 436 
 437 // -----------------------------------------------------------------------------
 438 //  Wait/Notify/NotifyAll
 439 // NOTE: must use heavy weight monitor to handle wait()
 440 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
 441   if (UseBiasedLocking) {
 442     BiasedLocking::revoke(obj, THREAD);
 443     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 444   }
 445   if (millis < 0) {
 446     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 447   }
 448   ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_wait);


 449 
 450   DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
 451   monitor->wait(millis, true, THREAD);
 452 
 453   // This dummy call is in place to get around dtrace bug 6254741.  Once
 454   // that's fixed we can uncomment the following line, remove the call
 455   // and change this function back into a "void" func.
 456   // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
 457   return dtrace_waited_probe(monitor, obj, THREAD);

 458 }
 459 
 460 void ObjectSynchronizer::wait_uninterruptibly(Handle obj, jlong millis, TRAPS) {
 461   if (UseBiasedLocking) {
 462     BiasedLocking::revoke(obj, THREAD);
 463     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 464   }
 465   if (millis < 0) {
 466     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 467   }
 468   inflate(THREAD, obj(), inflate_cause_wait)->wait(millis, false, THREAD);


 469 }
 470 
 471 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
 472   if (UseBiasedLocking) {
 473     BiasedLocking::revoke(obj, THREAD);
 474     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 475   }
 476 
 477   markWord mark = obj->mark();
 478   if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
 479     return;
 480   }
 481   inflate(THREAD, obj(), inflate_cause_notify)->notify(THREAD);


 482 }
 483 
 484 // NOTE: see comment of notify()
 485 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
 486   if (UseBiasedLocking) {
 487     BiasedLocking::revoke(obj, THREAD);
 488     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 489   }
 490 
 491   markWord mark = obj->mark();
 492   if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
 493     return;
 494   }
 495   inflate(THREAD, obj(), inflate_cause_notify)->notifyAll(THREAD);


 496 }
 497 
 498 // -----------------------------------------------------------------------------
 499 // Hash Code handling
 500 //
 501 // Performance concern:
 502 // OrderAccess::storestore() calls release() which at one time stored 0
 503 // into the global volatile OrderAccess::dummy variable. This store was
 504 // unnecessary for correctness. Many threads storing into a common location
 505 // causes considerable cache migration or "sloshing" on large SMP systems.
 506 // As such, I avoided using OrderAccess::storestore(). In some cases
 507 // OrderAccess::fence() -- which incurs local latency on the executing
 508 // processor -- is a better choice as it scales on SMP systems.
 509 //
 510 // See http://blogs.oracle.com/dave/entry/biased_locking_in_hotspot for
 511 // a discussion of coherency costs. Note that all our current reference
 512 // platforms provide strong ST-ST order, so the issue is moot on IA32,
 513 // x64, and SPARC.
 514 //
 515 // As a general policy we use "volatile" to control compiler-based reordering
 516 // and explicit fences (barriers) to control for architectural reordering
 517 // performed by the CPU(s) or platform.
 518 
 519 struct SharedGlobals {
 520   char         _pad_prefix[DEFAULT_CACHE_LINE_SIZE];
 521   // These are highly shared mostly-read variables.
 522   // To avoid false-sharing they need to be the sole occupants of a cache line.
 523   volatile int stw_random;
 524   volatile int stw_cycle;
 525   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int) * 2);
 526   // Hot RW variable -- Sequester to avoid false-sharing
 527   volatile int hc_sequence;
 528   DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int));
 529 };
 530 
 531 static SharedGlobals GVars;
 532 static int MonitorScavengeThreshold = 1000000;
 533 static volatile int ForceMonitorScavenge = 0; // Scavenge required and pending
 534 
 535 static markWord read_stable_mark(oop obj) {
 536   markWord mark = obj->mark();
 537   if (!mark.is_being_inflated()) {
 538     return mark;       // normal fast-path return
 539   }
 540 
 541   int its = 0;
 542   for (;;) {
 543     markWord mark = obj->mark();
 544     if (!mark.is_being_inflated()) {
 545       return mark;    // normal fast-path return
 546     }
 547 
 548     // The object is being inflated by some other thread.


 669       Handle hobj(self, obj);
 670       // Relaxing assertion for bug 6320749.
 671       assert(Universe::verify_in_progress() ||
 672              !SafepointSynchronize::is_at_safepoint(),
 673              "biases should not be seen by VM thread here");
 674       BiasedLocking::revoke(hobj, JavaThread::current());
 675       obj = hobj();
 676       assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 677     }
 678   }
 679 
 680   // hashCode() is a heap mutator ...
 681   // Relaxing assertion for bug 6320749.
 682   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 683          !SafepointSynchronize::is_at_safepoint(), "invariant");
 684   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 685          self->is_Java_thread() , "invariant");
 686   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 687          ((JavaThread *)self)->thread_state() != _thread_blocked, "invariant");
 688 

 689   ObjectMonitor* monitor = NULL;
 690   markWord temp, test;
 691   intptr_t hash;
 692   markWord mark = read_stable_mark(obj);
 693 
 694   // object should remain ineligible for biased locking
 695   assert(!mark.has_bias_pattern(), "invariant");
 696 
 697   if (mark.is_neutral()) {
 698     hash = mark.hash();               // this is a normal header
 699     if (hash != 0) {                  // if it has hash, just return it
 700       return hash;
 701     }
 702     hash = get_next_hash(self, obj);  // allocate a new hash code
 703     temp = mark.copy_set_hash(hash);  // merge the hash code into header
 704     // use (machine word version) atomic operation to install the hash
 705     test = obj->cas_set_mark(temp, mark);
 706     if (test == mark) {
 707       return hash;
 708     }
 709     // If atomic operation failed, we must inflate the header
 710     // into heavy weight monitor. We could add more code here
 711     // for fast path, but it does not worth the complexity.
 712   } else if (mark.has_monitor()) {
 713     monitor = mark.monitor();






 714     temp = monitor->header();
 715     assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
 716     hash = temp.hash();
 717     if (hash != 0) {
 718       return hash;
 719     }
 720     // Skip to the following code to reduce code size
 721   } else if (self->is_lock_owned((address)mark.locker())) {
 722     temp = mark.displaced_mark_helper(); // this is a lightweight monitor owned
 723     assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
 724     hash = temp.hash();                  // by current thread, check if the displaced
 725     if (hash != 0) {                     // header contains hash code
 726       return hash;
 727     }
 728     // WARNING:
 729     // The displaced header in the BasicLock on a thread's stack
 730     // is strictly immutable. It CANNOT be changed in ANY cases.
 731     // So we have to inflate the stack lock into an ObjectMonitor
 732     // even if the current thread owns the lock. The BasicLock on
 733     // a thread's stack can be asynchronously read by other threads
 734     // during an inflate() call so any change to that stack memory
 735     // may not propagate to other threads correctly.
 736   }
 737 
 738   // Inflate the monitor to set hash code
 739   monitor = inflate(self, obj, inflate_cause_hash_code);


 740   // Load displaced header and check it has hash code
 741   mark = monitor->header();
 742   assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
 743   hash = mark.hash();
 744   if (hash == 0) {
 745     hash = get_next_hash(self, obj);
 746     temp = mark.copy_set_hash(hash); // merge hash code into header
 747     assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
 748     uintptr_t v = Atomic::cmpxchg(temp.value(), (volatile uintptr_t*)monitor->header_addr(), mark.value());
 749     test = markWord(v);
 750     if (test != mark) {
 751       // The only non-deflation update to the ObjectMonitor's
 752       // header/dmw field is to merge in the hash code. If someone
 753       // adds a new usage of the header/dmw field, please update
 754       // this code.




 755       hash = test.hash();
 756       assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
 757       assert(hash != 0, "Trivial unexpected object/monitor header usage.");
 758     }
 759   }
 760   // We finally get the hash
 761   return hash;

 762 }
 763 
 764 // Deprecated -- use FastHashCode() instead.
 765 
 766 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
 767   return FastHashCode(Thread::current(), obj());
 768 }
 769 
 770 
 771 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
 772                                                    Handle h_obj) {
 773   if (UseBiasedLocking) {
 774     BiasedLocking::revoke(h_obj, thread);
 775     assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
 776   }
 777 
 778   assert(thread == JavaThread::current(), "Can only be called on current thread");
 779   oop obj = h_obj();
 780 

 781   markWord mark = read_stable_mark(obj);
 782 
 783   // Uncontended case, header points to stack
 784   if (mark.has_locker()) {
 785     return thread->is_lock_owned((address)mark.locker());
 786   }
 787   // Contended case, header points to ObjectMonitor (tagged pointer)
 788   if (mark.has_monitor()) {
 789     ObjectMonitor* monitor = mark.monitor();
 790     return monitor->is_entered(thread) != 0;






 791   }
 792   // Unlocked case, header in place
 793   assert(mark.is_neutral(), "sanity check");
 794   return false;

 795 }
 796 
 797 // Be aware of this method could revoke bias of the lock object.
 798 // This method queries the ownership of the lock handle specified by 'h_obj'.
 799 // If the current thread owns the lock, it returns owner_self. If no
 800 // thread owns the lock, it returns owner_none. Otherwise, it will return
 801 // owner_other.
 802 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
 803 (JavaThread *self, Handle h_obj) {
 804   // The caller must beware this method can revoke bias, and
 805   // revocation can result in a safepoint.
 806   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 807   assert(self->thread_state() != _thread_blocked, "invariant");
 808 
 809   // Possible mark states: neutral, biased, stack-locked, inflated
 810 
 811   if (UseBiasedLocking && h_obj()->mark().has_bias_pattern()) {
 812     // CASE: biased
 813     BiasedLocking::revoke(h_obj, self);
 814     assert(!h_obj->mark().has_bias_pattern(),
 815            "biases should be revoked by now");
 816   }
 817 
 818   assert(self == JavaThread::current(), "Can only be called on current thread");
 819   oop obj = h_obj();


 820   markWord mark = read_stable_mark(obj);
 821 
 822   // CASE: stack-locked.  Mark points to a BasicLock on the owner's stack.
 823   if (mark.has_locker()) {
 824     return self->is_lock_owned((address)mark.locker()) ?
 825       owner_self : owner_other;
 826   }
 827 
 828   // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor.
 829   // The Object:ObjectMonitor relationship is stable as long as we're
 830   // not at a safepoint.
 831   if (mark.has_monitor()) {
 832     void* owner = mark.monitor()->_owner;







 833     if (owner == NULL) return owner_none;
 834     return (owner == self ||
 835             self->is_lock_owned((address)owner)) ? owner_self : owner_other;
 836   }
 837 
 838   // CASE: neutral
 839   assert(mark.is_neutral(), "sanity check");
 840   return owner_none;           // it's unlocked

 841 }
 842 
 843 // FIXME: jvmti should call this
 844 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
 845   if (UseBiasedLocking) {
 846     if (SafepointSynchronize::is_at_safepoint()) {
 847       BiasedLocking::revoke_at_safepoint(h_obj);
 848     } else {
 849       BiasedLocking::revoke(h_obj, JavaThread::current());
 850     }
 851     assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
 852   }
 853 
 854   oop obj = h_obj();
 855   address owner = NULL;
 856 


 857   markWord mark = read_stable_mark(obj);
 858 
 859   // Uncontended case, header points to stack
 860   if (mark.has_locker()) {
 861     owner = (address) mark.locker();
 862   }
 863 
 864   // Contended case, header points to ObjectMonitor (tagged pointer)
 865   else if (mark.has_monitor()) {
 866     ObjectMonitor* monitor = mark.monitor();






 867     assert(monitor != NULL, "monitor should be non-null");
 868     owner = (address) monitor->owner();
 869   }
 870 
 871   if (owner != NULL) {
 872     // owning_thread_from_monitor_owner() may also return NULL here
 873     return Threads::owning_thread_from_monitor_owner(t_list, owner);
 874   }
 875 
 876   // Unlocked case, header in place
 877   // Cannot have assertion since this object may have been
 878   // locked by another thread when reaching here.
 879   // assert(mark.is_neutral(), "sanity check");
 880 
 881   return NULL;

 882 }
 883 
 884 // Visitors ...
 885 
 886 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
 887   PaddedObjectMonitor* block = OrderAccess::load_acquire(&g_block_list);
 888   while (block != NULL) {
 889     assert(block->object() == CHAINMARKER, "must be a block header");
 890     for (int i = _BLOCKSIZE - 1; i > 0; i--) {
 891       ObjectMonitor* mid = (ObjectMonitor *)(block + i);
 892       oop object = (oop)mid->object();
 893       if (object != NULL) {


 894         // Only process with closure if the object is set.


 895         closure->do_monitor(mid);
 896       }
 897     }



 898     block = (PaddedObjectMonitor*)block->_next_om;
 899   }
 900 }
 901 
 902 static bool monitors_used_above_threshold() {
 903   if (g_om_population == 0) {
 904     return false;
 905   }

 906   int monitors_used = g_om_population - g_om_free_count;



 907   int monitor_usage = (monitors_used * 100LL) / g_om_population;
 908   return monitor_usage > MonitorUsedDeflationThreshold;


 909 }
 910 
 911 bool ObjectSynchronizer::is_cleanup_needed() {
 912   if (MonitorUsedDeflationThreshold > 0) {
 913     return monitors_used_above_threshold();






































 914   }
 915   return false;











 916 }
 917 
 918 void ObjectSynchronizer::oops_do(OopClosure* f) {
 919   // We only scan the global used list here (for moribund threads), and
 920   // the thread-local monitors in Thread::oops_do().
 921   global_used_oops_do(f);
 922 }
 923 
 924 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) {
 925   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 926   list_oops_do(g_om_in_use_list, f);
 927 }
 928 
 929 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) {
 930   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 931   list_oops_do(thread->om_in_use_list, f);
 932 }
 933 
 934 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, OopClosure* f) {
 935   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 936   ObjectMonitor* mid;
 937   for (mid = list; mid != NULL; mid = mid->_next_om) {



 938     if (mid->object() != NULL) {
 939       f->do_oop((oop*)mid->object_addr());
 940     }
 941   }
 942 }
 943 
 944 
 945 // -----------------------------------------------------------------------------
 946 // ObjectMonitor Lifecycle
 947 // -----------------------
 948 // Inflation unlinks monitors from the global g_free_list and
 949 // associates them with objects.  Deflation -- which occurs at
 950 // STW-time -- disassociates idle monitors from objects.  Such
 951 // scavenged monitors are returned to the g_free_list.
 952 //
 953 // The global list is protected by gListLock.  All the critical sections
 954 // are short and operate in constant-time.
 955 //
 956 // ObjectMonitors reside in type-stable memory (TSM) and are immortal.
 957 //
 958 // Lifecycle:
 959 // --   unassigned and on the global free list
 960 // --   unassigned and on a thread's private om_free_list
 961 // --   assigned to an object.  The object is inflated and the mark refers
 962 //      to the objectmonitor.
 963 
 964 
 965 // Constraining monitor pool growth via MonitorBound ...
 966 //
 967 // If MonitorBound is not set (<= 0), MonitorBound checks are disabled.
 968 //

 969 // The monitor pool is grow-only.  We scavenge at STW safepoint-time, but the
 970 // the rate of scavenging is driven primarily by GC.  As such,  we can find
 971 // an inordinate number of monitors in circulation.
 972 // To avoid that scenario we can artificially induce a STW safepoint
 973 // if the pool appears to be growing past some reasonable bound.
 974 // Generally we favor time in space-time tradeoffs, but as there's no
 975 // natural back-pressure on the # of extant monitors we need to impose some
 976 // type of limit.  Beware that if MonitorBound is set to too low a value
 977 // we could just loop. In addition, if MonitorBound is set to a low value
 978 // we'll incur more safepoints, which are harmful to performance.
 979 // See also: GuaranteedSafepointInterval
 980 //
 981 // The current implementation uses asynchronous VM operations.
 982 //
 983 // If MonitorBound is set, the boundry applies to

 984 //     (g_om_population - g_om_free_count)
 985 // i.e., if there are not enough ObjectMonitors on the global free list,
 986 // then a safepoint deflation is induced. Picking a good MonitorBound value
 987 // is non-trivial.










 988 
 989 static void InduceScavenge(Thread* self, const char * Whence) {


 990   // Induce STW safepoint to trim monitors
 991   // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
 992   // More precisely, trigger an asynchronous STW safepoint as the number
 993   // of active monitors passes the specified threshold.
 994   // TODO: assert thread state is reasonable
 995 
 996   if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
 997     // Induce a 'null' safepoint to scavenge monitors
 998     // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
 999     // to the VMthread and have a lifespan longer than that of this activation record.
1000     // The VMThread will delete the op when completed.
1001     VMThread::execute(new VM_ScavengeMonitors());
1002   }
1003 }
1004 
1005 ObjectMonitor* ObjectSynchronizer::om_alloc(Thread* self) {

1006   // A large MAXPRIVATE value reduces both list lock contention
1007   // and list coherency traffic, but also tends to increase the
1008   // number of ObjectMonitors in circulation as well as the STW
1009   // scavenge costs.  As usual, we lean toward time in space-time
1010   // tradeoffs.
1011   const int MAXPRIVATE = 1024;

1012   stringStream ss;
1013   for (;;) {
1014     ObjectMonitor* m;
1015 
1016     // 1: try to allocate from the thread's local om_free_list.
1017     // Threads will attempt to allocate first from their local list, then
1018     // from the global list, and only after those attempts fail will the thread
1019     // attempt to instantiate new monitors.   Thread-local free lists take
1020     // heat off the gListLock and improve allocation latency, as well as reducing
1021     // coherency traffic on the shared global list.
1022     m = self->om_free_list;
1023     if (m != NULL) {
1024       self->om_free_list = m->_next_om;
1025       self->om_free_count--;
1026       guarantee(m->object() == NULL, "invariant");
1027       m->_next_om = self->om_in_use_list;
1028       self->om_in_use_list = m;
1029       self->om_in_use_count++;
1030       return m;
1031     }
1032 
1033     // 2: try to allocate from the global g_free_list
1034     // CONSIDER: use muxTry() instead of muxAcquire().
1035     // If the muxTry() fails then drop immediately into case 3.
1036     // If we're using thread-local free lists then try
1037     // to reprovision the caller's free list.
1038     if (g_free_list != NULL) {
1039       // Reprovision the thread's om_free_list.
1040       // Use bulk transfers to reduce the allocation rate and heat
1041       // on various locks.
1042       Thread::muxAcquire(&gListLock, "om_alloc(1)");
1043       for (int i = self->om_free_provision; --i >= 0 && g_free_list != NULL;) {
1044         g_om_free_count--;
1045         ObjectMonitor* take = g_free_list;
1046         g_free_list = take->_next_om;
1047         guarantee(take->object() == NULL, "invariant");

















1048         take->Recycle();




1049         om_release(self, take, false);
1050       }
1051       Thread::muxRelease(&gListLock);
1052       self->om_free_provision += 1 + (self->om_free_provision/2);
1053       if (self->om_free_provision > MAXPRIVATE) self->om_free_provision = MAXPRIVATE;
1054 
1055       const int mx = MonitorBound;
1056       if (mx > 0 && (g_om_population-g_om_free_count) > mx) {
1057         // Not enough ObjectMonitors on the global free list.
1058         // We can't safely induce a STW safepoint from om_alloc() as our thread
1059         // state may not be appropriate for such activities and callers may hold
1060         // naked oops, so instead we defer the action.
1061         InduceScavenge(self, "om_alloc");
1062       }
1063       continue;
1064     }
1065 
1066     // 3: allocate a block of new ObjectMonitors
1067     // Both the local and global free lists are empty -- resort to malloc().
1068     // In the current implementation ObjectMonitors are TSM - immortal.
1069     // Ideally, we'd write "new ObjectMonitor[_BLOCKSIZE], but we want
1070     // each ObjectMonitor to start at the beginning of a cache line,
1071     // so we use align_up().
1072     // A better solution would be to use C++ placement-new.
1073     // BEWARE: As it stands currently, we don't run the ctors!
1074     assert(_BLOCKSIZE > 1, "invariant");
1075     size_t neededsize = sizeof(PaddedObjectMonitor) * _BLOCKSIZE;
1076     PaddedObjectMonitor* temp;
1077     size_t aligned_size = neededsize + (DEFAULT_CACHE_LINE_SIZE - 1);
1078     void* real_malloc_addr = NEW_C_HEAP_ARRAY(char, aligned_size, mtInternal);
1079     temp = (PaddedObjectMonitor*)align_up(real_malloc_addr, DEFAULT_CACHE_LINE_SIZE);
1080     (void)memset((void *) temp, 0, neededsize);
1081 
1082     // Format the block.
1083     // initialize the linked list, each monitor points to its next
1084     // forming the single linked free list, the very first monitor
1085     // will points to next block, which forms the block list.
1086     // The trick of using the 1st element in the block as g_block_list
1087     // linkage should be reconsidered.  A better implementation would
1088     // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
1089 
1090     for (int i = 1; i < _BLOCKSIZE; i++) {
1091       temp[i]._next_om = (ObjectMonitor *)&temp[i+1];

1092     }
1093 
1094     // terminate the last monitor as the end of list
1095     temp[_BLOCKSIZE - 1]._next_om = NULL;
1096 
1097     // Element [0] is reserved for global list linkage
1098     temp[0].set_object(CHAINMARKER);
1099 
1100     // Consider carving out this thread's current request from the
1101     // block in hand.  This avoids some lock traffic and redundant
1102     // list activity.
1103 
1104     // Acquire the gListLock to manipulate g_block_list and g_free_list.
1105     // An Oyama-Taura-Yonezawa scheme might be more efficient.
1106     Thread::muxAcquire(&gListLock, "om_alloc(2)");
1107     g_om_population += _BLOCKSIZE-1;
1108     g_om_free_count += _BLOCKSIZE-1;
1109 
1110     // Add the new block to the list of extant blocks (g_block_list).
1111     // The very first ObjectMonitor in a block is reserved and dedicated.
1112     // It serves as blocklist "next" linkage.
1113     temp[0]._next_om = g_block_list;
1114     // There are lock-free uses of g_block_list so make sure that
1115     // the previous stores happen before we update g_block_list.
1116     OrderAccess::release_store(&g_block_list, temp);
1117 
1118     // Add the new string of ObjectMonitors to the global free list
1119     temp[_BLOCKSIZE - 1]._next_om = g_free_list;
1120     g_free_list = temp + 1;
1121     Thread::muxRelease(&gListLock);
1122   }
1123 }
1124 
1125 // Place "m" on the caller's private per-thread om_free_list.
1126 // In practice there's no need to clamp or limit the number of
1127 // monitors on a thread's om_free_list as the only non-allocation time
1128 // we'll call om_release() is to return a monitor to the free list after
1129 // a CAS attempt failed. This doesn't allow unbounded #s of monitors to
1130 // accumulate on a thread's free list.
1131 //
1132 // Key constraint: all ObjectMonitors on a thread's free list and the global
1133 // free list must have their object field set to null. This prevents the
1134 // scavenger -- deflate_monitor_list() -- from reclaiming them while we
1135 // are trying to release them.
1136 
1137 void ObjectSynchronizer::om_release(Thread* self, ObjectMonitor* m,
1138                                     bool from_per_thread_alloc) {
1139   guarantee(m->header().value() == 0, "invariant");
1140   guarantee(m->object() == NULL, "invariant");
1141   stringStream ss;
1142   guarantee((m->is_busy() | m->_recursions) == 0, "freeing in-use monitor: "
1143             "%s, recursions=" INTPTR_FORMAT, m->is_busy_to_string(&ss),
1144             m->_recursions);

1145   // _next_om is used for both per-thread in-use and free lists so
1146   // we have to remove 'm' from the in-use list first (as needed).
1147   if (from_per_thread_alloc) {
1148     // Need to remove 'm' from om_in_use_list.


1149     ObjectMonitor* cur_mid_in_use = NULL;


1150     bool extracted = false;
1151     for (ObjectMonitor* mid = self->om_in_use_list; mid != NULL; cur_mid_in_use = mid, mid = mid->_next_om) {




1152       if (m == mid) {
1153         // extract from per-thread in-use list
1154         if (mid == self->om_in_use_list) {
1155           self->om_in_use_list = mid->_next_om;
1156         } else if (cur_mid_in_use != NULL) {
1157           cur_mid_in_use->_next_om = mid->_next_om; // maintain the current thread in-use list







1158         }
1159         extracted = true;
1160         self->om_in_use_count--;




1161         break;
1162       }















1163     }
1164     assert(extracted, "Should have extracted from in-use list");
1165   }
1166 
1167   m->_next_om = self->om_free_list;
1168   self->om_free_list = m;
1169   self->om_free_count++;
1170 }
1171 
1172 // Return ObjectMonitors on a moribund thread's free and in-use
1173 // lists to the appropriate global lists. The ObjectMonitors on the
1174 // per-thread in-use list may still be in use by other threads.
1175 //
1176 // We currently call om_flush() from Threads::remove() before the
1177 // thread has been excised from the thread list and is no longer a
1178 // mutator. This means that om_flush() cannot run concurrently with
1179 // a safepoint and interleave with deflate_idle_monitors(). In
1180 // particular, this ensures that the thread's in-use monitors are
1181 // scanned by a GC safepoint, either via Thread::oops_do() (before
1182 // om_flush() is called) or via ObjectSynchronizer::oops_do() (after
1183 // om_flush() is called).





1184 
1185 void ObjectSynchronizer::om_flush(Thread* self) {

































































1186   ObjectMonitor* free_list = self->om_free_list;
1187   ObjectMonitor* free_tail = NULL;
1188   int free_count = 0;
1189   if (free_list != NULL) {
1190     ObjectMonitor* s;
1191     // The thread is going away. Set 'free_tail' to the last per-thread free
1192     // monitor which will be linked to g_free_list below under the gListLock.
1193     stringStream ss;
1194     for (s = free_list; s != NULL; s = s->_next_om) {
1195       free_count++;
1196       free_tail = s;
1197       guarantee(s->object() == NULL, "invariant");
1198       guarantee(!s->is_busy(), "must be !is_busy: %s", s->is_busy_to_string(&ss));
1199     }
1200     guarantee(free_tail != NULL, "invariant");
1201     assert(self->om_free_count == free_count, "free-count off");
1202     self->om_free_list = NULL;


1203     self->om_free_count = 0;


1204   }
1205 
1206   ObjectMonitor* in_use_list = self->om_in_use_list;
1207   ObjectMonitor* in_use_tail = NULL;
1208   int in_use_count = 0;
1209   if (in_use_list != NULL) {
1210     // The thread is going away, however the ObjectMonitors on the
1211     // om_in_use_list may still be in-use by other threads. Link
1212     // them to in_use_tail, which will be linked into the global
1213     // in-use list g_om_in_use_list below, under the gListLock.
1214     ObjectMonitor *cur_om;
1215     for (cur_om = in_use_list; cur_om != NULL; cur_om = cur_om->_next_om) {
1216       in_use_tail = cur_om;
1217       in_use_count++;
1218     }
1219     guarantee(in_use_tail != NULL, "invariant");
1220     assert(self->om_in_use_count == in_use_count, "in-use count off");
1221     self->om_in_use_list = NULL;
1222     self->om_in_use_count = 0;
1223   }
1224 
1225   Thread::muxAcquire(&gListLock, "om_flush");
1226   if (free_tail != NULL) {
1227     free_tail->_next_om = g_free_list;
1228     g_free_list = free_list;
1229     g_om_free_count += free_count;
1230   }
1231 
1232   if (in_use_tail != NULL) {
1233     in_use_tail->_next_om = g_om_in_use_list;
1234     g_om_in_use_list = in_use_list;
1235     g_om_in_use_count += in_use_count;
1236   }
1237 
1238   Thread::muxRelease(&gListLock);
1239 
1240   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1241   LogStreamHandle(Info, monitorinflation) lsh_info;
1242   LogStream* ls = NULL;
1243   if (log_is_enabled(Debug, monitorinflation)) {
1244     ls = &lsh_debug;
1245   } else if ((free_count != 0 || in_use_count != 0) &&
1246              log_is_enabled(Info, monitorinflation)) {
1247     ls = &lsh_info;
1248   }
1249   if (ls != NULL) {
1250     ls->print_cr("om_flush: jt=" INTPTR_FORMAT ", free_count=%d"
1251                  ", in_use_count=%d" ", om_free_provision=%d",
1252                  p2i(self), free_count, in_use_count, self->om_free_provision);
1253   }
1254 }
1255 
1256 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1257                                        const oop obj,
1258                                        ObjectSynchronizer::InflateCause cause) {
1259   assert(event != NULL, "invariant");
1260   assert(event->should_commit(), "invariant");
1261   event->set_monitorClass(obj->klass());
1262   event->set_address((uintptr_t)(void*)obj);
1263   event->set_cause((u1)cause);
1264   event->commit();
1265 }
1266 
1267 // Fast path code shared by multiple functions
1268 void ObjectSynchronizer::inflate_helper(oop obj) {

1269   markWord mark = obj->mark();
1270   if (mark.has_monitor()) {
1271     assert(ObjectSynchronizer::verify_objmon_isinpool(mark.monitor()), "monitor is invalid");
1272     assert(mark.monitor()->header().is_neutral(), "monitor must record a good object header");










1273     return;
1274   }
1275   inflate(Thread::current(), obj, inflate_cause_vm_internal);
1276 }
1277 
1278 ObjectMonitor* ObjectSynchronizer::inflate(Thread* self,
1279                                            oop object,
1280                                            const InflateCause cause) {
1281   // Inflate mutates the heap ...
1282   // Relaxing assertion for bug 6320749.
1283   assert(Universe::verify_in_progress() ||
1284          !SafepointSynchronize::is_at_safepoint(), "invariant");
1285 
1286   EventJavaMonitorInflate event;
1287 
1288   for (;;) {
1289     const markWord mark = object->mark();
1290     assert(!mark.has_bias_pattern(), "invariant");
1291 
1292     // The mark can be in one of the following states:
1293     // *  Inflated     - just return
1294     // *  Stack-locked - coerce it to inflated
1295     // *  INFLATING    - busy wait for conversion to complete
1296     // *  Neutral      - aggressively inflate the object.
1297     // *  BIASED       - Illegal.  We should never see this
1298 
1299     // CASE: inflated
1300     if (mark.has_monitor()) {
1301       ObjectMonitor* inf = mark.monitor();





1302       markWord dmw = inf->header();
1303       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1304       assert(inf->object() == object, "invariant");
1305       assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1306       return inf;
1307     }
1308 
1309     // CASE: inflation in progress - inflating over a stack-lock.
1310     // Some other thread is converting from stack-locked to inflated.
1311     // Only that thread can complete inflation -- other threads must wait.
1312     // The INFLATING value is transient.
1313     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1314     // We could always eliminate polling by parking the thread on some auxiliary list.
1315     if (mark == markWord::INFLATING()) {
1316       read_stable_mark(object);
1317       continue;
1318     }
1319 
1320     // CASE: stack-locked
1321     // Could be stack-locked either by this thread or by some other thread.
1322     //
1323     // Note that we allocate the objectmonitor speculatively, _before_ attempting
1324     // to install INFLATING into the mark word.  We originally installed INFLATING,
1325     // allocated the objectmonitor, and then finally STed the address of the
1326     // objectmonitor into the mark.  This was correct, but artificially lengthened
1327     // the interval in which INFLATED appeared in the mark, thus increasing
1328     // the odds of inflation contention.
1329     //
1330     // We now use per-thread private objectmonitor free lists.
1331     // These list are reprovisioned from the global free list outside the
1332     // critical INFLATING...ST interval.  A thread can transfer
1333     // multiple objectmonitors en-mass from the global free list to its local free list.
1334     // This reduces coherency traffic and lock contention on the global free list.
1335     // Using such local free lists, it doesn't matter if the om_alloc() call appears
1336     // before or after the CAS(INFLATING) operation.
1337     // See the comments in om_alloc().
1338 
1339     LogStreamHandle(Trace, monitorinflation) lsh;
1340 
1341     if (mark.has_locker()) {
1342       ObjectMonitor* m = om_alloc(self);
1343       // Optimistically prepare the objectmonitor - anticipate successful CAS
1344       // We do this before the CAS in order to minimize the length of time
1345       // in which INFLATING appears in the mark.
1346       m->Recycle();
1347       m->_Responsible  = NULL;
1348       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;   // Consider: maintain by type/class
1349 
1350       markWord cmp = object->cas_set_mark(markWord::INFLATING(), mark);
1351       if (cmp != mark) {

1352         om_release(self, m, true);
1353         continue;       // Interference -- just retry
1354       }
1355 
1356       // We've successfully installed INFLATING (0) into the mark-word.
1357       // This is the only case where 0 will appear in a mark-word.
1358       // Only the singular thread that successfully swings the mark-word
1359       // to 0 can perform (or more precisely, complete) inflation.
1360       //
1361       // Why do we CAS a 0 into the mark-word instead of just CASing the
1362       // mark-word from the stack-locked value directly to the new inflated state?
1363       // Consider what happens when a thread unlocks a stack-locked object.
1364       // It attempts to use CAS to swing the displaced header value from the
1365       // on-stack BasicLock back into the object header.  Recall also that the
1366       // header value (hash code, etc) can reside in (a) the object header, or
1367       // (b) a displaced header associated with the stack-lock, or (c) a displaced
1368       // header in an ObjectMonitor.  The inflate() routine must copy the header
1369       // value from the BasicLock on the owner's stack to the ObjectMonitor, all
1370       // the while preserving the hashCode stability invariants.  If the owner
1371       // decides to release the lock while the value is 0, the unlock will fail
1372       // and control will eventually pass from slow_exit() to inflate.  The owner
1373       // will then spin, waiting for the 0 value to disappear.   Put another way,
1374       // the 0 causes the owner to stall if the owner happens to try to
1375       // drop the lock (restoring the header from the BasicLock to the object)
1376       // while inflation is in-progress.  This protocol avoids races that might
1377       // would otherwise permit hashCode values to change or "flicker" for an object.
1378       // Critically, while object->mark is 0 mark.displaced_mark_helper() is stable.
1379       // 0 serves as a "BUSY" inflate-in-progress indicator.
1380 
1381 
1382       // fetch the displaced mark from the owner's stack.
1383       // The owner can't die or unwind past the lock while our INFLATING
1384       // object is in the mark.  Furthermore the owner can't complete
1385       // an unlock on the object, either.
1386       markWord dmw = mark.displaced_mark_helper();
1387       // Catch if the object's header is not neutral (not locked and
1388       // not marked is what we care about here).
1389       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1390 
1391       // Setup monitor fields to proper values -- prepare the monitor
1392       m->set_header(dmw);
1393 
1394       // Optimization: if the mark.locker stack address is associated
1395       // with this thread we could simply set m->_owner = self.
1396       // Note that a thread can inflate an object
1397       // that it has stack-locked -- as might happen in wait() -- directly
1398       // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1399       m->set_owner(mark.locker());




1400       m->set_object(object);
1401       // TODO-FIXME: assert BasicLock->dhw != 0.
1402 


1403       // Must preserve store ordering. The monitor state must
1404       // be stable at the time of publishing the monitor address.
1405       guarantee(object->mark() == markWord::INFLATING(), "invariant");
1406       object->release_set_mark(markWord::encode(m));
1407 





1408       // Hopefully the performance counters are allocated on distinct cache lines
1409       // to avoid false sharing on MP systems ...
1410       OM_PERFDATA_OP(Inflations, inc());
1411       if (log_is_enabled(Trace, monitorinflation)) {
1412         ResourceMark rm(self);
1413         lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1414                      INTPTR_FORMAT ", type='%s'", p2i(object),
1415                      object->mark().value(), object->klass()->external_name());
1416       }
1417       if (event.should_commit()) {
1418         post_monitor_inflate_event(&event, object, cause);
1419       }
1420       return m;

1421     }
1422 
1423     // CASE: neutral
1424     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1425     // If we know we're inflating for entry it's better to inflate by swinging a
1426     // pre-locked ObjectMonitor pointer into the object header.   A successful
1427     // CAS inflates the object *and* confers ownership to the inflating thread.
1428     // In the current implementation we use a 2-step mechanism where we CAS()
1429     // to inflate and then CAS() again to try to swing _owner from NULL to self.
1430     // An inflateTry() method that we could call from enter() would be useful.
1431 
1432     // Catch if the object's header is not neutral (not locked and
1433     // not marked is what we care about here).
1434     assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1435     ObjectMonitor* m = om_alloc(self);
1436     // prepare m for installation - set monitor to initial state
1437     m->Recycle();
1438     m->set_header(mark);



1439     m->set_object(object);
1440     m->_Responsible  = NULL;
1441     m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;       // consider: keep metastats by type/class
1442 


1443     if (object->cas_set_mark(markWord::encode(m), mark) != mark) {
1444       m->set_header(markWord::zero());
1445       m->set_object(NULL);
1446       m->Recycle();


1447       om_release(self, m, true);
1448       m = NULL;
1449       continue;
1450       // interference - the markword changed - just retry.
1451       // The state-transitions are one-way, so there's no chance of
1452       // live-lock -- "Inflated" is an absorbing state.
1453     }
1454 





1455     // Hopefully the performance counters are allocated on distinct
1456     // cache lines to avoid false sharing on MP systems ...
1457     OM_PERFDATA_OP(Inflations, inc());
1458     if (log_is_enabled(Trace, monitorinflation)) {
1459       ResourceMark rm(self);
1460       lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
1461                    INTPTR_FORMAT ", type='%s'", p2i(object),
1462                    object->mark().value(), object->klass()->external_name());
1463     }
1464     if (event.should_commit()) {
1465       post_monitor_inflate_event(&event, object, cause);
1466     }
1467     return m;

1468   }
1469 }
1470 
1471 
1472 // We maintain a list of in-use monitors for each thread.
1473 //

1474 // deflate_thread_local_monitors() scans a single thread's in-use list, while
1475 // deflate_idle_monitors() scans only a global list of in-use monitors which
1476 // is populated only as a thread dies (see om_flush()).
1477 //
1478 // These operations are called at all safepoints, immediately after mutators
1479 // are stopped, but before any objects have moved. Collectively they traverse
1480 // the population of in-use monitors, deflating where possible. The scavenged
1481 // monitors are returned to the global monitor free list.
1482 //
1483 // Beware that we scavenge at *every* stop-the-world point. Having a large
1484 // number of monitors in-use could negatively impact performance. We also want
1485 // to minimize the total # of monitors in circulation, as they incur a small
1486 // footprint penalty.
1487 //
1488 // Perversely, the heap size -- and thus the STW safepoint rate --
1489 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
1490 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
1491 // This is an unfortunate aspect of this design.


































1492 
1493 // Deflate a single monitor if not in-use
1494 // Return true if deflated, false if in-use
1495 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1496                                          ObjectMonitor** free_head_p,
1497                                          ObjectMonitor** free_tail_p) {
1498   bool deflated;
1499   // Normal case ... The monitor is associated with obj.
1500   const markWord mark = obj->mark();
1501   guarantee(mark == markWord::encode(mid), "should match: mark="
1502             INTPTR_FORMAT ", encoded mid=" INTPTR_FORMAT, mark.value(),
1503             markWord::encode(mid).value());
1504   // Make sure that mark.monitor() and markWord::encode() agree:
1505   guarantee(mark.monitor() == mid, "should match: monitor()=" INTPTR_FORMAT
1506             ", mid=" INTPTR_FORMAT, p2i(mark.monitor()), p2i(mid));
1507   const markWord dmw = mid->header();
1508   guarantee(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1509 
1510   if (mid->is_busy()) {


1511     deflated = false;
1512   } else {
1513     // Deflate the monitor if it is no longer being used
1514     // It's idle - scavenge and return to the global free list
1515     // plain old deflation ...
1516     if (log_is_enabled(Trace, monitorinflation)) {
1517       ResourceMark rm;
1518       log_trace(monitorinflation)("deflate_monitor: "
1519                                   "object=" INTPTR_FORMAT ", mark="
1520                                   INTPTR_FORMAT ", type='%s'", p2i(obj),
1521                                   mark.value(), obj->klass()->external_name());
1522     }
1523 
1524     // Restore the header back to obj
1525     obj->release_set_mark(dmw);







1526     mid->clear();
1527 
1528     assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT,
1529            p2i(mid->object()));

1530 
1531     // Move the deflated ObjectMonitor to the working free list
1532     // defined by free_head_p and free_tail_p.

1533     if (*free_head_p == NULL) *free_head_p = mid;
1534     if (*free_tail_p != NULL) {
1535       // We append to the list so the caller can use mid->_next_om
1536       // to fix the linkages in its context.
1537       ObjectMonitor* prevtail = *free_tail_p;
1538       // Should have been cleaned up by the caller:
1539       assert(prevtail->_next_om == NULL, "cleaned up deflated?");
1540       prevtail->_next_om = mid;




1541     }
1542     *free_tail_p = mid;
1543     // At this point, mid->_next_om still refers to its current
1544     // value and another ObjectMonitor's _next_om field still
1545     // refers to this ObjectMonitor. Those linkages have to be
1546     // cleaned up by the caller who has the complete context.
1547     deflated = true;
1548   }
1549   return deflated;
1550 }
1551 
1552 // Walk a given monitor list, and deflate idle monitors
1553 // The given list could be a per-thread list or a global list
1554 // Caller acquires gListLock as needed.














































































































































1555 //
1556 // In the case of parallel processing of thread local monitor lists,
1557 // work is done by Threads::parallel_threads_do() which ensures that
1558 // each Java thread is processed by exactly one worker thread, and
1559 // thus avoid conflicts that would arise when worker threads would
1560 // process the same monitor lists concurrently.
1561 //
1562 // See also ParallelSPCleanupTask and
1563 // SafepointSynchronize::do_cleanup_tasks() in safepoint.cpp and
1564 // Threads::parallel_java_threads_do() in thread.cpp.
1565 int ObjectSynchronizer::deflate_monitor_list(ObjectMonitor** list_p,

1566                                              ObjectMonitor** free_head_p,
1567                                              ObjectMonitor** free_tail_p) {
1568   ObjectMonitor* mid;
1569   ObjectMonitor* next;
1570   ObjectMonitor* cur_mid_in_use = NULL;


1571   int deflated_count = 0;
1572 
1573   for (mid = *list_p; mid != NULL;) {






1574     oop obj = (oop) mid->object();
1575     if (obj != NULL && deflate_monitor(mid, obj, free_head_p, free_tail_p)) {
1576       // Deflation succeeded and already updated free_head_p and
1577       // free_tail_p as needed. Finish the move to the local free list
1578       // by unlinking mid from the global or per-thread in-use list.
1579       if (mid == *list_p) {
1580         *list_p = mid->_next_om;
1581       } else if (cur_mid_in_use != NULL) {
1582         cur_mid_in_use->_next_om = mid->_next_om; // maintain the current thread in-use list







1583       }
1584       next = mid->_next_om;
1585       mid->_next_om = NULL;  // This mid is current tail in the free_head_p list
1586       mid = next;
1587       deflated_count++;







1588     } else {
























































































































1589       cur_mid_in_use = mid;
1590       mid = mid->_next_om;














1591     }



1592   }









1593   return deflated_count;
1594 }
1595 
1596 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) {
1597   counters->n_in_use = 0;              // currently associated with objects
1598   counters->n_in_circulation = 0;      // extant
1599   counters->n_scavenged = 0;           // reclaimed (global and per-thread)
1600   counters->per_thread_scavenged = 0;  // per-thread scavenge total
1601   counters->per_thread_times = 0.0;    // per-thread scavenge times

1602 }
1603 
1604 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) {
1605   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");









1606   bool deflated = false;
1607 
1608   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
1609   ObjectMonitor* free_tail_p = NULL;
1610   elapsedTimer timer;
1611 
1612   if (log_is_enabled(Info, monitorinflation)) {
1613     timer.start();
1614   }
1615 
1616   // Prevent om_flush from changing mids in Thread dtor's during deflation
1617   // And in case the vm thread is acquiring a lock during a safepoint
1618   // See e.g. 6320749
1619   Thread::muxAcquire(&gListLock, "deflate_idle_monitors");
1620 
1621   // Note: the thread-local monitors lists get deflated in
1622   // a separate pass. See deflate_thread_local_monitors().
1623 
1624   // For moribund threads, scan g_om_in_use_list
1625   int deflated_count = 0;
1626   if (g_om_in_use_list) {
1627     counters->n_in_circulation += g_om_in_use_count;
1628     deflated_count = deflate_monitor_list((ObjectMonitor **)&g_om_in_use_list, &free_head_p, &free_tail_p);
1629     g_om_in_use_count -= deflated_count;
1630     counters->n_scavenged += deflated_count;
1631     counters->n_in_use += g_om_in_use_count;
1632   }
1633 
1634   if (free_head_p != NULL) {
1635     // Move the deflated ObjectMonitors back to the global free list.
1636     guarantee(free_tail_p != NULL && counters->n_scavenged > 0, "invariant");
1637     assert(free_tail_p->_next_om == NULL, "invariant");
1638     // constant-time list splice - prepend scavenged segment to g_free_list
1639     free_tail_p->_next_om = g_free_list;
1640     g_free_list = free_head_p;

1641   }
1642   Thread::muxRelease(&gListLock);
1643   timer.stop();
1644 
1645   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1646   LogStreamHandle(Info, monitorinflation) lsh_info;
1647   LogStream* ls = NULL;
1648   if (log_is_enabled(Debug, monitorinflation)) {
1649     ls = &lsh_debug;
1650   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
1651     ls = &lsh_info;
1652   }
1653   if (ls != NULL) {
1654     ls->print_cr("deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
1655   }
1656 }
1657 






















































































































































































1658 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
1659   // Report the cumulative time for deflating each thread's idle
1660   // monitors. Note: if the work is split among more than one
1661   // worker thread, then the reported time will likely be more
1662   // than a beginning to end measurement of the phase.
1663   log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->per_thread_times, counters->per_thread_scavenged);
1664 
1665   g_om_free_count += counters->n_scavenged;





1666 
1667   if (log_is_enabled(Debug, monitorinflation)) {
1668     // exit_globals()'s call to audit_and_print_stats() is done
1669     // at the Info level.



1670     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
1671   } else if (log_is_enabled(Info, monitorinflation)) {
1672     Thread::muxAcquire(&gListLock, "finish_deflate_idle_monitors");
1673     log_info(monitorinflation)("g_om_population=%d, g_om_in_use_count=%d, "
1674                                "g_om_free_count=%d", g_om_population,
1675                                g_om_in_use_count, g_om_free_count);
1676     Thread::muxRelease(&gListLock);
1677   }
1678 
1679   ForceMonitorScavenge = 0;    // Reset
1680 
1681   OM_PERFDATA_OP(Deflations, inc(counters->n_scavenged));
1682   OM_PERFDATA_OP(MonExtant, set_value(counters->n_in_circulation));
1683 
1684   GVars.stw_random = os::random();
1685   GVars.stw_cycle++;




1686 }
1687 
1688 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
1689   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1690 





1691   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
1692   ObjectMonitor* free_tail_p = NULL;
1693   elapsedTimer timer;
1694 
1695   if (log_is_enabled(Info, safepoint, cleanup) ||
1696       log_is_enabled(Info, monitorinflation)) {
1697     timer.start();
1698   }
1699 
1700   int deflated_count = deflate_monitor_list(thread->om_in_use_list_addr(), &free_head_p, &free_tail_p);
1701 
1702   Thread::muxAcquire(&gListLock, "deflate_thread_local_monitors");
1703 
1704   // Adjust counters
1705   counters->n_in_circulation += thread->om_in_use_count;
1706   thread->om_in_use_count -= deflated_count;
1707   counters->n_scavenged += deflated_count;
1708   counters->n_in_use += thread->om_in_use_count;
1709   counters->per_thread_scavenged += deflated_count;
1710 
1711   if (free_head_p != NULL) {
1712     // Move the deflated ObjectMonitors back to the global free list.

1713     guarantee(free_tail_p != NULL && deflated_count > 0, "invariant");
1714     assert(free_tail_p->_next_om == NULL, "invariant");
1715 
1716     // constant-time list splice - prepend scavenged segment to g_free_list
1717     free_tail_p->_next_om = g_free_list;
1718     g_free_list = free_head_p;
1719   }
1720 
1721   timer.stop();
1722   // Safepoint logging cares about cumulative per_thread_times and
1723   // we'll capture most of the cost, but not the muxRelease() which
1724   // should be cheap.
1725   counters->per_thread_times += timer.seconds();
1726 
1727   Thread::muxRelease(&gListLock);
1728 
1729   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1730   LogStreamHandle(Info, monitorinflation) lsh_info;
1731   LogStream* ls = NULL;
1732   if (log_is_enabled(Debug, monitorinflation)) {
1733     ls = &lsh_debug;
1734   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
1735     ls = &lsh_info;
1736   }
1737   if (ls != NULL) {
1738     ls->print_cr("jt=" INTPTR_FORMAT ": deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(thread), timer.seconds(), deflated_count);
1739   }
1740 }
1741 
1742 // Monitor cleanup on JavaThread::exit
1743 
1744 // Iterate through monitor cache and attempt to release thread's monitors
1745 // Gives up on a particular monitor if an exception occurs, but continues
1746 // the overall iteration, swallowing the exception.
1747 class ReleaseJavaMonitorsClosure: public MonitorClosure {
1748  private:


1759 
1760 // Release all inflated monitors owned by THREAD.  Lightweight monitors are
1761 // ignored.  This is meant to be called during JNI thread detach which assumes
1762 // all remaining monitors are heavyweight.  All exceptions are swallowed.
1763 // Scanning the extant monitor list can be time consuming.
1764 // A simple optimization is to add a per-thread flag that indicates a thread
1765 // called jni_monitorenter() during its lifetime.
1766 //
1767 // Instead of No_Savepoint_Verifier it might be cheaper to
1768 // use an idiom of the form:
1769 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
1770 //   <code that must not run at safepoint>
1771 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
1772 // Since the tests are extremely cheap we could leave them enabled
1773 // for normal product builds.
1774 
1775 void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) {
1776   assert(THREAD == JavaThread::current(), "must be current Java thread");
1777   NoSafepointVerifier nsv;
1778   ReleaseJavaMonitorsClosure rjmc(THREAD);
1779   Thread::muxAcquire(&gListLock, "release_monitors_owned_by_thread");
1780   ObjectSynchronizer::monitors_iterate(&rjmc);
1781   Thread::muxRelease(&gListLock);
1782   THREAD->clear_pending_exception();
1783 }
1784 
1785 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) {
1786   switch (cause) {
1787     case inflate_cause_vm_internal:    return "VM Internal";
1788     case inflate_cause_monitor_enter:  return "Monitor Enter";
1789     case inflate_cause_wait:           return "Monitor Wait";
1790     case inflate_cause_notify:         return "Monitor Notify";
1791     case inflate_cause_hash_code:      return "Monitor Hash Code";
1792     case inflate_cause_jni_enter:      return "JNI Monitor Enter";
1793     case inflate_cause_jni_exit:       return "JNI Monitor Exit";
1794     default:
1795       ShouldNotReachHere();
1796   }
1797   return "Unknown";
1798 }
1799 
1800 //------------------------------------------------------------------------------
1801 // Debugging code


1815 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() {
1816   return (u_char*)&GVars.stw_random;
1817 }
1818 
1819 void ObjectSynchronizer::audit_and_print_stats(bool on_exit) {
1820   assert(on_exit || SafepointSynchronize::is_at_safepoint(), "invariant");
1821 
1822   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1823   LogStreamHandle(Info, monitorinflation) lsh_info;
1824   LogStreamHandle(Trace, monitorinflation) lsh_trace;
1825   LogStream* ls = NULL;
1826   if (log_is_enabled(Trace, monitorinflation)) {
1827     ls = &lsh_trace;
1828   } else if (log_is_enabled(Debug, monitorinflation)) {
1829     ls = &lsh_debug;
1830   } else if (log_is_enabled(Info, monitorinflation)) {
1831     ls = &lsh_info;
1832   }
1833   assert(ls != NULL, "sanity check");
1834 
1835   if (!on_exit) {
1836     // Not at VM exit so grab the global list lock.
1837     Thread::muxAcquire(&gListLock, "audit_and_print_stats");
1838   }
1839 
1840   // Log counts for the global and per-thread monitor lists:
1841   int chk_om_population = log_monitor_list_counts(ls);
1842   int error_cnt = 0;
1843 
1844   ls->print_cr("Checking global lists:");
1845 
1846   // Check g_om_population:
1847   if (g_om_population == chk_om_population) {
1848     ls->print_cr("g_om_population=%d equals chk_om_population=%d",
1849                  g_om_population, chk_om_population);
1850   } else {
1851     ls->print_cr("ERROR: g_om_population=%d is not equal to "
1852                  "chk_om_population=%d", g_om_population,
1853                  chk_om_population);
1854     error_cnt++;


1855   }
1856 
1857   // Check g_om_in_use_list and g_om_in_use_count:
1858   chk_global_in_use_list_and_count(ls, &error_cnt);
1859 
1860   // Check g_free_list and g_om_free_count:
1861   chk_global_free_list_and_count(ls, &error_cnt);
1862 
1863   if (!on_exit) {
1864     Thread::muxRelease(&gListLock);

1865   }
1866 
1867   ls->print_cr("Checking per-thread lists:");
1868 
1869   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1870     // Check om_in_use_list and om_in_use_count:
1871     chk_per_thread_in_use_list_and_count(jt, ls, &error_cnt);
1872 
1873     // Check om_free_list and om_free_count:
1874     chk_per_thread_free_list_and_count(jt, ls, &error_cnt);
1875   }
1876 
1877   if (error_cnt == 0) {
1878     ls->print_cr("No errors found in monitor list checks.");
1879   } else {
1880     log_error(monitorinflation)("found monitor list errors: error_cnt=%d", error_cnt);
1881   }
1882 
1883   if ((on_exit && log_is_enabled(Info, monitorinflation)) ||
1884       (!on_exit && log_is_enabled(Trace, monitorinflation))) {
1885     // When exiting this log output is at the Info level. When called
1886     // at a safepoint, this log output is at the Trace level since
1887     // there can be a lot of it.
1888     log_in_use_monitor_details(ls, on_exit);
1889   }
1890 
1891   ls->flush();
1892 
1893   guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt);
1894 }
1895 
1896 // Check a free monitor entry; log any errors.
1897 void ObjectSynchronizer::chk_free_entry(JavaThread* jt, ObjectMonitor* n,
1898                                         outputStream * out, int *error_cnt_p) {
1899   stringStream ss;
1900   if (n->is_busy()) {
1901     if (jt != NULL) {
1902       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
1903                     ": free per-thread monitor must not be busy: %s", p2i(jt),
1904                     p2i(n), n->is_busy_to_string(&ss));
1905     } else {
1906       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
1907                     "must not be busy: %s", p2i(n), n->is_busy_to_string(&ss));
1908     }
1909     *error_cnt_p = *error_cnt_p + 1;
1910   }
1911   if (n->header().value() != 0) {
1912     if (jt != NULL) {
1913       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
1914                     ": free per-thread monitor must have NULL _header "
1915                     "field: _header=" INTPTR_FORMAT, p2i(jt), p2i(n),
1916                     n->header().value());
1917     } else {

1918       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
1919                     "must have NULL _header field: _header=" INTPTR_FORMAT,
1920                     p2i(n), n->header().value());
1921     }
1922     *error_cnt_p = *error_cnt_p + 1;
1923   }

1924   if (n->object() != NULL) {
1925     if (jt != NULL) {
1926       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
1927                     ": free per-thread monitor must have NULL _object "
1928                     "field: _object=" INTPTR_FORMAT, p2i(jt), p2i(n),
1929                     p2i(n->object()));
1930     } else {
1931       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
1932                     "must have NULL _object field: _object=" INTPTR_FORMAT,
1933                     p2i(n), p2i(n->object()));
1934     }
1935     *error_cnt_p = *error_cnt_p + 1;
1936   }
1937 }
1938 
1939 // Check the global free list and count; log the results of the checks.
1940 void ObjectSynchronizer::chk_global_free_list_and_count(outputStream * out,
1941                                                         int *error_cnt_p) {
1942   int chk_om_free_count = 0;
1943   for (ObjectMonitor* n = g_free_list; n != NULL; n = n->_next_om) {
1944     chk_free_entry(NULL /* jt */, n, out, error_cnt_p);




1945     chk_om_free_count++;






1946   }
1947   if (g_om_free_count == chk_om_free_count) {
1948     out->print_cr("g_om_free_count=%d equals chk_om_free_count=%d",
1949                   g_om_free_count, chk_om_free_count);
1950   } else {
1951     out->print_cr("ERROR: g_om_free_count=%d is not equal to "
1952                   "chk_om_free_count=%d", g_om_free_count,
1953                   chk_om_free_count);































1954     *error_cnt_p = *error_cnt_p + 1;
1955   }
1956 }
1957 
1958 // Check the global in-use list and count; log the results of the checks.
1959 void ObjectSynchronizer::chk_global_in_use_list_and_count(outputStream * out,
1960                                                           int *error_cnt_p) {
1961   int chk_om_in_use_count = 0;
1962   for (ObjectMonitor* n = g_om_in_use_list; n != NULL; n = n->_next_om) {
1963     chk_in_use_entry(NULL /* jt */, n, out, error_cnt_p);




1964     chk_om_in_use_count++;






1965   }
1966   if (g_om_in_use_count == chk_om_in_use_count) {
1967     out->print_cr("g_om_in_use_count=%d equals chk_om_in_use_count=%d", g_om_in_use_count,
1968                   chk_om_in_use_count);
1969   } else {
1970     out->print_cr("ERROR: g_om_in_use_count=%d is not equal to chk_om_in_use_count=%d",



1971                   g_om_in_use_count, chk_om_in_use_count);
1972     *error_cnt_p = *error_cnt_p + 1;
1973   }
1974 }
1975 
1976 // Check an in-use monitor entry; log any errors.
1977 void ObjectSynchronizer::chk_in_use_entry(JavaThread* jt, ObjectMonitor* n,
1978                                           outputStream * out, int *error_cnt_p) {
1979   if (n->header().value() == 0) {
1980     if (jt != NULL) {
1981       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
1982                     ": in-use per-thread monitor must have non-NULL _header "
1983                     "field.", p2i(jt), p2i(n));
1984     } else {
1985       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor "
1986                     "must have non-NULL _header field.", p2i(n));
1987     }
1988     *error_cnt_p = *error_cnt_p + 1;
1989   }
1990   if (n->object() == NULL) {
1991     if (jt != NULL) {
1992       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT


2020       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2021                     ": in-use per-thread monitor's object does not refer "
2022                     "to the same monitor: obj=" INTPTR_FORMAT ", mark="
2023                     INTPTR_FORMAT ", obj_mon=" INTPTR_FORMAT, p2i(jt),
2024                     p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
2025     } else {
2026       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
2027                     "monitor's object does not refer to the same monitor: obj="
2028                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
2029                     INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
2030     }
2031     *error_cnt_p = *error_cnt_p + 1;
2032   }
2033 }
2034 
2035 // Check the thread's free list and count; log the results of the checks.
2036 void ObjectSynchronizer::chk_per_thread_free_list_and_count(JavaThread *jt,
2037                                                             outputStream * out,
2038                                                             int *error_cnt_p) {
2039   int chk_om_free_count = 0;
2040   for (ObjectMonitor* n = jt->om_free_list; n != NULL; n = n->_next_om) {
2041     chk_free_entry(jt, n, out, error_cnt_p);




2042     chk_om_free_count++;






2043   }
2044   if (jt->om_free_count == chk_om_free_count) {
2045     out->print_cr("jt=" INTPTR_FORMAT ": om_free_count=%d equals "
2046                   "chk_om_free_count=%d", p2i(jt), jt->om_free_count, chk_om_free_count);

2047   } else {
2048     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_free_count=%d is not "
2049                   "equal to chk_om_free_count=%d", p2i(jt), jt->om_free_count,
2050                   chk_om_free_count);
2051     *error_cnt_p = *error_cnt_p + 1;
2052   }
2053 }
2054 
2055 // Check the thread's in-use list and count; log the results of the checks.
2056 void ObjectSynchronizer::chk_per_thread_in_use_list_and_count(JavaThread *jt,
2057                                                               outputStream * out,
2058                                                               int *error_cnt_p) {
2059   int chk_om_in_use_count = 0;
2060   for (ObjectMonitor* n = jt->om_in_use_list; n != NULL; n = n->_next_om) {
2061     chk_in_use_entry(jt, n, out, error_cnt_p);




2062     chk_om_in_use_count++;






2063   }
2064   if (jt->om_in_use_count == chk_om_in_use_count) {
2065     out->print_cr("jt=" INTPTR_FORMAT ": om_in_use_count=%d equals "
2066                   "chk_om_in_use_count=%d", p2i(jt), jt->om_in_use_count,
2067                   chk_om_in_use_count);
2068   } else {
2069     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_in_use_count=%d is not "
2070                   "equal to chk_om_in_use_count=%d", p2i(jt), jt->om_in_use_count,
2071                   chk_om_in_use_count);
2072     *error_cnt_p = *error_cnt_p + 1;
2073   }
2074 }
2075 
2076 // Log details about ObjectMonitors on the in-use lists. The 'BHL'
2077 // flags indicate why the entry is in-use, 'object' and 'object type'
2078 // indicate the associated object and its type.
2079 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out,
2080                                                     bool on_exit) {
2081   if (!on_exit) {
2082     // Not at VM exit so grab the global list lock.
2083     Thread::muxAcquire(&gListLock, "log_in_use_monitor_details");
2084   }
2085 
2086   stringStream ss;
2087   if (g_om_in_use_count > 0) {
2088     out->print_cr("In-use global monitor info:");
2089     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2090     out->print_cr("%18s  %s  %18s  %18s",
2091                   "monitor", "BHL", "object", "object type");
2092     out->print_cr("==================  ===  ==================  ==================");
2093     for (ObjectMonitor* n = g_om_in_use_list; n != NULL; n = n->_next_om) {
2094       const oop obj = (oop) n->object();
2095       const markWord mark = n->header();




2096       ResourceMark rm;
2097       out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(n),
2098                  n->is_busy() != 0, mark.hash() != 0, n->owner() != NULL,
2099                  p2i(obj), obj->klass()->external_name());
2100       if (n->is_busy() != 0) {
2101         out->print(" (%s)", n->is_busy_to_string(&ss));

2102         ss.reset();
2103       }
2104       out->cr();





2105     }
2106   }
2107 
2108   if (!on_exit) {
2109     Thread::muxRelease(&gListLock);
2110   }
2111 
2112   out->print_cr("In-use per-thread monitor info:");
2113   out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2114   out->print_cr("%18s  %18s  %s  %18s  %18s",
2115                 "jt", "monitor", "BHL", "object", "object type");
2116   out->print_cr("==================  ==================  ===  ==================  ==================");
2117   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2118     for (ObjectMonitor* n = jt->om_in_use_list; n != NULL; n = n->_next_om) {
2119       const oop obj = (oop) n->object();
2120       const markWord mark = n->header();




2121       ResourceMark rm;
2122       out->print(INTPTR_FORMAT "  " INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT
2123                  "  %s", p2i(jt), p2i(n), n->is_busy() != 0,
2124                  mark.hash() != 0, n->owner() != NULL, p2i(obj),
2125                  obj->klass()->external_name());
2126       if (n->is_busy() != 0) {
2127         out->print(" (%s)", n->is_busy_to_string(&ss));
2128         ss.reset();
2129       }
2130       out->cr();






2131     }
2132   }
2133 
2134   out->flush();
2135 }
2136 
2137 // Log counts for the global and per-thread monitor lists and return
2138 // the population count.
2139 int ObjectSynchronizer::log_monitor_list_counts(outputStream * out) {
2140   int pop_count = 0;
2141   out->print_cr("%18s  %10s  %10s  %10s",
2142                 "Global Lists:", "InUse", "Free", "Total");
2143   out->print_cr("==================  ==========  ==========  ==========");
2144   out->print_cr("%18s  %10d  %10d  %10d", "",
2145                 g_om_in_use_count, g_om_free_count, g_om_population);
2146   pop_count += g_om_in_use_count + g_om_free_count;



2147 
2148   out->print_cr("%18s  %10s  %10s  %10s",
2149                 "Per-Thread Lists:", "InUse", "Free", "Provision");
2150   out->print_cr("==================  ==========  ==========  ==========");
2151 
2152   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2153     out->print_cr(INTPTR_FORMAT "  %10d  %10d  %10d", p2i(jt),
2154                   jt->om_in_use_count, jt->om_free_count, jt->om_free_provision);
2155     pop_count += jt->om_in_use_count + jt->om_free_count;
2156   }
2157   return pop_count;
2158 }
2159 
2160 #ifndef PRODUCT
2161 
2162 // Check if monitor belongs to the monitor cache
2163 // The list is grow-only so it's *relatively* safe to traverse
2164 // the list of extant blocks without taking a lock.
2165 
2166 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) {
2167   PaddedObjectMonitor* block = OrderAccess::load_acquire(&g_block_list);
2168   while (block != NULL) {
2169     assert(block->object() == CHAINMARKER, "must be a block header");
2170     if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) {
2171       address mon = (address)monitor;
2172       address blk = (address)block;
2173       size_t diff = mon - blk;
2174       assert((diff % sizeof(PaddedObjectMonitor)) == 0, "must be aligned");
2175       return 1;
2176     }



2177     block = (PaddedObjectMonitor*)block->_next_om;
2178   }
2179   return 0;
2180 }
2181 
2182 #endif


  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 "logging/log.hpp"
  28 #include "logging/logStream.hpp"
  29 #include "jfr/jfrEvents.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/metaspaceShared.hpp"
  32 #include "memory/padded.hpp"
  33 #include "memory/resourceArea.hpp"
  34 #include "memory/universe.hpp"
  35 #include "oops/markWord.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "runtime/atomic.hpp"
  38 #include "runtime/biasedLocking.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "runtime/handshake.hpp"
  41 #include "runtime/interfaceSupport.inline.hpp"
  42 #include "runtime/mutexLocker.hpp"
  43 #include "runtime/objectMonitor.hpp"
  44 #include "runtime/objectMonitor.inline.hpp"
  45 #include "runtime/osThread.hpp"
  46 #include "runtime/safepointVerifiers.hpp"
  47 #include "runtime/sharedRuntime.hpp"
  48 #include "runtime/stubRoutines.hpp"
  49 #include "runtime/synchronizer.hpp"
  50 #include "runtime/thread.inline.hpp"
  51 #include "runtime/timer.hpp"
  52 #include "runtime/vframe.hpp"
  53 #include "runtime/vmThread.hpp"
  54 #include "utilities/align.hpp"
  55 #include "utilities/dtrace.hpp"
  56 #include "utilities/events.hpp"
  57 #include "utilities/preserveException.hpp"
  58 
  59 // The "core" versions of monitor enter and exit reside in this file.
  60 // The interpreter and compilers contain specialized transliterated


 102   }
 103 
 104 #else //  ndef DTRACE_ENABLED
 105 
 106 #define DTRACE_MONITOR_WAIT_PROBE(obj, thread, millis, mon)    {;}
 107 #define DTRACE_MONITOR_PROBE(probe, obj, thread, mon)          {;}
 108 
 109 #endif // ndef DTRACE_ENABLED
 110 
 111 // This exists only as a workaround of dtrace bug 6254741
 112 int dtrace_waited_probe(ObjectMonitor* monitor, Handle obj, Thread* thr) {
 113   DTRACE_MONITOR_PROBE(waited, monitor, obj(), thr);
 114   return 0;
 115 }
 116 
 117 #define NINFLATIONLOCKS 256
 118 static volatile intptr_t gInflationLocks[NINFLATIONLOCKS];
 119 
 120 // global list of blocks of monitors
 121 PaddedObjectMonitor* volatile ObjectSynchronizer::g_block_list = NULL;
 122 bool volatile ObjectSynchronizer::_is_async_deflation_requested = false;
 123 bool volatile ObjectSynchronizer::_is_special_deflation_requested = false;
 124 jlong ObjectSynchronizer::_last_async_deflation_time_ns = 0;
 125 
 126 // Global ObjectMonitor free list. Newly allocated and deflated
 127 // ObjectMonitors are prepended here.
 128 static ObjectMonitor* volatile g_free_list = NULL;
 129 // Global ObjectMonitor in-use list. When a JavaThread is exiting,
 130 // ObjectMonitors on its per-thread in-use list are prepended here.
 131 static ObjectMonitor* volatile g_om_in_use_list = NULL;
 132 // Global ObjectMonitor wait list. If HandshakeAfterDeflateIdleMonitors
 133 // is true, deflated ObjectMonitors wait on this list until after a
 134 // handshake or a safepoint for platforms that don't support handshakes.
 135 // After the handshake or safepoint, the deflated ObjectMonitors are
 136 // prepended to g_free_list.
 137 static ObjectMonitor* volatile g_wait_list = NULL;
 138 

 139 static volatile int g_om_free_count = 0;    // # on g_free_list
 140 static volatile int g_om_in_use_count = 0;  // # on g_om_in_use_list
 141 static volatile int g_om_population = 0;    // # Extant -- in circulation
 142 static volatile int g_om_wait_count = 0;    // # on g_wait_list
 143 
 144 #define CHAINMARKER (cast_to_oop<intptr_t>(-1))
 145 
 146 
 147 // =====================> List Management functions
 148 
 149 // Return true if the ObjectMonitor's next field is marked.
 150 // Otherwise returns false.
 151 static bool is_next_marked(ObjectMonitor* om) {
 152   // Use load_acquire() since _next_om fields are updated with a
 153   // release_store().
 154   return ((intptr_t)OrderAccess::load_acquire(&om->_next_om) & 0x1) != 0;
 155 }
 156 
 157 // Mark an ObjectMonitor* and return it. Note: the om parameter
 158 // may or may not have been marked originally.
 159 static ObjectMonitor* mark_om_ptr(ObjectMonitor* om) {
 160   return (ObjectMonitor*)((intptr_t)om | 0x1);
 161 }
 162 
 163 // Mark the next field in an ObjectMonitor. If marking was successful,
 164 // then the unmarked next field is returned via parameter and true is
 165 // returned. Otherwise false is returned.
 166 static bool mark_next(ObjectMonitor* om, ObjectMonitor** next_p) {
 167   // Get current next field without any marking value.
 168   ObjectMonitor* next = (ObjectMonitor*)((intptr_t)om->_next_om & ~0x1);
 169   if (Atomic::cmpxchg(mark_om_ptr(next), &om->_next_om, next) != next) {
 170     return false;  // Could not mark the next field or it was already marked.
 171   }
 172   *next_p = next;
 173   return true;
 174 }
 175 
 176 // Loop until we mark the next field in an ObjectMonitor. The unmarked
 177 // next field is returned.
 178 static ObjectMonitor* mark_next_loop(ObjectMonitor* om) {
 179   ObjectMonitor* next;
 180   while (true) {
 181     if (mark_next(om, &next)) {
 182       // Marked om's next field so return the unmarked value.
 183       return next;
 184     }
 185   }
 186 }
 187 
 188 // Set the next field in an ObjectMonitor to the specified value.
 189 // The caller of set_next() must be the same thread that marked the
 190 // ObjectMonitor.
 191 static void set_next(ObjectMonitor* om, ObjectMonitor* value) {
 192   OrderAccess::release_store(&om->_next_om, value);
 193 }
 194 
 195 // Mark the next field in the list head ObjectMonitor. If marking was
 196 // successful, then the mid and the unmarked next field are returned
 197 // via parameter and true is returned. Otherwise false is returned.
 198 static bool mark_list_head(ObjectMonitor* volatile * list_p,
 199                            ObjectMonitor** mid_p, ObjectMonitor** next_p) {
 200   while (true) {
 201     ObjectMonitor* mid = *list_p;
 202     if (mid == NULL) {
 203       return false;  // The list is empty so nothing to mark.
 204     }
 205     if (mark_next(mid, next_p)) {
 206       if (*list_p != mid) {
 207         // The list head changed so we have to retry.
 208         set_next(mid, *next_p);  // unmark mid
 209         continue;
 210       }
 211       // We marked next field to guard against races.
 212       *mid_p = mid;
 213       return true;
 214     }
 215   }
 216 }
 217 
 218 // Return the unmarked next field in an ObjectMonitor. Note: the next
 219 // field may or may not have been marked originally.
 220 static ObjectMonitor* unmarked_next(ObjectMonitor* om) {
 221   // Use load_acquire() since _next_om fields are updated with a
 222   // release_store().
 223   return (ObjectMonitor*)((intptr_t)OrderAccess::load_acquire(&om->_next_om) & ~0x1);
 224 }
 225 
 226 // Mark the next ObjectMonitor for traversal. The current ObjectMonitor
 227 // is unmarked after the next ObjectMonitor is marked. *cur_p and *next_p
 228 // are updated to their next values in the list traversal. *cur_p is set
 229 // to NULL when the end of the list is reached.
 230 static void mark_next_for_traversal(ObjectMonitor** cur_p, ObjectMonitor** next_p) {
 231   ObjectMonitor* prev = *cur_p;      // Save current for unmarking.
 232   if (*next_p == NULL) {             // Reached the end of the list.
 233     set_next(prev, NULL);            // Unmark previous.
 234     *cur_p = NULL;                   // Tell the caller we are done.
 235     return;
 236   }
 237   (void)mark_next_loop(*next_p);     // Mark next.
 238   set_next(prev, *next_p);           // Unmark previous.
 239   *cur_p = *next_p;                  // Update current.
 240   *next_p = unmarked_next(*cur_p);   // Update next.
 241 }
 242 
 243 // Prepend a list of ObjectMonitors to the specified *list_p. 'tail' is
 244 // the last ObjectMonitor in the list and there are 'count' on the list.
 245 // Also updates the specified *count_p.
 246 static void prepend_list_to_common(ObjectMonitor* list, ObjectMonitor* tail,
 247                                    int count, ObjectMonitor* volatile* list_p,
 248                                    volatile int* count_p) {
 249   while (true) {
 250     ObjectMonitor* cur = *list_p;
 251     // Prepend list to *list_p.
 252     ObjectMonitor* next = NULL;
 253     if (!mark_next(tail, &next)) {
 254       continue;  // failed to mark next field so try it all again
 255     }
 256     set_next(tail, cur);  // tail now points to cur (and unmarks tail)
 257     if (cur == NULL) {
 258       // No potential race with takers or other prependers since
 259       // *list_p is empty.
 260       if (Atomic::cmpxchg(list, list_p, cur) == cur) {
 261         // Successfully switched *list_p to the list value.
 262         Atomic::add(count, count_p);
 263         break;
 264       }
 265       // Implied else: try it all again
 266     } else {
 267       // Try to mark next field to guard against races:
 268       if (!mark_next(cur, &next)) {
 269         continue;  // failed to mark next field so try it all again
 270       }
 271       // We marked the next field so try to switch *list_p to the list value.
 272       if (Atomic::cmpxchg(list, list_p, cur) != cur) {
 273         // The list head has changed so unmark the next field and try again:
 274         set_next(cur, next);
 275         continue;
 276       }
 277       Atomic::add(count, count_p);
 278       set_next(cur, next);  // unmark next field
 279       break;
 280     }
 281   }
 282 }
 283 
 284 // Prepend a newly allocated block of ObjectMonitors to g_block_list and
 285 // g_free_list. Also updates g_om_population and g_om_free_count.
 286 void ObjectSynchronizer::prepend_block_to_lists(PaddedObjectMonitor* new_blk) {
 287   // First we handle g_block_list:
 288   while (true) {
 289     PaddedObjectMonitor* cur = g_block_list;
 290     // Prepend new_blk to g_block_list. The first ObjectMonitor in
 291     // a block is reserved for use as linkage to the next block.
 292     new_blk[0]._next_om = cur;
 293     if (Atomic::cmpxchg(new_blk, &g_block_list, cur) == cur) {
 294       // Successfully switched g_block_list to the new_blk value.
 295       Atomic::add(_BLOCKSIZE - 1, &g_om_population);
 296       break;
 297     }
 298     // Implied else: try it all again
 299   }
 300 
 301   // Second we handle g_free_list:
 302   prepend_list_to_common(new_blk + 1, &new_blk[_BLOCKSIZE - 1], _BLOCKSIZE - 1,
 303                          &g_free_list, &g_om_free_count);
 304 }
 305 
 306 // Prepend a list of ObjectMonitors to g_free_list. 'tail' is the last
 307 // ObjectMonitor in the list and there are 'count' on the list. Also
 308 // updates g_om_free_count.
 309 static void prepend_list_to_g_free_list(ObjectMonitor* list,
 310                                         ObjectMonitor* tail, int count) {
 311   prepend_list_to_common(list, tail, count, &g_free_list, &g_om_free_count);
 312 }
 313 
 314 // Prepend a list of ObjectMonitors to g_wait_list. 'tail' is the last
 315 // ObjectMonitor in the list and there are 'count' on the list. Also
 316 // updates g_om_wait_count.
 317 static void prepend_list_to_g_wait_list(ObjectMonitor* list,
 318                                         ObjectMonitor* tail, int count) {
 319   assert(HandshakeAfterDeflateIdleMonitors, "sanity check");
 320   prepend_list_to_common(list, tail, count, &g_wait_list, &g_om_wait_count);
 321 }
 322 
 323 // Prepend a list of ObjectMonitors to g_om_in_use_list. 'tail' is the last
 324 // ObjectMonitor in the list and there are 'count' on the list. Also
 325 // updates g_om_in_use_list.
 326 static void prepend_list_to_g_om_in_use_list(ObjectMonitor* list,
 327                                              ObjectMonitor* tail, int count) {
 328   prepend_list_to_common(list, tail, count, &g_om_in_use_list, &g_om_in_use_count);
 329 }
 330 
 331 // Prepend an ObjectMonitor to the specified list. Also updates
 332 // the specified counter.
 333 static void prepend_to_common(ObjectMonitor* m, ObjectMonitor* volatile * list_p,
 334                               int volatile * count_p) {
 335   while (true) {
 336     (void)mark_next_loop(m);  // mark m so we can safely update its next field
 337     ObjectMonitor* cur = NULL;
 338     ObjectMonitor* next = NULL;
 339     // Mark the list head to guard against A-B-A race:
 340     if (mark_list_head(list_p, &cur, &next)) {
 341       // List head is now marked so we can safely switch it.
 342       set_next(m, cur);  // m now points to cur (and unmarks m)
 343       *list_p = m;  // Switch list head to unmarked m.
 344       // mark_list_head() used cmpxchg() above, switching list head can be lazier:
 345       OrderAccess::storestore();
 346       set_next(cur, next);  // Unmark the previous list head.
 347       break;
 348     }
 349     // The list is empty so try to set the list head.
 350     assert(cur == NULL, "cur must be NULL: cur=" INTPTR_FORMAT, p2i(cur));
 351     set_next(m, cur);  // m now points to NULL (and unmarks m)
 352     if (Atomic::cmpxchg(m, list_p, cur) == cur) {
 353       // List head is now unmarked m.
 354       break;
 355     }
 356     // Implied else: try it all again
 357   }
 358   Atomic::inc(count_p);
 359 }
 360 
 361 // Prepend an ObjectMonitor to a per-thread om_free_list.
 362 // Also updates the per-thread om_free_count.
 363 static void prepend_to_om_free_list(Thread* self, ObjectMonitor* m) {
 364   prepend_to_common(m, &self->om_free_list, &self->om_free_count);
 365 }
 366 
 367 // Prepend an ObjectMonitor to a per-thread om_in_use_list.
 368 // Also updates the per-thread om_in_use_count.
 369 static void prepend_to_om_in_use_list(Thread* self, ObjectMonitor* m) {
 370   prepend_to_common(m, &self->om_in_use_list, &self->om_in_use_count);
 371 }
 372 
 373 // Take an ObjectMonitor from the start of the specified list. Also
 374 // decrements the specified counter. Returns NULL if none are available.
 375 static ObjectMonitor* take_from_start_of_common(ObjectMonitor* volatile * list_p,
 376                                                 int volatile * count_p) {
 377   ObjectMonitor* next = NULL;
 378   ObjectMonitor* take = NULL;
 379   // Mark the list head to guard against A-B-A race:
 380   if (!mark_list_head(list_p, &take, &next)) {
 381     return NULL;  // None are available.
 382   }
 383   // Switch marked list head to next (which unmarks the list head, but
 384   // leaves take marked):
 385   *list_p = next;
 386   Atomic::dec(count_p);
 387   // mark_list_head() used cmpxchg() above, switching list head can be lazier:
 388   OrderAccess::storestore();
 389   // Unmark take, but leave the next value for any lagging list
 390   // walkers. It will get cleaned up when take is prepended to
 391   // the in-use list:
 392   set_next(take, next);
 393   return take;
 394 }
 395 
 396 // Take an ObjectMonitor from the start of the global free-list. Also
 397 // updates g_om_free_count. Returns NULL if none are available.
 398 static ObjectMonitor* take_from_start_of_g_free_list() {
 399   return take_from_start_of_common(&g_free_list, &g_om_free_count);
 400 }
 401 
 402 // Take an ObjectMonitor from the start of a per-thread free-list.
 403 // Also updates om_free_count. Returns NULL if none are available.
 404 static ObjectMonitor* take_from_start_of_om_free_list(Thread* self) {
 405   return take_from_start_of_common(&self->om_free_list, &self->om_free_count);
 406 }
 407 
 408 
 409 // =====================> Quick functions
 410 
 411 // The quick_* forms are special fast-path variants used to improve
 412 // performance.  In the simplest case, a "quick_*" implementation could
 413 // simply return false, in which case the caller will perform the necessary
 414 // state transitions and call the slow-path form.
 415 // The fast-path is designed to handle frequently arising cases in an efficient
 416 // manner and is just a degenerate "optimistic" variant of the slow-path.
 417 // returns true  -- to indicate the call was satisfied.
 418 // returns false -- to indicate the call needs the services of the slow-path.
 419 // A no-loitering ordinance is in effect for code in the quick_* family
 420 // operators: safepoints or indefinite blocking (blocking that might span a
 421 // safepoint) are forbidden. Generally the thread_state() is _in_Java upon
 422 // entry.
 423 //
 424 // Consider: An interesting optimization is to have the JIT recognize the
 425 // following common idiom:
 426 //   synchronized (someobj) { .... ; notify(); }
 427 // That is, we find a notify() or notifyAll() call that immediately precedes
 428 // the monitorexit operation.  In that case the JIT could fuse the operations


 467   }
 468 
 469   // biased locking and any other IMS exception states take the slow-path
 470   return false;
 471 }
 472 
 473 
 474 // The LockNode emitted directly at the synchronization site would have
 475 // been too big if it were to have included support for the cases of inflated
 476 // recursive enter and exit, so they go here instead.
 477 // Note that we can't safely call AsyncPrintJavaStack() from within
 478 // quick_enter() as our thread state remains _in_Java.
 479 
 480 bool ObjectSynchronizer::quick_enter(oop obj, Thread* self,
 481                                      BasicLock * lock) {
 482   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 483   assert(self->is_Java_thread(), "invariant");
 484   assert(((JavaThread *) self)->thread_state() == _thread_in_Java, "invariant");
 485   NoSafepointVerifier nsv;
 486   if (obj == NULL) return false;       // Need to throw NPE
 487 
 488   while (true) {
 489     const markWord mark = obj->mark();
 490 
 491     if (mark.has_monitor()) {
 492       ObjectMonitorHandle omh;
 493       if (!omh.save_om_ptr(obj, mark)) {
 494         // Lost a race with async deflation so try again.
 495         assert(AsyncDeflateIdleMonitors, "sanity check");
 496         continue;
 497       }
 498       ObjectMonitor* const m = omh.om_ptr();
 499       assert(m->object() == obj, "invariant");
 500       Thread* const owner = (Thread *) m->_owner;
 501 
 502       // Lock contention and Transactional Lock Elision (TLE) diagnostics
 503       // and observability
 504       // Case: light contention possibly amenable to TLE
 505       // Case: TLE inimical operations such as nested/recursive synchronization
 506 
 507       if (owner == self) {
 508         m->_recursions++;
 509         return true;
 510       }
 511 
 512       // This Java Monitor is inflated so obj's header will never be
 513       // displaced to this thread's BasicLock. Make the displaced header
 514       // non-NULL so this BasicLock is not seen as recursive nor as
 515       // being locked. We do this unconditionally so that this thread's
 516       // BasicLock cannot be mis-interpreted by any stack walkers. For
 517       // performance reasons, stack walkers generally first check for
 518       // Biased Locking in the object's header, the second check is for
 519       // stack-locking in the object's header, the third check is for
 520       // recursive stack-locking in the displaced header in the BasicLock,
 521       // and last are the inflated Java Monitor (ObjectMonitor) checks.
 522       lock->set_displaced_header(markWord::unused_mark());
 523 
 524       if (owner == NULL && m->try_set_owner_from(self, NULL) == NULL) {
 525         assert(m->_recursions == 0, "invariant");
 526         return true;
 527       }
 528 
 529       if (AsyncDeflateIdleMonitors &&
 530           m->try_set_owner_from(self, DEFLATER_MARKER) == DEFLATER_MARKER) {
 531         // The deflation protocol finished the first part (setting owner),
 532         // but it failed the second part (making ref_count negative) and
 533         // bailed. Or the ObjectMonitor was async deflated and reused.
 534         // Acquired the monitor.
 535         assert(m->_recursions == 0, "invariant");
 536         return true;
 537       }
 538     }
 539     break;
 540   }
 541 
 542   // Note that we could inflate in quick_enter.
 543   // This is likely a useful optimization
 544   // Critically, in quick_enter() we must not:
 545   // -- perform bias revocation, or
 546   // -- block indefinitely, or
 547   // -- reach a safepoint
 548 
 549   return false;        // revert to slow-path
 550 }
 551 
 552 // -----------------------------------------------------------------------------
 553 // Monitor Enter/Exit
 554 // The interpreter and compiler assembly code tries to lock using the fast path
 555 // of this algorithm. Make sure to update that code if the following function is
 556 // changed. The implementation is extremely sensitive to race condition. Be careful.
 557 
 558 void ObjectSynchronizer::enter(Handle obj, BasicLock* lock, TRAPS) {
 559   if (UseBiasedLocking) {
 560     if (!SafepointSynchronize::is_at_safepoint()) {


 571     // Anticipate successful CAS -- the ST of the displaced mark must
 572     // be visible <= the ST performed by the CAS.
 573     lock->set_displaced_header(mark);
 574     if (mark == obj()->cas_set_mark(markWord::from_pointer(lock), mark)) {
 575       return;
 576     }
 577     // Fall through to inflate() ...
 578   } else if (mark.has_locker() &&
 579              THREAD->is_lock_owned((address)mark.locker())) {
 580     assert(lock != mark.locker(), "must not re-lock the same lock");
 581     assert(lock != (BasicLock*)obj->mark().value(), "don't relock with same BasicLock");
 582     lock->set_displaced_header(markWord::from_pointer(NULL));
 583     return;
 584   }
 585 
 586   // The object header will never be displaced to this lock,
 587   // so it does not matter what the value is, except that it
 588   // must be non-zero to avoid looking like a re-entrant lock,
 589   // and must not look locked either.
 590   lock->set_displaced_header(markWord::unused_mark());
 591   ObjectMonitorHandle omh;
 592   inflate(&omh, THREAD, obj(), inflate_cause_monitor_enter);
 593   omh.om_ptr()->enter(THREAD);
 594 }
 595 
 596 void ObjectSynchronizer::exit(oop object, BasicLock* lock, TRAPS) {
 597   markWord mark = object->mark();
 598   // We cannot check for Biased Locking if we are racing an inflation.
 599   assert(mark == markWord::INFLATING() ||
 600          !mark.has_bias_pattern(), "should not see bias pattern here");
 601 
 602   markWord dhw = lock->displaced_header();
 603   if (dhw.value() == 0) {
 604     // If the displaced header is NULL, then this exit matches up with
 605     // a recursive enter. No real work to do here except for diagnostics.
 606 #ifndef PRODUCT
 607     if (mark != markWord::INFLATING()) {
 608       // Only do diagnostics if we are not racing an inflation. Simply
 609       // exiting a recursive enter of a Java Monitor that is being
 610       // inflated is safe; see the has_monitor() comment below.
 611       assert(!mark.is_neutral(), "invariant");
 612       assert(!mark.has_locker() ||
 613              THREAD->is_lock_owned((address)mark.locker()), "invariant");


 622         // does not own the Java Monitor.
 623         ObjectMonitor* m = mark.monitor();
 624         assert(((oop)(m->object()))->mark() == mark, "invariant");
 625         assert(m->is_entered(THREAD), "invariant");
 626       }
 627     }
 628 #endif
 629     return;
 630   }
 631 
 632   if (mark == markWord::from_pointer(lock)) {
 633     // If the object is stack-locked by the current thread, try to
 634     // swing the displaced header from the BasicLock back to the mark.
 635     assert(dhw.is_neutral(), "invariant");
 636     if (object->cas_set_mark(dhw, mark) == mark) {
 637       return;
 638     }
 639   }
 640 
 641   // We have to take the slow-path of possible inflation and then exit.
 642   ObjectMonitorHandle omh;
 643   inflate(&omh, THREAD, object, inflate_cause_vm_internal);
 644   omh.om_ptr()->exit(true, THREAD);
 645 }
 646 
 647 // -----------------------------------------------------------------------------
 648 // Class Loader  support to workaround deadlocks on the class loader lock objects
 649 // Also used by GC
 650 // complete_exit()/reenter() are used to wait on a nested lock
 651 // i.e. to give up an outer lock completely and then re-enter
 652 // Used when holding nested locks - lock acquisition order: lock1 then lock2
 653 //  1) complete_exit lock1 - saving recursion count
 654 //  2) wait on lock2
 655 //  3) when notified on lock2, unlock lock2
 656 //  4) reenter lock1 with original recursion count
 657 //  5) lock lock2
 658 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 659 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
 660   if (UseBiasedLocking) {
 661     BiasedLocking::revoke(obj, THREAD);
 662     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 663   }
 664 
 665   ObjectMonitorHandle omh;
 666   inflate(&omh, THREAD, obj(), inflate_cause_vm_internal);
 667   intptr_t ret_code = omh.om_ptr()->complete_exit(THREAD);
 668   return ret_code;
 669 }
 670 
 671 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 672 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
 673   if (UseBiasedLocking) {
 674     BiasedLocking::revoke(obj, THREAD);
 675     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 676   }
 677 
 678   ObjectMonitorHandle omh;
 679   inflate(&omh, THREAD, obj(), inflate_cause_vm_internal);
 680   omh.om_ptr()->reenter(recursion, THREAD);
 681 }
 682 // -----------------------------------------------------------------------------
 683 // JNI locks on java objects
 684 // NOTE: must use heavy weight monitor to handle jni monitor enter
 685 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) {
 686   // the current locking is from JNI instead of Java code
 687   if (UseBiasedLocking) {
 688     BiasedLocking::revoke(obj, THREAD);
 689     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 690   }
 691   THREAD->set_current_pending_monitor_is_from_java(false);
 692   ObjectMonitorHandle omh;
 693   inflate(&omh, THREAD, obj(), inflate_cause_jni_enter);
 694   omh.om_ptr()->enter(THREAD);
 695   THREAD->set_current_pending_monitor_is_from_java(true);
 696 }
 697 
 698 // NOTE: must use heavy weight monitor to handle jni monitor exit
 699 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
 700   if (UseBiasedLocking) {
 701     Handle h_obj(THREAD, obj);
 702     BiasedLocking::revoke(h_obj, THREAD);
 703     obj = h_obj();
 704   }
 705   assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 706 
 707   ObjectMonitorHandle omh;
 708   inflate(&omh, THREAD, obj, inflate_cause_jni_exit);
 709   ObjectMonitor* monitor = omh.om_ptr();
 710   // If this thread has locked the object, exit the monitor. We
 711   // intentionally do not use CHECK here because we must exit the
 712   // monitor even if an exception is pending.
 713   if (monitor->check_owner(THREAD)) {
 714     monitor->exit(true, THREAD);
 715   }
 716 }
 717 
 718 // -----------------------------------------------------------------------------
 719 // Internal VM locks on java objects
 720 // standard constructor, allows locking failures
 721 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool do_lock) {
 722   _dolock = do_lock;
 723   _thread = thread;
 724   _thread->check_for_valid_safepoint_state();
 725   _obj = obj;
 726 
 727   if (_dolock) {
 728     ObjectSynchronizer::enter(_obj, &_lock, _thread);
 729   }
 730 }
 731 
 732 ObjectLocker::~ObjectLocker() {
 733   if (_dolock) {
 734     ObjectSynchronizer::exit(_obj(), &_lock, _thread);
 735   }
 736 }
 737 
 738 
 739 // -----------------------------------------------------------------------------
 740 //  Wait/Notify/NotifyAll
 741 // NOTE: must use heavy weight monitor to handle wait()
 742 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
 743   if (UseBiasedLocking) {
 744     BiasedLocking::revoke(obj, THREAD);
 745     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 746   }
 747   if (millis < 0) {
 748     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 749   }
 750   ObjectMonitorHandle omh;
 751   inflate(&omh, THREAD, obj(), inflate_cause_wait);
 752   ObjectMonitor* monitor = omh.om_ptr();
 753 
 754   DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
 755   monitor->wait(millis, true, THREAD);
 756 
 757   // This dummy call is in place to get around dtrace bug 6254741.  Once
 758   // that's fixed we can uncomment the following line, remove the call
 759   // and change this function back into a "void" func.
 760   // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
 761   int ret_code = dtrace_waited_probe(monitor, obj, THREAD);
 762   return ret_code;
 763 }
 764 
 765 void ObjectSynchronizer::wait_uninterruptibly(Handle obj, jlong millis, TRAPS) {
 766   if (UseBiasedLocking) {
 767     BiasedLocking::revoke(obj, THREAD);
 768     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 769   }
 770   if (millis < 0) {
 771     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 772   }
 773   ObjectMonitorHandle omh;
 774   inflate(&omh, THREAD, obj(), inflate_cause_wait);
 775   omh.om_ptr()->wait(millis, false, THREAD);
 776 }
 777 
 778 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
 779   if (UseBiasedLocking) {
 780     BiasedLocking::revoke(obj, THREAD);
 781     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 782   }
 783 
 784   markWord mark = obj->mark();
 785   if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
 786     return;
 787   }
 788   ObjectMonitorHandle omh;
 789   inflate(&omh, THREAD, obj(), inflate_cause_notify);
 790   omh.om_ptr()->notify(THREAD);
 791 }
 792 
 793 // NOTE: see comment of notify()
 794 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
 795   if (UseBiasedLocking) {
 796     BiasedLocking::revoke(obj, THREAD);
 797     assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 798   }
 799 
 800   markWord mark = obj->mark();
 801   if (mark.has_locker() && THREAD->is_lock_owned((address)mark.locker())) {
 802     return;
 803   }
 804   ObjectMonitorHandle omh;
 805   inflate(&omh, THREAD, obj(), inflate_cause_notify);
 806   omh.om_ptr()->notifyAll(THREAD);
 807 }
 808 
 809 // -----------------------------------------------------------------------------
 810 // Hash Code handling
 811 //
 812 // Performance concern:
 813 // OrderAccess::storestore() calls release() which at one time stored 0
 814 // into the global volatile OrderAccess::dummy variable. This store was
 815 // unnecessary for correctness. Many threads storing into a common location
 816 // causes considerable cache migration or "sloshing" on large SMP systems.
 817 // As such, I avoided using OrderAccess::storestore(). In some cases
 818 // OrderAccess::fence() -- which incurs local latency on the executing
 819 // processor -- is a better choice as it scales on SMP systems.
 820 //
 821 // See http://blogs.oracle.com/dave/entry/biased_locking_in_hotspot for
 822 // a discussion of coherency costs. Note that all our current reference
 823 // platforms provide strong ST-ST order, so the issue is moot on IA32,
 824 // x64, and SPARC.
 825 //
 826 // As a general policy we use "volatile" to control compiler-based reordering
 827 // and explicit fences (barriers) to control for architectural reordering
 828 // performed by the CPU(s) or platform.
 829 
 830 struct SharedGlobals {
 831   char         _pad_prefix[OM_CACHE_LINE_SIZE];
 832   // These are highly shared mostly-read variables.
 833   // To avoid false-sharing they need to be the sole occupants of a cache line.
 834   volatile int stw_random;
 835   volatile int stw_cycle;
 836   DEFINE_PAD_MINUS_SIZE(1, OM_CACHE_LINE_SIZE, sizeof(volatile int) * 2);
 837   // Hot RW variable -- Sequester to avoid false-sharing
 838   volatile int hc_sequence;
 839   DEFINE_PAD_MINUS_SIZE(2, OM_CACHE_LINE_SIZE, sizeof(volatile int));
 840 };
 841 
 842 static SharedGlobals GVars;
 843 static int MonitorScavengeThreshold = 1000000;
 844 static volatile int ForceMonitorScavenge = 0; // Scavenge required and pending
 845 
 846 static markWord read_stable_mark(oop obj) {
 847   markWord mark = obj->mark();
 848   if (!mark.is_being_inflated()) {
 849     return mark;       // normal fast-path return
 850   }
 851 
 852   int its = 0;
 853   for (;;) {
 854     markWord mark = obj->mark();
 855     if (!mark.is_being_inflated()) {
 856       return mark;    // normal fast-path return
 857     }
 858 
 859     // The object is being inflated by some other thread.


 980       Handle hobj(self, obj);
 981       // Relaxing assertion for bug 6320749.
 982       assert(Universe::verify_in_progress() ||
 983              !SafepointSynchronize::is_at_safepoint(),
 984              "biases should not be seen by VM thread here");
 985       BiasedLocking::revoke(hobj, JavaThread::current());
 986       obj = hobj();
 987       assert(!obj->mark().has_bias_pattern(), "biases should be revoked by now");
 988     }
 989   }
 990 
 991   // hashCode() is a heap mutator ...
 992   // Relaxing assertion for bug 6320749.
 993   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 994          !SafepointSynchronize::is_at_safepoint(), "invariant");
 995   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 996          self->is_Java_thread() , "invariant");
 997   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 998          ((JavaThread *)self)->thread_state() != _thread_blocked, "invariant");
 999 
1000   while (true) {
1001     ObjectMonitor* monitor = NULL;
1002     markWord temp, test;
1003     intptr_t hash;
1004     markWord mark = read_stable_mark(obj);
1005 
1006     // object should remain ineligible for biased locking
1007     assert(!mark.has_bias_pattern(), "invariant");
1008 
1009     if (mark.is_neutral()) {
1010       hash = mark.hash();              // this is a normal header
1011       if (hash != 0) {                  // if it has hash, just return it
1012         return hash;
1013       }
1014       hash = get_next_hash(self, obj);  // allocate a new hash code
1015       temp = mark.copy_set_hash(hash); // merge the hash code into header
1016       // use (machine word version) atomic operation to install the hash
1017       test = obj->cas_set_mark(temp, mark);
1018       if (test == mark) {
1019         return hash;
1020       }
1021       // If atomic operation failed, we must inflate the header
1022       // into heavy weight monitor. We could add more code here
1023       // for fast path, but it does not worth the complexity.
1024     } else if (mark.has_monitor()) {
1025       ObjectMonitorHandle omh;
1026       if (!omh.save_om_ptr(obj, mark)) {
1027         // Lost a race with async deflation so try again.
1028         assert(AsyncDeflateIdleMonitors, "sanity check");
1029         continue;
1030       }
1031       monitor = omh.om_ptr();
1032       temp = monitor->header();
1033       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1034       hash = temp.hash();
1035       if (hash != 0) {
1036         return hash;
1037       }
1038       // Skip to the following code to reduce code size
1039     } else if (self->is_lock_owned((address)mark.locker())) {
1040       temp = mark.displaced_mark_helper(); // this is a lightweight monitor owned
1041       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1042       hash = temp.hash();              // by current thread, check if the displaced
1043       if (hash != 0) {                  // header contains hash code
1044         return hash;
1045       }
1046       // WARNING:
1047       // The displaced header in the BasicLock on a thread's stack
1048       // is strictly immutable. It CANNOT be changed in ANY cases.
1049       // So we have to inflate the stack lock into an ObjectMonitor
1050       // even if the current thread owns the lock. The BasicLock on
1051       // a thread's stack can be asynchronously read by other threads
1052       // during an inflate() call so any change to that stack memory
1053       // may not propagate to other threads correctly.
1054     }
1055 
1056     // Inflate the monitor to set hash code
1057     ObjectMonitorHandle omh;
1058     inflate(&omh, self, obj, inflate_cause_hash_code);
1059     monitor = omh.om_ptr();
1060     // Load displaced header and check it has hash code
1061     mark = monitor->header();
1062     assert(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT, mark.value());
1063     hash = mark.hash();
1064     if (hash == 0) {
1065       hash = get_next_hash(self, obj);
1066       temp = mark.copy_set_hash(hash); // merge hash code into header
1067       assert(temp.is_neutral(), "invariant: header=" INTPTR_FORMAT, temp.value());
1068       uintptr_t v = Atomic::cmpxchg(temp.value(), (volatile uintptr_t*)monitor->header_addr(), mark.value());
1069       test = markWord(v);
1070       if (test != mark) {
1071         // The only non-deflation update to the ObjectMonitor's
1072         // header/dmw field is to merge in the hash code. If someone
1073         // adds a new usage of the header/dmw field, please update
1074         // this code.
1075         // ObjectMonitor::install_displaced_markword_in_object()
1076         // does mark the header/dmw field as part of async deflation,
1077         // but that protocol cannot happen now due to the
1078         // ObjectMonitorHandle above.
1079         hash = test.hash();
1080         assert(test.is_neutral(), "invariant: header=" INTPTR_FORMAT, test.value());
1081         assert(hash != 0, "Trivial unexpected object/monitor header usage.");
1082       }
1083     }
1084     // We finally get the hash
1085     return hash;
1086   }
1087 }
1088 
1089 // Deprecated -- use FastHashCode() instead.
1090 
1091 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
1092   return FastHashCode(Thread::current(), obj());
1093 }
1094 
1095 
1096 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
1097                                                    Handle h_obj) {
1098   if (UseBiasedLocking) {
1099     BiasedLocking::revoke(h_obj, thread);
1100     assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
1101   }
1102 
1103   assert(thread == JavaThread::current(), "Can only be called on current thread");
1104   oop obj = h_obj();
1105 
1106   while (true) {
1107     markWord mark = read_stable_mark(obj);
1108 
1109     // Uncontended case, header points to stack
1110     if (mark.has_locker()) {
1111       return thread->is_lock_owned((address)mark.locker());
1112     }
1113     // Contended case, header points to ObjectMonitor (tagged pointer)
1114     if (mark.has_monitor()) {
1115       ObjectMonitorHandle omh;
1116       if (!omh.save_om_ptr(obj, mark)) {
1117         // Lost a race with async deflation so try again.
1118         assert(AsyncDeflateIdleMonitors, "sanity check");
1119         continue;
1120       }
1121       bool ret_code = omh.om_ptr()->is_entered(thread) != 0;
1122       return ret_code;
1123     }
1124     // Unlocked case, header in place
1125     assert(mark.is_neutral(), "sanity check");
1126     return false;
1127   }
1128 }
1129 
1130 // Be aware of this method could revoke bias of the lock object.
1131 // This method queries the ownership of the lock handle specified by 'h_obj'.
1132 // If the current thread owns the lock, it returns owner_self. If no
1133 // thread owns the lock, it returns owner_none. Otherwise, it will return
1134 // owner_other.
1135 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
1136 (JavaThread *self, Handle h_obj) {
1137   // The caller must beware this method can revoke bias, and
1138   // revocation can result in a safepoint.
1139   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
1140   assert(self->thread_state() != _thread_blocked, "invariant");
1141 
1142   // Possible mark states: neutral, biased, stack-locked, inflated
1143 
1144   if (UseBiasedLocking && h_obj()->mark().has_bias_pattern()) {
1145     // CASE: biased
1146     BiasedLocking::revoke(h_obj, self);
1147     assert(!h_obj->mark().has_bias_pattern(),
1148            "biases should be revoked by now");
1149   }
1150 
1151   assert(self == JavaThread::current(), "Can only be called on current thread");
1152   oop obj = h_obj();
1153 
1154   while (true) {
1155     markWord mark = read_stable_mark(obj);
1156 
1157     // CASE: stack-locked.  Mark points to a BasicLock on the owner's stack.
1158     if (mark.has_locker()) {
1159       return self->is_lock_owned((address)mark.locker()) ?
1160         owner_self : owner_other;
1161     }
1162 
1163     // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor.
1164     // The Object:ObjectMonitor relationship is stable as long as we're
1165     // not at a safepoint and AsyncDeflateIdleMonitors is false.
1166     if (mark.has_monitor()) {
1167       ObjectMonitorHandle omh;
1168       if (!omh.save_om_ptr(obj, mark)) {
1169         // Lost a race with async deflation so try again.
1170         assert(AsyncDeflateIdleMonitors, "sanity check");
1171         continue;
1172       }
1173       ObjectMonitor* monitor = omh.om_ptr();
1174       void* owner = monitor->_owner;
1175       if (owner == NULL) return owner_none;
1176       return (owner == self ||
1177               self->is_lock_owned((address)owner)) ? owner_self : owner_other;
1178     }
1179 
1180     // CASE: neutral
1181     assert(mark.is_neutral(), "sanity check");
1182     return owner_none;           // it's unlocked
1183   }
1184 }
1185 
1186 // FIXME: jvmti should call this
1187 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
1188   if (UseBiasedLocking) {
1189     if (SafepointSynchronize::is_at_safepoint()) {
1190       BiasedLocking::revoke_at_safepoint(h_obj);
1191     } else {
1192       BiasedLocking::revoke(h_obj, JavaThread::current());
1193     }
1194     assert(!h_obj->mark().has_bias_pattern(), "biases should be revoked by now");
1195   }
1196 
1197   oop obj = h_obj();

1198 
1199   while (true) {
1200     address owner = NULL;
1201     markWord mark = read_stable_mark(obj);
1202 
1203     // Uncontended case, header points to stack
1204     if (mark.has_locker()) {
1205       owner = (address) mark.locker();
1206     }
1207 
1208     // Contended case, header points to ObjectMonitor (tagged pointer)
1209     else if (mark.has_monitor()) {
1210       ObjectMonitorHandle omh;
1211       if (!omh.save_om_ptr(obj, mark)) {
1212         // Lost a race with async deflation so try again.
1213         assert(AsyncDeflateIdleMonitors, "sanity check");
1214         continue;
1215       }
1216       ObjectMonitor* monitor = omh.om_ptr();
1217       assert(monitor != NULL, "monitor should be non-null");
1218       owner = (address) monitor->owner();
1219     }
1220 
1221     if (owner != NULL) {
1222       // owning_thread_from_monitor_owner() may also return NULL here
1223       return Threads::owning_thread_from_monitor_owner(t_list, owner);
1224     }
1225 
1226     // Unlocked case, header in place
1227     // Cannot have assertion since this object may have been
1228     // locked by another thread when reaching here.
1229     // assert(mark.is_neutral(), "sanity check");
1230 
1231     return NULL;
1232   }
1233 }
1234 
1235 // Visitors ...
1236 
1237 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
1238   PaddedObjectMonitor* block = g_block_list;
1239   while (block != NULL) {
1240     assert(block->object() == CHAINMARKER, "must be a block header");
1241     for (int i = _BLOCKSIZE - 1; i > 0; i--) {
1242       ObjectMonitor* mid = (ObjectMonitor *)(block + i);
1243       ObjectMonitorHandle omh;
1244       if (!mid->is_free() && omh.set_om_ptr_if_safe(mid)) {
1245         // The ObjectMonitor* is not free and it has been made safe.
1246         if (mid->object() == NULL) {
1247           // Only process with closure if the object is set.
1248           continue;
1249         }
1250         closure->do_monitor(mid);
1251       }
1252     }
1253     // unmarked_next() is not needed with g_block_list (no next field
1254     // marking) and no load_acquire() needed because _next_om is
1255     // updated before g_block_list is changed with cmpxchg().
1256     block = (PaddedObjectMonitor*)block->_next_om;
1257   }
1258 }
1259 
1260 static bool monitors_used_above_threshold() {
1261   if (g_om_population == 0) {
1262     return false;
1263   }
1264   if (MonitorUsedDeflationThreshold > 0) {
1265     int monitors_used = g_om_population - g_om_free_count;
1266     if (HandshakeAfterDeflateIdleMonitors) {
1267       monitors_used -= g_om_wait_count;
1268     }
1269     int monitor_usage = (monitors_used * 100LL) / g_om_population;
1270     return monitor_usage > MonitorUsedDeflationThreshold;
1271   }
1272   return false;
1273 }
1274 
1275 // Returns true if MonitorBound is set (> 0) and if the specified
1276 // cnt is > MonitorBound. Otherwise returns false.
1277 static bool is_MonitorBound_exceeded(const int cnt) {
1278   const int mx = MonitorBound;
1279   return mx > 0 && cnt > mx;
1280 }
1281 
1282 bool ObjectSynchronizer::is_async_deflation_needed() {
1283   if (!AsyncDeflateIdleMonitors) {
1284     return false;
1285   }
1286   if (is_async_deflation_requested()) {
1287     // Async deflation request.
1288     return true;
1289   }
1290   if (AsyncDeflationInterval > 0 &&
1291       time_since_last_async_deflation_ms() > AsyncDeflationInterval &&
1292       monitors_used_above_threshold()) {
1293     // It's been longer than our specified deflate interval and there
1294     // are too many monitors in use. We don't deflate more frequently
1295     // than AsyncDeflationInterval (unless is_async_deflation_requested)
1296     // in order to not swamp the ServiceThread.
1297     _last_async_deflation_time_ns = os::javaTimeNanos();
1298     return true;
1299   }
1300   int monitors_used = g_om_population - g_om_free_count;
1301   if (HandshakeAfterDeflateIdleMonitors) {
1302     monitors_used -= g_om_wait_count;
1303   }
1304   if (is_MonitorBound_exceeded(monitors_used)) {
1305     // Not enough ObjectMonitors on the global free list.
1306     return true;
1307   }
1308   return false;
1309 }
1310 
1311 bool ObjectSynchronizer::is_safepoint_deflation_needed() {
1312   if (!AsyncDeflateIdleMonitors) {
1313     if (monitors_used_above_threshold()) {
1314       // Too many monitors in use.
1315       return true;
1316     }
1317     return false;
1318   }
1319   if (is_special_deflation_requested()) {
1320     // For AsyncDeflateIdleMonitors only do a safepoint deflation
1321     // if there is a special deflation request.
1322     return true;
1323   }
1324   return false;
1325 }
1326 
1327 jlong ObjectSynchronizer::time_since_last_async_deflation_ms() {
1328   return (os::javaTimeNanos() - _last_async_deflation_time_ns) / (NANOUNITS / MILLIUNITS);
1329 }
1330 
1331 void ObjectSynchronizer::oops_do(OopClosure* f) {
1332   // We only scan the global used list here (for moribund threads), and
1333   // the thread-local monitors in Thread::oops_do().
1334   global_used_oops_do(f);
1335 }
1336 
1337 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) {
1338   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1339   list_oops_do(g_om_in_use_list, g_om_in_use_count, f);
1340 }
1341 
1342 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) {
1343   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1344   list_oops_do(thread->om_in_use_list, thread->om_in_use_count, f);
1345 }
1346 
1347 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, int count, OopClosure* f) {
1348   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1349   // The oops_do() phase does not overlap with monitor deflation
1350   // so no need to update the ObjectMonitor's ref_count for this
1351   // ObjectMonitor* use and no need to mark ObjectMonitors for the
1352   // list traversal.
1353   for (ObjectMonitor* mid = list; mid != NULL; mid = unmarked_next(mid)) {
1354     if (mid->object() != NULL) {
1355       f->do_oop((oop*)mid->object_addr());
1356     }
1357   }
1358 }
1359 
1360 
1361 // -----------------------------------------------------------------------------
1362 // ObjectMonitor Lifecycle
1363 // -----------------------
1364 // Inflation unlinks monitors from the global g_free_list and
1365 // associates them with objects.  Deflation -- which occurs at
1366 // STW-time -- disassociates idle monitors from objects.  Such
1367 // scavenged monitors are returned to the g_free_list.
1368 //



1369 // ObjectMonitors reside in type-stable memory (TSM) and are immortal.
1370 //
1371 // Lifecycle:
1372 // --   unassigned and on the global free list
1373 // --   unassigned and on a thread's private om_free_list
1374 // --   assigned to an object.  The object is inflated and the mark refers
1375 //      to the objectmonitor.
1376 
1377 
1378 // Constraining monitor pool growth via MonitorBound ...
1379 //
1380 // If MonitorBound is not set (<= 0), MonitorBound checks are disabled.
1381 //
1382 // When safepoint deflation is being used (!AsyncDeflateIdleMonitors):
1383 // The monitor pool is grow-only.  We scavenge at STW safepoint-time, but the
1384 // the rate of scavenging is driven primarily by GC.  As such,  we can find
1385 // an inordinate number of monitors in circulation.
1386 // To avoid that scenario we can artificially induce a STW safepoint
1387 // if the pool appears to be growing past some reasonable bound.
1388 // Generally we favor time in space-time tradeoffs, but as there's no
1389 // natural back-pressure on the # of extant monitors we need to impose some
1390 // type of limit.  Beware that if MonitorBound is set to too low a value
1391 // we could just loop. In addition, if MonitorBound is set to a low value
1392 // we'll incur more safepoints, which are harmful to performance.
1393 // See also: GuaranteedSafepointInterval
1394 //
1395 // The current implementation uses asynchronous VM operations.
1396 //
1397 // When safepoint deflation is being used and MonitorBound is set, the
1398 // boundry applies to
1399 //     (g_om_population - g_om_free_count)
1400 // i.e., if there are not enough ObjectMonitors on the global free list,
1401 // then a safepoint deflation is induced. Picking a good MonitorBound value
1402 // is non-trivial.
1403 //
1404 // When async deflation is being used:
1405 // The monitor pool is still grow-only. Async deflation is requested
1406 // by a safepoint's cleanup phase or by the ServiceThread at periodic
1407 // intervals when is_async_deflation_needed() returns true. In
1408 // addition to other policies that are checked, if there are not
1409 // enough ObjectMonitors on the global free list, then
1410 // is_async_deflation_needed() will return true. The ServiceThread
1411 // calls deflate_global_idle_monitors_using_JT() and also calls
1412 // deflate_per_thread_idle_monitors_using_JT() as needed.
1413 
1414 static void InduceScavenge(Thread* self, const char * Whence) {
1415   assert(!AsyncDeflateIdleMonitors, "is not used by async deflation");
1416 
1417   // Induce STW safepoint to trim monitors
1418   // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
1419   // More precisely, trigger an asynchronous STW safepoint as the number
1420   // of active monitors passes the specified threshold.
1421   // TODO: assert thread state is reasonable
1422 
1423   if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
1424     // Induce a 'null' safepoint to scavenge monitors
1425     // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
1426     // to the VMthread and have a lifespan longer than that of this activation record.
1427     // The VMThread will delete the op when completed.
1428     VMThread::execute(new VM_ScavengeMonitors());
1429   }
1430 }
1431 
1432 ObjectMonitor* ObjectSynchronizer::om_alloc(Thread* self,
1433                                            const InflateCause cause) {
1434   // A large MAXPRIVATE value reduces both list lock contention
1435   // and list coherency traffic, but also tends to increase the
1436   // number of ObjectMonitors in circulation as well as the STW
1437   // scavenge costs.  As usual, we lean toward time in space-time
1438   // tradeoffs.
1439   const int MAXPRIVATE = 1024;
1440 
1441   stringStream ss;
1442   for (;;) {
1443     ObjectMonitor* m;
1444 
1445     // 1: try to allocate from the thread's local om_free_list.
1446     // Threads will attempt to allocate first from their local list, then
1447     // from the global list, and only after those attempts fail will the
1448     // thread attempt to instantiate new monitors. Thread-local free lists
1449     // improve allocation latency, as well as reducing coherency traffic
1450     // on the shared global list.
1451     m = take_from_start_of_om_free_list(self);
1452     if (m != NULL) {


1453       guarantee(m->object() == NULL, "invariant");
1454       m->set_allocation_state(ObjectMonitor::New);
1455       prepend_to_om_in_use_list(self, m);

1456       return m;
1457     }
1458 
1459     // 2: try to allocate from the global g_free_list
1460     // CONSIDER: use muxTry() instead of muxAcquire().
1461     // If the muxTry() fails then drop immediately into case 3.
1462     // If we're using thread-local free lists then try
1463     // to reprovision the caller's free list.
1464     if (g_free_list != NULL) {
1465       // Reprovision the thread's om_free_list.
1466       // Use bulk transfers to reduce the allocation rate and heat
1467       // on various locks.
1468       for (int i = self->om_free_provision; --i >= 0;) {
1469         ObjectMonitor* take = take_from_start_of_g_free_list();
1470         if (take == NULL) {
1471           break;  // No more are available.
1472         }
1473         guarantee(take->object() == NULL, "invariant");
1474         if (AsyncDeflateIdleMonitors) {
1475           // We allowed 3 field values to linger during async deflation.
1476           // We clear header and restore ref_count here, but we leave
1477           // owner == DEFLATER_MARKER so the simple C2 ObjectMonitor
1478           // enter optimization can no longer race with async deflation
1479           // and reuse.
1480           take->set_header(markWord::zero());
1481           if (take->ref_count() < 0) {
1482             // Add back max_jint to restore the ref_count field to its
1483             // proper value.
1484             Atomic::add(max_jint, &take->_ref_count);
1485 
1486             DEBUG_ONLY(jint l_ref_count = take->ref_count();)
1487             assert(l_ref_count >= 0, "must not be negative: l_ref_count=%d, ref_count=%d",
1488                    l_ref_count, take->ref_count());
1489           }
1490         }
1491         take->Recycle();
1492         // Since we're taking from the global free-list, take must be Free.
1493         // om_release() also sets the allocation state to Free because it
1494         // is called from other code paths.
1495         assert(take->is_free(), "invariant");
1496         om_release(self, take, false);
1497       }
1498       self->om_free_provision += 1 + (self->om_free_provision / 2);

1499       if (self->om_free_provision > MAXPRIVATE) self->om_free_provision = MAXPRIVATE;
1500 
1501       if (!AsyncDeflateIdleMonitors &&
1502           is_MonitorBound_exceeded(g_om_population - g_om_free_count)) {
1503         // Not enough ObjectMonitors on the global free list.
1504         // We can't safely induce a STW safepoint from om_alloc() as our thread
1505         // state may not be appropriate for such activities and callers may hold
1506         // naked oops, so instead we defer the action.
1507         InduceScavenge(self, "om_alloc");
1508       }
1509       continue;
1510     }
1511 
1512     // 3: allocate a block of new ObjectMonitors
1513     // Both the local and global free lists are empty -- resort to malloc().
1514     // In the current implementation ObjectMonitors are TSM - immortal.
1515     // Ideally, we'd write "new ObjectMonitor[_BLOCKSIZE], but we want
1516     // each ObjectMonitor to start at the beginning of a cache line,
1517     // so we use align_up().
1518     // A better solution would be to use C++ placement-new.
1519     // BEWARE: As it stands currently, we don't run the ctors!
1520     assert(_BLOCKSIZE > 1, "invariant");
1521     size_t neededsize = sizeof(PaddedObjectMonitor) * _BLOCKSIZE;
1522     PaddedObjectMonitor* temp;
1523     size_t aligned_size = neededsize + (OM_CACHE_LINE_SIZE - 1);
1524     void* real_malloc_addr = NEW_C_HEAP_ARRAY(char, aligned_size, mtInternal);
1525     temp = (PaddedObjectMonitor*)align_up(real_malloc_addr, OM_CACHE_LINE_SIZE);
1526     (void)memset((void *) temp, 0, neededsize);
1527 
1528     // Format the block.
1529     // initialize the linked list, each monitor points to its next
1530     // forming the single linked free list, the very first monitor
1531     // will points to next block, which forms the block list.
1532     // The trick of using the 1st element in the block as g_block_list
1533     // linkage should be reconsidered.  A better implementation would
1534     // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
1535 
1536     for (int i = 1; i < _BLOCKSIZE; i++) {
1537       temp[i]._next_om = (ObjectMonitor*)&temp[i + 1];
1538       assert(temp[i].is_free(), "invariant");
1539     }
1540 
1541     // terminate the last monitor as the end of list
1542     temp[_BLOCKSIZE - 1]._next_om = (ObjectMonitor*)NULL;
1543 
1544     // Element [0] is reserved for global list linkage
1545     temp[0].set_object(CHAINMARKER);
1546 
1547     // Consider carving out this thread's current request from the
1548     // block in hand.  This avoids some lock traffic and redundant
1549     // list activity.
1550 
1551     prepend_block_to_lists(temp);

















1552   }
1553 }
1554 
1555 // Place "m" on the caller's private per-thread om_free_list.
1556 // In practice there's no need to clamp or limit the number of
1557 // monitors on a thread's om_free_list as the only non-allocation time
1558 // we'll call om_release() is to return a monitor to the free list after
1559 // a CAS attempt failed. This doesn't allow unbounded #s of monitors to
1560 // accumulate on a thread's free list.
1561 //
1562 // Key constraint: all ObjectMonitors on a thread's free list and the global
1563 // free list must have their object field set to null. This prevents the
1564 // scavenger -- deflate_monitor_list() or deflate_monitor_list_using_JT()
1565 // -- from reclaiming them while we are trying to release them.
1566 
1567 void ObjectSynchronizer::om_release(Thread* self, ObjectMonitor* m,
1568                                     bool from_per_thread_alloc) {
1569   guarantee(m->header().value() == 0, "invariant");
1570   guarantee(m->object() == NULL, "invariant");
1571   stringStream ss;
1572   guarantee((m->is_busy() | m->_recursions) == 0, "freeing in-use monitor: "
1573             "%s, recursions=" INTX_FORMAT, m->is_busy_to_string(&ss),
1574             m->_recursions);
1575   m->set_allocation_state(ObjectMonitor::Free);
1576   // _next_om is used for both per-thread in-use and free lists so
1577   // we have to remove 'm' from the in-use list first (as needed).
1578   if (from_per_thread_alloc) {
1579     // Need to remove 'm' from om_in_use_list.
1580     // We use the more complicated mark-cur_mid_in_use-and-mid-as-we-go
1581     // protocol because async deflation can do list deletions in parallel.
1582     ObjectMonitor* cur_mid_in_use = NULL;
1583     ObjectMonitor* mid = NULL;
1584     ObjectMonitor* next = NULL;
1585     bool extracted = false;
1586 
1587     if (!mark_list_head(&self->om_in_use_list, &mid, &next)) {
1588       fatal("thread=" INTPTR_FORMAT " in-use list must not be empty.", p2i(self));
1589     }
1590     while (true) {
1591       if (m == mid) {
1592         // We found 'm' on the per-thread in-use list so try to extract it.
1593         if (cur_mid_in_use == NULL) {
1594           // mid is the list head and it is marked. Switch the list head
1595           // to next which unmarks the list head, but leaves mid marked:
1596           self->om_in_use_list = next;
1597           // mark_list_head() used cmpxchg() above, switching list head can be lazier:
1598           OrderAccess::storestore();
1599         } else {
1600           // mid and cur_mid_in_use are marked. Switch cur_mid_in_use's
1601           // next field to next which unmarks cur_mid_in_use, but leaves
1602           // mid marked:
1603           OrderAccess::release_store(&cur_mid_in_use->_next_om, next);
1604         }
1605         extracted = true;
1606         Atomic::dec(&self->om_in_use_count);
1607         // Unmark mid, but leave the next value for any lagging list
1608         // walkers. It will get cleaned up when mid is prepended to
1609         // the thread's free list:
1610         set_next(mid, next);
1611         break;
1612       }
1613       if (cur_mid_in_use != NULL) {
1614         set_next(cur_mid_in_use, mid);  // umark cur_mid_in_use
1615       }
1616       // The next cur_mid_in_use keeps mid's marked next field so
1617       // that it is stable for a possible next field change. It
1618       // cannot be deflated while it is marked.
1619       cur_mid_in_use = mid;
1620       mid = next;
1621       if (mid == NULL) {
1622         // Reached end of the list and didn't find m so:
1623         fatal("must find m=" INTPTR_FORMAT "on om_in_use_list=" INTPTR_FORMAT,
1624               p2i(m), p2i(self->om_in_use_list));
1625       }
1626       // Mark mid's next field so we can possibly extract it:
1627       next = mark_next_loop(mid);
1628     }

1629   }
1630 
1631   prepend_to_om_free_list(self, m);
1632   guarantee(m->is_free(), "invariant");

1633 }
1634 
1635 // Return ObjectMonitors on a moribund thread's free and in-use
1636 // lists to the appropriate global lists. The ObjectMonitors on the
1637 // per-thread in-use list may still be in use by other threads.
1638 //
1639 // We currently call om_flush() from Threads::remove() before the
1640 // thread has been excised from the thread list and is no longer a
1641 // mutator. This means that om_flush() cannot run concurrently with
1642 // a safepoint and interleave with deflate_idle_monitors(). In
1643 // particular, this ensures that the thread's in-use monitors are
1644 // scanned by a GC safepoint, either via Thread::oops_do() (before
1645 // om_flush() is called) or via ObjectSynchronizer::oops_do() (after
1646 // om_flush() is called).
1647 //
1648 // With AsyncDeflateIdleMonitors, deflate_global_idle_monitors_using_JT()
1649 // and deflate_per_thread_idle_monitors_using_JT() (in another thread) can
1650 // run at the same time as om_flush() so we have to follow a careful
1651 // protocol to prevent list corruption.
1652 
1653 void ObjectSynchronizer::om_flush(Thread* self) {
1654   // This function can race with an async deflater thread. Since
1655   // deflation has to process the per-thread in-use list before
1656   // prepending the deflated ObjectMonitors to the global free list,
1657   // we process the per-thread lists in the same order to prevent
1658   // ordering races.
1659   int in_use_count = 0;
1660   ObjectMonitor* in_use_list = NULL;
1661   ObjectMonitor* in_use_tail = NULL;
1662   ObjectMonitor* next = NULL;
1663 
1664   // An async deflation thread checks to see if the target thread
1665   // is exiting, but if it has made it past that check before we
1666   // started exiting, then it is racing to get to the in-use list.
1667   if (mark_list_head(&self->om_in_use_list, &in_use_list, &next)) {
1668     // At this point, we have marked the in-use list head so an
1669     // async deflation thread cannot come in after us. If an async
1670     // deflation thread is ahead of us, then we'll detect that and
1671     // wait for it to finish its work.
1672     //
1673     // The thread is going away, however the ObjectMonitors on the
1674     // om_in_use_list may still be in-use by other threads. Link
1675     // them to in_use_tail, which will be linked into the global
1676     // in-use list g_om_in_use_list below.
1677     //
1678     // Account for the in-use list head before the loop since it is
1679     // already marked (by this thread):
1680     in_use_tail = in_use_list;
1681     in_use_count++;
1682     for (ObjectMonitor* cur_om = unmarked_next(in_use_list); cur_om != NULL;) {
1683       if (is_next_marked(cur_om)) {
1684         // This next field is marked so there must be an async deflater
1685         // thread ahead of us so we'll give it a chance to finish.
1686         while (is_next_marked(cur_om)) {
1687           os::naked_short_sleep(1);
1688         }
1689         // Refetch the possibly changed next field and try again.
1690         cur_om = unmarked_next(in_use_tail);
1691         continue;
1692       }
1693       if (cur_om->is_free()) {
1694         // cur_om was deflated and the allocation state was changed
1695         // to Free while it was marked. We happened to see it just
1696         // after it was unmarked (and added to the free list).
1697         // Refetch the possibly changed next field and try again.
1698         cur_om = unmarked_next(in_use_tail);
1699         continue;
1700       }
1701       in_use_tail = cur_om;
1702       in_use_count++;
1703       cur_om = unmarked_next(cur_om);
1704     }
1705     guarantee(in_use_tail != NULL, "invariant");
1706     int l_om_in_use_count = self->om_in_use_count;
1707     ADIM_guarantee(l_om_in_use_count == in_use_count, "in-use counts don't "
1708                    "match: l_om_in_use_count=%d, in_use_count=%d",
1709                    l_om_in_use_count, in_use_count);
1710     self->om_in_use_count = 0;
1711     // Clear the in-use list head (which also unmarks it):
1712     self->om_in_use_list = (ObjectMonitor*)NULL;
1713     // mark_list_head() used cmpxchg() above, clearing the disconnected list head can be lazier:
1714     OrderAccess::storestore();
1715     set_next(in_use_list, next);
1716   }
1717 
1718   int free_count = 0;
1719   ObjectMonitor* free_list = self->om_free_list;
1720   ObjectMonitor* free_tail = NULL;

1721   if (free_list != NULL) {

1722     // The thread is going away. Set 'free_tail' to the last per-thread free
1723     // monitor which will be linked to g_free_list below.
1724     stringStream ss;
1725     for (ObjectMonitor* s = free_list; s != NULL; s = unmarked_next(s)) {
1726       free_count++;
1727       free_tail = s;
1728       guarantee(s->object() == NULL, "invariant");
1729       guarantee(!s->is_busy(), "must be !is_busy: %s", s->is_busy_to_string(&ss));
1730     }
1731     guarantee(free_tail != NULL, "invariant");
1732     int l_om_free_count = self->om_free_count;
1733     ADIM_guarantee(l_om_free_count == free_count, "free counts don't match: "
1734                    "l_om_free_count=%d, free_count=%d", l_om_free_count,
1735                    free_count);
1736     self->om_free_count = 0;
1737     self->om_free_list = NULL;
1738     OrderAccess::storestore();  // Lazier memory is okay for list walkers.
1739   }
1740 




















1741   if (free_tail != NULL) {
1742     prepend_list_to_g_free_list(free_list, free_tail, free_count);


1743   }
1744 
1745   if (in_use_tail != NULL) {
1746     prepend_list_to_g_om_in_use_list(in_use_list, in_use_tail, in_use_count);


1747   }
1748 


1749   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1750   LogStreamHandle(Info, monitorinflation) lsh_info;
1751   LogStream* ls = NULL;
1752   if (log_is_enabled(Debug, monitorinflation)) {
1753     ls = &lsh_debug;
1754   } else if ((free_count != 0 || in_use_count != 0) &&
1755              log_is_enabled(Info, monitorinflation)) {
1756     ls = &lsh_info;
1757   }
1758   if (ls != NULL) {
1759     ls->print_cr("om_flush: jt=" INTPTR_FORMAT ", free_count=%d"
1760                  ", in_use_count=%d" ", om_free_provision=%d",
1761                  p2i(self), free_count, in_use_count, self->om_free_provision);
1762   }
1763 }
1764 
1765 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1766                                        const oop obj,
1767                                        ObjectSynchronizer::InflateCause cause) {
1768   assert(event != NULL, "invariant");
1769   assert(event->should_commit(), "invariant");
1770   event->set_monitorClass(obj->klass());
1771   event->set_address((uintptr_t)(void*)obj);
1772   event->set_cause((u1)cause);
1773   event->commit();
1774 }
1775 
1776 // Fast path code shared by multiple functions
1777 void ObjectSynchronizer::inflate_helper(ObjectMonitorHandle* omh_p, oop obj) {
1778   while (true) {
1779     markWord mark = obj->mark();
1780     if (mark.has_monitor()) {
1781       if (!omh_p->save_om_ptr(obj, mark)) {
1782         // Lost a race with async deflation so try again.
1783         assert(AsyncDeflateIdleMonitors, "sanity check");
1784         continue;
1785       }
1786       ObjectMonitor* monitor = omh_p->om_ptr();
1787       assert(ObjectSynchronizer::verify_objmon_isinpool(monitor), "monitor is invalid");
1788       markWord dmw = monitor->header();
1789       assert(dmw.is_neutral(), "sanity check: header=" INTPTR_FORMAT, dmw.value());
1790       return;
1791     }
1792     inflate(omh_p, Thread::current(), obj, inflate_cause_vm_internal);
1793     return;
1794   }

1795 }
1796 
1797 void ObjectSynchronizer::inflate(ObjectMonitorHandle* omh_p, Thread* self,
1798                                  oop object, const InflateCause cause) {

1799   // Inflate mutates the heap ...
1800   // Relaxing assertion for bug 6320749.
1801   assert(Universe::verify_in_progress() ||
1802          !SafepointSynchronize::is_at_safepoint(), "invariant");
1803 
1804   EventJavaMonitorInflate event;
1805 
1806   for (;;) {
1807     const markWord mark = object->mark();
1808     assert(!mark.has_bias_pattern(), "invariant");
1809 
1810     // The mark can be in one of the following states:
1811     // *  Inflated     - just return
1812     // *  Stack-locked - coerce it to inflated
1813     // *  INFLATING    - busy wait for conversion to complete
1814     // *  Neutral      - aggressively inflate the object.
1815     // *  BIASED       - Illegal.  We should never see this
1816 
1817     // CASE: inflated
1818     if (mark.has_monitor()) {
1819       if (!omh_p->save_om_ptr(object, mark)) {
1820         // Lost a race with async deflation so try again.
1821         assert(AsyncDeflateIdleMonitors, "sanity check");
1822         continue;
1823       }
1824       ObjectMonitor* inf = omh_p->om_ptr();
1825       markWord dmw = inf->header();
1826       assert(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1827       assert(inf->object() == object, "invariant");
1828       assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1829       return;
1830     }
1831 
1832     // CASE: inflation in progress - inflating over a stack-lock.
1833     // Some other thread is converting from stack-locked to inflated.
1834     // Only that thread can complete inflation -- other threads must wait.
1835     // The INFLATING value is transient.
1836     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1837     // We could always eliminate polling by parking the thread on some auxiliary list.
1838     if (mark == markWord::INFLATING()) {
1839       read_stable_mark(object);
1840       continue;
1841     }
1842 
1843     // CASE: stack-locked
1844     // Could be stack-locked either by this thread or by some other thread.
1845     //
1846     // Note that we allocate the objectmonitor speculatively, _before_ attempting
1847     // to install INFLATING into the mark word.  We originally installed INFLATING,
1848     // allocated the objectmonitor, and then finally STed the address of the
1849     // objectmonitor into the mark.  This was correct, but artificially lengthened
1850     // the interval in which INFLATED appeared in the mark, thus increasing
1851     // the odds of inflation contention.
1852     //
1853     // We now use per-thread private objectmonitor free lists.
1854     // These list are reprovisioned from the global free list outside the
1855     // critical INFLATING...ST interval.  A thread can transfer
1856     // multiple objectmonitors en-mass from the global free list to its local free list.
1857     // This reduces coherency traffic and lock contention on the global free list.
1858     // Using such local free lists, it doesn't matter if the om_alloc() call appears
1859     // before or after the CAS(INFLATING) operation.
1860     // See the comments in om_alloc().
1861 
1862     LogStreamHandle(Trace, monitorinflation) lsh;
1863 
1864     if (mark.has_locker()) {
1865       ObjectMonitor* m = om_alloc(self, cause);
1866       // Optimistically prepare the objectmonitor - anticipate successful CAS
1867       // We do this before the CAS in order to minimize the length of time
1868       // in which INFLATING appears in the mark.
1869       m->Recycle();
1870       m->_Responsible  = NULL;
1871       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;   // Consider: maintain by type/class
1872 
1873       markWord cmp = object->cas_set_mark(markWord::INFLATING(), mark);
1874       if (cmp != mark) {
1875         // om_release() will reset the allocation state from New to Free.
1876         om_release(self, m, true);
1877         continue;       // Interference -- just retry
1878       }
1879 
1880       // We've successfully installed INFLATING (0) into the mark-word.
1881       // This is the only case where 0 will appear in a mark-word.
1882       // Only the singular thread that successfully swings the mark-word
1883       // to 0 can perform (or more precisely, complete) inflation.
1884       //
1885       // Why do we CAS a 0 into the mark-word instead of just CASing the
1886       // mark-word from the stack-locked value directly to the new inflated state?
1887       // Consider what happens when a thread unlocks a stack-locked object.
1888       // It attempts to use CAS to swing the displaced header value from the
1889       // on-stack BasicLock back into the object header.  Recall also that the
1890       // header value (hash code, etc) can reside in (a) the object header, or
1891       // (b) a displaced header associated with the stack-lock, or (c) a displaced
1892       // header in an ObjectMonitor.  The inflate() routine must copy the header
1893       // value from the BasicLock on the owner's stack to the ObjectMonitor, all
1894       // the while preserving the hashCode stability invariants.  If the owner
1895       // decides to release the lock while the value is 0, the unlock will fail
1896       // and control will eventually pass from slow_exit() to inflate.  The owner
1897       // will then spin, waiting for the 0 value to disappear.   Put another way,
1898       // the 0 causes the owner to stall if the owner happens to try to
1899       // drop the lock (restoring the header from the BasicLock to the object)
1900       // while inflation is in-progress.  This protocol avoids races that might
1901       // would otherwise permit hashCode values to change or "flicker" for an object.
1902       // Critically, while object->mark is 0 mark.displaced_mark_helper() is stable.
1903       // 0 serves as a "BUSY" inflate-in-progress indicator.
1904 
1905 
1906       // fetch the displaced mark from the owner's stack.
1907       // The owner can't die or unwind past the lock while our INFLATING
1908       // object is in the mark.  Furthermore the owner can't complete
1909       // an unlock on the object, either.
1910       markWord dmw = mark.displaced_mark_helper();
1911       // Catch if the object's header is not neutral (not locked and
1912       // not marked is what we care about here).
1913       ADIM_guarantee(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
1914 
1915       // Setup monitor fields to proper values -- prepare the monitor
1916       m->set_header(dmw);
1917 
1918       // Optimization: if the mark.locker stack address is associated
1919       // with this thread we could simply set m->_owner = self.
1920       // Note that a thread can inflate an object
1921       // that it has stack-locked -- as might happen in wait() -- directly
1922       // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1923       if (AsyncDeflateIdleMonitors) {
1924         m->simply_set_owner_from(mark.locker(), NULL, DEFLATER_MARKER);
1925       } else {
1926         m->simply_set_owner_from(mark.locker(), NULL);
1927       }
1928       m->set_object(object);
1929       // TODO-FIXME: assert BasicLock->dhw != 0.
1930 
1931       omh_p->set_om_ptr(m);
1932 
1933       // Must preserve store ordering. The monitor state must
1934       // be stable at the time of publishing the monitor address.
1935       guarantee(object->mark() == markWord::INFLATING(), "invariant");
1936       object->release_set_mark(markWord::encode(m));
1937 
1938       // Once ObjectMonitor is configured and the object is associated
1939       // with the ObjectMonitor, it is safe to allow async deflation:
1940       assert(m->is_new(), "freshly allocated monitor must be new");
1941       m->set_allocation_state(ObjectMonitor::Old);
1942 
1943       // Hopefully the performance counters are allocated on distinct cache lines
1944       // to avoid false sharing on MP systems ...
1945       OM_PERFDATA_OP(Inflations, inc());
1946       if (log_is_enabled(Trace, monitorinflation)) {
1947         ResourceMark rm(self);
1948         lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1949                      INTPTR_FORMAT ", type='%s'", p2i(object),
1950                      object->mark().value(), object->klass()->external_name());
1951       }
1952       if (event.should_commit()) {
1953         post_monitor_inflate_event(&event, object, cause);
1954       }
1955       ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free");
1956       return;
1957     }
1958 
1959     // CASE: neutral
1960     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1961     // If we know we're inflating for entry it's better to inflate by swinging a
1962     // pre-locked ObjectMonitor pointer into the object header.   A successful
1963     // CAS inflates the object *and* confers ownership to the inflating thread.
1964     // In the current implementation we use a 2-step mechanism where we CAS()
1965     // to inflate and then CAS() again to try to swing _owner from NULL to self.
1966     // An inflateTry() method that we could call from enter() would be useful.
1967 
1968     // Catch if the object's header is not neutral (not locked and
1969     // not marked is what we care about here).
1970     ADIM_guarantee(mark.is_neutral(), "invariant: header=" INTPTR_FORMAT,mark.value());
1971     ObjectMonitor* m = om_alloc(self, cause);
1972     // prepare m for installation - set monitor to initial state
1973     m->Recycle();
1974     m->set_header(mark);
1975     // If we leave _owner == DEFLATER_MARKER here, then the simple C2
1976     // ObjectMonitor enter optimization can no longer race with async
1977     // deflation and reuse.
1978     m->set_object(object);
1979     m->_Responsible  = NULL;
1980     m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;       // consider: keep metastats by type/class
1981 
1982     omh_p->set_om_ptr(m);
1983 
1984     if (object->cas_set_mark(markWord::encode(m), mark) != mark) {
1985       m->set_header(markWord::zero());
1986       m->set_object(NULL);
1987       m->Recycle();
1988       omh_p->set_om_ptr(NULL);
1989       // om_release() will reset the allocation state from New to Free.
1990       om_release(self, m, true);
1991       m = NULL;
1992       continue;
1993       // interference - the markword changed - just retry.
1994       // The state-transitions are one-way, so there's no chance of
1995       // live-lock -- "Inflated" is an absorbing state.
1996     }
1997 
1998     // Once the ObjectMonitor is configured and object is associated
1999     // with the ObjectMonitor, it is safe to allow async deflation:
2000     assert(m->is_new(), "freshly allocated monitor must be new");
2001     m->set_allocation_state(ObjectMonitor::Old);
2002 
2003     // Hopefully the performance counters are allocated on distinct
2004     // cache lines to avoid false sharing on MP systems ...
2005     OM_PERFDATA_OP(Inflations, inc());
2006     if (log_is_enabled(Trace, monitorinflation)) {
2007       ResourceMark rm(self);
2008       lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
2009                    INTPTR_FORMAT ", type='%s'", p2i(object),
2010                    object->mark().value(), object->klass()->external_name());
2011     }
2012     if (event.should_commit()) {
2013       post_monitor_inflate_event(&event, object, cause);
2014     }
2015     ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free");
2016     return;
2017   }
2018 }
2019 
2020 
2021 // We maintain a list of in-use monitors for each thread.
2022 //
2023 // For safepoint based deflation:
2024 // deflate_thread_local_monitors() scans a single thread's in-use list, while
2025 // deflate_idle_monitors() scans only a global list of in-use monitors which
2026 // is populated only as a thread dies (see om_flush()).
2027 //
2028 // These operations are called at all safepoints, immediately after mutators
2029 // are stopped, but before any objects have moved. Collectively they traverse
2030 // the population of in-use monitors, deflating where possible. The scavenged
2031 // monitors are returned to the global monitor free list.
2032 //
2033 // Beware that we scavenge at *every* stop-the-world point. Having a large
2034 // number of monitors in-use could negatively impact performance. We also want
2035 // to minimize the total # of monitors in circulation, as they incur a small
2036 // footprint penalty.
2037 //
2038 // Perversely, the heap size -- and thus the STW safepoint rate --
2039 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
2040 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
2041 // This is an unfortunate aspect of this design.
2042 //
2043 // For async deflation:
2044 // If a special deflation request is made, then the safepoint based
2045 // deflation mechanism is used. Otherwise, an async deflation request
2046 // is registered with the ServiceThread and it is notified.
2047 
2048 void ObjectSynchronizer::do_safepoint_work(DeflateMonitorCounters* counters) {
2049   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2050 
2051   // The per-thread in-use lists are handled in
2052   // ParallelSPCleanupThreadClosure::do_thread().
2053 
2054   if (!AsyncDeflateIdleMonitors || is_special_deflation_requested()) {
2055     // Use the older mechanism for the global in-use list or if a
2056     // special deflation has been requested before the safepoint.
2057     ObjectSynchronizer::deflate_idle_monitors(counters);
2058     return;
2059   }
2060 
2061   log_debug(monitorinflation)("requesting async deflation of idle monitors.");
2062   // Request deflation of idle monitors by the ServiceThread:
2063   set_is_async_deflation_requested(true);
2064   MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
2065   ml.notify_all();
2066 
2067   if (log_is_enabled(Debug, monitorinflation)) {
2068     // exit_globals()'s call to audit_and_print_stats() is done
2069     // at the Info level and not at a safepoint.
2070     // For safepoint based deflation, audit_and_print_stats() is called
2071     // in ObjectSynchronizer::finish_deflate_idle_monitors() at the
2072     // Debug level at a safepoint.
2073     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2074   }
2075 }
2076 
2077 // Deflate a single monitor if not in-use
2078 // Return true if deflated, false if in-use
2079 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
2080                                          ObjectMonitor** free_head_p,
2081                                          ObjectMonitor** free_tail_p) {
2082   bool deflated;
2083   // Normal case ... The monitor is associated with obj.
2084   const markWord mark = obj->mark();
2085   guarantee(mark == markWord::encode(mid), "should match: mark="
2086             INTPTR_FORMAT ", encoded mid=" INTPTR_FORMAT, mark.value(),
2087             markWord::encode(mid).value());
2088   // Make sure that mark.monitor() and markWord::encode() agree:
2089   guarantee(mark.monitor() == mid, "should match: monitor()=" INTPTR_FORMAT
2090             ", mid=" INTPTR_FORMAT, p2i(mark.monitor()), p2i(mid));
2091   const markWord dmw = mid->header();
2092   guarantee(dmw.is_neutral(), "invariant: header=" INTPTR_FORMAT, dmw.value());
2093 
2094   if (mid->is_busy() || mid->ref_count() != 0) {
2095     // Easy checks are first - the ObjectMonitor is busy or ObjectMonitor*
2096     // is in use so no deflation.
2097     deflated = false;
2098   } else {
2099     // Deflate the monitor if it is no longer being used
2100     // It's idle - scavenge and return to the global free list
2101     // plain old deflation ...
2102     if (log_is_enabled(Trace, monitorinflation)) {
2103       ResourceMark rm;
2104       log_trace(monitorinflation)("deflate_monitor: "
2105                                   "object=" INTPTR_FORMAT ", mark="
2106                                   INTPTR_FORMAT ", type='%s'", p2i(obj),
2107                                   mark.value(), obj->klass()->external_name());
2108     }
2109 
2110     // Restore the header back to obj
2111     obj->release_set_mark(dmw);
2112     if (AsyncDeflateIdleMonitors) {
2113       // clear() expects the owner field to be NULL and we won't race
2114       // with the simple C2 ObjectMonitor enter optimization since
2115       // we're at a safepoint. DEFLATER_MARKER is the only non-NULL
2116       // value we should see here.
2117       mid->try_set_owner_from(NULL, DEFLATER_MARKER);
2118     }
2119     mid->clear();
2120 
2121     assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT,
2122            p2i(mid->object()));
2123     assert(mid->is_free(), "invariant");
2124 
2125     // Move the deflated ObjectMonitor to the working free list
2126     // defined by free_head_p and free_tail_p. No races on this list
2127     // so no need for load_acquire() or store_release().
2128     if (*free_head_p == NULL) *free_head_p = mid;
2129     if (*free_tail_p != NULL) {
2130       // We append to the list so the caller can use mid->_next_om
2131       // to fix the linkages in its context.
2132       ObjectMonitor* prevtail = *free_tail_p;
2133       // Should have been cleaned up by the caller:
2134       // Note: Should not have to mark prevtail here since we're at a
2135       // safepoint and ObjectMonitors on the local free list should
2136       // not be accessed in parallel.
2137       assert(prevtail->_next_om == NULL, "must be NULL: _next_om="
2138              INTPTR_FORMAT, p2i(prevtail->_next_om));
2139       set_next(prevtail, mid);
2140     }
2141     *free_tail_p = mid;
2142     // At this point, mid->_next_om still refers to its current
2143     // value and another ObjectMonitor's _next_om field still
2144     // refers to this ObjectMonitor. Those linkages have to be
2145     // cleaned up by the caller who has the complete context.
2146     deflated = true;
2147   }
2148   return deflated;
2149 }
2150 
2151 // Deflate the specified ObjectMonitor if not in-use using a JavaThread.
2152 // Returns true if it was deflated and false otherwise.
2153 //
2154 // The async deflation protocol sets owner to DEFLATER_MARKER and
2155 // makes ref_count negative as signals to contending threads that
2156 // an async deflation is in progress. There are a number of checks
2157 // as part of the protocol to make sure that the calling thread has
2158 // not lost the race to a contending thread or to a thread that just
2159 // wants to use the ObjectMonitor*.
2160 //
2161 // The ObjectMonitor has been successfully async deflated when:
2162 // (owner == DEFLATER_MARKER && ref_count < 0)
2163 // Contending threads or ObjectMonitor* using threads that see those
2164 // values know to retry their operation.
2165 //
2166 bool ObjectSynchronizer::deflate_monitor_using_JT(ObjectMonitor* mid,
2167                                                   ObjectMonitor** free_head_p,
2168                                                   ObjectMonitor** free_tail_p) {
2169   assert(AsyncDeflateIdleMonitors, "sanity check");
2170   assert(Thread::current()->is_Java_thread(), "precondition");
2171   // A newly allocated ObjectMonitor should not be seen here so we
2172   // avoid an endless inflate/deflate cycle.
2173   assert(mid->is_old(), "must be old: allocation_state=%d",
2174          (int) mid->allocation_state());
2175 
2176   if (mid->is_busy() || mid->ref_count() != 0) {
2177     // Easy checks are first - the ObjectMonitor is busy or ObjectMonitor*
2178     // is in use so no deflation.
2179     return false;
2180   }
2181 
2182   if (mid->try_set_owner_from(DEFLATER_MARKER, NULL) == NULL) {
2183     // ObjectMonitor is not owned by another thread. Our setting
2184     // owner to DEFLATER_MARKER forces any contending thread through
2185     // the slow path. This is just the first part of the async
2186     // deflation dance.
2187 
2188     if (mid->_contentions != 0 || mid->_waiters != 0) {
2189       // Another thread has raced to enter the ObjectMonitor after
2190       // mid->is_busy() above or has already entered and waited on
2191       // it which makes it busy so no deflation. Restore owner to
2192       // NULL if it is still DEFLATER_MARKER.
2193       mid->try_set_owner_from(NULL, DEFLATER_MARKER);
2194       return false;
2195     }
2196 
2197     if (Atomic::cmpxchg(-max_jint, &mid->_ref_count, (jint)0) == 0) {
2198       // Make ref_count negative to force any contending threads or
2199       // ObjectMonitor* using threads to retry. This is the second
2200       // part of the async deflation dance.
2201 
2202       if (mid->owner_is_DEFLATER_MARKER()) {
2203         // If owner is still DEFLATER_MARKER, then we have successfully
2204         // signaled any contending threads to retry. If it is not, then we
2205         // have lost the race to an entering thread and the ObjectMonitor
2206         // is now busy. This is the third and final part of the async
2207         // deflation dance.
2208         // Note: This owner check solves the ABA problem with ref_count
2209         // where another thread acquired the ObjectMonitor, finished
2210         // using it and restored the ref_count to zero.
2211 
2212         // Sanity checks for the races:
2213         guarantee(mid->_contentions == 0, "must be 0: contentions=%d",
2214                   mid->_contentions);
2215         guarantee(mid->_waiters == 0, "must be 0: waiters=%d", mid->_waiters);
2216         guarantee(mid->_cxq == NULL, "must be no contending threads: cxq="
2217                   INTPTR_FORMAT, p2i(mid->_cxq));
2218         guarantee(mid->_EntryList == NULL,
2219                   "must be no entering threads: EntryList=" INTPTR_FORMAT,
2220                   p2i(mid->_EntryList));
2221 
2222         const oop obj = (oop) mid->object();
2223         if (log_is_enabled(Trace, monitorinflation)) {
2224           ResourceMark rm;
2225           log_trace(monitorinflation)("deflate_monitor_using_JT: "
2226                                       "object=" INTPTR_FORMAT ", mark="
2227                                       INTPTR_FORMAT ", type='%s'",
2228                                       p2i(obj), obj->mark().value(),
2229                                       obj->klass()->external_name());
2230         }
2231 
2232         // Install the old mark word if nobody else has already done it.
2233         mid->install_displaced_markword_in_object(obj);
2234         mid->clear_using_JT();
2235 
2236         assert(mid->object() == NULL, "must be NULL: object=" INTPTR_FORMAT,
2237                p2i(mid->object()));
2238         assert(mid->is_free(), "must be free: allocation_state=%d",
2239                (int) mid->allocation_state());
2240 
2241         // Move the deflated ObjectMonitor to the working free list
2242         // defined by free_head_p and free_tail_p. No races on this list
2243         // so no need for load_acquire() or store_release().
2244         if (*free_head_p == NULL) {
2245           // First one on the list.
2246           *free_head_p = mid;
2247         }
2248         if (*free_tail_p != NULL) {
2249           // We append to the list so the caller can use mid->_next_om
2250           // to fix the linkages in its context.
2251           ObjectMonitor* prevtail = *free_tail_p;
2252           // Should have been cleaned up by the caller:
2253           ObjectMonitor* next = mark_next_loop(prevtail);
2254           assert(unmarked_next(prevtail) == NULL, "must be NULL: _next_om="
2255                  INTPTR_FORMAT, p2i(unmarked_next(prevtail)));
2256           set_next(prevtail, mid);  // prevtail now points to mid (and is unmarked)
2257         }
2258         *free_tail_p = mid;
2259 
2260         // At this point, mid->_next_om still refers to its current
2261         // value and another ObjectMonitor's _next_om field still
2262         // refers to this ObjectMonitor. Those linkages have to be
2263         // cleaned up by the caller who has the complete context.
2264 
2265         // We leave owner == DEFLATER_MARKER and ref_count < 0
2266         // to force any racing threads to retry.
2267         return true;  // Success, ObjectMonitor has been deflated.
2268       }
2269 
2270       // The owner was changed from DEFLATER_MARKER so we lost the
2271       // race since the ObjectMonitor is now busy.
2272 
2273       // Add back max_jint to restore the ref_count field to its
2274       // proper value (which may not be what we saw above):
2275       Atomic::add(max_jint, &mid->_ref_count);
2276 
2277       DEBUG_ONLY(jint l_ref_count = mid->ref_count();)
2278       assert(l_ref_count >= 0, "must not be negative: l_ref_count=%d, ref_count=%d",
2279              l_ref_count, mid->ref_count());
2280       return false;
2281     }
2282 
2283     // The ref_count was no longer 0 so we lost the race since the
2284     // ObjectMonitor is now busy or the ObjectMonitor* is now is use.
2285     // Restore owner to NULL if it is still DEFLATER_MARKER:
2286     mid->try_set_owner_from(NULL, DEFLATER_MARKER);
2287   }
2288 
2289   // The owner field is no longer NULL so we lost the race since the
2290   // ObjectMonitor is now busy.
2291   return false;
2292 }
2293 
2294 // Walk a given monitor list, and deflate idle monitors.
2295 // The given list could be a per-thread list or a global list.
2296 //
2297 // In the case of parallel processing of thread local monitor lists,
2298 // work is done by Threads::parallel_threads_do() which ensures that
2299 // each Java thread is processed by exactly one worker thread, and
2300 // thus avoid conflicts that would arise when worker threads would
2301 // process the same monitor lists concurrently.
2302 //
2303 // See also ParallelSPCleanupTask and
2304 // SafepointSynchronize::do_cleanup_tasks() in safepoint.cpp and
2305 // Threads::parallel_java_threads_do() in thread.cpp.
2306 int ObjectSynchronizer::deflate_monitor_list(ObjectMonitor* volatile * list_p,
2307                                              int volatile * count_p,
2308                                              ObjectMonitor** free_head_p,
2309                                              ObjectMonitor** free_tail_p) {


2310   ObjectMonitor* cur_mid_in_use = NULL;
2311   ObjectMonitor* mid = NULL;
2312   ObjectMonitor* next = NULL;
2313   int deflated_count = 0;
2314 
2315   // We use the simpler mark-mid-as-we-go protocol since there are no
2316   // parallel list deletions since we are at a safepoint.
2317   if (!mark_list_head(list_p, &mid, &next)) {
2318     return 0;  // The list is empty so nothing to deflate.
2319   }
2320 
2321   while (true) {
2322     oop obj = (oop) mid->object();
2323     if (obj != NULL && deflate_monitor(mid, obj, free_head_p, free_tail_p)) {
2324       // Deflation succeeded and already updated free_head_p and
2325       // free_tail_p as needed. Finish the move to the local free list
2326       // by unlinking mid from the global or per-thread in-use list.
2327       if (cur_mid_in_use == NULL) {
2328         // mid is the list head and it is marked. Switch the list head
2329         // to next which unmarks the list head, but leaves mid marked:
2330         *list_p = next;
2331         // mark_list_head() used cmpxchg() above, switching list head can be lazier:
2332         OrderAccess::storestore();
2333       } else {
2334         // mid is marked. Switch cur_mid_in_use's next field to next
2335         // which is safe because we have no parallel list deletions,
2336         // but we leave mid marked:
2337         OrderAccess::release_store(&cur_mid_in_use->_next_om, next);
2338       }
2339       // At this point mid is disconnected from the in-use list so
2340       // its marked next field no longer has any effects.

2341       deflated_count++;
2342       Atomic::dec(count_p);
2343       // mid is current tail in the free_head_p list so NULL terminate it
2344       // (which also unmarks it):
2345       set_next(mid, NULL);
2346 
2347       // All the list management is done so move on to the next one:
2348       mid = next;
2349     } else {
2350       set_next(mid, next);  // unmark next field
2351 
2352       // All the list management is done so move on to the next one:
2353       cur_mid_in_use = mid;
2354       mid = next;
2355     }
2356     if (mid == NULL) {
2357       break;  // Reached end of the list so nothing more to deflate.
2358     }
2359     // Mark mid's next field so we can possibly deflate it:
2360     next = mark_next_loop(mid);
2361   }
2362   return deflated_count;
2363 }
2364 
2365 // Walk a given ObjectMonitor list and deflate idle ObjectMonitors using
2366 // a JavaThread. Returns the number of deflated ObjectMonitors. The given
2367 // list could be a per-thread in-use list or the global in-use list.
2368 // If a safepoint has started, then we save state via saved_mid_in_use_p
2369 // and return to the caller to honor the safepoint.
2370 //
2371 int ObjectSynchronizer::deflate_monitor_list_using_JT(ObjectMonitor* volatile * list_p,
2372                                                       int volatile * count_p,
2373                                                       ObjectMonitor** free_head_p,
2374                                                       ObjectMonitor** free_tail_p,
2375                                                       ObjectMonitor** saved_mid_in_use_p) {
2376   assert(AsyncDeflateIdleMonitors, "sanity check");
2377   assert(Thread::current()->is_Java_thread(), "precondition");
2378 
2379   ObjectMonitor* cur_mid_in_use = NULL;
2380   ObjectMonitor* mid = NULL;
2381   ObjectMonitor* next = NULL;
2382   ObjectMonitor* next_next = NULL;
2383   int deflated_count = 0;
2384 
2385   // We use the more complicated mark-cur_mid_in_use-and-mid-as-we-go
2386   // protocol because om_release() can do list deletions in parallel.
2387   // We also mark-next-next-as-we-go to prevent an om_flush() that is
2388   // behind this thread from passing us.
2389   if (*saved_mid_in_use_p == NULL) {
2390     // No saved state so start at the beginning.
2391     // Mark the list head's next field so we can possibly deflate it:
2392     if (!mark_list_head(list_p, &mid, &next)) {
2393       return 0;  // The list is empty so nothing to deflate.
2394     }
2395   } else {
2396     // We're restarting after a safepoint so restore the necessary state
2397     // before we resume.
2398     cur_mid_in_use = *saved_mid_in_use_p;
2399     // Mark cur_mid_in_use's next field so we can possibly update its
2400     // next field to extract a deflated ObjectMonitor.
2401     mid = mark_next_loop(cur_mid_in_use);
2402     if (mid == NULL) {
2403       set_next(cur_mid_in_use, NULL);  // unmark next field
2404       *saved_mid_in_use_p = NULL;
2405       return 0;  // The remainder is empty so nothing more to deflate.
2406     }
2407     // Mark mid's next field so we can possibly deflate it:
2408     next = mark_next_loop(mid);
2409   }
2410 
2411   while (true) {
2412     // The current mid's next field is marked at this point. If we have
2413     // a cur_mid_in_use, then its next field is also marked at this point.
2414 
2415     if (next != NULL) {
2416       // We mark next's next field so that an om_flush()
2417       // thread that is behind us cannot pass us when we
2418       // unmark the current mid's next field.
2419       next_next = mark_next_loop(next);
2420     }
2421 
2422     // Only try to deflate if there is an associated Java object and if
2423     // mid is old (is not newly allocated and is not newly freed).
2424     if (mid->object() != NULL && mid->is_old() &&
2425         deflate_monitor_using_JT(mid, free_head_p, free_tail_p)) {
2426       // Deflation succeeded and already updated free_head_p and
2427       // free_tail_p as needed. Finish the move to the local free list
2428       // by unlinking mid from the global or per-thread in-use list.
2429       if (cur_mid_in_use == NULL) {
2430         // mid is the list head and it is marked. Switch the list head
2431         // to next which is also marked (if not NULL) and also leave
2432         // mid marked:
2433         *list_p = next;
2434         // mark_list_head() used cmpxchg() above, switching list head can be lazier:
2435         OrderAccess::storestore();
2436       } else {
2437         ObjectMonitor* marked_next = mark_om_ptr(next);
2438         // mid and cur_mid_in_use are marked. Switch cur_mid_in_use's
2439         // next field to marked_next and also leave mid marked:
2440         OrderAccess::release_store(&cur_mid_in_use->_next_om, marked_next);
2441       }
2442       // At this point mid is disconnected from the in-use list so
2443       // its marked next field no longer has any effects.
2444       deflated_count++;
2445       Atomic::dec(count_p);
2446       // mid is current tail in the free_head_p list so NULL terminate it
2447       // (which also unmarks it):
2448       set_next(mid, NULL);
2449 
2450       // All the list management is done so move on to the next one:
2451       mid = next;  // mid keeps non-NULL next's marked next field
2452       next = next_next;
2453     } else {
2454       // mid is considered in-use if it does not have an associated
2455       // Java object or mid is not old or deflation did not succeed.
2456       // A mid->is_new() node can be seen here when it is freshly
2457       // returned by om_alloc() (and skips the deflation code path).
2458       // A mid->is_old() node can be seen here when deflation failed.
2459       // A mid->is_free() node can be seen here when a fresh node from
2460       // om_alloc() is released by om_release() due to losing the race
2461       // in inflate().
2462 
2463       // All the list management is done so move on to the next one:
2464       if (cur_mid_in_use != NULL) {
2465         set_next(cur_mid_in_use, mid);  // umark cur_mid_in_use
2466       }
2467       // The next cur_mid_in_use keeps mid's marked next field so
2468       // that it is stable for a possible next field change. It
2469       // cannot be modified by om_release() while it is marked.
2470       cur_mid_in_use = mid;
2471       mid = next;  // mid keeps non-NULL next's marked next field
2472       next = next_next;
2473 
2474       if (SafepointSynchronize::is_synchronizing() &&
2475           cur_mid_in_use != *list_p && cur_mid_in_use->is_old()) {
2476         // If a safepoint has started and cur_mid_in_use is not the list
2477         // head and is old, then it is safe to use as saved state. Return
2478         // to the caller before blocking.
2479         *saved_mid_in_use_p = cur_mid_in_use;
2480         set_next(cur_mid_in_use, mid);  // umark cur_mid_in_use
2481         if (mid != NULL) {
2482           set_next(mid, next);  // umark mid
2483         }
2484         return deflated_count;
2485       }
2486     }
2487     if (mid == NULL) {
2488       if (cur_mid_in_use != NULL) {
2489         set_next(cur_mid_in_use, mid);  // umark cur_mid_in_use
2490       }
2491       break;  // Reached end of the list so nothing more to deflate.
2492     }
2493 
2494     // The current mid's next field is marked at this point. If we have
2495     // a cur_mid_in_use, then its next field is also marked at this point.
2496   }
2497   // We finished the list without a safepoint starting so there's
2498   // no need to save state.
2499   *saved_mid_in_use_p = NULL;
2500   return deflated_count;
2501 }
2502 
2503 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2504   counters->n_in_use = 0;              // currently associated with objects
2505   counters->n_in_circulation = 0;      // extant
2506   counters->n_scavenged = 0;           // reclaimed (global and per-thread)
2507   counters->per_thread_scavenged = 0;  // per-thread scavenge total
2508   counters->per_thread_times = 0.0;    // per-thread scavenge times
2509   OrderAccess::storestore();           // flush inits for worker threads
2510 }
2511 
2512 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) {
2513   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2514 
2515   if (AsyncDeflateIdleMonitors) {
2516     // Nothing to do when global idle ObjectMonitors are deflated using
2517     // a JavaThread unless a special deflation has been requested.
2518     if (!is_special_deflation_requested()) {
2519       return;
2520     }
2521   }
2522 
2523   bool deflated = false;
2524 
2525   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
2526   ObjectMonitor* free_tail_p = NULL;
2527   elapsedTimer timer;
2528 
2529   if (log_is_enabled(Info, monitorinflation)) {
2530     timer.start();
2531   }
2532 





2533   // Note: the thread-local monitors lists get deflated in
2534   // a separate pass. See deflate_thread_local_monitors().
2535 
2536   // For moribund threads, scan g_om_in_use_list
2537   int deflated_count = 0;
2538   if (g_om_in_use_list != NULL) {
2539     // Update n_in_circulation before g_om_in_use_count is updated by deflation.
2540     Atomic::add(g_om_in_use_count, &counters->n_in_circulation);
2541 
2542     deflated_count = deflate_monitor_list(&g_om_in_use_list, &g_om_in_use_count, &free_head_p, &free_tail_p);
2543     Atomic::add(g_om_in_use_count, &counters->n_in_use);
2544   }
2545 
2546   if (free_head_p != NULL) {
2547     // Move the deflated ObjectMonitors back to the global free list.
2548     // No races on the working free list so no need for load_acquire().
2549     guarantee(free_tail_p != NULL && deflated_count > 0, "invariant");
2550     assert(free_tail_p->_next_om == NULL, "must be NULL: _next_om="
2551            INTPTR_FORMAT, p2i(free_tail_p->_next_om));
2552     prepend_list_to_g_free_list(free_head_p, free_tail_p, deflated_count);
2553     Atomic::add(deflated_count, &counters->n_scavenged);
2554   }

2555   timer.stop();
2556 
2557   LogStreamHandle(Debug, monitorinflation) lsh_debug;
2558   LogStreamHandle(Info, monitorinflation) lsh_info;
2559   LogStream* ls = NULL;
2560   if (log_is_enabled(Debug, monitorinflation)) {
2561     ls = &lsh_debug;
2562   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2563     ls = &lsh_info;
2564   }
2565   if (ls != NULL) {
2566     ls->print_cr("deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
2567   }
2568 }
2569 
2570 class HandshakeForDeflation : public ThreadClosure {
2571  public:
2572   void do_thread(Thread* thread) {
2573     log_trace(monitorinflation)("HandshakeForDeflation::do_thread: thread="
2574                                 INTPTR_FORMAT, p2i(thread));
2575   }
2576 };
2577 
2578 void ObjectSynchronizer::deflate_idle_monitors_using_JT() {
2579   assert(AsyncDeflateIdleMonitors, "sanity check");
2580 
2581   // Deflate any global idle monitors.
2582   deflate_global_idle_monitors_using_JT();
2583 
2584   int count = 0;
2585   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2586     if (jt->om_in_use_count > 0 && !jt->is_exiting()) {
2587       // This JavaThread is using ObjectMonitors so deflate any that
2588       // are idle unless this JavaThread is exiting; do not race with
2589       // ObjectSynchronizer::om_flush().
2590       deflate_per_thread_idle_monitors_using_JT(jt);
2591       count++;
2592     }
2593   }
2594   if (count > 0) {
2595     log_debug(monitorinflation)("did async deflation of idle monitors for %d thread(s).", count);
2596   }
2597 
2598   log_info(monitorinflation)("async g_om_population=%d, g_om_in_use_count=%d, "
2599                              "g_om_free_count=%d, g_om_wait_count=%d",
2600                              g_om_population, g_om_in_use_count,
2601                              g_om_free_count, g_om_wait_count);
2602 
2603   // The ServiceThread's async deflation request has been processed.
2604   set_is_async_deflation_requested(false);
2605 
2606   if (HandshakeAfterDeflateIdleMonitors && g_om_wait_count > 0) {
2607     // There are deflated ObjectMonitors waiting for a handshake
2608     // (or a safepoint) for safety.
2609 
2610     // g_wait_list and g_om_wait_count are only updated by the calling
2611     // thread so no need for load_acquire() or release_store().
2612     ObjectMonitor* list = g_wait_list;
2613     ADIM_guarantee(list != NULL, "g_wait_list must not be NULL");
2614     int count = g_om_wait_count;
2615     g_om_wait_count = 0;
2616     g_wait_list = NULL;
2617     OrderAccess::storestore();  // Lazier memory sync is okay for list walkers.
2618 
2619     // Find the tail for prepend_list_to_common(). No need to mark
2620     // ObjectMonitors for this list walk since only the deflater
2621     // thread manages the wait list.
2622     int l_count = 0;
2623     ObjectMonitor* tail = NULL;
2624     for (ObjectMonitor* n = list; n != NULL; n = unmarked_next(n)) {
2625       tail = n;
2626       l_count++;
2627     }
2628     ADIM_guarantee(count == l_count, "count=%d != l_count=%d", count, l_count);
2629 
2630     // Will execute a safepoint if !ThreadLocalHandshakes:
2631     HandshakeForDeflation hfd_tc;
2632     Handshake::execute(&hfd_tc);
2633 
2634     prepend_list_to_common(list, tail, count, &g_free_list, &g_om_free_count);
2635 
2636     log_info(monitorinflation)("moved %d idle monitors from global waiting list to global free list", count);
2637   }
2638 }
2639 
2640 // Deflate global idle ObjectMonitors using a JavaThread.
2641 //
2642 void ObjectSynchronizer::deflate_global_idle_monitors_using_JT() {
2643   assert(AsyncDeflateIdleMonitors, "sanity check");
2644   assert(Thread::current()->is_Java_thread(), "precondition");
2645   JavaThread* self = JavaThread::current();
2646 
2647   deflate_common_idle_monitors_using_JT(true /* is_global */, self);
2648 }
2649 
2650 // Deflate the specified JavaThread's idle ObjectMonitors using a JavaThread.
2651 //
2652 void ObjectSynchronizer::deflate_per_thread_idle_monitors_using_JT(JavaThread* target) {
2653   assert(AsyncDeflateIdleMonitors, "sanity check");
2654   assert(Thread::current()->is_Java_thread(), "precondition");
2655 
2656   deflate_common_idle_monitors_using_JT(false /* !is_global */, target);
2657 }
2658 
2659 // Deflate global or per-thread idle ObjectMonitors using a JavaThread.
2660 //
2661 void ObjectSynchronizer::deflate_common_idle_monitors_using_JT(bool is_global, JavaThread* target) {
2662   JavaThread* self = JavaThread::current();
2663 
2664   int deflated_count = 0;
2665   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged ObjectMonitors
2666   ObjectMonitor* free_tail_p = NULL;
2667   ObjectMonitor* saved_mid_in_use_p = NULL;
2668   elapsedTimer timer;
2669 
2670   if (log_is_enabled(Info, monitorinflation)) {
2671     timer.start();
2672   }
2673 
2674   if (is_global) {
2675     OM_PERFDATA_OP(MonExtant, set_value(g_om_in_use_count));
2676   } else {
2677     OM_PERFDATA_OP(MonExtant, inc(target->om_in_use_count));
2678   }
2679 
2680   do {
2681     int local_deflated_count;
2682     if (is_global) {
2683       local_deflated_count = deflate_monitor_list_using_JT(&g_om_in_use_list, &g_om_in_use_count, &free_head_p, &free_tail_p, &saved_mid_in_use_p);
2684     } else {
2685       local_deflated_count = deflate_monitor_list_using_JT(&target->om_in_use_list, &target->om_in_use_count, &free_head_p, &free_tail_p, &saved_mid_in_use_p);
2686     }
2687     deflated_count += local_deflated_count;
2688 
2689     if (free_head_p != NULL) {
2690       // Move the deflated ObjectMonitors to the global free list.
2691       // No races on the working list so no need for load_acquire().
2692       guarantee(free_tail_p != NULL && local_deflated_count > 0, "free_tail_p=" INTPTR_FORMAT ", local_deflated_count=%d", p2i(free_tail_p), local_deflated_count);
2693       // Note: The target thread can be doing an om_alloc() that
2694       // is trying to prepend an ObjectMonitor on its in-use list
2695       // at the same time that we have deflated the current in-use
2696       // list head and put it on the local free list. prepend_to_common()
2697       // will detect the race and retry which avoids list corruption,
2698       // but the next field in free_tail_p can flicker to marked
2699       // and then unmarked while prepend_to_common() is sorting it
2700       // all out.
2701       assert(unmarked_next(free_tail_p) == NULL, "must be NULL: _next_om="
2702              INTPTR_FORMAT, p2i(unmarked_next(free_tail_p)));
2703 
2704       if (HandshakeAfterDeflateIdleMonitors) {
2705         prepend_list_to_g_wait_list(free_head_p, free_tail_p, local_deflated_count);
2706       } else {
2707         prepend_list_to_g_free_list(free_head_p, free_tail_p, local_deflated_count);
2708       }
2709 
2710       OM_PERFDATA_OP(Deflations, inc(local_deflated_count));
2711     }
2712 
2713     if (saved_mid_in_use_p != NULL) {
2714       // deflate_monitor_list_using_JT() detected a safepoint starting.
2715       timer.stop();
2716       {
2717         if (is_global) {
2718           log_debug(monitorinflation)("pausing deflation of global idle monitors for a safepoint.");
2719         } else {
2720           log_debug(monitorinflation)("jt=" INTPTR_FORMAT ": pausing deflation of per-thread idle monitors for a safepoint.", p2i(target));
2721         }
2722         assert(SafepointSynchronize::is_synchronizing(), "sanity check");
2723         ThreadBlockInVM blocker(self);
2724       }
2725       // Prepare for another loop after the safepoint.
2726       free_head_p = NULL;
2727       free_tail_p = NULL;
2728       if (log_is_enabled(Info, monitorinflation)) {
2729         timer.start();
2730       }
2731     }
2732   } while (saved_mid_in_use_p != NULL);
2733   timer.stop();
2734 
2735   LogStreamHandle(Debug, monitorinflation) lsh_debug;
2736   LogStreamHandle(Info, monitorinflation) lsh_info;
2737   LogStream* ls = NULL;
2738   if (log_is_enabled(Debug, monitorinflation)) {
2739     ls = &lsh_debug;
2740   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2741     ls = &lsh_info;
2742   }
2743   if (ls != NULL) {
2744     if (is_global) {
2745       ls->print_cr("async-deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
2746     } else {
2747       ls->print_cr("jt=" INTPTR_FORMAT ": async-deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(target), timer.seconds(), deflated_count);
2748     }
2749   }
2750 }
2751 
2752 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2753   // Report the cumulative time for deflating each thread's idle
2754   // monitors. Note: if the work is split among more than one
2755   // worker thread, then the reported time will likely be more
2756   // than a beginning to end measurement of the phase.
2757   log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->per_thread_times, counters->per_thread_scavenged);
2758 
2759   bool needs_special_deflation = is_special_deflation_requested();
2760   if (AsyncDeflateIdleMonitors && !needs_special_deflation) {
2761     // Nothing to do when idle ObjectMonitors are deflated using
2762     // a JavaThread unless a special deflation has been requested.
2763     return;
2764   }
2765 
2766   if (log_is_enabled(Debug, monitorinflation)) {
2767     // exit_globals()'s call to audit_and_print_stats() is done
2768     // at the Info level and not at a safepoint.
2769     // For async deflation, audit_and_print_stats() is called in
2770     // ObjectSynchronizer::do_safepoint_work() at the Debug level
2771     // at a safepoint.
2772     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2773   } else if (log_is_enabled(Info, monitorinflation)) {

2774     log_info(monitorinflation)("g_om_population=%d, g_om_in_use_count=%d, "
2775                                "g_om_free_count=%d, g_om_wait_count=%d",
2776                                g_om_population, g_om_in_use_count,
2777                                g_om_free_count, g_om_wait_count);
2778   }
2779 
2780   ForceMonitorScavenge = 0;    // Reset
2781 
2782   OM_PERFDATA_OP(Deflations, inc(counters->n_scavenged));
2783   OM_PERFDATA_OP(MonExtant, set_value(counters->n_in_circulation));
2784 
2785   GVars.stw_random = os::random();
2786   GVars.stw_cycle++;
2787 
2788   if (needs_special_deflation) {
2789     set_is_special_deflation_requested(false);  // special deflation is done
2790   }
2791 }
2792 
2793 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
2794   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2795 
2796   if (AsyncDeflateIdleMonitors && !is_special_deflation_requested()) {
2797     // Nothing to do if a special deflation has NOT been requested.
2798     return;
2799   }
2800 
2801   ObjectMonitor* free_head_p = NULL;  // Local SLL of scavenged monitors
2802   ObjectMonitor* free_tail_p = NULL;
2803   elapsedTimer timer;
2804 
2805   if (log_is_enabled(Info, safepoint, cleanup) ||
2806       log_is_enabled(Info, monitorinflation)) {
2807     timer.start();
2808   }
2809 
2810   // Update n_in_circulation before om_in_use_count is updated by deflation.
2811   Atomic::add(thread->om_in_use_count, &counters->n_in_circulation);

2812 
2813   int deflated_count = deflate_monitor_list(&thread->om_in_use_list, &thread->om_in_use_count, &free_head_p, &free_tail_p);
2814   Atomic::add(thread->om_in_use_count, &counters->n_in_use);




2815 
2816   if (free_head_p != NULL) {
2817     // Move the deflated ObjectMonitors back to the global free list.
2818     // No races on the working list so no need for load_acquire().
2819     guarantee(free_tail_p != NULL && deflated_count > 0, "invariant");
2820     assert(free_tail_p->_next_om == NULL, "must be NULL: _next_om="
2821            INTPTR_FORMAT, p2i(free_tail_p->_next_om));
2822     prepend_list_to_g_free_list(free_head_p, free_tail_p, deflated_count);
2823     Atomic::add(deflated_count, &counters->n_scavenged);
2824     Atomic::add(deflated_count, &counters->per_thread_scavenged);
2825   }
2826 
2827   timer.stop();
2828   // Safepoint logging cares about cumulative per_thread_times and
2829   // we'll capture most of the cost, but not the muxRelease() which
2830   // should be cheap.
2831   counters->per_thread_times += timer.seconds();
2832 


2833   LogStreamHandle(Debug, monitorinflation) lsh_debug;
2834   LogStreamHandle(Info, monitorinflation) lsh_info;
2835   LogStream* ls = NULL;
2836   if (log_is_enabled(Debug, monitorinflation)) {
2837     ls = &lsh_debug;
2838   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2839     ls = &lsh_info;
2840   }
2841   if (ls != NULL) {
2842     ls->print_cr("jt=" INTPTR_FORMAT ": deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(thread), timer.seconds(), deflated_count);
2843   }
2844 }
2845 
2846 // Monitor cleanup on JavaThread::exit
2847 
2848 // Iterate through monitor cache and attempt to release thread's monitors
2849 // Gives up on a particular monitor if an exception occurs, but continues
2850 // the overall iteration, swallowing the exception.
2851 class ReleaseJavaMonitorsClosure: public MonitorClosure {
2852  private:


2863 
2864 // Release all inflated monitors owned by THREAD.  Lightweight monitors are
2865 // ignored.  This is meant to be called during JNI thread detach which assumes
2866 // all remaining monitors are heavyweight.  All exceptions are swallowed.
2867 // Scanning the extant monitor list can be time consuming.
2868 // A simple optimization is to add a per-thread flag that indicates a thread
2869 // called jni_monitorenter() during its lifetime.
2870 //
2871 // Instead of No_Savepoint_Verifier it might be cheaper to
2872 // use an idiom of the form:
2873 //   auto int tmp = SafepointSynchronize::_safepoint_counter ;
2874 //   <code that must not run at safepoint>
2875 //   guarantee (((tmp ^ _safepoint_counter) | (tmp & 1)) == 0) ;
2876 // Since the tests are extremely cheap we could leave them enabled
2877 // for normal product builds.
2878 
2879 void ObjectSynchronizer::release_monitors_owned_by_thread(TRAPS) {
2880   assert(THREAD == JavaThread::current(), "must be current Java thread");
2881   NoSafepointVerifier nsv;
2882   ReleaseJavaMonitorsClosure rjmc(THREAD);

2883   ObjectSynchronizer::monitors_iterate(&rjmc);

2884   THREAD->clear_pending_exception();
2885 }
2886 
2887 const char* ObjectSynchronizer::inflate_cause_name(const InflateCause cause) {
2888   switch (cause) {
2889     case inflate_cause_vm_internal:    return "VM Internal";
2890     case inflate_cause_monitor_enter:  return "Monitor Enter";
2891     case inflate_cause_wait:           return "Monitor Wait";
2892     case inflate_cause_notify:         return "Monitor Notify";
2893     case inflate_cause_hash_code:      return "Monitor Hash Code";
2894     case inflate_cause_jni_enter:      return "JNI Monitor Enter";
2895     case inflate_cause_jni_exit:       return "JNI Monitor Exit";
2896     default:
2897       ShouldNotReachHere();
2898   }
2899   return "Unknown";
2900 }
2901 
2902 //------------------------------------------------------------------------------
2903 // Debugging code


2917 u_char* ObjectSynchronizer::get_gvars_stw_random_addr() {
2918   return (u_char*)&GVars.stw_random;
2919 }
2920 
2921 void ObjectSynchronizer::audit_and_print_stats(bool on_exit) {
2922   assert(on_exit || SafepointSynchronize::is_at_safepoint(), "invariant");
2923 
2924   LogStreamHandle(Debug, monitorinflation) lsh_debug;
2925   LogStreamHandle(Info, monitorinflation) lsh_info;
2926   LogStreamHandle(Trace, monitorinflation) lsh_trace;
2927   LogStream* ls = NULL;
2928   if (log_is_enabled(Trace, monitorinflation)) {
2929     ls = &lsh_trace;
2930   } else if (log_is_enabled(Debug, monitorinflation)) {
2931     ls = &lsh_debug;
2932   } else if (log_is_enabled(Info, monitorinflation)) {
2933     ls = &lsh_info;
2934   }
2935   assert(ls != NULL, "sanity check");
2936 





2937   // Log counts for the global and per-thread monitor lists:
2938   int chk_om_population = log_monitor_list_counts(ls);
2939   int error_cnt = 0;
2940 
2941   ls->print_cr("Checking global lists:");
2942 
2943   // Check g_om_population:
2944   if (g_om_population == chk_om_population) {
2945     ls->print_cr("g_om_population=%d equals chk_om_population=%d",
2946                  g_om_population, chk_om_population);
2947   } else {
2948     // With lock free access to the monitor lists, it is possible for
2949     // log_monitor_list_counts() to return a value that doesn't match
2950     // g_om_population. So far a higher value has been seen in testing
2951     // so something is being double counted by log_monitor_list_counts().
2952     ls->print_cr("WARNING: g_om_population=%d is not equal to "
2953                  "chk_om_population=%d", g_om_population, chk_om_population);
2954   }
2955 
2956   // Check g_om_in_use_list and g_om_in_use_count:
2957   chk_global_in_use_list_and_count(ls, &error_cnt);
2958 
2959   // Check g_free_list and g_om_free_count:
2960   chk_global_free_list_and_count(ls, &error_cnt);
2961 
2962   if (HandshakeAfterDeflateIdleMonitors) {
2963     // Check g_wait_list and g_om_wait_count:
2964     chk_global_wait_list_and_count(ls, &error_cnt);
2965   }
2966 
2967   ls->print_cr("Checking per-thread lists:");
2968 
2969   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2970     // Check om_in_use_list and om_in_use_count:
2971     chk_per_thread_in_use_list_and_count(jt, ls, &error_cnt);
2972 
2973     // Check om_free_list and om_free_count:
2974     chk_per_thread_free_list_and_count(jt, ls, &error_cnt);
2975   }
2976 
2977   if (error_cnt == 0) {
2978     ls->print_cr("No errors found in monitor list checks.");
2979   } else {
2980     log_error(monitorinflation)("found monitor list errors: error_cnt=%d", error_cnt);
2981   }
2982 
2983   if ((on_exit && log_is_enabled(Info, monitorinflation)) ||
2984       (!on_exit && log_is_enabled(Trace, monitorinflation))) {
2985     // When exiting this log output is at the Info level. When called
2986     // at a safepoint, this log output is at the Trace level since
2987     // there can be a lot of it.
2988     log_in_use_monitor_details(ls);
2989   }
2990 
2991   ls->flush();
2992 
2993   guarantee(error_cnt == 0, "ERROR: found monitor list errors: error_cnt=%d", error_cnt);
2994 }
2995 
2996 // Check a free monitor entry; log any errors.
2997 void ObjectSynchronizer::chk_free_entry(JavaThread* jt, ObjectMonitor* n,
2998                                         outputStream * out, int *error_cnt_p) {
2999   stringStream ss;
3000   if (n->is_busy()) {
3001     if (jt != NULL) {
3002       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
3003                     ": free per-thread monitor must not be busy: %s", p2i(jt),
3004                     p2i(n), n->is_busy_to_string(&ss));
3005     } else {
3006       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
3007                     "must not be busy: %s", p2i(n), n->is_busy_to_string(&ss));
3008     }
3009     *error_cnt_p = *error_cnt_p + 1;
3010   }
3011   if (n->header().value() != 0) {
3012     if (jt != NULL) {
3013       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
3014                     ": free per-thread monitor must have NULL _header "
3015                     "field: _header=" INTPTR_FORMAT, p2i(jt), p2i(n),
3016                     n->header().value());
3017       *error_cnt_p = *error_cnt_p + 1;
3018     } else if (!AsyncDeflateIdleMonitors) {
3019       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
3020                     "must have NULL _header field: _header=" INTPTR_FORMAT,
3021                     p2i(n), n->header().value());

3022       *error_cnt_p = *error_cnt_p + 1;
3023     }
3024   }
3025   if (n->object() != NULL) {
3026     if (jt != NULL) {
3027       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
3028                     ": free per-thread monitor must have NULL _object "
3029                     "field: _object=" INTPTR_FORMAT, p2i(jt), p2i(n),
3030                     p2i(n->object()));
3031     } else {
3032       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
3033                     "must have NULL _object field: _object=" INTPTR_FORMAT,
3034                     p2i(n), p2i(n->object()));
3035     }
3036     *error_cnt_p = *error_cnt_p + 1;
3037   }
3038 }
3039 
3040 // Check the global free list and count; log the results of the checks.
3041 void ObjectSynchronizer::chk_global_free_list_and_count(outputStream * out,
3042                                                         int *error_cnt_p) {
3043   int chk_om_free_count = 0;
3044   ObjectMonitor* cur = NULL;
3045   ObjectMonitor* next = NULL;
3046   if (mark_list_head(&g_free_list, &cur, &next)) {
3047     // Marked the global free list head so process the list.
3048     while (true) {
3049       chk_free_entry(NULL /* jt */, cur, out, error_cnt_p);
3050       chk_om_free_count++;
3051 
3052       mark_next_for_traversal(&cur, &next);
3053       if (cur == NULL) {
3054         break;
3055       }
3056     }
3057   }
3058   if (g_om_free_count == chk_om_free_count) {
3059     out->print_cr("g_om_free_count=%d equals chk_om_free_count=%d",
3060                   g_om_free_count, chk_om_free_count);
3061   } else {
3062     // With lock free access to g_free_list, it is possible for an
3063     // ObjectMonitor to be prepended to g_free_list after we started
3064     // calculating chk_om_free_count so g_om_free_count may not
3065     // match anymore.
3066     out->print_cr("WARNING: g_om_free_count=%d is not equal to "
3067                   "chk_om_free_count=%d", g_om_free_count, chk_om_free_count);
3068   }
3069 }
3070 
3071 // Check the global wait list and count; log the results of the checks.
3072 void ObjectSynchronizer::chk_global_wait_list_and_count(outputStream * out,
3073                                                         int *error_cnt_p) {
3074   int chk_om_wait_count = 0;
3075   ObjectMonitor* cur = NULL;
3076   ObjectMonitor* next = NULL;
3077   if (mark_list_head(&g_wait_list, &cur, &next)) {
3078     // Marked the global wait list head so process the list.
3079     while (true) {
3080       // Rules for g_wait_list are the same as of g_free_list:
3081       chk_free_entry(NULL /* jt */, cur, out, error_cnt_p);
3082       chk_om_wait_count++;
3083 
3084       mark_next_for_traversal(&cur, &next);
3085       if (cur == NULL) {
3086         break;
3087       }
3088     }
3089   }
3090   if (g_om_wait_count == chk_om_wait_count) {
3091     out->print_cr("g_om_wait_count=%d equals chk_om_wait_count=%d",
3092                   g_om_wait_count, chk_om_wait_count);
3093   } else {
3094     out->print_cr("ERROR: g_om_wait_count=%d is not equal to "
3095                   "chk_om_wait_count=%d", g_om_wait_count, chk_om_wait_count);
3096     *error_cnt_p = *error_cnt_p + 1;
3097   }
3098 }
3099 
3100 // Check the global in-use list and count; log the results of the checks.
3101 void ObjectSynchronizer::chk_global_in_use_list_and_count(outputStream * out,
3102                                                           int *error_cnt_p) {
3103   int chk_om_in_use_count = 0;
3104   ObjectMonitor* cur = NULL;
3105   ObjectMonitor* next = NULL;
3106   if (mark_list_head(&g_om_in_use_list, &cur, &next)) {
3107     // Marked the global in-use list head so process the list.
3108     while (true) {
3109       chk_in_use_entry(NULL /* jt */, cur, out, error_cnt_p);
3110       chk_om_in_use_count++;
3111 
3112       mark_next_for_traversal(&cur, &next);
3113       if (cur == NULL) {
3114         break;
3115       }
3116     }
3117   }
3118   if (g_om_in_use_count == chk_om_in_use_count) {
3119     out->print_cr("g_om_in_use_count=%d equals chk_om_in_use_count=%d",
3120                   g_om_in_use_count, chk_om_in_use_count);
3121   } else {
3122     // With lock free access to the monitor lists, it is possible for
3123     // an exiting JavaThread to put its in-use ObjectMonitors on the
3124     // global in-use list after chk_om_in_use_count is calculated above.
3125     out->print_cr("WARNING: g_om_in_use_count=%d is not equal to chk_om_in_use_count=%d",
3126                   g_om_in_use_count, chk_om_in_use_count);

3127   }
3128 }
3129 
3130 // Check an in-use monitor entry; log any errors.
3131 void ObjectSynchronizer::chk_in_use_entry(JavaThread* jt, ObjectMonitor* n,
3132                                           outputStream * out, int *error_cnt_p) {
3133   if (n->header().value() == 0) {
3134     if (jt != NULL) {
3135       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
3136                     ": in-use per-thread monitor must have non-NULL _header "
3137                     "field.", p2i(jt), p2i(n));
3138     } else {
3139       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global monitor "
3140                     "must have non-NULL _header field.", p2i(n));
3141     }
3142     *error_cnt_p = *error_cnt_p + 1;
3143   }
3144   if (n->object() == NULL) {
3145     if (jt != NULL) {
3146       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT


3174       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
3175                     ": in-use per-thread monitor's object does not refer "
3176                     "to the same monitor: obj=" INTPTR_FORMAT ", mark="
3177                     INTPTR_FORMAT ", obj_mon=" INTPTR_FORMAT, p2i(jt),
3178                     p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
3179     } else {
3180       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": in-use global "
3181                     "monitor's object does not refer to the same monitor: obj="
3182                     INTPTR_FORMAT ", mark=" INTPTR_FORMAT ", obj_mon="
3183                     INTPTR_FORMAT, p2i(n), p2i(obj), mark.value(), p2i(obj_mon));
3184     }
3185     *error_cnt_p = *error_cnt_p + 1;
3186   }
3187 }
3188 
3189 // Check the thread's free list and count; log the results of the checks.
3190 void ObjectSynchronizer::chk_per_thread_free_list_and_count(JavaThread *jt,
3191                                                             outputStream * out,
3192                                                             int *error_cnt_p) {
3193   int chk_om_free_count = 0;
3194   ObjectMonitor* cur = NULL;
3195   ObjectMonitor* next = NULL;
3196   if (mark_list_head(&jt->om_free_list, &cur, &next)) {
3197     // Marked the per-thread free list head so process the list.
3198     while (true) {
3199       chk_free_entry(jt, cur, out, error_cnt_p);
3200       chk_om_free_count++;
3201 
3202       mark_next_for_traversal(&cur, &next);
3203       if (cur == NULL) {
3204         break;
3205       }
3206     }
3207   }
3208   if (jt->om_free_count == chk_om_free_count) {
3209     out->print_cr("jt=" INTPTR_FORMAT ": om_free_count=%d equals "
3210                   "chk_om_free_count=%d", p2i(jt), jt->om_free_count,
3211                   chk_om_free_count);
3212   } else {
3213     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_free_count=%d is not "
3214                   "equal to chk_om_free_count=%d", p2i(jt), jt->om_free_count,
3215                   chk_om_free_count);
3216     *error_cnt_p = *error_cnt_p + 1;
3217   }
3218 }
3219 
3220 // Check the thread's in-use list and count; log the results of the checks.
3221 void ObjectSynchronizer::chk_per_thread_in_use_list_and_count(JavaThread *jt,
3222                                                               outputStream * out,
3223                                                               int *error_cnt_p) {
3224   int chk_om_in_use_count = 0;
3225   ObjectMonitor* cur = NULL;
3226   ObjectMonitor* next = NULL;
3227   if (mark_list_head(&jt->om_in_use_list, &cur, &next)) {
3228     // Marked the per-thread in-use list head so process the list.
3229     while (true) {
3230       chk_in_use_entry(jt, cur, out, error_cnt_p);
3231       chk_om_in_use_count++;
3232 
3233       mark_next_for_traversal(&cur, &next);
3234       if (cur == NULL) {
3235         break;
3236       }
3237     }
3238   }
3239   if (jt->om_in_use_count == chk_om_in_use_count) {
3240     out->print_cr("jt=" INTPTR_FORMAT ": om_in_use_count=%d equals "
3241                   "chk_om_in_use_count=%d", p2i(jt),
3242                   jt->om_in_use_count, chk_om_in_use_count);
3243   } else {
3244     out->print_cr("ERROR: jt=" INTPTR_FORMAT ": om_in_use_count=%d is not "
3245                   "equal to chk_om_in_use_count=%d", p2i(jt),
3246                   jt->om_in_use_count, chk_om_in_use_count);
3247     *error_cnt_p = *error_cnt_p + 1;
3248   }
3249 }
3250 
3251 // Log details about ObjectMonitors on the in-use lists. The 'BHL'
3252 // flags indicate why the entry is in-use, 'object' and 'object type'
3253 // indicate the associated object and its type.
3254 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out) {






3255   stringStream ss;
3256   if (g_om_in_use_count > 0) {
3257     out->print_cr("In-use global monitor info:");
3258     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
3259     out->print_cr("%18s  %s  %7s  %18s  %18s",
3260                   "monitor", "BHL", "ref_cnt", "object", "object type");
3261     out->print_cr("==================  ===  =======  ==================  ==================");
3262     ObjectMonitor* cur = NULL;
3263     ObjectMonitor* next = NULL;
3264     if (mark_list_head(&g_om_in_use_list, &cur, &next)) {
3265       // Marked the global in-use list head so process the list.
3266       while (true) {
3267         const oop obj = (oop) cur->object();
3268         const markWord mark = cur->header();
3269         ResourceMark rm;
3270         out->print(INTPTR_FORMAT "  %d%d%d  %7d  " INTPTR_FORMAT "  %s",
3271                    p2i(cur), cur->is_busy() != 0, mark.hash() != 0,
3272                    cur->owner() != NULL, (int)cur->ref_count(), p2i(obj),
3273                    obj->klass()->external_name());
3274         if (cur->is_busy() != 0) {
3275           out->print(" (%s)", cur->is_busy_to_string(&ss));
3276           ss.reset();
3277         }
3278         out->cr();
3279 
3280         mark_next_for_traversal(&cur, &next);
3281         if (cur == NULL) {
3282           break;
3283         }
3284       }
3285     }



3286   }
3287 
3288   out->print_cr("In-use per-thread monitor info:");
3289   out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
3290   out->print_cr("%18s  %18s  %s  %7s  %18s  %18s",
3291                 "jt", "monitor", "BHL", "ref_cnt", "object", "object type");
3292   out->print_cr("==================  ==================  ===  =======  ==================  ==================");
3293   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
3294     ObjectMonitor* cur = NULL;
3295     ObjectMonitor* next = NULL;
3296     if (mark_list_head(&jt->om_in_use_list, &cur, &next)) {
3297       // Marked the global in-use list head so process the list.
3298       while (true) {
3299         const oop obj = (oop) cur->object();
3300         const markWord mark = cur->header();
3301         ResourceMark rm;
3302         out->print(INTPTR_FORMAT "  " INTPTR_FORMAT "  %d%d%d  %7d  "
3303                    INTPTR_FORMAT "  %s", p2i(jt), p2i(cur), cur->is_busy() != 0,
3304                    mark.hash() != 0, cur->owner() != NULL, (int)cur->ref_count(),
3305                    p2i(obj), obj->klass()->external_name());
3306         if (cur->is_busy() != 0) {
3307           out->print(" (%s)", cur->is_busy_to_string(&ss));
3308           ss.reset();
3309         }
3310         out->cr();
3311 
3312         mark_next_for_traversal(&cur, &next);
3313         if (cur == NULL) {
3314           break;
3315         }
3316       }
3317     }
3318   }
3319 
3320   out->flush();
3321 }
3322 
3323 // Log counts for the global and per-thread monitor lists and return
3324 // the population count.
3325 int ObjectSynchronizer::log_monitor_list_counts(outputStream * out) {
3326   int pop_count = 0;
3327   out->print_cr("%18s  %10s  %10s  %10s  %10s",
3328                 "Global Lists:", "InUse", "Free", "Wait", "Total");
3329   out->print_cr("==================  ==========  ==========  ==========  ==========");
3330   out->print_cr("%18s  %10d  %10d  %10d  %10d", "", g_om_in_use_count,
3331                 g_om_free_count, g_om_wait_count, g_om_population);
3332   pop_count += g_om_in_use_count + g_om_free_count;
3333   if (HandshakeAfterDeflateIdleMonitors) {
3334     pop_count += g_om_wait_count;
3335   }
3336 
3337   out->print_cr("%18s  %10s  %10s  %10s",
3338                 "Per-Thread Lists:", "InUse", "Free", "Provision");
3339   out->print_cr("==================  ==========  ==========  ==========");
3340 
3341   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
3342     out->print_cr(INTPTR_FORMAT "  %10d  %10d  %10d", p2i(jt),
3343                   jt->om_in_use_count, jt->om_free_count, jt->om_free_provision);
3344     pop_count += jt->om_in_use_count + jt->om_free_count;
3345   }
3346   return pop_count;
3347 }
3348 
3349 #ifndef PRODUCT
3350 
3351 // Check if monitor belongs to the monitor cache
3352 // The list is grow-only so it's *relatively* safe to traverse
3353 // the list of extant blocks without taking a lock.
3354 
3355 int ObjectSynchronizer::verify_objmon_isinpool(ObjectMonitor *monitor) {
3356   PaddedObjectMonitor* block = g_block_list;
3357   while (block != NULL) {
3358     assert(block->object() == CHAINMARKER, "must be a block header");
3359     if (monitor > &block[0] && monitor < &block[_BLOCKSIZE]) {
3360       address mon = (address)monitor;
3361       address blk = (address)block;
3362       size_t diff = mon - blk;
3363       assert((diff % sizeof(PaddedObjectMonitor)) == 0, "must be aligned");
3364       return 1;
3365     }
3366     // unmarked_next() is not needed with g_block_list (no next field
3367     // marking) and no load_acquire() needed because _next_om is
3368     // updated before g_block_list is changed with cmpxchg().
3369     block = (PaddedObjectMonitor*)block->_next_om;
3370   }
3371   return 0;
3372 }
3373 
3374 #endif
< prev index next >