aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/symbolTable.hpp" aoqi@0: #include "classfile/systemDictionary.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psAdaptiveSizePolicy.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psMarkSweep.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psMarkSweepDecorator.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psOldGen.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psScavenge.hpp" aoqi@0: #include "gc_implementation/parallelScavenge/psYoungGen.hpp" aoqi@0: #include "gc_implementation/shared/gcHeapSummary.hpp" aoqi@0: #include "gc_implementation/shared/gcTimer.hpp" aoqi@0: #include "gc_implementation/shared/gcTrace.hpp" aoqi@0: #include "gc_implementation/shared/gcTraceTime.hpp" aoqi@0: #include "gc_implementation/shared/isGCActiveMark.hpp" aoqi@0: #include "gc_implementation/shared/markSweep.hpp" aoqi@0: #include "gc_implementation/shared/spaceDecorator.hpp" aoqi@0: #include "gc_interface/gcCause.hpp" aoqi@0: #include "memory/gcLocker.inline.hpp" aoqi@0: #include "memory/referencePolicy.hpp" aoqi@0: #include "memory/referenceProcessor.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/biasedLocking.hpp" aoqi@0: #include "runtime/fprofiler.hpp" aoqi@0: #include "runtime/safepoint.hpp" aoqi@0: #include "runtime/vmThread.hpp" aoqi@0: #include "services/management.hpp" aoqi@0: #include "services/memoryService.hpp" aoqi@0: #include "utilities/events.hpp" aoqi@0: #include "utilities/stack.inline.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: elapsedTimer PSMarkSweep::_accumulated_time; aoqi@0: jlong PSMarkSweep::_time_of_last_gc = 0; aoqi@0: CollectorCounters* PSMarkSweep::_counters = NULL; aoqi@0: aoqi@0: void PSMarkSweep::initialize() { aoqi@0: MemRegion mr = Universe::heap()->reserved_region(); aoqi@0: _ref_processor = new ReferenceProcessor(mr); // a vanilla ref proc aoqi@0: _counters = new CollectorCounters("PSMarkSweep", 1); aoqi@0: } aoqi@0: aoqi@0: // This method contains all heap specific policy for invoking mark sweep. aoqi@0: // PSMarkSweep::invoke_no_policy() will only attempt to mark-sweep-compact aoqi@0: // the heap. It will do nothing further. If we need to bail out for policy aoqi@0: // reasons, scavenge before full gc, or any other specialized behavior, it aoqi@0: // needs to be added here. aoqi@0: // aoqi@0: // Note that this method should only be called from the vm_thread while aoqi@0: // at a safepoint! aoqi@0: // aoqi@0: // Note that the all_soft_refs_clear flag in the collector policy aoqi@0: // may be true because this method can be called without intervening aoqi@0: // activity. For example when the heap space is tight and full measure aoqi@0: // are being taken to free space. aoqi@0: aoqi@0: void PSMarkSweep::invoke(bool maximum_heap_compaction) { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint"); aoqi@0: assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread"); aoqi@0: assert(!Universe::heap()->is_gc_active(), "not reentrant"); aoqi@0: aoqi@0: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); aoqi@0: GCCause::Cause gc_cause = heap->gc_cause(); aoqi@0: PSAdaptiveSizePolicy* policy = heap->size_policy(); aoqi@0: IsGCActiveMark mark; aoqi@0: aoqi@0: if (ScavengeBeforeFullGC) { aoqi@0: PSScavenge::invoke_no_policy(); aoqi@0: } aoqi@0: aoqi@0: const bool clear_all_soft_refs = aoqi@0: heap->collector_policy()->should_clear_all_soft_refs(); aoqi@0: aoqi@0: uint count = maximum_heap_compaction ? 1 : MarkSweepAlwaysCompactCount; aoqi@0: UIntFlagSetting flag_setting(MarkSweepAlwaysCompactCount, count); aoqi@0: PSMarkSweep::invoke_no_policy(clear_all_soft_refs || maximum_heap_compaction); aoqi@0: } aoqi@0: aoqi@0: // This method contains no policy. You should probably aoqi@0: // be calling invoke() instead. aoqi@0: bool PSMarkSweep::invoke_no_policy(bool clear_all_softrefs) { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); aoqi@0: assert(ref_processor() != NULL, "Sanity"); aoqi@0: aoqi@0: if (GC_locker::check_active_before_gc()) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); aoqi@0: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); aoqi@0: GCCause::Cause gc_cause = heap->gc_cause(); aoqi@0: aoqi@0: _gc_timer->register_gc_start(); aoqi@0: _gc_tracer->report_gc_start(gc_cause, _gc_timer->gc_start()); aoqi@0: aoqi@0: PSAdaptiveSizePolicy* size_policy = heap->size_policy(); aoqi@0: aoqi@0: // The scope of casr should end after code that can change aoqi@0: // CollectorPolicy::_should_clear_all_soft_refs. aoqi@0: ClearedAllSoftRefs casr(clear_all_softrefs, heap->collector_policy()); aoqi@0: aoqi@0: PSYoungGen* young_gen = heap->young_gen(); aoqi@0: PSOldGen* old_gen = heap->old_gen(); aoqi@0: aoqi@0: // Increment the invocation count aoqi@0: heap->increment_total_collections(true /* full */); aoqi@0: aoqi@0: // Save information needed to minimize mangling aoqi@0: heap->record_gen_tops_before_GC(); aoqi@0: aoqi@0: // We need to track unique mark sweep invocations as well. aoqi@0: _total_invocations++; aoqi@0: aoqi@0: AdaptiveSizePolicyOutput(size_policy, heap->total_collections()); aoqi@0: aoqi@0: heap->print_heap_before_gc(); aoqi@0: heap->trace_heap_before_gc(_gc_tracer); aoqi@0: aoqi@0: // Fill in TLABs aoqi@0: heap->accumulate_statistics_all_tlabs(); aoqi@0: heap->ensure_parsability(true); // retire TLABs aoqi@0: aoqi@0: if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) { aoqi@0: HandleMark hm; // Discard invalid handles created during verification aoqi@0: Universe::verify(" VerifyBeforeGC:"); aoqi@0: } aoqi@0: aoqi@0: // Verify object start arrays aoqi@0: if (VerifyObjectStartArray && aoqi@0: VerifyBeforeGC) { aoqi@0: old_gen->verify_object_start_array(); aoqi@0: } aoqi@0: aoqi@0: heap->pre_full_gc_dump(_gc_timer); aoqi@0: aoqi@0: // Filled in below to track the state of the young gen after the collection. aoqi@0: bool eden_empty; aoqi@0: bool survivors_empty; aoqi@0: bool young_gen_empty; aoqi@0: aoqi@0: { aoqi@0: HandleMark hm; aoqi@0: aoqi@0: gclog_or_tty->date_stamp(PrintGC && PrintGCDateStamps); aoqi@0: TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty); aoqi@0: GCTraceTime t1(GCCauseString("Full GC", gc_cause), PrintGC, !PrintGCDetails, NULL); aoqi@0: TraceCollectorStats tcs(counters()); aoqi@0: TraceMemoryManagerStats tms(true /* Full GC */,gc_cause); aoqi@0: aoqi@0: if (TraceGen1Time) accumulated_time()->start(); aoqi@0: aoqi@0: // Let the size policy know we're starting aoqi@0: size_policy->major_collection_begin(); aoqi@0: aoqi@0: CodeCache::gc_prologue(); aoqi@0: Threads::gc_prologue(); aoqi@0: BiasedLocking::preserve_marks(); aoqi@0: aoqi@0: // Capture heap size before collection for printing. aoqi@0: size_t prev_used = heap->used(); aoqi@0: aoqi@0: // Capture metadata size before collection for sizing. aoqi@0: size_t metadata_prev_used = MetaspaceAux::used_bytes(); aoqi@0: aoqi@0: // For PrintGCDetails aoqi@0: size_t old_gen_prev_used = old_gen->used_in_bytes(); aoqi@0: size_t young_gen_prev_used = young_gen->used_in_bytes(); aoqi@0: aoqi@0: allocate_stacks(); aoqi@0: aoqi@0: COMPILER2_PRESENT(DerivedPointerTable::clear()); aoqi@0: aoqi@0: ref_processor()->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/); aoqi@0: ref_processor()->setup_policy(clear_all_softrefs); aoqi@0: aoqi@0: mark_sweep_phase1(clear_all_softrefs); aoqi@0: aoqi@0: mark_sweep_phase2(); aoqi@0: aoqi@0: // Don't add any more derived pointers during phase3 aoqi@0: COMPILER2_PRESENT(assert(DerivedPointerTable::is_active(), "Sanity")); aoqi@0: COMPILER2_PRESENT(DerivedPointerTable::set_active(false)); aoqi@0: aoqi@0: mark_sweep_phase3(); aoqi@0: aoqi@0: mark_sweep_phase4(); aoqi@0: aoqi@0: restore_marks(); aoqi@0: aoqi@0: deallocate_stacks(); aoqi@0: aoqi@0: if (ZapUnusedHeapArea) { aoqi@0: // Do a complete mangle (top to end) because the usage for aoqi@0: // scratch does not maintain a top pointer. aoqi@0: young_gen->to_space()->mangle_unused_area_complete(); aoqi@0: } aoqi@0: aoqi@0: eden_empty = young_gen->eden_space()->is_empty(); aoqi@0: if (!eden_empty) { aoqi@0: eden_empty = absorb_live_data_from_eden(size_policy, young_gen, old_gen); aoqi@0: } aoqi@0: aoqi@0: // Update heap occupancy information which is used as aoqi@0: // input to soft ref clearing policy at the next gc. aoqi@0: Universe::update_heap_info_at_gc(); aoqi@0: aoqi@0: survivors_empty = young_gen->from_space()->is_empty() && aoqi@0: young_gen->to_space()->is_empty(); aoqi@0: young_gen_empty = eden_empty && survivors_empty; aoqi@0: aoqi@0: BarrierSet* bs = heap->barrier_set(); aoqi@0: if (bs->is_a(BarrierSet::ModRef)) { aoqi@0: ModRefBarrierSet* modBS = (ModRefBarrierSet*)bs; aoqi@0: MemRegion old_mr = heap->old_gen()->reserved(); aoqi@0: if (young_gen_empty) { aoqi@0: modBS->clear(MemRegion(old_mr.start(), old_mr.end())); aoqi@0: } else { aoqi@0: modBS->invalidate(MemRegion(old_mr.start(), old_mr.end())); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Delete metaspaces for unloaded class loaders and clean up loader_data graph aoqi@0: ClassLoaderDataGraph::purge(); aoqi@0: MetaspaceAux::verify_metrics(); aoqi@0: aoqi@0: BiasedLocking::restore_marks(); aoqi@0: Threads::gc_epilogue(); aoqi@0: CodeCache::gc_epilogue(); aoqi@0: JvmtiExport::gc_epilogue(); aoqi@0: aoqi@0: COMPILER2_PRESENT(DerivedPointerTable::update_pointers()); aoqi@0: aoqi@0: ref_processor()->enqueue_discovered_references(NULL); aoqi@0: aoqi@0: // Update time of last GC aoqi@0: reset_millis_since_last_gc(); aoqi@0: aoqi@0: // Let the size policy know we're done aoqi@0: size_policy->major_collection_end(old_gen->used_in_bytes(), gc_cause); aoqi@0: aoqi@0: if (UseAdaptiveSizePolicy) { aoqi@0: aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: gclog_or_tty->print("AdaptiveSizeStart: "); aoqi@0: gclog_or_tty->stamp(); aoqi@0: gclog_or_tty->print_cr(" collection: %d ", aoqi@0: heap->total_collections()); aoqi@0: if (Verbose) { aoqi@0: gclog_or_tty->print("old_gen_capacity: %d young_gen_capacity: %d", aoqi@0: old_gen->capacity_in_bytes(), young_gen->capacity_in_bytes()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Don't check if the size_policy is ready here. Let aoqi@0: // the size_policy check that internally. aoqi@0: if (UseAdaptiveGenerationSizePolicyAtMajorCollection && aoqi@0: ((gc_cause != GCCause::_java_lang_system_gc) || aoqi@0: UseAdaptiveSizePolicyWithSystemGC)) { aoqi@0: // Calculate optimal free space amounts aoqi@0: assert(young_gen->max_size() > aoqi@0: young_gen->from_space()->capacity_in_bytes() + aoqi@0: young_gen->to_space()->capacity_in_bytes(), aoqi@0: "Sizes of space in young gen are out-of-bounds"); aoqi@0: aoqi@0: size_t young_live = young_gen->used_in_bytes(); aoqi@0: size_t eden_live = young_gen->eden_space()->used_in_bytes(); aoqi@0: size_t old_live = old_gen->used_in_bytes(); aoqi@0: size_t cur_eden = young_gen->eden_space()->capacity_in_bytes(); aoqi@0: size_t max_old_gen_size = old_gen->max_gen_size(); aoqi@0: size_t max_eden_size = young_gen->max_size() - aoqi@0: young_gen->from_space()->capacity_in_bytes() - aoqi@0: young_gen->to_space()->capacity_in_bytes(); aoqi@0: aoqi@0: // Used for diagnostics aoqi@0: size_policy->clear_generation_free_space_flags(); aoqi@0: aoqi@0: size_policy->compute_generations_free_space(young_live, aoqi@0: eden_live, aoqi@0: old_live, aoqi@0: cur_eden, aoqi@0: max_old_gen_size, aoqi@0: max_eden_size, aoqi@0: true /* full gc*/); aoqi@0: aoqi@0: size_policy->check_gc_overhead_limit(young_live, aoqi@0: eden_live, aoqi@0: max_old_gen_size, aoqi@0: max_eden_size, aoqi@0: true /* full gc*/, aoqi@0: gc_cause, aoqi@0: heap->collector_policy()); aoqi@0: aoqi@0: size_policy->decay_supplemental_growth(true /* full gc*/); aoqi@0: aoqi@0: heap->resize_old_gen(size_policy->calculated_old_free_size_in_bytes()); aoqi@0: aoqi@0: // Don't resize the young generation at an major collection. A aoqi@0: // desired young generation size may have been calculated but aoqi@0: // resizing the young generation complicates the code because the aoqi@0: // resizing of the old generation may have moved the boundary aoqi@0: // between the young generation and the old generation. Let the aoqi@0: // young generation resizing happen at the minor collections. aoqi@0: } aoqi@0: if (PrintAdaptiveSizePolicy) { aoqi@0: gclog_or_tty->print_cr("AdaptiveSizeStop: collection: %d ", aoqi@0: heap->total_collections()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (UsePerfData) { aoqi@0: heap->gc_policy_counters()->update_counters(); aoqi@0: heap->gc_policy_counters()->update_old_capacity( aoqi@0: old_gen->capacity_in_bytes()); aoqi@0: heap->gc_policy_counters()->update_young_capacity( aoqi@0: young_gen->capacity_in_bytes()); aoqi@0: } aoqi@0: aoqi@0: heap->resize_all_tlabs(); aoqi@0: aoqi@0: // We collected the heap, recalculate the metaspace capacity aoqi@0: MetaspaceGC::compute_new_size(); aoqi@0: aoqi@0: if (TraceGen1Time) accumulated_time()->stop(); aoqi@0: aoqi@0: if (PrintGC) { aoqi@0: if (PrintGCDetails) { aoqi@0: // Don't print a GC timestamp here. This is after the GC so aoqi@0: // would be confusing. aoqi@0: young_gen->print_used_change(young_gen_prev_used); aoqi@0: old_gen->print_used_change(old_gen_prev_used); aoqi@0: } aoqi@0: heap->print_heap_change(prev_used); aoqi@0: if (PrintGCDetails) { aoqi@0: MetaspaceAux::print_metaspace_change(metadata_prev_used); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Track memory usage and detect low memory aoqi@0: MemoryService::track_memory_usage(); aoqi@0: heap->update_counters(); aoqi@0: } aoqi@0: aoqi@0: if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) { aoqi@0: HandleMark hm; // Discard invalid handles created during verification aoqi@0: Universe::verify(" VerifyAfterGC:"); aoqi@0: } aoqi@0: aoqi@0: // Re-verify object start arrays aoqi@0: if (VerifyObjectStartArray && aoqi@0: VerifyAfterGC) { aoqi@0: old_gen->verify_object_start_array(); aoqi@0: } aoqi@0: aoqi@0: if (ZapUnusedHeapArea) { aoqi@0: old_gen->object_space()->check_mangled_unused_area_complete(); aoqi@0: } aoqi@0: aoqi@0: NOT_PRODUCT(ref_processor()->verify_no_references_recorded()); aoqi@0: aoqi@0: heap->print_heap_after_gc(); aoqi@0: heap->trace_heap_after_gc(_gc_tracer); aoqi@0: aoqi@0: heap->post_full_gc_dump(_gc_timer); aoqi@0: aoqi@0: #ifdef TRACESPINNING aoqi@0: ParallelTaskTerminator::print_termination_counts(); aoqi@0: #endif aoqi@0: aoqi@0: _gc_timer->register_gc_end(); aoqi@0: aoqi@0: _gc_tracer->report_gc_end(_gc_timer->gc_end(), _gc_timer->time_partitions()); aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: bool PSMarkSweep::absorb_live_data_from_eden(PSAdaptiveSizePolicy* size_policy, aoqi@0: PSYoungGen* young_gen, aoqi@0: PSOldGen* old_gen) { aoqi@0: MutableSpace* const eden_space = young_gen->eden_space(); aoqi@0: assert(!eden_space->is_empty(), "eden must be non-empty"); aoqi@0: assert(young_gen->virtual_space()->alignment() == aoqi@0: old_gen->virtual_space()->alignment(), "alignments do not match"); aoqi@0: aoqi@0: if (!(UseAdaptiveSizePolicy && UseAdaptiveGCBoundary)) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Both generations must be completely committed. aoqi@0: if (young_gen->virtual_space()->uncommitted_size() != 0) { aoqi@0: return false; aoqi@0: } aoqi@0: if (old_gen->virtual_space()->uncommitted_size() != 0) { aoqi@0: return false; aoqi@0: } aoqi@0: aoqi@0: // Figure out how much to take from eden. Include the average amount promoted aoqi@0: // in the total; otherwise the next young gen GC will simply bail out to a aoqi@0: // full GC. aoqi@0: const size_t alignment = old_gen->virtual_space()->alignment(); aoqi@0: const size_t eden_used = eden_space->used_in_bytes(); aoqi@0: const size_t promoted = (size_t)size_policy->avg_promoted()->padded_average(); aoqi@0: const size_t absorb_size = align_size_up(eden_used + promoted, alignment); aoqi@0: const size_t eden_capacity = eden_space->capacity_in_bytes(); aoqi@0: aoqi@0: if (absorb_size >= eden_capacity) { aoqi@0: return false; // Must leave some space in eden. aoqi@0: } aoqi@0: aoqi@0: const size_t new_young_size = young_gen->capacity_in_bytes() - absorb_size; aoqi@0: if (new_young_size < young_gen->min_gen_size()) { aoqi@0: return false; // Respect young gen minimum size. aoqi@0: } aoqi@0: aoqi@0: if (TraceAdaptiveGCBoundary && Verbose) { aoqi@0: gclog_or_tty->print(" absorbing " SIZE_FORMAT "K: " aoqi@0: "eden " SIZE_FORMAT "K->" SIZE_FORMAT "K " aoqi@0: "from " SIZE_FORMAT "K, to " SIZE_FORMAT "K " aoqi@0: "young_gen " SIZE_FORMAT "K->" SIZE_FORMAT "K ", aoqi@0: absorb_size / K, aoqi@0: eden_capacity / K, (eden_capacity - absorb_size) / K, aoqi@0: young_gen->from_space()->used_in_bytes() / K, aoqi@0: young_gen->to_space()->used_in_bytes() / K, aoqi@0: young_gen->capacity_in_bytes() / K, new_young_size / K); aoqi@0: } aoqi@0: aoqi@0: // Fill the unused part of the old gen. aoqi@0: MutableSpace* const old_space = old_gen->object_space(); aoqi@0: HeapWord* const unused_start = old_space->top(); aoqi@0: size_t const unused_words = pointer_delta(old_space->end(), unused_start); aoqi@0: aoqi@0: if (unused_words > 0) { aoqi@0: if (unused_words < CollectedHeap::min_fill_size()) { aoqi@0: return false; // If the old gen cannot be filled, must give up. aoqi@0: } aoqi@0: CollectedHeap::fill_with_objects(unused_start, unused_words); aoqi@0: } aoqi@0: aoqi@0: // Take the live data from eden and set both top and end in the old gen to aoqi@0: // eden top. (Need to set end because reset_after_change() mangles the region aoqi@0: // from end to virtual_space->high() in debug builds). aoqi@0: HeapWord* const new_top = eden_space->top(); aoqi@0: old_gen->virtual_space()->expand_into(young_gen->virtual_space(), aoqi@0: absorb_size); aoqi@0: young_gen->reset_after_change(); aoqi@0: old_space->set_top(new_top); aoqi@0: old_space->set_end(new_top); aoqi@0: old_gen->reset_after_change(); aoqi@0: aoqi@0: // Update the object start array for the filler object and the data from eden. aoqi@0: ObjectStartArray* const start_array = old_gen->start_array(); aoqi@0: for (HeapWord* p = unused_start; p < new_top; p += oop(p)->size()) { aoqi@0: start_array->allocate_block(p); aoqi@0: } aoqi@0: aoqi@0: // Could update the promoted average here, but it is not typically updated at aoqi@0: // full GCs and the value to use is unclear. Something like aoqi@0: // aoqi@0: // cur_promoted_avg + absorb_size / number_of_scavenges_since_last_full_gc. aoqi@0: aoqi@0: size_policy->set_bytes_absorbed_from_eden(absorb_size); aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void PSMarkSweep::allocate_stacks() { aoqi@0: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); aoqi@0: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); aoqi@0: aoqi@0: PSYoungGen* young_gen = heap->young_gen(); aoqi@0: aoqi@0: MutableSpace* to_space = young_gen->to_space(); aoqi@0: _preserved_marks = (PreservedMark*)to_space->top(); aoqi@0: _preserved_count = 0; aoqi@0: aoqi@0: // We want to calculate the size in bytes first. aoqi@0: _preserved_count_max = pointer_delta(to_space->end(), to_space->top(), sizeof(jbyte)); aoqi@0: // Now divide by the size of a PreservedMark aoqi@0: _preserved_count_max /= sizeof(PreservedMark); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void PSMarkSweep::deallocate_stacks() { aoqi@0: _preserved_mark_stack.clear(true); aoqi@0: _preserved_oop_stack.clear(true); aoqi@0: _marking_stack.clear(); aoqi@0: _objarray_stack.clear(true); aoqi@0: } aoqi@0: aoqi@0: void PSMarkSweep::mark_sweep_phase1(bool clear_all_softrefs) { aoqi@0: // Recursively traverse all live objects and mark them aoqi@0: GCTraceTime tm("phase 1", PrintGCDetails && Verbose, true, _gc_timer); aoqi@0: trace(" 1"); aoqi@0: aoqi@0: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); aoqi@0: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); aoqi@0: aoqi@0: // Need to clear claim bits before the tracing starts. aoqi@0: ClassLoaderDataGraph::clear_claimed_marks(); aoqi@0: aoqi@0: // General strong roots. aoqi@0: { aoqi@0: ParallelScavengeHeap::ParStrongRootsScope psrs; aoqi@0: Universe::oops_do(mark_and_push_closure()); aoqi@0: JNIHandles::oops_do(mark_and_push_closure()); // Global (strong) JNI handles aoqi@0: CLDToOopClosure mark_and_push_from_cld(mark_and_push_closure()); aoqi@0: CodeBlobToOopClosure each_active_code_blob(mark_and_push_closure(), /*do_marking=*/ true); aoqi@0: Threads::oops_do(mark_and_push_closure(), &mark_and_push_from_cld, &each_active_code_blob); aoqi@0: ObjectSynchronizer::oops_do(mark_and_push_closure()); aoqi@0: FlatProfiler::oops_do(mark_and_push_closure()); aoqi@0: Management::oops_do(mark_and_push_closure()); aoqi@0: JvmtiExport::oops_do(mark_and_push_closure()); aoqi@0: SystemDictionary::always_strong_oops_do(mark_and_push_closure()); aoqi@0: ClassLoaderDataGraph::always_strong_oops_do(mark_and_push_closure(), follow_klass_closure(), true); aoqi@0: // Do not treat nmethods as strong roots for mark/sweep, since we can unload them. aoqi@0: //CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure(mark_and_push_closure())); aoqi@0: } aoqi@0: aoqi@0: // Flush marking stack. aoqi@0: follow_stack(); aoqi@0: aoqi@0: // Process reference objects found during marking aoqi@0: { aoqi@0: ref_processor()->setup_policy(clear_all_softrefs); aoqi@0: const ReferenceProcessorStats& stats = aoqi@0: ref_processor()->process_discovered_references( aoqi@0: is_alive_closure(), mark_and_push_closure(), follow_stack_closure(), NULL, _gc_timer); aoqi@0: gc_tracer()->report_gc_reference_stats(stats); aoqi@0: } aoqi@0: aoqi@0: // This is the point where the entire marking should have completed. aoqi@0: assert(_marking_stack.is_empty(), "Marking should have completed"); aoqi@0: aoqi@0: // Unload classes and purge the SystemDictionary. aoqi@0: bool purged_class = SystemDictionary::do_unloading(is_alive_closure()); aoqi@0: aoqi@0: // Unload nmethods. aoqi@0: CodeCache::do_unloading(is_alive_closure(), purged_class); aoqi@0: aoqi@0: // Prune dead klasses from subklass/sibling/implementor lists. aoqi@0: Klass::clean_weak_klass_links(is_alive_closure()); aoqi@0: aoqi@0: // Delete entries for dead interned strings. aoqi@0: StringTable::unlink(is_alive_closure()); aoqi@0: aoqi@0: // Clean up unreferenced symbols in symbol table. aoqi@0: SymbolTable::unlink(); aoqi@0: _gc_tracer->report_object_count_after_gc(is_alive_closure()); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void PSMarkSweep::mark_sweep_phase2() { aoqi@0: GCTraceTime tm("phase 2", PrintGCDetails && Verbose, true, _gc_timer); aoqi@0: trace("2"); aoqi@0: aoqi@0: // Now all live objects are marked, compute the new object addresses. aoqi@0: aoqi@0: // It is not required that we traverse spaces in the same order in aoqi@0: // phase2, phase3 and phase4, but the ValidateMarkSweep live oops aoqi@0: // tracking expects us to do so. See comment under phase4. aoqi@0: aoqi@0: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); aoqi@0: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); aoqi@0: aoqi@0: PSOldGen* old_gen = heap->old_gen(); aoqi@0: aoqi@0: // Begin compacting into the old gen aoqi@0: PSMarkSweepDecorator::set_destination_decorator_tenured(); aoqi@0: aoqi@0: // This will also compact the young gen spaces. aoqi@0: old_gen->precompact(); aoqi@0: } aoqi@0: aoqi@0: // This should be moved to the shared markSweep code! aoqi@0: class PSAlwaysTrueClosure: public BoolObjectClosure { aoqi@0: public: aoqi@0: bool do_object_b(oop p) { return true; } aoqi@0: }; aoqi@0: static PSAlwaysTrueClosure always_true; aoqi@0: aoqi@0: void PSMarkSweep::mark_sweep_phase3() { aoqi@0: // Adjust the pointers to reflect the new locations aoqi@0: GCTraceTime tm("phase 3", PrintGCDetails && Verbose, true, _gc_timer); aoqi@0: trace("3"); aoqi@0: aoqi@0: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); aoqi@0: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); aoqi@0: aoqi@0: PSYoungGen* young_gen = heap->young_gen(); aoqi@0: PSOldGen* old_gen = heap->old_gen(); aoqi@0: aoqi@0: // Need to clear claim bits before the tracing starts. aoqi@0: ClassLoaderDataGraph::clear_claimed_marks(); aoqi@0: aoqi@0: // General strong roots. aoqi@0: Universe::oops_do(adjust_pointer_closure()); aoqi@0: JNIHandles::oops_do(adjust_pointer_closure()); // Global (strong) JNI handles aoqi@0: CLDToOopClosure adjust_from_cld(adjust_pointer_closure()); aoqi@0: Threads::oops_do(adjust_pointer_closure(), &adjust_from_cld, NULL); aoqi@0: ObjectSynchronizer::oops_do(adjust_pointer_closure()); aoqi@0: FlatProfiler::oops_do(adjust_pointer_closure()); aoqi@0: Management::oops_do(adjust_pointer_closure()); aoqi@0: JvmtiExport::oops_do(adjust_pointer_closure()); aoqi@0: // SO_AllClasses aoqi@0: SystemDictionary::oops_do(adjust_pointer_closure()); aoqi@0: ClassLoaderDataGraph::oops_do(adjust_pointer_closure(), adjust_klass_closure(), true); aoqi@0: aoqi@0: // Now adjust pointers in remaining weak roots. (All of which should aoqi@0: // have been cleared if they pointed to non-surviving objects.) aoqi@0: // Global (weak) JNI handles aoqi@0: JNIHandles::weak_oops_do(&always_true, adjust_pointer_closure()); aoqi@0: aoqi@0: CodeCache::oops_do(adjust_pointer_closure()); aoqi@0: StringTable::oops_do(adjust_pointer_closure()); aoqi@0: ref_processor()->weak_oops_do(adjust_pointer_closure()); aoqi@0: PSScavenge::reference_processor()->weak_oops_do(adjust_pointer_closure()); aoqi@0: aoqi@0: adjust_marks(); aoqi@0: aoqi@0: young_gen->adjust_pointers(); aoqi@0: old_gen->adjust_pointers(); aoqi@0: } aoqi@0: aoqi@0: void PSMarkSweep::mark_sweep_phase4() { aoqi@0: EventMark m("4 compact heap"); aoqi@0: GCTraceTime tm("phase 4", PrintGCDetails && Verbose, true, _gc_timer); aoqi@0: trace("4"); aoqi@0: aoqi@0: // All pointers are now adjusted, move objects accordingly aoqi@0: aoqi@0: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); aoqi@0: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); aoqi@0: aoqi@0: PSYoungGen* young_gen = heap->young_gen(); aoqi@0: PSOldGen* old_gen = heap->old_gen(); aoqi@0: aoqi@0: old_gen->compact(); aoqi@0: young_gen->compact(); aoqi@0: } aoqi@0: aoqi@0: jlong PSMarkSweep::millis_since_last_gc() { aoqi@0: // We need a monotonically non-deccreasing time in ms but aoqi@0: // os::javaTimeMillis() does not guarantee monotonicity. aoqi@0: jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; aoqi@0: jlong ret_val = now - _time_of_last_gc; aoqi@0: // XXX See note in genCollectedHeap::millis_since_last_gc(). aoqi@0: if (ret_val < 0) { aoqi@0: NOT_PRODUCT(warning("time warp: "INT64_FORMAT, ret_val);) aoqi@0: return 0; aoqi@0: } aoqi@0: return ret_val; aoqi@0: } aoqi@0: aoqi@0: void PSMarkSweep::reset_millis_since_last_gc() { aoqi@0: // We need a monotonically non-deccreasing time in ms but aoqi@0: // os::javaTimeMillis() does not guarantee monotonicity. aoqi@0: _time_of_last_gc = os::javaTimeNanos() / NANOSECS_PER_MILLISEC; aoqi@0: }