< prev index next >

src/hotspot/share/runtime/synchronizer.cpp

Print this page
rev 55489 : Checkpoint latest preliminary review patches for full OpenJDK review; merge with 8222295.patch.
rev 55490 : imported patch dcubed.monitor_deflate_conc.v2.01
rev 55491 : imported patch dcubed.monitor_deflate_conc.v2.02
rev 55492 : imported patch dcubed.monitor_deflate_conc.v2.03
rev 55493 : imported patch dcubed.monitor_deflate_conc.v2.04
rev 55494 : imported patch dcubed.monitor_deflate_conc.v2.05


 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 PaddedEnd<ObjectMonitor> * volatile ObjectSynchronizer::gBlockList = NULL;
 121 // global monitor free list
 122 ObjectMonitor * volatile ObjectSynchronizer::gFreeList  = NULL;
 123 // global monitor in-use list, for moribund threads,
 124 // monitors they inflated need to be scanned for deflation
 125 ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList  = NULL;
 126 // count of entries in gOmInUseList
 127 int ObjectSynchronizer::gOmInUseCount = 0;



 128 
 129 static volatile intptr_t gListLock = 0;      // protects global monitor lists
 130 static volatile int gMonitorFreeCount  = 0;  // # on gFreeList
 131 static volatile int gMonitorPopulation = 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


 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 markOop mark = obj->mark();
 215 
 216   if (mark->has_monitor()) {
 217     ObjectMonitor * const m = mark->monitor();






 218     assert(oopDesc::equals((oop) 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(markOopDesc::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 //  Fast Monitor Enter/Exit
 261 // This the fast monitor enter. The interpreter and compiler use
 262 // some assembly copies of this code. Make sure update those code
 263 // if the following function is changed. The implementation is
 264 // extremely sensitive to race condition. Be careful.
 265 
 266 void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock,


 310         // does not own the Java Monitor.
 311         ObjectMonitor * m = mark->monitor();
 312         assert(((oop)(m->object()))->mark() == mark, "invariant");
 313         assert(m->is_entered(THREAD), "invariant");
 314       }
 315     }
 316 #endif
 317     return;
 318   }
 319 
 320   if (mark == (markOop) lock) {
 321     // If the object is stack-locked by the current thread, try to
 322     // swing the displaced header from the BasicLock back to the mark.
 323     assert(dhw->is_neutral(), "invariant");
 324     if (object->cas_set_mark(dhw, mark) == mark) {
 325       return;
 326     }
 327   }
 328 
 329   // We have to take the slow-path of possible inflation and then exit.
 330   inflate(THREAD, object, inflate_cause_vm_internal)->exit(true, THREAD);


 331 }
 332 
 333 // -----------------------------------------------------------------------------
 334 // Interpreter/Compiler Slow Case
 335 // This routine is used to handle interpreter/compiler slow case
 336 // We don't need to use fast path here, because it must have been
 337 // failed in the interpreter/compiler code.
 338 void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
 339   markOop mark = obj->mark();
 340   assert(!mark->has_bias_pattern(), "should not see bias pattern here");
 341 
 342   if (mark->is_neutral()) {
 343     // Anticipate successful CAS -- the ST of the displaced mark must
 344     // be visible <= the ST performed by the CAS.
 345     lock->set_displaced_header(mark);
 346     if (mark == obj()->cas_set_mark((markOop) lock, mark)) {
 347       return;
 348     }
 349     // Fall through to inflate() ...
 350   } else if (mark->has_locker() &&
 351              THREAD->is_lock_owned((address)mark->locker())) {
 352     assert(lock != mark->locker(), "must not re-lock the same lock");
 353     assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
 354     lock->set_displaced_header(NULL);
 355     return;
 356   }
 357 
 358   // The object header will never be displaced to this lock,
 359   // so it does not matter what the value is, except that it
 360   // must be non-zero to avoid looking like a re-entrant lock,
 361   // and must not look locked either.
 362   lock->set_displaced_header(markOopDesc::unused_mark());
 363   inflate(THREAD, obj(), inflate_cause_monitor_enter)->enter(THREAD);


 364 }
 365 
 366 // This routine is used to handle interpreter/compiler slow case
 367 // We don't need to use fast path here, because it must have
 368 // failed in the interpreter/compiler code. Simply use the heavy
 369 // weight monitor should be ok, unless someone find otherwise.
 370 void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) {
 371   fast_exit(object, lock, THREAD);
 372 }
 373 
 374 // -----------------------------------------------------------------------------
 375 // Class Loader  support to workaround deadlocks on the class loader lock objects
 376 // Also used by GC
 377 // complete_exit()/reenter() are used to wait on a nested lock
 378 // i.e. to give up an outer lock completely and then re-enter
 379 // Used when holding nested locks - lock acquisition order: lock1 then lock2
 380 //  1) complete_exit lock1 - saving recursion count
 381 //  2) wait on lock2
 382 //  3) when notified on lock2, unlock lock2
 383 //  4) reenter lock1 with original recursion count
 384 //  5) lock lock2
 385 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 386 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
 387   if (UseBiasedLocking) {
 388     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 389     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 390   }
 391 
 392   ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
 393 
 394   return monitor->complete_exit(THREAD);

 395 }
 396 
 397 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 398 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
 399   if (UseBiasedLocking) {
 400     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 401     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 402   }
 403 
 404   ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal);
 405 
 406   monitor->reenter(recursion, THREAD);
 407 }
 408 // -----------------------------------------------------------------------------
 409 // JNI locks on java objects
 410 // NOTE: must use heavy weight monitor to handle jni monitor enter
 411 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) {
 412   // the current locking is from JNI instead of Java code
 413   if (UseBiasedLocking) {
 414     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 415     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 416   }
 417   THREAD->set_current_pending_monitor_is_from_java(false);
 418   inflate(THREAD, obj(), inflate_cause_jni_enter)->enter(THREAD);


 419   THREAD->set_current_pending_monitor_is_from_java(true);
 420 }
 421 
 422 // NOTE: must use heavy weight monitor to handle jni monitor exit
 423 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
 424   if (UseBiasedLocking) {
 425     Handle h_obj(THREAD, obj);
 426     BiasedLocking::revoke_and_rebias(h_obj, false, THREAD);
 427     obj = h_obj();
 428   }
 429   assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 430 
 431   ObjectMonitor* monitor = inflate(THREAD, obj, inflate_cause_jni_exit);


 432   // If this thread has locked the object, exit the monitor.  Note:  can't use
 433   // monitor->check(CHECK); must exit even if an exception is pending.
 434   if (monitor->check(THREAD)) {
 435     monitor->exit(true, THREAD);
 436   }
 437 }
 438 
 439 // -----------------------------------------------------------------------------
 440 // Internal VM locks on java objects
 441 // standard constructor, allows locking failures
 442 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) {
 443   _dolock = doLock;
 444   _thread = thread;
 445   debug_only(if (StrictSafepointChecks) _thread->check_for_valid_safepoint_state(false);)
 446   _obj = obj;
 447 
 448   if (_dolock) {
 449     ObjectSynchronizer::fast_enter(_obj, &_lock, false, _thread);
 450   }
 451 }
 452 
 453 ObjectLocker::~ObjectLocker() {
 454   if (_dolock) {
 455     ObjectSynchronizer::fast_exit(_obj(), &_lock, _thread);
 456   }
 457 }
 458 
 459 
 460 // -----------------------------------------------------------------------------
 461 //  Wait/Notify/NotifyAll
 462 // NOTE: must use heavy weight monitor to handle wait()
 463 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
 464   if (UseBiasedLocking) {
 465     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 466     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 467   }
 468   if (millis < 0) {
 469     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 470   }
 471   ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_wait);


 472 
 473   DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
 474   monitor->wait(millis, true, THREAD);
 475 
 476   // This dummy call is in place to get around dtrace bug 6254741.  Once
 477   // that's fixed we can uncomment the following line, remove the call
 478   // and change this function back into a "void" func.
 479   // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
 480   return dtrace_waited_probe(monitor, obj, THREAD);

 481 }
 482 
 483 void ObjectSynchronizer::waitUninterruptibly(Handle obj, jlong millis, TRAPS) {
 484   if (UseBiasedLocking) {
 485     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 486     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 487   }
 488   if (millis < 0) {
 489     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 490   }
 491   inflate(THREAD, obj(), inflate_cause_wait)->wait(millis, false, THREAD);


 492 }
 493 
 494 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
 495   if (UseBiasedLocking) {
 496     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 497     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 498   }
 499 
 500   markOop mark = obj->mark();
 501   if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
 502     return;
 503   }
 504   inflate(THREAD, obj(), inflate_cause_notify)->notify(THREAD);


 505 }
 506 
 507 // NOTE: see comment of notify()
 508 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
 509   if (UseBiasedLocking) {
 510     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 511     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 512   }
 513 
 514   markOop mark = obj->mark();
 515   if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
 516     return;
 517   }
 518   inflate(THREAD, obj(), inflate_cause_notify)->notifyAll(THREAD);


 519 }
 520 
 521 // -----------------------------------------------------------------------------
 522 // Hash Code handling
 523 //
 524 // Performance concern:
 525 // OrderAccess::storestore() calls release() which at one time stored 0
 526 // into the global volatile OrderAccess::dummy variable. This store was
 527 // unnecessary for correctness. Many threads storing into a common location
 528 // causes considerable cache migration or "sloshing" on large SMP systems.
 529 // As such, I avoided using OrderAccess::storestore(). In some cases
 530 // OrderAccess::fence() -- which incurs local latency on the executing
 531 // processor -- is a better choice as it scales on SMP systems.
 532 //
 533 // See http://blogs.oracle.com/dave/entry/biased_locking_in_hotspot for
 534 // a discussion of coherency costs. Note that all our current reference
 535 // platforms provide strong ST-ST order, so the issue is moot on IA32,
 536 // x64, and SPARC.
 537 //
 538 // As a general policy we use "volatile" to control compiler-based reordering


 692       Handle hobj(Self, obj);
 693       // Relaxing assertion for bug 6320749.
 694       assert(Universe::verify_in_progress() ||
 695              !SafepointSynchronize::is_at_safepoint(),
 696              "biases should not be seen by VM thread here");
 697       BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
 698       obj = hobj();
 699       assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 700     }
 701   }
 702 
 703   // hashCode() is a heap mutator ...
 704   // Relaxing assertion for bug 6320749.
 705   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 706          !SafepointSynchronize::is_at_safepoint(), "invariant");
 707   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 708          Self->is_Java_thread() , "invariant");
 709   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 710          ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
 711 

 712   ObjectMonitor* monitor = NULL;
 713   markOop temp, test;
 714   intptr_t hash;
 715   markOop mark = ReadStableMark(obj);
 716 
 717   // object should remain ineligible for biased locking
 718   assert(!mark->has_bias_pattern(), "invariant");
 719 
 720   if (mark->is_neutral()) {
 721     hash = mark->hash();              // this is a normal header
 722     if (hash != 0) {                  // if it has hash, just return it
 723       return hash;
 724     }
 725     hash = get_next_hash(Self, obj);  // allocate a new hash code
 726     temp = mark->copy_set_hash(hash); // merge the hash code into header
 727     // use (machine word version) atomic operation to install the hash
 728     test = obj->cas_set_mark(temp, mark);
 729     if (test == mark) {
 730       return hash;
 731     }
 732     // If atomic operation failed, we must inflate the header
 733     // into heavy weight monitor. We could add more code here
 734     // for fast path, but it does not worth the complexity.
 735   } else if (mark->has_monitor()) {
 736     monitor = mark->monitor();






 737     temp = monitor->header();
 738     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 739     hash = temp->hash();
 740     if (hash != 0) {
 741       return hash;
 742     }
 743     // Skip to the following code to reduce code size
 744   } else if (Self->is_lock_owned((address)mark->locker())) {
 745     temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
 746     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 747     hash = temp->hash();              // by current thread, check if the displaced
 748     if (hash != 0) {                  // header contains hash code
 749       return hash;
 750     }
 751     // WARNING:
 752     // The displaced header in the BasicLock on a thread's stack
 753     // is strictly immutable. It CANNOT be changed in ANY cases.
 754     // So we have to inflate the stack lock into an ObjectMonitor
 755     // even if the current thread owns the lock. The BasicLock on
 756     // a thread's stack can be asynchronously read by other threads
 757     // during an inflate() call so any change to that stack memory
 758     // may not propagate to other threads correctly.
 759   }
 760 
 761   // Inflate the monitor to set hash code
 762   monitor = inflate(Self, obj, inflate_cause_hash_code);


 763   // Load displaced header and check it has hash code
 764   mark = monitor->header();
 765   assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark));
 766   hash = mark->hash();
 767   if (hash == 0) {
 768     hash = get_next_hash(Self, obj);
 769     temp = mark->copy_set_hash(hash); // merge hash code into header
 770     assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 771     test = Atomic::cmpxchg(temp, monitor->header_addr(), mark);
 772     if (test != mark) {
 773       // The only update to the ObjectMonitor's header/dmw field
 774       // is to merge in the hash code. If someone adds a new usage
 775       // of the header/dmw field, please update this code.





 776       hash = test->hash();
 777       assert(test->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(test));
 778       assert(hash != 0, "Trivial unexpected object/monitor header usage.");
 779     }
 780   }
 781   // We finally get the hash
 782   return hash;

 783 }
 784 
 785 // Deprecated -- use FastHashCode() instead.
 786 
 787 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
 788   return FastHashCode(Thread::current(), obj());
 789 }
 790 
 791 
 792 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
 793                                                    Handle h_obj) {
 794   if (UseBiasedLocking) {
 795     BiasedLocking::revoke_and_rebias(h_obj, false, thread);
 796     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 797   }
 798 
 799   assert(thread == JavaThread::current(), "Can only be called on current thread");
 800   oop obj = h_obj();
 801 

 802   markOop mark = ReadStableMark(obj);
 803 
 804   // Uncontended case, header points to stack
 805   if (mark->has_locker()) {
 806     return thread->is_lock_owned((address)mark->locker());
 807   }
 808   // Contended case, header points to ObjectMonitor (tagged pointer)
 809   if (mark->has_monitor()) {
 810     ObjectMonitor* monitor = mark->monitor();
 811     return monitor->is_entered(thread) != 0;






 812   }
 813   // Unlocked case, header in place
 814   assert(mark->is_neutral(), "sanity check");
 815   return false;

 816 }
 817 
 818 // Be aware of this method could revoke bias of the lock object.
 819 // This method queries the ownership of the lock handle specified by 'h_obj'.
 820 // If the current thread owns the lock, it returns owner_self. If no
 821 // thread owns the lock, it returns owner_none. Otherwise, it will return
 822 // owner_other.
 823 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
 824 (JavaThread *self, Handle h_obj) {
 825   // The caller must beware this method can revoke bias, and
 826   // revocation can result in a safepoint.
 827   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 828   assert(self->thread_state() != _thread_blocked, "invariant");
 829 
 830   // Possible mark states: neutral, biased, stack-locked, inflated
 831 
 832   if (UseBiasedLocking && h_obj()->mark()->has_bias_pattern()) {
 833     // CASE: biased
 834     BiasedLocking::revoke_and_rebias(h_obj, false, self);
 835     assert(!h_obj->mark()->has_bias_pattern(),
 836            "biases should be revoked by now");
 837   }
 838 
 839   assert(self == JavaThread::current(), "Can only be called on current thread");
 840   oop obj = h_obj();


 841   markOop mark = ReadStableMark(obj);
 842 
 843   // CASE: stack-locked.  Mark points to a BasicLock on the owner's stack.
 844   if (mark->has_locker()) {
 845     return self->is_lock_owned((address)mark->locker()) ?
 846       owner_self : owner_other;
 847   }
 848 
 849   // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor.
 850   // The Object:ObjectMonitor relationship is stable as long as we're
 851   // not at a safepoint.
 852   if (mark->has_monitor()) {
 853     void * owner = mark->monitor()->_owner;







 854     if (owner == NULL) return owner_none;
 855     return (owner == self ||
 856             self->is_lock_owned((address)owner)) ? owner_self : owner_other;
 857   }
 858 
 859   // CASE: neutral
 860   assert(mark->is_neutral(), "sanity check");
 861   return owner_none;           // it's unlocked

 862 }
 863 
 864 // FIXME: jvmti should call this
 865 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
 866   if (UseBiasedLocking) {
 867     if (SafepointSynchronize::is_at_safepoint()) {
 868       BiasedLocking::revoke_at_safepoint(h_obj);
 869     } else {
 870       BiasedLocking::revoke_and_rebias(h_obj, false, JavaThread::current());
 871     }
 872     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 873   }
 874 
 875   oop obj = h_obj();
 876   address owner = NULL;
 877 


 878   markOop mark = ReadStableMark(obj);
 879 
 880   // Uncontended case, header points to stack
 881   if (mark->has_locker()) {
 882     owner = (address) mark->locker();
 883   }
 884 
 885   // Contended case, header points to ObjectMonitor (tagged pointer)
 886   else if (mark->has_monitor()) {
 887     ObjectMonitor* monitor = mark->monitor();






 888     assert(monitor != NULL, "monitor should be non-null");
 889     owner = (address) monitor->owner();
 890   }
 891 
 892   if (owner != NULL) {
 893     // owning_thread_from_monitor_owner() may also return NULL here
 894     return Threads::owning_thread_from_monitor_owner(t_list, owner);
 895   }
 896 
 897   // Unlocked case, header in place
 898   // Cannot have assertion since this object may have been
 899   // locked by another thread when reaching here.
 900   // assert(mark->is_neutral(), "sanity check");
 901 
 902   return NULL;

 903 }
 904 
 905 // Visitors ...
 906 
 907 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
 908   PaddedEnd<ObjectMonitor> * block = OrderAccess::load_acquire(&gBlockList);
 909   while (block != NULL) {
 910     assert(block->object() == CHAINMARKER, "must be a block header");
 911     for (int i = _BLOCKSIZE - 1; i > 0; i--) {
 912       ObjectMonitor* mid = (ObjectMonitor *)(block + i);
 913       oop object = (oop)mid->object();
 914       if (object != NULL) {










 915         closure->do_monitor(mid);
 916       }
 917     }
 918     block = (PaddedEnd<ObjectMonitor> *)block->FreeNext;
 919   }
 920 }
 921 
 922 // Get the next block in the block list.
 923 static inline PaddedEnd<ObjectMonitor>* next(PaddedEnd<ObjectMonitor>* block) {
 924   assert(block->object() == CHAINMARKER, "must be a block header");
 925   block = (PaddedEnd<ObjectMonitor>*) block->FreeNext;
 926   assert(block == NULL || block->object() == CHAINMARKER, "must be a block header");
 927   return block;
 928 }
 929 
 930 static bool monitors_used_above_threshold() {
 931   if (gMonitorPopulation == 0) {
 932     return false;
 933   }

 934   int monitors_used = gMonitorPopulation - gMonitorFreeCount;
 935   int monitor_usage = (monitors_used * 100LL) / gMonitorPopulation;
 936   return monitor_usage > MonitorUsedDeflationThreshold;


 937 }
 938 
 939 bool ObjectSynchronizer::is_cleanup_needed() {
 940   if (MonitorUsedDeflationThreshold > 0) {
 941     return monitors_used_above_threshold();









































 942   }
 943   return false;
 944 }
 945 




 946 void ObjectSynchronizer::oops_do(OopClosure* f) {
 947   // We only scan the global used list here (for moribund threads), and
 948   // the thread-local monitors in Thread::oops_do().
 949   global_used_oops_do(f);
 950 }
 951 
 952 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) {
 953   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 954   list_oops_do(gOmInUseList, f);
 955 }
 956 
 957 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) {
 958   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 959   list_oops_do(thread->omInUseList, f);
 960 }
 961 
 962 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, OopClosure* f) {
 963   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 964   ObjectMonitor* mid;
 965   for (mid = list; mid != NULL; mid = mid->FreeNext) {


 975 // -----------------------
 976 // Inflation unlinks monitors from the global gFreeList and
 977 // associates them with objects.  Deflation -- which occurs at
 978 // STW-time -- disassociates idle monitors from objects.  Such
 979 // scavenged monitors are returned to the gFreeList.
 980 //
 981 // The global list is protected by gListLock.  All the critical sections
 982 // are short and operate in constant-time.
 983 //
 984 // ObjectMonitors reside in type-stable memory (TSM) and are immortal.
 985 //
 986 // Lifecycle:
 987 // --   unassigned and on the global free list
 988 // --   unassigned and on a thread's private omFreeList
 989 // --   assigned to an object.  The object is inflated and the mark refers
 990 //      to the objectmonitor.
 991 
 992 
 993 // Constraining monitor pool growth via MonitorBound ...
 994 //



 995 // The monitor pool is grow-only.  We scavenge at STW safepoint-time, but the
 996 // the rate of scavenging is driven primarily by GC.  As such,  we can find
 997 // an inordinate number of monitors in circulation.
 998 // To avoid that scenario we can artificially induce a STW safepoint
 999 // if the pool appears to be growing past some reasonable bound.
1000 // Generally we favor time in space-time tradeoffs, but as there's no
1001 // natural back-pressure on the # of extant monitors we need to impose some
1002 // type of limit.  Beware that if MonitorBound is set to too low a value
1003 // we could just loop. In addition, if MonitorBound is set to a low value
1004 // we'll incur more safepoints, which are harmful to performance.
1005 // See also: GuaranteedSafepointInterval
1006 //
1007 // The current implementation uses asynchronous VM operations.
















1008 
1009 static void InduceScavenge(Thread * Self, const char * Whence) {


1010   // Induce STW safepoint to trim monitors
1011   // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
1012   // More precisely, trigger an asynchronous STW safepoint as the number
1013   // of active monitors passes the specified threshold.
1014   // TODO: assert thread state is reasonable
1015 
1016   if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
1017     // Induce a 'null' safepoint to scavenge monitors
1018     // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
1019     // to the VMthread and have a lifespan longer than that of this activation record.
1020     // The VMThread will delete the op when completed.
1021     VMThread::execute(new VM_ScavengeMonitors());
1022   }
1023 }
1024 
1025 ObjectMonitor* ObjectSynchronizer::omAlloc(Thread * Self) {

1026   // A large MAXPRIVATE value reduces both list lock contention
1027   // and list coherency traffic, but also tends to increase the
1028   // number of objectMonitors in circulation as well as the STW
1029   // scavenge costs.  As usual, we lean toward time in space-time
1030   // tradeoffs.
1031   const int MAXPRIVATE = 1024;

















1032   stringStream ss;
1033   for (;;) {
1034     ObjectMonitor * m;
1035 
1036     // 1: try to allocate from the thread's local omFreeList.
1037     // Threads will attempt to allocate first from their local list, then
1038     // from the global list, and only after those attempts fail will the thread
1039     // attempt to instantiate new monitors.   Thread-local free lists take
1040     // heat off the gListLock and improve allocation latency, as well as reducing
1041     // coherency traffic on the shared global list.
1042     m = Self->omFreeList;
1043     if (m != NULL) {
1044       Self->omFreeList = m->FreeNext;
1045       Self->omFreeCount--;
1046       guarantee(m->object() == NULL, "invariant");

1047       m->FreeNext = Self->omInUseList;
1048       Self->omInUseList = m;
1049       Self->omInUseCount++;
1050       return m;
1051     }
1052 
1053     // 2: try to allocate from the global gFreeList
1054     // CONSIDER: use muxTry() instead of muxAcquire().
1055     // If the muxTry() fails then drop immediately into case 3.
1056     // If we're using thread-local free lists then try
1057     // to reprovision the caller's free list.
1058     if (gFreeList != NULL) {
1059       // Reprovision the thread's omFreeList.
1060       // Use bulk transfers to reduce the allocation rate and heat
1061       // on various locks.
1062       Thread::muxAcquire(&gListLock, "omAlloc(1)");
1063       for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL;) {
1064         gMonitorFreeCount--;
1065         ObjectMonitor * take = gFreeList;
1066         gFreeList = take->FreeNext;
1067         guarantee(take->object() == NULL, "invariant");
















1068         take->Recycle();

1069         omRelease(Self, take, false);
1070       }
1071       Thread::muxRelease(&gListLock);
1072       Self->omFreeProvision += 1 + (Self->omFreeProvision/2);
1073       if (Self->omFreeProvision > MAXPRIVATE) Self->omFreeProvision = MAXPRIVATE;
1074 
1075       const int mx = MonitorBound;
1076       if (mx > 0 && (gMonitorPopulation-gMonitorFreeCount) > mx) {

1077         // We can't safely induce a STW safepoint from omAlloc() as our thread
1078         // state may not be appropriate for such activities and callers may hold
1079         // naked oops, so instead we defer the action.
1080         InduceScavenge(Self, "omAlloc");
1081       }
1082       continue;
1083     }
1084 
1085     // 3: allocate a block of new ObjectMonitors
1086     // Both the local and global free lists are empty -- resort to malloc().
1087     // In the current implementation objectMonitors are TSM - immortal.
1088     // Ideally, we'd write "new ObjectMonitor[_BLOCKSIZE], but we want
1089     // each ObjectMonitor to start at the beginning of a cache line,
1090     // so we use align_up().
1091     // A better solution would be to use C++ placement-new.
1092     // BEWARE: As it stands currently, we don't run the ctors!
1093     assert(_BLOCKSIZE > 1, "invariant");
1094     size_t neededsize = sizeof(PaddedEnd<ObjectMonitor>) * _BLOCKSIZE;
1095     PaddedEnd<ObjectMonitor> * temp;
1096     size_t aligned_size = neededsize + (DEFAULT_CACHE_LINE_SIZE - 1);


1101 
1102     // NOTE: (almost) no way to recover if allocation failed.
1103     // We might be able to induce a STW safepoint and scavenge enough
1104     // objectMonitors to permit progress.
1105     if (temp == NULL) {
1106       vm_exit_out_of_memory(neededsize, OOM_MALLOC_ERROR,
1107                             "Allocate ObjectMonitors");
1108     }
1109     (void)memset((void *) temp, 0, neededsize);
1110 
1111     // Format the block.
1112     // initialize the linked list, each monitor points to its next
1113     // forming the single linked free list, the very first monitor
1114     // will points to next block, which forms the block list.
1115     // The trick of using the 1st element in the block as gBlockList
1116     // linkage should be reconsidered.  A better implementation would
1117     // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
1118 
1119     for (int i = 1; i < _BLOCKSIZE; i++) {
1120       temp[i].FreeNext = (ObjectMonitor *)&temp[i+1];

1121     }
1122 
1123     // terminate the last monitor as the end of list
1124     temp[_BLOCKSIZE - 1].FreeNext = NULL;
1125 
1126     // Element [0] is reserved for global list linkage
1127     temp[0].set_object(CHAINMARKER);
1128 
1129     // Consider carving out this thread's current request from the
1130     // block in hand.  This avoids some lock traffic and redundant
1131     // list activity.
1132 
1133     // Acquire the gListLock to manipulate gBlockList and gFreeList.
1134     // An Oyama-Taura-Yonezawa scheme might be more efficient.
1135     Thread::muxAcquire(&gListLock, "omAlloc(2)");
1136     gMonitorPopulation += _BLOCKSIZE-1;
1137     gMonitorFreeCount += _BLOCKSIZE-1;
1138 
1139     // Add the new block to the list of extant blocks (gBlockList).
1140     // The very first objectMonitor in a block is reserved and dedicated.


1143     // There are lock-free uses of gBlockList so make sure that
1144     // the previous stores happen before we update gBlockList.
1145     OrderAccess::release_store(&gBlockList, temp);
1146 
1147     // Add the new string of objectMonitors to the global free list
1148     temp[_BLOCKSIZE - 1].FreeNext = gFreeList;
1149     gFreeList = temp + 1;
1150     Thread::muxRelease(&gListLock);
1151   }
1152 }
1153 
1154 // Place "m" on the caller's private per-thread omFreeList.
1155 // In practice there's no need to clamp or limit the number of
1156 // monitors on a thread's omFreeList as the only time we'll call
1157 // omRelease is to return a monitor to the free list after a CAS
1158 // attempt failed.  This doesn't allow unbounded #s of monitors to
1159 // accumulate on a thread's free list.
1160 //
1161 // Key constraint: all ObjectMonitors on a thread's free list and the global
1162 // free list must have their object field set to null. This prevents the
1163 // scavenger -- deflate_monitor_list() -- from reclaiming them.

