< prev index next >

src/share/vm/memory/genCollectedHeap.cpp

Print this page


   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  *


  70 
  71 GenCollectedHeap::GenCollectedHeap(GenCollectorPolicy *policy) :
  72   SharedHeap(policy),
  73   _rem_set(NULL),
  74   _gen_policy(policy),
  75   _gen_process_roots_tasks(new SubTasksDone(GCH_PS_NumElements)),
  76   _full_collections_completed(0)
  77 {
  78   if (_gen_process_roots_tasks == NULL ||
  79       !_gen_process_roots_tasks->valid()) {
  80     vm_exit_during_initialization("Failed necessary allocation.");
  81   }
  82   assert(policy != NULL, "Sanity check");
  83 }
  84 
  85 jint GenCollectedHeap::initialize() {
  86   CollectedHeap::pre_initialize();
  87 
  88   int i;
  89   _n_gens = gen_policy()->number_of_generations();

  90 
  91   // While there are no constraints in the GC code that HeapWordSize
  92   // be any particular value, there are multiple other areas in the
  93   // system which believe this to be true (e.g. oop->object_size in some
  94   // cases incorrectly returns the size in wordSize units rather than
  95   // HeapWordSize).
  96   guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
  97 
  98   // The heap must be at least as aligned as generations.
  99   size_t gen_alignment = Generation::GenGrain;
 100 
 101   _gen_specs = gen_policy()->generations();
 102 
 103   // Make sure the sizes are all aligned.
 104   for (i = 0; i < _n_gens; i++) {
 105     _gen_specs[i]->align(gen_alignment);
 106   }
 107 
 108   // Allocate space for the heap.
 109 


 111   size_t total_reserved = 0;
 112   ReservedSpace heap_rs;
 113 
 114   size_t heap_alignment = collector_policy()->heap_alignment();
 115 
 116   heap_address = allocate(heap_alignment, &total_reserved, &heap_rs);
 117 
 118   if (!heap_rs.is_reserved()) {
 119     vm_shutdown_during_initialization(
 120       "Could not reserve enough space for object heap");
 121     return JNI_ENOMEM;
 122   }
 123 
 124   initialize_reserved_region((HeapWord*)heap_rs.base(), (HeapWord*)(heap_rs.base() + heap_rs.size()));
 125 
 126   _rem_set = collector_policy()->create_rem_set(reserved_region());
 127   set_barrier_set(rem_set()->bs());
 128 
 129   _gch = this;
 130 
 131   for (i = 0; i < _n_gens; i++) {
 132     ReservedSpace this_rs = heap_rs.first_part(_gen_specs[i]->max_size(), false, false);
 133     _gens[i] = _gen_specs[i]->init(this_rs, i, rem_set());
 134     heap_rs = heap_rs.last_part(_gen_specs[i]->max_size());
 135   }

 136   clear_incremental_collection_failed();
 137 
 138 #if INCLUDE_ALL_GCS
 139   // If we are running CMS, create the collector responsible
 140   // for collecting the CMS generations.
 141   if (collector_policy()->is_concurrent_mark_sweep_policy()) {
 142     bool success = create_cms_collector();
 143     if (!success) return JNI_ENOMEM;
 144   }
 145 #endif // INCLUDE_ALL_GCS
 146 
 147   return JNI_OK;
 148 }
 149 
 150 
 151 char* GenCollectedHeap::allocate(size_t alignment,
 152                                  size_t* _total_reserved,
 153                                  ReservedSpace* heap_rs){
 154   const char overflow_msg[] = "The size of the object heap + VM data exceeds "
 155     "the maximum representable size";
 156 
 157   // Now figure out the total size.
 158   size_t total_reserved = 0;
 159   const size_t pageSize = UseLargePages ?
 160       os::large_page_size() : os::vm_page_size();
 161 
 162   assert(alignment % pageSize == 0, "Must be");
 163 
 164   for (int i = 0; i < _n_gens; i++) {
 165     total_reserved += _gen_specs[i]->max_size();
 166     if (total_reserved < _gen_specs[i]->max_size()) {
 167       vm_exit_during_initialization(overflow_msg);
 168     }
 169   }
 170   assert(total_reserved % alignment == 0,
 171          err_msg("Gen size; total_reserved=" SIZE_FORMAT ", alignment="
 172                  SIZE_FORMAT, total_reserved, alignment));
 173 
 174   *_total_reserved = total_reserved;
 175 
 176   *heap_rs = Universe::reserve_heap(total_reserved, alignment);
 177   return heap_rs->base();
 178 }
 179 
 180 
 181 void GenCollectedHeap::post_initialize() {
 182   SharedHeap::post_initialize();
 183   GenCollectorPolicy *policy = (GenCollectorPolicy *)collector_policy();
 184   guarantee(policy->is_generation_policy(), "Illegal policy type");
 185   assert((get_gen(0)->kind() == Generation::DefNew) ||
 186          (get_gen(0)->kind() == Generation::ParNew),
 187     "Wrong youngest generation type");
 188   DefNewGeneration* def_new_gen = (DefNewGeneration*)get_gen(0);
 189 
 190   Generation* old_gen = get_gen(1);
 191   assert(old_gen->kind() == Generation::ConcurrentMarkSweep ||
 192          old_gen->kind() == Generation::MarkSweepCompact,
 193     "Wrong generation kind");
 194 
 195   policy->initialize_size_policy(def_new_gen->eden()->capacity(),
 196                                  old_gen->capacity(),
 197                                  def_new_gen->from()->capacity());
 198   policy->initialize_gc_policy_counters();
 199 }
 200 
 201 void GenCollectedHeap::ref_processing_init() {
 202   SharedHeap::ref_processing_init();
 203   for (int i = 0; i < _n_gens; i++) {
 204     _gens[i]->ref_processor_init();
 205   }
 206 }
 207 
 208 size_t GenCollectedHeap::capacity() const {
 209   size_t res = 0;
 210   for (int i = 0; i < _n_gens; i++) {
 211     res += _gens[i]->capacity();
 212   }
 213   return res;
 214 }
 215 
 216 size_t GenCollectedHeap::used() const {
 217   size_t res = 0;
 218   for (int i = 0; i < _n_gens; i++) {
 219     res += _gens[i]->used();
 220   }
 221   return res;
 222 }
 223 
 224 // Save the "used_region" for generations level and lower.
 225 void GenCollectedHeap::save_used_regions(int level) {

 226   assert(level < _n_gens, "Illegal level parameter");
 227   for (int i = level; i >= 0; i--) {
 228     _gens[i]->save_used_region();
 229   }

 230 }
 231 
 232 size_t GenCollectedHeap::max_capacity() const {
 233   size_t res = 0;
 234   for (int i = 0; i < _n_gens; i++) {
 235     res += _gens[i]->max_capacity();
 236   }
 237   return res;
 238 }
 239 
 240 // Update the _full_collections_completed counter
 241 // at the end of a stop-world full GC.
 242 unsigned int GenCollectedHeap::update_full_collections_completed() {
 243   MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
 244   assert(_full_collections_completed <= _total_full_collections,
 245          "Can't complete more collections than were started");
 246   _full_collections_completed = _total_full_collections;
 247   ml.notify_all();
 248   return _full_collections_completed;
 249 }
 250 
 251 // Update the _full_collections_completed counter, as appropriate,
 252 // at the end of a concurrent GC cycle. Note the conditional update
 253 // below to allow this method to be called by a concurrent collector
 254 // without synchronizing in any manner with the VM thread (which
 255 // may already have initiated a STW full collection "concurrently").
 256 unsigned int GenCollectedHeap::update_full_collections_completed(unsigned int count) {
 257   MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);


 281 // higher than we are prepared to pay for such rudimentary debugging
 282 // support.
 283 void GenCollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr,
 284                                                          size_t size) {
 285   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
 286     // We are asked to check a size in HeapWords,
 287     // but the memory is mangled in juint words.
 288     juint* start = (juint*) (addr + skip_header_HeapWords());
 289     juint* end   = (juint*) (addr + size);
 290     for (juint* slot = start; slot < end; slot += 1) {
 291       assert(*slot == badHeapWordVal,
 292              "Found non badHeapWordValue in pre-allocation check");
 293     }
 294   }
 295 }
 296 #endif
 297 
 298 HeapWord* GenCollectedHeap::attempt_allocation(size_t size,
 299                                                bool is_tlab,
 300                                                bool first_only) {
 301   HeapWord* res;
 302   for (int i = 0; i < _n_gens; i++) {
 303     if (_gens[i]->should_allocate(size, is_tlab)) {
 304       res = _gens[i]->allocate(size, is_tlab);
 305       if (res != NULL) return res;
 306       else if (first_only) break;

 307     }



 308   }
 309   // Otherwise...
 310   return NULL;
 311 }
 312 
 313 HeapWord* GenCollectedHeap::mem_allocate(size_t size,
 314                                          bool* gc_overhead_limit_was_exceeded) {
 315   return collector_policy()->mem_allocate_work(size,
 316                                                false /* is_tlab */,
 317                                                gc_overhead_limit_was_exceeded);
 318 }
 319 
 320 bool GenCollectedHeap::must_clear_all_soft_refs() {
 321   return _gc_cause == GCCause::_last_ditch_collection;
 322 }
 323 
 324 bool GenCollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
 325   return UseConcMarkSweepGC &&
 326          ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) ||
 327           (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent));
 328 }
 329 
 330 void GenCollectedHeap::do_collection(bool  full,
 331                                      bool   clear_all_soft_refs,
 332                                      size_t size,
 333                                      bool   is_tlab,
 334                                      int    max_level) {
 335   bool prepared_for_verification = false;
 336   ResourceMark rm;
 337   DEBUG_ONLY(Thread* my_thread = Thread::current();)
 338 
 339   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 340   assert(my_thread->is_VM_thread() ||
 341          my_thread->is_ConcurrentGC_thread(),
 342          "incorrect thread type capability");
 343   assert(Heap_lock->is_locked(),
 344          "the requesting thread should have the Heap_lock");
 345   guarantee(!is_gc_active(), "collection is not reentrant");
 346   assert(max_level < n_gens(), "sanity check");
 347 
 348   if (GC_locker::check_active_before_gc()) {
 349     return; // GC is disabled (e.g. JNI GetXXXCritical operation)
 350   }
 351 
 352   const bool do_clear_all_soft_refs = clear_all_soft_refs ||
 353                           collector_policy()->should_clear_all_soft_refs();
 354 
 355   ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy());
 356 
 357   const size_t metadata_prev_used = MetaspaceAux::used_bytes();
 358 
 359   print_heap_before_gc();
 360 
 361   {
 362     FlagSetting fl(_is_gc_active, true);
 363 
 364     bool complete = full && (max_level == (n_gens()-1));
 365     const char* gc_cause_prefix = complete ? "Full GC" : "GC";
 366     TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
 367     // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
 368     // so we can assume here that the next GC id is what we want.
 369     GCTraceTime t(GCCauseString(gc_cause_prefix, gc_cause()), PrintGCDetails, false, NULL, GCId::peek());
 370 
 371     gc_prologue(complete);
 372     increment_total_collections(complete);
 373 
 374     size_t gch_prev_used = used();
 375 
 376     int starting_level = 0;
 377     if (full) {
 378       // Search for the oldest generation which will collect all younger
 379       // generations, and start collection loop there.
 380       for (int i = max_level; i >= 0; i--) {
 381         if (_gens[i]->full_collects_younger_generations()) {
 382           starting_level = i;
 383           break;
 384         }
 385       }
 386     }
 387 
 388     bool must_restore_marks_for_biased_locking = false;
 389 
 390     int max_level_collected = starting_level;
 391     for (int i = starting_level; i <= max_level; i++) {
 392       if (_gens[i]->should_collect(full, size, is_tlab)) {
 393         if (i == n_gens() - 1) {  // a major collection is to happen
 394           if (!complete) {
 395             // The full_collections increment was missed above.
 396             increment_total_full_collections();
 397           }
 398           pre_full_gc_dump(NULL);    // do any pre full gc dumps
 399         }
 400         // Timer for individual generations. Last argument is false: no CR
 401         // FIXME: We should try to start the timing earlier to cover more of the GC pause
 402         // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
 403         // so we can assume here that the next GC id is what we want.
 404         GCTraceTime t1(_gens[i]->short_name(), PrintGCDetails, false, NULL, GCId::peek());
 405         TraceCollectorStats tcs(_gens[i]->counters());
 406         TraceMemoryManagerStats tmms(_gens[i]->kind(),gc_cause());
 407 
 408         size_t prev_used = _gens[i]->used();
 409         _gens[i]->stat_record()->invocations++;
 410         _gens[i]->stat_record()->accumulated_time.start();
 411 
 412         // Must be done anew before each collection because
 413         // a previous collection will do mangling and will
 414         // change top of some spaces.
 415         record_gen_tops_before_GC();
 416 
 417         if (PrintGC && Verbose) {
 418           gclog_or_tty->print("level=%d invoke=%d size=" SIZE_FORMAT,
 419                      i,
 420                      _gens[i]->stat_record()->invocations,
 421                      size*HeapWordSize);
 422         }
 423 
 424         if (VerifyBeforeGC && i >= VerifyGCLevel &&
 425             total_collections() >= VerifyGCStartAt) {
 426           HandleMark hm;  // Discard invalid handles created during verification
 427           if (!prepared_for_verification) {
 428             prepare_for_verify();
 429             prepared_for_verification = true;
 430           }
 431           Universe::verify(" VerifyBeforeGC:");
 432         }
 433         COMPILER2_PRESENT(DerivedPointerTable::clear());
 434 
 435         if (!must_restore_marks_for_biased_locking &&
 436             _gens[i]->performs_in_place_marking()) {
 437           // We perform this mark word preservation work lazily
 438           // because it's only at this point that we know whether we
 439           // absolutely have to do it; we want to avoid doing it for
 440           // scavenge-only collections where it's unnecessary
 441           must_restore_marks_for_biased_locking = true;
 442           BiasedLocking::preserve_marks();
 443         }
 444 
 445         // Do collection work
 446         {
 447           // Note on ref discovery: For what appear to be historical reasons,
 448           // GCH enables and disabled (by enqueing) refs discovery.
 449           // In the future this should be moved into the generation's
 450           // collect method so that ref discovery and enqueueing concerns
 451           // are local to a generation. The collect method could return
 452           // an appropriate indication in the case that notification on
 453           // the ref lock was needed. This will make the treatment of
 454           // weak refs more uniform (and indeed remove such concerns
 455           // from GCH). XXX
 456 
 457           HandleMark hm;  // Discard invalid handles created during gc
 458           save_marks();   // save marks for all gens
 459           // We want to discover references, but not process them yet.
 460           // This mode is disabled in process_discovered_references if the
 461           // generation does some collection work, or in
 462           // enqueue_discovered_references if the generation returns
 463           // without doing any work.
 464           ReferenceProcessor* rp = _gens[i]->ref_processor();
 465           // If the discovery of ("weak") refs in this generation is
 466           // atomic wrt other collectors in this configuration, we
 467           // are guaranteed to have empty discovered ref lists.
 468           if (rp->discovery_is_atomic()) {
 469             rp->enable_discovery();
 470             rp->setup_policy(do_clear_all_soft_refs);
 471           } else {
 472             // collect() below will enable discovery as appropriate
 473           }
 474           _gens[i]->collect(full, do_clear_all_soft_refs, size, is_tlab);
 475           if (!rp->enqueuing_is_done()) {
 476             rp->enqueue_discovered_references();
 477           } else {
 478             rp->set_enqueuing_is_done(false);
 479           }
 480           rp->verify_no_references_recorded();
 481         }
 482         max_level_collected = i;
 483 
 484         // Determine if allocation request was met.
 485         if (size > 0) {
 486           if (!is_tlab || _gens[i]->supports_tlab_allocation()) {
 487             if (size*HeapWordSize <= _gens[i]->unsafe_max_alloc_nogc()) {
 488               size = 0;
 489             }
 490           }
 491         }
 492 
 493         COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
 494 
 495         _gens[i]->stat_record()->accumulated_time.stop();
 496 
 497         update_gc_stats(i, full);
 498 
 499         if (VerifyAfterGC && i >= VerifyGCLevel &&
 500             total_collections() >= VerifyGCStartAt) {
 501           HandleMark hm;  // Discard invalid handles created during verification
 502           Universe::verify(" VerifyAfterGC:");
 503         }
 504 
 505         if (PrintGCDetails) {
 506           gclog_or_tty->print(":");
 507           _gens[i]->print_heap_change(prev_used);
 508         }








































































 509       }
 510     }
 511 























 512     // Update "complete" boolean wrt what actually transpired --
 513     // for instance, a promotion failure could have led to
 514     // a whole heap collection.
 515     complete = complete || (max_level_collected == n_gens() - 1);
 516 
 517     if (complete) { // We did a "major" collection
 518       // FIXME: See comment at pre_full_gc_dump call
 519       post_full_gc_dump(NULL);   // do any post full gc dumps
 520     }
 521 
 522     if (PrintGCDetails) {
 523       print_heap_change(gch_prev_used);
 524 
 525       // Print metaspace info for full GC with PrintGCDetails flag.
 526       if (complete) {
 527         MetaspaceAux::print_metaspace_change(metadata_prev_used);
 528       }
 529     }
 530 
 531     for (int j = max_level_collected; j >= 0; j -= 1) {
 532       // Adjust generation sizes.
 533       _gens[j]->compute_new_size();

 534     }

 535 
 536     if (complete) {
 537       // Delete metaspaces for unloaded class loaders and clean up loader_data graph
 538       ClassLoaderDataGraph::purge();
 539       MetaspaceAux::verify_metrics();
 540       // Resize the metaspace capacity after full collections
 541       MetaspaceGC::compute_new_size();
 542       update_full_collections_completed();
 543     }
 544 
 545     // Track memory usage and detect low memory after GC finishes
 546     MemoryService::track_memory_usage();
 547 
 548     gc_epilogue(complete);
 549 
 550     if (must_restore_marks_for_biased_locking) {
 551       BiasedLocking::restore_marks();
 552     }
 553   }
 554 


 571 void GenCollectedHeap::
 572 gen_process_roots(int level,
 573                   bool younger_gens_as_roots,
 574                   bool activate_scope,
 575                   SharedHeap::ScanningOption so,
 576                   OopsInGenClosure* not_older_gens,
 577                   OopsInGenClosure* weak_roots,
 578                   OopsInGenClosure* older_gens,
 579                   CLDClosure* cld_closure,
 580                   CLDClosure* weak_cld_closure,
 581                   CodeBlobClosure* code_closure) {
 582 
 583   // General roots.
 584   SharedHeap::process_roots(activate_scope, so,
 585                             not_older_gens, weak_roots,
 586                             cld_closure, weak_cld_closure,
 587                             code_closure);
 588 
 589   if (younger_gens_as_roots) {
 590     if (!_gen_process_roots_tasks->is_task_claimed(GCH_PS_younger_gens)) {
 591       for (int i = 0; i < level; i++) {
 592         not_older_gens->set_generation(_gens[i]);
 593         _gens[i]->oop_iterate(not_older_gens);
 594       }
 595       not_older_gens->reset_generation();
 596     }
 597   }
 598   // When collection is parallel, all threads get to cooperate to do
 599   // older-gen scanning.
 600   for (int i = level+1; i < _n_gens; i++) {
 601     older_gens->set_generation(_gens[i]);
 602     rem_set()->younger_refs_iterate(_gens[i], older_gens);
 603     older_gens->reset_generation();
 604   }
 605 
 606   _gen_process_roots_tasks->all_tasks_completed();
 607 }
 608 
 609 void GenCollectedHeap::
 610 gen_process_roots(int level,
 611                   bool younger_gens_as_roots,
 612                   bool activate_scope,
 613                   SharedHeap::ScanningOption so,
 614                   bool only_strong_roots,
 615                   OopsInGenClosure* not_older_gens,
 616                   OopsInGenClosure* older_gens,
 617                   CLDClosure* cld_closure) {
 618 
 619   const bool is_adjust_phase = !only_strong_roots && !younger_gens_as_roots;
 620 
 621   bool is_moving_collection = false;
 622   if (level == 0 || is_adjust_phase) {
 623     // young collections are always moving
 624     is_moving_collection = true;
 625   }
 626 
 627   MarkingCodeBlobClosure mark_code_closure(not_older_gens, is_moving_collection);
 628   CodeBlobClosure* code_closure = &mark_code_closure;
 629 
 630   gen_process_roots(level,
 631                     younger_gens_as_roots,
 632                     activate_scope, so,
 633                     not_older_gens, only_strong_roots ? NULL : not_older_gens,
 634                     older_gens,
 635                     cld_closure, only_strong_roots ? NULL : cld_closure,
 636                     code_closure);
 637 
 638 }
 639 
 640 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) {
 641   SharedHeap::process_weak_roots(root_closure);
 642   // "Local" "weak" refs
 643   for (int i = 0; i < _n_gens; i++) {
 644     _gens[i]->ref_processor()->weak_oops_do(root_closure);
 645   }
 646 }
 647 
 648 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix)    \
 649 void GenCollectedHeap::                                                 \
 650 oop_since_save_marks_iterate(int level,                                 \
 651                              OopClosureType* cur,                       \
 652                              OopClosureType* older) {                   \
 653   _gens[level]->oop_since_save_marks_iterate##nv_suffix(cur);           \
 654   for (int i = level+1; i < n_gens(); i++) {                            \
 655     _gens[i]->oop_since_save_marks_iterate##nv_suffix(older);           \


 656   }                                                                     \
 657 }
 658 
 659 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN)
 660 
 661 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN
 662 
 663 bool GenCollectedHeap::no_allocs_since_save_marks(int level) {
 664   for (int i = level; i < _n_gens; i++) {
 665     if (!_gens[i]->no_allocs_since_save_marks()) return false;
 666   }
 667   return true;
 668 }
 669 
 670 bool GenCollectedHeap::supports_inline_contig_alloc() const {
 671   return _gens[0]->supports_inline_contig_alloc();
 672 }
 673 
 674 HeapWord** GenCollectedHeap::top_addr() const {
 675   return _gens[0]->top_addr();
 676 }
 677 
 678 HeapWord** GenCollectedHeap::end_addr() const {
 679   return _gens[0]->end_addr();
 680 }
 681 
 682 // public collection interfaces
 683 
 684 void GenCollectedHeap::collect(GCCause::Cause cause) {
 685   if (should_do_concurrent_full_gc(cause)) {
 686 #if INCLUDE_ALL_GCS
 687     // mostly concurrent full collection
 688     collect_mostly_concurrent(cause);
 689 #else  // INCLUDE_ALL_GCS
 690     ShouldNotReachHere();
 691 #endif // INCLUDE_ALL_GCS
 692   } else if (cause == GCCause::_wb_young_gc) {
 693     // minor collection for WhiteBox API
 694     collect(cause, 0);
 695   } else {
 696 #ifdef ASSERT
 697   if (cause == GCCause::_scavenge_alot) {
 698     // minor collection only
 699     collect(cause, 0);


 722 }
 723 
 724 // this is the private collection interface
 725 // The Heap_lock is expected to be held on entry.
 726 
 727 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) {
 728   // Read the GC count while holding the Heap_lock
 729   unsigned int gc_count_before      = total_collections();
 730   unsigned int full_gc_count_before = total_full_collections();
 731   {
 732     MutexUnlocker mu(Heap_lock);  // give up heap lock, execute gets it back
 733     VM_GenCollectFull op(gc_count_before, full_gc_count_before,
 734                          cause, max_level);
 735     VMThread::execute(&op);
 736   }
 737 }
 738 
 739 #if INCLUDE_ALL_GCS
 740 bool GenCollectedHeap::create_cms_collector() {
 741 
 742   assert(_gens[1]->kind() == Generation::ConcurrentMarkSweep,
 743          "Unexpected generation kinds");
 744   // Skip two header words in the block content verification
 745   NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();)
 746   CMSCollector* collector = new CMSCollector(
 747     (ConcurrentMarkSweepGeneration*)_gens[1],
 748     _rem_set->as_CardTableRS(),
 749     (ConcurrentMarkSweepPolicy*) collector_policy());
 750 
 751   if (collector == NULL || !collector->completed_initialization()) {
 752     if (collector) {
 753       delete collector;  // Be nice in embedded situation
 754     }
 755     vm_shutdown_during_initialization("Could not create CMS collector");
 756     return false;
 757   }
 758   return true;  // success
 759 }
 760 
 761 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) {
 762   assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
 763 
 764   MutexLocker ml(Heap_lock);
 765   // Read the GC counts while holding the Heap_lock
 766   unsigned int full_gc_count_before = total_full_collections();
 767   unsigned int gc_count_before      = total_collections();


 794                 local_max_level      /* max_level */);
 795   // Hack XXX FIX ME !!!
 796   // A scavenge may not have been attempted, or may have
 797   // been attempted and failed, because the old gen was too full
 798   if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker &&
 799       incremental_collection_will_fail(false /* don't consult_young */)) {
 800     if (PrintGCDetails) {
 801       gclog_or_tty->print_cr("GC locker: Trying a full collection "
 802                              "because scavenge failed");
 803     }
 804     // This time allow the old gen to be collected as well
 805     do_collection(true                 /* full */,
 806                   clear_all_soft_refs  /* clear_all_soft_refs */,
 807                   0                    /* size */,
 808                   false                /* is_tlab */,
 809                   n_gens() - 1         /* max_level */);
 810   }
 811 }
 812 
 813 bool GenCollectedHeap::is_in_young(oop p) {
 814   bool result = ((HeapWord*)p) < _gens[_n_gens - 1]->reserved().start();
 815   assert(result == _gens[0]->is_in_reserved(p),
 816          err_msg("incorrect test - result=%d, p=" INTPTR_FORMAT, result, p2i((void*)p)));
 817   return result;
 818 }
 819 
 820 // Returns "TRUE" iff "p" points into the committed areas of the heap.
 821 bool GenCollectedHeap::is_in(const void* p) const {
 822   #ifndef ASSERT
 823   guarantee(VerifyBeforeGC      ||
 824             VerifyDuringGC      ||
 825             VerifyBeforeExit    ||
 826             VerifyDuringStartup ||
 827             PrintAssembly       ||
 828             tty->count() != 0   ||   // already printing
 829             VerifyAfterGC       ||
 830     VMError::fatal_error_in_progress(), "too expensive");
 831 
 832   #endif
 833   // This might be sped up with a cache of the last generation that
 834   // answered yes.
 835   for (int i = 0; i < _n_gens; i++) {
 836     if (_gens[i]->is_in(p)) return true;
 837   }
 838   // Otherwise...
 839   return false;
 840 }
 841 
 842 #ifdef ASSERT
 843 // Don't implement this by using is_in_young().  This method is used
 844 // in some cases to check that is_in_young() is correct.
 845 bool GenCollectedHeap::is_in_partial_collection(const void* p) {
 846   assert(is_in_reserved(p) || p == NULL,
 847     "Does not work if address is non-null and outside of the heap");
 848   return p < _gens[_n_gens - 2]->reserved().end() && p != NULL;
 849 }
 850 #endif
 851 
 852 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) {
 853   for (int i = 0; i < _n_gens; i++) {
 854     _gens[i]->oop_iterate(cl);
 855   }
 856 }
 857 
 858 void GenCollectedHeap::object_iterate(ObjectClosure* cl) {
 859   for (int i = 0; i < _n_gens; i++) {
 860     _gens[i]->object_iterate(cl);
 861   }
 862 }
 863 
 864 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) {
 865   for (int i = 0; i < _n_gens; i++) {
 866     _gens[i]->safe_object_iterate(cl);
 867   }
 868 }
 869 
 870 Space* GenCollectedHeap::space_containing(const void* addr) const {
 871   for (int i = 0; i < _n_gens; i++) {
 872     Space* res = _gens[i]->space_containing(addr);
 873     if (res != NULL) return res;
 874   }
 875   // Otherwise...
 876   assert(false, "Could not find containing space");
 877   return NULL;
 878 }
 879 
 880 
 881 HeapWord* GenCollectedHeap::block_start(const void* addr) const {
 882   assert(is_in_reserved(addr), "block_start of address outside of heap");
 883   for (int i = 0; i < _n_gens; i++) {
 884     if (_gens[i]->is_in_reserved(addr)) {
 885       assert(_gens[i]->is_in(addr),
 886              "addr should be in allocated part of generation");
 887       return _gens[i]->block_start(addr);
 888     }
 889   }
 890   assert(false, "Some generation should contain the address");
 891   return NULL;


 892 }
 893 
 894 size_t GenCollectedHeap::block_size(const HeapWord* addr) const {
 895   assert(is_in_reserved(addr), "block_size of address outside of heap");
 896   for (int i = 0; i < _n_gens; i++) {
 897     if (_gens[i]->is_in_reserved(addr)) {
 898       assert(_gens[i]->is_in(addr),
 899              "addr should be in allocated part of generation");
 900       return _gens[i]->block_size(addr);
 901     }
 902   }
 903   assert(false, "Some generation should contain the address");
 904   return 0;

 905 }
 906 
 907 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const {
 908   assert(is_in_reserved(addr), "block_is_obj of address outside of heap");
 909   assert(block_start(addr) == addr, "addr must be a block start");
 910   for (int i = 0; i < _n_gens; i++) {
 911     if (_gens[i]->is_in_reserved(addr)) {
 912       return _gens[i]->block_is_obj(addr);
 913     }
 914   }
 915   assert(false, "Some generation should contain the address");
 916   return false;

 917 }
 918 
 919 bool GenCollectedHeap::supports_tlab_allocation() const {
 920   for (int i = 0; i < _n_gens; i += 1) {
 921     if (_gens[i]->supports_tlab_allocation()) {
 922       return true;
 923     }
 924   }
 925   return false;
 926 }
 927 
 928 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const {
 929   size_t result = 0;
 930   for (int i = 0; i < _n_gens; i += 1) {
 931     if (_gens[i]->supports_tlab_allocation()) {
 932       result += _gens[i]->tlab_capacity();
 933     }
 934   }
 935   return result;
 936 }
 937 
 938 size_t GenCollectedHeap::tlab_used(Thread* thr) const {
 939   size_t result = 0;
 940   for (int i = 0; i < _n_gens; i += 1) {
 941     if (_gens[i]->supports_tlab_allocation()) {
 942       result += _gens[i]->tlab_used();
 943     }
 944   }
 945   return result;
 946 }
 947 
 948 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const {
 949   size_t result = 0;
 950   for (int i = 0; i < _n_gens; i += 1) {
 951     if (_gens[i]->supports_tlab_allocation()) {
 952       result += _gens[i]->unsafe_max_tlab_alloc();
 953     }
 954   }
 955   return result;
 956 }
 957 
 958 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) {
 959   bool gc_overhead_limit_was_exceeded;
 960   return collector_policy()->mem_allocate_work(size /* size */,
 961                                                true /* is_tlab */,
 962                                                &gc_overhead_limit_was_exceeded);
 963 }
 964 
 965 // Requires "*prev_ptr" to be non-NULL.  Deletes and a block of minimal size
 966 // from the list headed by "*prev_ptr".
 967 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) {
 968   bool first = true;
 969   size_t min_size = 0;   // "first" makes this conceptually infinite.
 970   ScratchBlock **smallest_ptr, *smallest;
 971   ScratchBlock  *cur = *prev_ptr;
 972   while (cur) {
 973     assert(*prev_ptr == cur, "just checking");
 974     if (first || cur->num_words < min_size) {
 975       smallest_ptr = prev_ptr;


 984   *smallest_ptr = smallest->next;
 985   return smallest;
 986 }
 987 
 988 // Sort the scratch block list headed by res into decreasing size order,
 989 // and set "res" to the result.
 990 static void sort_scratch_list(ScratchBlock*& list) {
 991   ScratchBlock* sorted = NULL;
 992   ScratchBlock* unsorted = list;
 993   while (unsorted) {
 994     ScratchBlock *smallest = removeSmallestScratch(&unsorted);
 995     smallest->next  = sorted;
 996     sorted          = smallest;
 997   }
 998   list = sorted;
 999 }
1000 
1001 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor,
1002                                                size_t max_alloc_words) {
1003   ScratchBlock* res = NULL;
1004   for (int i = 0; i < _n_gens; i++) {
1005     _gens[i]->contribute_scratch(res, requestor, max_alloc_words);
1006   }
1007   sort_scratch_list(res);
1008   return res;
1009 }
1010 
1011 void GenCollectedHeap::release_scratch() {
1012   for (int i = 0; i < _n_gens; i++) {
1013     _gens[i]->reset_scratch();
1014   }
1015 }
1016 
1017 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure {
1018   void do_generation(Generation* gen) {
1019     gen->prepare_for_verify();
1020   }
1021 };
1022 
1023 void GenCollectedHeap::prepare_for_verify() {
1024   ensure_parsability(false);        // no need to retire TLABs
1025   GenPrepareForVerifyClosure blk;
1026   generation_iterate(&blk, false);
1027 }
1028 
1029 
1030 void GenCollectedHeap::generation_iterate(GenClosure* cl,
1031                                           bool old_to_young) {
1032   if (old_to_young) {
1033     for (int i = _n_gens-1; i >= 0; i--) {
1034       cl->do_generation(_gens[i]);
1035     }
1036   } else {
1037     for (int i = 0; i < _n_gens; i++) {
1038       cl->do_generation(_gens[i]);
1039     }
1040   }
1041 }
1042 
1043 void GenCollectedHeap::space_iterate(SpaceClosure* cl) {
1044   for (int i = 0; i < _n_gens; i++) {
1045     _gens[i]->space_iterate(cl, true);
1046   }
1047 }
1048 
1049 bool GenCollectedHeap::is_maximal_no_gc() const {
1050   for (int i = 0; i < _n_gens; i++) {
1051     if (!_gens[i]->is_maximal_no_gc()) {
1052       return false;
1053     }
1054   }
1055   return true;
1056 }
1057 
1058 void GenCollectedHeap::save_marks() {
1059   for (int i = 0; i < _n_gens; i++) {
1060     _gens[i]->save_marks();
1061   }
1062 }
1063 
1064 GenCollectedHeap* GenCollectedHeap::heap() {
1065   assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()");
1066   assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap");
1067   return _gch;
1068 }
1069 
1070 
1071 void GenCollectedHeap::prepare_for_compaction() {
1072   guarantee(_n_gens = 2, "Wrong number of generations");
1073   Generation* old_gen = _gens[1];
1074   // Start by compacting into same gen.
1075   CompactPoint cp(old_gen);
1076   old_gen->prepare_for_compaction(&cp);
1077   Generation* young_gen = _gens[0];
1078   young_gen->prepare_for_compaction(&cp);
1079 }
1080 
1081 GCStats* GenCollectedHeap::gc_stats(int level) const {
1082   return _gens[level]->gc_stats();




1083 }
1084 
1085 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) {
1086   for (int i = _n_gens-1; i >= 0; i--) {
1087     Generation* g = _gens[i];
1088     if (!silent) {
1089       gclog_or_tty->print("%s", g->name());
1090       gclog_or_tty->print(" ");
1091     }
1092     g->verify();




1093   }


1094   if (!silent) {
1095     gclog_or_tty->print("remset ");
1096   }
1097   rem_set()->verify();
1098 }
1099 
1100 void GenCollectedHeap::print_on(outputStream* st) const {
1101   for (int i = 0; i < _n_gens; i++) {
1102     _gens[i]->print_on(st);
1103   }
1104   MetaspaceAux::print_on(st);
1105 }
1106 
1107 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
1108   if (workers() != NULL) {
1109     workers()->threads_do(tc);
1110   }
1111 #if INCLUDE_ALL_GCS
1112   if (UseConcMarkSweepGC) {
1113     ConcurrentMarkSweepThread::threads_do(tc);
1114   }
1115 #endif // INCLUDE_ALL_GCS
1116 }
1117 
1118 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const {
1119 #if INCLUDE_ALL_GCS
1120   if (UseConcMarkSweepGC) {
1121     workers()->print_worker_threads_on(st);
1122     ConcurrentMarkSweepThread::print_all_on(st);
1123   }


   1 /*
   2  * Copyright (c) 2000, 2015, 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  *


  70 
  71 GenCollectedHeap::GenCollectedHeap(GenCollectorPolicy *policy) :
  72   SharedHeap(policy),
  73   _rem_set(NULL),
  74   _gen_policy(policy),
  75   _gen_process_roots_tasks(new SubTasksDone(GCH_PS_NumElements)),
  76   _full_collections_completed(0)
  77 {
  78   if (_gen_process_roots_tasks == NULL ||
  79       !_gen_process_roots_tasks->valid()) {
  80     vm_exit_during_initialization("Failed necessary allocation.");
  81   }
  82   assert(policy != NULL, "Sanity check");
  83 }
  84 
  85 jint GenCollectedHeap::initialize() {
  86   CollectedHeap::pre_initialize();
  87 
  88   int i;
  89   _n_gens = gen_policy()->number_of_generations();
  90   assert(_n_gens == 2, "There is no support for more than two generations");
  91 
  92   // While there are no constraints in the GC code that HeapWordSize
  93   // be any particular value, there are multiple other areas in the
  94   // system which believe this to be true (e.g. oop->object_size in some
  95   // cases incorrectly returns the size in wordSize units rather than
  96   // HeapWordSize).
  97   guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
  98 
  99   // The heap must be at least as aligned as generations.
 100   size_t gen_alignment = Generation::GenGrain;
 101 
 102   _gen_specs = gen_policy()->generations();
 103 
 104   // Make sure the sizes are all aligned.
 105   for (i = 0; i < _n_gens; i++) {
 106     _gen_specs[i]->align(gen_alignment);
 107   }
 108 
 109   // Allocate space for the heap.
 110 


 112   size_t total_reserved = 0;
 113   ReservedSpace heap_rs;
 114 
 115   size_t heap_alignment = collector_policy()->heap_alignment();
 116 
 117   heap_address = allocate(heap_alignment, &total_reserved, &heap_rs);
 118 
 119   if (!heap_rs.is_reserved()) {
 120     vm_shutdown_during_initialization(
 121       "Could not reserve enough space for object heap");
 122     return JNI_ENOMEM;
 123   }
 124 
 125   initialize_reserved_region((HeapWord*)heap_rs.base(), (HeapWord*)(heap_rs.base() + heap_rs.size()));
 126 
 127   _rem_set = collector_policy()->create_rem_set(reserved_region());
 128   set_barrier_set(rem_set()->bs());
 129 
 130   _gch = this;
 131 
 132   ReservedSpace young_rs = heap_rs.first_part(_gen_specs[0]->max_size(), false, false);
 133   _young_gen = _gen_specs[0]->init(young_rs, 0, rem_set());
 134   heap_rs = heap_rs.last_part(_gen_specs[0]->max_size());
 135 
 136   ReservedSpace old_rs = heap_rs.first_part(_gen_specs[1]->max_size(), false, false);
 137   _old_gen = _gen_specs[1]->init(old_rs, 1, rem_set());
 138   clear_incremental_collection_failed();
 139 
 140 #if INCLUDE_ALL_GCS
 141   // If we are running CMS, create the collector responsible
 142   // for collecting the CMS generations.
 143   if (collector_policy()->is_concurrent_mark_sweep_policy()) {
 144     bool success = create_cms_collector();
 145     if (!success) return JNI_ENOMEM;
 146   }
 147 #endif // INCLUDE_ALL_GCS
 148 
 149   return JNI_OK;
 150 }
 151 

 152 char* GenCollectedHeap::allocate(size_t alignment,
 153                                  size_t* _total_reserved,
 154                                  ReservedSpace* heap_rs){
 155   const char overflow_msg[] = "The size of the object heap + VM data exceeds "
 156     "the maximum representable size";
 157 
 158   // Now figure out the total size.
 159   size_t total_reserved = 0;
 160   const size_t pageSize = UseLargePages ?
 161       os::large_page_size() : os::vm_page_size();
 162 
 163   assert(alignment % pageSize == 0, "Must be");
 164 
 165   for (int i = 0; i < _n_gens; i++) {
 166     total_reserved += _gen_specs[i]->max_size();
 167     if (total_reserved < _gen_specs[i]->max_size()) {
 168       vm_exit_during_initialization(overflow_msg);
 169     }
 170   }
 171   assert(total_reserved % alignment == 0,
 172          err_msg("Gen size; total_reserved=" SIZE_FORMAT ", alignment="
 173                  SIZE_FORMAT, total_reserved, alignment));
 174 
 175   *_total_reserved = total_reserved;
 176 
 177   *heap_rs = Universe::reserve_heap(total_reserved, alignment);
 178   return heap_rs->base();
 179 }
 180 

 181 void GenCollectedHeap::post_initialize() {
 182   SharedHeap::post_initialize();
 183   GenCollectorPolicy *policy = (GenCollectorPolicy *)collector_policy();
 184   guarantee(policy->is_generation_policy(), "Illegal policy type");
 185   assert((get_gen(0)->kind() == Generation::DefNew) ||
 186          (get_gen(0)->kind() == Generation::ParNew),
 187     "Wrong youngest generation type");
 188   DefNewGeneration* def_new_gen = (DefNewGeneration*)get_gen(0);
 189 
 190   Generation* old_gen = get_gen(1);
 191   assert(old_gen->kind() == Generation::ConcurrentMarkSweep ||
 192          old_gen->kind() == Generation::MarkSweepCompact,
 193     "Wrong generation kind");
 194 
 195   policy->initialize_size_policy(def_new_gen->eden()->capacity(),
 196                                  old_gen->capacity(),
 197                                  def_new_gen->from()->capacity());
 198   policy->initialize_gc_policy_counters();
 199 }
 200 
 201 void GenCollectedHeap::ref_processing_init() {
 202   SharedHeap::ref_processing_init();
 203   _young_gen->ref_processor_init();
 204   _old_gen->ref_processor_init();

 205 }
 206 
 207 size_t GenCollectedHeap::capacity() const {
 208   return _young_gen->capacity() + _old_gen->capacity();




 209 }
 210 
 211 size_t GenCollectedHeap::used() const {
 212   return _young_gen->used() + _old_gen->used();




 213 }
 214 
 215 // Save the "used_region" for generations level and lower.
 216 void GenCollectedHeap::save_used_regions(int level) {
 217   assert(level >= 0, "Illegal level parameter");
 218   assert(level < _n_gens, "Illegal level parameter");
 219   if (level == 1) {
 220     _old_gen->save_used_region();
 221   }
 222   _young_gen->save_used_region();
 223 }
 224 
 225 size_t GenCollectedHeap::max_capacity() const {
 226   return _young_gen->max_capacity() + _old_gen->max_capacity();




 227 }
 228 
 229 // Update the _full_collections_completed counter
 230 // at the end of a stop-world full GC.
 231 unsigned int GenCollectedHeap::update_full_collections_completed() {
 232   MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);
 233   assert(_full_collections_completed <= _total_full_collections,
 234          "Can't complete more collections than were started");
 235   _full_collections_completed = _total_full_collections;
 236   ml.notify_all();
 237   return _full_collections_completed;
 238 }
 239 
 240 // Update the _full_collections_completed counter, as appropriate,
 241 // at the end of a concurrent GC cycle. Note the conditional update
 242 // below to allow this method to be called by a concurrent collector
 243 // without synchronizing in any manner with the VM thread (which
 244 // may already have initiated a STW full collection "concurrently").
 245 unsigned int GenCollectedHeap::update_full_collections_completed(unsigned int count) {
 246   MonitorLockerEx ml(FullGCCount_lock, Mutex::_no_safepoint_check_flag);


 270 // higher than we are prepared to pay for such rudimentary debugging
 271 // support.
 272 void GenCollectedHeap::check_for_non_bad_heap_word_value(HeapWord* addr,
 273                                                          size_t size) {
 274   if (CheckMemoryInitialization && ZapUnusedHeapArea) {
 275     // We are asked to check a size in HeapWords,
 276     // but the memory is mangled in juint words.
 277     juint* start = (juint*) (addr + skip_header_HeapWords());
 278     juint* end   = (juint*) (addr + size);
 279     for (juint* slot = start; slot < end; slot += 1) {
 280       assert(*slot == badHeapWordVal,
 281              "Found non badHeapWordValue in pre-allocation check");
 282     }
 283   }
 284 }
 285 #endif
 286 
 287 HeapWord* GenCollectedHeap::attempt_allocation(size_t size,
 288                                                bool is_tlab,
 289                                                bool first_only) {
 290   HeapWord* res = NULL;
 291 
 292   if (_young_gen->should_allocate(size, is_tlab)) {
 293     res = _young_gen->allocate(size, is_tlab);
 294     if (res != NULL || first_only) {
 295       return res;
 296     }
 297   }
 298 
 299   if (_old_gen->should_allocate(size, is_tlab)) {
 300     res = _old_gen->allocate(size, is_tlab);
 301   }
 302 
 303   return res;
 304 }
 305 
 306 HeapWord* GenCollectedHeap::mem_allocate(size_t size,
 307                                          bool* gc_overhead_limit_was_exceeded) {
 308   return collector_policy()->mem_allocate_work(size,
 309                                                false /* is_tlab */,
 310                                                gc_overhead_limit_was_exceeded);
 311 }
 312 
 313 bool GenCollectedHeap::must_clear_all_soft_refs() {
 314   return _gc_cause == GCCause::_last_ditch_collection;
 315 }
 316 
 317 bool GenCollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
 318   return UseConcMarkSweepGC &&
 319          ((cause == GCCause::_gc_locker && GCLockerInvokesConcurrent) ||
 320           (cause == GCCause::_java_lang_system_gc && ExplicitGCInvokesConcurrent));
 321 }
 322 
 323 void GenCollectedHeap::collect_generation(Generation* gen, bool full, size_t size,
 324                                           bool is_tlab, bool run_verification, bool clear_soft_refs,
 325                                           bool restore_marks_for_biased_locking) {



































































 326   // Timer for individual generations. Last argument is false: no CR
 327   // FIXME: We should try to start the timing earlier to cover more of the GC pause
 328   // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
 329   // so we can assume here that the next GC id is what we want.
 330   GCTraceTime t1(gen->short_name(), PrintGCDetails, false, NULL, GCId::peek());
 331   TraceCollectorStats tcs(gen->counters());
 332   TraceMemoryManagerStats tmms(gen->kind(),gc_cause());
 333 
 334   size_t prev_used = gen->used();
 335   gen->stat_record()->invocations++;
 336   gen->stat_record()->accumulated_time.start();
 337 
 338   // Must be done anew before each collection because
 339   // a previous collection will do mangling and will
 340   // change top of some spaces.
 341   record_gen_tops_before_GC();
 342 
 343   if (PrintGC && Verbose) {
 344     gclog_or_tty->print("level=%d invoke=%d size=" SIZE_FORMAT,
 345                         gen->level(),
 346                         gen->stat_record()->invocations,
 347                         size * HeapWordSize);
 348   }
 349 
 350   if (run_verification && VerifyBeforeGC) {

 351     HandleMark hm;  // Discard invalid handles created during verification




 352     Universe::verify(" VerifyBeforeGC:");
 353   }
 354   COMPILER2_PRESENT(DerivedPointerTable::clear());
 355 
 356   if (restore_marks_for_biased_locking) {

 357     // We perform this mark word preservation work lazily
 358     // because it's only at this point that we know whether we
 359     // absolutely have to do it; we want to avoid doing it for
 360     // scavenge-only collections where it's unnecessary

 361     BiasedLocking::preserve_marks();
 362   }
 363 
 364   // Do collection work
 365   {
 366     // Note on ref discovery: For what appear to be historical reasons,
 367     // GCH enables and disabled (by enqueing) refs discovery.
 368     // In the future this should be moved into the generation's
 369     // collect method so that ref discovery and enqueueing concerns
 370     // are local to a generation. The collect method could return
 371     // an appropriate indication in the case that notification on
 372     // the ref lock was needed. This will make the treatment of
 373     // weak refs more uniform (and indeed remove such concerns
 374     // from GCH). XXX
 375 
 376     HandleMark hm;  // Discard invalid handles created during gc
 377     save_marks();   // save marks for all gens
 378     // We want to discover references, but not process them yet.
 379     // This mode is disabled in process_discovered_references if the
 380     // generation does some collection work, or in
 381     // enqueue_discovered_references if the generation returns
 382     // without doing any work.
 383     ReferenceProcessor* rp = gen->ref_processor();
 384     // If the discovery of ("weak") refs in this generation is
 385     // atomic wrt other collectors in this configuration, we
 386     // are guaranteed to have empty discovered ref lists.
 387     if (rp->discovery_is_atomic()) {
 388       rp->enable_discovery();
 389       rp->setup_policy(clear_soft_refs);
 390     } else {
 391       // collect() below will enable discovery as appropriate
 392     }
 393     gen->collect(full, clear_soft_refs, size, is_tlab);
 394     if (!rp->enqueuing_is_done()) {
 395       rp->enqueue_discovered_references();
 396     } else {
 397       rp->set_enqueuing_is_done(false);
 398     }
 399     rp->verify_no_references_recorded();
 400   }










 401 
 402   COMPILER2_PRESENT(DerivedPointerTable::update_pointers());
 403 
 404   gen->stat_record()->accumulated_time.stop();
 405 
 406   update_gc_stats(gen->level(), full);
 407 
 408   if (run_verification && VerifyAfterGC) {

 409     HandleMark hm;  // Discard invalid handles created during verification
 410     Universe::verify(" VerifyAfterGC:");
 411   }
 412 
 413   if (PrintGCDetails) {
 414     gclog_or_tty->print(":");
 415     gen->print_heap_change(prev_used);
 416   }
 417 }
 418 
 419 void GenCollectedHeap::do_collection(bool   full,
 420                                      bool   clear_all_soft_refs,
 421                                      size_t size,
 422                                      bool   is_tlab,
 423                                      int    max_level) {
 424   ResourceMark rm;
 425   DEBUG_ONLY(Thread* my_thread = Thread::current();)
 426 
 427   assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
 428   assert(my_thread->is_VM_thread() ||
 429          my_thread->is_ConcurrentGC_thread(),
 430          "incorrect thread type capability");
 431   assert(Heap_lock->is_locked(),
 432          "the requesting thread should have the Heap_lock");
 433   guarantee(!is_gc_active(), "collection is not reentrant");
 434   assert(max_level < n_gens(), "sanity check");
 435 
 436   if (GC_locker::check_active_before_gc()) {
 437     return; // GC is disabled (e.g. JNI GetXXXCritical operation)
 438   }
 439 
 440   const bool do_clear_all_soft_refs = clear_all_soft_refs ||
 441                           collector_policy()->should_clear_all_soft_refs();
 442 
 443   ClearedAllSoftRefs casr(do_clear_all_soft_refs, collector_policy());
 444 
 445   const size_t metadata_prev_used = MetaspaceAux::used_bytes();
 446 
 447   print_heap_before_gc();
 448 
 449   {
 450     FlagSetting fl(_is_gc_active, true);
 451 
 452     bool complete = full && (max_level == (n_gens()-1));
 453     const char* gc_cause_prefix = complete ? "Full GC" : "GC";
 454     TraceCPUTime tcpu(PrintGCDetails, true, gclog_or_tty);
 455     // The PrintGCDetails logging starts before we have incremented the GC id. We will do that later
 456     // so we can assume here that the next GC id is what we want.
 457     GCTraceTime t(GCCauseString(gc_cause_prefix, gc_cause()), PrintGCDetails, false, NULL, GCId::peek());
 458 
 459     gc_prologue(complete);
 460     increment_total_collections(complete);
 461 
 462     size_t gch_prev_used = used();
 463     bool must_restore_marks_for_biased_locking = false;
 464     bool run_verification = total_collections() >= VerifyGCStartAt;
 465 
 466     bool prepared_for_verification = false;
 467     int max_level_collected = 0;
 468     if (!(max_level == 1 && full && _old_gen->full_collects_younger_generations()) &&
 469         _young_gen->should_collect(full, size, is_tlab)) {
 470       if (run_verification && VerifyGCLevel <= 0 && VerifyBeforeGC) {
 471         prepare_for_verify();
 472         prepared_for_verification = true;
 473       }
 474       if (_young_gen->performs_in_place_marking()) {
 475         must_restore_marks_for_biased_locking = true;
 476       }
 477       collect_generation(_young_gen,
 478                          full,
 479                          size,
 480                          is_tlab,
 481                          run_verification && VerifyGCLevel <= 0,
 482                          do_clear_all_soft_refs,
 483                          must_restore_marks_for_biased_locking);
 484 
 485       if (size > 0 && (!is_tlab || _young_gen->supports_tlab_allocation()) &&
 486           size * HeapWordSize <= _young_gen->unsafe_max_alloc_nogc()) {
 487         // Allocation request was met by young GC.
 488         size = 0;
 489       }
 490     }
 491 
 492     if (max_level == 1 && _old_gen->should_collect(full, size, is_tlab)) {
 493       if (!complete) {
 494         // The full_collections increment was missed above.
 495         increment_total_full_collections();
 496       }
 497       pre_full_gc_dump(NULL);    // do any pre full gc dumps
 498       if (!prepared_for_verification && run_verification &&
 499           VerifyGCLevel <= 1 && VerifyBeforeGC) {
 500         prepare_for_verify();
 501       }
 502       assert(_old_gen->performs_in_place_marking(), "All old generations do in place marking");
 503       collect_generation(_old_gen,
 504                          full,
 505                          size,
 506                          is_tlab,
 507                          run_verification && VerifyGCLevel <= 1,
 508                          do_clear_all_soft_refs,
 509                          !must_restore_marks_for_biased_locking);
 510 
 511       must_restore_marks_for_biased_locking = true;
 512       max_level_collected = 1;
 513     }
 514 
 515     // Update "complete" boolean wrt what actually transpired --
 516     // for instance, a promotion failure could have led to
 517     // a whole heap collection.
 518     complete = complete || (max_level_collected == n_gens() - 1);
 519 
 520     if (complete) { // We did a "major" collection
 521       // FIXME: See comment at pre_full_gc_dump call
 522       post_full_gc_dump(NULL);   // do any post full gc dumps
 523     }
 524 
 525     if (PrintGCDetails) {
 526       print_heap_change(gch_prev_used);
 527 
 528       // Print metaspace info for full GC with PrintGCDetails flag.
 529       if (complete) {
 530         MetaspaceAux::print_metaspace_change(metadata_prev_used);
 531       }
 532     }
 533 

 534     // Adjust generation sizes.
 535     if (max_level_collected == 1) {
 536       _old_gen->compute_new_size();
 537     }
 538     _young_gen->compute_new_size();
 539 
 540     if (complete) {
 541       // Delete metaspaces for unloaded class loaders and clean up loader_data graph
 542       ClassLoaderDataGraph::purge();
 543       MetaspaceAux::verify_metrics();
 544       // Resize the metaspace capacity after full collections
 545       MetaspaceGC::compute_new_size();
 546       update_full_collections_completed();
 547     }
 548 
 549     // Track memory usage and detect low memory after GC finishes
 550     MemoryService::track_memory_usage();
 551 
 552     gc_epilogue(complete);
 553 
 554     if (must_restore_marks_for_biased_locking) {
 555       BiasedLocking::restore_marks();
 556     }
 557   }
 558 


 575 void GenCollectedHeap::
 576 gen_process_roots(int level,
 577                   bool younger_gens_as_roots,
 578                   bool activate_scope,
 579                   SharedHeap::ScanningOption so,
 580                   OopsInGenClosure* not_older_gens,
 581                   OopsInGenClosure* weak_roots,
 582                   OopsInGenClosure* older_gens,
 583                   CLDClosure* cld_closure,
 584                   CLDClosure* weak_cld_closure,
 585                   CodeBlobClosure* code_closure) {
 586 
 587   // General roots.
 588   SharedHeap::process_roots(activate_scope, so,
 589                             not_older_gens, weak_roots,
 590                             cld_closure, weak_cld_closure,
 591                             code_closure);
 592 
 593   if (younger_gens_as_roots) {
 594     if (!_gen_process_roots_tasks->is_task_claimed(GCH_PS_younger_gens)) {
 595       if (level == 1) {
 596         not_older_gens->set_generation(_young_gen);
 597         _young_gen->oop_iterate(not_older_gens);
 598       }
 599       not_older_gens->reset_generation();
 600     }
 601   }
 602   // When collection is parallel, all threads get to cooperate to do
 603   // older-gen scanning.
 604   if (level == 0) {
 605     older_gens->set_generation(_old_gen);
 606     rem_set()->younger_refs_iterate(_old_gen, older_gens);
 607     older_gens->reset_generation();
 608   }
 609 
 610   _gen_process_roots_tasks->all_tasks_completed();
 611 }
 612 
 613 void GenCollectedHeap::
 614 gen_process_roots(int level,
 615                   bool younger_gens_as_roots,
 616                   bool activate_scope,
 617                   SharedHeap::ScanningOption so,
 618                   bool only_strong_roots,
 619                   OopsInGenClosure* not_older_gens,
 620                   OopsInGenClosure* older_gens,
 621                   CLDClosure* cld_closure) {
 622 
 623   const bool is_adjust_phase = !only_strong_roots && !younger_gens_as_roots;
 624 
 625   bool is_moving_collection = false;
 626   if (level == 0 || is_adjust_phase) {
 627     // young collections are always moving
 628     is_moving_collection = true;
 629   }
 630 
 631   MarkingCodeBlobClosure mark_code_closure(not_older_gens, is_moving_collection);
 632   CodeBlobClosure* code_closure = &mark_code_closure;
 633 
 634   gen_process_roots(level,
 635                     younger_gens_as_roots,
 636                     activate_scope, so,
 637                     not_older_gens, only_strong_roots ? NULL : not_older_gens,
 638                     older_gens,
 639                     cld_closure, only_strong_roots ? NULL : cld_closure,
 640                     code_closure);
 641 
 642 }
 643 
 644 void GenCollectedHeap::gen_process_weak_roots(OopClosure* root_closure) {
 645   SharedHeap::process_weak_roots(root_closure);
 646   // "Local" "weak" refs
 647   _young_gen->ref_processor()->weak_oops_do(root_closure);
 648   _old_gen->ref_processor()->weak_oops_do(root_closure);

 649 }
 650 
 651 #define GCH_SINCE_SAVE_MARKS_ITERATE_DEFN(OopClosureType, nv_suffix)    \
 652 void GenCollectedHeap::                                                 \
 653 oop_since_save_marks_iterate(int level,                                 \
 654                              OopClosureType* cur,                       \
 655                              OopClosureType* older) {                   \
 656   if (level == 0) {                                                     \
 657     _young_gen->oop_since_save_marks_iterate##nv_suffix(cur);           \
 658     _old_gen->oop_since_save_marks_iterate##nv_suffix(older);           \
 659   } else {                                                              \
 660     _old_gen->oop_since_save_marks_iterate##nv_suffix(cur);             \
 661   }                                                                     \
 662 }
 663 
 664 ALL_SINCE_SAVE_MARKS_CLOSURES(GCH_SINCE_SAVE_MARKS_ITERATE_DEFN)
 665 
 666 #undef GCH_SINCE_SAVE_MARKS_ITERATE_DEFN
 667 
 668 bool GenCollectedHeap::no_allocs_since_save_marks(int level) {
 669   if (level == 0 && !_young_gen->no_allocs_since_save_marks()) {
 670     return false;
 671   }
 672   return _old_gen->no_allocs_since_save_marks();
 673 }
 674 
 675 bool GenCollectedHeap::supports_inline_contig_alloc() const {
 676   return _young_gen->supports_inline_contig_alloc();
 677 }
 678 
 679 HeapWord** GenCollectedHeap::top_addr() const {
 680   return _young_gen->top_addr();
 681 }
 682 
 683 HeapWord** GenCollectedHeap::end_addr() const {
 684   return _young_gen->end_addr();
 685 }
 686 
 687 // public collection interfaces
 688 
 689 void GenCollectedHeap::collect(GCCause::Cause cause) {
 690   if (should_do_concurrent_full_gc(cause)) {
 691 #if INCLUDE_ALL_GCS
 692     // mostly concurrent full collection
 693     collect_mostly_concurrent(cause);
 694 #else  // INCLUDE_ALL_GCS
 695     ShouldNotReachHere();
 696 #endif // INCLUDE_ALL_GCS
 697   } else if (cause == GCCause::_wb_young_gc) {
 698     // minor collection for WhiteBox API
 699     collect(cause, 0);
 700   } else {
 701 #ifdef ASSERT
 702   if (cause == GCCause::_scavenge_alot) {
 703     // minor collection only
 704     collect(cause, 0);


 727 }
 728 
 729 // this is the private collection interface
 730 // The Heap_lock is expected to be held on entry.
 731 
 732 void GenCollectedHeap::collect_locked(GCCause::Cause cause, int max_level) {
 733   // Read the GC count while holding the Heap_lock
 734   unsigned int gc_count_before      = total_collections();
 735   unsigned int full_gc_count_before = total_full_collections();
 736   {
 737     MutexUnlocker mu(Heap_lock);  // give up heap lock, execute gets it back
 738     VM_GenCollectFull op(gc_count_before, full_gc_count_before,
 739                          cause, max_level);
 740     VMThread::execute(&op);
 741   }
 742 }
 743 
 744 #if INCLUDE_ALL_GCS
 745 bool GenCollectedHeap::create_cms_collector() {
 746 
 747   assert(_old_gen->kind() == Generation::ConcurrentMarkSweep,
 748          "Unexpected generation kinds");
 749   // Skip two header words in the block content verification
 750   NOT_PRODUCT(_skip_header_HeapWords = CMSCollector::skip_header_HeapWords();)
 751   CMSCollector* collector = new CMSCollector(
 752     (ConcurrentMarkSweepGeneration*)_old_gen,
 753     _rem_set->as_CardTableRS(),
 754     (ConcurrentMarkSweepPolicy*) collector_policy());
 755 
 756   if (collector == NULL || !collector->completed_initialization()) {
 757     if (collector) {
 758       delete collector;  // Be nice in embedded situation
 759     }
 760     vm_shutdown_during_initialization("Could not create CMS collector");
 761     return false;
 762   }
 763   return true;  // success
 764 }
 765 
 766 void GenCollectedHeap::collect_mostly_concurrent(GCCause::Cause cause) {
 767   assert(!Heap_lock->owned_by_self(), "Should not own Heap_lock");
 768 
 769   MutexLocker ml(Heap_lock);
 770   // Read the GC counts while holding the Heap_lock
 771   unsigned int full_gc_count_before = total_full_collections();
 772   unsigned int gc_count_before      = total_collections();


 799                 local_max_level      /* max_level */);
 800   // Hack XXX FIX ME !!!
 801   // A scavenge may not have been attempted, or may have
 802   // been attempted and failed, because the old gen was too full
 803   if (local_max_level == 0 && gc_cause() == GCCause::_gc_locker &&
 804       incremental_collection_will_fail(false /* don't consult_young */)) {
 805     if (PrintGCDetails) {
 806       gclog_or_tty->print_cr("GC locker: Trying a full collection "
 807                              "because scavenge failed");
 808     }
 809     // This time allow the old gen to be collected as well
 810     do_collection(true                 /* full */,
 811                   clear_all_soft_refs  /* clear_all_soft_refs */,
 812                   0                    /* size */,
 813                   false                /* is_tlab */,
 814                   n_gens() - 1         /* max_level */);
 815   }
 816 }
 817 
 818 bool GenCollectedHeap::is_in_young(oop p) {
 819   bool result = ((HeapWord*)p) < _old_gen->reserved().start();
 820   assert(result == _young_gen->is_in_reserved(p),
 821          err_msg("incorrect test - result=%d, p=" INTPTR_FORMAT, result, p2i((void*)p)));
 822   return result;
 823 }
 824 
 825 // Returns "TRUE" iff "p" points into the committed areas of the heap.
 826 bool GenCollectedHeap::is_in(const void* p) const {
 827   #ifndef ASSERT
 828   guarantee(VerifyBeforeGC      ||
 829             VerifyDuringGC      ||
 830             VerifyBeforeExit    ||
 831             VerifyDuringStartup ||
 832             PrintAssembly       ||
 833             tty->count() != 0   ||   // already printing
 834             VerifyAfterGC       ||
 835     VMError::fatal_error_in_progress(), "too expensive");
 836 
 837   #endif
 838   return _young_gen->is_in(p) || _old_gen->is_in(p);






 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 < _young_gen->reserved().end() && p != NULL;
 848 }
 849 #endif
 850 
 851 void GenCollectedHeap::oop_iterate(ExtendedOopClosure* cl) {
 852   _young_gen->oop_iterate(cl);
 853   _old_gen->oop_iterate(cl);

 854 }
 855 
 856 void GenCollectedHeap::object_iterate(ObjectClosure* cl) {
 857   _young_gen->object_iterate(cl);
 858   _old_gen->object_iterate(cl);

 859 }
 860 
 861 void GenCollectedHeap::safe_object_iterate(ObjectClosure* cl) {
 862   _young_gen->safe_object_iterate(cl);
 863   _old_gen->safe_object_iterate(cl);

 864 }
 865 
 866 Space* GenCollectedHeap::space_containing(const void* addr) const {
 867   Space* res = _young_gen->space_containing(addr);
 868   if (res != NULL) {
 869     return res;
 870   }
 871   res = _old_gen->space_containing(addr);
 872   assert(res != NULL, "Could not find containing space");
 873   return res;
 874 }
 875 

 876 HeapWord* GenCollectedHeap::block_start(const void* addr) const {
 877   assert(is_in_reserved(addr), "block_start of address outside of heap");
 878   if (_young_gen->is_in_reserved(addr)) {
 879     assert(_young_gen->is_in(addr), "addr should be in allocated part of generation");
 880     return _young_gen->block_start(addr);



 881   }
 882 
 883   assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address");
 884   assert(_old_gen->is_in(addr), "addr should be in allocated part of generation");
 885   return _old_gen->block_start(addr);
 886 }
 887 
 888 size_t GenCollectedHeap::block_size(const HeapWord* addr) const {
 889   assert(is_in_reserved(addr), "block_size of address outside of heap");
 890   if (_young_gen->is_in_reserved(addr)) {
 891     assert(_young_gen->is_in(addr), "addr should be in allocated part of generation");
 892     return _young_gen->block_size(addr);


 893   }
 894 
 895   assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address");
 896   assert(_old_gen->is_in(addr), "addr should be in allocated part of generation");
 897   return _old_gen->block_size(addr);
 898 }
 899 
 900 bool GenCollectedHeap::block_is_obj(const HeapWord* addr) const {
 901   assert(is_in_reserved(addr), "block_is_obj of address outside of heap");
 902   assert(block_start(addr) == addr, "addr must be a block start");
 903   if (_young_gen->is_in_reserved(addr)) {
 904     return _young_gen->block_is_obj(addr);


 905   }
 906 
 907   assert(_old_gen->is_in_reserved(addr), "Some generation should contain the address");
 908   return _old_gen->block_is_obj(addr);
 909 }
 910 
 911 bool GenCollectedHeap::supports_tlab_allocation() const {
 912   assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!");
 913   return _young_gen->supports_tlab_allocation();




 914 }
 915 
 916 size_t GenCollectedHeap::tlab_capacity(Thread* thr) const {
 917   assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!");
 918   if (_young_gen->supports_tlab_allocation()) {
 919     return _young_gen->tlab_capacity();

 920   }
 921   return 0;

 922 }
 923 
 924 size_t GenCollectedHeap::tlab_used(Thread* thr) const {
 925   assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!");
 926   if (_young_gen->supports_tlab_allocation()) {
 927     return _young_gen->tlab_used();

 928   }
 929   return 0;

 930 }
 931 
 932 size_t GenCollectedHeap::unsafe_max_tlab_alloc(Thread* thr) const {
 933   assert(!_old_gen->supports_tlab_allocation(), "Old gen supports TLAB allocation?!");
 934   if (_young_gen->supports_tlab_allocation()) {
 935     return _young_gen->unsafe_max_tlab_alloc();

 936   }
 937   return 0;

 938 }
 939 
 940 HeapWord* GenCollectedHeap::allocate_new_tlab(size_t size) {
 941   bool gc_overhead_limit_was_exceeded;
 942   return collector_policy()->mem_allocate_work(size /* size */,
 943                                                true /* is_tlab */,
 944                                                &gc_overhead_limit_was_exceeded);
 945 }
 946 
 947 // Requires "*prev_ptr" to be non-NULL.  Deletes and a block of minimal size
 948 // from the list headed by "*prev_ptr".
 949 static ScratchBlock *removeSmallestScratch(ScratchBlock **prev_ptr) {
 950   bool first = true;
 951   size_t min_size = 0;   // "first" makes this conceptually infinite.
 952   ScratchBlock **smallest_ptr, *smallest;
 953   ScratchBlock  *cur = *prev_ptr;
 954   while (cur) {
 955     assert(*prev_ptr == cur, "just checking");
 956     if (first || cur->num_words < min_size) {
 957       smallest_ptr = prev_ptr;


 966   *smallest_ptr = smallest->next;
 967   return smallest;
 968 }
 969 
 970 // Sort the scratch block list headed by res into decreasing size order,
 971 // and set "res" to the result.
 972 static void sort_scratch_list(ScratchBlock*& list) {
 973   ScratchBlock* sorted = NULL;
 974   ScratchBlock* unsorted = list;
 975   while (unsorted) {
 976     ScratchBlock *smallest = removeSmallestScratch(&unsorted);
 977     smallest->next  = sorted;
 978     sorted          = smallest;
 979   }
 980   list = sorted;
 981 }
 982 
 983 ScratchBlock* GenCollectedHeap::gather_scratch(Generation* requestor,
 984                                                size_t max_alloc_words) {
 985   ScratchBlock* res = NULL;
 986   _young_gen->contribute_scratch(res, requestor, max_alloc_words);
 987   _old_gen->contribute_scratch(res, requestor, max_alloc_words);

 988   sort_scratch_list(res);
 989   return res;
 990 }
 991 
 992 void GenCollectedHeap::release_scratch() {
 993   _young_gen->reset_scratch();
 994   _old_gen->reset_scratch();

 995 }
 996 
 997 class GenPrepareForVerifyClosure: public GenCollectedHeap::GenClosure {
 998   void do_generation(Generation* gen) {
 999     gen->prepare_for_verify();
1000   }
1001 };
1002 
1003 void GenCollectedHeap::prepare_for_verify() {
1004   ensure_parsability(false);        // no need to retire TLABs
1005   GenPrepareForVerifyClosure blk;
1006   generation_iterate(&blk, false);
1007 }
1008 

1009 void GenCollectedHeap::generation_iterate(GenClosure* cl,
1010                                           bool old_to_young) {
1011   if (old_to_young) {
1012     cl->do_generation(_old_gen);
1013     cl->do_generation(_young_gen);

1014   } else {
1015     cl->do_generation(_young_gen);
1016     cl->do_generation(_old_gen);

1017   }
1018 }
1019 
1020 void GenCollectedHeap::space_iterate(SpaceClosure* cl) {
1021   _young_gen->space_iterate(cl, true);
1022   _old_gen->space_iterate(cl, true);

1023 }
1024 
1025 bool GenCollectedHeap::is_maximal_no_gc() const {
1026   return _young_gen->is_maximal_no_gc() && _old_gen->is_maximal_no_gc();





1027 }
1028 
1029 void GenCollectedHeap::save_marks() {
1030   _young_gen->save_marks();
1031   _old_gen->save_marks();

1032 }
1033 
1034 GenCollectedHeap* GenCollectedHeap::heap() {
1035   assert(_gch != NULL, "Uninitialized access to GenCollectedHeap::heap()");
1036   assert(_gch->kind() == CollectedHeap::GenCollectedHeap, "not a generational heap");
1037   return _gch;
1038 }
1039 
1040 
1041 void GenCollectedHeap::prepare_for_compaction() {
1042   guarantee(_n_gens = 2, "Wrong number of generations");

1043   // Start by compacting into same gen.
1044   CompactPoint cp(_old_gen);
1045   _old_gen->prepare_for_compaction(&cp);
1046   _young_gen->prepare_for_compaction(&cp);

1047 }
1048 
1049 GCStats* GenCollectedHeap::gc_stats(int level) const {
1050   if (level == 0) {
1051     return _young_gen->gc_stats();
1052   } else {
1053     return _old_gen->gc_stats();
1054   }
1055 }
1056 
1057 void GenCollectedHeap::verify(bool silent, VerifyOption option /* ignored */) {


1058   if (!silent) {
1059     gclog_or_tty->print("%s", _old_gen->name());
1060     gclog_or_tty->print(" ");
1061   }
1062   _old_gen->verify();
1063 
1064   if (!silent) {
1065     gclog_or_tty->print("%s", _young_gen->name());
1066     gclog_or_tty->print(" ");
1067   }
1068   _young_gen->verify();
1069 
1070   if (!silent) {
1071     gclog_or_tty->print("remset ");
1072   }
1073   rem_set()->verify();
1074 }
1075 
1076 void GenCollectedHeap::print_on(outputStream* st) const {
1077   _young_gen->print_on(st);
1078   _old_gen->print_on(st);

1079   MetaspaceAux::print_on(st);
1080 }
1081 
1082 void GenCollectedHeap::gc_threads_do(ThreadClosure* tc) const {
1083   if (workers() != NULL) {
1084     workers()->threads_do(tc);
1085   }
1086 #if INCLUDE_ALL_GCS
1087   if (UseConcMarkSweepGC) {
1088     ConcurrentMarkSweepThread::threads_do(tc);
1089   }
1090 #endif // INCLUDE_ALL_GCS
1091 }
1092 
1093 void GenCollectedHeap::print_gc_threads_on(outputStream* st) const {
1094 #if INCLUDE_ALL_GCS
1095   if (UseConcMarkSweepGC) {
1096     workers()->print_worker_threads_on(st);
1097     ConcurrentMarkSweepThread::print_all_on(st);
1098   }


< prev index next >