1 /*
   2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc_implementation/shared/collectorCounters.hpp"
  27 #include "gc_implementation/shared/parGCAllocBuffer.hpp"
  28 #include "memory/allocation.inline.hpp"
  29 #include "memory/blockOffsetTable.inline.hpp"
  30 #include "memory/generation.inline.hpp"
  31 #include "memory/generationSpec.hpp"
  32 #include "memory/space.hpp"
  33 #include "memory/tenuredGeneration.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "runtime/java.hpp"
  36 #include "utilities/macros.hpp"
  37 
  38 TenuredGeneration::TenuredGeneration(ReservedSpace rs,
  39                                      size_t initial_byte_size, int level,
  40                                      GenRemSet* remset) :
  41   OneContigSpaceCardGeneration(rs, initial_byte_size,
  42                                level, remset, NULL)
  43 {
  44   HeapWord* bottom = (HeapWord*) _virtual_space.low();
  45   HeapWord* end    = (HeapWord*) _virtual_space.high();
  46   _the_space  = new TenuredSpace(_bts, MemRegion(bottom, end));
  47   _the_space->reset_saved_mark();
  48   _shrink_factor = 0;
  49   _capacity_at_prologue = 0;
  50 
  51   _gc_stats = new GCStats();
  52 
  53   // initialize performance counters
  54 
  55   const char* gen_name = "old";
  56   GenCollectorPolicy* gcp = (GenCollectorPolicy*) GenCollectedHeap::heap()->collector_policy();
  57 
  58   // Generation Counters -- generation 1, 1 subspace
  59   _gen_counters = new GenerationCounters(gen_name, 1, 1,
  60       gcp->min_old_size(), gcp->max_old_size(), &_virtual_space);
  61 
  62   _gc_counters = new CollectorCounters("MSC", 1);
  63 
  64   _space_counters = new CSpaceCounters(gen_name, 0,
  65                                        _virtual_space.reserved_size(),
  66                                        _the_space, _gen_counters);
  67 }
  68 
  69 void TenuredGeneration::gc_prologue(bool full) {
  70   _capacity_at_prologue = capacity();
  71   _used_at_prologue = used();
  72 }
  73 
  74 bool TenuredGeneration::should_collect(bool  full,
  75                                        size_t size,
  76                                        bool   is_tlab) {
  77   // This should be one big conditional or (||), but I want to be able to tell
  78   // why it returns what it returns (without re-evaluating the conditionals
  79   // in case they aren't idempotent), so I'm doing it this way.
  80   // DeMorgan says it's okay.
  81   bool result = false;
  82   if (!result && full) {
  83     result = true;
  84     if (PrintGC && Verbose) {
  85       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
  86                     " full");
  87     }
  88   }
  89   if (!result && should_allocate(size, is_tlab)) {
  90     result = true;
  91     if (PrintGC && Verbose) {
  92       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
  93                     " should_allocate(" SIZE_FORMAT ")",
  94                     size);
  95     }
  96   }
  97   // If we don't have very much free space.
  98   // XXX: 10000 should be a percentage of the capacity!!!
  99   if (!result && free() < 10000) {
 100     result = true;
 101     if (PrintGC && Verbose) {
 102       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
 103                     " free(): " SIZE_FORMAT,
 104                     free());
 105     }
 106   }
 107   // If we had to expand to accommodate promotions from younger generations
 108   if (!result && _capacity_at_prologue < capacity()) {
 109     result = true;
 110     if (PrintGC && Verbose) {
 111       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
 112                     "_capacity_at_prologue: " SIZE_FORMAT " < capacity(): " SIZE_FORMAT,
 113                     _capacity_at_prologue, capacity());
 114     }
 115   }
 116   return result;
 117 }
 118 
 119 void TenuredGeneration::compute_new_size() {
 120   assert_locked_or_safepoint(Heap_lock);
 121 
 122   // Compute some numbers about the state of the heap.
 123   const size_t used_after_gc = used();
 124   const size_t capacity_after_gc = capacity();
 125 
 126   CardGeneration::compute_new_size();
 127 
 128   assert(used() == used_after_gc && used_after_gc <= capacity(),
 129          err_msg("used: " SIZE_FORMAT " used_after_gc: " SIZE_FORMAT
 130          " capacity: " SIZE_FORMAT, used(), used_after_gc, capacity()));
 131 }
 132 
 133 void TenuredGeneration::update_gc_stats(int current_level,
 134                                         bool full) {
 135   // If the next lower level(s) has been collected, gather any statistics
 136   // that are of interest at this point.
 137   if (!full && (current_level + 1) == level()) {
 138     // Calculate size of data promoted from the younger generations
 139     // before doing the collection.
 140     size_t used_before_gc = used();
 141 
 142     // If the younger gen collections were skipped, then the
 143     // number of promoted bytes will be 0 and adding it to the
 144     // average will incorrectly lessen the average.  It is, however,
 145     // also possible that no promotion was needed.
 146     if (used_before_gc >= _used_at_prologue) {
 147       size_t promoted_in_bytes = used_before_gc - _used_at_prologue;
 148       gc_stats()->avg_promoted()->sample(promoted_in_bytes);
 149     }
 150   }
 151 }
 152 
 153 void TenuredGeneration::update_counters() {
 154   if (UsePerfData) {
 155     _space_counters->update_all();
 156     _gen_counters->update_all();
 157   }
 158 }
 159 
 160 bool TenuredGeneration::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {
 161   size_t available = max_contiguous_available();
 162   size_t av_promo  = (size_t)gc_stats()->avg_promoted()->padded_average();
 163   bool   res = (available >= av_promo) || (available >= max_promotion_in_bytes);
 164   if (PrintGC && Verbose) {
 165     gclog_or_tty->print_cr(
 166       "Tenured: promo attempt is%s safe: available("SIZE_FORMAT") %s av_promo("SIZE_FORMAT"),"
 167       "max_promo("SIZE_FORMAT")",
 168       res? "":" not", available, res? ">=":"<",
 169       av_promo, max_promotion_in_bytes);
 170   }
 171   return res;
 172 }