1164 
1165 void ObjectSynchronizer::omRelease(Thread * Self, ObjectMonitor * m,
1166                                    bool fromPerThreadAlloc) {
1167   guarantee(m->header() == NULL, "invariant");
1168   guarantee(m->object() == NULL, "invariant");
1169   stringStream ss;
1170   guarantee((m->is_busy() | m->_recursions) == 0, "freeing in-use monitor: "
1171             "%s, recursions=" INTPTR_FORMAT, m->is_busy_to_string(&ss),
1172             m->_recursions);

1173   // Remove from omInUseList
1174   if (fromPerThreadAlloc) {
1175     ObjectMonitor* cur_mid_in_use = NULL;
1176     bool extracted = false;
1177     for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; cur_mid_in_use = mid, mid = mid->FreeNext) {
1178       if (m == mid) {
1179         // extract from per-thread in-use list
1180         if (mid == Self->omInUseList) {
1181           Self->omInUseList = mid->FreeNext;
1182         } else if (cur_mid_in_use != NULL) {
1183           cur_mid_in_use->FreeNext = mid->FreeNext; // maintain the current thread in-use list
1184         }
1185         extracted = true;
1186         Self->omInUseCount--;
1187         break;
1188       }
1189     }
1190     assert(extracted, "Should have extracted from in-use list");
1191   }
1192 
1193   // FreeNext is used for both omInUseList and omFreeList, so clear old before setting new
1194   m->FreeNext = Self->omFreeList;

1195   Self->omFreeList = m;
1196   Self->omFreeCount++;
1197 }
1198 
1199 // Return the monitors of a moribund thread's local free list to
1200 // the global free list.  Typically a thread calls omFlush() when
1201 // it's dying.  We could also consider having the VM thread steal
1202 // monitors from threads that have not run java code over a few
1203 // consecutive STW safepoints.  Relatedly, we might decay
1204 // omFreeProvision at STW safepoints.
1205 //
1206 // Also return the monitors of a moribund thread's omInUseList to
1207 // a global gOmInUseList under the global list lock so these
1208 // will continue to be scanned.
1209 //
1210 // We currently call omFlush() from Threads::remove() _before the thread
1211 // has been excised from the thread list and is no longer a mutator.
1212 // This means that omFlush() cannot run concurrently with a safepoint and
1213 // interleave with the deflate_idle_monitors scavenge operator. In particular,
1214 // this ensures that the thread's monitors are scanned by a GC safepoint,
1215 // either via Thread::oops_do() (if safepoint happens before omFlush()) or via
1216 // ObjectSynchronizer::oops_do() (if it happens after omFlush() and the thread's
1217 // monitors have been transferred to the global in-use list).




1218 
1219 void ObjectSynchronizer::omFlush(Thread * Self) {
1220   ObjectMonitor * list = Self->omFreeList;  // Null-terminated SLL
1221   ObjectMonitor * tail = NULL;
1222   int tally = 0;
1223   if (list != NULL) {
1224     ObjectMonitor * s;
1225     // The thread is going away. Set 'tail' to the last per-thread free
1226     // monitor which will be linked to gFreeList below under the gListLock.
1227     stringStream ss;
1228     for (s = list; s != NULL; s = s->FreeNext) {
1229       tally++;
1230       tail = s;
1231       guarantee(s->object() == NULL, "invariant");
1232       guarantee(!s->is_busy(), "must be !is_busy: %s", s->is_busy_to_string(&ss));
1233     }
1234     guarantee(tail != NULL, "invariant");
1235     assert(Self->omFreeCount == tally, "free-count off");
1236     Self->omFreeList = NULL;
1237     Self->omFreeCount = 0;
1238   }
1239 
1240   ObjectMonitor * inUseList = Self->omInUseList;
1241   ObjectMonitor * inUseTail = NULL;
1242   int inUseTally = 0;
1243   if (inUseList != NULL) {
1244     ObjectMonitor *cur_om;
1245     // The thread is going away, however the omInUseList inflated
1246     // monitors may still be in-use by other threads.
1247     // Link them to inUseTail, which will be linked into the global in-use list
1248     // gOmInUseList below, under the gListLock
1249     for (cur_om = inUseList; cur_om != NULL; cur_om = cur_om->FreeNext) {
1250       inUseTail = cur_om;
1251       inUseTally++;

1252     }
1253     guarantee(inUseTail != NULL, "invariant");
1254     assert(Self->omInUseCount == inUseTally, "in-use count off");
1255     Self->omInUseList = NULL;
1256     Self->omInUseCount = 0;
1257   }
1258 
1259   Thread::muxAcquire(&gListLock, "omFlush");
1260   if (tail != NULL) {
1261     tail->FreeNext = gFreeList;
1262     gFreeList = list;
1263     gMonitorFreeCount += tally;
1264   }
1265 
1266   if (inUseTail != NULL) {
1267     inUseTail->FreeNext = gOmInUseList;
1268     gOmInUseList = inUseList;
1269     gOmInUseCount += inUseTally;
1270   }
1271 
1272   Thread::muxRelease(&gListLock);
1273 
1274   LogStreamHandle(Debug, monitorinflation) lsh_debug;


1282   }
1283   if (ls != NULL) {
1284     ls->print_cr("omFlush: jt=" INTPTR_FORMAT ", free_monitor_tally=%d"
1285                  ", in_use_monitor_tally=%d" ", omFreeProvision=%d",
1286                  p2i(Self), tally, inUseTally, Self->omFreeProvision);
1287   }
1288 }
1289 
1290 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1291                                        const oop obj,
1292                                        ObjectSynchronizer::InflateCause cause) {
1293   assert(event != NULL, "invariant");
1294   assert(event->should_commit(), "invariant");
1295   event->set_monitorClass(obj->klass());
1296   event->set_address((uintptr_t)(void*)obj);
1297   event->set_cause((u1)cause);
1298   event->commit();
1299 }
1300 
1301 // Fast path code shared by multiple functions
1302 void ObjectSynchronizer::inflate_helper(oop obj) {

1303   markOop mark = obj->mark();
1304   if (mark->has_monitor()) {
1305     assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid");
1306     assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header");










1307     return;
1308   }
1309   inflate(Thread::current(), obj, inflate_cause_vm_internal);
1310 }
1311 
1312 ObjectMonitor* ObjectSynchronizer::inflate(Thread * Self,
1313                                            oop object,
1314                                            const InflateCause cause) {
1315   // Inflate mutates the heap ...
1316   // Relaxing assertion for bug 6320749.
1317   assert(Universe::verify_in_progress() ||
1318          !SafepointSynchronize::is_at_safepoint(), "invariant");
1319 
1320   EventJavaMonitorInflate event;
1321 
1322   for (;;) {
1323     const markOop mark = object->mark();
1324     assert(!mark->has_bias_pattern(), "invariant");
1325 
1326     // The mark can be in one of the following states:
1327     // *  Inflated     - just return
1328     // *  Stack-locked - coerce it to inflated
1329     // *  INFLATING    - busy wait for conversion to complete
1330     // *  Neutral      - aggressively inflate the object.
1331     // *  BIASED       - Illegal.  We should never see this
1332 
1333     // CASE: inflated
1334     if (mark->has_monitor()) {
1335       ObjectMonitor * inf = mark->monitor();





1336       markOop dmw = inf->header();
1337       assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1338       assert(oopDesc::equals((oop) inf->object(), object), "invariant");
1339       assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1340       return inf;
1341     }
1342 
1343     // CASE: inflation in progress - inflating over a stack-lock.
1344     // Some other thread is converting from stack-locked to inflated.
1345     // Only that thread can complete inflation -- other threads must wait.
1346     // The INFLATING value is transient.
1347     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1348     // We could always eliminate polling by parking the thread on some auxiliary list.
1349     if (mark == markOopDesc::INFLATING()) {
1350       ReadStableMark(object);
1351       continue;
1352     }
1353 
1354     // CASE: stack-locked
1355     // Could be stack-locked either by this thread or by some other thread.
1356     //
1357     // Note that we allocate the objectmonitor speculatively, _before_ attempting
1358     // to install INFLATING into the mark word.  We originally installed INFLATING,
1359     // allocated the objectmonitor, and then finally STed the address of the
1360     // objectmonitor into the mark.  This was correct, but artificially lengthened
1361     // the interval in which INFLATED appeared in the mark, thus increasing
1362     // the odds of inflation contention.
1363     //
1364     // We now use per-thread private objectmonitor free lists.
1365     // These list are reprovisioned from the global free list outside the
1366     // critical INFLATING...ST interval.  A thread can transfer
1367     // multiple objectmonitors en-mass from the global free list to its local free list.
1368     // This reduces coherency traffic and lock contention on the global free list.
1369     // Using such local free lists, it doesn't matter if the omAlloc() call appears
1370     // before or after the CAS(INFLATING) operation.
1371     // See the comments in omAlloc().
1372 
1373     LogStreamHandle(Trace, monitorinflation) lsh;
1374 
1375     if (mark->has_locker()) {
1376       ObjectMonitor * m = omAlloc(Self);











1377       // Optimistically prepare the objectmonitor - anticipate successful CAS
1378       // We do this before the CAS in order to minimize the length of time
1379       // in which INFLATING appears in the mark.
1380       m->Recycle();
1381       m->_Responsible  = NULL;
1382       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;   // Consider: maintain by type/class
1383 
1384       markOop cmp = object->cas_set_mark(markOopDesc::INFLATING(), mark);
1385       if (cmp != mark) {
1386         omRelease(Self, m, true);
1387         continue;       // Interference -- just retry
1388       }
1389 
1390       // We've successfully installed INFLATING (0) into the mark-word.
1391       // This is the only case where 0 will appear in a mark-word.
1392       // Only the singular thread that successfully swings the mark-word
1393       // to 0 can perform (or more precisely, complete) inflation.
1394       //
1395       // Why do we CAS a 0 into the mark-word instead of just CASing the
1396       // mark-word from the stack-locked value directly to the new inflated state?


1403       // value from the basiclock on the owner's stack to the objectMonitor, all
1404       // the while preserving the hashCode stability invariants.  If the owner
1405       // decides to release the lock while the value is 0, the unlock will fail
1406       // and control will eventually pass from slow_exit() to inflate.  The owner
1407       // will then spin, waiting for the 0 value to disappear.   Put another way,
1408       // the 0 causes the owner to stall if the owner happens to try to
1409       // drop the lock (restoring the header from the basiclock to the object)
1410       // while inflation is in-progress.  This protocol avoids races that might
1411       // would otherwise permit hashCode values to change or "flicker" for an object.
1412       // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
1413       // 0 serves as a "BUSY" inflate-in-progress indicator.
1414 
1415 
1416       // fetch the displaced mark from the owner's stack.
1417       // The owner can't die or unwind past the lock while our INFLATING
1418       // object is in the mark.  Furthermore the owner can't complete
1419       // an unlock on the object, either.
1420       markOop dmw = mark->displaced_mark_helper();
1421       // Catch if the object's header is not neutral (not locked and
1422       // not marked is what we care about here).
1423       assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1424 
1425       // Setup monitor fields to proper values -- prepare the monitor
1426       m->set_header(dmw);
1427 
1428       // Optimization: if the mark->locker stack address is associated
1429       // with this thread we could simply set m->_owner = Self.
1430       // Note that a thread can inflate an object
1431       // that it has stack-locked -- as might happen in wait() -- directly
1432       // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1433       m->set_owner(mark->locker());
1434       m->set_object(object);
1435       // TODO-FIXME: assert BasicLock->dhw != 0.
1436 




1437       // Must preserve store ordering. The monitor state must
1438       // be stable at the time of publishing the monitor address.
1439       guarantee(object->mark() == markOopDesc::INFLATING(), "invariant");
1440       object->release_set_mark(markOopDesc::encode(m));
1441 
1442       // Hopefully the performance counters are allocated on distinct cache lines
1443       // to avoid false sharing on MP systems ...
1444       OM_PERFDATA_OP(Inflations, inc());
1445       if (log_is_enabled(Trace, monitorinflation)) {
1446         ResourceMark rm(Self);
1447         lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1448                      INTPTR_FORMAT ", type='%s'", p2i(object),
1449                      p2i(object->mark()), object->klass()->external_name());
1450       }
1451       if (event.should_commit()) {
1452         post_monitor_inflate_event(&event, object, cause);
1453       }
1454       return m;

1455     }
1456 
1457     // CASE: neutral
1458     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1459     // If we know we're inflating for entry it's better to inflate by swinging a
1460     // pre-locked objectMonitor pointer into the object header.   A successful
1461     // CAS inflates the object *and* confers ownership to the inflating thread.
1462     // In the current implementation we use a 2-step mechanism where we CAS()
1463     // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1464     // An inflateTry() method that we could call from fast_enter() and slow_enter()
1465     // would be useful.
1466 
1467     // Catch if the object's header is not neutral (not locked and
1468     // not marked is what we care about here).
1469     assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark));
1470     ObjectMonitor * m = omAlloc(Self);











