duke@435: /* johnc@3982: * Copyright (c) 2001, 2012, Oracle and/or its affiliates. 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: * 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. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "gc_implementation/shared/collectorCounters.hpp" johnc@3982: #include "gc_implementation/shared/parGCAllocBuffer.hpp" stefank@2314: #include "memory/allocation.inline.hpp" stefank@2314: #include "memory/blockOffsetTable.inline.hpp" stefank@2314: #include "memory/generation.inline.hpp" stefank@2314: #include "memory/generationSpec.hpp" stefank@2314: #include "memory/space.hpp" stefank@2314: #include "memory/tenuredGeneration.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "runtime/java.hpp" jprovino@4542: #include "utilities/macros.hpp" duke@435: duke@435: TenuredGeneration::TenuredGeneration(ReservedSpace rs, duke@435: size_t initial_byte_size, int level, duke@435: GenRemSet* remset) : duke@435: OneContigSpaceCardGeneration(rs, initial_byte_size, duke@435: MinHeapDeltaBytes, level, remset, NULL) duke@435: { duke@435: HeapWord* bottom = (HeapWord*) _virtual_space.low(); duke@435: HeapWord* end = (HeapWord*) _virtual_space.high(); duke@435: _the_space = new TenuredSpace(_bts, MemRegion(bottom, end)); duke@435: _the_space->reset_saved_mark(); duke@435: _shrink_factor = 0; duke@435: _capacity_at_prologue = 0; duke@435: duke@435: _gc_stats = new GCStats(); duke@435: duke@435: // initialize performance counters duke@435: duke@435: const char* gen_name = "old"; duke@435: duke@435: // Generation Counters -- generation 1, 1 subspace duke@435: _gen_counters = new GenerationCounters(gen_name, 1, 1, &_virtual_space); duke@435: duke@435: _gc_counters = new CollectorCounters("MSC", 1); duke@435: duke@435: _space_counters = new CSpaceCounters(gen_name, 0, duke@435: _virtual_space.reserved_size(), duke@435: _the_space, _gen_counters); jprovino@4542: #if INCLUDE_ALL_GCS brutisso@4387: if (UseParNewGC) { duke@435: typedef ParGCAllocBufferWithBOT* ParGCAllocBufferWithBOTPtr; duke@435: _alloc_buffers = NEW_C_HEAP_ARRAY(ParGCAllocBufferWithBOTPtr, zgu@3900: ParallelGCThreads, mtGC); duke@435: if (_alloc_buffers == NULL) duke@435: vm_exit_during_initialization("Could not allocate alloc_buffers"); duke@435: for (uint i = 0; i < ParallelGCThreads; i++) { duke@435: _alloc_buffers[i] = duke@435: new ParGCAllocBufferWithBOT(OldPLABSize, _bts); duke@435: if (_alloc_buffers[i] == NULL) duke@435: vm_exit_during_initialization("Could not allocate alloc_buffers"); duke@435: } duke@435: } else { duke@435: _alloc_buffers = NULL; duke@435: } jprovino@4542: #endif // INCLUDE_ALL_GCS duke@435: } duke@435: duke@435: duke@435: const char* TenuredGeneration::name() const { duke@435: return "tenured generation"; duke@435: } duke@435: duke@435: void TenuredGeneration::compute_new_size() { duke@435: assert(_shrink_factor <= 100, "invalid shrink factor"); duke@435: size_t current_shrink_factor = _shrink_factor; duke@435: _shrink_factor = 0; duke@435: duke@435: // We don't have floating point command-line arguments duke@435: // Note: argument processing ensures that MinHeapFreeRatio < 100. duke@435: const double minimum_free_percentage = MinHeapFreeRatio / 100.0; duke@435: const double maximum_used_percentage = 1.0 - minimum_free_percentage; duke@435: duke@435: // Compute some numbers about the state of the heap. duke@435: const size_t used_after_gc = used(); duke@435: const size_t capacity_after_gc = capacity(); duke@435: duke@435: const double min_tmp = used_after_gc / maximum_used_percentage; duke@435: size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx)); duke@435: // Don't shrink less than the initial generation size duke@435: minimum_desired_capacity = MAX2(minimum_desired_capacity, duke@435: spec()->init_size()); duke@435: assert(used_after_gc <= minimum_desired_capacity, "sanity check"); duke@435: duke@435: if (PrintGC && Verbose) { duke@435: const size_t free_after_gc = free(); duke@435: const double free_percentage = ((double)free_after_gc) / capacity_after_gc; duke@435: gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: "); duke@435: gclog_or_tty->print_cr(" " duke@435: " minimum_free_percentage: %6.2f" duke@435: " maximum_used_percentage: %6.2f", duke@435: minimum_free_percentage, duke@435: maximum_used_percentage); duke@435: gclog_or_tty->print_cr(" " duke@435: " free_after_gc : %6.1fK" duke@435: " used_after_gc : %6.1fK" duke@435: " capacity_after_gc : %6.1fK", duke@435: free_after_gc / (double) K, duke@435: used_after_gc / (double) K, duke@435: capacity_after_gc / (double) K); duke@435: gclog_or_tty->print_cr(" " duke@435: " free_percentage: %6.2f", duke@435: free_percentage); duke@435: } duke@435: duke@435: if (capacity_after_gc < minimum_desired_capacity) { duke@435: // If we have less free space than we want then expand duke@435: size_t expand_bytes = minimum_desired_capacity - capacity_after_gc; duke@435: // Don't expand unless it's significant duke@435: if (expand_bytes >= _min_heap_delta_bytes) { duke@435: expand(expand_bytes, 0); // safe if expansion fails duke@435: } duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr(" expanding:" duke@435: " minimum_desired_capacity: %6.1fK" duke@435: " expand_bytes: %6.1fK" duke@435: " _min_heap_delta_bytes: %6.1fK", duke@435: minimum_desired_capacity / (double) K, duke@435: expand_bytes / (double) K, duke@435: _min_heap_delta_bytes / (double) K); duke@435: } duke@435: return; duke@435: } duke@435: duke@435: // No expansion, now see if we want to shrink duke@435: size_t shrink_bytes = 0; duke@435: // We would never want to shrink more than this duke@435: size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity; duke@435: duke@435: if (MaxHeapFreeRatio < 100) { duke@435: const double maximum_free_percentage = MaxHeapFreeRatio / 100.0; duke@435: const double minimum_used_percentage = 1.0 - maximum_free_percentage; duke@435: const double max_tmp = used_after_gc / minimum_used_percentage; duke@435: size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx)); duke@435: maximum_desired_capacity = MAX2(maximum_desired_capacity, duke@435: spec()->init_size()); duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr(" " duke@435: " maximum_free_percentage: %6.2f" duke@435: " minimum_used_percentage: %6.2f", duke@435: maximum_free_percentage, duke@435: minimum_used_percentage); duke@435: gclog_or_tty->print_cr(" " duke@435: " _capacity_at_prologue: %6.1fK" duke@435: " minimum_desired_capacity: %6.1fK" duke@435: " maximum_desired_capacity: %6.1fK", duke@435: _capacity_at_prologue / (double) K, duke@435: minimum_desired_capacity / (double) K, duke@435: maximum_desired_capacity / (double) K); duke@435: } duke@435: assert(minimum_desired_capacity <= maximum_desired_capacity, duke@435: "sanity check"); duke@435: duke@435: if (capacity_after_gc > maximum_desired_capacity) { duke@435: // Capacity too large, compute shrinking size duke@435: shrink_bytes = capacity_after_gc - maximum_desired_capacity; duke@435: // We don't want shrink all the way back to initSize if people call duke@435: // System.gc(), because some programs do that between "phases" and then duke@435: // we'd just have to grow the heap up again for the next phase. So we duke@435: // damp the shrinking: 0% on the first call, 10% on the second call, 40% duke@435: // on the third call, and 100% by the fourth call. But if we recompute duke@435: // size without shrinking, it goes back to 0%. duke@435: shrink_bytes = shrink_bytes / 100 * current_shrink_factor; duke@435: assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); duke@435: if (current_shrink_factor == 0) { duke@435: _shrink_factor = 10; duke@435: } else { duke@435: _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100); duke@435: } duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr(" " duke@435: " shrinking:" duke@435: " initSize: %.1fK" duke@435: " maximum_desired_capacity: %.1fK", duke@435: spec()->init_size() / (double) K, duke@435: maximum_desired_capacity / (double) K); duke@435: gclog_or_tty->print_cr(" " duke@435: " shrink_bytes: %.1fK" duke@435: " current_shrink_factor: %d" duke@435: " new shrink factor: %d" duke@435: " _min_heap_delta_bytes: %.1fK", duke@435: shrink_bytes / (double) K, duke@435: current_shrink_factor, duke@435: _shrink_factor, duke@435: _min_heap_delta_bytes / (double) K); duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (capacity_after_gc > _capacity_at_prologue) { duke@435: // We might have expanded for promotions, in which case we might want to duke@435: // take back that expansion if there's room after GC. That keeps us from duke@435: // stretching the heap with promotions when there's plenty of room. duke@435: size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue; duke@435: expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes); duke@435: // We have two shrinking computations, take the largest duke@435: shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion); duke@435: assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size"); duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr(" " duke@435: " aggressive shrinking:" duke@435: " _capacity_at_prologue: %.1fK" duke@435: " capacity_after_gc: %.1fK" duke@435: " expansion_for_promotion: %.1fK" duke@435: " shrink_bytes: %.1fK", duke@435: capacity_after_gc / (double) K, duke@435: _capacity_at_prologue / (double) K, duke@435: expansion_for_promotion / (double) K, duke@435: shrink_bytes / (double) K); duke@435: } duke@435: } duke@435: // Don't shrink unless it's significant duke@435: if (shrink_bytes >= _min_heap_delta_bytes) { duke@435: shrink(shrink_bytes); duke@435: } duke@435: assert(used() == used_after_gc && used_after_gc <= capacity(), duke@435: "sanity check"); duke@435: } duke@435: duke@435: void TenuredGeneration::gc_prologue(bool full) { duke@435: _capacity_at_prologue = capacity(); duke@435: _used_at_prologue = used(); duke@435: if (VerifyBeforeGC) { duke@435: verify_alloc_buffers_clean(); duke@435: } duke@435: } duke@435: duke@435: void TenuredGeneration::gc_epilogue(bool full) { duke@435: if (VerifyAfterGC) { duke@435: verify_alloc_buffers_clean(); duke@435: } duke@435: OneContigSpaceCardGeneration::gc_epilogue(full); duke@435: } duke@435: duke@435: duke@435: bool TenuredGeneration::should_collect(bool full, duke@435: size_t size, duke@435: bool is_tlab) { duke@435: // This should be one big conditional or (||), but I want to be able to tell duke@435: // why it returns what it returns (without re-evaluating the conditionals duke@435: // in case they aren't idempotent), so I'm doing it this way. duke@435: // DeMorgan says it's okay. duke@435: bool result = false; duke@435: if (!result && full) { duke@435: result = true; duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" duke@435: " full"); duke@435: } duke@435: } duke@435: if (!result && should_allocate(size, is_tlab)) { duke@435: result = true; duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" duke@435: " should_allocate(" SIZE_FORMAT ")", duke@435: size); duke@435: } duke@435: } duke@435: // If we don't have very much free space. duke@435: // XXX: 10000 should be a percentage of the capacity!!! duke@435: if (!result && free() < 10000) { duke@435: result = true; duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" duke@435: " free(): " SIZE_FORMAT, duke@435: free()); duke@435: } duke@435: } duke@435: // If we had to expand to accomodate promotions from younger generations duke@435: if (!result && _capacity_at_prologue < capacity()) { duke@435: result = true; duke@435: if (PrintGC && Verbose) { duke@435: gclog_or_tty->print_cr("TenuredGeneration::should_collect: because" duke@435: "_capacity_at_prologue: " SIZE_FORMAT " < capacity(): " SIZE_FORMAT, duke@435: _capacity_at_prologue, capacity()); duke@435: } duke@435: } duke@435: return result; duke@435: } duke@435: duke@435: void TenuredGeneration::collect(bool full, duke@435: bool clear_all_soft_refs, duke@435: size_t size, duke@435: bool is_tlab) { duke@435: retire_alloc_buffers_before_full_gc(); duke@435: OneContigSpaceCardGeneration::collect(full, clear_all_soft_refs, duke@435: size, is_tlab); duke@435: } duke@435: duke@435: void TenuredGeneration::update_gc_stats(int current_level, duke@435: bool full) { duke@435: // If the next lower level(s) has been collected, gather any statistics duke@435: // that are of interest at this point. duke@435: if (!full && (current_level + 1) == level()) { duke@435: // Calculate size of data promoted from the younger generations duke@435: // before doing the collection. duke@435: size_t used_before_gc = used(); duke@435: duke@435: // If the younger gen collections were skipped, then the duke@435: // number of promoted bytes will be 0 and adding it to the duke@435: // average will incorrectly lessen the average. It is, however, duke@435: // also possible that no promotion was needed. duke@435: if (used_before_gc >= _used_at_prologue) { duke@435: size_t promoted_in_bytes = used_before_gc - _used_at_prologue; duke@435: gc_stats()->avg_promoted()->sample(promoted_in_bytes); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void TenuredGeneration::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: jprovino@4542: #if INCLUDE_ALL_GCS duke@435: oop TenuredGeneration::par_promote(int thread_num, duke@435: oop old, markOop m, size_t word_sz) { duke@435: duke@435: ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num]; duke@435: HeapWord* obj_ptr = buf->allocate(word_sz); duke@435: bool is_lab = true; duke@435: if (obj_ptr == NULL) { duke@435: #ifndef PRODUCT duke@435: if (Universe::heap()->promotion_should_fail()) { duke@435: return NULL; duke@435: } duke@435: #endif // #ifndef PRODUCT duke@435: duke@435: // Slow path: duke@435: if (word_sz * 100 < ParallelGCBufferWastePct * buf->word_sz()) { duke@435: // Is small enough; abandon this buffer and start a new one. duke@435: size_t buf_size = buf->word_sz(); duke@435: HeapWord* buf_space = duke@435: TenuredGeneration::par_allocate(buf_size, false); duke@435: if (buf_space == NULL) { duke@435: buf_space = expand_and_allocate(buf_size, false, true /* parallel*/); duke@435: } duke@435: if (buf_space != NULL) { duke@435: buf->retire(false, false); duke@435: buf->set_buf(buf_space); duke@435: obj_ptr = buf->allocate(word_sz); duke@435: assert(obj_ptr != NULL, "Buffer was definitely big enough..."); duke@435: } duke@435: }; duke@435: // Otherwise, buffer allocation failed; try allocating object duke@435: // individually. duke@435: if (obj_ptr == NULL) { duke@435: obj_ptr = TenuredGeneration::par_allocate(word_sz, false); duke@435: if (obj_ptr == NULL) { duke@435: obj_ptr = expand_and_allocate(word_sz, false, true /* parallel */); duke@435: } duke@435: } duke@435: if (obj_ptr == NULL) return NULL; duke@435: } duke@435: assert(obj_ptr != NULL, "program logic"); duke@435: Copy::aligned_disjoint_words((HeapWord*)old, obj_ptr, word_sz); duke@435: oop obj = oop(obj_ptr); duke@435: // Restore the mark word copied above. duke@435: obj->set_mark(m); duke@435: return obj; duke@435: } duke@435: duke@435: void TenuredGeneration::par_promote_alloc_undo(int thread_num, duke@435: HeapWord* obj, duke@435: size_t word_sz) { duke@435: ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num]; duke@435: if (buf->contains(obj)) { duke@435: guarantee(buf->contains(obj + word_sz - 1), duke@435: "should contain whole object"); duke@435: buf->undo_allocation(obj, word_sz); duke@435: } else { jcoomes@916: CollectedHeap::fill_with_object(obj, word_sz); duke@435: } duke@435: } duke@435: duke@435: void TenuredGeneration::par_promote_alloc_done(int thread_num) { duke@435: ParGCAllocBufferWithBOT* buf = _alloc_buffers[thread_num]; duke@435: buf->retire(true, ParallelGCRetainPLAB); duke@435: } duke@435: duke@435: void TenuredGeneration::retire_alloc_buffers_before_full_gc() { duke@435: if (UseParNewGC) { duke@435: for (uint i = 0; i < ParallelGCThreads; i++) { duke@435: _alloc_buffers[i]->retire(true /*end_of_gc*/, false /*retain*/); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Verify that any retained parallel allocation buffers do not duke@435: // intersect with dirty cards. duke@435: void TenuredGeneration::verify_alloc_buffers_clean() { duke@435: if (UseParNewGC) { duke@435: for (uint i = 0; i < ParallelGCThreads; i++) { jmasa@441: _rs->verify_aligned_region_empty(_alloc_buffers[i]->range()); duke@435: } duke@435: } duke@435: } jmasa@441: jprovino@4542: #else // INCLUDE_ALL_GCS duke@435: void TenuredGeneration::retire_alloc_buffers_before_full_gc() {} duke@435: void TenuredGeneration::verify_alloc_buffers_clean() {} jprovino@4542: #endif // INCLUDE_ALL_GCS duke@435: ysr@2243: bool TenuredGeneration::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const { ysr@2243: size_t available = max_contiguous_available(); ysr@2243: size_t av_promo = (size_t)gc_stats()->avg_promoted()->padded_average(); ysr@2243: bool res = (available >= av_promo) || (available >= max_promotion_in_bytes); ysr@2243: if (PrintGC && Verbose) { ysr@2243: gclog_or_tty->print_cr( ysr@2243: "Tenured: promo attempt is%s safe: available("SIZE_FORMAT") %s av_promo("SIZE_FORMAT")," ysr@2243: "max_promo("SIZE_FORMAT")", ysr@2243: res? "":" not", available, res? ">=":"<", ysr@2243: av_promo, max_promotion_in_bytes); duke@435: } ysr@2243: return res; duke@435: }