--- old/src/hotspot/share/runtime/synchronizer.cpp 2019-04-18 22:08:20.308306023 -0400 +++ new/src/hotspot/share/runtime/synchronizer.cpp 2019-04-18 22:08:19.556306036 -0400 @@ -124,6 +124,8 @@ ObjectMonitor * volatile ObjectSynchronizer::gOmInUseList = NULL; // count of entries in gOmInUseList int ObjectSynchronizer::gOmInUseCount = 0; +bool ObjectSynchronizer::_gOmShouldDeflateIdleMonitors = false; +bool volatile ObjectSynchronizer::_is_cleanup_requested = false; static volatile intptr_t gListLock = 0; // protects global monitor lists static volatile int gMonitorFreeCount = 0; // # on gFreeList @@ -210,40 +212,50 @@ assert(((JavaThread *) Self)->thread_state() == _thread_in_Java, "invariant"); NoSafepointVerifier nsv; if (obj == NULL) return false; // Need to throw NPE - const markOop mark = obj->mark(); - if (mark->has_monitor()) { - ObjectMonitor * const m = mark->monitor(); - assert(oopDesc::equals((oop) m->object(), obj), "invariant"); - Thread * const owner = (Thread *) m->_owner; - - // Lock contention and Transactional Lock Elision (TLE) diagnostics - // and observability - // Case: light contention possibly amenable to TLE - // Case: TLE inimical operations such as nested/recursive synchronization - - if (owner == Self) { - m->_recursions++; - return true; - } - - // This Java Monitor is inflated so obj's header will never be - // displaced to this thread's BasicLock. Make the displaced header - // non-NULL so this BasicLock is not seen as recursive nor as - // being locked. We do this unconditionally so that this thread's - // BasicLock cannot be mis-interpreted by any stack walkers. For - // performance reasons, stack walkers generally first check for - // Biased Locking in the object's header, the second check is for - // stack-locking in the object's header, the third check is for - // recursive stack-locking in the displaced header in the BasicLock, - // and last are the inflated Java Monitor (ObjectMonitor) checks. - lock->set_displaced_header(markOopDesc::unused_mark()); + while (true) { + const markOop mark = obj->mark(); + + if (mark->has_monitor()) { + ObjectMonitorHandle omh; + if (!omh.save_om_ptr(obj, mark)) { + // Lost a race with async deflation so try again. + assert(AsyncDeflateIdleMonitors, "sanity check"); + continue; + } + ObjectMonitor * const m = omh.om_ptr(); + assert(oopDesc::equals((oop) m->object(), obj), "invariant"); + Thread * const owner = (Thread *) m->_owner; + + // Lock contention and Transactional Lock Elision (TLE) diagnostics + // and observability + // Case: light contention possibly amenable to TLE + // Case: TLE inimical operations such as nested/recursive synchronization + + if (owner == Self) { + m->_recursions++; + return true; + } - if (owner == NULL && Atomic::replace_if_null(Self, &(m->_owner))) { - assert(m->_recursions == 0, "invariant"); - assert(m->_owner == Self, "invariant"); - return true; + // This Java Monitor is inflated so obj's header will never be + // displaced to this thread's BasicLock. Make the displaced header + // non-NULL so this BasicLock is not seen as recursive nor as + // being locked. We do this unconditionally so that this thread's + // BasicLock cannot be mis-interpreted by any stack walkers. For + // performance reasons, stack walkers generally first check for + // Biased Locking in the object's header, the second check is for + // stack-locking in the object's header, the third check is for + // recursive stack-locking in the displaced header in the BasicLock, + // and last are the inflated Java Monitor (ObjectMonitor) checks. + lock->set_displaced_header(markOopDesc::unused_mark()); + + if (owner == NULL && Atomic::replace_if_null(Self, &(m->_owner))) { + assert(m->_recursions == 0, "invariant"); + assert(m->_owner == Self, "invariant"); + return true; + } } + break; } // Note that we could inflate in quick_enter. @@ -327,7 +339,9 @@ } // We have to take the slow-path of possible inflation and then exit. - inflate(THREAD, object, inflate_cause_vm_internal)->exit(true, THREAD); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, object, inflate_cause_vm_internal); + omh.om_ptr()->exit(true, THREAD); } // ----------------------------------------------------------------------------- @@ -336,31 +350,36 @@ // We don't need to use fast path here, because it must have been // failed in the interpreter/compiler code. void ObjectSynchronizer::slow_enter(Handle obj, BasicLock* lock, TRAPS) { - markOop mark = obj->mark(); - assert(!mark->has_bias_pattern(), "should not see bias pattern here"); + bool do_loop = true; + while (do_loop) { + markOop mark = obj->mark(); + assert(!mark->has_bias_pattern(), "should not see bias pattern here"); - if (mark->is_neutral()) { - // Anticipate successful CAS -- the ST of the displaced mark must - // be visible <= the ST performed by the CAS. - lock->set_displaced_header(mark); - if (mark == obj()->cas_set_mark((markOop) lock, mark)) { + if (mark->is_neutral()) { + // Anticipate successful CAS -- the ST of the displaced mark must + // be visible <= the ST performed by the CAS. + lock->set_displaced_header(mark); + if (mark == obj()->cas_set_mark((markOop) lock, mark)) { + return; + } + // Fall through to inflate() ... + } else if (mark->has_locker() && + THREAD->is_lock_owned((address)mark->locker())) { + assert(lock != mark->locker(), "must not re-lock the same lock"); + assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock"); + lock->set_displaced_header(NULL); return; } - // Fall through to inflate() ... - } else if (mark->has_locker() && - THREAD->is_lock_owned((address)mark->locker())) { - assert(lock != mark->locker(), "must not re-lock the same lock"); - assert(lock != (BasicLock*)obj->mark(), "don't relock with same BasicLock"); - lock->set_displaced_header(NULL); - return; - } - // The object header will never be displaced to this lock, - // so it does not matter what the value is, except that it - // must be non-zero to avoid looking like a re-entrant lock, - // and must not look locked either. - lock->set_displaced_header(markOopDesc::unused_mark()); - inflate(THREAD, obj(), inflate_cause_monitor_enter)->enter(THREAD); + // The object header will never be displaced to this lock, + // so it does not matter what the value is, except that it + // must be non-zero to avoid looking like a re-entrant lock, + // and must not look locked either. + lock->set_displaced_header(markOopDesc::unused_mark()); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_monitor_enter); + do_loop = !omh.om_ptr()->enter(THREAD); + } } // This routine is used to handle interpreter/compiler slow case @@ -389,9 +408,10 @@ assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); } - ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal); - - return monitor->complete_exit(THREAD); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_vm_internal); + intptr_t ret_code = omh.om_ptr()->complete_exit(THREAD); + return ret_code; } // NOTE: must use heavy weight monitor to handle complete_exit/reenter() @@ -401,9 +421,12 @@ assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); } - ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_vm_internal); - - monitor->reenter(recursion, THREAD); + bool do_loop = true; + while (do_loop) { + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_vm_internal); + do_loop = !omh.om_ptr()->reenter(recursion, THREAD); + } } // ----------------------------------------------------------------------------- // JNI locks on java objects @@ -415,7 +438,12 @@ assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); } THREAD->set_current_pending_monitor_is_from_java(false); - inflate(THREAD, obj(), inflate_cause_jni_enter)->enter(THREAD); + bool do_loop = true; + while (do_loop) { + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_jni_enter); + do_loop = !omh.om_ptr()->enter(THREAD); + } THREAD->set_current_pending_monitor_is_from_java(true); } @@ -428,7 +456,9 @@ } assert(!obj->mark()->has_bias_pattern(), "biases should be revoked by now"); - ObjectMonitor* monitor = inflate(THREAD, obj, inflate_cause_jni_exit); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj, inflate_cause_jni_exit); + ObjectMonitor * monitor = omh.om_ptr(); // If this thread has locked the object, exit the monitor. Note: can't use // monitor->check(CHECK); must exit even if an exception is pending. if (monitor->check(THREAD)) { @@ -468,7 +498,9 @@ if (millis < 0) { THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); } - ObjectMonitor* monitor = inflate(THREAD, obj(), inflate_cause_wait); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_wait); + ObjectMonitor * monitor = omh.om_ptr(); DTRACE_MONITOR_WAIT_PROBE(monitor, obj(), THREAD, millis); monitor->wait(millis, true, THREAD); @@ -477,7 +509,8 @@ // that's fixed we can uncomment the following line, remove the call // and change this function back into a "void" func. // DTRACE_MONITOR_PROBE(waited, monitor, obj(), THREAD); - return dtrace_waited_probe(monitor, obj, THREAD); + int ret_code = dtrace_waited_probe(monitor, obj, THREAD); + return ret_code; } void ObjectSynchronizer::waitUninterruptibly(Handle obj, jlong millis, TRAPS) { @@ -488,7 +521,9 @@ if (millis < 0) { THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); } - inflate(THREAD, obj(), inflate_cause_wait)->wait(millis, false, THREAD); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_wait); + omh.om_ptr()->wait(millis, false, THREAD); } void ObjectSynchronizer::notify(Handle obj, TRAPS) { @@ -501,7 +536,9 @@ if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) { return; } - inflate(THREAD, obj(), inflate_cause_notify)->notify(THREAD); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_notify); + omh.om_ptr()->notify(THREAD); } // NOTE: see comment of notify() @@ -515,7 +552,9 @@ if (mark->has_locker() && THREAD->is_lock_owned((address)mark->locker())) { return; } - inflate(THREAD, obj(), inflate_cause_notify)->notifyAll(THREAD); + ObjectMonitorHandle omh; + inflate(&omh, THREAD, obj(), inflate_cause_notify); + omh.om_ptr()->notifyAll(THREAD); } // ----------------------------------------------------------------------------- @@ -709,77 +748,92 @@ assert(Universe::verify_in_progress() || DumpSharedSpaces || ((JavaThread *)Self)->thread_state() != _thread_blocked, "invariant"); - ObjectMonitor* monitor = NULL; - markOop temp, test; - intptr_t hash; - markOop mark = ReadStableMark(obj); - - // object should remain ineligible for biased locking - assert(!mark->has_bias_pattern(), "invariant"); - - if (mark->is_neutral()) { - hash = mark->hash(); // this is a normal header - if (hash != 0) { // if it has hash, just return it - return hash; - } - hash = get_next_hash(Self, obj); // allocate a new hash code - temp = mark->copy_set_hash(hash); // merge the hash code into header - // use (machine word version) atomic operation to install the hash - test = obj->cas_set_mark(temp, mark); - if (test == mark) { - return hash; - } - // If atomic operation failed, we must inflate the header - // into heavy weight monitor. We could add more code here - // for fast path, but it does not worth the complexity. - } else if (mark->has_monitor()) { - monitor = mark->monitor(); - temp = monitor->header(); - assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp)); - hash = temp->hash(); - if (hash != 0) { - return hash; - } - // Skip to the following code to reduce code size - } else if (Self->is_lock_owned((address)mark->locker())) { - temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned - assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp)); - hash = temp->hash(); // by current thread, check if the displaced - if (hash != 0) { // header contains hash code - return hash; - } - // WARNING: - // The displaced header in the BasicLock on a thread's stack - // is strictly immutable. It CANNOT be changed in ANY cases. - // So we have to inflate the stack lock into an ObjectMonitor - // even if the current thread owns the lock. The BasicLock on - // a thread's stack can be asynchronously read by other threads - // during an inflate() call so any change to that stack memory - // may not propagate to other threads correctly. - } - - // Inflate the monitor to set hash code - monitor = inflate(Self, obj, inflate_cause_hash_code); - // Load displaced header and check it has hash code - mark = monitor->header(); - assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark)); - hash = mark->hash(); - if (hash == 0) { - hash = get_next_hash(Self, obj); - temp = mark->copy_set_hash(hash); // merge hash code into header - assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp)); - test = Atomic::cmpxchg(temp, monitor->header_addr(), mark); - if (test != mark) { - // The only update to the ObjectMonitor's header/dmw field - // is to merge in the hash code. If someone adds a new usage - // of the header/dmw field, please update this code. - hash = test->hash(); - assert(test->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(test)); - assert(hash != 0, "Trivial unexpected object/monitor header usage."); + while (true) { + ObjectMonitor* monitor = NULL; + markOop temp, test; + intptr_t hash; + markOop mark = ReadStableMark(obj); + + // object should remain ineligible for biased locking + assert(!mark->has_bias_pattern(), "invariant"); + + if (mark->is_neutral()) { + hash = mark->hash(); // this is a normal header + if (hash != 0) { // if it has hash, just return it + return hash; + } + hash = get_next_hash(Self, obj); // allocate a new hash code + temp = mark->copy_set_hash(hash); // merge the hash code into header + // use (machine word version) atomic operation to install the hash + test = obj->cas_set_mark(temp, mark); + if (test == mark) { + return hash; + } + // If atomic operation failed, we must inflate the header + // into heavy weight monitor. We could add more code here + // for fast path, but it does not worth the complexity. + } else if (mark->has_monitor()) { + ObjectMonitorHandle omh; + if (!omh.save_om_ptr(obj, mark)) { + // Lost a race with async deflation so try again. + assert(AsyncDeflateIdleMonitors, "sanity check"); + continue; + } + monitor = omh.om_ptr(); + temp = monitor->header(); + assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp)); + hash = temp->hash(); + if (hash != 0) { + return hash; + } + // Skip to the following code to reduce code size + } else if (Self->is_lock_owned((address)mark->locker())) { + temp = mark->displaced_mark_helper(); // this is a lightweight monitor owned + assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp)); + hash = temp->hash(); // by current thread, check if the displaced + if (hash != 0) { // header contains hash code + return hash; + } + // WARNING: + // The displaced header in the BasicLock on a thread's stack + // is strictly immutable. It CANNOT be changed in ANY cases. + // So we have to inflate the stack lock into an ObjectMonitor + // even if the current thread owns the lock. The BasicLock on + // a thread's stack can be asynchronously read by other threads + // during an inflate() call so any change to that stack memory + // may not propagate to other threads correctly. + } + + // Inflate the monitor to set hash code + ObjectMonitorHandle omh; + inflate(&omh, Self, obj, inflate_cause_hash_code); + monitor = omh.om_ptr(); + // Load displaced header and check it has hash code + mark = monitor->header(); + assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark)); + hash = mark->hash(); + if (hash == 0) { + hash = get_next_hash(Self, obj); + temp = mark->copy_set_hash(hash); // merge hash code into header + assert(temp->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(temp)); + test = Atomic::cmpxchg(temp, monitor->header_addr(), mark); + if (test != mark) { + // The only non-deflation update to the ObjectMonitor's + // header/dmw field is to merge in the hash code. If someone + // adds a new usage of the header/dmw field, please update + // this code. + // ObjectMonitor::install_displaced_markword_in_object() + // does mark the header/dmw field as part of async deflation, + // but that protocol cannot happen now due to the + // ObjectMonitorHandle above. + hash = test->hash(); + assert(test->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(test)); + assert(hash != 0, "Trivial unexpected object/monitor header usage."); + } } + // We finally get the hash + return hash; } - // We finally get the hash - return hash; } // Deprecated -- use FastHashCode() instead. @@ -799,20 +853,28 @@ assert(thread == JavaThread::current(), "Can only be called on current thread"); oop obj = h_obj(); - markOop mark = ReadStableMark(obj); + while (true) { + markOop mark = ReadStableMark(obj); - // Uncontended case, header points to stack - if (mark->has_locker()) { - return thread->is_lock_owned((address)mark->locker()); - } - // Contended case, header points to ObjectMonitor (tagged pointer) - if (mark->has_monitor()) { - ObjectMonitor* monitor = mark->monitor(); - return monitor->is_entered(thread) != 0; + // Uncontended case, header points to stack + if (mark->has_locker()) { + return thread->is_lock_owned((address)mark->locker()); + } + // Contended case, header points to ObjectMonitor (tagged pointer) + if (mark->has_monitor()) { + ObjectMonitorHandle omh; + if (!omh.save_om_ptr(obj, mark)) { + // Lost a race with async deflation so try again. + assert(AsyncDeflateIdleMonitors, "sanity check"); + continue; + } + bool ret_code = omh.om_ptr()->is_entered(thread) != 0; + return ret_code; + } + // Unlocked case, header in place + assert(mark->is_neutral(), "sanity check"); + return false; } - // Unlocked case, header in place - assert(mark->is_neutral(), "sanity check"); - return false; } // Be aware of this method could revoke bias of the lock object. @@ -838,27 +900,37 @@ assert(self == JavaThread::current(), "Can only be called on current thread"); oop obj = h_obj(); - markOop mark = ReadStableMark(obj); - // CASE: stack-locked. Mark points to a BasicLock on the owner's stack. - if (mark->has_locker()) { - return self->is_lock_owned((address)mark->locker()) ? - owner_self : owner_other; - } + while (true) { + markOop mark = ReadStableMark(obj); - // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor. - // The Object:ObjectMonitor relationship is stable as long as we're - // not at a safepoint. - if (mark->has_monitor()) { - void * owner = mark->monitor()->_owner; - if (owner == NULL) return owner_none; - return (owner == self || - self->is_lock_owned((address)owner)) ? owner_self : owner_other; - } + // CASE: stack-locked. Mark points to a BasicLock on the owner's stack. + if (mark->has_locker()) { + return self->is_lock_owned((address)mark->locker()) ? + owner_self : owner_other; + } - // CASE: neutral - assert(mark->is_neutral(), "sanity check"); - return owner_none; // it's unlocked + // CASE: inflated. Mark (tagged pointer) points to an ObjectMonitor. + // The Object:ObjectMonitor relationship is stable as long as we're + // not at a safepoint and AsyncDeflateIdleMonitors is false. + if (mark->has_monitor()) { + ObjectMonitorHandle omh; + if (!omh.save_om_ptr(obj, mark)) { + // Lost a race with async deflation so try again. + assert(AsyncDeflateIdleMonitors, "sanity check"); + continue; + } + ObjectMonitor * monitor = omh.om_ptr(); + void * owner = monitor->_owner; + if (owner == NULL) return owner_none; + return (owner == self || + self->is_lock_owned((address)owner)) ? owner_self : owner_other; + } + + // CASE: neutral + assert(mark->is_neutral(), "sanity check"); + return owner_none; // it's unlocked + } } // FIXME: jvmti should call this @@ -873,33 +945,41 @@ } oop obj = h_obj(); - address owner = NULL; - markOop mark = ReadStableMark(obj); + while (true) { + address owner = NULL; + markOop mark = ReadStableMark(obj); - // Uncontended case, header points to stack - if (mark->has_locker()) { - owner = (address) mark->locker(); - } + // Uncontended case, header points to stack + if (mark->has_locker()) { + owner = (address) mark->locker(); + } - // Contended case, header points to ObjectMonitor (tagged pointer) - else if (mark->has_monitor()) { - ObjectMonitor* monitor = mark->monitor(); - assert(monitor != NULL, "monitor should be non-null"); - owner = (address) monitor->owner(); - } + // Contended case, header points to ObjectMonitor (tagged pointer) + else if (mark->has_monitor()) { + ObjectMonitorHandle omh; + if (!omh.save_om_ptr(obj, mark)) { + // Lost a race with async deflation so try again. + assert(AsyncDeflateIdleMonitors, "sanity check"); + continue; + } + ObjectMonitor* monitor = omh.om_ptr(); + assert(monitor != NULL, "monitor should be non-null"); + owner = (address) monitor->owner(); + } - if (owner != NULL) { - // owning_thread_from_monitor_owner() may also return NULL here - return Threads::owning_thread_from_monitor_owner(t_list, owner); - } + if (owner != NULL) { + // owning_thread_from_monitor_owner() may also return NULL here + return Threads::owning_thread_from_monitor_owner(t_list, owner); + } - // Unlocked case, header in place - // Cannot have assertion since this object may have been - // locked by another thread when reaching here. - // assert(mark->is_neutral(), "sanity check"); + // Unlocked case, header in place + // Cannot have assertion since this object may have been + // locked by another thread when reaching here. + // assert(mark->is_neutral(), "sanity check"); - return NULL; + return NULL; + } } // Visitors ... @@ -910,8 +990,18 @@ assert(block->object() == CHAINMARKER, "must be a block header"); for (int i = _BLOCKSIZE - 1; i > 0; i--) { ObjectMonitor* mid = (ObjectMonitor *)(block + i); - oop object = (oop)mid->object(); - if (object != NULL) { + if (mid->is_active()) { + ObjectMonitorHandle omh(mid); + + if (mid->object() == NULL || + (AsyncDeflateIdleMonitors && mid->_owner == DEFLATER_MARKER)) { + // Only process with closure if the object is set. + // For async deflation, race here if monitor is not owned! + // The above ref_count bump (in ObjectMonitorHandle ctr) + // will cause subsequent async deflation to skip it. + // However, previous or concurrent async deflation is a race. + continue; + } closure->do_monitor(mid); } } @@ -1022,13 +1112,28 @@ } } -ObjectMonitor* ObjectSynchronizer::omAlloc(Thread * Self) { +ObjectMonitor* ObjectSynchronizer::omAlloc(Thread * Self, + const InflateCause cause) { // A large MAXPRIVATE value reduces both list lock contention // and list coherency traffic, but also tends to increase the // number of objectMonitors in circulation as well as the STW // scavenge costs. As usual, we lean toward time in space-time // tradeoffs. const int MAXPRIVATE = 1024; + + if (AsyncDeflateIdleMonitors) { + JavaThread * jt = (JavaThread *)Self; + if (jt->omShouldDeflateIdleMonitors && jt->omInUseCount > 0 && + cause != inflate_cause_vm_internal) { + // Deflate any per-thread idle monitors for this JavaThread if + // this is not an internal inflation. Clean up your own mess. + // (Gibbs Rule 45) Otherwise, skip this cleanup. + // deflate_global_idle_monitors_using_JT() is called by the ServiceThread. + debug_only(jt->check_for_valid_safepoint_state(false);) + ObjectSynchronizer::deflate_per_thread_idle_monitors_using_JT(); + } + } + for (;;) { ObjectMonitor * m; @@ -1043,6 +1148,7 @@ Self->omFreeList = m->FreeNext; Self->omFreeCount--; guarantee(m->object() == NULL, "invariant"); + m->set_allocation_state(ObjectMonitor::New); m->FreeNext = Self->omInUseList; Self->omInUseList = m; Self->omInUseCount++; @@ -1064,8 +1170,15 @@ ObjectMonitor * take = gFreeList; gFreeList = take->FreeNext; guarantee(take->object() == NULL, "invariant"); + if (AsyncDeflateIdleMonitors) { + // Clear any values we allowed to linger during async deflation. + take->_header = NULL; + take->set_owner(NULL); + take->_contentions = 0; + } guarantee(!take->is_busy(), "invariant"); take->Recycle(); + assert(take->is_free(), "invariant"); omRelease(Self, take, false); } Thread::muxRelease(&gListLock); @@ -1118,6 +1231,7 @@ for (int i = 1; i < _BLOCKSIZE; i++) { temp[i].FreeNext = (ObjectMonitor *)&temp[i+1]; + assert(temp[i].is_free(), "invariant"); } // terminate the last monitor as the end of list @@ -1160,13 +1274,15 @@ // // Key constraint: all ObjectMonitors on a thread's free list and the global // free list must have their object field set to null. This prevents the -// scavenger -- deflate_monitor_list() -- from reclaiming them. +// scavenger -- deflate_monitor_list() or deflate_monitor_list_using_JT() +// -- from reclaiming them while we are trying to release them. void ObjectSynchronizer::omRelease(Thread * Self, ObjectMonitor * m, bool fromPerThreadAlloc) { guarantee(m->header() == NULL, "invariant"); guarantee(m->object() == NULL, "invariant"); guarantee(((m->is_busy()|m->_recursions) == 0), "freeing in-use monitor"); + m->set_allocation_state(ObjectMonitor::Free); // Remove from omInUseList if (fromPerThreadAlloc) { ObjectMonitor* cur_mid_in_use = NULL; @@ -1189,6 +1305,7 @@ // FreeNext is used for both omInUseList and omFreeList, so clear old before setting new m->FreeNext = Self->omFreeList; + guarantee(m->is_free(), "invariant"); Self->omFreeList = m; Self->omFreeCount++; } @@ -1212,6 +1329,10 @@ // either via Thread::oops_do() (if safepoint happens before omFlush()) or via // ObjectSynchronizer::oops_do() (if it happens after omFlush() and the thread's // monitors have been transferred to the global in-use list). +// +// With AsyncDeflateIdleMonitors, deflate_global_idle_monitors_using_JT() +// and deflate_per_thread_idle_monitors_using_JT() (in another thread) can +// run at the same time as omFlush() so we have to be careful. void ObjectSynchronizer::omFlush(Thread * Self) { ObjectMonitor * list = Self->omFreeList; // Null-terminated SLL @@ -1231,7 +1352,7 @@ s->set_owner(NULL); // redundant but good hygiene } guarantee(tail != NULL, "invariant"); - assert(Self->omFreeCount == tally, "free-count off"); + ADIM_guarantee(Self->omFreeCount == tally, "free-count off"); Self->omFreeList = NULL; Self->omFreeCount = 0; } @@ -1248,9 +1369,10 @@ for (cur_om = inUseList; cur_om != NULL; cur_om = cur_om->FreeNext) { inUseTail = cur_om; inUseTally++; + ADIM_guarantee(cur_om->is_active(), "invariant"); } guarantee(inUseTail != NULL, "invariant"); - assert(Self->omInUseCount == inUseTally, "in-use count off"); + ADIM_guarantee(Self->omInUseCount == inUseTally, "in-use count off"); Self->omInUseList = NULL; Self->omInUseCount = 0; } @@ -1298,19 +1420,28 @@ } // Fast path code shared by multiple functions -void ObjectSynchronizer::inflate_helper(oop obj) { - markOop mark = obj->mark(); - if (mark->has_monitor()) { - assert(ObjectSynchronizer::verify_objmon_isinpool(mark->monitor()), "monitor is invalid"); - assert(mark->monitor()->header()->is_neutral(), "monitor must record a good object header"); +void ObjectSynchronizer::inflate_helper(ObjectMonitorHandle * omh_p, oop obj) { + while (true) { + markOop mark = obj->mark(); + if (mark->has_monitor()) { + if (!omh_p->save_om_ptr(obj, mark)) { + // Lost a race with async deflation so try again. + assert(AsyncDeflateIdleMonitors, "sanity check"); + continue; + } + ObjectMonitor * monitor = omh_p->om_ptr(); + assert(ObjectSynchronizer::verify_objmon_isinpool(monitor), "monitor is invalid"); + markOop dmw = monitor->header(); + assert(dmw->is_neutral(), "sanity check: header=" INTPTR_FORMAT, p2i(dmw)); + return; + } + inflate(omh_p, Thread::current(), obj, inflate_cause_vm_internal); return; } - inflate(Thread::current(), obj, inflate_cause_vm_internal); } -ObjectMonitor* ObjectSynchronizer::inflate(Thread * Self, - oop object, - const InflateCause cause) { +void ObjectSynchronizer::inflate(ObjectMonitorHandle * omh_p, Thread * Self, + oop object, const InflateCause cause) { // Inflate mutates the heap ... // Relaxing assertion for bug 6320749. assert(Universe::verify_in_progress() || @@ -1331,12 +1462,17 @@ // CASE: inflated if (mark->has_monitor()) { - ObjectMonitor * inf = mark->monitor(); + if (!omh_p->save_om_ptr(object, mark)) { + // Lost a race with async deflation so try again. + assert(AsyncDeflateIdleMonitors, "sanity check"); + continue; + } + ObjectMonitor * inf = omh_p->om_ptr(); markOop dmw = inf->header(); assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw)); assert(oopDesc::equals((oop) inf->object(), object), "invariant"); assert(ObjectSynchronizer::verify_objmon_isinpool(inf), "monitor is invalid"); - return inf; + return; } // CASE: inflation in progress - inflating over a stack-lock. @@ -1372,7 +1508,18 @@ LogStreamHandle(Trace, monitorinflation) lsh; if (mark->has_locker()) { - ObjectMonitor * m = omAlloc(Self); + ObjectMonitor * m; + if (!AsyncDeflateIdleMonitors || cause == inflate_cause_vm_internal) { + // If !AsyncDeflateIdleMonitors or if an internal inflation, then + // we won't stop for a potential safepoint in omAlloc. + m = omAlloc(Self, cause); + } else { + // If AsyncDeflateIdleMonitors and not an internal inflation, then + // we may stop for a safepoint in omAlloc() so protect object. + Handle h_obj(Self, object); + m = omAlloc(Self, cause); + object = h_obj(); // Refresh object. + } // Optimistically prepare the objectmonitor - anticipate successful CAS // We do this before the CAS in order to minimize the length of time // in which INFLATING appears in the mark. @@ -1420,7 +1567,7 @@ markOop dmw = mark->displaced_mark_helper(); // Catch if the object's header is not neutral (not locked and // not marked is what we care about here). - assert(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw)); + ADIM_guarantee(dmw->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(dmw)); // Setup monitor fields to proper values -- prepare the monitor m->set_header(dmw); @@ -1434,6 +1581,10 @@ m->set_object(object); // TODO-FIXME: assert BasicLock->dhw != 0. + omh_p->set_om_ptr(m); + assert(m->is_new(), "freshly allocated monitor must be new"); + m->set_allocation_state(ObjectMonitor::Old); + // Must preserve store ordering. The monitor state must // be stable at the time of publishing the monitor address. guarantee(object->mark() == markOopDesc::INFLATING(), "invariant"); @@ -1451,7 +1602,8 @@ if (event.should_commit()) { post_monitor_inflate_event(&event, object, cause); } - return m; + ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free"); + return; } // CASE: neutral @@ -1466,8 +1618,19 @@ // Catch if the object's header is not neutral (not locked and // not marked is what we care about here). - assert(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark)); - ObjectMonitor * m = omAlloc(Self); + ADIM_guarantee(mark->is_neutral(), "invariant: header=" INTPTR_FORMAT, p2i(mark)); + ObjectMonitor * m; + if (!AsyncDeflateIdleMonitors || cause == inflate_cause_vm_internal) { + // If !AsyncDeflateIdleMonitors or if an internal inflation, then + // we won't stop for a potential safepoint in omAlloc. + m = omAlloc(Self, cause); + } else { + // If AsyncDeflateIdleMonitors and not an internal inflation, then + // we may stop for a safepoint in omAlloc() so protect object. + Handle h_obj(Self, object); + m = omAlloc(Self, cause); + object = h_obj(); // Refresh object. + } // prepare m for installation - set monitor to initial state m->Recycle(); m->set_header(mark); @@ -1477,10 +1640,16 @@ m->_Responsible = NULL; m->_SpinDuration = ObjectMonitor::Knob_SpinLimit; // consider: keep metastats by type/class + omh_p->set_om_ptr(m); + assert(m->is_new(), "freshly allocated monitor must be new"); + m->set_allocation_state(ObjectMonitor::Old); + if (object->cas_set_mark(markOopDesc::encode(m), mark) != mark) { m->set_header(NULL); m->set_object(NULL); m->Recycle(); + omh_p->set_om_ptr(NULL); + // omRelease() will reset the allocation state omRelease(Self, m, true); m = NULL; continue; @@ -1501,7 +1670,8 @@ if (event.should_commit()) { post_monitor_inflate_event(&event, object, cause); } - return m; + ADIM_guarantee(!m->is_free(), "inflated monitor to be returned cannot be free"); + return; } } @@ -1527,6 +1697,26 @@ // which in turn can mean large(r) numbers of ObjectMonitors in circulation. // This is an unfortunate aspect of this design. +void ObjectSynchronizer::do_safepoint_work(DeflateMonitorCounters* _counters) { + assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); + + // The per-thread in-use lists are handled in + // ParallelSPCleanupThreadClosure::do_thread(). + + if (!AsyncDeflateIdleMonitors || is_cleanup_requested()) { + // Use the older mechanism for the global in-use list or + // if a special cleanup has been requested. + ObjectSynchronizer::deflate_idle_monitors(_counters); + return; + } + + log_debug(monitorinflation)("requesting deflation of idle monitors."); + // Request deflation of global idle monitors by the ServiceThread: + _gOmShouldDeflateIdleMonitors = true; + MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); + Service_lock->notify_all(); +} + // Deflate a single monitor if not in-use // Return true if deflated, false if in-use bool ObjectSynchronizer::deflate_monitor(ObjectMonitor* mid, oop obj, @@ -1564,6 +1754,7 @@ assert(mid->object() == NULL, "invariant: object=" INTPTR_FORMAT, p2i(mid->object())); + assert(mid->is_free(), "invariant"); // Move the object to the working free list defined by freeHeadp, freeTailp if (*freeHeadp == NULL) *freeHeadp = mid; @@ -1578,6 +1769,148 @@ return deflated; } +// Deflate the specified ObjectMonitor if not in-use using a JavaThread. +// Returns true if it was deflated and false otherwise. +// +// The async deflation protocol sets owner to DEFLATER_MARKER and +// makes contentions negative as signals to contending threads that +// an async deflation is in progress. There are a number of checks +// as part of the protocol to make sure that the calling thread has +// not lost the race to a contending thread. +// +// The ObjectMonitor has been successfully async deflated when: +// (owner == DEFLATER_MARKER && contentions < 0). Contending threads +// that see those values know to retry their operation. +// +bool ObjectSynchronizer::deflate_monitor_using_JT(ObjectMonitor* mid, + ObjectMonitor** freeHeadp, + ObjectMonitor** freeTailp) { + assert(AsyncDeflateIdleMonitors, "sanity check"); + assert(Thread::current()->is_Java_thread(), "precondition"); + // A newly allocated ObjectMonitor should not be seen here so we + // avoid an endless inflate/deflate cycle. + assert(mid->is_old(), "must be old: allocation_state=%d", + (int) mid->allocation_state()); + + if (mid->is_busy() || mid->ref_count() != 0) { + // Easy checks are first - the ObjectMonitor is busy or ObjectMonitor* + // is in use so no deflation. + return false; + } + + if (Atomic::replace_if_null(DEFLATER_MARKER, &(mid->_owner))) { + // ObjectMonitor is not owned by another thread. Our setting + // owner to DEFLATER_MARKER forces any contending thread through + // the slow path. This is just the first part of the async + // deflation dance. + + if (mid->_waiters != 0 || mid->ref_count() != 0) { + // Another thread has raced to enter the ObjectMonitor after + // mid->is_busy() above and has already waited on it which + // makes it busy so no deflation. Or the ObjectMonitor* is + // in use for some other operation like inflate(). Restore + // owner to NULL if it is still DEFLATER_MARKER. + Atomic::cmpxchg((void*)NULL, &mid->_owner, DEFLATER_MARKER); + return false; + } + + if (Atomic::cmpxchg(-max_jint, &mid->_contentions, (jint)0) == 0) { + // Make contentions negative to force racing threads to retry. + // This is the second part of the async deflation dance. + + if (mid->_owner == DEFLATER_MARKER && mid->ref_count() == 0) { + // If owner is still DEFLATER_MARKER, then we have successfully + // signaled any racing threads to retry. If it is not, then we + // have lost the race to an entering thread and the ObjectMonitor + // is now busy. If the ObjectMonitor* is in use, then we have + // lost that race. This is the third and final part of the async + // deflation dance. + // Note: This owner check solves the ABA problem with contentions + // where another thread acquired the ObjectMonitor, finished + // using it and restored the contentions to zero. + // Note: This ref_count check solves the race with save_om_ptr() + // where its ref_count increment happens after the first ref_count + // check in this function and before contentions is made negative. + + // Sanity checks for the races: + guarantee(mid->_waiters == 0, "must be 0: waiters=%d", mid->_waiters); + guarantee(mid->_cxq == NULL, "must be no contending threads: cxq=" + INTPTR_FORMAT, p2i(mid->_cxq)); + guarantee(mid->_EntryList == NULL, + "must be no entering threads: EntryList=" INTPTR_FORMAT, + p2i(mid->_EntryList)); + + const oop obj = (oop) mid->object(); + if (log_is_enabled(Trace, monitorinflation)) { + ResourceMark rm; + log_trace(monitorinflation)("deflate_monitor_using_JT: " + "object=" INTPTR_FORMAT ", mark=" + INTPTR_FORMAT ", type='%s'", + p2i(obj), p2i(obj->mark()), + obj->klass()->external_name()); + } + + // Install the old mark word if nobody else has already done it. + mid->install_displaced_markword_in_object(obj); + mid->clear_using_JT(); + + assert(mid->object() == NULL, "must be NULL: object=" INTPTR_FORMAT, + p2i(mid->object())); + assert(mid->is_free(), "must be free: allocation_state=%d", + (int) mid->allocation_state()); + + // Move the deflated ObjectMonitor to the working free list + // defined by freeHeadp and freeTailp. + if (*freeHeadp == NULL) { + // First one on the list. + *freeHeadp = mid; + } + if (*freeTailp != NULL) { + // We append to the list so the caller can use mid->FreeNext + // to fix the linkages in its context. + ObjectMonitor * prevtail = *freeTailp; + // Should have been cleaned up by the caller: + assert(prevtail->FreeNext == NULL, "must be NULL: FreeNext=" + INTPTR_FORMAT, p2i(prevtail->FreeNext)); + prevtail->FreeNext = mid; + } + *freeTailp = mid; + + // At this point, mid->FreeNext still refers to its current + // value and another ObjectMonitor's FreeNext field still + // refers to this ObjectMonitor. Those linkages have to be + // cleaned up by the caller who has the complete context. + + // We leave owner == DEFLATER_MARKER and contentions < 0 + // to force any racing threads to retry. + return true; // Success, ObjectMonitor has been deflated. + } + + // The owner was changed from DEFLATER_MARKER or ObjectMonitor* + // is in use so we lost the race since the ObjectMonitor is now + // busy. + + // Restore owner to NULL if it is still DEFLATER_MARKER: + Atomic::cmpxchg((void*)NULL, &mid->_owner, DEFLATER_MARKER); + + // Add back max_jint to restore the contentions field to its + // proper value (which may not be what we saw above): + Atomic::add(max_jint, &mid->_contentions); + + assert(mid->_contentions >= 0, "must not be negative: contentions=%d", + mid->_contentions); + } + + // The contentions was no longer 0 so we lost the race since the + // ObjectMonitor is now busy. + assert(mid->_owner != DEFLATER_MARKER, "must not be DEFLATER_MARKER"); + } + + // The owner field is no longer NULL so we lost the race since the + // ObjectMonitor is now busy. + return false; +} + // Walk a given monitor list, and deflate idle monitors // The given list could be a per-thread list or a global list // Caller acquires gListLock as needed. @@ -1621,6 +1954,82 @@ return deflated_count; } +// Walk a given ObjectMonitor list and deflate idle ObjectMonitors using +// a JavaThread. Returns the number of deflated ObjectMonitors. The given +// list could be a per-thread in-use list or the global in-use list. +// Caller acquires gListLock as appropriate. If a safepoint has started, +// then we save state via savedMidInUsep and return to the caller to +// honor the safepoint. +// +int ObjectSynchronizer::deflate_monitor_list_using_JT(ObjectMonitor** listHeadp, + ObjectMonitor** freeHeadp, + ObjectMonitor** freeTailp, + ObjectMonitor** savedMidInUsep) { + assert(AsyncDeflateIdleMonitors, "sanity check"); + assert(Thread::current()->is_Java_thread(), "precondition"); + + ObjectMonitor* mid; + ObjectMonitor* next; + ObjectMonitor* cur_mid_in_use = NULL; + int deflated_count = 0; + + if (*savedMidInUsep == NULL) { + // No saved state so start at the beginning. + mid = *listHeadp; + } else { + // We're restarting after a safepoint so restore the necessary state + // before we resume. + cur_mid_in_use = *savedMidInUsep; + mid = cur_mid_in_use->FreeNext; + } + while (mid != NULL) { + // Only try to deflate if there is an associated Java object and if + // mid is old (is not newly allocated and is not newly freed). + if (mid->object() != NULL && mid->is_old() && + deflate_monitor_using_JT(mid, freeHeadp, freeTailp)) { + // Deflation succeeded so update the in-use list. + if (mid == *listHeadp) { + *listHeadp = mid->FreeNext; + } else if (cur_mid_in_use != NULL) { + // Maintain the current in-use list. + cur_mid_in_use->FreeNext = mid->FreeNext; + } + next = mid->FreeNext; + mid->FreeNext = NULL; + // At this point mid is disconnected from the in-use list + // and is the current tail in the freeHeadp list. + mid = next; + deflated_count++; + } else { + // mid is considered in-use if it does not have an associated + // Java object or mid is not old or deflation did not succeed. + // A mid->is_new() node can be seen here when it is freshly + // returned by omAlloc() (and skips the deflation code path). + // A mid->is_old() node can be seen here when deflation failed. + // A mid->is_free() node can be seen here when a fresh node from + // omAlloc() is released by omRelease() due to losing the race + // in inflate(). + + cur_mid_in_use = mid; + mid = mid->FreeNext; + + if (SafepointSynchronize::is_synchronizing() && + cur_mid_in_use != *listHeadp && cur_mid_in_use->is_old()) { + // If a safepoint has started and cur_mid_in_use is not the list + // head and is old, then it is safe to use as saved state. Return + // to the caller so gListLock can be dropped as appropriate + // before blocking. + *savedMidInUsep = cur_mid_in_use; + return deflated_count; + } + } + } + // We finished the list without a safepoint starting so there's + // no need to save state. + *savedMidInUsep = NULL; + return deflated_count; +} + void ObjectSynchronizer::prepare_deflate_idle_monitors(DeflateMonitorCounters* counters) { counters->nInuse = 0; // currently associated with objects counters->nInCirculation = 0; // extant @@ -1631,6 +2040,15 @@ void ObjectSynchronizer::deflate_idle_monitors(DeflateMonitorCounters* counters) { assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); + + if (AsyncDeflateIdleMonitors) { + // Nothing to do when global idle ObjectMonitors are deflated using + // a JavaThread unless a special cleanup has been requested. + if (!is_cleanup_requested()) { + return; + } + } + bool deflated = false; ObjectMonitor * freeHeadp = NULL; // Local SLL of scavenged monitors @@ -1683,14 +2101,147 @@ } } +// Deflate global idle ObjectMonitors using a JavaThread. +// +void ObjectSynchronizer::deflate_global_idle_monitors_using_JT() { + assert(AsyncDeflateIdleMonitors, "sanity check"); + assert(Thread::current()->is_Java_thread(), "precondition"); + JavaThread * self = JavaThread::current(); + + _gOmShouldDeflateIdleMonitors = false; + + deflate_common_idle_monitors_using_JT(true /* is_global */, self); +} + +// Deflate per-thread idle ObjectMonitors using a JavaThread. +// +void ObjectSynchronizer::deflate_per_thread_idle_monitors_using_JT() { + assert(AsyncDeflateIdleMonitors, "sanity check"); + assert(Thread::current()->is_Java_thread(), "precondition"); + JavaThread * self = JavaThread::current(); + + self->omShouldDeflateIdleMonitors = false; + + deflate_common_idle_monitors_using_JT(false /* !is_global */, self); +} + +// Deflate global or per-thread idle ObjectMonitors using a JavaThread. +// +void ObjectSynchronizer::deflate_common_idle_monitors_using_JT(bool is_global, JavaThread * self) { + int deflated_count = 0; + ObjectMonitor * freeHeadp = NULL; // Local SLL of scavenged ObjectMonitors + ObjectMonitor * freeTailp = NULL; + ObjectMonitor * savedMidInUsep = NULL; + elapsedTimer timer; + + if (log_is_enabled(Info, monitorinflation)) { + timer.start(); + } + + if (is_global) { + Thread::muxAcquire(&gListLock, "deflate_global_idle_monitors_using_JT(1)"); + OM_PERFDATA_OP(MonExtant, set_value(gOmInUseCount)); + } else { + OM_PERFDATA_OP(MonExtant, inc(self->omInUseCount)); + } + + do { + int local_deflated_count; + if (is_global) { + local_deflated_count = deflate_monitor_list_using_JT((ObjectMonitor **)&gOmInUseList, &freeHeadp, &freeTailp, &savedMidInUsep); + gOmInUseCount -= local_deflated_count; + } else { + local_deflated_count = deflate_monitor_list_using_JT(self->omInUseList_addr(), &freeHeadp, &freeTailp, &savedMidInUsep); + self->omInUseCount -= local_deflated_count; + } + deflated_count += local_deflated_count; + + if (freeHeadp != NULL) { + // Move the scavenged ObjectMonitors to the global free list. + guarantee(freeTailp != NULL && local_deflated_count > 0, "freeTailp=" INTPTR_FORMAT ", local_deflated_count=%d", p2i(freeTailp), local_deflated_count); + assert(freeTailp->FreeNext == NULL, "invariant"); + + if (!is_global) { + Thread::muxAcquire(&gListLock, "deflate_per_thread_idle_monitors_using_JT(2)"); + } + // Constant-time list splice - prepend scavenged segment to gFreeList. + freeTailp->FreeNext = gFreeList; + gFreeList = freeHeadp; + + gMonitorFreeCount += local_deflated_count; + OM_PERFDATA_OP(Deflations, inc(local_deflated_count)); + if (!is_global) { + Thread::muxRelease(&gListLock); + } + } + + if (savedMidInUsep != NULL) { + // deflate_monitor_list_using_JT() detected a safepoint starting. + if (is_global) { + Thread::muxRelease(&gListLock); + } + timer.stop(); + { + if (is_global) { + log_debug(monitorinflation)("pausing deflation of global idle monitors for a safepoint."); + } else { + log_debug(monitorinflation)("jt=" INTPTR_FORMAT ": pausing deflation of per-thread idle monitors for a safepoint.", p2i(self)); + } + assert(SafepointSynchronize::is_synchronizing(), "sanity check"); + ThreadBlockInVM blocker(self); + } + // Prepare for another loop after the safepoint. + freeHeadp = NULL; + freeTailp = NULL; + if (log_is_enabled(Info, monitorinflation)) { + timer.start(); + } + if (is_global) { + Thread::muxAcquire(&gListLock, "deflate_global_idle_monitors_using_JT(3)"); + } + } + } while (savedMidInUsep != NULL); + if (is_global) { + Thread::muxRelease(&gListLock); + } + timer.stop(); + + LogStreamHandle(Debug, monitorinflation) lsh_debug; + LogStreamHandle(Info, monitorinflation) lsh_info; + LogStream * ls = NULL; + if (log_is_enabled(Debug, monitorinflation)) { + ls = &lsh_debug; + } else if (deflated_count != 0 && log_is_enabled(Info, monitorinflation)) { + ls = &lsh_info; + } + if (ls != NULL) { + if (is_global) { + ls->print_cr("async-deflating global idle monitors, %3.7f secs, %d monitors", timer.seconds(), deflated_count); + } else { + ls->print_cr("jt=" INTPTR_FORMAT ": async-deflating per-thread idle monitors, %3.7f secs, %d monitors", p2i(self), timer.seconds(), deflated_count); + } + } +} + void ObjectSynchronizer::finish_deflate_idle_monitors(DeflateMonitorCounters* counters) { // Report the cumulative time for deflating each thread's idle // monitors. Note: if the work is split among more than one // worker thread, then the reported time will likely be more // than a beginning to end measurement of the phase. + // Note: AsyncDeflateIdleMonitors only deflates per-thread idle + // monitors at a safepoint when a special cleanup has been requested. log_info(safepoint, cleanup)("deflating per-thread idle monitors, %3.7f secs, monitors=%d", counters->perThreadTimes, counters->perThreadScavenged); - gMonitorFreeCount += counters->nScavenged; + bool needs_special_cleanup = is_cleanup_requested(); + if (!AsyncDeflateIdleMonitors || needs_special_cleanup) { + // AsyncDeflateIdleMonitors does not use these counters unless + // there is a special cleanup request. + + gMonitorFreeCount += counters->nScavenged; + + OM_PERFDATA_OP(Deflations, inc(counters->nScavenged)); + OM_PERFDATA_OP(MonExtant, set_value(counters->nInCirculation)); + } if (log_is_enabled(Debug, monitorinflation)) { // exit_globals()'s call to audit_and_print_stats() is done @@ -1705,17 +2256,28 @@ } ForceMonitorScavenge = 0; // Reset - - OM_PERFDATA_OP(Deflations, inc(counters->nScavenged)); - OM_PERFDATA_OP(MonExtant, set_value(counters->nInCirculation)); - GVars.stwRandom = os::random(); GVars.stwCycle++; + if (needs_special_cleanup) { + set_is_cleanup_requested(false); // special clean up is done + } } void ObjectSynchronizer::deflate_thread_local_monitors(Thread* thread, DeflateMonitorCounters* counters) { assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); + if (AsyncDeflateIdleMonitors) { + if (!is_cleanup_requested()) { + // Mark the JavaThread for idle monitor cleanup if a special + // cleanup has NOT been requested. + if (thread->omInUseCount > 0) { + // This JavaThread is using monitors so mark it. + thread->omShouldDeflateIdleMonitors = true; + } + return; + } + } + ObjectMonitor * freeHeadp = NULL; // Local SLL of scavenged monitors ObjectMonitor * freeTailp = NULL; elapsedTimer timer; @@ -1924,7 +2486,8 @@ // Check a free monitor entry; log any errors. void ObjectSynchronizer::chk_free_entry(JavaThread * jt, ObjectMonitor * n, outputStream * out, int *error_cnt_p) { - if (n->is_busy()) { + if ((!AsyncDeflateIdleMonitors && n->is_busy()) || + (AsyncDeflateIdleMonitors && n->is_busy_async())) { if (jt != NULL) { out->print_cr("ERROR: jt=" INTPTR_FORMAT ", monitor=" INTPTR_FORMAT ": free per-thread monitor must not be busy.", p2i(jt), @@ -1941,12 +2504,13 @@ ": free per-thread monitor must have NULL _header " "field: _header=" INTPTR_FORMAT, p2i(jt), p2i(n), p2i(n->header())); - } else { + *error_cnt_p = *error_cnt_p + 1; + } else if (!AsyncDeflateIdleMonitors) { out->print_cr("ERROR: monitor=" INTPTR_FORMAT ": free global monitor " "must have NULL _header field: _header=" INTPTR_FORMAT, p2i(n), p2i(n->header())); + *error_cnt_p = *error_cnt_p + 1; } - *error_cnt_p = *error_cnt_p + 1; } if (n->object() != NULL) { if (jt != NULL) { @@ -2113,16 +2677,17 @@ if (gOmInUseCount > 0) { out->print_cr("In-use global monitor info:"); out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)"); - out->print_cr("%18s %s %18s %18s", - "monitor", "BHL", "object", "object type"); - out->print_cr("================== === ================== =================="); + out->print_cr("%18s %s %7s %18s %18s", + "monitor", "BHL", "ref_cnt", "object", "object type"); + out->print_cr("================== === ======= ================== =================="); for (ObjectMonitor * n = gOmInUseList; n != NULL; n = n->FreeNext) { const oop obj = (oop) n->object(); const markOop mark = n->header(); ResourceMark rm; - out->print_cr(INTPTR_FORMAT " %d%d%d " INTPTR_FORMAT " %s", p2i(n), - n->is_busy() != 0, mark->hash() != 0, n->owner() != NULL, - p2i(obj), obj->klass()->external_name()); + out->print_cr(INTPTR_FORMAT " %d%d%d %7d " INTPTR_FORMAT " %s", + p2i(n), n->is_busy() != 0, mark->hash() != 0, + n->owner() != NULL, (int)n->ref_count(), p2i(obj), + obj->klass()->external_name()); } } @@ -2132,18 +2697,18 @@ out->print_cr("In-use per-thread monitor info:"); out->print_cr("(B -> is_busy, H -> has hash code, L -> lock status)"); - out->print_cr("%18s %18s %s %18s %18s", - "jt", "monitor", "BHL", "object", "object type"); - out->print_cr("================== ================== === ================== =================="); + out->print_cr("%18s %18s %s %7s %18s %18s", + "jt", "monitor", "BHL", "ref_cnt", "object", "object type"); + out->print_cr("================== ================== === ======= ================== =================="); for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) { for (ObjectMonitor * n = jt->omInUseList; n != NULL; n = n->FreeNext) { const oop obj = (oop) n->object(); const markOop mark = n->header(); ResourceMark rm; - out->print_cr(INTPTR_FORMAT " " INTPTR_FORMAT " %d%d%d " INTPTR_FORMAT - " %s", p2i(jt), p2i(n), n->is_busy() != 0, - mark->hash() != 0, n->owner() != NULL, p2i(obj), - obj->klass()->external_name()); + out->print_cr(INTPTR_FORMAT " " INTPTR_FORMAT " %d%d%d %7d " + INTPTR_FORMAT " %s", p2i(jt), p2i(n), n->is_busy() != 0, + mark->hash() != 0, n->owner() != NULL, (int)n->ref_count(), + p2i(obj), obj->klass()->external_name()); } }