1471     // prepare m for installation - set monitor to initial state
1472     m->Recycle();
1473     m->set_header(mark);



1474     m->set_object(object);
1475     m->_Responsible  = NULL;
1476     m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;       // consider: keep metastats by type/class
1477 




1478     if (object->cas_set_mark(markOopDesc::encode(m), mark) != mark) {
1479       m->set_header(NULL);
1480       m->set_object(NULL);
1481       m->Recycle();


1482       omRelease(Self, m, true);
1483       m = NULL;
1484       continue;
1485       // interference - the markword changed - just retry.
1486       // The state-transitions are one-way, so there's no chance of
1487       // live-lock -- "Inflated" is an absorbing state.
1488     }
1489 
1490     // Hopefully the performance counters are allocated on distinct
1491     // cache lines to avoid false sharing on MP systems ...
1492     OM_PERFDATA_OP(Inflations, inc());
1493     if (log_is_enabled(Trace, monitorinflation)) {
1494       ResourceMark rm(Self);
1495       lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
1496                    INTPTR_FORMAT ", type='%s'", p2i(object),
1497                    p2i(object->mark()), object->klass()->external_name());
1498     }
1499     if (event.should_commit()) {
1500       post_monitor_inflate_event(&event, object, cause);
1501     }
1502     return m;

1503   }
1504 }
1505 
1506 
1507 // We maintain a list of in-use monitors for each thread.
1508 //

1509 // deflate_thread_local_monitors() scans a single thread's in-use list, while
1510 // deflate_idle_monitors() scans only a global list of in-use monitors which
1511 // is populated only as a thread dies (see omFlush()).
1512 //
1513 // These operations are called at all safepoints, immediately after mutators
1514 // are stopped, but before any objects have moved. Collectively they traverse
1515 // the population of in-use monitors, deflating where possible. The scavenged
1516 // monitors are returned to the global monitor free list.
1517 //
1518 // Beware that we scavenge at *every* stop-the-world point. Having a large
1519 // number of monitors in-use could negatively impact performance. We also want
1520 // to minimize the total # of monitors in circulation, as they incur a small
1521 // footprint penalty.
1522 //
1523 // Perversely, the heap size -- and thus the STW safepoint rate --
1524 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
1525 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
1526 // This is an unfortunate aspect of this design.

























1527 
1528 // Deflate a single monitor if not in-use
1529 // Return true if deflated, false if in-use
1530 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1531                                          ObjectMonitor** freeHeadp,
1532                                          ObjectMonitor** freeTailp) {
1533   bool deflated;
1534   // Normal case ... The monitor is associated with obj.
1535   const markOop mark = obj->mark();
1536   guarantee(mark == markOopDesc::encode(mid), "should match: mark="
1537             INTPTR_FORMAT ", encoded mid=" INTPTR_FORMAT, p2i(mark),
1538             p2i(markOopDesc::encode(mid)));
1539   // Make sure that mark->monitor() and markOopDesc::encode() agree:
1540   guarantee(mark->monitor() == mid, "should match: monitor()=" INTPTR_FORMAT
1541             ", mid=" INTPTR_FORMAT, p2i(mark->monitor()), p2i(mid));
1542   const markOop dmw = mid->header();
1543   guarantee(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1544 
1545   if (mid->is_busy()) {


1546     deflated = false;
1547   } else {
1548     // Deflate the monitor if it is no longer being used
1549     // It's idle - scavenge and return to the global free list
1550     // plain old deflation ...
1551     if (log_is_enabled(Trace, monitorinflation)) {
1552       ResourceMark rm;
1553       log_trace(monitorinflation)("deflate_monitor: "
1554                                   "object=" INTPTR_FORMAT ", mark="
1555                                   INTPTR_FORMAT ", type='%s'", p2i(obj),
1556                                   p2i(mark), obj->klass()->external_name());
1557     }
1558 
1559     // Restore the header back to obj
1560     obj->release_set_mark(dmw);






1561     mid->clear();
1562 
1563     assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT,
1564            p2i(mid->object()));

1565 
1566     // Move the object to the working free list defined by freeHeadp, freeTailp
1567     if (*freeHeadp == NULL) *freeHeadp = mid;
1568     if (*freeTailp != NULL) {
1569       ObjectMonitor * prevtail = *freeTailp;
1570       assert(prevtail->FreeNext == NULL, "cleaned up deflated?");
1571       prevtail->FreeNext = mid;
1572     }
1573     *freeTailp = mid;
1574     deflated = true;
1575   }
1576   return deflated;
1577 }
1578 












































































































































1579 // Walk a given monitor list, and deflate idle monitors
1580 // The given list could be a per-thread list or a global list
1581 // Caller acquires gListLock as needed.
1582 //
1583 // In the case of parallel processing of thread local monitor lists,
1584 // work is done by Threads::parallel_threads_do() which ensures that
1585 // each Java thread is processed by exactly one worker thread, and
1586 // thus avoid conflicts that would arise when worker threads would
1587 // process the same monitor lists concurrently.
1588 //
1589 // See also ParallelSPCleanupTask and
1590 // SafepointSynchronize::do_cleanup_tasks() in safepoint.cpp and
1591 // Threads::parallel_java_threads_do() in thread.cpp.
1592 int ObjectSynchronizer::deflate_monitor_list(ObjectMonitor** listHeadp,
1593                                              ObjectMonitor** freeHeadp,
1594                                              ObjectMonitor** freeTailp) {
1595   ObjectMonitor* mid;
1596   ObjectMonitor* next;
1597   ObjectMonitor* cur_mid_in_use = NULL;
1598   int deflated_count = 0;


1602     if (obj != NULL && deflate_monitor(mid, obj, freeHeadp, freeTailp)) {
1603       // if deflate_monitor succeeded,
1604       // extract from per-thread in-use list
1605       if (mid == *listHeadp) {
1606         *listHeadp = mid->FreeNext;
1607       } else if (cur_mid_in_use != NULL) {
1608         cur_mid_in_use->FreeNext = mid->FreeNext; // maintain the current thread in-use list
1609       }
1610       next = mid->FreeNext;
1611       mid->FreeNext = NULL;  // This mid is current tail in the freeHeadp list
1612       mid = next;
1613       deflated_count++;
1614     } else {
1615       cur_mid_in_use = mid;
1616       mid = mid->FreeNext;
1617     }
1618   }
1619   return deflated_count;
1620 }
1621 












































































1622 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) {
1623   counters->nInuse = 0;              // currently associated with objects
1624   counters->nInCirculation = 0;      // extant
1625   counters->nScavenged = 0;          // reclaimed (global and per-thread)
1626   counters->perThreadScavenged = 0;  // per-thread scavenge total
1627   counters->perThreadTimes = 0.0;    // per-thread scavenge times
1628 }
1629 
1630 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) {
1631   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");









1632   bool deflated = false;
1633 
1634   ObjectMonitor * freeHeadp = NULL;  // Local SLL of scavenged monitors
1635   ObjectMonitor * freeTailp = NULL;
1636   elapsedTimer timer;
1637 
1638   if (log_is_enabled(Info, monitorinflation)) {
1639     timer.start();
1640   }
1641 
1642   // Prevent omFlush from changing mids in Thread dtor's during deflation
1643   // And in case the vm thread is acquiring a lock during a safepoint
1644   // See e.g. 6320749
1645   Thread::muxAcquire(&gListLock, "deflate_idle_monitors");
1646 
1647   // Note: the thread-local monitors lists get deflated in
1648   // a separate pass. See deflate_thread_local_monitors().
1649 
1650   // For moribund threads, scan gOmInUseList
1651   int deflated_count = 0;


1664     // constant-time list splice - prepend scavenged segment to gFreeList
1665     freeTailp->FreeNext = gFreeList;
1666     gFreeList = freeHeadp;
1667   }
1668   Thread::muxRelease(&gListLock);
1669   timer.stop();
1670 
1671   LogStreamHandle(Debug, monitorinflation) lsh_debug;
1672   LogStreamHandle(Info, monitorinflation) lsh_info;
1673   LogStream * ls = NULL;
1674   if (log_is_enabled(Debug, monitorinflation)) {
1675     ls = &lsh_debug;
1676   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
1677     ls = &lsh_info;
1678   }
1679   if (ls != NULL) {
1680     ls->print_cr("deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
1681   }
1682 }
1683 
























































































































1684 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
1685   // Report the cumulative time for deflating each thread's idle
1686   // monitors. Note: if the work is split among more than one
1687   // worker thread, then the reported time will likely be more
1688   // than a beginning to end measurement of the phase.


1689   log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->perThreadTimes, counters->perThreadScavenged);
1690 





1691   gMonitorFreeCount += counters->nScavenged;
1692 




1693   if (log_is_enabled(Debug, monitorinflation)) {
1694     // exit_globals()'s call to audit_and_print_stats() is done
1695     // at the Info level.
1696     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
1697   } else if (log_is_enabled(Info, monitorinflation)) {
1698     Thread::muxAcquire(&gListLock, "finish_deflate_idle_monitors");
1699     log_info(monitorinflation)("gMonitorPopulation=%d, gOmInUseCount=%d, "
1700                                "gMonitorFreeCount=%d", gMonitorPopulation,
1701                                gOmInUseCount, gMonitorFreeCount);
1702     Thread::muxRelease(&gListLock);
1703   }
1704 
1705   ForceMonitorScavenge = 0;    // Reset
1706 
1707   OM_PERFDATA_OP(Deflations, inc(counters->nScavenged));
1708   OM_PERFDATA_OP(MonExtant, set_value(counters->nInCirculation));
1709 
1710   GVars.stwRandom = os::random();
1711   GVars.stwCycle++;



1712 }
1713 
1714 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
1715   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1716 












1717   ObjectMonitor * freeHeadp = NULL;  // Local SLL of scavenged monitors
1718   ObjectMonitor * freeTailp = NULL;
1719   elapsedTimer timer;
1720 
1721   if (log_is_enabled(Info, safepoint, cleanup) ||
1722       log_is_enabled(Info, monitorinflation)) {
1723     timer.start();
1724   }
1725 
1726   int deflated_count = deflate_monitor_list(thread->omInUseList_addr(), &freeHeadp, &freeTailp);
1727 
1728   Thread::muxAcquire(&gListLock, "deflate_thread_local_monitors");
1729 
1730   // Adjust counters
1731   counters->nInCirculation += thread->omInUseCount;
1732   thread->omInUseCount -= deflated_count;
1733   counters->nScavenged += deflated_count;
1734   counters->nInuse += thread->omInUseCount;
1735   counters->perThreadScavenged += deflated_count;
1736 


1923 void ObjectSynchronizer::chk_free_entry(JavaThread * jt, ObjectMonitor * n,
1924                                         outputStream * out, int *error_cnt_p) {
1925   stringStream ss;
1926   if (n->is_busy()) {
1927     if (jt != NULL) {
1928       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
1929                     ": free per-thread monitor must not be busy: %s", p2i(jt),
1930                     p2i(n), n->is_busy_to_string(&ss));
1931     } else {
1932       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
1933                     "must not be busy: %s", p2i(n), n->is_busy_to_string(&ss));
1934     }
1935     *error_cnt_p = *error_cnt_p + 1;
1936   }
1937   if (n->header() != NULL) {
1938     if (jt != NULL) {
1939       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
1940                     ": free per-thread monitor must have NULL _header "
1941                     "field: _header=" INTPTR_FORMAT, p2i(jt), p2i(n),
1942                     p2i(n->header()));
1943     } else {

1944       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
1945                     "must have NULL _header field: _header=" INTPTR_FORMAT,
1946                     p2i(n), p2i(n->header()));
1947     }
1948     *error_cnt_p = *error_cnt_p + 1;
1949   }

