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