/* * Copyright (c) 2013, 2015, Red Hat, Inc. and/or its affiliates. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. * */ #include "gc/shared/gcTraceTime.inline.hpp" #include "gc/shenandoah/shenandoahConcurrentMark.inline.hpp" #include "gc/shenandoah/shenandoahConcurrentThread.hpp" #include "gc/shenandoah/shenandoahCollectorPolicy.hpp" #include "gc/shenandoah/shenandoahHeap.inline.hpp" #include "gc/shenandoah/shenandoahMonitoringSupport.hpp" #include "gc/shenandoah/vm_operations_shenandoah.hpp" #include "memory/iterator.hpp" #include "memory/universe.hpp" #include "runtime/vmThread.hpp" ShenandoahConcurrentThread::ShenandoahConcurrentThread() : ConcurrentGCThread(), _full_gc_lock(Mutex::leaf, "ShenandoahFullGC_lock", true, Monitor::_safepoint_check_always), _do_full_gc(false), _graceful_shutdown(0) { create_and_start(); } ShenandoahConcurrentThread::~ShenandoahConcurrentThread() { // This is here so that super is called. } void ShenandoahConcurrentThread::run_service() { ShenandoahHeap* heap = ShenandoahHeap::heap(); while (!should_terminate()) { if (in_graceful_shutdown()) { break; } else if (is_full_gc()) { service_fullgc_cycle(); } else if (heap->shenandoahPolicy()->should_start_partial_gc()) { service_partial_cycle(); } else if (heap->shenandoahPolicy()->should_start_concurrent_mark(heap->used(), heap->capacity())) { service_normal_cycle(); if (heap->is_evacuation_in_progress()) { heap->set_evacuation_in_progress_concurrently(false); } } else { Thread::current()->_ParkEvent->park(10); } heap->monitoring_support()->update_counters(); // Make sure the _do_full_gc flag changes are seen. OrderAccess::storeload(); } // Wait for the actual stop(), can't leave run_service() earlier. while (!should_terminate()) { Thread::current()->_ParkEvent->park(10); } } void ShenandoahConcurrentThread::service_partial_cycle() { GCIdMark gc_id_mark; VM_ShenandoahPartialGC partial_gc; VMThread::execute(&partial_gc); } void ShenandoahConcurrentThread::service_normal_cycle() { if (check_cancellation()) return; ShenandoahHeap* heap = ShenandoahHeap::heap(); GCTimer* gc_timer = heap->gc_timer(); gc_timer->register_gc_start(); heap->shenandoahPolicy()->increase_cycle_counter(); GCIdMark gc_id_mark; TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters()); TraceMemoryManagerStats tmms(false, GCCause::_no_cause_specified); // Start initial mark under STW: { // Workers are setup by VM_ShenandoahInitMark TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters()); VM_ShenandoahInitMark initMark; heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross); heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::init_mark_gross); VMThread::execute(&initMark); heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::init_mark_gross); heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross); } if (check_cancellation()) return; // Continue concurrent mark: { // Setup workers for concurrent marking phase WorkGang* workers = heap->workers(); uint n_workers = ShenandoahCollectorPolicy::calc_workers_for_conc_marking(workers->active_workers(), Threads::number_of_non_daemon_threads()); ShenandoahWorkerScope scope(workers, n_workers); GCTraceTime(Info, gc) time("Concurrent marking", gc_timer, GCCause::_no_gc, true); TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters()); ShenandoahHeap::heap()->concurrentMark()->mark_from_roots(); } // Possibly hand over remaining marking work to final-mark phase. bool clear_full_gc = false; if (heap->cancelled_concgc()) { heap->shenandoahPolicy()->record_cm_cancelled(); if (_full_gc_cause == GCCause::_allocation_failure && heap->shenandoahPolicy()->handover_cancelled_marking()) { heap->set_cancelled_concgc(false); clear_full_gc = true; heap->shenandoahPolicy()->record_cm_degenerated(); } else { heap->gc_timer()->register_gc_end(); return; } } else { heap->shenandoahPolicy()->record_cm_success(); } // Proceed to complete marking under STW, and start evacuation: { // Workers are setup by VM_ShenandoahStartEvacuation TraceCollectorStats tcs(heap->monitoring_support()->stw_collection_counters()); VM_ShenandoahStartEvacuation finishMark; heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::total_pause_gross); heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::final_mark_gross); VMThread::execute(&finishMark); heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::final_mark_gross); heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::total_pause_gross); } if (check_cancellation()) return; // If we handed off remaining marking work above, we need to kick off waiting Java threads if (clear_full_gc) { reset_full_gc(); } // Continue concurrent evacuation: { // Setup workers for concurrent evacuation phase WorkGang* workers = heap->workers(); uint n_workers = ShenandoahCollectorPolicy::calc_workers_for_conc_evacuation(workers->active_workers(), Threads::number_of_non_daemon_threads()); ShenandoahWorkerScope scope(workers, n_workers); GCTraceTime(Info, gc) time("Concurrent evacuation ", gc_timer, GCCause::_no_gc, true); TraceCollectorStats tcs(heap->monitoring_support()->concurrent_collection_counters()); heap->do_evacuation(); } // Prepare for the next normal cycle: if (check_cancellation()) return; { GCTraceTime(Info, gc) time("Concurrent reset bitmaps", gc_timer, GCCause::_no_gc); heap->shenandoahPolicy()->record_phase_start(ShenandoahCollectorPolicy::reset_bitmaps); WorkGang* workers = heap->workers(); ShenandoahPushWorkerScope scope(workers, heap->max_workers()); heap->reset_next_mark_bitmap(workers); heap->shenandoahPolicy()->record_phase_end(ShenandoahCollectorPolicy::reset_bitmaps); } gc_timer->register_gc_end(); } bool ShenandoahConcurrentThread::check_cancellation() { ShenandoahHeap* heap = ShenandoahHeap::heap(); if (heap->cancelled_concgc()) { assert (is_full_gc() || in_graceful_shutdown(), "Cancel GC either for Full GC, or gracefully exiting"); heap->gc_timer()->register_gc_end(); return true; } return false; } void ShenandoahConcurrentThread::stop_service() { // Nothing to do here. } void ShenandoahConcurrentThread::service_fullgc_cycle() { GCIdMark gc_id_mark; ShenandoahHeap* heap = ShenandoahHeap::heap(); { if (_full_gc_cause == GCCause::_allocation_failure) { heap->shenandoahPolicy()->record_allocation_failure_gc(); } else { heap->shenandoahPolicy()->record_user_requested_gc(); } TraceCollectorStats tcs(heap->monitoring_support()->full_collection_counters()); TraceMemoryManagerStats tmms(true, _full_gc_cause); VM_ShenandoahFullGC full_gc(_full_gc_cause); VMThread::execute(&full_gc); } reset_full_gc(); } void ShenandoahConcurrentThread::do_full_gc(GCCause::Cause cause) { assert(Thread::current()->is_Java_thread(), "expect Java thread here"); if (try_set_full_gc()) { _full_gc_cause = cause; // Now that full GC is scheduled, we can abort everything else ShenandoahHeap::heap()->cancel_concgc(cause); } else { if (_full_gc_cause != cause) { log_info(gc)("Full GC is already pending with cause: %s; new cause is %s", GCCause::to_string(_full_gc_cause), GCCause::to_string(cause)); } } MonitorLockerEx ml(&_full_gc_lock); while (is_full_gc()) { ml.wait(); } assert(!is_full_gc(), "expect full GC to have completed"); } void ShenandoahConcurrentThread::reset_full_gc() { OrderAccess::release_store_fence(&_do_full_gc, 0); MonitorLockerEx ml(&_full_gc_lock); ml.notify_all(); } bool ShenandoahConcurrentThread::try_set_full_gc() { jbyte old = Atomic::cmpxchg(1, &_do_full_gc, 0); return old == 0; // success } bool ShenandoahConcurrentThread::is_full_gc() { return OrderAccess::load_acquire(&_do_full_gc) == 1; } void ShenandoahConcurrentThread::print() const { print_on(tty); } void ShenandoahConcurrentThread::print_on(outputStream* st) const { st->print("Shenandoah Concurrent Thread"); Thread::print_on(st); st->cr(); } void ShenandoahConcurrentThread::sleepBeforeNextCycle() { assert(false, "Wake up in the GC thread that never sleeps :-)"); } void ShenandoahConcurrentThread::start() { create_and_start(); } void ShenandoahConcurrentThread::prepare_for_graceful_shutdown() { OrderAccess::release_store_fence(&_graceful_shutdown, 1); } bool ShenandoahConcurrentThread::in_graceful_shutdown() { return OrderAccess::load_acquire(&_graceful_shutdown) == 1; }