1950   if (n->object() != NULL) {
1951     if (jt != NULL) {
1952       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
1953                     ": free per-thread monitor must have NULL _object "
1954                     "field: _object=" INTPTR_FORMAT, p2i(jt), p2i(n),
1955                     p2i(n->object()));
1956     } else {
1957       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
1958                     "must have NULL _object field: _object=" INTPTR_FORMAT,
1959                     p2i(n), p2i(n->object()));
1960     }
1961     *error_cnt_p = *error_cnt_p + 1;
1962   }
1963 }
1964 
1965 // Check the global free list and count; log the results of the checks.
1966 void ObjectSynchronizer::chk_global_free_list_and_count(outputStream * out,
1967                                                         int *error_cnt_p) {
1968   int chkMonitorFreeCount = 0;
1969   for (ObjectMonitor * n = gFreeList; n != NULL; n = n->FreeNext) {


2096                   "equal to chkOmInUseCount=%d", p2i(jt), jt->omInUseCount,
2097                   chkOmInUseCount);
2098     *error_cnt_p = *error_cnt_p + 1;
2099   }
2100 }
2101 
2102 // Log details about ObjectMonitors on the in-use lists. The 'BHL'
2103 // flags indicate why the entry is in-use, 'object' and 'object type'
2104 // indicate the associated object and its type.
2105 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out,
2106                                                     bool on_exit) {
2107   if (!on_exit) {
2108     // Not at VM exit so grab the global list lock.
2109     Thread::muxAcquire(&gListLock, "log_in_use_monitor_details");
2110   }
2111 
2112   stringStream ss;
2113   if (gOmInUseCount > 0) {
2114     out->print_cr("In-use global monitor info:");
2115     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2116     out->print_cr("%18s  %s  %18s  %18s",
2117                   "monitor", "BHL", "object", "object type");
2118     out->print_cr("==================  ===  ==================  ==================");
2119     for (ObjectMonitor * n = gOmInUseList; n != NULL; n = n->FreeNext) {
2120       const oop obj = (oop) n->object();
2121       const markOop mark = n->header();
2122       ResourceMark rm;
2123       out->print(INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT "  %s", p2i(n),
2124                  n->is_busy() != 0, mark->hash() != 0, n->owner() != NULL,
2125                  p2i(obj), 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   if (!on_exit) {
2135     Thread::muxRelease(&gListLock);
2136   }
2137 
2138   out->print_cr("In-use per-thread monitor info:");
2139   out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2140   out->print_cr("%18s  %18s  %s  %18s  %18s",
2141                 "jt", "monitor", "BHL", "object", "object type");
2142   out->print_cr("==================  ==================  ===  ==================  ==================");
2143   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2144     for (ObjectMonitor * n = jt->omInUseList; n != NULL; n = n->FreeNext) {
2145       const oop obj = (oop) n->object();
2146       const markOop mark = n->header();
2147       ResourceMark rm;
2148       out->print(INTPTR_FORMAT "  " INTPTR_FORMAT "  %d%d%d  " INTPTR_FORMAT
2149                  "  %s", p2i(jt), p2i(n), n->is_busy() != 0,
2150                  mark->hash() != 0, n->owner() != NULL, p2i(obj),
2151                  obj->klass()->external_name());
2152       if (n->is_busy() != 0) {
2153         out->print(" (%s)", n->is_busy_to_string(&ss));
2154         ss.reset();
2155       }
2156       out->cr();
2157     }
2158   }
2159 
2160   out->flush();
2161 }
2162 
2163 // Log counts for the global and per-thread monitor lists and return
2164 // the population count.
2165 int ObjectSynchronizer::log_monitor_list_counts(outputStream * out) {
2166   int popCount = 0;
2167   out->print_cr("%18s  %10s  %10s  %10s",
2168                 "Global Lists:", "InUse", "Free", "Total");
2169   out->print_cr("==================  ==========  ==========  ==========");
2170   out->print_cr("%18s  %10d  %10d  %10d", "",
2171                 gOmInUseCount, gMonitorFreeCount, gMonitorPopulation);




 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 PaddedEnd<ObjectMonitor> * volatile ObjectSynchronizer::gBlockList = NULL;
 121 // global monitor free list
 122 ObjectMonitor * volatile ObjectSynchronizer::gFreeList  = NULL;
 123 // global monitor in-use list, for moribund threads,
 124 // monitors they inflated need to be scanned for deflation
 125 ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList  = NULL;
 126 // count of entries in gOmInUseList
 127 int ObjectSynchronizer::gOmInUseCount = 0;
 128 bool volatile ObjectSynchronizer::_is_async_deflation_requested = false;
 129 bool volatile ObjectSynchronizer::_is_special_deflation_requested = false;
 130 jlong ObjectSynchronizer::_last_async_deflation_time_ns = 0;
 131 
 132 static volatile intptr_t gListLock = 0;      // protects global monitor lists
 133 static volatile int gMonitorFreeCount  = 0;  // # on gFreeList
 134 static volatile int gMonitorPopulation = 0;  // # Extant -- in circulation
 135 
 136 #define CHAINMARKER (cast_to_oop<intptr_t>(-1))
 137 
 138 
 139 // =====================> Quick functions
 140 
 141 // The quick_* forms are special fast-path variants used to improve
 142 // performance.  In the simplest case, a "quick_*" implementation could
 143 // simply return false, in which case the caller will perform the necessary
 144 // state transitions and call the slow-path form.
 145 // The fast-path is designed to handle frequently arising cases in an efficient
 146 // manner and is just a degenerate "optimistic" variant of the slow-path.
 147 // returns true  -- to indicate the call was satisfied.
 148 // returns false -- to indicate the call needs the services of the slow-path.
 149 // A no-loitering ordinance is in effect for code in the quick_* family
 150 // operators: safepoints or indefinite blocking (blocking that might span a


 197   }
 198 
 199   // biased locking and any other IMS exception states take the slow-path
 200   return false;
 201 }
 202 
 203 
 204 // The LockNode emitted directly at the synchronization site would have
 205 // been too big if it were to have included support for the cases of inflated
 206 // recursive enter and exit, so they go here instead.
 207 // Note that we can't safely call AsyncPrintJavaStack() from within
 208 // quick_enter() as our thread state remains _in_Java.
 209 
 210 bool ObjectSynchronizer::quick_enter(oop obj, Thread * Self,
 211                                      BasicLock * lock) {
 212   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 213   assert(Self->is_Java_thread(), "invariant");
 214   assert(((JavaThread *) Self)->thread_state() == _thread_in_Java, "invariant");
 215   NoSafepointVerifier nsv;
 216   if (obj == NULL) return false;       // Need to throw NPE
 217 
 218   while (true) {
 219     const markOop mark = obj->mark();
 220 
 221     if (mark->has_monitor()) {
 222       ObjectMonitorHandle omh;
 223       if (!omh.save_om_ptr(obj, mark)) {
 224         // Lost a race with async deflation so try again.
 225         assert(AsyncDeflateIdleMonitors, "sanity check");
 226         continue;
 227       }
 228       ObjectMonitor * const m = omh.om_ptr();
 229       assert(oopDesc::equals((oop) m->object(), obj), "invariant");
 230       Thread * const owner = (Thread *) m->_owner;
 231 
 232       // Lock contention and Transactional Lock Elision (TLE) diagnostics
 233       // and observability
 234       // Case: light contention possibly amenable to TLE
 235       // Case: TLE inimical operations such as nested/recursive synchronization
 236 
 237       if (owner == Self) {
 238         m->_recursions++;
 239         return true;
 240       }
 241 
 242       // This Java Monitor is inflated so obj's header will never be
 243       // displaced to this thread's BasicLock. Make the displaced header
 244       // non-NULL so this BasicLock is not seen as recursive nor as
 245       // being locked. We do this unconditionally so that this thread's
 246       // BasicLock cannot be mis-interpreted by any stack walkers. For
 247       // performance reasons, stack walkers generally first check for
 248       // Biased Locking in the object's header, the second check is for
 249       // stack-locking in the object's header, the third check is for
 250       // recursive stack-locking in the displaced header in the BasicLock,
 251       // and last are the inflated Java Monitor (ObjectMonitor) checks.
 252       lock->set_displaced_header(markOopDesc::unused_mark());
 253 
 254       if (owner == NULL && Atomic::replace_if_null(Self, &(m->_owner))) {
 255         assert(m->_recursions == 0, "invariant");
 256         return true;
 257       }
 258 
 259       if (AsyncDeflateIdleMonitors &&
 260           Atomic::cmpxchg(Self, &m->_owner, DEFLATER_MARKER) == DEFLATER_MARKER) {
 261         // The deflation protocol finished the first part (setting owner),
 262         // but it failed the second part (making ref_count negative) and
 263         // bailed. Or the ObjectMonitor was async deflated and reused.
 264         // Acquired the monitor.
 265         assert(m->_recursions == 0, "invariant");
 266         return true;
 267       }
 268     }
 269     break;
 270   }
 271 
 272   // Note that we could inflate in quick_enter.
 273   // This is likely a useful optimization
 274   // Critically, in quick_enter() we must not:
 275   // -- perform bias revocation, or
 276   // -- block indefinitely, or
 277   // -- reach a safepoint
 278 
 279   return false;        // revert to slow-path
 280 }
 281 
 282 // -----------------------------------------------------------------------------
 283 //  Fast Monitor Enter/Exit
 284 // This the fast monitor enter. The interpreter and compiler use
 285 // some assembly copies of this code. Make sure update those code
 286 // if the following function is changed. The implementation is
 287 // extremely sensitive to race condition. Be careful.
 288 
 289 void ObjectSynchronizer::fast_enter(Handle obj, BasicLock* lock,


 333         // does not own the Java Monitor.
 334         ObjectMonitor * m = mark->monitor();
 335         assert(((oop)(m->object()))->mark() == mark, "invariant");
 336         assert(m->is_entered(THREAD), "invariant");
 337       }
 338     }
 339 #endif
 340     return;
 341   }
 342 
 343   if (mark == (markOop) lock) {
 344     // If the object is stack-locked by the current thread, try to
 345     // swing the displaced header from the BasicLock back to the mark.
 346     assert(dhw->is_neutral(), "invariant");
 347     if (object->cas_set_mark(dhw, mark) == mark) {
 348       return;
 349     }
 350   }
 351 
 352   // We have to take the slow-path of possible inflation and then exit.
 353   ObjectMonitorHandle omh;
 354   inflate(&omh, THREAD, object, inflate_cause_vm_internal);
 355   omh.om_ptr()->exit(true, THREAD);
 356 }
 357 
 358 // -----------------------------------------------------------------------------
 359 // Interpreter/Compiler Slow Case
 360 // This routine is used to handle interpreter/compiler slow case
 361 // We don't need to use fast path here, because it must have been
 362 // failed in the interpreter/compiler code.
 363 void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) {
 364   markOop mark = obj->mark();
 365   assert(!mark->has_bias_pattern(), "should not see bias pattern here");
 366 
 367   if (mark->is_neutral()) {
 368     // Anticipate successful CAS -- the ST of the displaced mark must
 369     // be visible <= the ST performed by the CAS.
 370     lock->set_displaced_header(mark);
 371     if (mark == obj()->cas_set_mark((markOop) lock, mark)) {
 372       return;
 373     }
 374     // Fall through to inflate() ...
 375   } else if (mark->has_locker() &&
 376              THREAD->is_lock_owned((address)mark->locker())) {
 377     assert(lock != mark->locker(), "must not re-lock the same lock");
 378     assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock");
 379     lock->set_displaced_header(NULL);
 380     return;
 381   }
 382 
 383   // The object header will never be displaced to this lock,
 384   // so it does not matter what the value is, except that it
 385   // must be non-zero to avoid looking like a re-entrant lock,
 386   // and must not look locked either.
 387   lock->set_displaced_header(markOopDesc::unused_mark());
 388   ObjectMonitorHandle omh;
 389   inflate(&omh, THREAD, obj(), inflate_cause_monitor_enter);
 390   omh.om_ptr()->enter(THREAD);
 391 }
 392 
 393 // This routine is used to handle interpreter/compiler slow case
 394 // We don't need to use fast path here, because it must have
 395 // failed in the interpreter/compiler code. Simply use the heavy
 396 // weight monitor should be ok, unless someone find otherwise.
 397 void ObjectSynchronizer::slow_exit(oop object, BasicLock* lock, TRAPS) {
 398   fast_exit(object, lock, THREAD);
 399 }
 400 
 401 // -----------------------------------------------------------------------------
 402 // Class Loader  support to workaround deadlocks on the class loader lock objects
 403 // Also used by GC
 404 // complete_exit()/reenter() are used to wait on a nested lock
 405 // i.e. to give up an outer lock completely and then re-enter
 406 // Used when holding nested locks - lock acquisition order: lock1 then lock2
 407 //  1) complete_exit lock1 - saving recursion count
 408 //  2) wait on lock2
 409 //  3) when notified on lock2, unlock lock2
 410 //  4) reenter lock1 with original recursion count
 411 //  5) lock lock2
 412 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 413 intptr_t ObjectSynchronizer::complete_exit(Handle obj, TRAPS) {
 414   if (UseBiasedLocking) {
 415     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 416     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 417   }
 418 
 419   ObjectMonitorHandle omh;
 420   inflate(&omh, THREAD, obj(), inflate_cause_vm_internal);
 421   intptr_t ret_code = omh.om_ptr()->complete_exit(THREAD);
 422   return ret_code;
 423 }
 424 
 425 // NOTE: must use heavy weight monitor to handle complete_exit/reenter()
 426 void ObjectSynchronizer::reenter(Handle obj, intptr_t recursion, TRAPS) {
 427   if (UseBiasedLocking) {
 428     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 429     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 430   }
 431 
 432   ObjectMonitorHandle omh;
 433   inflate(&omh, THREAD, obj(), inflate_cause_vm_internal);
 434   omh.om_ptr()->reenter(recursion, THREAD);
 435 }
 436 // -----------------------------------------------------------------------------
 437 // JNI locks on java objects
 438 // NOTE: must use heavy weight monitor to handle jni monitor enter
 439 void ObjectSynchronizer::jni_enter(Handle obj, TRAPS) {
 440   // the current locking is from JNI instead of Java code
 441   if (UseBiasedLocking) {
 442     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 443     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 444   }
 445   THREAD->set_current_pending_monitor_is_from_java(false);
 446   ObjectMonitorHandle omh;
 447   inflate(&omh, THREAD, obj(), inflate_cause_jni_enter);
 448   omh.om_ptr()->enter(THREAD);
 449   THREAD->set_current_pending_monitor_is_from_java(true);
 450 }
 451 
 452 // NOTE: must use heavy weight monitor to handle jni monitor exit
 453 void ObjectSynchronizer::jni_exit(oop obj, Thread* THREAD) {
 454   if (UseBiasedLocking) {
 455     Handle h_obj(THREAD, obj);
 456     BiasedLocking::revoke_and_rebias(h_obj, false, THREAD);
 457     obj = h_obj();
 458   }
 459   assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 460 
 461   ObjectMonitorHandle omh;
 462   inflate(&omh, THREAD, obj, inflate_cause_jni_exit);
 463   ObjectMonitor * monitor = omh.om_ptr();
 464   // If this thread has locked the object, exit the monitor.  Note:  can't use
 465   // monitor->check(CHECK); must exit even if an exception is pending.
 466   if (monitor->check(THREAD)) {
 467     monitor->exit(true, THREAD);
 468   }
 469 }
 470 
 471 // -----------------------------------------------------------------------------
 472 // Internal VM locks on java objects
 473 // standard constructor, allows locking failures
 474 ObjectLocker::ObjectLocker(Handle obj, Thread* thread, bool doLock) {
 475   _dolock = doLock;
 476   _thread = thread;
 477   debug_only(if (StrictSafepointChecks) _thread->check_for_valid_safepoint_state(false);)
 478   _obj = obj;
 479 
 480   if (_dolock) {
 481     ObjectSynchronizer::fast_enter(_obj, &_lock, false, _thread);
 482   }
 483 }
 484 
 485 ObjectLocker::~ObjectLocker() {
 486   if (_dolock) {
 487     ObjectSynchronizer::fast_exit(_obj(), &_lock, _thread);
 488   }
 489 }
 490 
 491 
 492 // -----------------------------------------------------------------------------
 493 //  Wait/Notify/NotifyAll
 494 // NOTE: must use heavy weight monitor to handle wait()
 495 int ObjectSynchronizer::wait(Handle obj, jlong millis, TRAPS) {
 496   if (UseBiasedLocking) {
 497     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 498     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 499   }
 500   if (millis < 0) {
 501     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 502   }
 503   ObjectMonitorHandle omh;
 504   inflate(&omh, THREAD, obj(), inflate_cause_wait);
 505   ObjectMonitor * monitor = omh.om_ptr();
 506 
 507   DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis);
 508   monitor->wait(millis, true, THREAD);
 509 
 510   // This dummy call is in place to get around dtrace bug 6254741.  Once
 511   // that's fixed we can uncomment the following line, remove the call
 512   // and change this function back into a "void" func.
 513   // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD);
 514   int ret_code = dtrace_waited_probe(monitor, obj, THREAD);
 515   return ret_code;
 516 }
 517 
 518 void ObjectSynchronizer::waitUninterruptibly(Handle obj, jlong millis, TRAPS) {
 519   if (UseBiasedLocking) {
 520     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 521     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 522   }
 523   if (millis < 0) {
 524     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
 525   }
 526   ObjectMonitorHandle omh;
 527   inflate(&omh, THREAD, obj(), inflate_cause_wait);
 528   omh.om_ptr()->wait(millis, false, THREAD);
 529 }
 530 
 531 void ObjectSynchronizer::notify(Handle obj, TRAPS) {
 532   if (UseBiasedLocking) {
 533     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 534     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 535   }
 536 
 537   markOop mark = obj->mark();
 538   if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
 539     return;
 540   }
 541   ObjectMonitorHandle omh;
 542   inflate(&omh, THREAD, obj(), inflate_cause_notify);
 543   omh.om_ptr()->notify(THREAD);
 544 }
 545 
 546 // NOTE: see comment of notify()
 547 void ObjectSynchronizer::notifyall(Handle obj, TRAPS) {
 548   if (UseBiasedLocking) {
 549     BiasedLocking::revoke_and_rebias(obj, false, THREAD);
 550     assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 551   }
 552 
 553   markOop mark = obj->mark();
 554   if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) {
 555     return;
 556   }
 557   ObjectMonitorHandle omh;
 558   inflate(&omh, THREAD, obj(), inflate_cause_notify);
 559   omh.om_ptr()->notifyAll(THREAD);
 560 }
 561 
 562 // -----------------------------------------------------------------------------
 563 // Hash Code handling
 564 //
 565 // Performance concern:
 566 // OrderAccess::storestore() calls release() which at one time stored 0
 567 // into the global volatile OrderAccess::dummy variable. This store was
 568 // unnecessary for correctness. Many threads storing into a common location
 569 // causes considerable cache migration or "sloshing" on large SMP systems.
 570 // As such, I avoided using OrderAccess::storestore(). In some cases
 571 // OrderAccess::fence() -- which incurs local latency on the executing
 572 // processor -- is a better choice as it scales on SMP systems.
 573 //
 574 // See http://blogs.oracle.com/dave/entry/biased_locking_in_hotspot for
 575 // a discussion of coherency costs. Note that all our current reference
 576 // platforms provide strong ST-ST order, so the issue is moot on IA32,
 577 // x64, and SPARC.
 578 //
 579 // As a general policy we use "volatile" to control compiler-based reordering


 733       Handle hobj(Self, obj);
 734       // Relaxing assertion for bug 6320749.
 735       assert(Universe::verify_in_progress() ||
 736              !SafepointSynchronize::is_at_safepoint(),
 737              "biases should not be seen by VM thread here");
 738       BiasedLocking::revoke_and_rebias(hobj, false, JavaThread::current());
 739       obj = hobj();
 740       assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 741     }
 742   }
 743 
 744   // hashCode() is a heap mutator ...
 745   // Relaxing assertion for bug 6320749.
 746   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 747          !SafepointSynchronize::is_at_safepoint(), "invariant");
 748   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 749          Self->is_Java_thread() , "invariant");
 750   assert(Universe::verify_in_progress() || DumpSharedSpaces ||
 751          ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant");
 752 
 753   while (true) {
 754     ObjectMonitor* monitor = NULL;
 755     markOop temp, test;
 756     intptr_t hash;
 757     markOop mark = ReadStableMark(obj);
 758 
 759     // object should remain ineligible for biased locking
 760     assert(!mark->has_bias_pattern(), "invariant");
 761 
 762     if (mark->is_neutral()) {
 763       hash = mark->hash();              // this is a normal header
 764       if (hash != 0) {                  // if it has hash, just return it
 765         return hash;
 766       }
 767       hash = get_next_hash(Self, obj);  // allocate a new hash code
 768       temp = mark->copy_set_hash(hash); // merge the hash code into header
 769       // use (machine word version) atomic operation to install the hash
 770       test = obj->cas_set_mark(temp, mark);
 771       if (test == mark) {
 772         return hash;
 773       }
 774       // If atomic operation failed, we must inflate the header
 775       // into heavy weight monitor. We could add more code here
 776       // for fast path, but it does not worth the complexity.
 777     } else if (mark->has_monitor()) {
 778       ObjectMonitorHandle omh;
 779       if (!omh.save_om_ptr(obj, mark)) {
 780         // Lost a race with async deflation so try again.
 781         assert(AsyncDeflateIdleMonitors, "sanity check");
 782         continue;
 783       }
 784       monitor = omh.om_ptr();
 785       temp = monitor->header();
 786       assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 787       hash = temp->hash();
 788       if (hash != 0) {
 789         return hash;
 790       }
 791       // Skip to the following code to reduce code size
 792     } else if (Self->is_lock_owned((address)mark->locker())) {
 793       temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned
 794       assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 795       hash = temp->hash();              // by current thread, check if the displaced
 796       if (hash != 0) {                  // header contains hash code
 797         return hash;
 798       }
 799       // WARNING:
 800       // The displaced header in the BasicLock on a thread's stack
 801       // is strictly immutable. It CANNOT be changed in ANY cases.
 802       // So we have to inflate the stack lock into an ObjectMonitor
 803       // even if the current thread owns the lock. The BasicLock on
 804       // a thread's stack can be asynchronously read by other threads
 805       // during an inflate() call so any change to that stack memory
 806       // may not propagate to other threads correctly.
 807     }
 808 
 809     // Inflate the monitor to set hash code
 810     ObjectMonitorHandle omh;
 811     inflate(&omh, Self, obj, inflate_cause_hash_code);
 812     monitor = omh.om_ptr();
 813     // Load displaced header and check it has hash code
 814     mark = monitor->header();
 815     assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark));
 816     hash = mark->hash();
 817     if (hash == 0) {
 818       hash = get_next_hash(Self, obj);
 819       temp = mark->copy_set_hash(hash); // merge hash code into header
 820       assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp));
 821       test = Atomic::cmpxchg(temp, monitor->header_addr(), mark);
 822       if (test != mark) {
 823         // The only non-deflation update to the ObjectMonitor's
 824         // header/dmw field is to merge in the hash code. If someone
 825         // adds a new usage of the header/dmw field, please update
 826         // this code.
 827         // ObjectMonitor::install_displaced_markword_in_object()
 828         // does mark the header/dmw field as part of async deflation,
 829         // but that protocol cannot happen now due to the
 830         // ObjectMonitorHandle above.
 831         hash = test->hash();
 832         assert(test->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(test));
 833         assert(hash != 0, "Trivial unexpected object/monitor header usage.");
 834       }
 835     }
 836     // We finally get the hash
 837     return hash;
 838   }
 839 }
 840 
 841 // Deprecated -- use FastHashCode() instead.
 842 
 843 intptr_t ObjectSynchronizer::identity_hash_value_for(Handle obj) {
 844   return FastHashCode(Thread::current(), obj());
 845 }
 846 
 847 
 848 bool ObjectSynchronizer::current_thread_holds_lock(JavaThread* thread,
 849                                                    Handle h_obj) {
 850   if (UseBiasedLocking) {
 851     BiasedLocking::revoke_and_rebias(h_obj, false, thread);
 852     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 853   }
 854 
 855   assert(thread == JavaThread::current(), "Can only be called on current thread");
 856   oop obj = h_obj();
 857 
 858   while (true) {
 859     markOop mark = ReadStableMark(obj);
 860 
 861     // Uncontended case, header points to stack
 862     if (mark->has_locker()) {
 863       return thread->is_lock_owned((address)mark->locker());
 864     }
 865     // Contended case, header points to ObjectMonitor (tagged pointer)
 866     if (mark->has_monitor()) {
 867       ObjectMonitorHandle omh;
 868       if (!omh.save_om_ptr(obj, mark)) {
 869         // Lost a race with async deflation so try again.
 870         assert(AsyncDeflateIdleMonitors, "sanity check");
 871         continue;
 872       }
 873       bool ret_code = omh.om_ptr()->is_entered(thread) != 0;
 874       return ret_code;
 875     }
 876     // Unlocked case, header in place
 877     assert(mark->is_neutral(), "sanity check");
 878     return false;
 879   }
 880 }
 881 
 882 // Be aware of this method could revoke bias of the lock object.
 883 // This method queries the ownership of the lock handle specified by 'h_obj'.
 884 // If the current thread owns the lock, it returns owner_self. If no
 885 // thread owns the lock, it returns owner_none. Otherwise, it will return
 886 // owner_other.
 887 ObjectSynchronizer::LockOwnership ObjectSynchronizer::query_lock_ownership
 888 (JavaThread *self, Handle h_obj) {
 889   // The caller must beware this method can revoke bias, and
 890   // revocation can result in a safepoint.
 891   assert(!SafepointSynchronize::is_at_safepoint(), "invariant");
 892   assert(self->thread_state() != _thread_blocked, "invariant");
 893 
 894   // Possible mark states: neutral, biased, stack-locked, inflated
 895 
 896   if (UseBiasedLocking && h_obj()->mark()->has_bias_pattern()) {
 897     // CASE: biased
 898     BiasedLocking::revoke_and_rebias(h_obj, false, self);
 899     assert(!h_obj->mark()->has_bias_pattern(),
 900            "biases should be revoked by now");
 901   }
 902 
 903   assert(self == JavaThread::current(), "Can only be called on current thread");
 904   oop obj = h_obj();
 905 
 906   while (true) {
 907     markOop mark = ReadStableMark(obj);
 908 
 909     // CASE: stack-locked.  Mark points to a BasicLock on the owner's stack.
 910     if (mark->has_locker()) {
 911       return self->is_lock_owned((address)mark->locker()) ?
 912         owner_self : owner_other;
 913     }
 914 
 915     // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor.
 916     // The Object:ObjectMonitor relationship is stable as long as we're
 917     // not at a safepoint and AsyncDeflateIdleMonitors is false.
 918     if (mark->has_monitor()) {
 919       ObjectMonitorHandle omh;
 920       if (!omh.save_om_ptr(obj, mark)) {
 921         // Lost a race with async deflation so try again.
 922         assert(AsyncDeflateIdleMonitors, "sanity check");
 923         continue;
 924       }
 925       ObjectMonitor * monitor = omh.om_ptr();
 926       void * owner = monitor->_owner;
 927       if (owner == NULL) return owner_none;
 928       return (owner == self ||
 929               self->is_lock_owned((address)owner)) ? owner_self : owner_other;
 930     }
 931 
 932     // CASE: neutral
 933     assert(mark->is_neutral(), "sanity check");
 934     return owner_none;           // it's unlocked
 935   }
 936 }
 937 
 938 // FIXME: jvmti should call this
 939 JavaThread* ObjectSynchronizer::get_lock_owner(ThreadsList * t_list, Handle h_obj) {
 940   if (UseBiasedLocking) {
 941     if (SafepointSynchronize::is_at_safepoint()) {
 942       BiasedLocking::revoke_at_safepoint(h_obj);
 943     } else {
 944       BiasedLocking::revoke_and_rebias(h_obj, false, JavaThread::current());
 945     }
 946     assert(!h_obj->mark()->has_bias_pattern(), "biases should be revoked by now");
 947   }
 948 
 949   oop obj = h_obj();

 950 
 951   while (true) {
 952     address owner = NULL;
 953     markOop mark = ReadStableMark(obj);
 954 
 955     // Uncontended case, header points to stack
 956     if (mark->has_locker()) {
 957       owner = (address) mark->locker();
 958     }
 959 
 960     // Contended case, header points to ObjectMonitor (tagged pointer)
 961     else if (mark->has_monitor()) {
 962       ObjectMonitorHandle omh;
 963       if (!omh.save_om_ptr(obj, mark)) {
 964         // Lost a race with async deflation so try again.
 965         assert(AsyncDeflateIdleMonitors, "sanity check");
 966         continue;
 967       }
 968       ObjectMonitor* monitor = omh.om_ptr();
 969       assert(monitor != NULL, "monitor should be non-null");
 970       owner = (address) monitor->owner();
 971     }
 972 
 973     if (owner != NULL) {
 974       // owning_thread_from_monitor_owner() may also return NULL here
 975       return Threads::owning_thread_from_monitor_owner(t_list, owner);
 976     }
 977 
 978     // Unlocked case, header in place
 979     // Cannot have assertion since this object may have been
 980     // locked by another thread when reaching here.
 981     // assert(mark->is_neutral(), "sanity check");
 982 
 983     return NULL;
 984   }
 985 }
 986 
 987 // Visitors ...
 988 
 989 void ObjectSynchronizer::monitors_iterate(MonitorClosure* closure) {
 990   PaddedEnd<ObjectMonitor> * block = OrderAccess::load_acquire(&gBlockList);
 991   while (block != NULL) {
 992     assert(block->object() == CHAINMARKER, "must be a block header");
 993     for (int i = _BLOCKSIZE - 1; i > 0; i--) {
 994       ObjectMonitor* mid = (ObjectMonitor *)(block + i);
 995       if (mid->is_active()) {
 996         ObjectMonitorHandle omh(mid);
 997 
 998         if (mid->object() == NULL ||
 999             (AsyncDeflateIdleMonitors && mid->_owner == DEFLATER_MARKER)) {
1000           // Only process with closure if the object is set.
1001           // For async deflation, race here if monitor is not owned!
1002           // The above ref_count bump (in ObjectMonitorHandle ctr)
1003           // will cause subsequent async deflation to skip it.
1004           // However, previous or concurrent async deflation is a race.
1005           continue;
1006         }
1007         closure->do_monitor(mid);
1008       }
1009     }
1010     block = (PaddedEnd<ObjectMonitor> *)block->FreeNext;
1011   }
1012 }
1013 
1014 // Get the next block in the block list.
1015 static inline PaddedEnd<ObjectMonitor>* next(PaddedEnd<ObjectMonitor>* block) {
1016   assert(block->object() == CHAINMARKER, "must be a block header");
1017   block = (PaddedEnd<ObjectMonitor>*) block->FreeNext;
1018   assert(block == NULL || block->object() == CHAINMARKER, "must be a block header");
1019   return block;
1020 }
1021 
1022 static bool monitors_used_above_threshold() {
1023   if (gMonitorPopulation == 0) {
1024     return false;
1025   }
1026   if (MonitorUsedDeflationThreshold > 0) {
1027     int monitors_used = gMonitorPopulation - gMonitorFreeCount;
1028     int monitor_usage = (monitors_used * 100LL) / gMonitorPopulation;
1029     return monitor_usage > MonitorUsedDeflationThreshold;
1030   }
1031   return false;
1032 }
1033 
1034 // Returns true if MonitorBound is set (> 0) and if the specified
1035 // cnt is > MonitorBound. Otherwise returns false.
1036 static bool is_MonitorBound_exceeded(const int cnt) {
1037   const int mx = MonitorBound;
1038   return mx > 0 && cnt > mx;
1039 }
1040 
1041 bool ObjectSynchronizer::is_async_deflation_needed() {
1042   if (!AsyncDeflateIdleMonitors) {
1043     return false;
1044   }
1045   if (is_async_deflation_requested()) {
1046     // Async deflation request.
1047     return true;
1048   }
1049   if (AsyncDeflationInterval > 0 &&
1050       time_since_last_async_deflation_ms() > AsyncDeflationInterval &&
1051       monitors_used_above_threshold()) {
1052     // It's been longer than our specified deflate interval and there
1053     // are too many monitors in use. We don't deflate more frequently
1054     // than AsyncDeflationInterval (unless is_async_deflation_requested)
1055     // in order to not swamp the ServiceThread.
1056     _last_async_deflation_time_ns = os::javaTimeNanos();
1057     return true;
1058   }
1059   if (is_MonitorBound_exceeded(gMonitorPopulation - gMonitorFreeCount)) {
1060     // Not enough ObjectMonitors on the global free list.
1061     return true;
1062   }
1063   return false;
1064 }
1065 
1066 bool ObjectSynchronizer::is_safepoint_deflation_needed() {
1067   if (!AsyncDeflateIdleMonitors) {
1068     if (monitors_used_above_threshold()) {
1069       // Too many monitors in use.
1070       return true;
1071     }
1072     return false;
1073   }
1074   if (is_special_deflation_requested()) {
1075     // For AsyncDeflateIdleMonitors only do a safepoint deflation
1076     // if there is a special deflation request.
1077     return true;
1078   }
1079   return false;
1080 }
1081 
1082 jlong ObjectSynchronizer::time_since_last_async_deflation_ms() {
1083   return (os::javaTimeNanos() - _last_async_deflation_time_ns) / (NANOUNITS / MILLIUNITS);
1084 }
1085 
1086 void ObjectSynchronizer::oops_do(OopClosure* f) {
1087   // We only scan the global used list here (for moribund threads), and
1088   // the thread-local monitors in Thread::oops_do().
1089   global_used_oops_do(f);
1090 }
1091 
1092 void ObjectSynchronizer::global_used_oops_do(OopClosure* f) {
1093   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1094   list_oops_do(gOmInUseList, f);
1095 }
1096 
1097 void ObjectSynchronizer::thread_local_used_oops_do(Thread* thread, OopClosure* f) {
1098   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1099   list_oops_do(thread->omInUseList, f);
1100 }
1101 
1102 void ObjectSynchronizer::list_oops_do(ObjectMonitor* list, OopClosure* f) {
1103   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1104   ObjectMonitor* mid;
1105   for (mid = list; mid != NULL; mid = mid->FreeNext) {


1115 // -----------------------
1116 // Inflation unlinks monitors from the global gFreeList and
1117 // associates them with objects.  Deflation -- which occurs at
1118 // STW-time -- disassociates idle monitors from objects.  Such
1119 // scavenged monitors are returned to the gFreeList.
1120 //
1121 // The global list is protected by gListLock.  All the critical sections
1122 // are short and operate in constant-time.
1123 //
1124 // ObjectMonitors reside in type-stable memory (TSM) and are immortal.
1125 //
1126 // Lifecycle:
1127 // --   unassigned and on the global free list
1128 // --   unassigned and on a thread's private omFreeList
1129 // --   assigned to an object.  The object is inflated and the mark refers
1130 //      to the objectmonitor.
1131 
1132 
1133 // Constraining monitor pool growth via MonitorBound ...
1134 //
1135 // If MonitorBound is not set (<= 0), MonitorBound checks are disabled.
1136 //
1137 // When safepoint deflation is being used (!AsyncDeflateIdleMonitors):
1138 // The monitor pool is grow-only.  We scavenge at STW safepoint-time, but the
1139 // the rate of scavenging is driven primarily by GC.  As such,  we can find
1140 // an inordinate number of monitors in circulation.
1141 // To avoid that scenario we can artificially induce a STW safepoint
1142 // if the pool appears to be growing past some reasonable bound.
1143 // Generally we favor time in space-time tradeoffs, but as there's no
1144 // natural back-pressure on the # of extant monitors we need to impose some
1145 // type of limit.  Beware that if MonitorBound is set to too low a value
1146 // we could just loop. In addition, if MonitorBound is set to a low value
1147 // we'll incur more safepoints, which are harmful to performance.
1148 // See also: GuaranteedSafepointInterval
1149 //
1150 // The current implementation uses asynchronous VM operations.
1151 //
1152 // When safepoint deflation is being used and MonitorBound is set, the
1153 // boundry applies to (gMonitorPopulation - gMonitorFreeCount), i.e.,
1154 // if there are not enough ObjectMonitors on the global free list, then
1155 // a safepoint deflation is induced. Picking a good MonitorBound value
1156 // is non-trivial.
1157 //
1158 // When async deflation is being used:
1159 // The monitor pool is still grow-only. Async deflation is requested
1160 // by a safepoint's cleanup phase or by the ServiceThread at periodic
1161 // intervals when is_async_deflation_needed() returns true. In
1162 // addition to other policies that are checked, if there are not
1163 // enough ObjectMonitors on the global free list, then
1164 // is_async_deflation_needed() will return true. The ServiceThread
1165 // calls deflate_global_idle_monitors_using_JT() and also sets the
1166 // per-thread omShouldDeflateIdleMonitors flag as needed.
1167 
1168 static void InduceScavenge(Thread * Self, const char * Whence) {
1169   assert(!AsyncDeflateIdleMonitors, "is not used by async deflation");
1170 
1171   // Induce STW safepoint to trim monitors
1172   // Ultimately, this results in a call to deflate_idle_monitors() in the near future.
1173   // More precisely, trigger an asynchronous STW safepoint as the number
1174   // of active monitors passes the specified threshold.
1175   // TODO: assert thread state is reasonable
1176 
1177   if (ForceMonitorScavenge == 0 && Atomic::xchg (1, &ForceMonitorScavenge) == 0) {
1178     // Induce a 'null' safepoint to scavenge monitors
1179     // Must VM_Operation instance be heap allocated as the op will be enqueue and posted
1180     // to the VMthread and have a lifespan longer than that of this activation record.
1181     // The VMThread will delete the op when completed.
1182     VMThread::execute(new VM_ScavengeMonitors());
1183   }
1184 }
1185 
1186 ObjectMonitor* ObjectSynchronizer::omAlloc(Thread * Self,
1187                                            const InflateCause cause) {
1188   // A large MAXPRIVATE value reduces both list lock contention
1189   // and list coherency traffic, but also tends to increase the
1190   // number of objectMonitors in circulation as well as the STW
1191   // scavenge costs.  As usual, we lean toward time in space-time
1192   // tradeoffs.
1193   const int MAXPRIVATE = 1024;
1194 
1195   if (AsyncDeflateIdleMonitors) {
1196     JavaThread * jt = (JavaThread *)Self;
1197     if (jt->omShouldDeflateIdleMonitors && jt->omInUseCount > 0 &&
1198         cause != inflate_cause_vm_internal) {
1199       // Deflate any per-thread idle monitors for this JavaThread if
1200       // this is not an internal inflation; internal inflations can
1201       // occur in places where it is not safe to pause for a safepoint.
1202       // Clean up your own mess (Gibbs Rule 45). Otherwise, skip this
1203       // deflation. deflate_global_idle_monitors_using_JT() is called
1204       // by the ServiceThread. Per-thread async deflation is triggered
1205       // by the ServiceThread via omShouldDeflateIdleMonitors.
1206       debug_only(jt->check_for_valid_safepoint_state(false);)
1207       ObjectSynchronizer::deflate_per_thread_idle_monitors_using_JT();
1208     }
1209   }
1210 
1211   stringStream ss;
1212   for (;;) {
1213     ObjectMonitor * m;
1214 
1215     // 1: try to allocate from the thread's local omFreeList.
1216     // Threads will attempt to allocate first from their local list, then
1217     // from the global list, and only after those attempts fail will the thread
1218     // attempt to instantiate new monitors.   Thread-local free lists take
1219     // heat off the gListLock and improve allocation latency, as well as reducing
1220     // coherency traffic on the shared global list.
1221     m = Self->omFreeList;
1222     if (m != NULL) {
1223       Self->omFreeList = m->FreeNext;
1224       Self->omFreeCount--;
1225       guarantee(m->object() == NULL, "invariant");
1226       m->set_allocation_state(ObjectMonitor::New);
1227       m->FreeNext = Self->omInUseList;
1228       Self->omInUseList = m;
1229       Self->omInUseCount++;
1230       return m;
1231     }
1232 
1233     // 2: try to allocate from the global gFreeList
1234     // CONSIDER: use muxTry() instead of muxAcquire().
1235     // If the muxTry() fails then drop immediately into case 3.
1236     // If we're using thread-local free lists then try
1237     // to reprovision the caller's free list.
1238     if (gFreeList != NULL) {
1239       // Reprovision the thread's omFreeList.
1240       // Use bulk transfers to reduce the allocation rate and heat
1241       // on various locks.
1242       Thread::muxAcquire(&gListLock, "omAlloc(1)");
1243       for (int i = Self->omFreeProvision; --i >= 0 && gFreeList != NULL;) {
1244         gMonitorFreeCount--;
1245         ObjectMonitor * take = gFreeList;
1246         gFreeList = take->FreeNext;
1247         guarantee(take->object() == NULL, "invariant");
1248         if (AsyncDeflateIdleMonitors) {
1249           // We allowed 3 field values to linger during async deflation.
1250           // We clear header and restore ref_count here, but we leave
1251           // owner == DEFLATER_MARKER so the simple C2 ObjectMonitor
1252           // enter optimization can no longer race with async deflation
1253           // and reuse.
1254           take->_header = NULL;
1255           if (take->ref_count() < 0) {
1256             // Add back max_jint to restore the ref_count field to its
1257             // proper value.
1258             Atomic::add(max_jint, &take->_ref_count);
1259 
1260             assert(take->ref_count() >= 0, "must not be negative: ref_count=%d",
1261                    take->ref_count());
1262           }
1263         }
1264         take->Recycle();
1265         assert(take->is_free(), "invariant");
1266         omRelease(Self, take, false);
1267       }
1268       Thread::muxRelease(&gListLock);
1269       Self->omFreeProvision += 1 + (Self->omFreeProvision/2);
1270       if (Self->omFreeProvision > MAXPRIVATE) Self->omFreeProvision = MAXPRIVATE;
1271 
1272       if (!AsyncDeflateIdleMonitors &&
1273           is_MonitorBound_exceeded(gMonitorPopulation - gMonitorFreeCount)) {
1274         // Not enough ObjectMonitors on the global free list.
1275         // We can't safely induce a STW safepoint from omAlloc() as our thread
1276         // state may not be appropriate for such activities and callers may hold
1277         // naked oops, so instead we defer the action.
1278         InduceScavenge(Self, "omAlloc");
1279       }
1280       continue;
1281     }
1282 
1283     // 3: allocate a block of new ObjectMonitors
1284     // Both the local and global free lists are empty -- resort to malloc().
1285     // In the current implementation objectMonitors are TSM - immortal.
1286     // Ideally, we'd write "new ObjectMonitor[_BLOCKSIZE], but we want
1287     // each ObjectMonitor to start at the beginning of a cache line,
1288     // so we use align_up().
1289     // A better solution would be to use C++ placement-new.
1290     // BEWARE: As it stands currently, we don't run the ctors!
1291     assert(_BLOCKSIZE > 1, "invariant");
1292     size_t neededsize = sizeof(PaddedEnd<ObjectMonitor>) * _BLOCKSIZE;
1293     PaddedEnd<ObjectMonitor> * temp;
1294     size_t aligned_size = neededsize + (DEFAULT_CACHE_LINE_SIZE - 1);


1299 
1300     // NOTE: (almost) no way to recover if allocation failed.
1301     // We might be able to induce a STW safepoint and scavenge enough
1302     // objectMonitors to permit progress.
1303     if (temp == NULL) {
1304       vm_exit_out_of_memory(neededsize, OOM_MALLOC_ERROR,
1305                             "Allocate ObjectMonitors");
1306     }
1307     (void)memset((void *) temp, 0, neededsize);
1308 
1309     // Format the block.
1310     // initialize the linked list, each monitor points to its next
1311     // forming the single linked free list, the very first monitor
1312     // will points to next block, which forms the block list.
1313     // The trick of using the 1st element in the block as gBlockList
1314     // linkage should be reconsidered.  A better implementation would
1315     // look like: class Block { Block * next; int N; ObjectMonitor Body [N] ; }
1316 
1317     for (int i = 1; i < _BLOCKSIZE; i++) {
1318       temp[i].FreeNext = (ObjectMonitor *)&temp[i+1];
1319       assert(temp[i].is_free(), "invariant");
1320     }
1321 
1322     // terminate the last monitor as the end of list
1323     temp[_BLOCKSIZE - 1].FreeNext = NULL;
1324 
1325     // Element [0] is reserved for global list linkage
1326     temp[0].set_object(CHAINMARKER);
1327 
1328     // Consider carving out this thread's current request from the
1329     // block in hand.  This avoids some lock traffic and redundant
1330     // list activity.
1331 
1332     // Acquire the gListLock to manipulate gBlockList and gFreeList.
1333     // An Oyama-Taura-Yonezawa scheme might be more efficient.
1334     Thread::muxAcquire(&gListLock, "omAlloc(2)");
1335     gMonitorPopulation += _BLOCKSIZE-1;
1336     gMonitorFreeCount += _BLOCKSIZE-1;
1337 
1338     // Add the new block to the list of extant blocks (gBlockList).
1339     // The very first objectMonitor in a block is reserved and dedicated.


1342     // There are lock-free uses of gBlockList so make sure that
1343     // the previous stores happen before we update gBlockList.
1344     OrderAccess::release_store(&gBlockList, temp);
1345 
1346     // Add the new string of objectMonitors to the global free list
1347     temp[_BLOCKSIZE - 1].FreeNext = gFreeList;
1348     gFreeList = temp + 1;
1349     Thread::muxRelease(&gListLock);
1350   }
1351 }
1352 
1353 // Place "m" on the caller's private per-thread omFreeList.
1354 // In practice there's no need to clamp or limit the number of
1355 // monitors on a thread's omFreeList as the only time we'll call
1356 // omRelease is to return a monitor to the free list after a CAS
1357 // attempt failed.  This doesn't allow unbounded #s of monitors to
1358 // accumulate on a thread's free list.
1359 //
1360 // Key constraint: all ObjectMonitors on a thread's free list and the global
1361 // free list must have their object field set to null. This prevents the
1362 // scavenger -- deflate_monitor_list() or deflate_monitor_list_using_JT()
1363 // -- from reclaiming them while we are trying to release them.
1364 
1365 void ObjectSynchronizer::omRelease(Thread * Self, ObjectMonitor * m,
1366                                    bool fromPerThreadAlloc) {
1367   guarantee(m->header() == NULL, "invariant");
1368   guarantee(m->object() == NULL, "invariant");
1369   stringStream ss;
1370   guarantee((m->is_busy() | m->_recursions) == 0, "freeing in-use monitor: "
1371             "%s, recursions=" INTPTR_FORMAT, m->is_busy_to_string(&ss),
1372             m->_recursions);
1373   m->set_allocation_state(ObjectMonitor::Free);
1374   // Remove from omInUseList
1375   if (fromPerThreadAlloc) {
1376     ObjectMonitor* cur_mid_in_use = NULL;
1377     bool extracted = false;
1378     for (ObjectMonitor* mid = Self->omInUseList; mid != NULL; cur_mid_in_use = mid, mid = mid->FreeNext) {
1379       if (m == mid) {
1380         // extract from per-thread in-use list
1381         if (mid == Self->omInUseList) {
1382           Self->omInUseList = mid->FreeNext;
1383         } else if (cur_mid_in_use != NULL) {
1384           cur_mid_in_use->FreeNext = mid->FreeNext; // maintain the current thread in-use list
1385         }
1386         extracted = true;
1387         Self->omInUseCount--;
1388         break;
1389       }
1390     }
1391     assert(extracted, "Should have extracted from in-use list");
1392   }
1393 
1394   // FreeNext is used for both omInUseList and omFreeList, so clear old before setting new
1395   m->FreeNext = Self->omFreeList;
1396   guarantee(m->is_free(), "invariant");
1397   Self->omFreeList = m;
1398   Self->omFreeCount++;
1399 }
1400 
1401 // Return the monitors of a moribund thread's local free list to
1402 // the global free list.  Typically a thread calls omFlush() when
1403 // it's dying.  We could also consider having the VM thread steal
1404 // monitors from threads that have not run java code over a few
1405 // consecutive STW safepoints.  Relatedly, we might decay
1406 // omFreeProvision at STW safepoints.
1407 //
1408 // Also return the monitors of a moribund thread's omInUseList to
1409 // a global gOmInUseList under the global list lock so these
1410 // will continue to be scanned.
1411 //
1412 // We currently call omFlush() from Threads::remove() _before the thread
1413 // has been excised from the thread list and is no longer a mutator.
1414 // This means that omFlush() cannot run concurrently with a safepoint and
1415 // interleave with the deflate_idle_monitors scavenge operator. In particular,
1416 // this ensures that the thread's monitors are scanned by a GC safepoint,
1417 // either via Thread::oops_do() (if safepoint happens before omFlush()) or via
1418 // ObjectSynchronizer::oops_do() (if it happens after omFlush() and the thread's
1419 // monitors have been transferred to the global in-use list).
1420 //
1421 // With AsyncDeflateIdleMonitors, deflate_global_idle_monitors_using_JT()
1422 // and deflate_per_thread_idle_monitors_using_JT() (in another thread) can
1423 // run at the same time as omFlush() so we have to be careful.
1424 
1425 void ObjectSynchronizer::omFlush(Thread * Self) {
1426   ObjectMonitor * list = Self->omFreeList;  // Null-terminated SLL
1427   ObjectMonitor * tail = NULL;
1428   int tally = 0;
1429   if (list != NULL) {
1430     ObjectMonitor * s;
1431     // The thread is going away. Set 'tail' to the last per-thread free
1432     // monitor which will be linked to gFreeList below under the gListLock.
1433     stringStream ss;
1434     for (s = list; s != NULL; s = s->FreeNext) {
1435       tally++;
1436       tail = s;
1437       guarantee(s->object() == NULL, "invariant");
1438       guarantee(!s->is_busy(), "must be !is_busy: %s", s->is_busy_to_string(&ss));
1439     }
1440     guarantee(tail != NULL, "invariant");
1441     ADIM_guarantee(Self->omFreeCount == tally, "free-count off");
1442     Self->omFreeList = NULL;
1443     Self->omFreeCount = 0;
1444   }
1445 
1446   ObjectMonitor * inUseList = Self->omInUseList;
1447   ObjectMonitor * inUseTail = NULL;
1448   int inUseTally = 0;
1449   if (inUseList != NULL) {
1450     ObjectMonitor *cur_om;
1451     // The thread is going away, however the omInUseList inflated
1452     // monitors may still be in-use by other threads.
1453     // Link them to inUseTail, which will be linked into the global in-use list
1454     // gOmInUseList below, under the gListLock
1455     for (cur_om = inUseList; cur_om != NULL; cur_om = cur_om->FreeNext) {
1456       inUseTail = cur_om;
1457       inUseTally++;
1458       ADIM_guarantee(cur_om->is_active(), "invariant");
1459     }
1460     guarantee(inUseTail != NULL, "invariant");
1461     ADIM_guarantee(Self->omInUseCount == inUseTally, "in-use count off");
1462     Self->omInUseList = NULL;
1463     Self->omInUseCount = 0;
1464   }
1465 
1466   Thread::muxAcquire(&gListLock, "omFlush");
1467   if (tail != NULL) {
1468     tail->FreeNext = gFreeList;
1469     gFreeList = list;
1470     gMonitorFreeCount += tally;
1471   }
1472 
1473   if (inUseTail != NULL) {
1474     inUseTail->FreeNext = gOmInUseList;
1475     gOmInUseList = inUseList;
1476     gOmInUseCount += inUseTally;
1477   }
1478 
1479   Thread::muxRelease(&gListLock);
1480 
1481   LogStreamHandle(Debug, monitorinflation) lsh_debug;


1489   }
1490   if (ls != NULL) {
1491     ls->print_cr("omFlush: jt=" INTPTR_FORMAT ", free_monitor_tally=%d"
1492                  ", in_use_monitor_tally=%d" ", omFreeProvision=%d",
1493                  p2i(Self), tally, inUseTally, Self->omFreeProvision);
1494   }
1495 }
1496 
1497 static void post_monitor_inflate_event(EventJavaMonitorInflate* event,
1498                                        const oop obj,
1499                                        ObjectSynchronizer::InflateCause cause) {
1500   assert(event != NULL, "invariant");
1501   assert(event->should_commit(), "invariant");
1502   event->set_monitorClass(obj->klass());
1503   event->set_address((uintptr_t)(void*)obj);
1504   event->set_cause((u1)cause);
1505   event->commit();
1506 }
1507 
1508 // Fast path code shared by multiple functions
1509 void ObjectSynchronizer::inflate_helper(ObjectMonitorHandle * omh_p, oop obj) {
1510   while (true) {
1511     markOop mark = obj->mark();
1512     if (mark->has_monitor()) {
1513       if (!omh_p->save_om_ptr(obj, mark)) {
1514         // Lost a race with async deflation so try again.
1515         assert(AsyncDeflateIdleMonitors, "sanity check");
1516         continue;
1517       }
1518       ObjectMonitor * monitor = omh_p->om_ptr();
1519       assert(ObjectSynchronizer::verify_objmon_isinpool(monitor), "monitor is invalid");
1520       markOop dmw = monitor->header();
1521       assert(dmw->is_neutral(), "sanity check: header=" INTPTR_FORMAT, p2i(dmw));
1522       return;
1523     }
1524     inflate(omh_p, Thread::current(), obj, inflate_cause_vm_internal);
1525     return;
1526   }

1527 }
1528 
1529 void ObjectSynchronizer::inflate(ObjectMonitorHandle * omh_p, Thread * Self,
1530                                  oop object, const InflateCause cause) {

1531   // Inflate mutates the heap ...
1532   // Relaxing assertion for bug 6320749.
1533   assert(Universe::verify_in_progress() ||
1534          !SafepointSynchronize::is_at_safepoint(), "invariant");
1535 
1536   EventJavaMonitorInflate event;
1537 
1538   for (;;) {
1539     const markOop mark = object->mark();
1540     assert(!mark->has_bias_pattern(), "invariant");
1541 
1542     // The mark can be in one of the following states:
1543     // *  Inflated     - just return
1544     // *  Stack-locked - coerce it to inflated
1545     // *  INFLATING    - busy wait for conversion to complete
1546     // *  Neutral      - aggressively inflate the object.
1547     // *  BIASED       - Illegal.  We should never see this
1548 
1549     // CASE: inflated
1550     if (mark->has_monitor()) {
1551       if (!omh_p->save_om_ptr(object, mark)) {
1552         // Lost a race with async deflation so try again.
1553         assert(AsyncDeflateIdleMonitors, "sanity check");
1554         continue;
1555       }
1556       ObjectMonitor * inf = omh_p->om_ptr();
1557       markOop dmw = inf->header();
1558       assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1559       assert(oopDesc::equals((oop) inf->object(), object), "invariant");
1560       assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid");
1561       return;
1562     }
1563 
1564     // CASE: inflation in progress - inflating over a stack-lock.
1565     // Some other thread is converting from stack-locked to inflated.
1566     // Only that thread can complete inflation -- other threads must wait.
1567     // The INFLATING value is transient.
1568     // Currently, we spin/yield/park and poll the markword, waiting for inflation to finish.
1569     // We could always eliminate polling by parking the thread on some auxiliary list.
1570     if (mark == markOopDesc::INFLATING()) {
1571       ReadStableMark(object);
1572       continue;
1573     }
1574 
1575     // CASE: stack-locked
1576     // Could be stack-locked either by this thread or by some other thread.
1577     //
1578     // Note that we allocate the objectmonitor speculatively, _before_ attempting
1579     // to install INFLATING into the mark word.  We originally installed INFLATING,
1580     // allocated the objectmonitor, and then finally STed the address of the
1581     // objectmonitor into the mark.  This was correct, but artificially lengthened
1582     // the interval in which INFLATED appeared in the mark, thus increasing
1583     // the odds of inflation contention.
1584     //
1585     // We now use per-thread private objectmonitor free lists.
1586     // These list are reprovisioned from the global free list outside the
1587     // critical INFLATING...ST interval.  A thread can transfer
1588     // multiple objectmonitors en-mass from the global free list to its local free list.
1589     // This reduces coherency traffic and lock contention on the global free list.
1590     // Using such local free lists, it doesn't matter if the omAlloc() call appears
1591     // before or after the CAS(INFLATING) operation.
1592     // See the comments in omAlloc().
1593 
1594     LogStreamHandle(Trace, monitorinflation) lsh;
1595 
1596     if (mark->has_locker()) {
1597       ObjectMonitor * m;
1598       if (!AsyncDeflateIdleMonitors || cause == inflate_cause_vm_internal) {
1599         // If !AsyncDeflateIdleMonitors or if an internal inflation, then
1600         // we won't stop for a potential safepoint in omAlloc.
1601         m = omAlloc(Self, cause);
1602       } else {
1603         // If AsyncDeflateIdleMonitors and not an internal inflation, then
1604         // we may stop for a safepoint in omAlloc() so protect object.
1605         Handle h_obj(Self, object);
1606         m = omAlloc(Self, cause);
1607         object = h_obj();  // Refresh object.
1608       }
1609       // Optimistically prepare the objectmonitor - anticipate successful CAS
1610       // We do this before the CAS in order to minimize the length of time
1611       // in which INFLATING appears in the mark.
1612       m->Recycle();
1613       m->_Responsible  = NULL;
1614       m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;   // Consider: maintain by type/class
1615 
1616       markOop cmp = object->cas_set_mark(markOopDesc::INFLATING(), mark);
1617       if (cmp != mark) {
1618         omRelease(Self, m, true);
1619         continue;       // Interference -- just retry
1620       }
1621 
1622       // We've successfully installed INFLATING (0) into the mark-word.
1623       // This is the only case where 0 will appear in a mark-word.
1624       // Only the singular thread that successfully swings the mark-word
1625       // to 0 can perform (or more precisely, complete) inflation.
1626       //
1627       // Why do we CAS a 0 into the mark-word instead of just CASing the
1628       // mark-word from the stack-locked value directly to the new inflated state?


1635       // value from the basiclock on the owner's stack to the objectMonitor, all
1636       // the while preserving the hashCode stability invariants.  If the owner
1637       // decides to release the lock while the value is 0, the unlock will fail
1638       // and control will eventually pass from slow_exit() to inflate.  The owner
1639       // will then spin, waiting for the 0 value to disappear.   Put another way,
1640       // the 0 causes the owner to stall if the owner happens to try to
1641       // drop the lock (restoring the header from the basiclock to the object)
1642       // while inflation is in-progress.  This protocol avoids races that might
1643       // would otherwise permit hashCode values to change or "flicker" for an object.
1644       // Critically, while object->mark is 0 mark->displaced_mark_helper() is stable.
1645       // 0 serves as a "BUSY" inflate-in-progress indicator.
1646 
1647 
1648       // fetch the displaced mark from the owner's stack.
1649       // The owner can't die or unwind past the lock while our INFLATING
1650       // object is in the mark.  Furthermore the owner can't complete
1651       // an unlock on the object, either.
1652       markOop dmw = mark->displaced_mark_helper();
1653       // Catch if the object's header is not neutral (not locked and
1654       // not marked is what we care about here).
1655       ADIM_guarantee(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1656 
1657       // Setup monitor fields to proper values -- prepare the monitor
1658       m->set_header(dmw);
1659 
1660       // Optimization: if the mark->locker stack address is associated
1661       // with this thread we could simply set m->_owner = Self.
1662       // Note that a thread can inflate an object
1663       // that it has stack-locked -- as might happen in wait() -- directly
1664       // with CAS.  That is, we can avoid the xchg-NULL .... ST idiom.
1665       m->set_owner(mark->locker());
1666       m->set_object(object);
1667       // TODO-FIXME: assert BasicLock->dhw != 0.
1668 
1669       omh_p->set_om_ptr(m);
1670       assert(m->is_new(), "freshly allocated monitor must be new");
1671       m->set_allocation_state(ObjectMonitor::Old);
1672 
1673       // Must preserve store ordering. The monitor state must
1674       // be stable at the time of publishing the monitor address.
1675       guarantee(object->mark() == markOopDesc::INFLATING(), "invariant");
1676       object->release_set_mark(markOopDesc::encode(m));
1677 
1678       // Hopefully the performance counters are allocated on distinct cache lines
1679       // to avoid false sharing on MP systems ...
1680       OM_PERFDATA_OP(Inflations, inc());
1681       if (log_is_enabled(Trace, monitorinflation)) {
1682         ResourceMark rm(Self);
1683         lsh.print_cr("inflate(has_locker): object=" INTPTR_FORMAT ", mark="
1684                      INTPTR_FORMAT ", type='%s'", p2i(object),
1685                      p2i(object->mark()), object->klass()->external_name());
1686       }
1687       if (event.should_commit()) {
1688         post_monitor_inflate_event(&event, object, cause);
1689       }
1690       ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free");
1691       return;
1692     }
1693 
1694     // CASE: neutral
1695     // TODO-FIXME: for entry we currently inflate and then try to CAS _owner.
1696     // If we know we're inflating for entry it's better to inflate by swinging a
1697     // pre-locked objectMonitor pointer into the object header.   A successful
1698     // CAS inflates the object *and* confers ownership to the inflating thread.
1699     // In the current implementation we use a 2-step mechanism where we CAS()
1700     // to inflate and then CAS() again to try to swing _owner from NULL to Self.
1701     // An inflateTry() method that we could call from fast_enter() and slow_enter()
1702     // would be useful.
1703 
1704     // Catch if the object's header is not neutral (not locked and
1705     // not marked is what we care about here).
1706     ADIM_guarantee(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark));
1707     ObjectMonitor * m;
1708     if (!AsyncDeflateIdleMonitors || cause == inflate_cause_vm_internal) {
1709       // If !AsyncDeflateIdleMonitors or if an internal inflation, then
1710       // we won't stop for a potential safepoint in omAlloc.
1711       m = omAlloc(Self, cause);
1712     } else {
1713       // If AsyncDeflateIdleMonitors and not an internal inflation, then
1714       // we may stop for a safepoint in omAlloc() so protect object.
1715       Handle h_obj(Self, object);
1716       m = omAlloc(Self, cause);
1717       object = h_obj();  // Refresh object.
1718     }
1719     // prepare m for installation - set monitor to initial state
1720     m->Recycle();
1721     m->set_header(mark);
1722     // If we leave _owner == DEFLATER_MARKER here, then the simple C2
1723     // ObjectMonitor enter optimization can no longer race with async
1724     // deflation and reuse.
1725     m->set_object(object);
1726     m->_Responsible  = NULL;
1727     m->_SpinDuration = ObjectMonitor::Knob_SpinLimit;       // consider: keep metastats by type/class
1728 
1729     omh_p->set_om_ptr(m);
1730     assert(m->is_new(), "freshly allocated monitor must be new");
1731     m->set_allocation_state(ObjectMonitor::Old);
1732 
1733     if (object->cas_set_mark(markOopDesc::encode(m), mark) != mark) {
1734       m->set_header(NULL);
1735       m->set_object(NULL);
1736       m->Recycle();
1737       omh_p->set_om_ptr(NULL);
1738       // omRelease() will reset the allocation state
1739       omRelease(Self, m, true);
1740       m = NULL;
1741       continue;
1742       // interference - the markword changed - just retry.
1743       // The state-transitions are one-way, so there's no chance of
1744       // live-lock -- "Inflated" is an absorbing state.
1745     }
1746 
1747     // Hopefully the performance counters are allocated on distinct
1748     // cache lines to avoid false sharing on MP systems ...
1749     OM_PERFDATA_OP(Inflations, inc());
1750     if (log_is_enabled(Trace, monitorinflation)) {
1751       ResourceMark rm(Self);
1752       lsh.print_cr("inflate(neutral): object=" INTPTR_FORMAT ", mark="
1753                    INTPTR_FORMAT ", type='%s'", p2i(object),
1754                    p2i(object->mark()), object->klass()->external_name());
1755     }
1756     if (event.should_commit()) {
1757       post_monitor_inflate_event(&event, object, cause);
1758     }
1759     ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free");
1760     return;
1761   }
1762 }
1763 
1764 
1765 // We maintain a list of in-use monitors for each thread.
1766 //
1767 // For safepoint based deflation:
1768 // deflate_thread_local_monitors() scans a single thread's in-use list, while
1769 // deflate_idle_monitors() scans only a global list of in-use monitors which
1770 // is populated only as a thread dies (see omFlush()).
1771 //
1772 // These operations are called at all safepoints, immediately after mutators
1773 // are stopped, but before any objects have moved. Collectively they traverse
1774 // the population of in-use monitors, deflating where possible. The scavenged
1775 // monitors are returned to the global monitor free list.
1776 //
1777 // Beware that we scavenge at *every* stop-the-world point. Having a large
1778 // number of monitors in-use could negatively impact performance. We also want
1779 // to minimize the total # of monitors in circulation, as they incur a small
1780 // footprint penalty.
1781 //
1782 // Perversely, the heap size -- and thus the STW safepoint rate --
1783 // typically drives the scavenge rate.  Large heaps can mean infrequent GC,
1784 // which in turn can mean large(r) numbers of ObjectMonitors in circulation.
1785 // This is an unfortunate aspect of this design.
1786 //
1787 // For async deflation:
1788 // If a special deflation request is made, then the safepoint based
1789 // deflation mechanism is used. Otherwise, an async deflation request
1790 // is registered with the ServiceThread and it is notified.
1791 
1792 void ObjectSynchronizer::do_safepoint_work(DeflateMonitorCounters* _counters) {
1793   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
1794 
1795   // The per-thread in-use lists are handled in
1796   // ParallelSPCleanupThreadClosure::do_thread().
1797 
1798   if (!AsyncDeflateIdleMonitors || is_special_deflation_requested()) {
1799     // Use the older mechanism for the global in-use list or if a
1800     // special deflation has been requested before the safepoint.
1801     ObjectSynchronizer::deflate_idle_monitors(_counters);
1802     return;
1803   }
1804 
1805   log_debug(monitorinflation)("requesting async deflation of idle monitors.");
1806   // Request deflation of idle monitors by the ServiceThread:
1807   set_is_async_deflation_requested(true);
1808   MonitorLocker ml(Service_lock, Mutex::_no_safepoint_check_flag);
1809   ml.notify_all();
1810 }
1811 
1812 // Deflate a single monitor if not in-use
1813 // Return true if deflated, false if in-use
1814 bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj,
1815                                          ObjectMonitor** freeHeadp,
1816                                          ObjectMonitor** freeTailp) {
1817   bool deflated;
1818   // Normal case ... The monitor is associated with obj.
1819   const markOop mark = obj->mark();
1820   guarantee(mark == markOopDesc::encode(mid), "should match: mark="
1821             INTPTR_FORMAT ", encoded mid=" INTPTR_FORMAT, p2i(mark),
1822             p2i(markOopDesc::encode(mid)));
1823   // Make sure that mark->monitor() and markOopDesc::encode() agree:
1824   guarantee(mark->monitor() == mid, "should match: monitor()=" INTPTR_FORMAT
1825             ", mid=" INTPTR_FORMAT, p2i(mark->monitor()), p2i(mid));
1826   const markOop dmw = mid->header();
1827   guarantee(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw));
1828 
1829   if (mid->is_busy() || mid->ref_count() != 0) {
1830     // Easy checks are first - the ObjectMonitor is busy or ObjectMonitor*
1831     // is in use so no deflation.
1832     deflated = false;
1833   } else {
1834     // Deflate the monitor if it is no longer being used
1835     // It's idle - scavenge and return to the global free list
1836     // plain old deflation ...
1837     if (log_is_enabled(Trace, monitorinflation)) {
1838       ResourceMark rm;
1839       log_trace(monitorinflation)("deflate_monitor: "
1840                                   "object=" INTPTR_FORMAT ", mark="
1841                                   INTPTR_FORMAT ", type='%s'", p2i(obj),
1842                                   p2i(mark), obj->klass()->external_name());
1843     }
1844 
1845     // Restore the header back to obj
1846     obj->release_set_mark(dmw);
1847     if (AsyncDeflateIdleMonitors) {
1848       // clear() expects the owner field to be NULL and we won't race
1849       // with the simple C2 ObjectMonitor enter optimization since
1850       // we're at a safepoint.
1851       mid->set_owner(NULL);
1852     }
1853     mid->clear();
1854 
1855     assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT,
1856            p2i(mid->object()));
1857     assert(mid->is_free(), "invariant");
1858 
1859     // Move the object to the working free list defined by freeHeadp, freeTailp
1860     if (*freeHeadp == NULL) *freeHeadp = mid;
1861     if (*freeTailp != NULL) {
1862       ObjectMonitor * prevtail = *freeTailp;
1863       assert(prevtail->FreeNext == NULL, "cleaned up deflated?");
1864       prevtail->FreeNext = mid;
1865     }
1866     *freeTailp = mid;
1867     deflated = true;
1868   }
1869   return deflated;
1870 }
1871 
1872 // Deflate the specified ObjectMonitor if not in-use using a JavaThread.
1873 // Returns true if it was deflated and false otherwise.
1874 //
1875 // The async deflation protocol sets owner to DEFLATER_MARKER and
1876 // makes ref_count negative as signals to contending threads that
1877 // an async deflation is in progress. There are a number of checks
1878 // as part of the protocol to make sure that the calling thread has
1879 // not lost the race to a contending thread or to a thread that just
1880 // wants to use the ObjectMonitor*.
1881 //
1882 // The ObjectMonitor has been successfully async deflated when:
1883 // (owner == DEFLATER_MARKER && ref_count < 0)
1884 // Contending threads or ObjectMonitor* using threads that see those
1885 // values know to retry their operation.
1886 //
1887 bool ObjectSynchronizer::deflate_monitor_using_JT(ObjectMonitor* mid,
1888                                                   ObjectMonitor** freeHeadp,
1889                                                   ObjectMonitor** freeTailp) {
1890   assert(AsyncDeflateIdleMonitors, "sanity check");
1891   assert(Thread::current()->is_Java_thread(), "precondition");
1892   // A newly allocated ObjectMonitor should not be seen here so we
1893   // avoid an endless inflate/deflate cycle.
1894   assert(mid->is_old(), "must be old: allocation_state=%d",
1895          (int) mid->allocation_state());
1896 
1897   if (mid->is_busy() || mid->ref_count() != 0) {
1898     // Easy checks are first - the ObjectMonitor is busy or ObjectMonitor*
1899     // is in use so no deflation.
1900     return false;
1901   }
1902 
1903   if (Atomic::replace_if_null(DEFLATER_MARKER, &(mid->_owner))) {
1904     // ObjectMonitor is not owned by another thread. Our setting
1905     // owner to DEFLATER_MARKER forces any contending thread through
1906     // the slow path. This is just the first part of the async
1907     // deflation dance.
1908 
1909     if (mid->_contentions != 0 || mid->_waiters != 0) {
1910       // Another thread has raced to enter the ObjectMonitor after
1911       // mid->is_busy() above or has already entered and waited on
1912       // it which makes it busy so no deflation. Restore owner to
1913       // NULL if it is still DEFLATER_MARKER.
1914       Atomic::cmpxchg((void*)NULL, &mid->_owner, DEFLATER_MARKER);
1915       return false;
1916     }
1917 
1918     if (Atomic::cmpxchg(-max_jint, &mid->_ref_count, (jint)0) == 0) {
1919       // Make ref_count negative to force any contending threads or
1920       // ObjectMonitor* using threads to retry. This is the second
1921       // part of the async deflation dance.
1922 
1923       if (mid->owner_is_DEFLATER_MARKER()) {
1924         // If owner is still DEFLATER_MARKER, then we have successfully
1925         // signaled any contending threads to retry. If it is not, then we
1926         // have lost the race to an entering thread and the ObjectMonitor
1927         // is now busy. This is the third and final part of the async
1928         // deflation dance.
1929         // Note: This owner check solves the ABA problem with ref_count
1930         // where another thread acquired the ObjectMonitor, finished
1931         // using it and restored the ref_count to zero.
1932 
1933         // Sanity checks for the races:
1934         guarantee(mid->_contentions == 0, "must be 0: contentions=%d",
1935                   mid->_contentions);
1936         guarantee(mid->_waiters == 0, "must be 0: waiters=%d", mid->_waiters);
1937         guarantee(mid->_cxq == NULL, "must be no contending threads: cxq="
1938                   INTPTR_FORMAT, p2i(mid->_cxq));
1939         guarantee(mid->_EntryList == NULL,
1940                   "must be no entering threads: EntryList=" INTPTR_FORMAT,
1941                   p2i(mid->_EntryList));
1942 
1943         const oop obj = (oop) mid->object();
1944         if (log_is_enabled(Trace, monitorinflation)) {
1945           ResourceMark rm;
1946           log_trace(monitorinflation)("deflate_monitor_using_JT: "
1947                                       "object=" INTPTR_FORMAT ", mark="
1948                                       INTPTR_FORMAT ", type='%s'",
1949                                       p2i(obj), p2i(obj->mark()),
1950                                       obj->klass()->external_name());
1951         }
1952 
1953         // Install the old mark word if nobody else has already done it.
1954         mid->install_displaced_markword_in_object(obj);
1955         mid->clear_using_JT();
1956 
1957         assert(mid->object() == NULL, "must be NULL: object=" INTPTR_FORMAT,
1958                p2i(mid->object()));
1959         assert(mid->is_free(), "must be free: allocation_state=%d",
1960                (int) mid->allocation_state());
1961 
1962         // Move the deflated ObjectMonitor to the working free list
1963         // defined by freeHeadp and freeTailp.
1964         if (*freeHeadp == NULL) {
1965           // First one on the list.
1966           *freeHeadp = mid;
1967         }
1968         if (*freeTailp != NULL) {
1969           // We append to the list so the caller can use mid->FreeNext
1970           // to fix the linkages in its context.
1971           ObjectMonitor * prevtail = *freeTailp;
1972           // Should have been cleaned up by the caller:
1973           assert(prevtail->FreeNext == NULL, "must be NULL: FreeNext="
1974                  INTPTR_FORMAT, p2i(prevtail->FreeNext));
1975           prevtail->FreeNext = mid;
1976         }
1977         *freeTailp = mid;
1978 
1979         // At this point, mid->FreeNext still refers to its current
1980         // value and another ObjectMonitor's FreeNext field still
1981         // refers to this ObjectMonitor. Those linkages have to be
1982         // cleaned up by the caller who has the complete context.
1983 
1984         // We leave owner == DEFLATER_MARKER and ref_count < 0
1985         // to force any racing threads to retry.
1986         return true;  // Success, ObjectMonitor has been deflated.
1987       }
1988 
1989       // The owner was changed from DEFLATER_MARKER so we lost the
1990       // race since the ObjectMonitor is now busy.
1991 
1992       // Add back max_jint to restore the ref_count field to its
1993       // proper value (which may not be what we saw above):
1994       Atomic::add(max_jint, &mid->_ref_count);
1995 
1996       assert(mid->ref_count() >= 0, "must not be negative: ref_count=%d",
1997              mid->ref_count());
1998       return false;
1999     }
2000 
2001     // The ref_count was no longer 0 so we lost the race since the
2002     // ObjectMonitor is now busy or the ObjectMonitor* is now is use.
2003     // Restore owner to NULL if it is still DEFLATER_MARKER:
2004     Atomic::cmpxchg((void*)NULL, &mid->_owner, DEFLATER_MARKER);
2005   }
2006 
2007   // The owner field is no longer NULL so we lost the race since the
2008   // ObjectMonitor is now busy.
2009   return false;
2010 }
2011 
2012 // Walk a given monitor list, and deflate idle monitors
2013 // The given list could be a per-thread list or a global list
2014 // Caller acquires gListLock as needed.
2015 //
2016 // In the case of parallel processing of thread local monitor lists,
2017 // work is done by Threads::parallel_threads_do() which ensures that
2018 // each Java thread is processed by exactly one worker thread, and
2019 // thus avoid conflicts that would arise when worker threads would
2020 // process the same monitor lists concurrently.
2021 //
2022 // See also ParallelSPCleanupTask and
2023 // SafepointSynchronize::do_cleanup_tasks() in safepoint.cpp and
2024 // Threads::parallel_java_threads_do() in thread.cpp.
2025 int ObjectSynchronizer::deflate_monitor_list(ObjectMonitor** listHeadp,
2026                                              ObjectMonitor** freeHeadp,
2027                                              ObjectMonitor** freeTailp) {
2028   ObjectMonitor* mid;
2029   ObjectMonitor* next;
2030   ObjectMonitor* cur_mid_in_use = NULL;
2031   int deflated_count = 0;


2035     if (obj != NULL && deflate_monitor(mid, obj, freeHeadp, freeTailp)) {
2036       // if deflate_monitor succeeded,
2037       // extract from per-thread in-use list
2038       if (mid == *listHeadp) {
2039         *listHeadp = mid->FreeNext;
2040       } else if (cur_mid_in_use != NULL) {
2041         cur_mid_in_use->FreeNext = mid->FreeNext; // maintain the current thread in-use list
2042       }
2043       next = mid->FreeNext;
2044       mid->FreeNext = NULL;  // This mid is current tail in the freeHeadp list
2045       mid = next;
2046       deflated_count++;
2047     } else {
2048       cur_mid_in_use = mid;
2049       mid = mid->FreeNext;
2050     }
2051   }
2052   return deflated_count;
2053 }
2054 
2055 // Walk a given ObjectMonitor list and deflate idle ObjectMonitors using
2056 // a JavaThread. Returns the number of deflated ObjectMonitors. The given
2057 // list could be a per-thread in-use list or the global in-use list.
2058 // Caller acquires gListLock as appropriate. If a safepoint has started,
2059 // then we save state via savedMidInUsep and return to the caller to
2060 // honor the safepoint.
2061 //
2062 int ObjectSynchronizer::deflate_monitor_list_using_JT(ObjectMonitor** listHeadp,
2063                                                       ObjectMonitor** freeHeadp,
2064                                                       ObjectMonitor** freeTailp,
2065                                                       ObjectMonitor** savedMidInUsep) {
2066   assert(AsyncDeflateIdleMonitors, "sanity check");
2067   assert(Thread::current()->is_Java_thread(), "precondition");
2068 
2069   ObjectMonitor* mid;
2070   ObjectMonitor* next;
2071   ObjectMonitor* cur_mid_in_use = NULL;
2072   int deflated_count = 0;
2073 
2074   if (*savedMidInUsep == NULL) {
2075     // No saved state so start at the beginning.
2076     mid = *listHeadp;
2077   } else {
2078     // We're restarting after a safepoint so restore the necessary state
2079     // before we resume.
2080     cur_mid_in_use = *savedMidInUsep;
2081     mid = cur_mid_in_use->FreeNext;
2082   }
2083   while (mid != NULL) {
2084     // Only try to deflate if there is an associated Java object and if
2085     // mid is old (is not newly allocated and is not newly freed).
2086     if (mid->object() != NULL && mid->is_old() &&
2087         deflate_monitor_using_JT(mid, freeHeadp, freeTailp)) {
2088       // Deflation succeeded so update the in-use list.
2089       if (mid == *listHeadp) {
2090         *listHeadp = mid->FreeNext;
2091       } else if (cur_mid_in_use != NULL) {
2092         // Maintain the current in-use list.
2093         cur_mid_in_use->FreeNext = mid->FreeNext;
2094       }
2095       next = mid->FreeNext;
2096       mid->FreeNext = NULL;
2097       // At this point mid is disconnected from the in-use list
2098       // and is the current tail in the freeHeadp list.
2099       mid = next;
2100       deflated_count++;
2101     } else {
2102       // mid is considered in-use if it does not have an associated
2103       // Java object or mid is not old or deflation did not succeed.
2104       // A mid->is_new() node can be seen here when it is freshly
2105       // returned by omAlloc() (and skips the deflation code path).
2106       // A mid->is_old() node can be seen here when deflation failed.
2107       // A mid->is_free() node can be seen here when a fresh node from
2108       // omAlloc() is released by omRelease() due to losing the race
2109       // in inflate().
2110 
2111       cur_mid_in_use = mid;
2112       mid = mid->FreeNext;
2113 
2114       if (SafepointSynchronize::is_synchronizing() &&
2115           cur_mid_in_use != *listHeadp && cur_mid_in_use->is_old()) {
2116         // If a safepoint has started and cur_mid_in_use is not the list
2117         // head and is old, then it is safe to use as saved state. Return
2118         // to the caller so gListLock can be dropped as appropriate
2119         // before blocking.
2120         *savedMidInUsep = cur_mid_in_use;
2121         return deflated_count;
2122       }
2123     }
2124   }
2125   // We finished the list without a safepoint starting so there's
2126   // no need to save state.
2127   *savedMidInUsep = NULL;
2128   return deflated_count;
2129 }
2130 
2131 void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2132   counters->nInuse = 0;              // currently associated with objects
2133   counters->nInCirculation = 0;      // extant
2134   counters->nScavenged = 0;          // reclaimed (global and per-thread)
2135   counters->perThreadScavenged = 0;  // per-thread scavenge total
2136   counters->perThreadTimes = 0.0;    // per-thread scavenge times
2137 }
2138 
2139 void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) {
2140   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2141 
2142   if (AsyncDeflateIdleMonitors) {
2143     // Nothing to do when global idle ObjectMonitors are deflated using
2144     // a JavaThread unless a special deflation has been requested.
2145     if (!is_special_deflation_requested()) {
2146       return;
2147     }
2148   }
2149 
2150   bool deflated = false;
2151 
2152   ObjectMonitor * freeHeadp = NULL;  // Local SLL of scavenged monitors
2153   ObjectMonitor * freeTailp = NULL;
2154   elapsedTimer timer;
2155 
2156   if (log_is_enabled(Info, monitorinflation)) {
2157     timer.start();
2158   }
2159 
2160   // Prevent omFlush from changing mids in Thread dtor's during deflation
2161   // And in case the vm thread is acquiring a lock during a safepoint
2162   // See e.g. 6320749
2163   Thread::muxAcquire(&gListLock, "deflate_idle_monitors");
2164 
2165   // Note: the thread-local monitors lists get deflated in
2166   // a separate pass. See deflate_thread_local_monitors().
2167 
2168   // For moribund threads, scan gOmInUseList
2169   int deflated_count = 0;


2182     // constant-time list splice - prepend scavenged segment to gFreeList
2183     freeTailp->FreeNext = gFreeList;
2184     gFreeList = freeHeadp;
2185   }
2186   Thread::muxRelease(&gListLock);
2187   timer.stop();
2188 
2189   LogStreamHandle(Debug, monitorinflation) lsh_debug;
2190   LogStreamHandle(Info, monitorinflation) lsh_info;
2191   LogStream * ls = NULL;
2192   if (log_is_enabled(Debug, monitorinflation)) {
2193     ls = &lsh_debug;
2194   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2195     ls = &lsh_info;
2196   }
2197   if (ls != NULL) {
2198     ls->print_cr("deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
2199   }
2200 }
2201 
2202 // Deflate global idle ObjectMonitors using a JavaThread.
2203 //
2204 void ObjectSynchronizer::deflate_global_idle_monitors_using_JT() {
2205   assert(AsyncDeflateIdleMonitors, "sanity check");
2206   assert(Thread::current()->is_Java_thread(), "precondition");
2207   JavaThread * self = JavaThread::current();
2208 
2209   deflate_common_idle_monitors_using_JT(true /* is_global */, self);
2210 }
2211 
2212 // Deflate per-thread idle ObjectMonitors using a JavaThread.
2213 //
2214 void ObjectSynchronizer::deflate_per_thread_idle_monitors_using_JT() {
2215   assert(AsyncDeflateIdleMonitors, "sanity check");
2216   assert(Thread::current()->is_Java_thread(), "precondition");
2217   JavaThread * self = JavaThread::current();
2218 
2219   self->omShouldDeflateIdleMonitors = false;
2220 
2221   deflate_common_idle_monitors_using_JT(false /* !is_global */, self);
2222 }
2223 
2224 // Deflate global or per-thread idle ObjectMonitors using a JavaThread.
2225 //
2226 void ObjectSynchronizer::deflate_common_idle_monitors_using_JT(bool is_global, JavaThread * self) {
2227   int deflated_count = 0;
2228   ObjectMonitor * freeHeadp = NULL;  // Local SLL of scavenged ObjectMonitors
2229   ObjectMonitor * freeTailp = NULL;
2230   ObjectMonitor * savedMidInUsep = NULL;
2231   elapsedTimer timer;
2232 
2233   if (log_is_enabled(Info, monitorinflation)) {
2234     timer.start();
2235   }
2236 
2237   if (is_global) {
2238     Thread::muxAcquire(&gListLock, "deflate_global_idle_monitors_using_JT(1)");
2239     OM_PERFDATA_OP(MonExtant, set_value(gOmInUseCount));
2240   } else {
2241     OM_PERFDATA_OP(MonExtant, inc(self->omInUseCount));
2242   }
2243 
2244   do {
2245     int local_deflated_count;
2246     if (is_global) {
2247       local_deflated_count = deflate_monitor_list_using_JT((ObjectMonitor **)&gOmInUseList, &freeHeadp, &freeTailp, &savedMidInUsep);
2248       gOmInUseCount -= local_deflated_count;
2249     } else {
2250       local_deflated_count = deflate_monitor_list_using_JT(self->omInUseList_addr(), &freeHeadp, &freeTailp, &savedMidInUsep);
2251       self->omInUseCount -= local_deflated_count;
2252     }
2253     deflated_count += local_deflated_count;
2254 
2255     if (freeHeadp != NULL) {
2256       // Move the scavenged ObjectMonitors to the global free list.
2257       guarantee(freeTailp != NULL && local_deflated_count > 0, "freeTailp=" INTPTR_FORMAT ", local_deflated_count=%d", p2i(freeTailp), local_deflated_count);
2258       assert(freeTailp->FreeNext == NULL, "invariant");
2259 
2260       if (!is_global) {
2261         Thread::muxAcquire(&gListLock, "deflate_per_thread_idle_monitors_using_JT(2)");
2262       }
2263       // Constant-time list splice - prepend scavenged segment to gFreeList.
2264       freeTailp->FreeNext = gFreeList;
2265       gFreeList = freeHeadp;
2266 
2267       gMonitorFreeCount += local_deflated_count;
2268       OM_PERFDATA_OP(Deflations, inc(local_deflated_count));
2269       if (!is_global) {
2270         Thread::muxRelease(&gListLock);
2271       }
2272     }
2273 
2274     if (savedMidInUsep != NULL) {
2275       // deflate_monitor_list_using_JT() detected a safepoint starting.
2276       if (is_global) {
2277         Thread::muxRelease(&gListLock);
2278       }
2279       timer.stop();
2280       {
2281         if (is_global) {
2282           log_debug(monitorinflation)("pausing deflation of global idle monitors for a safepoint.");
2283         } else {
2284           log_debug(monitorinflation)("jt=" INTPTR_FORMAT ": pausing deflation of per-thread idle monitors for a safepoint.", p2i(self));
2285         }
2286         assert(SafepointSynchronize::is_synchronizing(), "sanity check");
2287         ThreadBlockInVM blocker(self);
2288       }
2289       // Prepare for another loop after the safepoint.
2290       freeHeadp = NULL;
2291       freeTailp = NULL;
2292       if (log_is_enabled(Info, monitorinflation)) {
2293         timer.start();
2294       }
2295       if (is_global) {
2296         Thread::muxAcquire(&gListLock, "deflate_global_idle_monitors_using_JT(3)");
2297       }
2298     }
2299   } while (savedMidInUsep != NULL);
2300   if (is_global) {
2301     Thread::muxRelease(&gListLock);
2302   }
2303   timer.stop();
2304 
2305   LogStreamHandle(Debug, monitorinflation) lsh_debug;
2306   LogStreamHandle(Info, monitorinflation) lsh_info;
2307   LogStream * ls = NULL;
2308   if (log_is_enabled(Debug, monitorinflation)) {
2309     ls = &lsh_debug;
2310   } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) {
2311     ls = &lsh_info;
2312   }
2313   if (ls != NULL) {
2314     if (is_global) {
2315       ls->print_cr("async-deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count);
2316     } else {
2317       ls->print_cr("jt=" INTPTR_FORMAT ": async-deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(self), timer.seconds(), deflated_count);
2318     }
2319   }
2320 }
2321 
2322 void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) {
2323   // Report the cumulative time for deflating each thread's idle
2324   // monitors. Note: if the work is split among more than one
2325   // worker thread, then the reported time will likely be more
2326   // than a beginning to end measurement of the phase.
2327   // Note: AsyncDeflateIdleMonitors only deflates per-thread idle
2328   // monitors at a safepoint when a special deflation has been requested.
2329   log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->perThreadTimes, counters->perThreadScavenged);
2330 
2331   bool needs_special_deflation = is_special_deflation_requested();
2332   if (!AsyncDeflateIdleMonitors || needs_special_deflation) {
2333     // AsyncDeflateIdleMonitors does not use these counters unless
2334     // there is a special deflation request.
2335 
2336     gMonitorFreeCount += counters->nScavenged;
2337 
2338     OM_PERFDATA_OP(Deflations, inc(counters->nScavenged));
2339     OM_PERFDATA_OP(MonExtant, set_value(counters->nInCirculation));
2340   }
2341 
2342   if (log_is_enabled(Debug, monitorinflation)) {
2343     // exit_globals()'s call to audit_and_print_stats() is done
2344     // at the Info level.
2345     ObjectSynchronizer::audit_and_print_stats(false /* on_exit */);
2346   } else if (log_is_enabled(Info, monitorinflation)) {
2347     Thread::muxAcquire(&gListLock, "finish_deflate_idle_monitors");
2348     log_info(monitorinflation)("gMonitorPopulation=%d, gOmInUseCount=%d, "
2349                                "gMonitorFreeCount=%d", gMonitorPopulation,
2350                                gOmInUseCount, gMonitorFreeCount);
2351     Thread::muxRelease(&gListLock);
2352   }
2353 
2354   ForceMonitorScavenge = 0;    // Reset




