0N/A/*
2212N/A * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepGeneration.inline.hpp"
1879N/A#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
1879N/A#include "gc_implementation/concurrentMarkSweep/vmCMSOperations.hpp"
4141N/A#include "gc_implementation/shared/gcTimer.hpp"
4141N/A#include "gc_implementation/shared/gcTraceTime.hpp"
1879N/A#include "gc_implementation/shared/isGCActiveMark.hpp"
1879N/A#include "memory/gcLocker.inline.hpp"
1879N/A#include "runtime/interfaceSupport.hpp"
4141N/A#include "runtime/os.hpp"
1879N/A#include "utilities/dtrace.hpp"
2842N/A
2842N/A
2842N/A#ifndef USDT2
0N/AHS_DTRACE_PROBE_DECL(hs_private, cms__initmark__begin);
0N/AHS_DTRACE_PROBE_DECL(hs_private, cms__initmark__end);
0N/A
0N/AHS_DTRACE_PROBE_DECL(hs_private, cms__remark__begin);
0N/AHS_DTRACE_PROBE_DECL(hs_private, cms__remark__end);
2842N/A#endif /* !USDT2 */
0N/A
0N/A//////////////////////////////////////////////////////////
0N/A// Methods in abstract class VM_CMS_Operation
0N/A//////////////////////////////////////////////////////////
0N/Avoid VM_CMS_Operation::acquire_pending_list_lock() {
0N/A // The caller may block while communicating
0N/A // with the SLT thread in order to acquire/release the PLL.
0N/A ConcurrentMarkSweepThread::slt()->
0N/A manipulatePLL(SurrogateLockerThread::acquirePLL);
0N/A}
0N/A
0N/Avoid VM_CMS_Operation::release_and_notify_pending_list_lock() {
0N/A // The caller may block while communicating
0N/A // with the SLT thread in order to acquire/release the PLL.
0N/A ConcurrentMarkSweepThread::slt()->
0N/A manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL);
0N/A}
0N/A
0N/Avoid VM_CMS_Operation::verify_before_gc() {
0N/A if (VerifyBeforeGC &&
0N/A GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
4141N/A GCTraceTime tm("Verify Before", false, false, _collector->_gc_timer_cm);
0N/A HandleMark hm;
0N/A FreelistLocker x(_collector);
0N/A MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag);
0N/A Universe::heap()->prepare_for_verify();
4008N/A Universe::verify();
0N/A }
0N/A}
0N/A
0N/Avoid VM_CMS_Operation::verify_after_gc() {
0N/A if (VerifyAfterGC &&
0N/A GenCollectedHeap::heap()->total_collections() >= VerifyGCStartAt) {
4141N/A GCTraceTime tm("Verify After", false, false, _collector->_gc_timer_cm);
0N/A HandleMark hm;
0N/A FreelistLocker x(_collector);
0N/A MutexLockerEx y(_collector->bitMapLock(), Mutex::_no_safepoint_check_flag);
4008N/A Universe::verify();
0N/A }
0N/A}
0N/A
0N/Abool VM_CMS_Operation::lost_race() const {
0N/A if (CMSCollector::abstract_state() == CMSCollector::Idling) {
0N/A // We lost a race to a foreground collection
0N/A // -- there's nothing to do
0N/A return true;
0N/A }
0N/A assert(CMSCollector::abstract_state() == legal_state(),
0N/A "Inconsistent collector state?");
0N/A return false;
0N/A}
0N/A
0N/Abool VM_CMS_Operation::doit_prologue() {
0N/A assert(Thread::current()->is_ConcurrentGC_thread(), "just checking");
0N/A assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock");
0N/A assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
0N/A "Possible deadlock");
0N/A
0N/A if (needs_pll()) {
0N/A acquire_pending_list_lock();
0N/A }
0N/A // Get the Heap_lock after the pending_list_lock.
0N/A Heap_lock->lock();
0N/A if (lost_race()) {
0N/A assert(_prologue_succeeded == false, "Initialized in c'tor");
0N/A Heap_lock->unlock();
0N/A if (needs_pll()) {
0N/A release_and_notify_pending_list_lock();
0N/A }
0N/A } else {
0N/A _prologue_succeeded = true;
0N/A }
0N/A return _prologue_succeeded;
0N/A}
0N/A
0N/Avoid VM_CMS_Operation::doit_epilogue() {
0N/A assert(Thread::current()->is_ConcurrentGC_thread(), "just checking");
0N/A assert(!CMSCollector::foregroundGCShouldWait(), "Possible deadlock");
0N/A assert(!ConcurrentMarkSweepThread::cms_thread_has_cms_token(),
0N/A "Possible deadlock");
0N/A
0N/A // Release the Heap_lock first.
0N/A Heap_lock->unlock();
0N/A if (needs_pll()) {
0N/A release_and_notify_pending_list_lock();
0N/A }
0N/A}
0N/A
0N/A//////////////////////////////////////////////////////////
0N/A// Methods in class VM_CMS_Initial_Mark
0N/A//////////////////////////////////////////////////////////
0N/Avoid VM_CMS_Initial_Mark::doit() {
0N/A if (lost_race()) {
0N/A // Nothing to do.
0N/A return;
0N/A }
2842N/A#ifndef USDT2
0N/A HS_DTRACE_PROBE(hs_private, cms__initmark__begin);
2842N/A#else /* USDT2 */
2842N/A HS_PRIVATE_CMS_INITMARK_BEGIN(
2842N/A );
2842N/A#endif /* USDT2 */
0N/A
4141N/A _collector->_gc_timer_cm->register_gc_pause_start("Initial Mark", os::elapsed_counter());
4141N/A
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
0N/A GCCauseSetter gccs(gch, GCCause::_cms_initial_mark);
0N/A
0N/A VM_CMS_Operation::verify_before_gc();
0N/A
0N/A IsGCActiveMark x; // stop-world GC active
3733N/A _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsInitial, gch->gc_cause());
0N/A
0N/A VM_CMS_Operation::verify_after_gc();
4141N/A
4141N/A _collector->_gc_timer_cm->register_gc_pause_end(os::elapsed_counter());
4141N/A
2842N/A#ifndef USDT2
0N/A HS_DTRACE_PROBE(hs_private, cms__initmark__end);
2842N/A#else /* USDT2 */
2842N/A HS_PRIVATE_CMS_INITMARK_END(
2842N/A );
2842N/A#endif /* USDT2 */
0N/A}
0N/A
0N/A//////////////////////////////////////////////////////////
0N/A// Methods in class VM_CMS_Final_Remark_Operation
0N/A//////////////////////////////////////////////////////////
0N/Avoid VM_CMS_Final_Remark::doit() {
0N/A if (lost_race()) {
0N/A // Nothing to do.
0N/A return;
0N/A }
2842N/A#ifndef USDT2
0N/A HS_DTRACE_PROBE(hs_private, cms__remark__begin);
2842N/A#else /* USDT2 */
2842N/A HS_PRIVATE_CMS_REMARK_BEGIN(
2842N/A );
2842N/A#endif /* USDT2 */
0N/A
4141N/A _collector->_gc_timer_cm->register_gc_pause_start("Final Mark", os::elapsed_counter());
4141N/A
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
0N/A GCCauseSetter gccs(gch, GCCause::_cms_final_remark);
0N/A
0N/A VM_CMS_Operation::verify_before_gc();
0N/A
0N/A IsGCActiveMark x; // stop-world GC active
3733N/A _collector->do_CMS_operation(CMSCollector::CMS_op_checkpointRootsFinal, gch->gc_cause());
0N/A
0N/A VM_CMS_Operation::verify_after_gc();
4141N/A
4346N/A _collector->save_heap_summary();
4141N/A _collector->_gc_timer_cm->register_gc_pause_end(os::elapsed_counter());
4141N/A
2842N/A#ifndef USDT2
0N/A HS_DTRACE_PROBE(hs_private, cms__remark__end);
2842N/A#else /* USDT2 */
2842N/A HS_PRIVATE_CMS_REMARK_END(
2842N/A );
2842N/A#endif /* USDT2 */
0N/A}
0N/A
0N/A// VM operation to invoke a concurrent collection of a
0N/A// GenCollectedHeap heap.
0N/Avoid VM_GenCollectFullConcurrent::doit() {
0N/A assert(Thread::current()->is_VM_thread(), "Should be VM thread");
1440N/A assert(GCLockerInvokesConcurrent || ExplicitGCInvokesConcurrent, "Unexpected");
0N/A
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
0N/A if (_gc_count_before == gch->total_collections()) {
0N/A // The "full" of do_full_collection call below "forces"
0N/A // a collection; the second arg, 0, below ensures that
0N/A // only the young gen is collected. XXX In the future,
0N/A // we'll probably need to have something in this interface
0N/A // to say do this only if we are sure we will not bail
0N/A // out to a full collection in this attempt, but that's
0N/A // for the future.
0N/A assert(SafepointSynchronize::is_at_safepoint(),
0N/A "We can only be executing this arm of if at a safepoint");
0N/A GCCauseSetter gccs(gch, _gc_cause);
0N/A gch->do_full_collection(gch->must_clear_all_soft_refs(),
0N/A 0 /* collect only youngest gen */);
0N/A } // Else no need for a foreground young gc
0N/A assert((_gc_count_before < gch->total_collections()) ||
0N/A (GC_locker::is_active() /* gc may have been skipped */
0N/A && (_gc_count_before == gch->total_collections())),
0N/A "total_collections() should be monotonically increasing");
0N/A
0N/A MutexLockerEx x(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
2212N/A assert(_full_gc_count_before <= gch->total_full_collections(), "Error");
0N/A if (gch->total_full_collections() == _full_gc_count_before) {
2212N/A // Disable iCMS until the full collection is done, and
2212N/A // remember that we did so.
0N/A CMSCollector::disable_icms();
2212N/A _disabled_icms = true;
0N/A // In case CMS thread was in icms_wait(), wake it up.
0N/A CMSCollector::start_icms();
1440N/A // Nudge the CMS thread to start a concurrent collection.
4518N/A CMSCollector::request_full_gc(_full_gc_count_before, _gc_cause);
0N/A } else {
2212N/A assert(_full_gc_count_before < gch->total_full_collections(), "Error");
0N/A FullGCCount_lock->notify_all(); // Inform the Java thread its work is done
0N/A }
0N/A}
0N/A
0N/Abool VM_GenCollectFullConcurrent::evaluate_at_safepoint() const {
0N/A Thread* thr = Thread::current();
0N/A assert(thr != NULL, "Unexpected tid");
0N/A if (!thr->is_Java_thread()) {
0N/A assert(thr->is_VM_thread(), "Expected to be evaluated by VM thread");
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
0N/A if (_gc_count_before != gch->total_collections()) {
0N/A // No need to do a young gc, we'll just nudge the CMS thread
0N/A // in the doit() method above, to be executed soon.
0N/A assert(_gc_count_before < gch->total_collections(),
0N/A "total_collections() should be monotnically increasing");
0N/A return false; // no need for foreground young gc
0N/A }
0N/A }
0N/A return true; // may still need foreground young gc
0N/A}
0N/A
0N/A
0N/Avoid VM_GenCollectFullConcurrent::doit_epilogue() {
0N/A Thread* thr = Thread::current();
0N/A assert(thr->is_Java_thread(), "just checking");
0N/A JavaThread* jt = (JavaThread*)thr;
0N/A // Release the Heap_lock first.
0N/A Heap_lock->unlock();
0N/A release_and_notify_pending_list_lock();
0N/A
0N/A // It is fine to test whether completed collections has
0N/A // exceeded our request count without locking because
0N/A // the completion count is monotonically increasing;
0N/A // this will break for very long-running apps when the
0N/A // count overflows and wraps around. XXX fix me !!!
0N/A // e.g. at the rate of 1 full gc per ms, this could
0N/A // overflow in about 1000 years.
0N/A GenCollectedHeap* gch = GenCollectedHeap::heap();
1440N/A if (_gc_cause != GCCause::_gc_locker &&
1440N/A gch->total_full_collections_completed() <= _full_gc_count_before) {
1576N/A // maybe we should change the condition to test _gc_cause ==
1576N/A // GCCause::_java_lang_system_gc, instead of
1576N/A // _gc_cause != GCCause::_gc_locker
1576N/A assert(_gc_cause == GCCause::_java_lang_system_gc,
1576N/A "the only way to get here if this was a System.gc()-induced GC");
1440N/A assert(ExplicitGCInvokesConcurrent, "Error");
0N/A // Now, wait for witnessing concurrent gc cycle to complete,
0N/A // but do so in native mode, because we want to lock the
0N/A // FullGCEvent_lock, which may be needed by the VM thread
0N/A // or by the CMS thread, so we do not want to be suspended
0N/A // while holding that lock.
0N/A ThreadToNativeFromVM native(jt);
0N/A MutexLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
0N/A // Either a concurrent or a stop-world full gc is sufficient
0N/A // witness to our request.
0N/A while (gch->total_full_collections_completed() <= _full_gc_count_before) {
0N/A FullGCCount_lock->wait(Mutex::_no_safepoint_check_flag);
0N/A }
0N/A }
2212N/A // Enable iCMS back if we disabled it earlier.
2212N/A if (_disabled_icms) {
2212N/A CMSCollector::enable_icms();
2212N/A }
0N/A}