1 /*
   2  * Copyright (c) 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 
  27 #include "memory/blockOffsetTable.inline.hpp"
  28 #include "memory/cardGeneration.inline.hpp"
  29 #include "memory/gcLocker.hpp"
  30 #include "memory/generationSpec.hpp"
  31 #include "memory/genOopClosures.inline.hpp"
  32 #include "memory/genRemSet.hpp"
  33 #include "memory/iterator.hpp"
  34 #include "memory/memRegion.hpp"
  35 #include "memory/space.inline.hpp"
  36 #include "runtime/java.hpp"
  37 
  38 CardGeneration::CardGeneration(ReservedSpace rs, size_t initial_byte_size,
  39                                int level,
  40                                GenRemSet* remset) :
  41   Generation(rs, initial_byte_size, level), _rs(remset),
  42   _shrink_factor(0), _min_heap_delta_bytes(), _capacity_at_prologue(),
  43   _used_at_prologue()
  44 {
  45   HeapWord* start = (HeapWord*)rs.base();
  46   size_t reserved_byte_size = rs.size();
  47   assert((uintptr_t(start) & 3) == 0, "bad alignment");
  48   assert((reserved_byte_size & 3) == 0, "bad alignment");
  49   MemRegion reserved_mr(start, heap_word_size(reserved_byte_size));
  50   _bts = new BlockOffsetSharedArray(reserved_mr,
  51                                     heap_word_size(initial_byte_size));
  52   MemRegion committed_mr(start, heap_word_size(initial_byte_size));
  53   _rs->resize_covered_region(committed_mr);
  54   if (_bts == NULL)
  55     vm_exit_during_initialization("Could not allocate a BlockOffsetArray");
  56 
  57   // Verify that the start and end of this generation is the start of a card.
  58   // If this wasn't true, a single card could span more than on generation,
  59   // which would cause problems when we commit/uncommit memory, and when we
  60   // clear and dirty cards.
  61   guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned");
  62   if (reserved_mr.end() != Universe::heap()->reserved_region().end()) {
  63     // Don't check at the very end of the heap as we'll assert that we're probing off
  64     // the end if we try.
  65     guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned");
  66   }
  67   _min_heap_delta_bytes = MinHeapDeltaBytes;
  68   _capacity_at_prologue = initial_byte_size;
  69   _used_at_prologue = 0;
  70 }
  71 
  72 bool CardGeneration::grow_by(size_t bytes) {
  73   assert_correct_size_change_locking();
  74   bool result = _virtual_space.expand_by(bytes);
  75   if (result) {
  76     size_t new_word_size =
  77        heap_word_size(_virtual_space.committed_size());
  78     MemRegion mr(space()->bottom(), new_word_size);
  79     // Expand card table
  80     Universe::heap()->barrier_set()->resize_covered_region(mr);
  81     // Expand shared block offset array
  82     _bts->resize(new_word_size);
  83 
  84     // Fix for bug #4668531
  85     if (ZapUnusedHeapArea) {
  86       MemRegion mangle_region(space()->end(),
  87       (HeapWord*)_virtual_space.high());
  88       SpaceMangler::mangle_region(mangle_region);
  89     }
  90 
  91     // Expand space -- also expands space's BOT
  92     // (which uses (part of) shared array above)
  93     space()->set_end((HeapWord*)_virtual_space.high());
  94 
  95     // update the space and generation capacity counters
  96     update_counters();
  97 
  98     if (Verbose && PrintGC) {
  99       size_t new_mem_size = _virtual_space.committed_size();
 100       size_t old_mem_size = new_mem_size - bytes;
 101       gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by "
 102                       SIZE_FORMAT "K to " SIZE_FORMAT "K",
 103                       name(), old_mem_size/K, bytes/K, new_mem_size/K);
 104     }
 105   }
 106   return result;
 107 }
 108 
 109 bool CardGeneration::expand(size_t bytes, size_t expand_bytes) {
 110   assert_locked_or_safepoint(Heap_lock);
 111   if (bytes == 0) {
 112     return true;  // That's what grow_by(0) would return
 113   }
 114   size_t aligned_bytes  = ReservedSpace::page_align_size_up(bytes);
 115   if (aligned_bytes == 0){
 116     // The alignment caused the number of bytes to wrap.  An expand_by(0) will
 117     // return true with the implication that an expansion was done when it
 118     // was not.  A call to expand implies a best effort to expand by "bytes"
 119     // but not a guarantee.  Align down to give a best effort.  This is likely
 120     // the most that the generation can expand since it has some capacity to
 121     // start with.
 122     aligned_bytes = ReservedSpace::page_align_size_down(bytes);
 123   }
 124   size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes);
 125   bool success = false;
 126   if (aligned_expand_bytes > aligned_bytes) {
 127     success = grow_by(aligned_expand_bytes);
 128   }
 129   if (!success) {
 130     success = grow_by(aligned_bytes);
 131   }
 132   if (!success) {
 133     success = grow_to_reserved();
 134   }
 135   if (PrintGC && Verbose) {
 136     if (success && GC_locker::is_active_and_needs_gc()) {
 137       gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead");
 138     }
 139   }
 140 
 141   return success;
 142 }
 143 
 144 bool CardGeneration::grow_to_reserved() {
 145   assert_correct_size_change_locking();
 146   bool success = true;
 147   const size_t remaining_bytes = _virtual_space.uncommitted_size();
 148   if (remaining_bytes > 0) {
 149     success = grow_by(remaining_bytes);
 150     DEBUG_ONLY(if (!success) warning("grow to reserved failed");)
 151   }
 152   return success;
 153 }
 154 
 155 void CardGeneration::shrink(size_t bytes) {
 156   assert_correct_size_change_locking();
 157 
 158   size_t size = ReservedSpace::page_align_size_down(bytes);
 159   if (size == 0) {
 160     return;
 161   }
 162 
 163   // Shrink committed space
 164   _virtual_space.shrink_by(size);
 165   // Shrink space; this also shrinks the space's BOT
 166   space()->set_end((HeapWord*) _virtual_space.high());
 167   size_t new_word_size = heap_word_size(space()->capacity());
 168   // Shrink the shared block offset array
 169   _bts->resize(new_word_size);
 170   MemRegion mr(space()->bottom(), new_word_size);
 171   // Shrink the card table
 172   Universe::heap()->barrier_set()->resize_covered_region(mr);
 173 
 174   if (Verbose && PrintGC) {
 175     size_t new_mem_size = _virtual_space.committed_size();
 176     size_t old_mem_size = new_mem_size + size;
 177     gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K to " SIZE_FORMAT "K",
 178                   name(), old_mem_size/K, new_mem_size/K);
 179   }
 180 }
 181 
 182 // No young generation references, clear this generation's cards.
 183 void CardGeneration::clear_remembered_set() {
 184   _rs->clear(reserved());
 185 }
 186 
 187 
 188 // Objects in this generation may have moved, invalidate this
 189 // generation's cards.
 190 void CardGeneration::invalidate_remembered_set() {
 191   _rs->invalidate(used_region());
 192 }
 193 
 194 
 195 void CardGeneration::compute_new_size() {
 196   assert(_shrink_factor <= 100, "invalid shrink factor");
 197   size_t current_shrink_factor = _shrink_factor;
 198   _shrink_factor = 0;
 199 
 200   // We don't have floating point command-line arguments
 201   // Note:  argument processing ensures that MinHeapFreeRatio < 100.
 202   const double minimum_free_percentage = MinHeapFreeRatio / 100.0;
 203   const double maximum_used_percentage = 1.0 - minimum_free_percentage;
 204 
 205   // Compute some numbers about the state of the heap.
 206   const size_t used_after_gc = used();
 207   const size_t capacity_after_gc = capacity();
 208 
 209   const double min_tmp = used_after_gc / maximum_used_percentage;
 210   size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx));
 211   // Don't shrink less than the initial generation size
 212   minimum_desired_capacity = MAX2(minimum_desired_capacity,
 213                                   spec()->init_size());
 214   assert(used_after_gc <= minimum_desired_capacity, "sanity check");
 215 
 216   if (PrintGC && Verbose) {
 217     const size_t free_after_gc = free();
 218     const double free_percentage = ((double)free_after_gc) / capacity_after_gc;
 219     gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: ");
 220     gclog_or_tty->print_cr("  "
 221                   "  minimum_free_percentage: %6.2f"
 222                   "  maximum_used_percentage: %6.2f",
 223                   minimum_free_percentage,
 224                   maximum_used_percentage);
 225     gclog_or_tty->print_cr("  "
 226                   "   free_after_gc   : %6.1fK"
 227                   "   used_after_gc   : %6.1fK"
 228                   "   capacity_after_gc   : %6.1fK",
 229                   free_after_gc / (double) K,
 230                   used_after_gc / (double) K,
 231                   capacity_after_gc / (double) K);
 232     gclog_or_tty->print_cr("  "
 233                   "   free_percentage: %6.2f",
 234                   free_percentage);
 235   }
 236 
 237   if (capacity_after_gc < minimum_desired_capacity) {
 238     // If we have less free space than we want then expand
 239     size_t expand_bytes = minimum_desired_capacity - capacity_after_gc;
 240     // Don't expand unless it's significant
 241     if (expand_bytes >= _min_heap_delta_bytes) {
 242       expand(expand_bytes, 0); // safe if expansion fails
 243     }
 244     if (PrintGC && Verbose) {
 245       gclog_or_tty->print_cr("    expanding:"
 246                     "  minimum_desired_capacity: %6.1fK"
 247                     "  expand_bytes: %6.1fK"
 248                     "  _min_heap_delta_bytes: %6.1fK",
 249                     minimum_desired_capacity / (double) K,
 250                     expand_bytes / (double) K,
 251                     _min_heap_delta_bytes / (double) K);
 252     }
 253     return;
 254   }
 255 
 256   // No expansion, now see if we want to shrink
 257   size_t shrink_bytes = 0;
 258   // We would never want to shrink more than this
 259   size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity;
 260 
 261   if (MaxHeapFreeRatio < 100) {
 262     const double maximum_free_percentage = MaxHeapFreeRatio / 100.0;
 263     const double minimum_used_percentage = 1.0 - maximum_free_percentage;
 264     const double max_tmp = used_after_gc / minimum_used_percentage;
 265     size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
 266     maximum_desired_capacity = MAX2(maximum_desired_capacity,
 267                                     spec()->init_size());
 268     if (PrintGC && Verbose) {
 269       gclog_or_tty->print_cr("  "
 270                              "  maximum_free_percentage: %6.2f"
 271                              "  minimum_used_percentage: %6.2f",
 272                              maximum_free_percentage,
 273                              minimum_used_percentage);
 274       gclog_or_tty->print_cr("  "
 275                              "  _capacity_at_prologue: %6.1fK"
 276                              "  minimum_desired_capacity: %6.1fK"
 277                              "  maximum_desired_capacity: %6.1fK",
 278                              _capacity_at_prologue / (double) K,
 279                              minimum_desired_capacity / (double) K,
 280                              maximum_desired_capacity / (double) K);
 281     }
 282     assert(minimum_desired_capacity <= maximum_desired_capacity,
 283            "sanity check");
 284 
 285     if (capacity_after_gc > maximum_desired_capacity) {
 286       // Capacity too large, compute shrinking size
 287       shrink_bytes = capacity_after_gc - maximum_desired_capacity;
 288       // We don't want shrink all the way back to initSize if people call
 289       // System.gc(), because some programs do that between "phases" and then
 290       // we'd just have to grow the heap up again for the next phase.  So we
 291       // damp the shrinking: 0% on the first call, 10% on the second call, 40%
 292       // on the third call, and 100% by the fourth call.  But if we recompute
 293       // size without shrinking, it goes back to 0%.
 294       shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
 295       assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size");
 296       if (current_shrink_factor == 0) {
 297         _shrink_factor = 10;
 298       } else {
 299         _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100);
 300       }
 301       if (PrintGC && Verbose) {
 302         gclog_or_tty->print_cr("  "
 303                       "  shrinking:"
 304                       "  initSize: %.1fK"
 305                       "  maximum_desired_capacity: %.1fK",
 306                       spec()->init_size() / (double) K,
 307                       maximum_desired_capacity / (double) K);
 308         gclog_or_tty->print_cr("  "
 309                       "  shrink_bytes: %.1fK"
 310                       "  current_shrink_factor: " SIZE_FORMAT
 311                       "  new shrink factor: " SIZE_FORMAT
 312                       "  _min_heap_delta_bytes: %.1fK",
 313                       shrink_bytes / (double) K,
 314                       current_shrink_factor,
 315                       _shrink_factor,
 316                       _min_heap_delta_bytes / (double) K);
 317       }
 318     }
 319   }
 320 
 321   if (capacity_after_gc > _capacity_at_prologue) {
 322     // We might have expanded for promotions, in which case we might want to
 323     // take back that expansion if there's room after GC.  That keeps us from
 324     // stretching the heap with promotions when there's plenty of room.
 325     size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue;
 326     expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes);
 327     // We have two shrinking computations, take the largest
 328     shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion);
 329     assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size");
 330     if (PrintGC && Verbose) {
 331       gclog_or_tty->print_cr("  "
 332                              "  aggressive shrinking:"
 333                              "  _capacity_at_prologue: %.1fK"
 334                              "  capacity_after_gc: %.1fK"
 335                              "  expansion_for_promotion: %.1fK"
 336                              "  shrink_bytes: %.1fK",
 337                              capacity_after_gc / (double) K,
 338                              _capacity_at_prologue / (double) K,
 339                              expansion_for_promotion / (double) K,
 340                              shrink_bytes / (double) K);
 341     }
 342   }
 343   // Don't shrink unless it's significant
 344   if (shrink_bytes >= _min_heap_delta_bytes) {
 345     shrink(shrink_bytes);
 346   }
 347 }
 348 
 349 // Currently nothing to do.
 350 void CardGeneration::prepare_for_verify() {}
 351 
 352 void CardGeneration::space_iterate(SpaceClosure* blk,
 353                                                  bool usedOnly) {
 354   blk->do_space(space());
 355 }
 356 
 357 void CardGeneration::younger_refs_iterate(OopsInGenClosure* blk) {
 358   blk->set_generation(this);
 359   younger_refs_in_space_iterate(space(), blk);
 360   blk->reset_generation();
 361 }