2355   GVars.stwRandom = os::random();
2356   GVars.stwCycle++;
2357   if (needs_special_deflation) {
2358     set_is_special_deflation_requested(false);  // special deflation is done
2359   }
2360 }
2361 
2362 void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) {
2363   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
2364 
2365   if (AsyncDeflateIdleMonitors) {
2366     if (!is_special_deflation_requested()) {
2367       // Mark the JavaThread for idle monitor deflation if a special
2368       // deflation has NOT been requested.
2369       if (thread->omInUseCount > 0) {
2370         // This JavaThread is using monitors so mark it.
2371         thread->omShouldDeflateIdleMonitors = true;
2372       }
2373       return;
2374     }
2375   }
2376 
2377   ObjectMonitor * freeHeadp = NULL;  // Local SLL of scavenged monitors
2378   ObjectMonitor * freeTailp = NULL;
2379   elapsedTimer timer;
2380 
2381   if (log_is_enabled(Info, safepoint, cleanup) ||
2382       log_is_enabled(Info, monitorinflation)) {
2383     timer.start();
2384   }
2385 
2386   int deflated_count = deflate_monitor_list(thread->omInUseList_addr(), &freeHeadp, &freeTailp);
2387 
2388   Thread::muxAcquire(&gListLock, "deflate_thread_local_monitors");
2389 
2390   // Adjust counters
2391   counters->nInCirculation += thread->omInUseCount;
2392   thread->omInUseCount -= deflated_count;
2393   counters->nScavenged += deflated_count;
2394   counters->nInuse += thread->omInUseCount;
2395   counters->perThreadScavenged += deflated_count;
2396 


2583 void ObjectSynchronizer::chk_free_entry(JavaThread * jt, ObjectMonitor * n,
2584                                         outputStream * out, int *error_cnt_p) {
2585   stringStream ss;
2586   if (n->is_busy()) {
2587     if (jt != NULL) {
2588       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2589                     ": free per-thread monitor must not be busy: %s", p2i(jt),
2590                     p2i(n), n->is_busy_to_string(&ss));
2591     } else {
2592       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
2593                     "must not be busy: %s", p2i(n), n->is_busy_to_string(&ss));
2594     }
2595     *error_cnt_p = *error_cnt_p + 1;
2596   }
2597   if (n->header() != NULL) {
2598     if (jt != NULL) {
2599       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2600                     ": free per-thread monitor must have NULL _header "
2601                     "field: _header=" INTPTR_FORMAT, p2i(jt), p2i(n),
2602                     p2i(n->header()));
2603       *error_cnt_p = *error_cnt_p + 1;
2604     } else if (!AsyncDeflateIdleMonitors) {
2605       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
2606                     "must have NULL _header field: _header=" INTPTR_FORMAT,
2607                     p2i(n), p2i(n->header()));

