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