ysr@777: /* tonyp@2472: * Copyright (c) 2001, 2011, Oracle and/or its affiliates. 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: * 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. ysr@777: * ysr@777: */ ysr@777: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/g1/concurrentMarkThread.inline.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectedHeap.inline.hpp" stefank@2314: #include "gc_implementation/g1/g1CollectorPolicy.hpp" stefank@2314: #include "gc_implementation/g1/g1MMUTracker.hpp" stefank@2314: #include "gc_implementation/g1/vm_operations_g1.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "runtime/vmThread.hpp" 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 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: tonyp@2472: G1CollectedHeap* g1h = G1CollectedHeap::heap(); tonyp@2472: G1CollectorPolicy* g1_policy = g1h->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: 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()); johnc@3218: ysr@777: double counting_start_time = os::elapsedVTime(); 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: } johnc@3218: 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 { tonyp@2472: g1h->set_marking_complete(); ysr@777: } ysr@777: tonyp@2472: // Check if cleanup set the free_regions_coming flag. If it tonyp@2472: // hasn't, we can just skip the next step. tonyp@2472: if (g1h->free_regions_coming()) { tonyp@2472: // The following will finish freeing up any regions that we tonyp@2472: // found to be empty during cleanup. We'll do this part tonyp@2472: // without joining the suspendible set. If an evacuation pause tonyp@2643: // takes place, then we would carry on freeing regions in tonyp@2472: // case they are needed by the pause. If a Full GC takes tonyp@2643: // place, it would wait for us to process the regions tonyp@2472: // reclaimed by cleanup. tonyp@2472: 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: tonyp@3208: // Now do the concurrent cleanup operation. ysr@777: _cm->completeCleanup(); tonyp@3208: tonyp@2501: // Notify anyone who's waiting that there are no more free tonyp@3208: // regions coming. We have to do this before we join the STS tonyp@3208: // (in fact, we should not attempt to join the STS in the tonyp@3208: // interval between finishing the cleanup pause and clearing tonyp@3208: // the free_regions_coming flag) otherwise we might deadlock: tonyp@3208: // a GC worker could be blocked waiting for the notification tonyp@3208: // whereas this thread will be blocked for the pause to finish tonyp@3208: // while it's trying to join the STS, which is conditional on tonyp@3208: // the GC workers finishing. tonyp@2501: g1h->reset_free_regions_coming(); tonyp@2501: tonyp@2472: double cleanup_end_sec = os::elapsedTime(); tonyp@2472: if (PrintGC) { tonyp@2472: gclog_or_tty->date_stamp(PrintGCDateStamps); tonyp@2472: gclog_or_tty->stamp(PrintGCTimeStamps); tonyp@2472: gclog_or_tty->print_cr("[GC concurrent-cleanup-end, %1.7lf]", tonyp@2472: cleanup_end_sec - cleanup_start_sec); ysr@777: } ysr@777: } tonyp@2472: guarantee(cm()->cleanup_list_is_empty(), tonyp@2472: "at this point there should be no regions on the cleanup list"); ysr@777: tonyp@3208: // There is a tricky race before recording that the concurrent tonyp@3208: // cleanup has completed and a potential Full GC starting around tonyp@3208: // the same time. We want to make sure that the Full GC calls tonyp@3208: // abort() on concurrent mark after tonyp@3208: // record_concurrent_mark_cleanup_completed(), since abort() is tonyp@3208: // the method that will reset the concurrent mark state. If we tonyp@3208: // end up calling record_concurrent_mark_cleanup_completed() tonyp@3208: // after abort() then we might incorrectly undo some of the work tonyp@3208: // abort() did. Checking the has_aborted() flag after joining tonyp@3208: // the STS allows the correct ordering of the two methods. There tonyp@3208: // are two scenarios: tonyp@3208: // tonyp@3208: // a) If we reach here before the Full GC, the fact that we have tonyp@3208: // joined the STS means that the Full GC cannot start until we tonyp@3208: // leave the STS, so record_concurrent_mark_cleanup_completed() tonyp@3208: // will complete before abort() is called. tonyp@3208: // tonyp@3208: // b) If we reach here during the Full GC, we'll be held up from tonyp@3208: // joining the STS until the Full GC is done, which means that tonyp@3208: // abort() will have completed and has_aborted() will return tonyp@3208: // true to prevent us from calling tonyp@3208: // record_concurrent_mark_cleanup_completed() (and, in fact, it's tonyp@3208: // not needed any more as the concurrent mark state has been tonyp@3208: // already reset). tonyp@3208: _sts.join(); tonyp@3208: if (!cm()->has_aborted()) { tonyp@3208: g1_policy->record_concurrent_mark_cleanup_completed(); tonyp@3208: } tonyp@3208: _sts.leave(); tonyp@3208: 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: tonyp@3208: // 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: } tonyp@2011: tonyp@2011: // Update the number of full collections that have been tonyp@2011: // completed. This will also notify the FullGCCount_lock in case a tonyp@2011: // Java thread is waiting for a full GC to happen (e.g., it tonyp@2011: // called System.gc() with +ExplicitGCInvokesConcurrent). tonyp@2372: _sts.join(); tonyp@2472: g1h->increment_full_collections_completed(true /* concurrent */); tonyp@2372: _sts.leave(); 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: tonyp@1454: void ConcurrentMarkThread::print() const { tonyp@1454: print_on(tty); tonyp@1454: } tonyp@1454: tonyp@1454: void ConcurrentMarkThread::print_on(outputStream* st) const { tonyp@1454: st->print("\"G1 Main Concurrent Mark GC Thread\" "); tonyp@1454: Thread::print_on(st); tonyp@1454: st->cr(); ysr@777: } ysr@777: ysr@777: void ConcurrentMarkThread::sleepBeforeNextCycle() { ysr@777: // We join here because we don't want to do the "shouldConcurrentMark()" ysr@777: // below while the world is otherwise stopped. johnc@2195: assert(!in_progress(), "should have been cleared"); johnc@2195: 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: johnc@3218: // Note: As is the case with CMS - this method, although exported johnc@3218: // by the ConcurrentMarkThread, which is a non-JavaThread, can only johnc@3218: // be called by a JavaThread. Currently this is done at vm creation johnc@3218: // time (post-vm-init) by the main/Primordial (Java)Thread. johnc@3218: // XXX Consider changing this in the future to allow the CM thread ysr@777: // itself to create this thread? ysr@777: void ConcurrentMarkThread::makeSurrogateLockerThread(TRAPS) { johnc@3218: assert(UseG1GC, "SLT thread needed only for concurrent GC"); johnc@3218: assert(THREAD->is_Java_thread(), "must be a Java thread"); ysr@777: assert(_slt == NULL, "SLT already created"); ysr@777: _slt = SurrogateLockerThread::make(THREAD); ysr@777: }