2608       *error_cnt_p = *error_cnt_p + 1;
2609     }
2610   }
2611   if (n->object() != NULL) {
2612     if (jt != NULL) {
2613       out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT
2614                     ": free per-thread monitor must have NULL _object "
2615                     "field: _object=" INTPTR_FORMAT, p2i(jt), p2i(n),
2616                     p2i(n->object()));
2617     } else {
2618       out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor "
2619                     "must have NULL _object field: _object=" INTPTR_FORMAT,
2620                     p2i(n), p2i(n->object()));
2621     }
2622     *error_cnt_p = *error_cnt_p + 1;
2623   }
2624 }
2625 
2626 // Check the global free list and count; log the results of the checks.
2627 void ObjectSynchronizer::chk_global_free_list_and_count(outputStream * out,
2628                                                         int *error_cnt_p) {
2629   int chkMonitorFreeCount = 0;
2630   for (ObjectMonitor * n = gFreeList; n != NULL; n = n->FreeNext) {


2757                   "equal to chkOmInUseCount=%d", p2i(jt), jt->omInUseCount,
2758                   chkOmInUseCount);
2759     *error_cnt_p = *error_cnt_p + 1;
2760   }
2761 }
2762 
2763 // Log details about ObjectMonitors on the in-use lists. The 'BHL'
2764 // flags indicate why the entry is in-use, 'object' and 'object type'
2765 // indicate the associated object and its type.
2766 void ObjectSynchronizer::log_in_use_monitor_details(outputStream * out,
2767                                                     bool on_exit) {
2768   if (!on_exit) {
2769     // Not at VM exit so grab the global list lock.
2770     Thread::muxAcquire(&gListLock, "log_in_use_monitor_details");
2771   }
2772 
2773   stringStream ss;
2774   if (gOmInUseCount > 0) {
2775     out->print_cr("In-use global monitor info:");
2776     out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2777     out->print_cr("%18s  %s  %7s  %18s  %18s",
2778                   "monitor", "BHL", "ref_cnt", "object", "object type");
2779     out->print_cr("==================  ===  =======  ==================  ==================");
2780     for (ObjectMonitor * n = gOmInUseList; n != NULL; n = n->FreeNext) {
2781       const oop obj = (oop) n->object();
2782       const markOop mark = n->header();
2783       ResourceMark rm;
2784       out->print(INTPTR_FORMAT "  %d%d%d  %7d  " INTPTR_FORMAT "  %s",
2785                  p2i(n), n->is_busy() != 0, mark->hash() != 0,
2786                  n->owner() != NULL, (int)n->ref_count(), p2i(obj),
2787                  obj->klass()->external_name());
2788       if (n->is_busy() != 0) {
2789         out->print(" (%s)", n->is_busy_to_string(&ss));
2790         ss.reset();
2791       }
2792       out->cr();
2793     }
2794   }
2795 
2796   if (!on_exit) {
2797     Thread::muxRelease(&gListLock);
2798   }
2799 
2800   out->print_cr("In-use per-thread monitor info:");
2801   out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)");
2802   out->print_cr("%18s  %18s  %s  %7s  %18s  %18s",
2803                 "jt", "monitor", "BHL", "ref_cnt", "object", "object type");
2804   out->print_cr("==================  ==================  ===  =======  ==================  ==================");
2805   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
2806     for (ObjectMonitor * n = jt->omInUseList; n != NULL; n = n->FreeNext) {
2807       const oop obj = (oop) n->object();
2808       const markOop mark = n->header();
2809       ResourceMark rm;
2810       out->print(INTPTR_FORMAT "  " INTPTR_FORMAT "  %d%d%d  %7d  "
2811                  INTPTR_FORMAT "  %s", p2i(jt), p2i(n), n->is_busy() != 0,
2812                  mark->hash() != 0, n->owner() != NULL, (int)n->ref_count(),
2813                  p2i(obj), obj->klass()->external_name());
2814       if (n->is_busy() != 0) {
2815         out->print(" (%s)", n->is_busy_to_string(&ss));
2816         ss.reset();
2817       }
2818       out->cr();
2819     }
2820   }
2821 
2822   out->flush();
2823 }
2824 
2825 // Log counts for the global and per-thread monitor lists and return
2826 // the population count.
2827 int ObjectSynchronizer::log_monitor_list_counts(outputStream * out) {
2828   int popCount = 0;
2829   out->print_cr("%18s  %10s  %10s  %10s",
2830                 "Global Lists:", "InUse", "Free", "Total");
2831   out->print_cr("==================  ==========  ==========  ==========");
2832   out->print_cr("%18s  %10d  %10d  %10d", "",
2833                 gOmInUseCount, gMonitorFreeCount, gMonitorPopulation);


< prev index next >