aoqi@0: /* aoqi@0: * Copyright (c) 2001, 2013, 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 "gc_implementation/shared/collectorCounters.hpp" aoqi@0: #include "gc_implementation/shared/parGCAllocBuffer.hpp" aoqi@0: #include "memory/allocation.inline.hpp" aoqi@0: #include "memory/blockOffsetTable.inline.hpp" aoqi@0: #include "memory/generation.inline.hpp" aoqi@0: #include "memory/generationSpec.hpp" aoqi@0: #include "memory/space.hpp" aoqi@0: #include "memory/tenuredGeneration.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "runtime/java.hpp" aoqi@0: #include "utilities/macros.hpp" aoqi@0: aoqi@0: TenuredGeneration::TenuredGeneration(ReservedSpace rs, aoqi@0: size_t initial_byte_size, int level, aoqi@0: GenRemSet* remset) : aoqi@0: OneContigSpaceCardGeneration(rs, initial_byte_size, aoqi@0: level, remset, NULL) aoqi@0: { aoqi@0: HeapWord* bottom = (HeapWord*) _virtual_space.low(); aoqi@0: HeapWord* end = (HeapWord*) _virtual_space.high(); aoqi@0: _the_space = new TenuredSpace(_bts, MemRegion(bottom, end)); aoqi@0: _the_space->reset_saved_mark(); aoqi@0: _shrink_factor = 0; aoqi@0: _capacity_at_prologue = 0; aoqi@0: aoqi@0: _gc_stats = new GCStats(); aoqi@0: aoqi@0: // initialize performance counters aoqi@0: aoqi@0: const char* gen_name = "old"; aoqi@0: aoqi@0: // Generation Counters -- generation 1, 1 subspace aoqi@0: _gen_counters = new GenerationCounters(gen_name, 1, 1, &_virtual_space); aoqi@0: aoqi@0: _gc_counters = new CollectorCounters("MSC", 1); aoqi@0: aoqi@0: _space_counters = new CSpaceCounters(gen_name, 0, aoqi@0: _virtual_space.reserved_size(), aoqi@0: _the_space, _gen_counters); aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: if (UseParNewGC) { aoqi@0: typedef ParGCAllocBufferWithBOT* ParGCAllocBufferWithBOTPtr; aoqi@0: _alloc_buffers = NEW_C_HEAP_ARRAY(ParGCAllocBufferWithBOTPtr, aoqi@0: ParallelGCThreads, mtGC); aoqi@0: if (_alloc_buffers == NULL) aoqi@0: vm_exit_during_initialization("Could not allocate alloc_buffers"); aoqi@0: for (uint i = 0; i < ParallelGCThreads; i++) { aoqi@0: _alloc_buffers[i] = aoqi@0: new ParGCAllocBufferWithBOT(OldPLABSize, _bts); aoqi@0: if (_alloc_buffers[i] == NULL) aoqi@0: vm_exit_during_initialization("Could not allocate alloc_buffers"); aoqi@0: } aoqi@0: } else { aoqi@0: _alloc_buffers = NULL; aoqi@0: } aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: } aoqi@0: aoqi@0: aoqi@0: const char* TenuredGeneration::name() const { aoqi@0: return "tenured generation"; aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::gc_prologue(bool full) { aoqi@0: _capacity_at_prologue = capacity(); aoqi@0: _used_at_prologue = used(); aoqi@0: if (VerifyBeforeGC) { aoqi@0: verify_alloc_buffers_clean(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::gc_epilogue(bool full) { aoqi@0: if (VerifyAfterGC) { aoqi@0: verify_alloc_buffers_clean(); aoqi@0: } aoqi@0: OneContigSpaceCardGeneration::gc_epilogue(full); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: bool TenuredGeneration::should_collect(bool full, aoqi@0: size_t size, aoqi@0: bool is_tlab) { aoqi@0: // This should be one big conditional or (||), but I want to be able to tell aoqi@0: // why it returns what it returns (without re-evaluating the conditionals aoqi@0: // in case they aren't idempotent), so I'm doing it this way. aoqi@0: // DeMorgan says it's okay. aoqi@0: bool result = false; aoqi@0: if (!result && full) { aoqi@0: result = true; aoqi@0: if (PrintGC && Verbose) { aoqi@0: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" aoqi@0: " full"); aoqi@0: } aoqi@0: } aoqi@0: if (!result && should_allocate(size, is_tlab)) { aoqi@0: result = true; aoqi@0: if (PrintGC && Verbose) { aoqi@0: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" aoqi@0: " should_allocate(" SIZE_FORMAT ")", aoqi@0: size); aoqi@0: } aoqi@0: } aoqi@0: // If we don't have very much free space. aoqi@0: // XXX: 10000 should be a percentage of the capacity!!! aoqi@0: if (!result && free() < 10000) { aoqi@0: result = true; aoqi@0: if (PrintGC && Verbose) { aoqi@0: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" aoqi@0: " free(): " SIZE_FORMAT, aoqi@0: free()); aoqi@0: } aoqi@0: } aoqi@0: // If we had to expand to accomodate promotions from younger generations aoqi@0: if (!result && _capacity_at_prologue < capacity()) { aoqi@0: result = true; aoqi@0: if (PrintGC && Verbose) { aoqi@0: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" aoqi@0: "_capacity_at_prologue: " SIZE_FORMAT " < capacity(): " SIZE_FORMAT, aoqi@0: _capacity_at_prologue, capacity()); aoqi@0: } aoqi@0: } aoqi@0: return result; aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::collect(bool full, aoqi@0: bool clear_all_soft_refs, aoqi@0: size_t size, aoqi@0: bool is_tlab) { aoqi@0: retire_alloc_buffers_before_full_gc(); aoqi@0: OneContigSpaceCardGeneration::collect(full, clear_all_soft_refs, aoqi@0: size, is_tlab); aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::compute_new_size() { aoqi@0: assert_locked_or_safepoint(Heap_lock); aoqi@0: aoqi@0: // Compute some numbers about the state of the heap. aoqi@0: const size_t used_after_gc = used(); aoqi@0: const size_t capacity_after_gc = capacity(); aoqi@0: aoqi@0: CardGeneration::compute_new_size(); aoqi@0: aoqi@0: assert(used() == used_after_gc && used_after_gc <= capacity(), aoqi@0: err_msg("used: " SIZE_FORMAT " used_after_gc: " SIZE_FORMAT aoqi@0: " capacity: " SIZE_FORMAT, used(), used_after_gc, capacity())); aoqi@0: } aoqi@0: void TenuredGeneration::update_gc_stats(int current_level, aoqi@0: bool full) { aoqi@0: // If the next lower level(s) has been collected, gather any statistics aoqi@0: // that are of interest at this point. aoqi@0: if (!full && (current_level + 1) == level()) { aoqi@0: // Calculate size of data promoted from the younger generations aoqi@0: // before doing the collection. aoqi@0: size_t used_before_gc = used(); aoqi@0: aoqi@0: // If the younger gen collections were skipped, then the aoqi@0: // number of promoted bytes will be 0 and adding it to the aoqi@0: // average will incorrectly lessen the average. It is, however, aoqi@0: // also possible that no promotion was needed. aoqi@0: if (used_before_gc >= _used_at_prologue) { aoqi@0: size_t promoted_in_bytes = used_before_gc - _used_at_prologue; aoqi@0: gc_stats()->avg_promoted()->sample(promoted_in_bytes); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::update_counters() { aoqi@0: if (UsePerfData) { aoqi@0: _space_counters->update_all(); aoqi@0: _gen_counters->update_all(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #if INCLUDE_ALL_GCS aoqi@0: oop TenuredGeneration::par_promote(int thread_num, aoqi@0: oop old, markOop m, size_t word_sz) { aoqi@0: aoqi@0: ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num]; aoqi@0: HeapWord* obj_ptr = buf->allocate(word_sz); aoqi@0: bool is_lab = true; aoqi@0: if (obj_ptr == NULL) { aoqi@0: #ifndef PRODUCT aoqi@0: if (Universe::heap()->promotion_should_fail()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: #endif // #ifndef PRODUCT aoqi@0: aoqi@0: // Slow path: aoqi@0: if (word_sz * 100 < ParallelGCBufferWastePct * buf->word_sz()) { aoqi@0: // Is small enough; abandon this buffer and start a new one. aoqi@0: size_t buf_size = buf->word_sz(); aoqi@0: HeapWord* buf_space = aoqi@0: TenuredGeneration::par_allocate(buf_size, false); aoqi@0: if (buf_space == NULL) { aoqi@0: buf_space = expand_and_allocate(buf_size, false, true /* parallel*/); aoqi@0: } aoqi@0: if (buf_space != NULL) { aoqi@0: buf->retire(false, false); aoqi@0: buf->set_buf(buf_space); aoqi@0: obj_ptr = buf->allocate(word_sz); aoqi@0: assert(obj_ptr != NULL, "Buffer was definitely big enough..."); aoqi@0: } aoqi@0: }; aoqi@0: // Otherwise, buffer allocation failed; try allocating object aoqi@0: // individually. aoqi@0: if (obj_ptr == NULL) { aoqi@0: obj_ptr = TenuredGeneration::par_allocate(word_sz, false); aoqi@0: if (obj_ptr == NULL) { aoqi@0: obj_ptr = expand_and_allocate(word_sz, false, true /* parallel */); aoqi@0: } aoqi@0: } aoqi@0: if (obj_ptr == NULL) return NULL; aoqi@0: } aoqi@0: assert(obj_ptr != NULL, "program logic"); aoqi@0: Copy::aligned_disjoint_words((HeapWord*)old, obj_ptr, word_sz); aoqi@0: oop obj = oop(obj_ptr); aoqi@0: // Restore the mark word copied above. aoqi@0: obj->set_mark(m); aoqi@0: return obj; aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::par_promote_alloc_undo(int thread_num, aoqi@0: HeapWord* obj, aoqi@0: size_t word_sz) { aoqi@0: ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num]; aoqi@0: if (buf->contains(obj)) { aoqi@0: guarantee(buf->contains(obj + word_sz - 1), aoqi@0: "should contain whole object"); aoqi@0: buf->undo_allocation(obj, word_sz); aoqi@0: } else { aoqi@0: CollectedHeap::fill_with_object(obj, word_sz); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::par_promote_alloc_done(int thread_num) { aoqi@0: ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num]; aoqi@0: buf->retire(true, ParallelGCRetainPLAB); aoqi@0: } aoqi@0: aoqi@0: void TenuredGeneration::retire_alloc_buffers_before_full_gc() { aoqi@0: if (UseParNewGC) { aoqi@0: for (uint i = 0; i < ParallelGCThreads; i++) { aoqi@0: _alloc_buffers[i]->retire(true /*end_of_gc*/, false /*retain*/); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // Verify that any retained parallel allocation buffers do not aoqi@0: // intersect with dirty cards. aoqi@0: void TenuredGeneration::verify_alloc_buffers_clean() { aoqi@0: if (UseParNewGC) { aoqi@0: for (uint i = 0; i < ParallelGCThreads; i++) { aoqi@0: _rs->verify_aligned_region_empty(_alloc_buffers[i]->range()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: #else // INCLUDE_ALL_GCS aoqi@0: void TenuredGeneration::retire_alloc_buffers_before_full_gc() {} aoqi@0: void TenuredGeneration::verify_alloc_buffers_clean() {} aoqi@0: #endif // INCLUDE_ALL_GCS aoqi@0: aoqi@0: bool TenuredGeneration::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const { aoqi@0: size_t available = max_contiguous_available(); aoqi@0: size_t av_promo = (size_t)gc_stats()->avg_promoted()->padded_average(); aoqi@0: bool res = (available >= av_promo) || (available >= max_promotion_in_bytes); aoqi@0: if (PrintGC && Verbose) { aoqi@0: gclog_or_tty->print_cr( aoqi@0: "Tenured: promo attempt is%s safe: available("SIZE_FORMAT") %s av_promo("SIZE_FORMAT")," aoqi@0: "max_promo("SIZE_FORMAT")", aoqi@0: res? "":" not", available, res? ">=":"<", aoqi@0: av_promo, max_promotion_in_bytes); aoqi@0: } aoqi@0: return res; aoqi@0: }