duke@435: /* xdono@772: * Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_psOldGen.cpp.incl" duke@435: duke@435: inline const char* PSOldGen::select_name() { duke@435: return UseParallelOldGC ? "ParOldGen" : "PSOldGen"; duke@435: } duke@435: duke@435: PSOldGen::PSOldGen(ReservedSpace rs, size_t alignment, duke@435: size_t initial_size, size_t min_size, size_t max_size, duke@435: const char* perf_data_name, int level): duke@435: _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size), duke@435: _max_gen_size(max_size) duke@435: { duke@435: initialize(rs, alignment, perf_data_name, level); duke@435: } duke@435: duke@435: PSOldGen::PSOldGen(size_t initial_size, duke@435: size_t min_size, size_t max_size, duke@435: const char* perf_data_name, int level): duke@435: _name(select_name()), _init_gen_size(initial_size), _min_gen_size(min_size), duke@435: _max_gen_size(max_size) duke@435: {} duke@435: duke@435: void PSOldGen::initialize(ReservedSpace rs, size_t alignment, duke@435: const char* perf_data_name, int level) { duke@435: initialize_virtual_space(rs, alignment); duke@435: initialize_work(perf_data_name, level); duke@435: // The old gen can grow to gen_size_limit(). _reserve reflects only duke@435: // the current maximum that can be committed. duke@435: assert(_reserved.byte_size() <= gen_size_limit(), "Consistency check"); duke@435: } duke@435: duke@435: void PSOldGen::initialize_virtual_space(ReservedSpace rs, size_t alignment) { duke@435: duke@435: _virtual_space = new PSVirtualSpace(rs, alignment); duke@435: if (!_virtual_space->expand_by(_init_gen_size)) { duke@435: vm_exit_during_initialization("Could not reserve enough space for " duke@435: "object heap"); duke@435: } duke@435: } duke@435: duke@435: void PSOldGen::initialize_work(const char* perf_data_name, int level) { duke@435: // duke@435: // Basic memory initialization duke@435: // duke@435: duke@435: MemRegion limit_reserved((HeapWord*)virtual_space()->low_boundary(), duke@435: heap_word_size(_max_gen_size)); duke@435: assert(limit_reserved.byte_size() == _max_gen_size, duke@435: "word vs bytes confusion"); duke@435: // duke@435: // Object start stuff duke@435: // duke@435: duke@435: start_array()->initialize(limit_reserved); duke@435: duke@435: _reserved = MemRegion((HeapWord*)virtual_space()->low_boundary(), duke@435: (HeapWord*)virtual_space()->high_boundary()); duke@435: duke@435: // duke@435: // Card table stuff duke@435: // duke@435: duke@435: MemRegion cmr((HeapWord*)virtual_space()->low(), duke@435: (HeapWord*)virtual_space()->high()); jmasa@698: if (ZapUnusedHeapArea) { jmasa@698: // Mangle newly committed space immediately rather than jmasa@698: // waiting for the initialization of the space even though jmasa@698: // mangling is related to spaces. Doing it here eliminates jmasa@698: // the need to carry along information that a complete mangling jmasa@698: // (bottom to end) needs to be done. jmasa@698: SpaceMangler::mangle_region(cmr); jmasa@698: } jmasa@698: duke@435: Universe::heap()->barrier_set()->resize_covered_region(cmr); duke@435: duke@435: CardTableModRefBS* _ct = (CardTableModRefBS*)Universe::heap()->barrier_set(); duke@435: assert (_ct->kind() == BarrierSet::CardTableModRef, "Sanity"); duke@435: duke@435: // Verify that the start and end of this generation is the start of a card. duke@435: // If this wasn't true, a single card could span more than one generation, duke@435: // which would cause problems when we commit/uncommit memory, and when we duke@435: // clear and dirty cards. duke@435: guarantee(_ct->is_card_aligned(_reserved.start()), "generation must be card aligned"); duke@435: if (_reserved.end() != Universe::heap()->reserved_region().end()) { duke@435: // Don't check at the very end of the heap as we'll assert that we're probing off duke@435: // the end if we try. duke@435: guarantee(_ct->is_card_aligned(_reserved.end()), "generation must be card aligned"); duke@435: } duke@435: duke@435: // duke@435: // ObjectSpace stuff duke@435: // duke@435: duke@435: _object_space = new MutableSpace(); duke@435: duke@435: if (_object_space == NULL) duke@435: vm_exit_during_initialization("Could not allocate an old gen space"); duke@435: jmasa@698: object_space()->initialize(cmr, jmasa@698: SpaceDecorator::Clear, jmasa@698: SpaceDecorator::Mangle); duke@435: duke@435: _object_mark_sweep = new PSMarkSweepDecorator(_object_space, start_array(), MarkSweepDeadRatio); duke@435: duke@435: if (_object_mark_sweep == NULL) duke@435: vm_exit_during_initialization("Could not complete allocation of old generation"); duke@435: duke@435: // Update the start_array duke@435: start_array()->set_covered_region(cmr); duke@435: duke@435: // Generation Counters, generation 'level', 1 subspace duke@435: _gen_counters = new PSGenerationCounters(perf_data_name, level, 1, duke@435: virtual_space()); duke@435: _space_counters = new SpaceCounters(perf_data_name, 0, duke@435: virtual_space()->reserved_size(), duke@435: _object_space, _gen_counters); duke@435: } duke@435: duke@435: // Assume that the generation has been allocated if its duke@435: // reserved size is not 0. duke@435: bool PSOldGen::is_allocated() { duke@435: return virtual_space()->reserved_size() != 0; duke@435: } duke@435: duke@435: void PSOldGen::precompact() { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: duke@435: // Reset start array first. duke@435: start_array()->reset(); duke@435: duke@435: object_mark_sweep()->precompact(); duke@435: duke@435: // Now compact the young gen duke@435: heap->young_gen()->precompact(); duke@435: } duke@435: duke@435: void PSOldGen::adjust_pointers() { duke@435: object_mark_sweep()->adjust_pointers(); duke@435: } duke@435: duke@435: void PSOldGen::compact() { duke@435: object_mark_sweep()->compact(ZapUnusedHeapArea); duke@435: } duke@435: duke@435: void PSOldGen::move_and_update(ParCompactionManager* cm) { duke@435: PSParallelCompact::move_and_update(cm, PSParallelCompact::old_space_id); duke@435: } duke@435: duke@435: size_t PSOldGen::contiguous_available() const { duke@435: return object_space()->free_in_bytes() + virtual_space()->uncommitted_size(); duke@435: } duke@435: duke@435: // Allocation. We report all successful allocations to the size policy duke@435: // Note that the perm gen does not use this method, and should not! duke@435: HeapWord* PSOldGen::allocate(size_t word_size, bool is_tlab) { duke@435: assert_locked_or_safepoint(Heap_lock); duke@435: HeapWord* res = allocate_noexpand(word_size, is_tlab); duke@435: duke@435: if (res == NULL) { duke@435: res = expand_and_allocate(word_size, is_tlab); duke@435: } duke@435: duke@435: // Allocations in the old generation need to be reported duke@435: if (res != NULL) { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: heap->size_policy()->tenured_allocation(word_size); duke@435: } duke@435: duke@435: return res; duke@435: } duke@435: duke@435: HeapWord* PSOldGen::expand_and_allocate(size_t word_size, bool is_tlab) { duke@435: assert(!is_tlab, "TLAB's are not supported in PSOldGen"); duke@435: expand(word_size*HeapWordSize); duke@435: if (GCExpandToAllocateDelayMillis > 0) { duke@435: os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false); duke@435: } duke@435: return allocate_noexpand(word_size, is_tlab); duke@435: } duke@435: duke@435: HeapWord* PSOldGen::expand_and_cas_allocate(size_t word_size) { duke@435: expand(word_size*HeapWordSize); duke@435: if (GCExpandToAllocateDelayMillis > 0) { duke@435: os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false); duke@435: } duke@435: return cas_allocate_noexpand(word_size); duke@435: } duke@435: duke@435: void PSOldGen::expand(size_t bytes) { jmasa@706: if (bytes == 0) { jmasa@706: return; jmasa@706: } duke@435: MutexLocker x(ExpandHeap_lock); duke@435: const size_t alignment = virtual_space()->alignment(); duke@435: size_t aligned_bytes = align_size_up(bytes, alignment); duke@435: size_t aligned_expand_bytes = align_size_up(MinHeapDeltaBytes, alignment); jmasa@706: if (aligned_bytes == 0){ jmasa@706: // The alignment caused the number of bytes to wrap. An expand_by(0) will jmasa@706: // return true with the implication that and expansion was done when it jmasa@706: // was not. A call to expand implies a best effort to expand by "bytes" jmasa@706: // but not a guarantee. Align down to give a best effort. This is likely jmasa@706: // the most that the generation can expand since it has some capacity to jmasa@706: // start with. jmasa@706: aligned_bytes = align_size_down(bytes, alignment); jmasa@706: } duke@435: duke@435: bool success = false; duke@435: if (aligned_expand_bytes > aligned_bytes) { duke@435: success = expand_by(aligned_expand_bytes); duke@435: } duke@435: if (!success) { duke@435: success = expand_by(aligned_bytes); duke@435: } duke@435: if (!success) { duke@435: success = expand_to_reserved(); duke@435: } duke@435: jmasa@706: if (PrintGC && Verbose) { jmasa@706: if (success && GC_locker::is_active()) { duke@435: gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: bool PSOldGen::expand_by(size_t bytes) { duke@435: assert_lock_strong(ExpandHeap_lock); duke@435: assert_locked_or_safepoint(Heap_lock); jmasa@706: if (bytes == 0) { jmasa@706: return true; // That's what virtual_space()->expand_by(0) would return jmasa@706: } duke@435: bool result = virtual_space()->expand_by(bytes); duke@435: if (result) { jmasa@698: if (ZapUnusedHeapArea) { jmasa@698: // We need to mangle the newly expanded area. The memregion spans jmasa@698: // end -> new_end, we assume that top -> end is already mangled. jmasa@698: // Do the mangling before post_resize() is called because jmasa@698: // the space is available for allocation after post_resize(); jmasa@698: HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high(); jmasa@698: assert(object_space()->end() < virtual_space_high, jmasa@698: "Should be true before post_resize()"); jmasa@698: MemRegion mangle_region(object_space()->end(), virtual_space_high); jmasa@698: // Note that the object space has not yet been updated to jmasa@698: // coincede with the new underlying virtual space. jmasa@698: SpaceMangler::mangle_region(mangle_region); jmasa@698: } duke@435: post_resize(); duke@435: if (UsePerfData) { duke@435: _space_counters->update_capacity(); duke@435: _gen_counters->update_all(); duke@435: } duke@435: } duke@435: duke@435: if (result && Verbose && PrintGC) { duke@435: size_t new_mem_size = virtual_space()->committed_size(); duke@435: size_t old_mem_size = new_mem_size - bytes; duke@435: gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by " duke@435: SIZE_FORMAT "K to " duke@435: SIZE_FORMAT "K", duke@435: name(), old_mem_size/K, bytes/K, new_mem_size/K); duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: bool PSOldGen::expand_to_reserved() { duke@435: assert_lock_strong(ExpandHeap_lock); duke@435: assert_locked_or_safepoint(Heap_lock); duke@435: duke@435: bool result = true; duke@435: const size_t remaining_bytes = virtual_space()->uncommitted_size(); duke@435: if (remaining_bytes > 0) { duke@435: result = expand_by(remaining_bytes); duke@435: DEBUG_ONLY(if (!result) warning("grow to reserve failed")); duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: void PSOldGen::shrink(size_t bytes) { duke@435: assert_lock_strong(ExpandHeap_lock); duke@435: assert_locked_or_safepoint(Heap_lock); duke@435: duke@435: size_t size = align_size_down(bytes, virtual_space()->alignment()); duke@435: if (size > 0) { duke@435: assert_lock_strong(ExpandHeap_lock); duke@435: virtual_space()->shrink_by(bytes); duke@435: post_resize(); duke@435: duke@435: if (Verbose && PrintGC) { duke@435: size_t new_mem_size = virtual_space()->committed_size(); duke@435: size_t old_mem_size = new_mem_size + bytes; duke@435: gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K by " duke@435: SIZE_FORMAT "K to " duke@435: SIZE_FORMAT "K", duke@435: name(), old_mem_size/K, bytes/K, new_mem_size/K); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void PSOldGen::resize(size_t desired_free_space) { duke@435: const size_t alignment = virtual_space()->alignment(); duke@435: const size_t size_before = virtual_space()->committed_size(); duke@435: size_t new_size = used_in_bytes() + desired_free_space; duke@435: if (new_size < used_in_bytes()) { duke@435: // Overflowed the addition. duke@435: new_size = gen_size_limit(); duke@435: } duke@435: // Adjust according to our min and max duke@435: new_size = MAX2(MIN2(new_size, gen_size_limit()), min_gen_size()); duke@435: duke@435: assert(gen_size_limit() >= reserved().byte_size(), "max new size problem?"); duke@435: new_size = align_size_up(new_size, alignment); duke@435: duke@435: const size_t current_size = capacity_in_bytes(); duke@435: duke@435: if (PrintAdaptiveSizePolicy && Verbose) { duke@435: gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: " duke@435: "desired free: " SIZE_FORMAT " used: " SIZE_FORMAT duke@435: " new size: " SIZE_FORMAT " current size " SIZE_FORMAT duke@435: " gen limits: " SIZE_FORMAT " / " SIZE_FORMAT, duke@435: desired_free_space, used_in_bytes(), new_size, current_size, duke@435: gen_size_limit(), min_gen_size()); duke@435: } duke@435: duke@435: if (new_size == current_size) { duke@435: // No change requested duke@435: return; duke@435: } duke@435: if (new_size > current_size) { duke@435: size_t change_bytes = new_size - current_size; duke@435: expand(change_bytes); duke@435: } else { duke@435: size_t change_bytes = current_size - new_size; duke@435: // shrink doesn't grab this lock, expand does. Is that right? duke@435: MutexLocker x(ExpandHeap_lock); duke@435: shrink(change_bytes); duke@435: } duke@435: duke@435: if (PrintAdaptiveSizePolicy) { duke@435: ParallelScavengeHeap* heap = (ParallelScavengeHeap*)Universe::heap(); duke@435: assert(heap->kind() == CollectedHeap::ParallelScavengeHeap, "Sanity"); duke@435: gclog_or_tty->print_cr("AdaptiveSizePolicy::old generation size: " duke@435: "collection: %d " duke@435: "(" SIZE_FORMAT ") -> (" SIZE_FORMAT ") ", duke@435: heap->total_collections(), duke@435: size_before, virtual_space()->committed_size()); duke@435: } duke@435: } duke@435: duke@435: // NOTE! We need to be careful about resizing. During a GC, multiple duke@435: // allocators may be active during heap expansion. If we allow the duke@435: // heap resizing to become visible before we have correctly resized duke@435: // all heap related data structures, we may cause program failures. duke@435: void PSOldGen::post_resize() { duke@435: // First construct a memregion representing the new size duke@435: MemRegion new_memregion((HeapWord*)virtual_space()->low(), duke@435: (HeapWord*)virtual_space()->high()); duke@435: size_t new_word_size = new_memregion.word_size(); duke@435: duke@435: start_array()->set_covered_region(new_memregion); duke@435: Universe::heap()->barrier_set()->resize_covered_region(new_memregion); duke@435: duke@435: HeapWord* const virtual_space_high = (HeapWord*) virtual_space()->high(); duke@435: duke@435: // ALWAYS do this last!! duke@435: object_space()->set_end(virtual_space_high); duke@435: duke@435: assert(new_word_size == heap_word_size(object_space()->capacity_in_bytes()), duke@435: "Sanity"); duke@435: } duke@435: duke@435: size_t PSOldGen::gen_size_limit() { duke@435: return _max_gen_size; duke@435: } duke@435: duke@435: void PSOldGen::reset_after_change() { duke@435: ShouldNotReachHere(); duke@435: return; duke@435: } duke@435: duke@435: size_t PSOldGen::available_for_expansion() { duke@435: ShouldNotReachHere(); duke@435: return 0; duke@435: } duke@435: duke@435: size_t PSOldGen::available_for_contraction() { duke@435: ShouldNotReachHere(); duke@435: return 0; duke@435: } duke@435: duke@435: void PSOldGen::print() const { print_on(tty);} duke@435: void PSOldGen::print_on(outputStream* st) const { duke@435: st->print(" %-15s", name()); duke@435: if (PrintGCDetails && Verbose) { duke@435: st->print(" total " SIZE_FORMAT ", used " SIZE_FORMAT, duke@435: capacity_in_bytes(), used_in_bytes()); duke@435: } else { duke@435: st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K", duke@435: capacity_in_bytes()/K, used_in_bytes()/K); duke@435: } duke@435: st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")", duke@435: virtual_space()->low_boundary(), duke@435: virtual_space()->high(), duke@435: virtual_space()->high_boundary()); duke@435: duke@435: st->print(" object"); object_space()->print_on(st); duke@435: } duke@435: duke@435: void PSOldGen::print_used_change(size_t prev_used) const { duke@435: gclog_or_tty->print(" [%s:", name()); duke@435: gclog_or_tty->print(" " SIZE_FORMAT "K" duke@435: "->" SIZE_FORMAT "K" duke@435: "(" SIZE_FORMAT "K)", duke@435: prev_used / K, used_in_bytes() / K, duke@435: capacity_in_bytes() / K); duke@435: gclog_or_tty->print("]"); duke@435: } duke@435: duke@435: void PSOldGen::update_counters() { duke@435: if (UsePerfData) { duke@435: _space_counters->update_all(); duke@435: _gen_counters->update_all(); duke@435: } duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void PSOldGen::space_invariants() { duke@435: assert(object_space()->end() == (HeapWord*) virtual_space()->high(), duke@435: "Space invariant"); duke@435: assert(object_space()->bottom() == (HeapWord*) virtual_space()->low(), duke@435: "Space invariant"); duke@435: assert(virtual_space()->low_boundary() <= virtual_space()->low(), duke@435: "Space invariant"); duke@435: assert(virtual_space()->high_boundary() >= virtual_space()->high(), duke@435: "Space invariant"); duke@435: assert(virtual_space()->low_boundary() == (char*) _reserved.start(), duke@435: "Space invariant"); duke@435: assert(virtual_space()->high_boundary() == (char*) _reserved.end(), duke@435: "Space invariant"); duke@435: assert(virtual_space()->committed_size() <= virtual_space()->reserved_size(), duke@435: "Space invariant"); duke@435: } duke@435: #endif duke@435: duke@435: void PSOldGen::verify(bool allow_dirty) { duke@435: object_space()->verify(allow_dirty); duke@435: } duke@435: class VerifyObjectStartArrayClosure : public ObjectClosure { duke@435: PSOldGen* _gen; duke@435: ObjectStartArray* _start_array; duke@435: duke@435: public: duke@435: VerifyObjectStartArrayClosure(PSOldGen* gen, ObjectStartArray* start_array) : duke@435: _gen(gen), _start_array(start_array) { } duke@435: duke@435: virtual void do_object(oop obj) { duke@435: HeapWord* test_addr = (HeapWord*)obj + 1; duke@435: guarantee(_start_array->object_start(test_addr) == (HeapWord*)obj, "ObjectStartArray cannot find start of object"); duke@435: guarantee(_start_array->is_block_allocated((HeapWord*)obj), "ObjectStartArray missing block allocation"); duke@435: } duke@435: }; duke@435: duke@435: void PSOldGen::verify_object_start_array() { duke@435: VerifyObjectStartArrayClosure check( this, &_start_array ); duke@435: object_iterate(&check); duke@435: } jmasa@698: jmasa@698: #ifndef PRODUCT jmasa@698: void PSOldGen::record_spaces_top() { jmasa@698: assert(ZapUnusedHeapArea, "Not mangling unused space"); jmasa@698: object_space()->set_top_for_allocations(); jmasa@698: } jmasa@698: #endif