1 /*
   2  * Copyright (c) 2001, 2014, 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/gcTimer.hpp"
  28 #include "gc_implementation/shared/parGCAllocBuffer.hpp"
  29 #include "memory/allocation.inline.hpp"
  30 #include "memory/blockOffsetTable.inline.hpp"
  31 #include "memory/cardGeneration.inline.hpp"
  32 #include "memory/generationSpec.hpp"
  33 #include "memory/genMarkSweep.hpp"
  34 #include "memory/genOopClosures.inline.hpp"
  35 #include "memory/space.hpp"
  36 #include "memory/tenuredGeneration.inline.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "runtime/java.hpp"
  39 #include "utilities/macros.hpp"
  40 
  41 TenuredGeneration::TenuredGeneration(ReservedSpace rs,
  42                                      size_t initial_byte_size, int level,
  43                                      GenRemSet* remset) :
  44   CardGeneration(rs, initial_byte_size, level, remset)
  45 {
  46   HeapWord* bottom = (HeapWord*) _virtual_space.low();
  47   HeapWord* end    = (HeapWord*) _virtual_space.high();
  48   _the_space  = new TenuredSpace(_bts, MemRegion(bottom, end));
  49   _the_space->reset_saved_mark();
  50   _shrink_factor = 0;
  51   _capacity_at_prologue = 0;
  52 
  53   _gc_stats = new GCStats();
  54 
  55   // initialize performance counters
  56 
  57   const char* gen_name = "old";
  58   GenCollectorPolicy* gcp = (GenCollectorPolicy*) GenCollectedHeap::heap()->collector_policy();
  59 
  60   // Generation Counters -- generation 1, 1 subspace
  61   _gen_counters = new GenerationCounters(gen_name, 1, 1,
  62       gcp->min_old_size(), gcp->max_old_size(), &_virtual_space);
  63 
  64   _gc_counters = new CollectorCounters("MSC", 1);
  65 
  66   _space_counters = new CSpaceCounters(gen_name, 0,
  67                                        _virtual_space.reserved_size(),
  68                                        _the_space, _gen_counters);
  69 }
  70 
  71 void TenuredGeneration::gc_prologue(bool full) {
  72   _capacity_at_prologue = capacity();
  73   _used_at_prologue = used();
  74 }
  75 
  76 bool TenuredGeneration::should_collect(bool  full,
  77                                        size_t size,
  78                                        bool   is_tlab) {
  79   // This should be one big conditional or (||), but I want to be able to tell
  80   // why it returns what it returns (without re-evaluating the conditionals
  81   // in case they aren't idempotent), so I'm doing it this way.
  82   // DeMorgan says it's okay.
  83   bool result = false;
  84   if (!result && full) {
  85     result = true;
  86     if (PrintGC && Verbose) {
  87       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
  88                     " full");
  89     }
  90   }
  91   if (!result && should_allocate(size, is_tlab)) {
  92     result = true;
  93     if (PrintGC && Verbose) {
  94       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
  95                     " should_allocate(" SIZE_FORMAT ")",
  96                     size);
  97     }
  98   }
  99   // If we don't have very much free space.
 100   // XXX: 10000 should be a percentage of the capacity!!!
 101   if (!result && free() < 10000) {
 102     result = true;
 103     if (PrintGC && Verbose) {
 104       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
 105                     " free(): " SIZE_FORMAT,
 106                     free());
 107     }
 108   }
 109   // If we had to expand to accommodate promotions from younger generations
 110   if (!result && _capacity_at_prologue < capacity()) {
 111     result = true;
 112     if (PrintGC && Verbose) {
 113       gclog_or_tty->print_cr("TenuredGeneration::should_collect: because"
 114                     "_capacity_at_prologue: " SIZE_FORMAT " < capacity(): " SIZE_FORMAT,
 115                     _capacity_at_prologue, capacity());
 116     }
 117   }
 118   return result;
 119 }
 120 
 121 void TenuredGeneration::compute_new_size() {
 122   assert_locked_or_safepoint(Heap_lock);
 123 
 124   // Compute some numbers about the state of the heap.
 125   const size_t used_after_gc = used();
 126   const size_t capacity_after_gc = capacity();
 127 
 128   CardGeneration::compute_new_size();
 129 
 130   assert(used() == used_after_gc && used_after_gc <= capacity(),
 131          err_msg("used: " SIZE_FORMAT " used_after_gc: " SIZE_FORMAT
 132          " capacity: " SIZE_FORMAT, used(), used_after_gc, capacity()));
 133 }
 134 
 135 void TenuredGeneration::update_gc_stats(int current_level,
 136                                         bool full) {
 137   // If the next lower level(s) has been collected, gather any statistics
 138   // that are of interest at this point.
 139   if (!full && (current_level + 1) == level()) {
 140     // Calculate size of data promoted from the younger generations
 141     // before doing the collection.
 142     size_t used_before_gc = used();
 143 
 144     // If the younger gen collections were skipped, then the
 145     // number of promoted bytes will be 0 and adding it to the
 146     // average will incorrectly lessen the average.  It is, however,
 147     // also possible that no promotion was needed.
 148     if (used_before_gc >= _used_at_prologue) {
 149       size_t promoted_in_bytes = used_before_gc - _used_at_prologue;
 150       gc_stats()->avg_promoted()->sample(promoted_in_bytes);
 151     }
 152   }
 153 }
 154 
 155 void TenuredGeneration::update_counters() {
 156   if (UsePerfData) {
 157     _space_counters->update_all();
 158     _gen_counters->update_all();
 159   }
 160 }
 161 
 162 bool TenuredGeneration::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {
 163   size_t available = max_contiguous_available();
 164   size_t av_promo  = (size_t)gc_stats()->avg_promoted()->padded_average();
 165   bool   res = (available >= av_promo) || (available >= max_promotion_in_bytes);
 166   if (PrintGC && Verbose) {
 167     gclog_or_tty->print_cr(
 168       "Tenured: promo attempt is%s safe: available("SIZE_FORMAT") %s av_promo("SIZE_FORMAT"),"
 169       "max_promo("SIZE_FORMAT")",
 170       res? "":" not", available, res? ">=":"<",
 171       av_promo, max_promotion_in_bytes);
 172   }
 173   return res;
 174 }
 175 
 176 void TenuredGeneration::collect(bool   full,
 177                                 bool   clear_all_soft_refs,
 178                                 size_t size,
 179                                 bool   is_tlab) {
 180   GenCollectedHeap* gch = GenCollectedHeap::heap();
 181 
 182   SpecializationStats::clear();
 183   // Temporarily expand the span of our ref processor, so
 184   // refs discovery is over the entire heap, not just this generation
 185   ReferenceProcessorSpanMutator
 186     x(ref_processor(), gch->reserved_region());
 187 
 188   STWGCTimer* gc_timer = GenMarkSweep::gc_timer();
 189   gc_timer->register_gc_start();
 190 
 191   SerialOldTracer* gc_tracer = GenMarkSweep::gc_tracer();
 192   gc_tracer->report_gc_start(gch->gc_cause(), gc_timer->gc_start());
 193 
 194   GenMarkSweep::invoke_at_safepoint(_level, ref_processor(), clear_all_soft_refs);
 195 
 196   gc_timer->register_gc_end();
 197 
 198   gc_tracer->report_gc_end(gc_timer->gc_end(), gc_timer->time_partitions());
 199 
 200   SpecializationStats::print();
 201 }
 202 
 203 HeapWord*
 204 TenuredGeneration::expand_and_allocate(size_t word_size,
 205                                        bool is_tlab,
 206                                        bool parallel) {
 207   assert(!is_tlab, "TenuredGeneration does not support TLAB allocation");
 208   if (parallel) {
 209     MutexLocker x(ParGCRareEvent_lock);
 210     HeapWord* result = NULL;
 211     size_t byte_size = word_size * HeapWordSize;
 212     while (true) {
 213       expand(byte_size, _min_heap_delta_bytes);
 214       if (GCExpandToAllocateDelayMillis > 0) {
 215         os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
 216       }
 217       result = _the_space->par_allocate(word_size);
 218       if ( result != NULL) {
 219         return result;
 220       } else {
 221         // If there's not enough expansion space available, give up.
 222         if (_virtual_space.uncommitted_size() < byte_size) {
 223           return NULL;
 224         }
 225         // else try again
 226       }
 227     }
 228   } else {
 229     expand(word_size*HeapWordSize, _min_heap_delta_bytes);
 230     return _the_space->allocate(word_size);
 231   }
 232 }
 233 
 234 bool TenuredGeneration::expand(size_t bytes, size_t expand_bytes) {
 235   GCMutexLocker x(ExpandHeap_lock);
 236   return CardGeneration::expand(bytes, expand_bytes);
 237 }
 238 
 239 size_t TenuredGeneration::unsafe_max_alloc_nogc() const {
 240   return _the_space->free();
 241 }
 242 
 243 size_t TenuredGeneration::contiguous_available() const {
 244   return _the_space->free() + _virtual_space.uncommitted_size();
 245 }
 246 
 247 void TenuredGeneration::assert_correct_size_change_locking() {
 248   assert_locked_or_safepoint(ExpandHeap_lock);
 249 }
 250 
 251 // Currently nothing to do.
 252 void TenuredGeneration::prepare_for_verify() {}
 253 
 254 void TenuredGeneration::object_iterate(ObjectClosure* blk) {
 255   _the_space->object_iterate(blk);
 256 }
 257 
 258 void TenuredGeneration::save_marks() {
 259   _the_space->set_saved_mark();
 260 }
 261 
 262 void TenuredGeneration::reset_saved_marks() {
 263   _the_space->reset_saved_mark();
 264 }
 265 
 266 bool TenuredGeneration::no_allocs_since_save_marks() {
 267   return _the_space->saved_mark_at_top();
 268 }
 269 
 270 #define TenuredGen_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix)     \
 271                                                                                 \
 272 void TenuredGeneration::                                                        \
 273 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) {                  \
 274   blk->set_generation(this);                                                    \
 275   _the_space->oop_since_save_marks_iterate##nv_suffix(blk);                     \
 276   blk->reset_generation();                                                      \
 277   save_marks();                                                                 \
 278 }
 279 
 280 ALL_SINCE_SAVE_MARKS_CLOSURES(TenuredGen_SINCE_SAVE_MARKS_ITERATE_DEFN)
 281 
 282 #undef TenuredGen_SINCE_SAVE_MARKS_ITERATE_DEFN
 283 
 284 void TenuredGeneration::gc_epilogue(bool full) {
 285   // update the generation and space performance counters
 286   update_counters();
 287   if (ZapUnusedHeapArea) {
 288     _the_space->check_mangled_unused_area_complete();
 289   }
 290 }
 291 
 292 void TenuredGeneration::record_spaces_top() {
 293   assert(ZapUnusedHeapArea, "Not mangling unused space");
 294   _the_space->set_top_for_allocations();
 295 }
 296 
 297 void TenuredGeneration::verify() {
 298   _the_space->verify();
 299 }
 300 
 301 void TenuredGeneration::print_on(outputStream* st)  const {
 302   Generation::print_on(st);
 303   st->print("   the");
 304   _the_space->print_on(st);
 305 }