ysr@777: /* ysr@777: * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. ysr@777: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ysr@777: * ysr@777: * This code is free software; you can redistribute it and/or modify it ysr@777: * under the terms of the GNU General Public License version 2 only, as ysr@777: * published by the Free Software Foundation. ysr@777: * ysr@777: * This code is distributed in the hope that it will be useful, but WITHOUT ysr@777: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ysr@777: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ysr@777: * version 2 for more details (a copy is included in the LICENSE file that ysr@777: * accompanied this code). ysr@777: * ysr@777: * You should have received a copy of the GNU General Public License version ysr@777: * 2 along with this work; if not, write to the Free Software Foundation, ysr@777: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ysr@777: * ysr@777: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, ysr@777: * CA 95054 USA or visit www.sun.com if you need additional information or ysr@777: * have any questions. ysr@777: * ysr@777: */ ysr@777: ysr@777: #include "incls/_precompiled.incl" ysr@777: #include "incls/_concurrentMarkThread.cpp.incl" ysr@777: ysr@777: // ======= Concurrent Mark Thread ======== ysr@777: ysr@777: // The CM thread is created when the G1 garbage collector is used ysr@777: ysr@777: SurrogateLockerThread* ysr@777: ConcurrentMarkThread::_slt = NULL; ysr@777: ysr@777: ConcurrentMarkThread::ConcurrentMarkThread(ConcurrentMark* cm) : ysr@777: ConcurrentGCThread(), ysr@777: _cm(cm), ysr@777: _started(false), ysr@777: _in_progress(false), ysr@777: _vtime_accum(0.0), ysr@777: _vtime_mark_accum(0.0), ysr@777: _vtime_count_accum(0.0) ysr@777: { ysr@777: create_and_start(); ysr@777: } ysr@777: ysr@777: class CMCheckpointRootsInitialClosure: public VoidClosure { ysr@777: ysr@777: ConcurrentMark* _cm; ysr@777: public: ysr@777: ysr@777: CMCheckpointRootsInitialClosure(ConcurrentMark* cm) : ysr@777: _cm(cm) {} ysr@777: ysr@777: void do_void(){ ysr@777: _cm->checkpointRootsInitial(); ysr@777: } ysr@777: }; ysr@777: ysr@777: class CMCheckpointRootsFinalClosure: public VoidClosure { ysr@777: ysr@777: ConcurrentMark* _cm; ysr@777: public: ysr@777: ysr@777: CMCheckpointRootsFinalClosure(ConcurrentMark* cm) : ysr@777: _cm(cm) {} ysr@777: ysr@777: void do_void(){ ysr@777: _cm->checkpointRootsFinal(false); // !clear_all_soft_refs ysr@777: } ysr@777: }; ysr@777: ysr@777: class CMCleanUp: public VoidClosure { ysr@777: ConcurrentMark* _cm; ysr@777: public: ysr@777: ysr@777: CMCleanUp(ConcurrentMark* cm) : ysr@777: _cm(cm) {} ysr@777: ysr@777: void do_void(){ ysr@777: _cm->cleanup(); ysr@777: } ysr@777: }; ysr@777: ysr@777: ysr@777: ysr@777: void ConcurrentMarkThread::run() { ysr@777: initialize_in_thread(); ysr@777: _vtime_start = os::elapsedVTime(); ysr@777: wait_for_universe_init(); ysr@777: ysr@777: G1CollectedHeap* g1 = G1CollectedHeap::heap(); ysr@777: G1CollectorPolicy* g1_policy = g1->g1_policy(); ysr@777: G1MMUTracker *mmu_tracker = g1_policy->mmu_tracker(); ysr@777: Thread *current_thread = Thread::current(); ysr@777: ysr@777: while (!_should_terminate) { ysr@777: // wait until started is set. ysr@777: sleepBeforeNextCycle(); ysr@777: { ysr@777: ResourceMark rm; ysr@777: HandleMark hm; ysr@777: double cycle_start = os::elapsedVTime(); ysr@777: double mark_start_sec = os::elapsedTime(); ysr@777: char verbose_str[128]; ysr@777: ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); tonyp@1054: gclog_or_tty->print_cr("[GC concurrent-mark-start]"); ysr@777: } ysr@777: ysr@777: if (!g1_policy->in_young_gc_mode()) { ysr@777: // this ensures the flag is not set if we bail out of the marking ysr@777: // cycle; normally the flag is cleared immediately after cleanup ysr@777: g1->set_marking_complete(); ysr@777: ysr@777: if (g1_policy->adaptive_young_list_length()) { ysr@777: double now = os::elapsedTime(); ysr@777: double init_prediction_ms = g1_policy->predict_init_time_ms(); ysr@777: jlong sleep_time_ms = mmu_tracker->when_ms(now, init_prediction_ms); ysr@777: os::sleep(current_thread, sleep_time_ms, false); ysr@777: } ysr@777: ysr@777: // We don't have to skip here if we've been asked to restart, because ysr@777: // in the worst case we just enqueue a new VM operation to start a ysr@777: // marking. Note that the init operation resets has_aborted() ysr@777: CMCheckpointRootsInitialClosure init_cl(_cm); ysr@777: strcpy(verbose_str, "GC initial-mark"); ysr@777: VM_CGC_Operation op(&init_cl, verbose_str); ysr@777: VMThread::execute(&op); ysr@777: } ysr@777: ysr@777: int iter = 0; ysr@777: do { ysr@777: iter++; ysr@777: if (!cm()->has_aborted()) { ysr@777: _cm->markFromRoots(); ysr@777: } ysr@777: ysr@777: double mark_end_time = os::elapsedVTime(); ysr@777: double mark_end_sec = os::elapsedTime(); ysr@777: _vtime_mark_accum += (mark_end_time - cycle_start); ysr@777: if (!cm()->has_aborted()) { ysr@777: if (g1_policy->adaptive_young_list_length()) { ysr@777: double now = os::elapsedTime(); ysr@777: double remark_prediction_ms = g1_policy->predict_remark_time_ms(); ysr@777: jlong sleep_time_ms = mmu_tracker->when_ms(now, remark_prediction_ms); ysr@777: os::sleep(current_thread, sleep_time_ms, false); ysr@777: } ysr@777: ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); ysr@777: gclog_or_tty->print_cr("[GC concurrent-mark-end, %1.7lf sec]", ysr@777: mark_end_sec - mark_start_sec); ysr@777: } ysr@777: ysr@777: CMCheckpointRootsFinalClosure final_cl(_cm); ysr@777: sprintf(verbose_str, "GC remark"); ysr@777: VM_CGC_Operation op(&final_cl, verbose_str); ysr@777: VMThread::execute(&op); ysr@777: } ysr@777: if (cm()->restart_for_overflow() && ysr@777: G1TraceMarkStackOverflow) { ysr@777: gclog_or_tty->print_cr("Restarting conc marking because of MS overflow " ysr@777: "in remark (restart #%d).", iter); ysr@777: } ysr@777: ysr@777: if (cm()->restart_for_overflow()) { ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); ysr@777: gclog_or_tty->print_cr("[GC concurrent-mark-restart-for-overflow]"); ysr@777: } ysr@777: } ysr@777: } while (cm()->restart_for_overflow()); ysr@777: double counting_start_time = os::elapsedVTime(); ysr@777: ysr@777: // YSR: These look dubious (i.e. redundant) !!! FIX ME ysr@777: slt()->manipulatePLL(SurrogateLockerThread::acquirePLL); ysr@777: slt()->manipulatePLL(SurrogateLockerThread::releaseAndNotifyPLL); ysr@777: ysr@777: if (!cm()->has_aborted()) { ysr@777: double count_start_sec = os::elapsedTime(); ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); ysr@777: gclog_or_tty->print_cr("[GC concurrent-count-start]"); ysr@777: } ysr@777: ysr@777: _sts.join(); ysr@777: _cm->calcDesiredRegions(); ysr@777: _sts.leave(); ysr@777: ysr@777: if (!cm()->has_aborted()) { ysr@777: double count_end_sec = os::elapsedTime(); ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); ysr@777: gclog_or_tty->print_cr("[GC concurrent-count-end, %1.7lf]", ysr@777: count_end_sec - count_start_sec); ysr@777: } ysr@777: } ysr@777: } ysr@777: double end_time = os::elapsedVTime(); ysr@777: _vtime_count_accum += (end_time - counting_start_time); ysr@777: // Update the total virtual time before doing this, since it will try ysr@777: // to measure it to get the vtime for this marking. We purposely ysr@777: // neglect the presumably-short "completeCleanup" phase here. ysr@777: _vtime_accum = (end_time - _vtime_start); ysr@777: if (!cm()->has_aborted()) { ysr@777: if (g1_policy->adaptive_young_list_length()) { ysr@777: double now = os::elapsedTime(); ysr@777: double cleanup_prediction_ms = g1_policy->predict_cleanup_time_ms(); ysr@777: jlong sleep_time_ms = mmu_tracker->when_ms(now, cleanup_prediction_ms); ysr@777: os::sleep(current_thread, sleep_time_ms, false); ysr@777: } ysr@777: ysr@777: CMCleanUp cl_cl(_cm); ysr@777: sprintf(verbose_str, "GC cleanup"); ysr@777: VM_CGC_Operation op(&cl_cl, verbose_str); ysr@777: VMThread::execute(&op); ysr@777: } else { ysr@777: G1CollectedHeap::heap()->set_marking_complete(); ysr@777: } ysr@777: ysr@777: if (!cm()->has_aborted()) { ysr@777: double cleanup_start_sec = os::elapsedTime(); ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); ysr@777: gclog_or_tty->print_cr("[GC concurrent-cleanup-start]"); ysr@777: } ysr@777: ysr@777: // Now do the remainder of the cleanup operation. ysr@777: _sts.join(); ysr@777: _cm->completeCleanup(); ysr@777: if (!cm()->has_aborted()) { ysr@777: g1_policy->record_concurrent_mark_cleanup_completed(); ysr@777: ysr@777: double cleanup_end_sec = os::elapsedTime(); ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); ysr@777: gclog_or_tty->print_cr("[GC concurrent-cleanup-end, %1.7lf]", ysr@777: cleanup_end_sec - cleanup_start_sec); ysr@777: } ysr@777: } ysr@777: _sts.leave(); ysr@777: } ysr@777: // We're done: no more unclean regions coming. ysr@777: G1CollectedHeap::heap()->set_unclean_regions_coming(false); ysr@777: ysr@777: if (cm()->has_aborted()) { ysr@777: if (PrintGC) { ysr@777: gclog_or_tty->date_stamp(PrintGCDateStamps); ysr@777: gclog_or_tty->stamp(PrintGCTimeStamps); ysr@777: gclog_or_tty->print_cr("[GC concurrent-mark-abort]"); ysr@777: } ysr@777: } ysr@777: ysr@777: _sts.join(); ysr@777: _cm->disable_co_trackers(); ysr@777: _sts.leave(); ysr@777: ysr@777: // we now want to allow clearing of the marking bitmap to be ysr@777: // suspended by a collection pause. ysr@777: _sts.join(); ysr@777: _cm->clearNextBitmap(); ysr@777: _sts.leave(); ysr@777: } ysr@777: } ysr@777: assert(_should_terminate, "just checking"); ysr@777: ysr@777: terminate(); ysr@777: } ysr@777: ysr@777: ysr@777: void ConcurrentMarkThread::yield() { ysr@777: _sts.yield("Concurrent Mark"); ysr@777: } ysr@777: ysr@777: void ConcurrentMarkThread::stop() { ysr@777: // it is ok to take late safepoints here, if needed ysr@777: MutexLockerEx mu(Terminator_lock); ysr@777: _should_terminate = true; ysr@777: while (!_has_terminated) { ysr@777: Terminator_lock->wait(); ysr@777: } ysr@777: } ysr@777: ysr@777: void ConcurrentMarkThread::print() { ysr@777: gclog_or_tty->print("\"Concurrent Mark GC Thread\" "); ysr@777: Thread::print(); ysr@777: gclog_or_tty->cr(); ysr@777: } ysr@777: ysr@777: void ConcurrentMarkThread::sleepBeforeNextCycle() { ysr@777: clear_in_progress(); ysr@777: // We join here because we don't want to do the "shouldConcurrentMark()" ysr@777: // below while the world is otherwise stopped. ysr@777: MutexLockerEx x(CGC_lock, Mutex::_no_safepoint_check_flag); ysr@777: while (!started()) { ysr@777: CGC_lock->wait(Mutex::_no_safepoint_check_flag); ysr@777: } ysr@777: set_in_progress(); ysr@777: clear_started(); ysr@777: } ysr@777: ysr@777: // Note: this method, although exported by the ConcurrentMarkSweepThread, ysr@777: // which is a non-JavaThread, can only be called by a JavaThread. ysr@777: // Currently this is done at vm creation time (post-vm-init) by the ysr@777: // main/Primordial (Java)Thread. ysr@777: // XXX Consider changing this in the future to allow the CMS thread ysr@777: // itself to create this thread? ysr@777: void ConcurrentMarkThread::makeSurrogateLockerThread(TRAPS) { ysr@777: assert(_slt == NULL, "SLT already created"); ysr@777: _slt = SurrogateLockerThread::make(THREAD); ysr@777: }