1 /*
   2  * Copyright (c) 1997, 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/gcTimer.hpp"
  27 #include "gc_implementation/shared/gcTrace.hpp"
  28 #include "gc_implementation/shared/spaceDecorator.hpp"
  29 #include "gc_interface/collectedHeap.inline.hpp"
  30 #include "memory/allocation.inline.hpp"
  31 #include "memory/blockOffsetTable.inline.hpp"
  32 #include "memory/cardTableRS.hpp"
  33 #include "memory/gcLocker.inline.hpp"
  34 #include "memory/genCollectedHeap.hpp"
  35 #include "memory/genMarkSweep.hpp"
  36 #include "memory/genOopClosures.hpp"
  37 #include "memory/genOopClosures.inline.hpp"
  38 #include "memory/generation.hpp"
  39 #include "memory/generation.inline.hpp"
  40 #include "memory/space.inline.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "runtime/java.hpp"
  43 #include "utilities/copy.hpp"
  44 #include "utilities/events.hpp"
  45 
  46 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
  47 
  48 Generation::Generation(ReservedSpace rs, size_t initial_size, int level) :
  49   _level(level),
  50   _ref_processor(NULL) {
  51   if (!_virtual_space.initialize(rs, initial_size)) {
  52     vm_exit_during_initialization("Could not reserve enough space for "
  53                     "object heap");
  54   }
  55   // Mangle all of the the initial generation.
  56   if (ZapUnusedHeapArea) {
  57     MemRegion mangle_region((HeapWord*)_virtual_space.low(),
  58       (HeapWord*)_virtual_space.high());
  59     SpaceMangler::mangle_region(mangle_region);
  60   }
  61   _reserved = MemRegion((HeapWord*)_virtual_space.low_boundary(),
  62           (HeapWord*)_virtual_space.high_boundary());
  63 }
  64 
  65 GenerationSpec* Generation::spec() {
  66   GenCollectedHeap* gch = GenCollectedHeap::heap();
  67   assert(0 <= level() && level() < gch->_n_gens, "Bad gen level");
  68   return gch->_gen_specs[level()];
  69 }
  70 
  71 size_t Generation::max_capacity() const {
  72   return reserved().byte_size();
  73 }
  74 
  75 void Generation::print_heap_change(size_t prev_used) const {
  76   if (PrintGCDetails && Verbose) {
  77     gclog_or_tty->print(" "  SIZE_FORMAT
  78                         "->" SIZE_FORMAT
  79                         "("  SIZE_FORMAT ")",
  80                         prev_used, used(), capacity());
  81   } else {
  82     gclog_or_tty->print(" "  SIZE_FORMAT "K"
  83                         "->" SIZE_FORMAT "K"
  84                         "("  SIZE_FORMAT "K)",
  85                         prev_used / K, used() / K, capacity() / K);
  86   }
  87 }
  88 
  89 // By default we get a single threaded default reference processor;
  90 // generations needing multi-threaded refs processing or discovery override this method.
  91 void Generation::ref_processor_init() {
  92   assert(_ref_processor == NULL, "a reference processor already exists");
  93   assert(!_reserved.is_empty(), "empty generation?");
  94   _ref_processor = new ReferenceProcessor(_reserved);    // a vanilla reference processor
  95   if (_ref_processor == NULL) {
  96     vm_exit_during_initialization("Could not allocate ReferenceProcessor object");
  97   }
  98 }
  99 
 100 void Generation::print() const { print_on(tty); }
 101 
 102 void Generation::print_on(outputStream* st)  const {
 103   st->print(" %-20s", name());
 104   st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
 105              capacity()/K, used()/K);
 106   st->print_cr(" [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
 107               _virtual_space.low_boundary(),
 108               _virtual_space.high(),
 109               _virtual_space.high_boundary());
 110 }
 111 
 112 void Generation::print_summary_info() { print_summary_info_on(tty); }
 113 
 114 void Generation::print_summary_info_on(outputStream* st) {
 115   StatRecord* sr = stat_record();
 116   double time = sr->accumulated_time.seconds();
 117   st->print_cr("[Accumulated GC generation %d time %3.7f secs, "
 118                "%d GC's, avg GC time %3.7f]",
 119                level(), time, sr->invocations,
 120                sr->invocations > 0 ? time / sr->invocations : 0.0);
 121 }
 122 
 123 // Utility iterator classes
 124 
 125 class GenerationIsInReservedClosure : public SpaceClosure {
 126  public:
 127   const void* _p;
 128   Space* sp;
 129   virtual void do_space(Space* s) {
 130     if (sp == NULL) {
 131       if (s->is_in_reserved(_p)) sp = s;
 132     }
 133   }
 134   GenerationIsInReservedClosure(const void* p) : _p(p), sp(NULL) {}
 135 };
 136 
 137 class GenerationIsInClosure : public SpaceClosure {
 138  public:
 139   const void* _p;
 140   Space* sp;
 141   virtual void do_space(Space* s) {
 142     if (sp == NULL) {
 143       if (s->is_in(_p)) sp = s;
 144     }
 145   }
 146   GenerationIsInClosure(const void* p) : _p(p), sp(NULL) {}
 147 };
 148 
 149 bool Generation::is_in(const void* p) const {
 150   GenerationIsInClosure blk(p);
 151   ((Generation*)this)->space_iterate(&blk);
 152   return blk.sp != NULL;
 153 }
 154 
 155 DefNewGeneration* Generation::as_DefNewGeneration() {
 156   assert((kind() == Generation::DefNew) ||
 157          (kind() == Generation::ParNew),
 158     "Wrong youngest generation type");
 159   return (DefNewGeneration*) this;
 160 }
 161 
 162 Generation* Generation::next_gen() const {
 163   GenCollectedHeap* gch = GenCollectedHeap::heap();
 164   int next = level() + 1;
 165   if (next < gch->_n_gens) {
 166     return gch->_gens[next];
 167   } else {
 168     return NULL;
 169   }
 170 }
 171 
 172 size_t Generation::max_contiguous_available() const {
 173   // The largest number of contiguous free words in this or any higher generation.
 174   size_t max = 0;
 175   for (const Generation* gen = this; gen != NULL; gen = gen->next_gen()) {
 176     size_t avail = gen->contiguous_available();
 177     if (avail > max) {
 178       max = avail;
 179     }
 180   }
 181   return max;
 182 }
 183 
 184 bool Generation::promotion_attempt_is_safe(size_t max_promotion_in_bytes) const {
 185   size_t available = max_contiguous_available();
 186   bool   res = (available >= max_promotion_in_bytes);
 187   if (PrintGC && Verbose) {
 188     gclog_or_tty->print_cr(
 189       "Generation: promo attempt is%s safe: available("SIZE_FORMAT") %s max_promo("SIZE_FORMAT")",
 190       res? "":" not", available, res? ">=":"<",
 191       max_promotion_in_bytes);
 192   }
 193   return res;
 194 }
 195 
 196 // Ignores "ref" and calls allocate().
 197 oop Generation::promote(oop obj, size_t obj_size) {
 198   assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
 199 
 200 #ifndef PRODUCT
 201   if (Universe::heap()->promotion_should_fail()) {
 202     return NULL;
 203   }
 204 #endif  // #ifndef PRODUCT
 205 
 206   HeapWord* result = allocate(obj_size, false);
 207   if (result != NULL) {
 208     Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
 209     return oop(result);
 210   } else {
 211     GenCollectedHeap* gch = GenCollectedHeap::heap();
 212     return gch->handle_failed_promotion(this, obj, obj_size);
 213   }
 214 }
 215 
 216 oop Generation::par_promote(int thread_num,
 217                             oop obj, markOop m, size_t word_sz) {
 218   // Could do a bad general impl here that gets a lock.  But no.
 219   ShouldNotCallThis();
 220   return NULL;
 221 }
 222 
 223 void Generation::par_promote_alloc_undo(int thread_num,
 224                                         HeapWord* obj, size_t word_sz) {
 225   // Could do a bad general impl here that gets a lock.  But no.
 226   guarantee(false, "No good general implementation.");
 227 }
 228 
 229 Space* Generation::space_containing(const void* p) const {
 230   GenerationIsInReservedClosure blk(p);
 231   // Cast away const
 232   ((Generation*)this)->space_iterate(&blk);
 233   return blk.sp;
 234 }
 235 
 236 // Some of these are mediocre general implementations.  Should be
 237 // overridden to get better performance.
 238 
 239 class GenerationBlockStartClosure : public SpaceClosure {
 240  public:
 241   const void* _p;
 242   HeapWord* _start;
 243   virtual void do_space(Space* s) {
 244     if (_start == NULL && s->is_in_reserved(_p)) {
 245       _start = s->block_start(_p);
 246     }
 247   }
 248   GenerationBlockStartClosure(const void* p) { _p = p; _start = NULL; }
 249 };
 250 
 251 HeapWord* Generation::block_start(const void* p) const {
 252   GenerationBlockStartClosure blk(p);
 253   // Cast away const
 254   ((Generation*)this)->space_iterate(&blk);
 255   return blk._start;
 256 }
 257 
 258 class GenerationBlockSizeClosure : public SpaceClosure {
 259  public:
 260   const HeapWord* _p;
 261   size_t size;
 262   virtual void do_space(Space* s) {
 263     if (size == 0 && s->is_in_reserved(_p)) {
 264       size = s->block_size(_p);
 265     }
 266   }
 267   GenerationBlockSizeClosure(const HeapWord* p) { _p = p; size = 0; }
 268 };
 269 
 270 size_t Generation::block_size(const HeapWord* p) const {
 271   GenerationBlockSizeClosure blk(p);
 272   // Cast away const
 273   ((Generation*)this)->space_iterate(&blk);
 274   assert(blk.size > 0, "seems reasonable");
 275   return blk.size;
 276 }
 277 
 278 class GenerationBlockIsObjClosure : public SpaceClosure {
 279  public:
 280   const HeapWord* _p;
 281   bool is_obj;
 282   virtual void do_space(Space* s) {
 283     if (!is_obj && s->is_in_reserved(_p)) {
 284       is_obj |= s->block_is_obj(_p);
 285     }
 286   }
 287   GenerationBlockIsObjClosure(const HeapWord* p) { _p = p; is_obj = false; }
 288 };
 289 
 290 bool Generation::block_is_obj(const HeapWord* p) const {
 291   GenerationBlockIsObjClosure blk(p);
 292   // Cast away const
 293   ((Generation*)this)->space_iterate(&blk);
 294   return blk.is_obj;
 295 }
 296 
 297 class GenerationOopIterateClosure : public SpaceClosure {
 298  public:
 299   ExtendedOopClosure* _cl;
 300   virtual void do_space(Space* s) {
 301     s->oop_iterate(_cl);
 302   }
 303   GenerationOopIterateClosure(ExtendedOopClosure* cl) :
 304     _cl(cl) {}
 305 };
 306 
 307 void Generation::oop_iterate(ExtendedOopClosure* cl) {
 308   GenerationOopIterateClosure blk(cl);
 309   space_iterate(&blk);
 310 }
 311 
 312 void Generation::younger_refs_in_space_iterate(Space* sp,
 313                                                OopsInGenClosure* cl) {
 314   GenRemSet* rs = SharedHeap::heap()->rem_set();
 315   rs->younger_refs_in_space_iterate(sp, cl);
 316 }
 317 
 318 class GenerationObjIterateClosure : public SpaceClosure {
 319  private:
 320   ObjectClosure* _cl;
 321  public:
 322   virtual void do_space(Space* s) {
 323     s->object_iterate(_cl);
 324   }
 325   GenerationObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
 326 };
 327 
 328 void Generation::object_iterate(ObjectClosure* cl) {
 329   GenerationObjIterateClosure blk(cl);
 330   space_iterate(&blk);
 331 }
 332 
 333 class GenerationSafeObjIterateClosure : public SpaceClosure {
 334  private:
 335   ObjectClosure* _cl;
 336  public:
 337   virtual void do_space(Space* s) {
 338     s->safe_object_iterate(_cl);
 339   }
 340   GenerationSafeObjIterateClosure(ObjectClosure* cl) : _cl(cl) {}
 341 };
 342 
 343 void Generation::safe_object_iterate(ObjectClosure* cl) {
 344   GenerationSafeObjIterateClosure blk(cl);
 345   space_iterate(&blk);
 346 }
 347 
 348 void Generation::prepare_for_compaction(CompactPoint* cp) {
 349   // Generic implementation, can be specialized
 350   CompactibleSpace* space = first_compaction_space();
 351   while (space != NULL) {
 352     space->prepare_for_compaction(cp);
 353     space = space->next_compaction_space();
 354   }
 355 }
 356 
 357 class AdjustPointersClosure: public SpaceClosure {
 358  public:
 359   void do_space(Space* sp) {
 360     sp->adjust_pointers();
 361   }
 362 };
 363 
 364 void Generation::adjust_pointers() {
 365   // Note that this is done over all spaces, not just the compactible
 366   // ones.
 367   AdjustPointersClosure blk;
 368   space_iterate(&blk, true);
 369 }
 370 
 371 void Generation::compact() {
 372   CompactibleSpace* sp = first_compaction_space();
 373   while (sp != NULL) {
 374     sp->compact();
 375     sp = sp->next_compaction_space();
 376   }
 377 }
 378 
 379 CardGeneration::CardGeneration(ReservedSpace rs, size_t initial_byte_size,
 380                                int level,
 381                                GenRemSet* remset) :
 382   Generation(rs, initial_byte_size, level), _rs(remset),
 383   _shrink_factor(0), _min_heap_delta_bytes(), _capacity_at_prologue(),
 384   _used_at_prologue()
 385 {
 386   HeapWord* start = (HeapWord*)rs.base();
 387   size_t reserved_byte_size = rs.size();
 388   assert((uintptr_t(start) & 3) == 0, "bad alignment");
 389   assert((reserved_byte_size & 3) == 0, "bad alignment");
 390   MemRegion reserved_mr(start, heap_word_size(reserved_byte_size));
 391   _bts = new BlockOffsetSharedArray(reserved_mr,
 392                                     heap_word_size(initial_byte_size));
 393   MemRegion committed_mr(start, heap_word_size(initial_byte_size));
 394   _rs->resize_covered_region(committed_mr);
 395   if (_bts == NULL)
 396     vm_exit_during_initialization("Could not allocate a BlockOffsetArray");
 397 
 398   // Verify that the start and end of this generation is the start of a card.
 399   // If this wasn't true, a single card could span more than on generation,
 400   // which would cause problems when we commit/uncommit memory, and when we
 401   // clear and dirty cards.
 402   guarantee(_rs->is_aligned(reserved_mr.start()), "generation must be card aligned");
 403   if (reserved_mr.end() != Universe::heap()->reserved_region().end()) {
 404     // Don't check at the very end of the heap as we'll assert that we're probing off
 405     // the end if we try.
 406     guarantee(_rs->is_aligned(reserved_mr.end()), "generation must be card aligned");
 407   }
 408   _min_heap_delta_bytes = MinHeapDeltaBytes;
 409   _capacity_at_prologue = initial_byte_size;
 410   _used_at_prologue = 0;
 411 }
 412 
 413 bool CardGeneration::expand(size_t bytes, size_t expand_bytes) {
 414   assert_locked_or_safepoint(Heap_lock);
 415   if (bytes == 0) {
 416     return true;  // That's what grow_by(0) would return
 417   }
 418   size_t aligned_bytes  = ReservedSpace::page_align_size_up(bytes);
 419   if (aligned_bytes == 0){
 420     // The alignment caused the number of bytes to wrap.  An expand_by(0) will
 421     // return true with the implication that an expansion was done when it
 422     // was not.  A call to expand implies a best effort to expand by "bytes"
 423     // but not a guarantee.  Align down to give a best effort.  This is likely
 424     // the most that the generation can expand since it has some capacity to
 425     // start with.
 426     aligned_bytes = ReservedSpace::page_align_size_down(bytes);
 427   }
 428   size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes);
 429   bool success = false;
 430   if (aligned_expand_bytes > aligned_bytes) {
 431     success = grow_by(aligned_expand_bytes);
 432   }
 433   if (!success) {
 434     success = grow_by(aligned_bytes);
 435   }
 436   if (!success) {
 437     success = grow_to_reserved();
 438   }
 439   if (PrintGC && Verbose) {
 440     if (success && GC_locker::is_active_and_needs_gc()) {
 441       gclog_or_tty->print_cr("Garbage collection disabled, expanded heap instead");
 442     }
 443   }
 444 
 445   return success;
 446 }
 447 
 448 
 449 // No young generation references, clear this generation's cards.
 450 void CardGeneration::clear_remembered_set() {
 451   _rs->clear(reserved());
 452 }
 453 
 454 
 455 // Objects in this generation may have moved, invalidate this
 456 // generation's cards.
 457 void CardGeneration::invalidate_remembered_set() {
 458   _rs->invalidate(used_region());
 459 }
 460 
 461 
 462 void CardGeneration::compute_new_size() {
 463   assert(_shrink_factor <= 100, "invalid shrink factor");
 464   size_t current_shrink_factor = _shrink_factor;
 465   _shrink_factor = 0;
 466 
 467   // We don't have floating point command-line arguments
 468   // Note:  argument processing ensures that MinHeapFreeRatio < 100.
 469   const double minimum_free_percentage = MinHeapFreeRatio / 100.0;
 470   const double maximum_used_percentage = 1.0 - minimum_free_percentage;
 471 
 472   // Compute some numbers about the state of the heap.
 473   const size_t used_after_gc = used();
 474   const size_t capacity_after_gc = capacity();
 475 
 476   const double min_tmp = used_after_gc / maximum_used_percentage;
 477   size_t minimum_desired_capacity = (size_t)MIN2(min_tmp, double(max_uintx));
 478   // Don't shrink less than the initial generation size
 479   minimum_desired_capacity = MAX2(minimum_desired_capacity,
 480                                   spec()->init_size());
 481   assert(used_after_gc <= minimum_desired_capacity, "sanity check");
 482 
 483   if (PrintGC && Verbose) {
 484     const size_t free_after_gc = free();
 485     const double free_percentage = ((double)free_after_gc) / capacity_after_gc;
 486     gclog_or_tty->print_cr("TenuredGeneration::compute_new_size: ");
 487     gclog_or_tty->print_cr("  "
 488                   "  minimum_free_percentage: %6.2f"
 489                   "  maximum_used_percentage: %6.2f",
 490                   minimum_free_percentage,
 491                   maximum_used_percentage);
 492     gclog_or_tty->print_cr("  "
 493                   "   free_after_gc   : %6.1fK"
 494                   "   used_after_gc   : %6.1fK"
 495                   "   capacity_after_gc   : %6.1fK",
 496                   free_after_gc / (double) K,
 497                   used_after_gc / (double) K,
 498                   capacity_after_gc / (double) K);
 499     gclog_or_tty->print_cr("  "
 500                   "   free_percentage: %6.2f",
 501                   free_percentage);
 502   }
 503 
 504   if (capacity_after_gc < minimum_desired_capacity) {
 505     // If we have less free space than we want then expand
 506     size_t expand_bytes = minimum_desired_capacity - capacity_after_gc;
 507     // Don't expand unless it's significant
 508     if (expand_bytes >= _min_heap_delta_bytes) {
 509       expand(expand_bytes, 0); // safe if expansion fails
 510     }
 511     if (PrintGC && Verbose) {
 512       gclog_or_tty->print_cr("    expanding:"
 513                     "  minimum_desired_capacity: %6.1fK"
 514                     "  expand_bytes: %6.1fK"
 515                     "  _min_heap_delta_bytes: %6.1fK",
 516                     minimum_desired_capacity / (double) K,
 517                     expand_bytes / (double) K,
 518                     _min_heap_delta_bytes / (double) K);
 519     }
 520     return;
 521   }
 522 
 523   // No expansion, now see if we want to shrink
 524   size_t shrink_bytes = 0;
 525   // We would never want to shrink more than this
 526   size_t max_shrink_bytes = capacity_after_gc - minimum_desired_capacity;
 527 
 528   if (MaxHeapFreeRatio < 100) {
 529     const double maximum_free_percentage = MaxHeapFreeRatio / 100.0;
 530     const double minimum_used_percentage = 1.0 - maximum_free_percentage;
 531     const double max_tmp = used_after_gc / minimum_used_percentage;
 532     size_t maximum_desired_capacity = (size_t)MIN2(max_tmp, double(max_uintx));
 533     maximum_desired_capacity = MAX2(maximum_desired_capacity,
 534                                     spec()->init_size());
 535     if (PrintGC && Verbose) {
 536       gclog_or_tty->print_cr("  "
 537                              "  maximum_free_percentage: %6.2f"
 538                              "  minimum_used_percentage: %6.2f",
 539                              maximum_free_percentage,
 540                              minimum_used_percentage);
 541       gclog_or_tty->print_cr("  "
 542                              "  _capacity_at_prologue: %6.1fK"
 543                              "  minimum_desired_capacity: %6.1fK"
 544                              "  maximum_desired_capacity: %6.1fK",
 545                              _capacity_at_prologue / (double) K,
 546                              minimum_desired_capacity / (double) K,
 547                              maximum_desired_capacity / (double) K);
 548     }
 549     assert(minimum_desired_capacity <= maximum_desired_capacity,
 550            "sanity check");
 551 
 552     if (capacity_after_gc > maximum_desired_capacity) {
 553       // Capacity too large, compute shrinking size
 554       shrink_bytes = capacity_after_gc - maximum_desired_capacity;
 555       // We don't want shrink all the way back to initSize if people call
 556       // System.gc(), because some programs do that between "phases" and then
 557       // we'd just have to grow the heap up again for the next phase.  So we
 558       // damp the shrinking: 0% on the first call, 10% on the second call, 40%
 559       // on the third call, and 100% by the fourth call.  But if we recompute
 560       // size without shrinking, it goes back to 0%.
 561       shrink_bytes = shrink_bytes / 100 * current_shrink_factor;
 562       assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size");
 563       if (current_shrink_factor == 0) {
 564         _shrink_factor = 10;
 565       } else {
 566         _shrink_factor = MIN2(current_shrink_factor * 4, (size_t) 100);
 567       }
 568       if (PrintGC && Verbose) {
 569         gclog_or_tty->print_cr("  "
 570                       "  shrinking:"
 571                       "  initSize: %.1fK"
 572                       "  maximum_desired_capacity: %.1fK",
 573                       spec()->init_size() / (double) K,
 574                       maximum_desired_capacity / (double) K);
 575         gclog_or_tty->print_cr("  "
 576                       "  shrink_bytes: %.1fK"
 577                       "  current_shrink_factor: " SIZE_FORMAT
 578                       "  new shrink factor: " SIZE_FORMAT
 579                       "  _min_heap_delta_bytes: %.1fK",
 580                       shrink_bytes / (double) K,
 581                       current_shrink_factor,
 582                       _shrink_factor,
 583                       _min_heap_delta_bytes / (double) K);
 584       }
 585     }
 586   }
 587 
 588   if (capacity_after_gc > _capacity_at_prologue) {
 589     // We might have expanded for promotions, in which case we might want to
 590     // take back that expansion if there's room after GC.  That keeps us from
 591     // stretching the heap with promotions when there's plenty of room.
 592     size_t expansion_for_promotion = capacity_after_gc - _capacity_at_prologue;
 593     expansion_for_promotion = MIN2(expansion_for_promotion, max_shrink_bytes);
 594     // We have two shrinking computations, take the largest
 595     shrink_bytes = MAX2(shrink_bytes, expansion_for_promotion);
 596     assert(shrink_bytes <= max_shrink_bytes, "invalid shrink size");
 597     if (PrintGC && Verbose) {
 598       gclog_or_tty->print_cr("  "
 599                              "  aggressive shrinking:"
 600                              "  _capacity_at_prologue: %.1fK"
 601                              "  capacity_after_gc: %.1fK"
 602                              "  expansion_for_promotion: %.1fK"
 603                              "  shrink_bytes: %.1fK",
 604                              capacity_after_gc / (double) K,
 605                              _capacity_at_prologue / (double) K,
 606                              expansion_for_promotion / (double) K,
 607                              shrink_bytes / (double) K);
 608     }
 609   }
 610   // Don't shrink unless it's significant
 611   if (shrink_bytes >= _min_heap_delta_bytes) {
 612     shrink(shrink_bytes);
 613   }
 614 }
 615 
 616 // Currently nothing to do.
 617 void CardGeneration::prepare_for_verify() {}
 618 
 619 
 620 void OneContigSpaceCardGeneration::collect(bool   full,
 621                                            bool   clear_all_soft_refs,
 622                                            size_t size,
 623                                            bool   is_tlab) {
 624   GenCollectedHeap* gch = GenCollectedHeap::heap();
 625 
 626   SpecializationStats::clear();
 627   // Temporarily expand the span of our ref processor, so
 628   // refs discovery is over the entire heap, not just this generation
 629   ReferenceProcessorSpanMutator
 630     x(ref_processor(), gch->reserved_region());
 631 
 632   STWGCTimer* gc_timer = GenMarkSweep::gc_timer();
 633   gc_timer->register_gc_start();
 634 
 635   SerialOldTracer* gc_tracer = GenMarkSweep::gc_tracer();
 636   gc_tracer->report_gc_start(gch->gc_cause(), gc_timer->gc_start());
 637 
 638   GenMarkSweep::invoke_at_safepoint(_level, ref_processor(), clear_all_soft_refs);
 639 
 640   gc_timer->register_gc_end();
 641 
 642   gc_tracer->report_gc_end(gc_timer->gc_end(), gc_timer->time_partitions());
 643 
 644   SpecializationStats::print();
 645 }
 646 
 647 HeapWord*
 648 OneContigSpaceCardGeneration::expand_and_allocate(size_t word_size,
 649                                                   bool is_tlab,
 650                                                   bool parallel) {
 651   assert(!is_tlab, "OneContigSpaceCardGeneration does not support TLAB allocation");
 652   if (parallel) {
 653     MutexLocker x(ParGCRareEvent_lock);
 654     HeapWord* result = NULL;
 655     size_t byte_size = word_size * HeapWordSize;
 656     while (true) {
 657       expand(byte_size, _min_heap_delta_bytes);
 658       if (GCExpandToAllocateDelayMillis > 0) {
 659         os::sleep(Thread::current(), GCExpandToAllocateDelayMillis, false);
 660       }
 661       result = _the_space->par_allocate(word_size);
 662       if ( result != NULL) {
 663         return result;
 664       } else {
 665         // If there's not enough expansion space available, give up.
 666         if (_virtual_space.uncommitted_size() < byte_size) {
 667           return NULL;
 668         }
 669         // else try again
 670       }
 671     }
 672   } else {
 673     expand(word_size*HeapWordSize, _min_heap_delta_bytes);
 674     return _the_space->allocate(word_size);
 675   }
 676 }
 677 
 678 bool OneContigSpaceCardGeneration::expand(size_t bytes, size_t expand_bytes) {
 679   GCMutexLocker x(ExpandHeap_lock);
 680   return CardGeneration::expand(bytes, expand_bytes);
 681 }
 682 
 683 
 684 void OneContigSpaceCardGeneration::shrink(size_t bytes) {
 685   assert_locked_or_safepoint(ExpandHeap_lock);
 686   size_t size = ReservedSpace::page_align_size_down(bytes);
 687   if (size > 0) {
 688     shrink_by(size);
 689   }
 690 }
 691 
 692 
 693 size_t OneContigSpaceCardGeneration::capacity() const {
 694   return _the_space->capacity();
 695 }
 696 
 697 
 698 size_t OneContigSpaceCardGeneration::used() const {
 699   return _the_space->used();
 700 }
 701 
 702 
 703 size_t OneContigSpaceCardGeneration::free() const {
 704   return _the_space->free();
 705 }
 706 
 707 MemRegion OneContigSpaceCardGeneration::used_region() const {
 708   return the_space()->used_region();
 709 }
 710 
 711 size_t OneContigSpaceCardGeneration::unsafe_max_alloc_nogc() const {
 712   return _the_space->free();
 713 }
 714 
 715 size_t OneContigSpaceCardGeneration::contiguous_available() const {
 716   return _the_space->free() + _virtual_space.uncommitted_size();
 717 }
 718 
 719 bool OneContigSpaceCardGeneration::grow_by(size_t bytes) {
 720   assert_locked_or_safepoint(ExpandHeap_lock);
 721   bool result = _virtual_space.expand_by(bytes);
 722   if (result) {
 723     size_t new_word_size =
 724        heap_word_size(_virtual_space.committed_size());
 725     MemRegion mr(_the_space->bottom(), new_word_size);
 726     // Expand card table
 727     Universe::heap()->barrier_set()->resize_covered_region(mr);
 728     // Expand shared block offset array
 729     _bts->resize(new_word_size);
 730 
 731     // Fix for bug #4668531
 732     if (ZapUnusedHeapArea) {
 733       MemRegion mangle_region(_the_space->end(),
 734       (HeapWord*)_virtual_space.high());
 735       SpaceMangler::mangle_region(mangle_region);
 736     }
 737 
 738     // Expand space -- also expands space's BOT
 739     // (which uses (part of) shared array above)
 740     _the_space->set_end((HeapWord*)_virtual_space.high());
 741 
 742     // update the space and generation capacity counters
 743     update_counters();
 744 
 745     if (Verbose && PrintGC) {
 746       size_t new_mem_size = _virtual_space.committed_size();
 747       size_t old_mem_size = new_mem_size - bytes;
 748       gclog_or_tty->print_cr("Expanding %s from " SIZE_FORMAT "K by "
 749                       SIZE_FORMAT "K to " SIZE_FORMAT "K",
 750                       name(), old_mem_size/K, bytes/K, new_mem_size/K);
 751     }
 752   }
 753   return result;
 754 }
 755 
 756 
 757 bool OneContigSpaceCardGeneration::grow_to_reserved() {
 758   assert_locked_or_safepoint(ExpandHeap_lock);
 759   bool success = true;
 760   const size_t remaining_bytes = _virtual_space.uncommitted_size();
 761   if (remaining_bytes > 0) {
 762     success = grow_by(remaining_bytes);
 763     DEBUG_ONLY(if (!success) warning("grow to reserved failed");)
 764   }
 765   return success;
 766 }
 767 
 768 void OneContigSpaceCardGeneration::shrink_by(size_t bytes) {
 769   assert_locked_or_safepoint(ExpandHeap_lock);
 770   // Shrink committed space
 771   _virtual_space.shrink_by(bytes);
 772   // Shrink space; this also shrinks the space's BOT
 773   _the_space->set_end((HeapWord*) _virtual_space.high());
 774   size_t new_word_size = heap_word_size(_the_space->capacity());
 775   // Shrink the shared block offset array
 776   _bts->resize(new_word_size);
 777   MemRegion mr(_the_space->bottom(), new_word_size);
 778   // Shrink the card table
 779   Universe::heap()->barrier_set()->resize_covered_region(mr);
 780 
 781   if (Verbose && PrintGC) {
 782     size_t new_mem_size = _virtual_space.committed_size();
 783     size_t old_mem_size = new_mem_size + bytes;
 784     gclog_or_tty->print_cr("Shrinking %s from " SIZE_FORMAT "K to " SIZE_FORMAT "K",
 785                   name(), old_mem_size/K, new_mem_size/K);
 786   }
 787 }
 788 
 789 // Currently nothing to do.
 790 void OneContigSpaceCardGeneration::prepare_for_verify() {}
 791 
 792 
 793 // Override for a card-table generation with one contiguous
 794 // space. NOTE: For reasons that are lost in the fog of history,
 795 // this code is used when you iterate over perm gen objects,
 796 // even when one uses CDS, where the perm gen has a couple of
 797 // other spaces; this is because CompactingPermGenGen derives
 798 // from OneContigSpaceCardGeneration. This should be cleaned up,
 799 // see CR 6897789..
 800 void OneContigSpaceCardGeneration::object_iterate(ObjectClosure* blk) {
 801   _the_space->object_iterate(blk);
 802 }
 803 
 804 void OneContigSpaceCardGeneration::space_iterate(SpaceClosure* blk,
 805                                                  bool usedOnly) {
 806   blk->do_space(_the_space);
 807 }
 808 
 809 void OneContigSpaceCardGeneration::younger_refs_iterate(OopsInGenClosure* blk) {
 810   blk->set_generation(this);
 811   younger_refs_in_space_iterate(_the_space, blk);
 812   blk->reset_generation();
 813 }
 814 
 815 void OneContigSpaceCardGeneration::save_marks() {
 816   _the_space->set_saved_mark();
 817 }
 818 
 819 
 820 void OneContigSpaceCardGeneration::reset_saved_marks() {
 821   _the_space->reset_saved_mark();
 822 }
 823 
 824 
 825 bool OneContigSpaceCardGeneration::no_allocs_since_save_marks() {
 826   return _the_space->saved_mark_at_top();
 827 }
 828 
 829 #define OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix)      \
 830                                                                                 \
 831 void OneContigSpaceCardGeneration::                                             \
 832 oop_since_save_marks_iterate##nv_suffix(OopClosureType* blk) {                  \
 833   blk->set_generation(this);                                                    \
 834   _the_space->oop_since_save_marks_iterate##nv_suffix(blk);                     \
 835   blk->reset_generation();                                                      \
 836   save_marks();                                                                 \
 837 }
 838 
 839 ALL_SINCE_SAVE_MARKS_CLOSURES(OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN)
 840 
 841 #undef OneContig_SINCE_SAVE_MARKS_ITERATE_DEFN
 842 
 843 
 844 void OneContigSpaceCardGeneration::gc_epilogue(bool full) {
 845   _last_gc = WaterMark(the_space(), the_space()->top());
 846 
 847   // update the generation and space performance counters
 848   update_counters();
 849   if (ZapUnusedHeapArea) {
 850     the_space()->check_mangled_unused_area_complete();
 851   }
 852 }
 853 
 854 void OneContigSpaceCardGeneration::record_spaces_top() {
 855   assert(ZapUnusedHeapArea, "Not mangling unused space");
 856   the_space()->set_top_for_allocations();
 857 }
 858 
 859 void OneContigSpaceCardGeneration::verify() {
 860   the_space()->verify();
 861 }
 862 
 863 void OneContigSpaceCardGeneration::print_on(outputStream* st)  const {
 864   Generation::print_on(st);
 865   st->print("   the");
 866   the_space()->print_on(st);
 867 }