duke@435: /* phh@3481: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp" stefank@2314: #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp" stefank@2314: #include "memory/genCollectedHeap.hpp" stefank@2314: #include "oops/instanceRefKlass.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/init.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: #include "runtime/java.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/mutexLocker.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "runtime/vmThread.hpp" duke@435: duke@435: // ======= Concurrent Mark Sweep Thread ======== duke@435: duke@435: // The CMS thread is created when Concurrent Mark Sweep is used in the duke@435: // older of two generations in a generational memory system. duke@435: duke@435: ConcurrentMarkSweepThread* duke@435: ConcurrentMarkSweepThread::_cmst = NULL; duke@435: CMSCollector* ConcurrentMarkSweepThread::_collector = NULL; duke@435: bool ConcurrentMarkSweepThread::_should_terminate = false; duke@435: int ConcurrentMarkSweepThread::_CMS_flag = CMS_nil; duke@435: duke@435: volatile jint ConcurrentMarkSweepThread::_pending_yields = 0; duke@435: volatile jint ConcurrentMarkSweepThread::_pending_decrements = 0; duke@435: ysr@2647: volatile jint ConcurrentMarkSweepThread::_icms_disabled = 0; duke@435: volatile bool ConcurrentMarkSweepThread::_should_run = false; duke@435: // When icms is enabled, the icms thread is stopped until explicitly duke@435: // started. duke@435: volatile bool ConcurrentMarkSweepThread::_should_stop = true; duke@435: duke@435: SurrogateLockerThread* duke@435: ConcurrentMarkSweepThread::_slt = NULL; duke@435: SurrogateLockerThread::SLT_msg_type duke@435: ConcurrentMarkSweepThread::_sltBuffer = SurrogateLockerThread::empty; duke@435: Monitor* duke@435: ConcurrentMarkSweepThread::_sltMonitor = NULL; duke@435: duke@435: ConcurrentMarkSweepThread::ConcurrentMarkSweepThread(CMSCollector* collector) duke@435: : ConcurrentGCThread() { duke@435: assert(UseConcMarkSweepGC, "UseConcMarkSweepGC should be set"); duke@435: assert(_cmst == NULL, "CMS thread already created"); duke@435: _cmst = this; duke@435: assert(_collector == NULL, "Collector already set"); duke@435: _collector = collector; duke@435: duke@435: set_name("Concurrent Mark-Sweep GC Thread"); duke@435: duke@435: if (os::create_thread(this, os::cgc_thread)) { phh@3481: // An old comment here said: "Priority should be just less phh@3481: // than that of VMThread". Since the VMThread runs at phh@3481: // NearMaxPriority, the old comment was inaccurate, but phh@3481: // changing the default priority to NearMaxPriority-1 phh@3481: // could change current behavior, so the default of phh@3481: // NearMaxPriority stays in place. phh@3481: // phh@3481: // Note that there's a possibility of the VMThread phh@3481: // starving if UseCriticalCMSThreadPriority is on. phh@3481: // That won't happen on Solaris for various reasons, phh@3481: // but may well happen on non-Solaris platforms. phh@3481: int native_prio; phh@3481: if (UseCriticalCMSThreadPriority) { phh@3481: native_prio = os::java_to_os_priority[CriticalPriority]; phh@3481: } else { phh@3481: native_prio = os::java_to_os_priority[NearMaxPriority]; phh@3481: } phh@3481: os::set_native_priority(this, native_prio); phh@3481: duke@435: if (!DisableStartThread) { duke@435: os::start_thread(this); duke@435: } duke@435: } duke@435: _sltMonitor = SLT_lock; ysr@2647: assert(!CMSIncrementalMode || icms_is_enabled(), "Error"); duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::run() { duke@435: assert(this == cmst(), "just checking"); duke@435: duke@435: this->record_stack_base_and_size(); duke@435: this->initialize_thread_local_storage(); duke@435: this->set_active_handles(JNIHandleBlock::allocate_block()); duke@435: // From this time Thread::current() should be working. duke@435: assert(this == Thread::current(), "just checking"); duke@435: if (BindCMSThreadToCPU && !os::bind_to_processor(CPUForCMSThread)) { duke@435: warning("Couldn't bind CMS thread to processor %u", CPUForCMSThread); duke@435: } duke@435: // Wait until Universe::is_fully_initialized() duke@435: { duke@435: CMSLoopCountWarn loopX("CMS::run", "waiting for " duke@435: "Universe::is_fully_initialized()", 2); duke@435: MutexLockerEx x(CGC_lock, true); duke@435: set_CMS_flag(CMS_cms_wants_token); duke@435: // Wait until Universe is initialized and all initialization is completed. duke@435: while (!is_init_completed() && !Universe::is_fully_initialized() && duke@435: !_should_terminate) { duke@435: CGC_lock->wait(true, 200); duke@435: loopX.tick(); duke@435: } duke@435: // Wait until the surrogate locker thread that will do duke@435: // pending list locking on our behalf has been created. duke@435: // We cannot start the SLT thread ourselves since we need duke@435: // to be a JavaThread to do so. duke@435: CMSLoopCountWarn loopY("CMS::run", "waiting for SLT installation", 2); duke@435: while (_slt == NULL && !_should_terminate) { duke@435: CGC_lock->wait(true, 200); duke@435: loopY.tick(); duke@435: } duke@435: clear_CMS_flag(CMS_cms_wants_token); duke@435: } duke@435: duke@435: while (!_should_terminate) { duke@435: sleepBeforeNextCycle(); duke@435: if (_should_terminate) break; sla@5237: GCCause::Cause cause = _collector->_full_gc_requested ? sla@5237: _collector->_full_gc_cause : GCCause::_cms_concurrent_mark; sla@5237: _collector->collect_in_background(false, cause); duke@435: } duke@435: assert(_should_terminate, "just checking"); duke@435: // Check that the state of any protocol for synchronization duke@435: // between background (CMS) and foreground collector is "clean" duke@435: // (i.e. will not potentially block the foreground collector, duke@435: // requiring action by us). duke@435: verify_ok_to_terminate(); duke@435: // Signal that it is terminated duke@435: { duke@435: MutexLockerEx mu(Terminator_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: assert(_cmst == this, "Weird!"); duke@435: _cmst = NULL; duke@435: Terminator_lock->notify(); duke@435: } duke@435: duke@435: // Thread destructor usually does this.. duke@435: ThreadLocalStorage::set_thread(NULL); duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void ConcurrentMarkSweepThread::verify_ok_to_terminate() const { duke@435: assert(!(CGC_lock->owned_by_self() || cms_thread_has_cms_token() || duke@435: cms_thread_wants_cms_token()), duke@435: "Must renounce all worldly possessions and desires for nirvana"); duke@435: _collector->verify_ok_to_terminate(); duke@435: } duke@435: #endif duke@435: duke@435: // create and start a new ConcurrentMarkSweep Thread for given CMS generation duke@435: ConcurrentMarkSweepThread* ConcurrentMarkSweepThread::start(CMSCollector* collector) { duke@435: if (!_should_terminate) { duke@435: assert(cmst() == NULL, "start() called twice?"); duke@435: ConcurrentMarkSweepThread* th = new ConcurrentMarkSweepThread(collector); duke@435: assert(cmst() == th, "Where did the just-created CMS thread go?"); duke@435: return th; duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::stop() { duke@435: if (CMSIncrementalMode) { duke@435: // Disable incremental mode and wake up the thread so it notices the change. duke@435: disable_icms(); duke@435: start_icms(); duke@435: } duke@435: // it is ok to take late safepoints here, if needed duke@435: { duke@435: MutexLockerEx x(Terminator_lock); duke@435: _should_terminate = true; duke@435: } duke@435: { // Now post a notify on CGC_lock so as to nudge duke@435: // CMS thread(s) that might be slumbering in duke@435: // sleepBeforeNextCycle. duke@435: MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); duke@435: CGC_lock->notify_all(); duke@435: } duke@435: { // Now wait until (all) CMS thread(s) have exited duke@435: MutexLockerEx x(Terminator_lock); duke@435: while(cmst() != NULL) { duke@435: Terminator_lock->wait(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::threads_do(ThreadClosure* tc) { duke@435: assert(tc != NULL, "Null ThreadClosure"); duke@435: if (_cmst != NULL) { duke@435: tc->do_thread(_cmst); duke@435: } duke@435: assert(Universe::is_fully_initialized(), duke@435: "Called too early, make sure heap is fully initialized"); duke@435: if (_collector != NULL) { duke@435: AbstractWorkGang* gang = _collector->conc_workers(); duke@435: if (gang != NULL) { duke@435: gang->threads_do(tc); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::print_on(outputStream* st) const { duke@435: st->print("\"%s\" ", name()); duke@435: Thread::print_on(st); duke@435: st->cr(); duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::print_all_on(outputStream* st) { duke@435: if (_cmst != NULL) { duke@435: _cmst->print_on(st); dholmes@4077: st->cr(); duke@435: } duke@435: if (_collector != NULL) { duke@435: AbstractWorkGang* gang = _collector->conc_workers(); duke@435: if (gang != NULL) { duke@435: gang->print_worker_threads_on(st); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::synchronize(bool is_cms_thread) { duke@435: assert(UseConcMarkSweepGC, "just checking"); duke@435: duke@435: MutexLockerEx x(CGC_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: if (!is_cms_thread) { duke@435: assert(Thread::current()->is_VM_thread(), "Not a VM thread"); duke@435: CMSSynchronousYieldRequest yr; duke@435: while (CMS_flag_is_set(CMS_cms_has_token)) { duke@435: // indicate that we want to get the token duke@435: set_CMS_flag(CMS_vm_wants_token); duke@435: CGC_lock->wait(true); duke@435: } duke@435: // claim the token and proceed duke@435: clear_CMS_flag(CMS_vm_wants_token); duke@435: set_CMS_flag(CMS_vm_has_token); duke@435: } else { duke@435: assert(Thread::current()->is_ConcurrentGC_thread(), duke@435: "Not a CMS thread"); duke@435: // The following barrier assumes there's only one CMS thread. duke@435: // This will need to be modified is there are more CMS threads than one. duke@435: while (CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token)) { duke@435: set_CMS_flag(CMS_cms_wants_token); duke@435: CGC_lock->wait(true); duke@435: } duke@435: // claim the token duke@435: clear_CMS_flag(CMS_cms_wants_token); duke@435: set_CMS_flag(CMS_cms_has_token); duke@435: } duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::desynchronize(bool is_cms_thread) { duke@435: assert(UseConcMarkSweepGC, "just checking"); duke@435: duke@435: MutexLockerEx x(CGC_lock, duke@435: Mutex::_no_safepoint_check_flag); duke@435: if (!is_cms_thread) { duke@435: assert(Thread::current()->is_VM_thread(), "Not a VM thread"); duke@435: assert(CMS_flag_is_set(CMS_vm_has_token), "just checking"); duke@435: clear_CMS_flag(CMS_vm_has_token); duke@435: if (CMS_flag_is_set(CMS_cms_wants_token)) { duke@435: // wake-up a waiting CMS thread duke@435: CGC_lock->notify(); duke@435: } duke@435: assert(!CMS_flag_is_set(CMS_vm_has_token | CMS_vm_wants_token), duke@435: "Should have been cleared"); duke@435: } else { duke@435: assert(Thread::current()->is_ConcurrentGC_thread(), duke@435: "Not a CMS thread"); duke@435: assert(CMS_flag_is_set(CMS_cms_has_token), "just checking"); duke@435: clear_CMS_flag(CMS_cms_has_token); duke@435: if (CMS_flag_is_set(CMS_vm_wants_token)) { duke@435: // wake-up a waiting VM thread duke@435: CGC_lock->notify(); duke@435: } duke@435: assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), duke@435: "Should have been cleared"); duke@435: } duke@435: } duke@435: jmasa@4679: // Wait until any cms_lock event ysr@2242: void ConcurrentMarkSweepThread::wait_on_cms_lock(long t_millis) { duke@435: MutexLockerEx x(CGC_lock, duke@435: Mutex::_no_safepoint_check_flag); ysr@2242: if (_should_terminate || _collector->_full_gc_requested) { ysr@2242: return; ysr@2242: } duke@435: set_CMS_flag(CMS_cms_wants_token); // to provoke notifies ysr@2242: CGC_lock->wait(Mutex::_no_safepoint_check_flag, t_millis); duke@435: clear_CMS_flag(CMS_cms_wants_token); duke@435: assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), duke@435: "Should not be set"); duke@435: } duke@435: jmasa@4679: // Wait until the next synchronous GC, a concurrent full gc request, jmasa@4679: // or a timeout, whichever is earlier. jmasa@4679: void ConcurrentMarkSweepThread::wait_on_cms_lock_for_scavenge(long t_millis) { jmasa@4679: // Wait time in millis or 0 value representing infinite wait for a scavenge jmasa@4679: assert(t_millis >= 0, "Wait time for scavenge should be 0 or positive"); jmasa@4679: jmasa@4679: GenCollectedHeap* gch = GenCollectedHeap::heap(); jmasa@4679: double start_time_secs = os::elapsedTime(); jmasa@4679: double end_time_secs = start_time_secs + (t_millis / ((double) MILLIUNITS)); jmasa@4679: jmasa@4679: // Total collections count before waiting loop jmasa@4679: unsigned int before_count; jmasa@4679: { jmasa@4679: MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag); jmasa@4679: before_count = gch->total_collections(); jmasa@4679: } jmasa@4679: jmasa@4679: unsigned int loop_count = 0; jmasa@4679: jmasa@4679: while(!_should_terminate) { jmasa@4679: double now_time = os::elapsedTime(); jmasa@4679: long wait_time_millis; jmasa@4679: jmasa@4679: if(t_millis != 0) { jmasa@4679: // New wait limit jmasa@4679: wait_time_millis = (long) ((end_time_secs - now_time) * MILLIUNITS); jmasa@4679: if(wait_time_millis <= 0) { jmasa@4679: // Wait time is over jmasa@4679: break; jmasa@4679: } jmasa@4679: } else { jmasa@4679: // No wait limit, wait if necessary forever jmasa@4679: wait_time_millis = 0; jmasa@4679: } jmasa@4679: jmasa@4679: // Wait until the next event or the remaining timeout jmasa@4679: { jmasa@4679: MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); jmasa@4679: jmasa@4679: if (_should_terminate || _collector->_full_gc_requested) { jmasa@4679: return; jmasa@4679: } jmasa@4679: set_CMS_flag(CMS_cms_wants_token); // to provoke notifies jmasa@4679: assert(t_millis == 0 || wait_time_millis > 0, "Sanity"); jmasa@4679: CGC_lock->wait(Mutex::_no_safepoint_check_flag, wait_time_millis); jmasa@4679: clear_CMS_flag(CMS_cms_wants_token); jmasa@4679: assert(!CMS_flag_is_set(CMS_cms_has_token | CMS_cms_wants_token), jmasa@4679: "Should not be set"); jmasa@4679: } jmasa@4679: jmasa@4679: // Extra wait time check before entering the heap lock to get the collection count jmasa@4679: if(t_millis != 0 && os::elapsedTime() >= end_time_secs) { jmasa@4679: // Wait time is over jmasa@4679: break; jmasa@4679: } jmasa@4679: jmasa@4679: // Total collections count after the event jmasa@4679: unsigned int after_count; jmasa@4679: { jmasa@4679: MutexLockerEx hl(Heap_lock, Mutex::_no_safepoint_check_flag); jmasa@4679: after_count = gch->total_collections(); jmasa@4679: } jmasa@4679: jmasa@4679: if(before_count != after_count) { jmasa@4679: // There was a collection - success jmasa@4679: break; jmasa@4679: } jmasa@4679: jmasa@4679: // Too many loops warning jmasa@4679: if(++loop_count == 0) { jmasa@4679: warning("wait_on_cms_lock_for_scavenge() has looped %u times", loop_count - 1); jmasa@4679: } jmasa@4679: } jmasa@4679: } jmasa@4679: duke@435: void ConcurrentMarkSweepThread::sleepBeforeNextCycle() { duke@435: while (!_should_terminate) { duke@435: if (CMSIncrementalMode) { duke@435: icms_wait(); jmasa@4679: if(CMSWaitDuration >= 0) { jmasa@4679: // Wait until the next synchronous GC, a concurrent full gc jmasa@4679: // request or a timeout, whichever is earlier. jmasa@4679: wait_on_cms_lock_for_scavenge(CMSWaitDuration); jmasa@4679: } duke@435: return; duke@435: } else { jmasa@4679: if(CMSWaitDuration >= 0) { jmasa@4679: // Wait until the next synchronous GC, a concurrent full gc jmasa@4679: // request or a timeout, whichever is earlier. jmasa@4679: wait_on_cms_lock_for_scavenge(CMSWaitDuration); jmasa@4679: } else { jmasa@4679: // Wait until any cms_lock event or check interval not to call shouldConcurrentCollect permanently jmasa@4679: wait_on_cms_lock(CMSCheckInterval); jmasa@4679: } duke@435: } duke@435: // Check if we should start a CMS collection cycle duke@435: if (_collector->shouldConcurrentCollect()) { duke@435: return; duke@435: } duke@435: // .. collection criterion not yet met, let's go back duke@435: // and wait some more duke@435: } duke@435: } duke@435: duke@435: // Incremental CMS duke@435: void ConcurrentMarkSweepThread::start_icms() { duke@435: assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking"); duke@435: MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag); duke@435: trace_state("start_icms"); duke@435: _should_run = true; duke@435: iCMS_lock->notify_all(); duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::stop_icms() { duke@435: assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking"); duke@435: MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag); duke@435: if (!_should_stop) { duke@435: trace_state("stop_icms"); duke@435: _should_stop = true; duke@435: _should_run = false; duke@435: asynchronous_yield_request(); duke@435: iCMS_lock->notify_all(); duke@435: } duke@435: } duke@435: duke@435: void ConcurrentMarkSweepThread::icms_wait() { duke@435: assert(UseConcMarkSweepGC && CMSIncrementalMode, "just checking"); ysr@2647: if (_should_stop && icms_is_enabled()) { duke@435: MutexLockerEx x(iCMS_lock, Mutex::_no_safepoint_check_flag); duke@435: trace_state("pause_icms"); duke@435: _collector->stats().stop_cms_timer(); ysr@2647: while(!_should_run && icms_is_enabled()) { duke@435: iCMS_lock->wait(Mutex::_no_safepoint_check_flag); duke@435: } duke@435: _collector->stats().start_cms_timer(); duke@435: _should_stop = false; duke@435: trace_state("pause_icms end"); duke@435: } duke@435: } duke@435: duke@435: // Note: this method, although exported by the ConcurrentMarkSweepThread, duke@435: // which is a non-JavaThread, can only be called by a JavaThread. duke@435: // Currently this is done at vm creation time (post-vm-init) by the duke@435: // main/Primordial (Java)Thread. duke@435: // XXX Consider changing this in the future to allow the CMS thread duke@435: // itself to create this thread? duke@435: void ConcurrentMarkSweepThread::makeSurrogateLockerThread(TRAPS) { duke@435: assert(UseConcMarkSweepGC, "SLT thread needed only for CMS GC"); duke@435: assert(_slt == NULL, "SLT already created"); duke@435: _slt = SurrogateLockerThread::make(THREAD); duke@435: }