1 /*
   2  * Copyright (c) 2000, 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 "classfile/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "gc_implementation/shared/collectorCounters.hpp"
  32 #include "gc_implementation/shared/gcTrace.hpp"
  33 #include "gc_implementation/shared/gcTraceTime.hpp"
  34 #include "gc_implementation/shared/vmGCOperations.hpp"
  35 #include "gc_interface/collectedHeap.inline.hpp"
  36 #include "memory/filemap.hpp"
  37 #include "memory/gcLocker.inline.hpp"
  38 #include "memory/genCollectedHeap.hpp"
  39 #include "memory/genOopClosures.inline.hpp"
  40 #include "memory/generation.inline.hpp"
  41 #include "memory/generationSpec.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "memory/sharedHeap.hpp"
  44 #include "memory/space.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "oops/oop.inline2.hpp"
  47 #include "runtime/biasedLocking.hpp"
  48 #include "runtime/fprofiler.hpp"
  49 #include "runtime/handles.hpp"
  50 #include "runtime/handles.inline.hpp"
  51 #include "runtime/java.hpp"
  52 #include "runtime/vmThread.hpp"
  53 #include "services/management.hpp"
  54 #include "services/memoryService.hpp"
  55 #include "utilities/vmError.hpp"
  56 #include "utilities/workgroup.hpp"
  57 #include "utilities/macros.hpp"
  58 #if INCLUDE_ALL_GCS
  59 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
  60 #include "gc_implementation/concurrentMarkSweep/vmCMSOperations.hpp"
  61 #endif // INCLUDE_ALL_GCS
  62 #if INCLUDE_JFR
  63 #include "jfr/jfr.hpp"
  64 #endif // INCLUDE_JFR
  65 
  66 GenCollectedHeap* GenCollectedHeap::_gch;
  67 NOT_PRODUCT(size_t GenCollectedHeap::_skip_header_HeapWords = 0;)
  68 
  69 // The set of potentially parallel tasks in root scanning.
  70 enum GCH_strong_roots_tasks {
  71   GCH_PS_Universe_oops_do,
  72   GCH_PS_JNIHandles_oops_do,
  73   GCH_PS_ObjectSynchronizer_oops_do,
  74   GCH_PS_FlatProfiler_oops_do,
  75   GCH_PS_Management_oops_do,
  76   GCH_PS_SystemDictionary_oops_do,
  77   GCH_PS_ClassLoaderDataGraph_oops_do,
  78   GCH_PS_jvmti_oops_do,
  79   GCH_PS_CodeCache_oops_do,
  80   GCH_PS_younger_gens,
  81   // Leave this one last.
  82   GCH_PS_NumElements
  83 };
  84 
  85 GenCollectedHeap::GenCollectedHeap(GenCollectorPolicy *policy) :
  86   SharedHeap(policy),
  87   _gen_policy(policy),
  88   _process_strong_tasks(new SubTasksDone(GCH_PS_NumElements)),
  89   _full_collections_completed(0)
  90 {
  91   assert(policy != NULL, "Sanity check");
  92 }
  93 
  94 jint GenCollectedHeap::initialize() {
  95   CollectedHeap::pre_initialize();
  96 
  97   int i;
  98   _n_gens = gen_policy()->number_of_generations();
  99 
 100   // While there are no constraints in the GC code that HeapWordSize
 101   // be any particular value, there are multiple other areas in the
 102   // system which believe this to be true (e.g. oop->object_size in some
 103   // cases incorrectly returns the size in wordSize units rather than
 104   // HeapWordSize).
 105   guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
 106 
 107   // The heap must be at least as aligned as generations.
 108   size_t gen_alignment = Generation::GenGrain;
 109 
 110   _gen_specs = gen_policy()->generations();
 111 
 112   // Make sure the sizes are all aligned.
 113   for (i = 0; i < _n_gens; i++) {
 114     _gen_specs[i]->align(gen_alignment);
 115   }
 116 
 117   // Allocate space for the heap.
 118 
 119   char* heap_address;
 120   size_t total_reserved = 0;
 121   int n_covered_regions = 0;
 122   ReservedSpace heap_rs;
 123 
 124   size_t heap_alignment = collector_policy()->heap_alignment();
 125 
 126   heap_address = allocate(heap_alignment, &total_reserved,
 127                           &n_covered_regions, &heap_rs);
 128 
 129   if (!heap_rs.is_reserved()) {
 130     vm_shutdown_during_initialization(
 131       "Could not reserve enough space for object heap");
 132     return JNI_ENOMEM;
 133   }
 134 
 135   _reserved = MemRegion((HeapWord*)heap_rs.base(),
 136                         (HeapWord*)(heap_rs.base() + heap_rs.size()));
 137 
 138   // It is important to do this in a way such that concurrent readers can't
 139   // temporarily think somethings in the heap.  (Seen this happen in asserts.)
 140   _reserved.set_word_size(0);
 141   _reserved.set_start((HeapWord*)heap_rs.base());
 142   size_t actual_heap_size = heap_rs.size();
 143   _reserved.set_end((HeapWord*)(heap_rs.base() + actual_heap_size));
 144 
 145   _rem_set = collector_policy()->create_rem_set(_reserved, n_covered_regions);
 146   set_barrier_set(rem_set()->bs());
 147 
 148   _gch = this;
 149 
 150   for (i = 0; i < _n_gens; i++) {
 151     ReservedSpace this_rs = heap_rs.first_part(_gen_specs[i]->max_size(), false, false);
 152     _gens[i] = _gen_specs[i]->init(this_rs, i, rem_set());
 153     heap_rs = heap_rs.last_part(_gen_specs[i]->max_size());
 154   }
 155   clear_incremental_collection_failed();
 156 
 157 #if INCLUDE_ALL_GCS
 158   // If we are running CMS, create the collector responsible
 159   // for collecting the CMS generations.
 160   if (collector_policy()->is_concurrent_mark_sweep_policy()) {
 161     bool success = create_cms_collector();
 162     if (!success) return JNI_ENOMEM;
 163   }
 164 #endif // INCLUDE_ALL_GCS
 165 
 166   return JNI_OK;
 167 }
 168 
 169 
 170 char* GenCollectedHeap::allocate(size_t alignment,
 171                                  size_t* _total_reserved,
 172                                  int* _n_covered_regions,
 173                                  ReservedSpace* heap_rs){
 174   const char overflow_msg[] = "The size of the object heap + VM data exceeds "
 175     "the maximum representable size";
 176 
 177   // Now figure out the total size.
 178   size_t total_reserved = 0;
 179   int n_covered_regions = 0;
 180   const size_t pageSize = UseLargePages ?
 181       os::large_page_size() : os::vm_page_size();
 182 
 183   assert(alignment % pageSize == 0, "Must be");
 184 
 185   for (int i = 0; i < _n_gens; i++) {
 186     total_reserved += _gen_specs[i]->max_size();
 187     if (total_reserved < _gen_specs[i]->max_size()) {
 188       vm_exit_during_initialization(overflow_msg);
 189     }
 190     n_covered_regions += _gen_specs[i]->n_covered_regions();
 191   }
 192   assert(total_reserved % alignment == 0,
 193          err_msg("Gen size; total_reserved=" SIZE_FORMAT ", alignment="
 194                  SIZE_FORMAT, total_reserved, alignment));
 195 
 196   // Needed until the cardtable is fixed to have the right number
 197   // of covered regions.
 198   n_covered_regions += 2;
 199 
 200   *_total_reserved = total_reserved;
 201   *_n_covered_regions = n_covered_regions;
 202 
 203   *heap_rs = Universe::reserve_heap(total_reserved, alignment);
 204   return heap_rs->base();
 205 }
 206 
 207 
 208 void GenCollectedHeap::post_initialize() {
 209   SharedHeap::post_initialize();
 210   TwoGenerationCollectorPolicy *policy =
 211     (TwoGenerationCollectorPolicy *)collector_policy();
 212   guarantee(policy->is_two_generation_policy(), "Illegal policy type");
 213   DefNewGeneration* def_new_gen = (DefNewGeneration*) get_gen(0);
 214   assert(def_new_gen->kind() == Generation::DefNew ||
 215          def_new_gen->kind() == Generation::ParNew ||
 216          def_new_gen->kind() == Generation::ASParNew,
 217          "Wrong generation kind");
 218 
 219   Generation* old_gen = get_gen(1);
 220   assert(old_gen->kind() == Generation::ConcurrentMarkSweep ||
 221          old_gen->kind() == Generation::ASConcurrentMarkSweep ||
 222          old_gen->kind() == Generation::MarkSweepCompact,
 223     "Wrong generation kind");
 224 
 225   policy->initialize_size_policy(def_new_gen->eden()->capacity(),
 226                                  old_gen->capacity(),
 227                                  def_new_gen->from()->capacity());
 228   policy->initialize_gc_policy_counters();
 229 }
 230 
 231 void GenCollectedHeap::ref_processing_init() {
 232   SharedHeap::ref_processing_init();
 233   for (int i = 0; i < _n_gens; i++) {
 234     _gens[i]->ref_processor_init();
 235   }
 236 }
 237 
 238 size_t GenCollectedHeap::capacity() const {
 239   size_t res = 0;
 240   for (int i = 0; i < _n_gens; i++) {
 241     res += _gens[i]->capacity();
 242   }
 243   return res;
 244 }
 245 
 246 size_t GenCollectedHeap::used() const {
 247   size_t res = 0;
 248   for (int i = 0; i < _n_gens; i++) {
 249     res += _gens[i]->used();
 250   }
 251   return res;
 252 }
 253 
 254 // Save the "used_region" for generations level and lower.
 255 void GenCollectedHeap::save_used_regions(int level) {
 256   assert(level < _n_gens, "Illegal level parameter");
 257   for (int i = level; i >= 0; i--) {
 258     _gens[i]->save_used_region();
 259   }
 260 }
 261 
 262 size_t GenCollectedHeap::max_capacity() const {
 263   size_t res = 0;
 264   for (int i = 0; i < _n_gens; i++) {
 265     res += _gens[i]->max_capacity();
 266   }
 267   return res;
 268 }
 269 
 270 // Update the _full_collections_completed counter
 271 // at the end of a stop-world full GC.
 272 unsigned int GenCollectedHeap::update_full_collections_completed() {
 273   MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
 274   assert(_full_collections_completed <= _total_full_collections,
 275          "Can't complete more collections than were started");
 276   _full_collections_completed = _total_full_collections;
 277   ml.notify_all();
 278   return _full_collections_completed;
 279 }
 280 
 281 // Update the _full_collections_completed counter, as appropriate,
 282 // at the end of a concurrent GC cycle. Note the conditional update
 283 // below to allow this method to be called by a concurrent collector
 284 // without synchronizing in any manner with the VM thread (which
 285 // may already have initiated a STW full collection "concurrently").
 286 unsigned int GenCollectedHeap::update_full_collections_completed(unsigned int count) {
 287   MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
 288   assert((_full_collections_completed <= _total_full_collections) &&
 289          (count <= _total_full_collections),
 290          "Can't complete more collections than were started");
 291   if (count > _full_collections_completed) {
 292     _full_collections_completed = count;
 293     ml.notify_all();
 294   }
 295   return _full_collections_completed;
 296 }
 297 
 298 
 299 #ifndef PRODUCT
 300 // Override of memory state checking method in CollectedHeap:
 301 // Some collectors (CMS for example) can't have badHeapWordVal written
 302 // in the first two words of an object. (For instance , in the case of
 303 // CMS these words hold state used to synchronize between certain
 304 // (concurrent) GC steps and direct allocating mutators.)
 305 // The skip_header_HeapWords() method below, allows us to skip
 306 // over the requisite number of HeapWord's. Note that (for
 307 // generational collectors) this means that those many words are
 308 // skipped in each object, irrespective of the generation in which
 309 // that object lives. The resultant loss of precision seems to be
 310 // harmless and the pain of avoiding that imprecision appears somewhat
 311 // higher than we are prepared to pay for such rudimentary debugging
 312 // support.
 313 void GenCollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr,
 314                                                          size_t size) {
 315   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
 316     // We are asked to check a size in HeapWords,
 317     // but the memory is mangled in juint words.
 318     juint* start = (juint*) (addr + skip_header_HeapWords());
 319     juint* end   = (juint*) (addr + size);
 320     for (juint* slot = start; slot < end; slot += 1) {
 321       assert(*slot == badHeapWordVal,
 322              "Found non badHeapWordValue in pre-allocation check");
 323     }
 324   }
 325 }
 326 #endif
 327 
 328 HeapWord* GenCollectedHeap::attempt_allocation(size_t size,
 329                                                bool is_tlab,
 330                                                bool first_only) {
 331   HeapWord* res;
 332   for (int i = 0; i < _n_gens; i++) {
 333     if (_gens[i]->should_allocate(size, is_tlab)) {
 334       res = _gens[i]->allocate(size, is_tlab);
 335       if (res != NULL) return res;
 336       else if (first_only) break;
 337     }
 338   }
 339   // Otherwise...
 340   return NULL;
 341 }
 342 
 343 HeapWord* GenCollectedHeap::mem_allocate(size_t size,
 344                                          bool* gc_overhead_limit_was_exceeded) {
 345   return collector_policy()->mem_allocate_work(size,
 346                                                false /* is_tlab */,
 347                                                gc_overhead_limit_was_exceeded);
 348 }
 349 
 350 bool GenCollectedHeap::must_clear_all_soft_refs() {
 351   return _gc_cause == GCCause::_last_ditch_collection;
 352 }
 353 
 354 bool GenCollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
 355   return UseConcMarkSweepGC &&
 356          ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) ||
 357           (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent));
 358 }
 359 
 360 void GenCollectedHeap::do_collection(bool  full,
 361                                      bool   clear_all_soft_refs,
 362                                      size_t size,
 363                                      bool   is_tlab,
 364                                      int    max_level) {
 365   bool prepared_for_verification = false;
 366   ResourceMark rm;
 367   DEBUG_ONLY(Thread* my_thread = Thread::current();)
 368 
 369   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 370   assert(my_thread->is_VM_thread() ||
 371          my_thread->is_ConcurrentGC_thread(),
 372          "incorrect thread type capability");
 373   assert(Heap_lock->is_locked(),
 374          "the requesting thread should have the Heap_lock");
 375   guarantee(!is_gc_active(), "collection is not reentrant");
 376   assert(max_level < n_gens(), "sanity check");
 377 
 378   if (GC_locker::check_active_before_gc()) {
 379     return; // GC is disabled (e.g. JNI GetXXXCritical operation)
 380   }
 381 
 382   const bool do_clear_all_soft_refs = clear_all_soft_refs ||
 383                           collector_policy()->should_clear_all_soft_refs();
 384 
 385   ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy());
 386 
 387   const size_t metadata_prev_used = MetaspaceAux::used_bytes();
 388 
 389   print_heap_before_gc();
 390 
 391   {
 392     FlagSetting fl(_is_gc_active, true);
 393 
 394     bool complete = full && (max_level == (n_gens()-1));
 395     const char* gc_cause_prefix = complete ? "Full GC" : "GC";
 396     TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
 397     // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
 398     // so we can assume here that the next GC id is what we want.
 399     GCTraceTime t(GCCauseString(gc_cause_prefix, gc_cause()), PrintGCDetails, false, NULL, GCId::peek());
 400 
 401     gc_prologue(complete);
 402     increment_total_collections(complete);
 403 
 404     size_t gch_prev_used = used();
 405 
 406     int starting_level = 0;
 407     if (full) {
 408       // Search for the oldest generation which will collect all younger
 409       // generations, and start collection loop there.
 410       for (int i = max_level; i >= 0; i--) {
 411         if (_gens[i]->full_collects_younger_generations()) {
 412           starting_level = i;
 413           break;
 414         }
 415       }
 416     }
 417 
 418     bool must_restore_marks_for_biased_locking = false;
 419 
 420     int max_level_collected = starting_level;
 421     for (int i = starting_level; i <= max_level; i++) {
 422       if (_gens[i]->should_collect(full, size, is_tlab)) {
 423         if (i == n_gens() - 1) {  // a major collection is to happen
 424           if (!complete) {
 425             // The full_collections increment was missed above.
 426             increment_total_full_collections();
 427           }
 428           pre_full_gc_dump(NULL);    // do any pre full gc dumps
 429         }
 430         // Timer for individual generations. Last argument is false: no CR
 431         // FIXME: We should try to start the timing earlier to cover more of the GC pause
 432         // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
 433         // so we can assume here that the next GC id is what we want.
 434         GCTraceTime t1(_gens[i]->short_name(), PrintGCDetails, false, NULL, GCId::peek());
 435         TraceCollectorStats tcs(_gens[i]->counters());
 436         TraceMemoryManagerStats tmms(_gens[i]->kind(),gc_cause());
 437 
 438         size_t prev_used = _gens[i]->used();
 439         _gens[i]->stat_record()->invocations++;
 440         _gens[i]->stat_record()->accumulated_time.start();
 441 
 442         // Must be done anew before each collection because
 443         // a previous collection will do mangling and will
 444         // change top of some spaces.
 445         record_gen_tops_before_GC();
 446 
 447         if (PrintGC && Verbose) {
 448           gclog_or_tty->print("level=%d invoke=%d size=" SIZE_FORMAT,
 449                      i,
 450                      _gens[i]->stat_record()->invocations,
 451                      size*HeapWordSize);
 452         }
 453 
 454         if (VerifyBeforeGC && i >= VerifyGCLevel &&
 455             total_collections() >= VerifyGCStartAt) {
 456           HandleMark hm;  // Discard invalid handles created during verification
 457           if (!prepared_for_verification) {
 458             prepare_for_verify();
 459             prepared_for_verification = true;
 460           }
 461           Universe::verify(" VerifyBeforeGC:");
 462         }
 463         COMPILER2_PRESENT(DerivedPointerTable::clear());
 464 
 465         if (!must_restore_marks_for_biased_locking &&
 466             _gens[i]->performs_in_place_marking()) {
 467           // We perform this mark word preservation work lazily
 468           // because it's only at this point that we know whether we
 469           // absolutely have to do it; we want to avoid doing it for
 470           // scavenge-only collections where it's unnecessary
 471           must_restore_marks_for_biased_locking = true;
 472           BiasedLocking::preserve_marks();
 473         }
 474 
 475         // Do collection work
 476         {
 477           // Note on ref discovery: For what appear to be historical reasons,
 478           // GCH enables and disabled (by enqueing) refs discovery.
 479           // In the future this should be moved into the generation's
 480           // collect method so that ref discovery and enqueueing concerns
 481           // are local to a generation. The collect method could return
 482           // an appropriate indication in the case that notification on
 483           // the ref lock was needed. This will make the treatment of
 484           // weak refs more uniform (and indeed remove such concerns
 485           // from GCH). XXX
 486 
 487           HandleMark hm;  // Discard invalid handles created during gc
 488           save_marks();   // save marks for all gens
 489           // We want to discover references, but not process them yet.
 490           // This mode is disabled in process_discovered_references if the
 491           // generation does some collection work, or in
 492           // enqueue_discovered_references if the generation returns
 493           // without doing any work.
 494           ReferenceProcessor* rp = _gens[i]->ref_processor();
 495           // If the discovery of ("weak") refs in this generation is
 496           // atomic wrt other collectors in this configuration, we
 497           // are guaranteed to have empty discovered ref lists.
 498           if (rp->discovery_is_atomic()) {
 499             rp->enable_discovery(true /*verify_disabled*/, true /*verify_no_refs*/);
 500             rp->setup_policy(do_clear_all_soft_refs);
 501           } else {
 502             // collect() below will enable discovery as appropriate
 503           }
 504           _gens[i]->collect(full, do_clear_all_soft_refs, size, is_tlab);
 505           if (!rp->enqueuing_is_done()) {
 506             rp->enqueue_discovered_references();
 507           } else {
 508             rp->set_enqueuing_is_done(false);
 509           }
 510           rp->verify_no_references_recorded();
 511         }
 512         max_level_collected = i;
 513 
 514         // Determine if allocation request was met.
 515         if (size > 0) {
 516           if (!is_tlab || _gens[i]->supports_tlab_allocation()) {
 517             if (size*HeapWordSize <= _gens[i]->unsafe_max_alloc_nogc()) {
 518               size = 0;
 519             }
 520           }
 521         }
 522 
 523         COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
 524 
 525         _gens[i]->stat_record()->accumulated_time.stop();
 526 
 527         update_gc_stats(i, full);
 528 
 529         if (VerifyAfterGC && i >= VerifyGCLevel &&
 530             total_collections() >= VerifyGCStartAt) {
 531           HandleMark hm;  // Discard invalid handles created during verification
 532           Universe::verify(" VerifyAfterGC:");
 533         }
 534 
 535         if (PrintGCDetails) {
 536           gclog_or_tty->print(":");
 537           _gens[i]->print_heap_change(prev_used);
 538         }
 539       }
 540     }
 541 
 542     // Update "complete" boolean wrt what actually transpired --
 543     // for instance, a promotion failure could have led to
 544     // a whole heap collection.
 545     complete = complete || (max_level_collected == n_gens() - 1);
 546 
 547     if (complete) { // We did a "major" collection
 548       // FIXME: See comment at pre_full_gc_dump call
 549       post_full_gc_dump(NULL);   // do any post full gc dumps
 550     }
 551 
 552     if (PrintGCDetails) {
 553       print_heap_change(gch_prev_used);
 554 
 555       // Print metaspace info for full GC with PrintGCDetails flag.
 556       if (complete) {
 557         MetaspaceAux::print_metaspace_change(metadata_prev_used);
 558       }
 559     }
 560 
 561     for (int j = max_level_collected; j >= 0; j -= 1) {
 562       // Adjust generation sizes.
 563       _gens[j]->compute_new_size();
 564     }
 565 
 566     if (complete) {
 567       // Delete metaspaces for unloaded class loaders and clean up loader_data graph
 568       ClassLoaderDataGraph::purge();
 569       MetaspaceAux::verify_metrics();
 570       // Resize the metaspace capacity after full collections
 571       MetaspaceGC::compute_new_size();
 572       update_full_collections_completed();
 573     }
 574 
 575     // Track memory usage and detect low memory after GC finishes
 576     MemoryService::track_memory_usage();
 577 
 578     gc_epilogue(complete);
 579 
 580     if (must_restore_marks_for_biased_locking) {
 581       BiasedLocking::restore_marks();
 582     }
 583   }
 584 
 585   AdaptiveSizePolicy* sp = gen_policy()->size_policy();
 586   AdaptiveSizePolicyOutput(sp, total_collections());
 587 
 588   print_heap_after_gc();
 589 
 590 #ifdef TRACESPINNING
 591   ParallelTaskTerminator::print_termination_counts();
 592 #endif
 593 }
 594 
 595 HeapWord* GenCollectedHeap::satisfy_failed_allocation(size_t size, bool is_tlab) {
 596   return collector_policy()->satisfy_failed_allocation(size, is_tlab);
 597 }
 598 
 599 void GenCollectedHeap::set_par_threads(uint t) {
 600   SharedHeap::set_par_threads(t);
 601   set_n_termination(t);
 602 }
 603 
 604 void GenCollectedHeap::set_n_termination(uint t) {
 605   _process_strong_tasks->set_n_threads(t);
 606 }
 607 
 608 #ifdef ASSERT
 609 class AssertNonScavengableClosure: public OopClosure {
 610 public:
 611   virtual void do_oop(oop* p) {
 612     assert(!Universe::heap()->is_in_partial_collection(*p),
 613       "Referent should not be scavengable.");  }
 614   virtual void do_oop(narrowOop* p) { ShouldNotReachHere(); }
 615 };
 616 static AssertNonScavengableClosure assert_is_non_scavengable_closure;
 617 #endif
 618 
 619 void GenCollectedHeap::process_roots(bool activate_scope,
 620                                      ScanningOption so,
 621                                      OopClosure* strong_roots,
 622                                      OopClosure* weak_roots,
 623                                      CLDClosure* strong_cld_closure,
 624                                      CLDClosure* weak_cld_closure,
 625                                      CodeBlobClosure* code_roots) {
 626   StrongRootsScope srs(this, activate_scope);
 627 
 628   // General roots.
 629   assert(_strong_roots_parity != 0, "must have called prologue code");
 630   assert(code_roots != NULL, "code root closure should always be set");
 631   // _n_termination for _process_strong_tasks should be set up stream
 632   // in a method not running in a GC worker.  Otherwise the GC worker
 633   // could be trying to change the termination condition while the task
 634   // is executing in another GC worker.
 635 
 636   if (!_process_strong_tasks->is_task_claimed(GCH_PS_ClassLoaderDataGraph_oops_do)) {
 637     ClassLoaderDataGraph::roots_cld_do(strong_cld_closure, weak_cld_closure);
 638   }
 639 
 640   // Some CLDs contained in the thread frames should be considered strong.
 641   // Don't process them if they will be processed during the ClassLoaderDataGraph phase.
 642   CLDClosure* roots_from_clds_p = (strong_cld_closure != weak_cld_closure) ? strong_cld_closure : NULL;
 643   // Only process code roots from thread stacks if we aren't visiting the entire CodeCache anyway
 644   CodeBlobClosure* roots_from_code_p = (so & SO_AllCodeCache) ? NULL : code_roots;
 645 
 646   Threads::possibly_parallel_oops_do(strong_roots, roots_from_clds_p, roots_from_code_p);
 647 
 648   if (!_process_strong_tasks->is_task_claimed(GCH_PS_Universe_oops_do)) {
 649     Universe::oops_do(strong_roots);
 650   }
 651   // Global (strong) JNI handles
 652   if (!_process_strong_tasks->is_task_claimed(GCH_PS_JNIHandles_oops_do)) {
 653     JNIHandles::oops_do(strong_roots);
 654   }
 655 
 656   if (!_process_strong_tasks->is_task_claimed(GCH_PS_ObjectSynchronizer_oops_do)) {
 657     ObjectSynchronizer::oops_do(strong_roots);
 658   }
 659   if (!_process_strong_tasks->is_task_claimed(GCH_PS_FlatProfiler_oops_do)) {
 660     FlatProfiler::oops_do(strong_roots);
 661   }
 662   if (!_process_strong_tasks->is_task_claimed(GCH_PS_Management_oops_do)) {
 663     Management::oops_do(strong_roots);
 664   }
 665   if (!_process_strong_tasks->is_task_claimed(GCH_PS_jvmti_oops_do)) {
 666     JvmtiExport::oops_do(strong_roots);
 667   }
 668 
 669   if (!_process_strong_tasks->is_task_claimed(GCH_PS_SystemDictionary_oops_do)) {
 670     SystemDictionary::roots_oops_do(strong_roots, weak_roots);
 671   }
 672 
 673   // All threads execute the following. A specific chunk of buckets
 674   // from the StringTable are the individual tasks.
 675   if (weak_roots != NULL) {
 676     if (CollectedHeap::use_parallel_gc_threads()) {
 677       StringTable::possibly_parallel_oops_do(weak_roots);
 678     } else {
 679       StringTable::oops_do(weak_roots);
 680     }
 681   }
 682 
 683   if (!_process_strong_tasks->is_task_claimed(GCH_PS_CodeCache_oops_do)) {
 684     if (so & SO_ScavengeCodeCache) {
 685       assert(code_roots != NULL, "must supply closure for code cache");
 686 
 687       // We only visit parts of the CodeCache when scavenging.
 688       CodeCache::scavenge_root_nmethods_do(code_roots);
 689     }
 690     if (so & SO_AllCodeCache) {
 691       assert(code_roots != NULL, "must supply closure for code cache");
 692 
 693       // CMSCollector uses this to do intermediate-strength collections.
 694       // We scan the entire code cache, since CodeCache::do_unloading is not called.
 695       CodeCache::blobs_do(code_roots);
 696     }
 697     // Verify that the code cache contents are not subject to
 698     // movement by a scavenging collection.
 699     DEBUG_ONLY(CodeBlobToOopClosure assert_code_is_non_scavengable(&assert_is_non_scavengable_closure, !CodeBlobToOopClosure::FixRelocations));
 700     DEBUG_ONLY(CodeCache::asserted_non_scavengable_nmethods_do(&assert_code_is_non_scavengable));
 701   }
 702 
 703 }
 704 
 705 void GenCollectedHeap::gen_process_roots(int level,
 706                                          bool younger_gens_as_roots,
 707                                          bool activate_scope,
 708                                          ScanningOption so,
 709                                          bool only_strong_roots,
 710                                          OopsInGenClosure* not_older_gens,
 711                                          OopsInGenClosure* older_gens,
 712                                          CLDClosure* cld_closure) {
 713   const bool is_adjust_phase = !only_strong_roots && !younger_gens_as_roots;
 714 
 715   bool is_moving_collection = false;
 716   if (level == 0 || is_adjust_phase) {
 717     // young collections are always moving
 718     is_moving_collection = true;
 719   }
 720 
 721   MarkingCodeBlobClosure mark_code_closure(not_older_gens, is_moving_collection);
 722   OopsInGenClosure* weak_roots = only_strong_roots ? NULL : not_older_gens;
 723   CLDClosure* weak_cld_closure = only_strong_roots ? NULL : cld_closure;
 724 
 725   process_roots(activate_scope, so,
 726                 not_older_gens, weak_roots,
 727                 cld_closure, weak_cld_closure,
 728                 &mark_code_closure);
 729 
 730   if (younger_gens_as_roots) {
 731     if (!_process_strong_tasks->is_task_claimed(GCH_PS_younger_gens)) {
 732       for (int i = 0; i < level; i++) {
 733         not_older_gens->set_generation(_gens[i]);
 734         _gens[i]->oop_iterate(not_older_gens);
 735       }
 736       not_older_gens->reset_generation();
 737     }
 738   }
 739   // When collection is parallel, all threads get to cooperate to do
 740   // older-gen scanning.
 741   for (int i = level+1; i < _n_gens; i++) {
 742     older_gens->set_generation(_gens[i]);
 743     rem_set()->younger_refs_iterate(_gens[i], older_gens);
 744     older_gens->reset_generation();
 745   }
 746 
 747   _process_strong_tasks->all_tasks_completed();
 748 }
 749 
 750 
 751 class AlwaysTrueClosure: public BoolObjectClosure {
 752 public:
 753   bool do_object_b(oop p) { return true; }
 754 };
 755 static AlwaysTrueClosure always_true;
 756 
 757 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) {
 758   JNIHandles::weak_oops_do(&always_true, root_closure);
 759   JFR_ONLY(Jfr::weak_oops_do(&always_true, root_closure));
 760   for (int i = 0; i < _n_gens; i++) {
 761     _gens[i]->ref_processor()->weak_oops_do(root_closure);
 762   }
 763 }
 764 
 765 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix)    \
 766 void GenCollectedHeap::                                                 \
 767 oop_since_save_marks_iterate(int level,                                 \
 768                              OopClosureType* cur,                       \
 769                              OopClosureType* older) {                   \
 770   _gens[level]->oop_since_save_marks_iterate##nv_suffix(cur);           \
 771   for (int i = level+1; i < n_gens(); i++) {                            \
 772     _gens[i]->oop_since_save_marks_iterate##nv_suffix(older);           \
 773   }                                                                     \
 774 }
 775 
 776 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN)
 777 
 778 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN
 779 
 780 bool GenCollectedHeap::no_allocs_since_save_marks(int level) {
 781   for (int i = level; i < _n_gens; i++) {
 782     if (!_gens[i]->no_allocs_since_save_marks()) return false;
 783   }
 784   return true;
 785 }
 786 
 787 bool GenCollectedHeap::supports_inline_contig_alloc() const {
 788   return _gens[0]->supports_inline_contig_alloc();
 789 }
 790 
 791 HeapWord** GenCollectedHeap::top_addr() const {
 792   return _gens[0]->top_addr();
 793 }
 794 
 795 HeapWord** GenCollectedHeap::end_addr() const {
 796   return _gens[0]->end_addr();
 797 }
 798 
 799 // public collection interfaces
 800 
 801 void GenCollectedHeap::collect(GCCause::Cause cause) {
 802   if (should_do_concurrent_full_gc(cause)) {
 803 #if INCLUDE_ALL_GCS
 804     // mostly concurrent full collection
 805     collect_mostly_concurrent(cause);
 806 #else  // INCLUDE_ALL_GCS
 807     ShouldNotReachHere();
 808 #endif // INCLUDE_ALL_GCS
 809   } else if (cause == GCCause::_wb_young_gc) {
 810     // minor collection for WhiteBox API
 811     collect(cause, 0);
 812   } else {
 813 #ifdef ASSERT
 814   if (cause == GCCause::_scavenge_alot) {
 815     // minor collection only
 816     collect(cause, 0);
 817   } else {
 818     // Stop-the-world full collection
 819     collect(cause, n_gens() - 1);
 820   }
 821 #else
 822     // Stop-the-world full collection
 823     collect(cause, n_gens() - 1);
 824 #endif
 825   }
 826 }
 827 
 828 void GenCollectedHeap::collect(GCCause::Cause cause, int max_level) {
 829   // The caller doesn't have the Heap_lock
 830   assert(!Heap_lock->owned_by_self(), "this thread should not own the Heap_lock");
 831   MutexLocker ml(Heap_lock);
 832   collect_locked(cause, max_level);
 833 }
 834 
 835 void GenCollectedHeap::collect_locked(GCCause::Cause cause) {
 836   // The caller has the Heap_lock
 837   assert(Heap_lock->owned_by_self(), "this thread should own the Heap_lock");
 838   collect_locked(cause, n_gens() - 1);
 839 }
 840 
 841 // this is the private collection interface
 842 // The Heap_lock is expected to be held on entry.
 843 
 844 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) {
 845   // Read the GC count while holding the Heap_lock
 846   unsigned int gc_count_before      = total_collections();
 847   unsigned int full_gc_count_before = total_full_collections();
 848   {
 849     MutexUnlocker mu(Heap_lock);  // give up heap lock, execute gets it back
 850     VM_GenCollectFull op(gc_count_before, full_gc_count_before,
 851                          cause, max_level);
 852     VMThread::execute(&op);
 853   }
 854 }
 855 
 856 #if INCLUDE_ALL_GCS
 857 bool GenCollectedHeap::create_cms_collector() {
 858 
 859   assert(((_gens[1]->kind() == Generation::ConcurrentMarkSweep) ||
 860          (_gens[1]->kind() == Generation::ASConcurrentMarkSweep)),
 861          "Unexpected generation kinds");
 862   // Skip two header words in the block content verification
 863   NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();)
 864   CMSCollector* collector = new CMSCollector(
 865     (ConcurrentMarkSweepGeneration*)_gens[1],
 866     _rem_set->as_CardTableRS(),
 867     (ConcurrentMarkSweepPolicy*) collector_policy());
 868 
 869   if (collector == NULL || !collector->completed_initialization()) {
 870     if (collector) {
 871       delete collector;  // Be nice in embedded situation
 872     }
 873     vm_shutdown_during_initialization("Could not create CMS collector");
 874     return false;
 875   }
 876   return true;  // success
 877 }
 878 
 879 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) {
 880   assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
 881 
 882   MutexLocker ml(Heap_lock);
 883   // Read the GC counts while holding the Heap_lock
 884   unsigned int full_gc_count_before = total_full_collections();
 885   unsigned int gc_count_before      = total_collections();
 886   {
 887     MutexUnlocker mu(Heap_lock);
 888     VM_GenCollectFullConcurrent op(gc_count_before, full_gc_count_before, cause);
 889     VMThread::execute(&op);
 890   }
 891 }
 892 #endif // INCLUDE_ALL_GCS
 893 
 894 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs) {
 895    do_full_collection(clear_all_soft_refs, _n_gens - 1);
 896 }
 897 
 898 void GenCollectedHeap::do_full_collection(bool clear_all_soft_refs,
 899                                           int max_level) {
 900   int local_max_level;
 901   if (!incremental_collection_will_fail(false /* don't consult_young */) &&
 902       gc_cause() == GCCause::_gc_locker) {
 903     local_max_level = 0;
 904   } else {
 905     local_max_level = max_level;
 906   }
 907 
 908   do_collection(true                 /* full */,
 909                 clear_all_soft_refs  /* clear_all_soft_refs */,
 910                 0                    /* size */,
 911                 false                /* is_tlab */,
 912                 local_max_level      /* max_level */);
 913   // Hack XXX FIX ME !!!
 914   // A scavenge may not have been attempted, or may have
 915   // been attempted and failed, because the old gen was too full
 916   if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker &&
 917       incremental_collection_will_fail(false /* don't consult_young */)) {
 918     if (PrintGCDetails) {
 919       gclog_or_tty->print_cr("GC locker: Trying a full collection "
 920                              "because scavenge failed");
 921     }
 922     // This time allow the old gen to be collected as well
 923     do_collection(true                 /* full */,
 924                   clear_all_soft_refs  /* clear_all_soft_refs */,
 925                   0                    /* size */,
 926                   false                /* is_tlab */,
 927                   n_gens() - 1         /* max_level */);
 928   }
 929 }
 930 
 931 bool GenCollectedHeap::is_in_young(oop p) {
 932   bool result = ((HeapWord*)p) < _gens[_n_gens - 1]->reserved().start();
 933   assert(result == _gens[0]->is_in_reserved(p),
 934          err_msg("incorrect test - result=%d, p=" PTR_FORMAT, result, p2i((void*)p)));
 935   return result;
 936 }
 937 
 938 // Returns "TRUE" iff "p" points into the committed areas of the heap.
 939 bool GenCollectedHeap::is_in(const void* p) const {
 940   #ifndef ASSERT
 941   guarantee(VerifyBeforeGC      ||
 942             VerifyDuringGC      ||
 943             VerifyBeforeExit    ||
 944             VerifyDuringStartup ||
 945             PrintAssembly       ||
 946             tty->count() != 0   ||   // already printing
 947             VerifyAfterGC       ||
 948     VMError::fatal_error_in_progress(), "too expensive");
 949 
 950   #endif
 951   // This might be sped up with a cache of the last generation that
 952   // answered yes.
 953   for (int i = 0; i < _n_gens; i++) {
 954     if (_gens[i]->is_in(p)) return true;
 955   }
 956   // Otherwise...
 957   return false;
 958 }
 959 
 960 #ifdef ASSERT
 961 // Don't implement this by using is_in_young().  This method is used
 962 // in some cases to check that is_in_young() is correct.
 963 bool GenCollectedHeap::is_in_partial_collection(const void* p) {
 964   assert(is_in_reserved(p) || p == NULL,
 965     "Does not work if address is non-null and outside of the heap");
 966   return p < _gens[_n_gens - 2]->reserved().end() && p != NULL;
 967 }
 968 #endif
 969 
 970 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) {
 971   for (int i = 0; i < _n_gens; i++) {
 972     _gens[i]->oop_iterate(cl);
 973   }
 974 }
 975 
 976 void GenCollectedHeap::object_iterate(ObjectClosure* cl) {
 977   for (int i = 0; i < _n_gens; i++) {
 978     _gens[i]->object_iterate(cl);
 979   }
 980 }
 981 
 982 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) {
 983   for (int i = 0; i < _n_gens; i++) {
 984     _gens[i]->safe_object_iterate(cl);
 985   }
 986 }
 987 
 988 Space* GenCollectedHeap::space_containing(const void* addr) const {
 989   for (int i = 0; i < _n_gens; i++) {
 990     Space* res = _gens[i]->space_containing(addr);
 991     if (res != NULL) return res;
 992   }
 993   // Otherwise...
 994   assert(false, "Could not find containing space");
 995   return NULL;
 996 }
 997 
 998 
 999 HeapWord* GenCollectedHeap::block_start(const void* addr) const {
1000   assert(is_in_reserved(addr), "block_start of address outside of heap");
1001   for (int i = 0; i < _n_gens; i++) {
1002     if (_gens[i]->is_in_reserved(addr)) {
1003       assert(_gens[i]->is_in(addr),
1004              "addr should be in allocated part of generation");
1005       return _gens[i]->block_start(addr);
1006     }
1007   }
1008   assert(false, "Some generation should contain the address");
1009   return NULL;
1010 }
1011 
1012 size_t GenCollectedHeap::block_size(const HeapWord* addr) const {
1013   assert(is_in_reserved(addr), "block_size of address outside of heap");
1014   for (int i = 0; i < _n_gens; i++) {
1015     if (_gens[i]->is_in_reserved(addr)) {
1016       assert(_gens[i]->is_in(addr),
1017              "addr should be in allocated part of generation");
1018       return _gens[i]->block_size(addr);
1019     }
1020   }
1021   assert(false, "Some generation should contain the address");
1022   return 0;
1023 }
1024 
1025 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const {
1026   assert(is_in_reserved(addr), "block_is_obj of address outside of heap");
1027   assert(block_start(addr) == addr, "addr must be a block start");
1028   for (int i = 0; i < _n_gens; i++) {
1029     if (_gens[i]->is_in_reserved(addr)) {
1030       return _gens[i]->block_is_obj(addr);
1031     }
1032   }
1033   assert(false, "Some generation should contain the address");
1034   return false;
1035 }
1036 
1037 bool GenCollectedHeap::supports_tlab_allocation() const {
1038   for (int i = 0; i < _n_gens; i += 1) {
1039     if (_gens[i]->supports_tlab_allocation()) {
1040       return true;
1041     }
1042   }
1043   return false;
1044 }
1045 
1046 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const {
1047   size_t result = 0;
1048   for (int i = 0; i < _n_gens; i += 1) {
1049     if (_gens[i]->supports_tlab_allocation()) {
1050       result += _gens[i]->tlab_capacity();
1051     }
1052   }
1053   return result;
1054 }
1055 
1056 size_t GenCollectedHeap::tlab_used(Thread* thr) const {
1057   size_t result = 0;
1058   for (int i = 0; i < _n_gens; i += 1) {
1059     if (_gens[i]->supports_tlab_allocation()) {
1060       result += _gens[i]->tlab_used();
1061     }
1062   }
1063   return result;
1064 }
1065 
1066 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const {
1067   size_t result = 0;
1068   for (int i = 0; i < _n_gens; i += 1) {
1069     if (_gens[i]->supports_tlab_allocation()) {
1070       result += _gens[i]->unsafe_max_tlab_alloc();
1071     }
1072   }
1073   return result;
1074 }
1075 
1076 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) {
1077   bool gc_overhead_limit_was_exceeded;
1078   return collector_policy()->mem_allocate_work(size /* size */,
1079                                                true /* is_tlab */,
1080                                                &gc_overhead_limit_was_exceeded);
1081 }
1082 
1083 // Requires "*prev_ptr" to be non-NULL.  Deletes and a block of minimal size
1084 // from the list headed by "*prev_ptr".
1085 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) {
1086   bool first = true;
1087   size_t min_size = 0;   // "first" makes this conceptually infinite.
1088   ScratchBlock **smallest_ptr, *smallest;
1089   ScratchBlock  *cur = *prev_ptr;
1090   while (cur) {
1091     assert(*prev_ptr == cur, "just checking");
1092     if (first || cur->num_words < min_size) {
1093       smallest_ptr = prev_ptr;
1094       smallest     = cur;
1095       min_size     = smallest->num_words;
1096       first        = false;
1097     }
1098     prev_ptr = &cur->next;
1099     cur     =  cur->next;
1100   }
1101   smallest      = *smallest_ptr;
1102   *smallest_ptr = smallest->next;
1103   return smallest;
1104 }
1105 
1106 // Sort the scratch block list headed by res into decreasing size order,
1107 // and set "res" to the result.
1108 static void sort_scratch_list(ScratchBlock*& list) {
1109   ScratchBlock* sorted = NULL;
1110   ScratchBlock* unsorted = list;
1111   while (unsorted) {
1112     ScratchBlock *smallest = removeSmallestScratch(&unsorted);
1113     smallest->next  = sorted;
1114     sorted          = smallest;
1115   }
1116   list = sorted;
1117 }
1118 
1119 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor,
1120                                                size_t max_alloc_words) {
1121   ScratchBlock* res = NULL;
1122   for (int i = 0; i < _n_gens; i++) {
1123     _gens[i]->contribute_scratch(res, requestor, max_alloc_words);
1124   }
1125   sort_scratch_list(res);
1126   return res;
1127 }
1128 
1129 void GenCollectedHeap::release_scratch() {
1130   for (int i = 0; i < _n_gens; i++) {
1131     _gens[i]->reset_scratch();
1132   }
1133 }
1134 
1135 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure {
1136   void do_generation(Generation* gen) {
1137     gen->prepare_for_verify();
1138   }
1139 };
1140 
1141 void GenCollectedHeap::prepare_for_verify() {
1142   ensure_parsability(false);        // no need to retire TLABs
1143   GenPrepareForVerifyClosure blk;
1144   generation_iterate(&blk, false);
1145 }
1146 
1147 
1148 void GenCollectedHeap::generation_iterate(GenClosure* cl,
1149                                           bool old_to_young) {
1150   if (old_to_young) {
1151     for (int i = _n_gens-1; i >= 0; i--) {
1152       cl->do_generation(_gens[i]);
1153     }
1154   } else {
1155     for (int i = 0; i < _n_gens; i++) {
1156       cl->do_generation(_gens[i]);
1157     }
1158   }
1159 }
1160 
1161 void GenCollectedHeap::space_iterate(SpaceClosure* cl) {
1162   for (int i = 0; i < _n_gens; i++) {
1163     _gens[i]->space_iterate(cl, true);
1164   }
1165 }
1166 
1167 bool GenCollectedHeap::is_maximal_no_gc() const {
1168   for (int i = 0; i < _n_gens; i++) {
1169     if (!_gens[i]->is_maximal_no_gc()) {
1170       return false;
1171     }
1172   }
1173   return true;
1174 }
1175 
1176 void GenCollectedHeap::save_marks() {
1177   for (int i = 0; i < _n_gens; i++) {
1178     _gens[i]->save_marks();
1179   }
1180 }
1181 
1182 GenCollectedHeap* GenCollectedHeap::heap() {
1183   assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()");
1184   assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap");
1185   return _gch;
1186 }
1187 
1188 
1189 void GenCollectedHeap::prepare_for_compaction() {
1190   guarantee(_n_gens = 2, "Wrong number of generations");
1191   Generation* old_gen = _gens[1];
1192   // Start by compacting into same gen.
1193   CompactPoint cp(old_gen);
1194   old_gen->prepare_for_compaction(&cp);
1195   Generation* young_gen = _gens[0];
1196   young_gen->prepare_for_compaction(&cp);
1197 }
1198 
1199 GCStats* GenCollectedHeap::gc_stats(int level) const {
1200   return _gens[level]->gc_stats();
1201 }
1202 
1203 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) {
1204   for (int i = _n_gens-1; i >= 0; i--) {
1205     Generation* g = _gens[i];
1206     if (!silent) {
1207       gclog_or_tty->print("%s", g->name());
1208       gclog_or_tty->print(" ");
1209     }
1210     g->verify();
1211   }
1212   if (!silent) {
1213     gclog_or_tty->print("remset ");
1214   }
1215   rem_set()->verify();
1216 }
1217 
1218 void GenCollectedHeap::print_on(outputStream* st) const {
1219   for (int i = 0; i < _n_gens; i++) {
1220     _gens[i]->print_on(st);
1221   }
1222   MetaspaceAux::print_on(st);
1223 }
1224 
1225 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
1226   if (workers() != NULL) {
1227     workers()->threads_do(tc);
1228   }
1229 #if INCLUDE_ALL_GCS
1230   if (UseConcMarkSweepGC) {
1231     ConcurrentMarkSweepThread::threads_do(tc);
1232   }
1233 #endif // INCLUDE_ALL_GCS
1234 }
1235 
1236 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const {
1237 #if INCLUDE_ALL_GCS
1238   if (UseParNewGC) {
1239     workers()->print_worker_threads_on(st);
1240   }
1241   if (UseConcMarkSweepGC) {
1242     ConcurrentMarkSweepThread::print_all_on(st);
1243   }
1244 #endif // INCLUDE_ALL_GCS
1245 }
1246 
1247 void GenCollectedHeap::print_on_error(outputStream* st) const {
1248   this->CollectedHeap::print_on_error(st);
1249 
1250 #if INCLUDE_ALL_GCS
1251   if (UseConcMarkSweepGC) {
1252     st->cr();
1253     CMSCollector::print_on_error(st);
1254   }
1255 #endif // INCLUDE_ALL_GCS
1256 }
1257 
1258 void GenCollectedHeap::print_tracing_info() const {
1259   if (TraceGen0Time) {
1260     get_gen(0)->print_summary_info();
1261   }
1262   if (TraceGen1Time) {
1263     get_gen(1)->print_summary_info();
1264   }
1265 }
1266 
1267 void GenCollectedHeap::print_heap_change(size_t prev_used) const {
1268   if (PrintGCDetails && Verbose) {
1269     gclog_or_tty->print(" "  SIZE_FORMAT
1270                         "->" SIZE_FORMAT
1271                         "("  SIZE_FORMAT ")",
1272                         prev_used, used(), capacity());
1273   } else {
1274     gclog_or_tty->print(" "  SIZE_FORMAT "K"
1275                         "->" SIZE_FORMAT "K"
1276                         "("  SIZE_FORMAT "K)",
1277                         prev_used / K, used() / K, capacity() / K);
1278   }
1279 }
1280 
1281 class GenGCPrologueClosure: public GenCollectedHeap::GenClosure {
1282  private:
1283   bool _full;
1284  public:
1285   void do_generation(Generation* gen) {
1286     gen->gc_prologue(_full);
1287   }
1288   GenGCPrologueClosure(bool full) : _full(full) {};
1289 };
1290 
1291 void GenCollectedHeap::gc_prologue(bool full) {
1292   assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
1293 
1294   always_do_update_barrier = false;
1295   // Fill TLAB's and such
1296   CollectedHeap::accumulate_statistics_all_tlabs();
1297   ensure_parsability(true);   // retire TLABs
1298 
1299   // Walk generations
1300   GenGCPrologueClosure blk(full);
1301   generation_iterate(&blk, false);  // not old-to-young.
1302 };
1303 
1304 class GenGCEpilogueClosure: public GenCollectedHeap::GenClosure {
1305  private:
1306   bool _full;
1307  public:
1308   void do_generation(Generation* gen) {
1309     gen->gc_epilogue(_full);
1310   }
1311   GenGCEpilogueClosure(bool full) : _full(full) {};
1312 };
1313 
1314 void GenCollectedHeap::gc_epilogue(bool full) {
1315 #ifdef COMPILER2
1316   assert(DerivedPointerTable::is_empty(), "derived pointer present");
1317   size_t actual_gap = pointer_delta((HeapWord*) (max_uintx-3), *(end_addr()));
1318   guarantee(actual_gap > (size_t)FastAllocateSizeLimit, "inline allocation wraps");
1319 #endif /* COMPILER2 */
1320 
1321   resize_all_tlabs();
1322 
1323   GenGCEpilogueClosure blk(full);
1324   generation_iterate(&blk, false);  // not old-to-young.
1325 
1326   if (!CleanChunkPoolAsync) {
1327     Chunk::clean_chunk_pool();
1328   }
1329 
1330   MetaspaceCounters::update_performance_counters();
1331   CompressedClassSpaceCounters::update_performance_counters();
1332 
1333   always_do_update_barrier = UseConcMarkSweepGC;
1334 };
1335 
1336 #ifndef PRODUCT
1337 class GenGCSaveTopsBeforeGCClosure: public GenCollectedHeap::GenClosure {
1338  private:
1339  public:
1340   void do_generation(Generation* gen) {
1341     gen->record_spaces_top();
1342   }
1343 };
1344 
1345 void GenCollectedHeap::record_gen_tops_before_GC() {
1346   if (ZapUnusedHeapArea) {
1347     GenGCSaveTopsBeforeGCClosure blk;
1348     generation_iterate(&blk, false);  // not old-to-young.
1349   }
1350 }
1351 #endif  // not PRODUCT
1352 
1353 class GenEnsureParsabilityClosure: public GenCollectedHeap::GenClosure {
1354  public:
1355   void do_generation(Generation* gen) {
1356     gen->ensure_parsability();
1357   }
1358 };
1359 
1360 void GenCollectedHeap::ensure_parsability(bool retire_tlabs) {
1361   CollectedHeap::ensure_parsability(retire_tlabs);
1362   GenEnsureParsabilityClosure ep_cl;
1363   generation_iterate(&ep_cl, false);
1364 }
1365 
1366 oop GenCollectedHeap::handle_failed_promotion(Generation* old_gen,
1367                                               oop obj,
1368                                               size_t obj_size) {
1369   guarantee(old_gen->level() == 1, "We only get here with an old generation");
1370   assert(obj_size == (size_t)obj->size(), "bad obj_size passed in");
1371   HeapWord* result = NULL;
1372 
1373   result = old_gen->expand_and_allocate(obj_size, false);
1374 
1375   if (result != NULL) {
1376     Copy::aligned_disjoint_words((HeapWord*)obj, result, obj_size);
1377   }
1378   return oop(result);
1379 }
1380 
1381 class GenTimeOfLastGCClosure: public GenCollectedHeap::GenClosure {
1382   jlong _time;   // in ms
1383   jlong _now;    // in ms
1384 
1385  public:
1386   GenTimeOfLastGCClosure(jlong now) : _time(now), _now(now) { }
1387 
1388   jlong time() { return _time; }
1389 
1390   void do_generation(Generation* gen) {
1391     _time = MIN2(_time, gen->time_of_last_gc(_now));
1392   }
1393 };
1394 
1395 jlong GenCollectedHeap::millis_since_last_gc() {
1396   // We need a monotonically non-deccreasing time in ms but
1397   // os::javaTimeMillis() does not guarantee monotonicity.
1398   jlong now = os::javaTimeNanos() / NANOSECS_PER_MILLISEC;
1399   GenTimeOfLastGCClosure tolgc_cl(now);
1400   // iterate over generations getting the oldest
1401   // time that a generation was collected
1402   generation_iterate(&tolgc_cl, false);
1403 
1404   // javaTimeNanos() is guaranteed to be monotonically non-decreasing
1405   // provided the underlying platform provides such a time source
1406   // (and it is bug free). So we still have to guard against getting
1407   // back a time later than 'now'.
1408   jlong retVal = now - tolgc_cl.time();
1409   if (retVal < 0) {
1410     NOT_PRODUCT(warning("time warp: "INT64_FORMAT, (int64_t) retVal);)
1411     return 0;
1412   }
1413   return retVal;
1414 }