duke@435: /* ysr@2242: * Copyright (c) 2001, 2010, 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: duke@435: volatile bool ConcurrentMarkSweepThread::_icms_enabled = false; 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)) { duke@435: // XXX: need to set this to low priority duke@435: // unless "agressive mode" set; priority duke@435: // should be just less than that of VMThread. duke@435: os::set_priority(this, NearMaxPriority); duke@435: if (!DisableStartThread) { duke@435: os::start_thread(this); duke@435: } duke@435: } duke@435: _sltMonitor = SLT_lock; duke@435: set_icms_enabled(CMSIncrementalMode); 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; duke@435: _collector->collect_in_background(false); // !clear_all_soft_refs 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); 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: ysr@2242: // Wait until the next synchronous GC, a concurrent full gc request, ysr@2242: // or a timeout, whichever is earlier. 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: duke@435: void ConcurrentMarkSweepThread::sleepBeforeNextCycle() { duke@435: while (!_should_terminate) { duke@435: if (CMSIncrementalMode) { duke@435: icms_wait(); duke@435: return; duke@435: } else { ysr@2242: // Wait until the next synchronous GC, a concurrent full gc ysr@2242: // request or a timeout, whichever is earlier. duke@435: wait_on_cms_lock(CMSWaitDuration); 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"); duke@435: if (_should_stop && icms_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(); duke@435: while(!_should_run && icms_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: }