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