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