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