1 /*
   2  * Copyright (c) 2001, 2021, 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/classLoaderDataGraph.hpp"
  27 #include "classfile/metadataOnStackMark.hpp"
  28 #include "classfile/stringTable.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "compiler/oopMap.hpp"
  32 #include "gc/g1/g1Allocator.inline.hpp"
  33 #include "gc/g1/g1Arguments.hpp"
  34 #include "gc/g1/g1BarrierSet.hpp"
  35 #include "gc/g1/g1BatchedTask.hpp"
  36 #include "gc/g1/g1CollectedHeap.inline.hpp"
  37 #include "gc/g1/g1CollectionSet.hpp"
  38 #include "gc/g1/g1CollectionSetCandidates.hpp"
  39 #include "gc/g1/g1CollectorState.hpp"
  40 #include "gc/g1/g1ConcurrentRefine.hpp"
  41 #include "gc/g1/g1ConcurrentRefineThread.hpp"
  42 #include "gc/g1/g1ConcurrentMarkThread.inline.hpp"
  43 #include "gc/g1/g1DirtyCardQueue.hpp"
  44 #include "gc/g1/g1EvacStats.inline.hpp"
  45 #include "gc/g1/g1FullCollector.hpp"
  46 #include "gc/g1/g1GCCounters.hpp"
  47 #include "gc/g1/g1GCParPhaseTimesTracker.hpp"
  48 #include "gc/g1/g1GCPhaseTimes.hpp"
  49 #include "gc/g1/g1GCPauseType.hpp"
  50 #include "gc/g1/g1HeapSizingPolicy.hpp"
  51 #include "gc/g1/g1HeapTransition.hpp"
  52 #include "gc/g1/g1HeapVerifier.hpp"
  53 #include "gc/g1/g1HotCardCache.hpp"
  54 #include "gc/g1/g1InitLogger.hpp"
  55 #include "gc/g1/g1MemoryPool.hpp"
  56 #include "gc/g1/g1OopClosures.inline.hpp"
  57 #include "gc/g1/g1ParallelCleaning.hpp"
  58 #include "gc/g1/g1ParScanThreadState.inline.hpp"
  59 #include "gc/g1/g1PeriodicGCTask.hpp"
  60 #include "gc/g1/g1Policy.hpp"
  61 #include "gc/g1/g1RedirtyCardsQueue.hpp"
  62 #include "gc/g1/g1RegionToSpaceMapper.hpp"
  63 #include "gc/g1/g1RemSet.hpp"
  64 #include "gc/g1/g1RootClosures.hpp"
  65 #include "gc/g1/g1RootProcessor.hpp"
  66 #include "gc/g1/g1SATBMarkQueueSet.hpp"
  67 #include "gc/g1/g1SegmentedArrayFreeMemoryTask.hpp"
  68 #include "gc/g1/g1ServiceThread.hpp"
  69 #include "gc/g1/g1ThreadLocalData.hpp"
  70 #include "gc/g1/g1Trace.hpp"
  71 #include "gc/g1/g1UncommitRegionTask.hpp"
  72 #include "gc/g1/g1VMOperations.hpp"
  73 #include "gc/g1/g1YoungCollector.hpp"
  74 #include "gc/g1/g1YoungGCEvacFailureInjector.hpp"
  75 #include "gc/g1/heapRegion.inline.hpp"
  76 #include "gc/g1/heapRegionRemSet.inline.hpp"
  77 #include "gc/g1/heapRegionSet.inline.hpp"
  78 #include "gc/shared/concurrentGCBreakpoints.hpp"
  79 #include "gc/shared/gcBehaviours.hpp"
  80 #include "gc/shared/gcHeapSummary.hpp"
  81 #include "gc/shared/gcId.hpp"
  82 #include "gc/shared/gcLocker.hpp"
  83 #include "gc/shared/gcTimer.hpp"
  84 #include "gc/shared/gcTraceTime.inline.hpp"
  85 #include "gc/shared/generationSpec.hpp"
  86 #include "gc/shared/isGCActiveMark.hpp"
  87 #include "gc/shared/locationPrinter.inline.hpp"
  88 #include "gc/shared/oopStorageParState.hpp"
  89 #include "gc/shared/preservedMarks.inline.hpp"
  90 #include "gc/shared/referenceProcessor.inline.hpp"
  91 #include "gc/shared/suspendibleThreadSet.hpp"
  92 #include "gc/shared/taskqueue.inline.hpp"
  93 #include "gc/shared/taskTerminator.hpp"
  94 #include "gc/shared/tlab_globals.hpp"
  95 #include "gc/shared/workerPolicy.hpp"
  96 #include "gc/shared/weakProcessor.inline.hpp"
  97 #include "logging/log.hpp"
  98 #include "memory/allocation.hpp"
  99 #include "memory/heapInspection.hpp"
 100 #include "memory/iterator.hpp"
 101 #include "memory/metaspaceUtils.hpp"
 102 #include "memory/resourceArea.hpp"
 103 #include "memory/universe.hpp"
 104 #include "oops/access.inline.hpp"
 105 #include "oops/compressedOops.inline.hpp"
 106 #include "oops/oop.inline.hpp"
 107 #include "runtime/atomic.hpp"
 108 #include "runtime/handles.inline.hpp"
 109 #include "runtime/init.hpp"
 110 #include "runtime/java.hpp"
 111 #include "runtime/orderAccess.hpp"
 112 #include "runtime/threadSMR.hpp"
 113 #include "runtime/vmThread.hpp"
 114 #include "utilities/align.hpp"
 115 #include "utilities/autoRestore.hpp"
 116 #include "utilities/bitMap.inline.hpp"
 117 #include "utilities/globalDefinitions.hpp"
 118 #include "utilities/stack.inline.hpp"
 119 
 120 size_t G1CollectedHeap::_humongous_object_threshold_in_words = 0;
 121 
 122 // INVARIANTS/NOTES
 123 //
 124 // All allocation activity covered by the G1CollectedHeap interface is
 125 // serialized by acquiring the HeapLock.  This happens in mem_allocate
 126 // and allocate_new_tlab, which are the "entry" points to the
 127 // allocation code from the rest of the JVM.  (Note that this does not
 128 // apply to TLAB allocation, which is not part of this interface: it
 129 // is done by clients of this interface.)
 130 
 131 void G1RegionMappingChangedListener::reset_from_card_cache(uint start_idx, size_t num_regions) {
 132   HeapRegionRemSet::invalidate_from_card_cache(start_idx, num_regions);
 133 }
 134 
 135 void G1RegionMappingChangedListener::on_commit(uint start_idx, size_t num_regions, bool zero_filled) {
 136   // The from card cache is not the memory that is actually committed. So we cannot
 137   // take advantage of the zero_filled parameter.
 138   reset_from_card_cache(start_idx, num_regions);
 139 }
 140 
 141 void G1CollectedHeap::run_batch_task(G1BatchedTask* cl) {
 142   uint num_workers = MAX2(1u, MIN2(cl->num_workers_estimate(), workers()->active_workers()));
 143   cl->set_max_workers(num_workers);
 144   workers()->run_task(cl, num_workers);
 145 }
 146 
 147 HeapRegion* G1CollectedHeap::new_heap_region(uint hrs_index,
 148                                              MemRegion mr) {
 149   return new HeapRegion(hrs_index, bot(), mr, &_card_set_config);
 150 }
 151 
 152 // Private methods.
 153 
 154 HeapRegion* G1CollectedHeap::new_region(size_t word_size,
 155                                         HeapRegionType type,
 156                                         bool do_expand,
 157                                         uint node_index) {
 158   assert(!is_humongous(word_size) || word_size <= HeapRegion::GrainWords,
 159          "the only time we use this to allocate a humongous region is "
 160          "when we are allocating a single humongous region");
 161 
 162   HeapRegion* res = _hrm.allocate_free_region(type, node_index);
 163 
 164   if (res == NULL && do_expand) {
 165     // Currently, only attempts to allocate GC alloc regions set
 166     // do_expand to true. So, we should only reach here during a
 167     // safepoint.
 168     assert(SafepointSynchronize::is_at_safepoint(), "invariant");
 169 
 170     log_debug(gc, ergo, heap)("Attempt heap expansion (region allocation request failed). Allocation request: " SIZE_FORMAT "B",
 171                               word_size * HeapWordSize);
 172 
 173     assert(word_size * HeapWordSize < HeapRegion::GrainBytes,
 174            "This kind of expansion should never be more than one region. Size: " SIZE_FORMAT,
 175            word_size * HeapWordSize);
 176     if (expand_single_region(node_index)) {
 177       // Given that expand_single_region() succeeded in expanding the heap, and we
 178       // always expand the heap by an amount aligned to the heap
 179       // region size, the free list should in theory not be empty.
 180       // In either case allocate_free_region() will check for NULL.
 181       res = _hrm.allocate_free_region(type, node_index);
 182     }
 183   }
 184   return res;
 185 }
 186 
 187 HeapWord*
 188 G1CollectedHeap::humongous_obj_allocate_initialize_regions(HeapRegion* first_hr,
 189                                                            uint num_regions,
 190                                                            size_t word_size) {
 191   assert(first_hr != NULL, "pre-condition");
 192   assert(is_humongous(word_size), "word_size should be humongous");
 193   assert(num_regions * HeapRegion::GrainWords >= word_size, "pre-condition");
 194 
 195   // Index of last region in the series.
 196   uint first = first_hr->hrm_index();
 197   uint last = first + num_regions - 1;
 198 
 199   // We need to initialize the region(s) we just discovered. This is
 200   // a bit tricky given that it can happen concurrently with
 201   // refinement threads refining cards on these regions and
 202   // potentially wanting to refine the BOT as they are scanning
 203   // those cards (this can happen shortly after a cleanup; see CR
 204   // 6991377). So we have to set up the region(s) carefully and in
 205   // a specific order.
 206 
 207   // The word size sum of all the regions we will allocate.
 208   size_t word_size_sum = (size_t) num_regions * HeapRegion::GrainWords;
 209   assert(word_size <= word_size_sum, "sanity");
 210 
 211   // The passed in hr will be the "starts humongous" region. The header
 212   // of the new object will be placed at the bottom of this region.
 213   HeapWord* new_obj = first_hr->bottom();
 214   // This will be the new top of the new object.
 215   HeapWord* obj_top = new_obj + word_size;
 216 
 217   // First, we need to zero the header of the space that we will be
 218   // allocating. When we update top further down, some refinement
 219   // threads might try to scan the region. By zeroing the header we
 220   // ensure that any thread that will try to scan the region will
 221   // come across the zero klass word and bail out.
 222   //
 223   // NOTE: It would not have been correct to have used
 224   // CollectedHeap::fill_with_object() and make the space look like
 225   // an int array. The thread that is doing the allocation will
 226   // later update the object header to a potentially different array
 227   // type and, for a very short period of time, the klass and length
 228   // fields will be inconsistent. This could cause a refinement
 229   // thread to calculate the object size incorrectly.
 230   Copy::fill_to_words(new_obj, oopDesc::header_size(), 0);
 231 
 232   // Next, pad out the unused tail of the last region with filler
 233   // objects, for improved usage accounting.
 234   // How many words we use for filler objects.
 235   size_t word_fill_size = word_size_sum - word_size;
 236 
 237   // How many words memory we "waste" which cannot hold a filler object.
 238   size_t words_not_fillable = 0;
 239 
 240   if (word_fill_size >= min_fill_size()) {
 241     fill_with_objects(obj_top, word_fill_size);
 242   } else if (word_fill_size > 0) {
 243     // We have space to fill, but we cannot fit an object there.
 244     words_not_fillable = word_fill_size;
 245     word_fill_size = 0;
 246   }
 247 
 248   // We will set up the first region as "starts humongous". This
 249   // will also update the BOT covering all the regions to reflect
 250   // that there is a single object that starts at the bottom of the
 251   // first region.
 252   first_hr->set_starts_humongous(obj_top, word_fill_size);
 253   _policy->remset_tracker()->update_at_allocate(first_hr);
 254   // Then, if there are any, we will set up the "continues
 255   // humongous" regions.
 256   HeapRegion* hr = NULL;
 257   for (uint i = first + 1; i <= last; ++i) {
 258     hr = region_at(i);
 259     hr->set_continues_humongous(first_hr);
 260     _policy->remset_tracker()->update_at_allocate(hr);
 261   }
 262 
 263   // Up to this point no concurrent thread would have been able to
 264   // do any scanning on any region in this series. All the top
 265   // fields still point to bottom, so the intersection between
 266   // [bottom,top] and [card_start,card_end] will be empty. Before we
 267   // update the top fields, we'll do a storestore to make sure that
 268   // no thread sees the update to top before the zeroing of the
 269   // object header and the BOT initialization.
 270   OrderAccess::storestore();
 271 
 272   // Now, we will update the top fields of the "continues humongous"
 273   // regions except the last one.
 274   for (uint i = first; i < last; ++i) {
 275     hr = region_at(i);
 276     hr->set_top(hr->end());
 277   }
 278 
 279   hr = region_at(last);
 280   // If we cannot fit a filler object, we must set top to the end
 281   // of the humongous object, otherwise we cannot iterate the heap
 282   // and the BOT will not be complete.
 283   hr->set_top(hr->end() - words_not_fillable);
 284 
 285   assert(hr->bottom() < obj_top && obj_top <= hr->end(),
 286          "obj_top should be in last region");
 287 
 288   _verifier->check_bitmaps("Humongous Region Allocation", first_hr);
 289 
 290   assert(words_not_fillable == 0 ||
 291          first_hr->bottom() + word_size_sum - words_not_fillable == hr->top(),
 292          "Miscalculation in humongous allocation");
 293 
 294   increase_used((word_size_sum - words_not_fillable) * HeapWordSize);
 295 
 296   for (uint i = first; i <= last; ++i) {
 297     hr = region_at(i);
 298     _humongous_set.add(hr);
 299     _hr_printer.alloc(hr);
 300   }
 301 
 302   return new_obj;
 303 }
 304 
 305 size_t G1CollectedHeap::humongous_obj_size_in_regions(size_t word_size) {
 306   assert(is_humongous(word_size), "Object of size " SIZE_FORMAT " must be humongous here", word_size);
 307   return align_up(word_size, HeapRegion::GrainWords) / HeapRegion::GrainWords;
 308 }
 309 
 310 // If could fit into free regions w/o expansion, try.
 311 // Otherwise, if can expand, do so.
 312 // Otherwise, if using ex regions might help, try with ex given back.
 313 HeapWord* G1CollectedHeap::humongous_obj_allocate(size_t word_size) {
 314   assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
 315 
 316   _verifier->verify_region_sets_optional();
 317 
 318   uint obj_regions = (uint) humongous_obj_size_in_regions(word_size);
 319 
 320   // Policy: First try to allocate a humongous object in the free list.
 321   HeapRegion* humongous_start = _hrm.allocate_humongous(obj_regions);
 322   if (humongous_start == NULL) {
 323     // Policy: We could not find enough regions for the humongous object in the
 324     // free list. Look through the heap to find a mix of free and uncommitted regions.
 325     // If so, expand the heap and allocate the humongous object.
 326     humongous_start = _hrm.expand_and_allocate_humongous(obj_regions);
 327     if (humongous_start != NULL) {
 328       // We managed to find a region by expanding the heap.
 329       log_debug(gc, ergo, heap)("Heap expansion (humongous allocation request). Allocation request: " SIZE_FORMAT "B",
 330                                 word_size * HeapWordSize);
 331       policy()->record_new_heap_size(num_regions());
 332     } else {
 333       // Policy: Potentially trigger a defragmentation GC.
 334     }
 335   }
 336 
 337   HeapWord* result = NULL;
 338   if (humongous_start != NULL) {
 339     result = humongous_obj_allocate_initialize_regions(humongous_start, obj_regions, word_size);
 340     assert(result != NULL, "it should always return a valid result");
 341 
 342     // A successful humongous object allocation changes the used space
 343     // information of the old generation so we need to recalculate the
 344     // sizes and update the jstat counters here.
 345     monitoring_support()->update_sizes();
 346   }
 347 
 348   _verifier->verify_region_sets_optional();
 349 
 350   return result;
 351 }
 352 
 353 HeapWord* G1CollectedHeap::allocate_new_tlab(size_t min_size,
 354                                              size_t requested_size,
 355                                              size_t* actual_size) {
 356   assert_heap_not_locked_and_not_at_safepoint();
 357   assert(!is_humongous(requested_size), "we do not allow humongous TLABs");
 358 
 359   return attempt_allocation(min_size, requested_size, actual_size);
 360 }
 361 
 362 HeapWord*
 363 G1CollectedHeap::mem_allocate(size_t word_size,
 364                               bool*  gc_overhead_limit_was_exceeded) {
 365   assert_heap_not_locked_and_not_at_safepoint();
 366 
 367   if (is_humongous(word_size)) {
 368     return attempt_allocation_humongous(word_size);
 369   }
 370   size_t dummy = 0;
 371   return attempt_allocation(word_size, word_size, &dummy);
 372 }
 373 
 374 HeapWord* G1CollectedHeap::attempt_allocation_slow(size_t word_size) {
 375   ResourceMark rm; // For retrieving the thread names in log messages.
 376 
 377   // Make sure you read the note in attempt_allocation_humongous().
 378 
 379   assert_heap_not_locked_and_not_at_safepoint();
 380   assert(!is_humongous(word_size), "attempt_allocation_slow() should not "
 381          "be called for humongous allocation requests");
 382 
 383   // We should only get here after the first-level allocation attempt
 384   // (attempt_allocation()) failed to allocate.
 385 
 386   // We will loop until a) we manage to successfully perform the
 387   // allocation or b) we successfully schedule a collection which
 388   // fails to perform the allocation. b) is the only case when we'll
 389   // return NULL.
 390   HeapWord* result = NULL;
 391   for (uint try_count = 1, gclocker_retry_count = 0; /* we'll return */; try_count += 1) {
 392     bool should_try_gc;
 393     bool preventive_collection_required = false;
 394     uint gc_count_before;
 395 
 396     {
 397       MutexLocker x(Heap_lock);
 398 
 399       // Now that we have the lock, we first retry the allocation in case another
 400       // thread changed the region while we were waiting to acquire the lock.
 401       size_t actual_size;
 402       result = _allocator->attempt_allocation(word_size, word_size, &actual_size);
 403       if (result != NULL) {
 404         return result;
 405       }
 406 
 407       preventive_collection_required = policy()->preventive_collection_required(1);
 408       if (!preventive_collection_required) {
 409         // We've already attempted a lock-free allocation above, so we don't want to
 410         // do it again. Let's jump straight to replacing the active region.
 411         result = _allocator->attempt_allocation_using_new_region(word_size);
 412         if (result != NULL) {
 413           return result;
 414         }
 415 
 416         // If the GCLocker is active and we are bound for a GC, try expanding young gen.
 417         // This is different to when only GCLocker::needs_gc() is set: try to avoid
 418         // waiting because the GCLocker is active to not wait too long.
 419         if (GCLocker::is_active_and_needs_gc() && policy()->can_expand_young_list()) {
 420           // No need for an ergo message here, can_expand_young_list() does this when
 421           // it returns true.
 422           result = _allocator->attempt_allocation_force(word_size);
 423           if (result != NULL) {
 424             return result;
 425           }
 426         }
 427       }
 428 
 429       // Only try a GC if the GCLocker does not signal the need for a GC. Wait until
 430       // the GCLocker initiated GC has been performed and then retry. This includes
 431       // the case when the GC Locker is not active but has not been performed.
 432       should_try_gc = !GCLocker::needs_gc();
 433       // Read the GC count while still holding the Heap_lock.
 434       gc_count_before = total_collections();
 435     }
 436 
 437     if (should_try_gc) {
 438       GCCause::Cause gc_cause = preventive_collection_required ? GCCause::_g1_preventive_collection
 439                                                               : GCCause::_g1_inc_collection_pause;
 440       bool succeeded;
 441       result = do_collection_pause(word_size, gc_count_before, &succeeded, gc_cause);
 442       if (result != NULL) {
 443         assert(succeeded, "only way to get back a non-NULL result");
 444         log_trace(gc, alloc)("%s: Successfully scheduled collection returning " PTR_FORMAT,
 445                              Thread::current()->name(), p2i(result));
 446         return result;
 447       }
 448 
 449       if (succeeded) {
 450         // We successfully scheduled a collection which failed to allocate. No
 451         // point in trying to allocate further. We'll just return NULL.
 452         log_trace(gc, alloc)("%s: Successfully scheduled collection failing to allocate "
 453                              SIZE_FORMAT " words", Thread::current()->name(), word_size);
 454         return NULL;
 455       }
 456       log_trace(gc, alloc)("%s: Unsuccessfully scheduled collection allocating " SIZE_FORMAT " words",
 457                            Thread::current()->name(), word_size);
 458     } else {
 459       // Failed to schedule a collection.
 460       if (gclocker_retry_count > GCLockerRetryAllocationCount) {
 461         log_warning(gc, alloc)("%s: Retried waiting for GCLocker too often allocating "
 462                                SIZE_FORMAT " words", Thread::current()->name(), word_size);
 463         return NULL;
 464       }
 465       log_trace(gc, alloc)("%s: Stall until clear", Thread::current()->name());
 466       // The GCLocker is either active or the GCLocker initiated
 467       // GC has not yet been performed. Stall until it is and
 468       // then retry the allocation.
 469       GCLocker::stall_until_clear();
 470       gclocker_retry_count += 1;
 471     }
 472 
 473     // We can reach here if we were unsuccessful in scheduling a
 474     // collection (because another thread beat us to it) or if we were
 475     // stalled due to the GC locker. In either can we should retry the
 476     // allocation attempt in case another thread successfully
 477     // performed a collection and reclaimed enough space. We do the
 478     // first attempt (without holding the Heap_lock) here and the
 479     // follow-on attempt will be at the start of the next loop
 480     // iteration (after taking the Heap_lock).
 481     size_t dummy = 0;
 482     result = _allocator->attempt_allocation(word_size, word_size, &dummy);
 483     if (result != NULL) {
 484       return result;
 485     }
 486 
 487     // Give a warning if we seem to be looping forever.
 488     if ((QueuedAllocationWarningCount > 0) &&
 489         (try_count % QueuedAllocationWarningCount == 0)) {
 490       log_warning(gc, alloc)("%s:  Retried allocation %u times for " SIZE_FORMAT " words",
 491                              Thread::current()->name(), try_count, word_size);
 492     }
 493   }
 494 
 495   ShouldNotReachHere();
 496   return NULL;
 497 }
 498 
 499 void G1CollectedHeap::begin_archive_alloc_range(bool open) {
 500   assert_at_safepoint_on_vm_thread();
 501   assert(_archive_allocator == nullptr, "should not be initialized");
 502   _archive_allocator = G1ArchiveAllocator::create_allocator(this, open);
 503 }
 504 
 505 bool G1CollectedHeap::is_archive_alloc_too_large(size_t word_size) {
 506   // Allocations in archive regions cannot be of a size that would be considered
 507   // humongous even for a minimum-sized region, because G1 region sizes/boundaries
 508   // may be different at archive-restore time.
 509   return word_size >= humongous_threshold_for(HeapRegion::min_region_size_in_words());
 510 }
 511 
 512 HeapWord* G1CollectedHeap::archive_mem_allocate(size_t word_size) {
 513   assert_at_safepoint_on_vm_thread();
 514   assert(_archive_allocator != nullptr, "_archive_allocator not initialized");
 515   if (is_archive_alloc_too_large(word_size)) {
 516     return nullptr;
 517   }
 518   return _archive_allocator->archive_mem_allocate(word_size);
 519 }
 520 
 521 void G1CollectedHeap::end_archive_alloc_range(GrowableArray<MemRegion>* ranges,
 522                                               size_t end_alignment_in_bytes) {
 523   assert_at_safepoint_on_vm_thread();
 524   assert(_archive_allocator != nullptr, "_archive_allocator not initialized");
 525 
 526   // Call complete_archive to do the real work, filling in the MemRegion
 527   // array with the archive regions.
 528   _archive_allocator->complete_archive(ranges, end_alignment_in_bytes);
 529   delete _archive_allocator;
 530   _archive_allocator = nullptr;
 531 }
 532 
 533 bool G1CollectedHeap::check_archive_addresses(MemRegion* ranges, size_t count) {
 534   assert(ranges != NULL, "MemRegion array NULL");
 535   assert(count != 0, "No MemRegions provided");
 536   MemRegion reserved = _hrm.reserved();
 537   for (size_t i = 0; i < count; i++) {
 538     if (!reserved.contains(ranges[i].start()) || !reserved.contains(ranges[i].last())) {
 539       return false;
 540     }
 541   }
 542   return true;
 543 }
 544 
 545 bool G1CollectedHeap::alloc_archive_regions(MemRegion* ranges,
 546                                             size_t count,
 547                                             bool open) {
 548   assert(!is_init_completed(), "Expect to be called at JVM init time");
 549   assert(ranges != NULL, "MemRegion array NULL");
 550   assert(count != 0, "No MemRegions provided");
 551   MutexLocker x(Heap_lock);
 552 
 553   MemRegion reserved = _hrm.reserved();
 554   HeapWord* prev_last_addr = NULL;
 555   HeapRegion* prev_last_region = NULL;
 556 
 557   // Temporarily disable pretouching of heap pages. This interface is used
 558   // when mmap'ing archived heap data in, so pre-touching is wasted.
 559   FlagSetting fs(AlwaysPreTouch, false);
 560 
 561   // For each specified MemRegion range, allocate the corresponding G1
 562   // regions and mark them as archive regions. We expect the ranges
 563   // in ascending starting address order, without overlap.
 564   for (size_t i = 0; i < count; i++) {
 565     MemRegion curr_range = ranges[i];
 566     HeapWord* start_address = curr_range.start();
 567     size_t word_size = curr_range.word_size();
 568     HeapWord* last_address = curr_range.last();
 569     size_t commits = 0;
 570 
 571     guarantee(reserved.contains(start_address) && reserved.contains(last_address),
 572               "MemRegion outside of heap [" PTR_FORMAT ", " PTR_FORMAT "]",
 573               p2i(start_address), p2i(last_address));
 574     guarantee(start_address > prev_last_addr,
 575               "Ranges not in ascending order: " PTR_FORMAT " <= " PTR_FORMAT ,
 576               p2i(start_address), p2i(prev_last_addr));
 577     prev_last_addr = last_address;
 578 
 579     // Check for ranges that start in the same G1 region in which the previous
 580     // range ended, and adjust the start address so we don't try to allocate
 581     // the same region again. If the current range is entirely within that
 582     // region, skip it, just adjusting the recorded top.
 583     HeapRegion* start_region = _hrm.addr_to_region(start_address);
 584     if ((prev_last_region != NULL) && (start_region == prev_last_region)) {
 585       start_address = start_region->end();
 586       if (start_address > last_address) {
 587         increase_used(word_size * HeapWordSize);
 588         start_region->set_top(last_address + 1);
 589         continue;
 590       }
 591       start_region->set_top(start_address);
 592       curr_range = MemRegion(start_address, last_address + 1);
 593       start_region = _hrm.addr_to_region(start_address);
 594     }
 595 
 596     // Perform the actual region allocation, exiting if it fails.
 597     // Then note how much new space we have allocated.
 598     if (!_hrm.allocate_containing_regions(curr_range, &commits, workers())) {
 599       return false;
 600     }
 601     increase_used(word_size * HeapWordSize);
 602     if (commits != 0) {
 603       log_debug(gc, ergo, heap)("Attempt heap expansion (allocate archive regions). Total size: " SIZE_FORMAT "B",
 604                                 HeapRegion::GrainWords * HeapWordSize * commits);
 605 
 606     }
 607 
 608     // Mark each G1 region touched by the range as archive, add it to
 609     // the old set, and set top.
 610     HeapRegion* curr_region = _hrm.addr_to_region(start_address);
 611     HeapRegion* last_region = _hrm.addr_to_region(last_address);
 612     prev_last_region = last_region;
 613 
 614     while (curr_region != NULL) {
 615       assert(curr_region->is_empty() && !curr_region->is_pinned(),
 616              "Region already in use (index %u)", curr_region->hrm_index());
 617       if (open) {
 618         curr_region->set_open_archive();
 619       } else {
 620         curr_region->set_closed_archive();
 621       }
 622       _hr_printer.alloc(curr_region);
 623       _archive_set.add(curr_region);
 624       HeapWord* top;
 625       HeapRegion* next_region;
 626       if (curr_region != last_region) {
 627         top = curr_region->end();
 628         next_region = _hrm.next_region_in_heap(curr_region);
 629       } else {
 630         top = last_address + 1;
 631         next_region = NULL;
 632       }
 633       curr_region->set_top(top);
 634       curr_region = next_region;
 635     }
 636   }
 637   return true;
 638 }
 639 
 640 void G1CollectedHeap::fill_archive_regions(MemRegion* ranges, size_t count) {
 641   assert(!is_init_completed(), "Expect to be called at JVM init time");
 642   assert(ranges != NULL, "MemRegion array NULL");
 643   assert(count != 0, "No MemRegions provided");
 644   MemRegion reserved = _hrm.reserved();
 645   HeapWord *prev_last_addr = NULL;
 646   HeapRegion* prev_last_region = NULL;
 647 
 648   // For each MemRegion, create filler objects, if needed, in the G1 regions
 649   // that contain the address range. The address range actually within the
 650   // MemRegion will not be modified. That is assumed to have been initialized
 651   // elsewhere, probably via an mmap of archived heap data.
 652   MutexLocker x(Heap_lock);
 653   for (size_t i = 0; i < count; i++) {
 654     HeapWord* start_address = ranges[i].start();
 655     HeapWord* last_address = ranges[i].last();
 656 
 657     assert(reserved.contains(start_address) && reserved.contains(last_address),
 658            "MemRegion outside of heap [" PTR_FORMAT ", " PTR_FORMAT "]",
 659            p2i(start_address), p2i(last_address));
 660     assert(start_address > prev_last_addr,
 661            "Ranges not in ascending order: " PTR_FORMAT " <= " PTR_FORMAT ,
 662            p2i(start_address), p2i(prev_last_addr));
 663 
 664     HeapRegion* start_region = _hrm.addr_to_region(start_address);
 665     HeapRegion* last_region = _hrm.addr_to_region(last_address);
 666     HeapWord* bottom_address = start_region->bottom();
 667 
 668     // Check for a range beginning in the same region in which the
 669     // previous one ended.
 670     if (start_region == prev_last_region) {
 671       bottom_address = prev_last_addr + 1;
 672     }
 673 
 674     // Verify that the regions were all marked as archive regions by
 675     // alloc_archive_regions.
 676     HeapRegion* curr_region = start_region;
 677     while (curr_region != NULL) {
 678       guarantee(curr_region->is_archive(),
 679                 "Expected archive region at index %u", curr_region->hrm_index());
 680       if (curr_region != last_region) {
 681         curr_region = _hrm.next_region_in_heap(curr_region);
 682       } else {
 683         curr_region = NULL;
 684       }
 685     }
 686 
 687     prev_last_addr = last_address;
 688     prev_last_region = last_region;
 689 
 690     // Fill the memory below the allocated range with dummy object(s),
 691     // if the region bottom does not match the range start, or if the previous
 692     // range ended within the same G1 region, and there is a gap.
 693     assert(start_address >= bottom_address, "bottom address should not be greater than start address");
 694     if (start_address > bottom_address) {
 695       size_t fill_size = pointer_delta(start_address, bottom_address);
 696       G1CollectedHeap::fill_with_objects(bottom_address, fill_size);
 697       increase_used(fill_size * HeapWordSize);
 698     }
 699   }
 700 }
 701 
 702 inline HeapWord* G1CollectedHeap::attempt_allocation(size_t min_word_size,
 703                                                      size_t desired_word_size,
 704                                                      size_t* actual_word_size) {
 705   assert_heap_not_locked_and_not_at_safepoint();
 706   assert(!is_humongous(desired_word_size), "attempt_allocation() should not "
 707          "be called for humongous allocation requests");
 708 
 709   HeapWord* result = _allocator->attempt_allocation(min_word_size, desired_word_size, actual_word_size);
 710 
 711   if (result == NULL) {
 712     *actual_word_size = desired_word_size;
 713     result = attempt_allocation_slow(desired_word_size);
 714   }
 715 
 716   assert_heap_not_locked();
 717   if (result != NULL) {
 718     assert(*actual_word_size != 0, "Actual size must have been set here");
 719     dirty_young_block(result, *actual_word_size);
 720   } else {
 721     *actual_word_size = 0;
 722   }
 723 
 724   return result;
 725 }
 726 
 727 void G1CollectedHeap::populate_archive_regions_bot_part(MemRegion* ranges, size_t count) {
 728   assert(!is_init_completed(), "Expect to be called at JVM init time");
 729   assert(ranges != NULL, "MemRegion array NULL");
 730   assert(count != 0, "No MemRegions provided");
 731 
 732   HeapWord* st = ranges[0].start();
 733   HeapWord* last = ranges[count-1].last();
 734   HeapRegion* hr_st = _hrm.addr_to_region(st);
 735   HeapRegion* hr_last = _hrm.addr_to_region(last);
 736 
 737   HeapRegion* hr_curr = hr_st;
 738   while (hr_curr != NULL) {
 739     hr_curr->update_bot();
 740     if (hr_curr != hr_last) {
 741       hr_curr = _hrm.next_region_in_heap(hr_curr);
 742     } else {
 743       hr_curr = NULL;
 744     }
 745   }
 746 }
 747 
 748 void G1CollectedHeap::dealloc_archive_regions(MemRegion* ranges, size_t count) {
 749   assert(!is_init_completed(), "Expect to be called at JVM init time");
 750   assert(ranges != NULL, "MemRegion array NULL");
 751   assert(count != 0, "No MemRegions provided");
 752   MemRegion reserved = _hrm.reserved();
 753   HeapWord* prev_last_addr = NULL;
 754   HeapRegion* prev_last_region = NULL;
 755   size_t size_used = 0;
 756   uint shrink_count = 0;
 757 
 758   // For each Memregion, free the G1 regions that constitute it, and
 759   // notify mark-sweep that the range is no longer to be considered 'archive.'
 760   MutexLocker x(Heap_lock);
 761   for (size_t i = 0; i < count; i++) {
 762     HeapWord* start_address = ranges[i].start();
 763     HeapWord* last_address = ranges[i].last();
 764 
 765     assert(reserved.contains(start_address) && reserved.contains(last_address),
 766            "MemRegion outside of heap [" PTR_FORMAT ", " PTR_FORMAT "]",
 767            p2i(start_address), p2i(last_address));
 768     assert(start_address > prev_last_addr,
 769            "Ranges not in ascending order: " PTR_FORMAT " <= " PTR_FORMAT ,
 770            p2i(start_address), p2i(prev_last_addr));
 771     size_used += ranges[i].byte_size();
 772     prev_last_addr = last_address;
 773 
 774     HeapRegion* start_region = _hrm.addr_to_region(start_address);
 775     HeapRegion* last_region = _hrm.addr_to_region(last_address);
 776 
 777     // Check for ranges that start in the same G1 region in which the previous
 778     // range ended, and adjust the start address so we don't try to free
 779     // the same region again. If the current range is entirely within that
 780     // region, skip it.
 781     if (start_region == prev_last_region) {
 782       start_address = start_region->end();
 783       if (start_address > last_address) {
 784         continue;
 785       }
 786       start_region = _hrm.addr_to_region(start_address);
 787     }
 788     prev_last_region = last_region;
 789 
 790     // After verifying that each region was marked as an archive region by
 791     // alloc_archive_regions, set it free and empty and uncommit it.
 792     HeapRegion* curr_region = start_region;
 793     while (curr_region != NULL) {
 794       guarantee(curr_region->is_archive(),
 795                 "Expected archive region at index %u", curr_region->hrm_index());
 796       uint curr_index = curr_region->hrm_index();
 797       _archive_set.remove(curr_region);
 798       curr_region->set_free();
 799       curr_region->set_top(curr_region->bottom());
 800       if (curr_region != last_region) {
 801         curr_region = _hrm.next_region_in_heap(curr_region);
 802       } else {
 803         curr_region = NULL;
 804       }
 805 
 806       _hrm.shrink_at(curr_index, 1);
 807       shrink_count++;
 808     }
 809   }
 810 
 811   if (shrink_count != 0) {
 812     log_debug(gc, ergo, heap)("Attempt heap shrinking (archive regions). Total size: " SIZE_FORMAT "B",
 813                               HeapRegion::GrainWords * HeapWordSize * shrink_count);
 814     // Explicit uncommit.
 815     uncommit_regions(shrink_count);
 816   }
 817   decrease_used(size_used);
 818 }
 819 
 820 HeapWord* G1CollectedHeap::attempt_allocation_humongous(size_t word_size) {
 821   ResourceMark rm; // For retrieving the thread names in log messages.
 822 
 823   // The structure of this method has a lot of similarities to
 824   // attempt_allocation_slow(). The reason these two were not merged
 825   // into a single one is that such a method would require several "if
 826   // allocation is not humongous do this, otherwise do that"
 827   // conditional paths which would obscure its flow. In fact, an early
 828   // version of this code did use a unified method which was harder to
 829   // follow and, as a result, it had subtle bugs that were hard to
 830   // track down. So keeping these two methods separate allows each to
 831   // be more readable. It will be good to keep these two in sync as
 832   // much as possible.
 833 
 834   assert_heap_not_locked_and_not_at_safepoint();
 835   assert(is_humongous(word_size), "attempt_allocation_humongous() "
 836          "should only be called for humongous allocations");
 837 
 838   // Humongous objects can exhaust the heap quickly, so we should check if we
 839   // need to start a marking cycle at each humongous object allocation. We do
 840   // the check before we do the actual allocation. The reason for doing it
 841   // before the allocation is that we avoid having to keep track of the newly
 842   // allocated memory while we do a GC.
 843   if (policy()->need_to_start_conc_mark("concurrent humongous allocation",
 844                                         word_size)) {
 845     collect(GCCause::_g1_humongous_allocation);
 846   }
 847 
 848   // We will loop until a) we manage to successfully perform the
 849   // allocation or b) we successfully schedule a collection which
 850   // fails to perform the allocation. b) is the only case when we'll
 851   // return NULL.
 852   HeapWord* result = NULL;
 853   for (uint try_count = 1, gclocker_retry_count = 0; /* we'll return */; try_count += 1) {
 854     bool should_try_gc;
 855     bool preventive_collection_required = false;
 856     uint gc_count_before;
 857 
 858 
 859     {
 860       MutexLocker x(Heap_lock);
 861 
 862       size_t size_in_regions = humongous_obj_size_in_regions(word_size);
 863       preventive_collection_required = policy()->preventive_collection_required((uint)size_in_regions);
 864       if (!preventive_collection_required) {
 865         // Given that humongous objects are not allocated in young
 866         // regions, we'll first try to do the allocation without doing a
 867         // collection hoping that there's enough space in the heap.
 868         result = humongous_obj_allocate(word_size);
 869         if (result != NULL) {
 870           policy()->old_gen_alloc_tracker()->
 871             add_allocated_humongous_bytes_since_last_gc(size_in_regions * HeapRegion::GrainBytes);
 872           return result;
 873         }
 874       }
 875 
 876       // Only try a GC if the GCLocker does not signal the need for a GC. Wait until
 877       // the GCLocker initiated GC has been performed and then retry. This includes
 878       // the case when the GC Locker is not active but has not been performed.
 879       should_try_gc = !GCLocker::needs_gc();
 880       // Read the GC count while still holding the Heap_lock.
 881       gc_count_before = total_collections();
 882     }
 883 
 884     if (should_try_gc) {
 885       GCCause::Cause gc_cause = preventive_collection_required ? GCCause::_g1_preventive_collection
 886                                                               : GCCause::_g1_humongous_allocation;
 887       bool succeeded;
 888       result = do_collection_pause(word_size, gc_count_before, &succeeded, gc_cause);
 889       if (result != NULL) {
 890         assert(succeeded, "only way to get back a non-NULL result");
 891         log_trace(gc, alloc)("%s: Successfully scheduled collection returning " PTR_FORMAT,
 892                              Thread::current()->name(), p2i(result));
 893         size_t size_in_regions = humongous_obj_size_in_regions(word_size);
 894         policy()->old_gen_alloc_tracker()->
 895           record_collection_pause_humongous_allocation(size_in_regions * HeapRegion::GrainBytes);
 896         return result;
 897       }
 898 
 899       if (succeeded) {
 900         // We successfully scheduled a collection which failed to allocate. No
 901         // point in trying to allocate further. We'll just return NULL.
 902         log_trace(gc, alloc)("%s: Successfully scheduled collection failing to allocate "
 903                              SIZE_FORMAT " words", Thread::current()->name(), word_size);
 904         return NULL;
 905       }
 906       log_trace(gc, alloc)("%s: Unsuccessfully scheduled collection allocating " SIZE_FORMAT "",
 907                            Thread::current()->name(), word_size);
 908     } else {
 909       // Failed to schedule a collection.
 910       if (gclocker_retry_count > GCLockerRetryAllocationCount) {
 911         log_warning(gc, alloc)("%s: Retried waiting for GCLocker too often allocating "
 912                                SIZE_FORMAT " words", Thread::current()->name(), word_size);
 913         return NULL;
 914       }
 915       log_trace(gc, alloc)("%s: Stall until clear", Thread::current()->name());
 916       // The GCLocker is either active or the GCLocker initiated
 917       // GC has not yet been performed. Stall until it is and
 918       // then retry the allocation.
 919       GCLocker::stall_until_clear();
 920       gclocker_retry_count += 1;
 921     }
 922 
 923 
 924     // We can reach here if we were unsuccessful in scheduling a
 925     // collection (because another thread beat us to it) or if we were
 926     // stalled due to the GC locker. In either can we should retry the
 927     // allocation attempt in case another thread successfully
 928     // performed a collection and reclaimed enough space.
 929     // Humongous object allocation always needs a lock, so we wait for the retry
 930     // in the next iteration of the loop, unlike for the regular iteration case.
 931     // Give a warning if we seem to be looping forever.
 932 
 933     if ((QueuedAllocationWarningCount > 0) &&
 934         (try_count % QueuedAllocationWarningCount == 0)) {
 935       log_warning(gc, alloc)("%s: Retried allocation %u times for " SIZE_FORMAT " words",
 936                              Thread::current()->name(), try_count, word_size);
 937     }
 938   }
 939 
 940   ShouldNotReachHere();
 941   return NULL;
 942 }
 943 
 944 HeapWord* G1CollectedHeap::attempt_allocation_at_safepoint(size_t word_size,
 945                                                            bool expect_null_mutator_alloc_region) {
 946   assert_at_safepoint_on_vm_thread();
 947   assert(!_allocator->has_mutator_alloc_region() || !expect_null_mutator_alloc_region,
 948          "the current alloc region was unexpectedly found to be non-NULL");
 949 
 950   if (!is_humongous(word_size)) {
 951     return _allocator->attempt_allocation_locked(word_size);
 952   } else {
 953     HeapWord* result = humongous_obj_allocate(word_size);
 954     if (result != NULL && policy()->need_to_start_conc_mark("STW humongous allocation")) {
 955       collector_state()->set_initiate_conc_mark_if_possible(true);
 956     }
 957     return result;
 958   }
 959 
 960   ShouldNotReachHere();
 961 }
 962 
 963 class PostCompactionPrinterClosure: public HeapRegionClosure {
 964 private:
 965   G1HRPrinter* _hr_printer;
 966 public:
 967   bool do_heap_region(HeapRegion* hr) {
 968     assert(!hr->is_young(), "not expecting to find young regions");
 969     _hr_printer->post_compaction(hr);
 970     return false;
 971   }
 972 
 973   PostCompactionPrinterClosure(G1HRPrinter* hr_printer)
 974     : _hr_printer(hr_printer) { }
 975 };
 976 
 977 void G1CollectedHeap::print_heap_after_full_collection() {
 978   // Post collection region logging.
 979   // We should do this after we potentially resize the heap so
 980   // that all the COMMIT / UNCOMMIT events are generated before
 981   // the compaction events.
 982   if (_hr_printer.is_active()) {
 983     PostCompactionPrinterClosure cl(hr_printer());
 984     heap_region_iterate(&cl);
 985   }
 986 }
 987 
 988 void G1CollectedHeap::abort_concurrent_cycle() {
 989   // If we start the compaction before the CM threads finish
 990   // scanning the root regions we might trip them over as we'll
 991   // be moving objects / updating references. So let's wait until
 992   // they are done. By telling them to abort, they should complete
 993   // early.
 994   _cm->root_regions()->abort();
 995   _cm->root_regions()->wait_until_scan_finished();
 996 
 997   // Disable discovery and empty the discovered lists
 998   // for the CM ref processor.
 999   _ref_processor_cm->disable_discovery();
1000   _ref_processor_cm->abandon_partial_discovery();
1001   _ref_processor_cm->verify_no_references_recorded();
1002 
1003   // Abandon current iterations of concurrent marking and concurrent
1004   // refinement, if any are in progress.
1005   concurrent_mark()->concurrent_cycle_abort();
1006 }
1007 
1008 void G1CollectedHeap::prepare_heap_for_full_collection() {
1009   // Make sure we'll choose a new allocation region afterwards.
1010   _allocator->release_mutator_alloc_regions();
1011   _allocator->abandon_gc_alloc_regions();
1012 
1013   // We may have added regions to the current incremental collection
1014   // set between the last GC or pause and now. We need to clear the
1015   // incremental collection set and then start rebuilding it afresh
1016   // after this full GC.
1017   abandon_collection_set(collection_set());
1018 
1019   _hrm.remove_all_free_regions();
1020 }
1021 
1022 void G1CollectedHeap::verify_before_full_collection(bool explicit_gc) {
1023   assert(!GCCause::is_user_requested_gc(gc_cause()) || explicit_gc, "invariant");
1024   assert_used_and_recalculate_used_equal(this);
1025   if (!VerifyBeforeGC) {
1026     return;
1027   }
1028   _verifier->verify_region_sets_optional();
1029   _verifier->verify_before_gc(G1HeapVerifier::G1VerifyFull);
1030   _verifier->check_bitmaps("Full GC Start");
1031 }
1032 
1033 void G1CollectedHeap::prepare_heap_for_mutators() {
1034   // Delete metaspaces for unloaded class loaders and clean up loader_data graph
1035   ClassLoaderDataGraph::purge(/*at_safepoint*/true);
1036   DEBUG_ONLY(MetaspaceUtils::verify();)
1037 
1038   // Prepare heap for normal collections.
1039   assert(num_free_regions() == 0, "we should not have added any free regions");
1040   rebuild_region_sets(false /* free_list_only */);
1041   abort_refinement();
1042   resize_heap_if_necessary();
1043   uncommit_regions_if_necessary();
1044 
1045   // Rebuild the strong code root lists for each region
1046   rebuild_strong_code_roots();
1047 
1048   // Purge code root memory
1049   purge_code_root_memory();
1050 
1051   // Start a new incremental collection set for the next pause
1052   start_new_collection_set();
1053 
1054   _allocator->init_mutator_alloc_regions();
1055 
1056   // Post collection state updates.
1057   MetaspaceGC::compute_new_size();
1058 }
1059 
1060 void G1CollectedHeap::abort_refinement() {
1061   if (_hot_card_cache->use_cache()) {
1062     _hot_card_cache->reset_hot_cache();
1063   }
1064 
1065   // Discard all remembered set updates and reset refinement statistics.
1066   G1BarrierSet::dirty_card_queue_set().abandon_logs();
1067   assert(G1BarrierSet::dirty_card_queue_set().num_cards() == 0,
1068          "DCQS should be empty");
1069   concurrent_refine()->get_and_reset_refinement_stats();
1070 }
1071 
1072 void G1CollectedHeap::verify_after_full_collection() {
1073   if (!VerifyAfterGC) {
1074     return;
1075   }
1076   _hrm.verify_optional();
1077   _verifier->verify_region_sets_optional();
1078   _verifier->verify_after_gc(G1HeapVerifier::G1VerifyFull);
1079 
1080   // This call implicitly verifies that the next bitmap is clear after Full GC.
1081   _verifier->check_bitmaps("Full GC End");
1082 
1083   // At this point there should be no regions in the
1084   // entire heap tagged as young.
1085   assert(check_young_list_empty(), "young list should be empty at this point");
1086 
1087   // Note: since we've just done a full GC, concurrent
1088   // marking is no longer active. Therefore we need not
1089   // re-enable reference discovery for the CM ref processor.
1090   // That will be done at the start of the next marking cycle.
1091   // We also know that the STW processor should no longer
1092   // discover any new references.
1093   assert(!_ref_processor_stw->discovery_enabled(), "Postcondition");
1094   assert(!_ref_processor_cm->discovery_enabled(), "Postcondition");
1095   _ref_processor_stw->verify_no_references_recorded();
1096   _ref_processor_cm->verify_no_references_recorded();
1097 }
1098 
1099 bool G1CollectedHeap::do_full_collection(bool explicit_gc,
1100                                          bool clear_all_soft_refs,
1101                                          bool do_maximum_compaction) {
1102   assert_at_safepoint_on_vm_thread();
1103 
1104   if (GCLocker::check_active_before_gc()) {
1105     // Full GC was not completed.
1106     return false;
1107   }
1108 
1109   const bool do_clear_all_soft_refs = clear_all_soft_refs ||
1110       soft_ref_policy()->should_clear_all_soft_refs();
1111 
1112   G1FullGCMark gc_mark;
1113   GCTraceTime(Info, gc) tm("Pause Full", NULL, gc_cause(), true);
1114   G1FullCollector collector(this, explicit_gc, do_clear_all_soft_refs, do_maximum_compaction);
1115 
1116   collector.prepare_collection();
1117   collector.collect();
1118   collector.complete_collection();
1119 
1120   // Full collection was successfully completed.
1121   return true;
1122 }
1123 
1124 void G1CollectedHeap::do_full_collection(bool clear_all_soft_refs) {
1125   // Currently, there is no facility in the do_full_collection(bool) API to notify
1126   // the caller that the collection did not succeed (e.g., because it was locked
1127   // out by the GC locker). So, right now, we'll ignore the return value.
1128   // When clear_all_soft_refs is set we want to do a maximum compaction
1129   // not leaving any dead wood.
1130   bool do_maximum_compaction = clear_all_soft_refs;
1131   bool dummy = do_full_collection(true,                /* explicit_gc */
1132                                   clear_all_soft_refs,
1133                                   do_maximum_compaction);
1134 }
1135 
1136 bool G1CollectedHeap::upgrade_to_full_collection() {
1137   GCCauseSetter compaction(this, GCCause::_g1_compaction_pause);
1138   log_info(gc, ergo)("Attempting full compaction clearing soft references");
1139   bool success = do_full_collection(false /* explicit gc */,
1140                                     true  /* clear_all_soft_refs */,
1141                                     false /* do_maximum_compaction */);
1142   // do_full_collection only fails if blocked by GC locker and that can't
1143   // be the case here since we only call this when already completed one gc.
1144   assert(success, "invariant");
1145   return success;
1146 }
1147 
1148 void G1CollectedHeap::resize_heap_if_necessary() {
1149   assert_at_safepoint_on_vm_thread();
1150 
1151   bool should_expand;
1152   size_t resize_amount = _heap_sizing_policy->full_collection_resize_amount(should_expand);
1153 
1154   if (resize_amount == 0) {
1155     return;
1156   } else if (should_expand) {
1157     expand(resize_amount, _workers);
1158   } else {
1159     shrink(resize_amount);
1160   }
1161 }
1162 
1163 HeapWord* G1CollectedHeap::satisfy_failed_allocation_helper(size_t word_size,
1164                                                             bool do_gc,
1165                                                             bool maximum_compaction,
1166                                                             bool expect_null_mutator_alloc_region,
1167                                                             bool* gc_succeeded) {
1168   *gc_succeeded = true;
1169   // Let's attempt the allocation first.
1170   HeapWord* result =
1171     attempt_allocation_at_safepoint(word_size,
1172                                     expect_null_mutator_alloc_region);
1173   if (result != NULL) {
1174     return result;
1175   }
1176 
1177   // In a G1 heap, we're supposed to keep allocation from failing by
1178   // incremental pauses.  Therefore, at least for now, we'll favor
1179   // expansion over collection.  (This might change in the future if we can
1180   // do something smarter than full collection to satisfy a failed alloc.)
1181   result = expand_and_allocate(word_size);
1182   if (result != NULL) {
1183     return result;
1184   }
1185 
1186   if (do_gc) {
1187     GCCauseSetter compaction(this, GCCause::_g1_compaction_pause);
1188     // Expansion didn't work, we'll try to do a Full GC.
1189     // If maximum_compaction is set we clear all soft references and don't
1190     // allow any dead wood to be left on the heap.
1191     if (maximum_compaction) {
1192       log_info(gc, ergo)("Attempting maximum full compaction clearing soft references");
1193     } else {
1194       log_info(gc, ergo)("Attempting full compaction");
1195     }
1196     *gc_succeeded = do_full_collection(false, /* explicit_gc */
1197                                        maximum_compaction /* clear_all_soft_refs */ ,
1198                                        maximum_compaction /* do_maximum_compaction */);
1199   }
1200 
1201   return NULL;
1202 }
1203 
1204 HeapWord* G1CollectedHeap::satisfy_failed_allocation(size_t word_size,
1205                                                      bool* succeeded) {
1206   assert_at_safepoint_on_vm_thread();
1207 
1208   // Attempts to allocate followed by Full GC.
1209   HeapWord* result =
1210     satisfy_failed_allocation_helper(word_size,
1211                                      true,  /* do_gc */
1212                                      false, /* maximum_collection */
1213                                      false, /* expect_null_mutator_alloc_region */
1214                                      succeeded);
1215 
1216   if (result != NULL || !*succeeded) {
1217     return result;
1218   }
1219 
1220   // Attempts to allocate followed by Full GC that will collect all soft references.
1221   result = satisfy_failed_allocation_helper(word_size,
1222                                             true, /* do_gc */
1223                                             true, /* maximum_collection */
1224                                             true, /* expect_null_mutator_alloc_region */
1225                                             succeeded);
1226 
1227   if (result != NULL || !*succeeded) {
1228     return result;
1229   }
1230 
1231   // Attempts to allocate, no GC
1232   result = satisfy_failed_allocation_helper(word_size,
1233                                             false, /* do_gc */
1234                                             false, /* maximum_collection */
1235                                             true,  /* expect_null_mutator_alloc_region */
1236                                             succeeded);
1237 
1238   if (result != NULL) {
1239     return result;
1240   }
1241 
1242   assert(!soft_ref_policy()->should_clear_all_soft_refs(),
1243          "Flag should have been handled and cleared prior to this point");
1244 
1245   // What else?  We might try synchronous finalization later.  If the total
1246   // space available is large enough for the allocation, then a more
1247   // complete compaction phase than we've tried so far might be
1248   // appropriate.
1249   return NULL;
1250 }
1251 
1252 // Attempting to expand the heap sufficiently
1253 // to support an allocation of the given "word_size".  If
1254 // successful, perform the allocation and return the address of the
1255 // allocated block, or else "NULL".
1256 
1257 HeapWord* G1CollectedHeap::expand_and_allocate(size_t word_size) {
1258   assert_at_safepoint_on_vm_thread();
1259 
1260   _verifier->verify_region_sets_optional();
1261 
1262   size_t expand_bytes = MAX2(word_size * HeapWordSize, MinHeapDeltaBytes);
1263   log_debug(gc, ergo, heap)("Attempt heap expansion (allocation request failed). Allocation request: " SIZE_FORMAT "B",
1264                             word_size * HeapWordSize);
1265 
1266 
1267   if (expand(expand_bytes, _workers)) {
1268     _hrm.verify_optional();
1269     _verifier->verify_region_sets_optional();
1270     return attempt_allocation_at_safepoint(word_size,
1271                                            false /* expect_null_mutator_alloc_region */);
1272   }
1273   return NULL;
1274 }
1275 
1276 bool G1CollectedHeap::expand(size_t expand_bytes, WorkerThreads* pretouch_workers, double* expand_time_ms) {
1277   size_t aligned_expand_bytes = ReservedSpace::page_align_size_up(expand_bytes);
1278   aligned_expand_bytes = align_up(aligned_expand_bytes,
1279                                        HeapRegion::GrainBytes);
1280 
1281   log_debug(gc, ergo, heap)("Expand the heap. requested expansion amount: " SIZE_FORMAT "B expansion amount: " SIZE_FORMAT "B",
1282                             expand_bytes, aligned_expand_bytes);
1283 
1284   if (is_maximal_no_gc()) {
1285     log_debug(gc, ergo, heap)("Did not expand the heap (heap already fully expanded)");
1286     return false;
1287   }
1288 
1289   double expand_heap_start_time_sec = os::elapsedTime();
1290   uint regions_to_expand = (uint)(aligned_expand_bytes / HeapRegion::GrainBytes);
1291   assert(regions_to_expand > 0, "Must expand by at least one region");
1292 
1293   uint expanded_by = _hrm.expand_by(regions_to_expand, pretouch_workers);
1294   if (expand_time_ms != NULL) {
1295     *expand_time_ms = (os::elapsedTime() - expand_heap_start_time_sec) * MILLIUNITS;
1296   }
1297 
1298   if (expanded_by > 0) {
1299     size_t actual_expand_bytes = expanded_by * HeapRegion::GrainBytes;
1300     assert(actual_expand_bytes <= aligned_expand_bytes, "post-condition");
1301     policy()->record_new_heap_size(num_regions());
1302   } else {
1303     log_debug(gc, ergo, heap)("Did not expand the heap (heap expansion operation failed)");
1304 
1305     // The expansion of the virtual storage space was unsuccessful.
1306     // Let's see if it was because we ran out of swap.
1307     if (G1ExitOnExpansionFailure &&
1308         _hrm.available() >= regions_to_expand) {
1309       // We had head room...
1310       vm_exit_out_of_memory(aligned_expand_bytes, OOM_MMAP_ERROR, "G1 heap expansion");
1311     }
1312   }
1313   return expanded_by > 0;
1314 }
1315 
1316 bool G1CollectedHeap::expand_single_region(uint node_index) {
1317   uint expanded_by = _hrm.expand_on_preferred_node(node_index);
1318 
1319   if (expanded_by == 0) {
1320     assert(is_maximal_no_gc(), "Should be no regions left, available: %u", _hrm.available());
1321     log_debug(gc, ergo, heap)("Did not expand the heap (heap already fully expanded)");
1322     return false;
1323   }
1324 
1325   policy()->record_new_heap_size(num_regions());
1326   return true;
1327 }
1328 
1329 void G1CollectedHeap::shrink_helper(size_t shrink_bytes) {
1330   size_t aligned_shrink_bytes =
1331     ReservedSpace::page_align_size_down(shrink_bytes);
1332   aligned_shrink_bytes = align_down(aligned_shrink_bytes,
1333                                          HeapRegion::GrainBytes);
1334   uint num_regions_to_remove = (uint)(shrink_bytes / HeapRegion::GrainBytes);
1335 
1336   uint num_regions_removed = _hrm.shrink_by(num_regions_to_remove);
1337   size_t shrunk_bytes = num_regions_removed * HeapRegion::GrainBytes;
1338 
1339   log_debug(gc, ergo, heap)("Shrink the heap. requested shrinking amount: " SIZE_FORMAT "B aligned shrinking amount: " SIZE_FORMAT "B attempted shrinking amount: " SIZE_FORMAT "B",
1340                             shrink_bytes, aligned_shrink_bytes, shrunk_bytes);
1341   if (num_regions_removed > 0) {
1342     log_debug(gc, heap)("Uncommittable regions after shrink: %u", num_regions_removed);
1343     policy()->record_new_heap_size(num_regions());
1344   } else {
1345     log_debug(gc, ergo, heap)("Did not expand the heap (heap shrinking operation failed)");
1346   }
1347 }
1348 
1349 void G1CollectedHeap::shrink(size_t shrink_bytes) {
1350   _verifier->verify_region_sets_optional();
1351 
1352   // We should only reach here at the end of a Full GC or during Remark which
1353   // means we should not not be holding to any GC alloc regions. The method
1354   // below will make sure of that and do any remaining clean up.
1355   _allocator->abandon_gc_alloc_regions();
1356 
1357   // Instead of tearing down / rebuilding the free lists here, we
1358   // could instead use the remove_all_pending() method on free_list to
1359   // remove only the ones that we need to remove.
1360   _hrm.remove_all_free_regions();
1361   shrink_helper(shrink_bytes);
1362   rebuild_region_sets(true /* free_list_only */);
1363 
1364   _hrm.verify_optional();
1365   _verifier->verify_region_sets_optional();
1366 }
1367 
1368 class OldRegionSetChecker : public HeapRegionSetChecker {
1369 public:
1370   void check_mt_safety() {
1371     // Master Old Set MT safety protocol:
1372     // (a) If we're at a safepoint, operations on the master old set
1373     // should be invoked:
1374     // - by the VM thread (which will serialize them), or
1375     // - by the GC workers while holding the FreeList_lock, if we're
1376     //   at a safepoint for an evacuation pause (this lock is taken
1377     //   anyway when an GC alloc region is retired so that a new one
1378     //   is allocated from the free list), or
1379     // - by the GC workers while holding the OldSets_lock, if we're at a
1380     //   safepoint for a cleanup pause.
1381     // (b) If we're not at a safepoint, operations on the master old set
1382     // should be invoked while holding the Heap_lock.
1383 
1384     if (SafepointSynchronize::is_at_safepoint()) {
1385       guarantee(Thread::current()->is_VM_thread() ||
1386                 FreeList_lock->owned_by_self() || OldSets_lock->owned_by_self(),
1387                 "master old set MT safety protocol at a safepoint");
1388     } else {
1389       guarantee(Heap_lock->owned_by_self(), "master old set MT safety protocol outside a safepoint");
1390     }
1391   }
1392   bool is_correct_type(HeapRegion* hr) { return hr->is_old(); }
1393   const char* get_description() { return "Old Regions"; }
1394 };
1395 
1396 class ArchiveRegionSetChecker : public HeapRegionSetChecker {
1397 public:
1398   void check_mt_safety() {
1399     guarantee(!Universe::is_fully_initialized() || SafepointSynchronize::is_at_safepoint(),
1400               "May only change archive regions during initialization or safepoint.");
1401   }
1402   bool is_correct_type(HeapRegion* hr) { return hr->is_archive(); }
1403   const char* get_description() { return "Archive Regions"; }
1404 };
1405 
1406 class HumongousRegionSetChecker : public HeapRegionSetChecker {
1407 public:
1408   void check_mt_safety() {
1409     // Humongous Set MT safety protocol:
1410     // (a) If we're at a safepoint, operations on the master humongous
1411     // set should be invoked by either the VM thread (which will
1412     // serialize them) or by the GC workers while holding the
1413     // OldSets_lock.
1414     // (b) If we're not at a safepoint, operations on the master
1415     // humongous set should be invoked while holding the Heap_lock.
1416 
1417     if (SafepointSynchronize::is_at_safepoint()) {
1418       guarantee(Thread::current()->is_VM_thread() ||
1419                 OldSets_lock->owned_by_self(),
1420                 "master humongous set MT safety protocol at a safepoint");
1421     } else {
1422       guarantee(Heap_lock->owned_by_self(),
1423                 "master humongous set MT safety protocol outside a safepoint");
1424     }
1425   }
1426   bool is_correct_type(HeapRegion* hr) { return hr->is_humongous(); }
1427   const char* get_description() { return "Humongous Regions"; }
1428 };
1429 
1430 G1CollectedHeap::G1CollectedHeap() :
1431   CollectedHeap(),
1432   _service_thread(NULL),
1433   _periodic_gc_task(NULL),
1434   _free_segmented_array_memory_task(NULL),
1435   _workers(NULL),
1436   _card_table(NULL),
1437   _collection_pause_end(Ticks::now()),
1438   _soft_ref_policy(),
1439   _old_set("Old Region Set", new OldRegionSetChecker()),
1440   _archive_set("Archive Region Set", new ArchiveRegionSetChecker()),
1441   _humongous_set("Humongous Region Set", new HumongousRegionSetChecker()),
1442   _bot(NULL),
1443   _listener(),
1444   _numa(G1NUMA::create()),
1445   _hrm(),
1446   _allocator(NULL),
1447   _evac_failure_injector(),
1448   _verifier(NULL),
1449   _summary_bytes_used(0),
1450   _bytes_used_during_gc(0),
1451   _archive_allocator(nullptr),
1452   _survivor_evac_stats("Young", YoungPLABSize, PLABWeight),
1453   _old_evac_stats("Old", OldPLABSize, PLABWeight),
1454   _monitoring_support(nullptr),
1455   _humongous_reclaim_candidates(),
1456   _num_humongous_objects(0),
1457   _num_humongous_reclaim_candidates(0),
1458   _hr_printer(),
1459   _collector_state(),
1460   _old_marking_cycles_started(0),
1461   _old_marking_cycles_completed(0),
1462   _eden(),
1463   _survivor(),
1464   _gc_timer_stw(new (ResourceObj::C_HEAP, mtGC) STWGCTimer()),
1465   _gc_tracer_stw(new (ResourceObj::C_HEAP, mtGC) G1NewTracer()),
1466   _policy(new G1Policy(_gc_timer_stw)),
1467   _heap_sizing_policy(NULL),
1468   _collection_set(this, _policy),
1469   _hot_card_cache(NULL),
1470   _rem_set(NULL),
1471   _card_set_config(),
1472   _cm(NULL),
1473   _cm_thread(NULL),
1474   _cr(NULL),
1475   _task_queues(NULL),
1476   _ref_processor_stw(NULL),
1477   _is_alive_closure_stw(this),
1478   _is_subject_to_discovery_stw(this),
1479   _ref_processor_cm(NULL),
1480   _is_alive_closure_cm(this),
1481   _is_subject_to_discovery_cm(this),
1482   _region_attr() {
1483 
1484   _verifier = new G1HeapVerifier(this);
1485 
1486   _allocator = new G1Allocator(this);
1487 
1488   _heap_sizing_policy = G1HeapSizingPolicy::create(this, _policy->analytics());
1489 
1490   _humongous_object_threshold_in_words = humongous_threshold_for(HeapRegion::GrainWords);
1491 
1492   // Override the default _filler_array_max_size so that no humongous filler
1493   // objects are created.
1494   _filler_array_max_size = _humongous_object_threshold_in_words;
1495 
1496   uint n_queues = ParallelGCThreads;
1497   _task_queues = new G1ScannerTasksQueueSet(n_queues);
1498 
1499   for (uint i = 0; i < n_queues; i++) {
1500     G1ScannerTasksQueue* q = new G1ScannerTasksQueue();
1501     _task_queues->register_queue(i, q);
1502   }
1503 
1504   _gc_tracer_stw->initialize();
1505 
1506   guarantee(_task_queues != NULL, "task_queues allocation failure.");
1507 }
1508 
1509 G1RegionToSpaceMapper* G1CollectedHeap::create_aux_memory_mapper(const char* description,
1510                                                                  size_t size,
1511                                                                  size_t translation_factor) {
1512   size_t preferred_page_size = os::page_size_for_region_unaligned(size, 1);
1513   // Allocate a new reserved space, preferring to use large pages.
1514   ReservedSpace rs(size, preferred_page_size);
1515   size_t page_size = rs.page_size();
1516   G1RegionToSpaceMapper* result  =
1517     G1RegionToSpaceMapper::create_mapper(rs,
1518                                          size,
1519                                          page_size,
1520                                          HeapRegion::GrainBytes,
1521                                          translation_factor,
1522                                          mtGC);
1523 
1524   os::trace_page_sizes_for_requested_size(description,
1525                                           size,
1526                                           page_size,
1527                                           preferred_page_size,
1528                                           rs.base(),
1529                                           rs.size());
1530 
1531   return result;
1532 }
1533 
1534 jint G1CollectedHeap::initialize_concurrent_refinement() {
1535   jint ecode = JNI_OK;
1536   _cr = G1ConcurrentRefine::create(&ecode);
1537   return ecode;
1538 }
1539 
1540 jint G1CollectedHeap::initialize_service_thread() {
1541   _service_thread = new G1ServiceThread();
1542   if (_service_thread->osthread() == NULL) {
1543     vm_shutdown_during_initialization("Could not create G1ServiceThread");
1544     return JNI_ENOMEM;
1545   }
1546   return JNI_OK;
1547 }
1548 
1549 jint G1CollectedHeap::initialize() {
1550 
1551   // Necessary to satisfy locking discipline assertions.
1552 
1553   MutexLocker x(Heap_lock);
1554 
1555   // While there are no constraints in the GC code that HeapWordSize
1556   // be any particular value, there are multiple other areas in the
1557   // system which believe this to be true (e.g. oop->object_size in some
1558   // cases incorrectly returns the size in wordSize units rather than
1559   // HeapWordSize).
1560   guarantee(HeapWordSize == wordSize, "HeapWordSize must equal wordSize");
1561 
1562   size_t init_byte_size = InitialHeapSize;
1563   size_t reserved_byte_size = G1Arguments::heap_reserved_size_bytes();
1564 
1565   // Ensure that the sizes are properly aligned.
1566   Universe::check_alignment(init_byte_size, HeapRegion::GrainBytes, "g1 heap");
1567   Universe::check_alignment(reserved_byte_size, HeapRegion::GrainBytes, "g1 heap");
1568   Universe::check_alignment(reserved_byte_size, HeapAlignment, "g1 heap");
1569 
1570   // Reserve the maximum.
1571 
1572   // When compressed oops are enabled, the preferred heap base
1573   // is calculated by subtracting the requested size from the
1574   // 32Gb boundary and using the result as the base address for
1575   // heap reservation. If the requested size is not aligned to
1576   // HeapRegion::GrainBytes (i.e. the alignment that is passed
1577   // into the ReservedHeapSpace constructor) then the actual
1578   // base of the reserved heap may end up differing from the
1579   // address that was requested (i.e. the preferred heap base).
1580   // If this happens then we could end up using a non-optimal
1581   // compressed oops mode.
1582 
1583   ReservedHeapSpace heap_rs = Universe::reserve_heap(reserved_byte_size,
1584                                                      HeapAlignment);
1585 
1586   initialize_reserved_region(heap_rs);
1587 
1588   // Create the barrier set for the entire reserved region.
1589   G1CardTable* ct = new G1CardTable(heap_rs.region());
1590   ct->initialize();
1591   G1BarrierSet* bs = new G1BarrierSet(ct);
1592   bs->initialize();
1593   assert(bs->is_a(BarrierSet::G1BarrierSet), "sanity");
1594   BarrierSet::set_barrier_set(bs);
1595   _card_table = ct;
1596 
1597   {
1598     G1SATBMarkQueueSet& satbqs = bs->satb_mark_queue_set();
1599     satbqs.set_process_completed_buffers_threshold(G1SATBProcessCompletedThreshold);
1600     satbqs.set_buffer_enqueue_threshold_percentage(G1SATBBufferEnqueueingThresholdPercent);
1601   }
1602 
1603   // Create the hot card cache.
1604   _hot_card_cache = new G1HotCardCache(this);
1605 
1606   // Create space mappers.
1607   size_t page_size = heap_rs.page_size();
1608   G1RegionToSpaceMapper* heap_storage =
1609     G1RegionToSpaceMapper::create_mapper(heap_rs,
1610                                          heap_rs.size(),
1611                                          page_size,
1612                                          HeapRegion::GrainBytes,
1613                                          1,
1614                                          mtJavaHeap);
1615   if(heap_storage == NULL) {
1616     vm_shutdown_during_initialization("Could not initialize G1 heap");
1617     return JNI_ERR;
1618   }
1619 
1620   os::trace_page_sizes("Heap",
1621                        MinHeapSize,
1622                        reserved_byte_size,
1623                        page_size,
1624                        heap_rs.base(),
1625                        heap_rs.size());
1626   heap_storage->set_mapping_changed_listener(&_listener);
1627 
1628   // Create storage for the BOT, card table, card counts table (hot card cache) and the bitmaps.
1629   G1RegionToSpaceMapper* bot_storage =
1630     create_aux_memory_mapper("Block Offset Table",
1631                              G1BlockOffsetTable::compute_size(heap_rs.size() / HeapWordSize),
1632                              G1BlockOffsetTable::heap_map_factor());
1633 
1634   G1RegionToSpaceMapper* cardtable_storage =
1635     create_aux_memory_mapper("Card Table",
1636                              G1CardTable::compute_size(heap_rs.size() / HeapWordSize),
1637                              G1CardTable::heap_map_factor());
1638 
1639   G1RegionToSpaceMapper* card_counts_storage =
1640     create_aux_memory_mapper("Card Counts Table",
1641                              G1CardCounts::compute_size(heap_rs.size() / HeapWordSize),
1642                              G1CardCounts::heap_map_factor());
1643 
1644   size_t bitmap_size = G1CMBitMap::compute_size(heap_rs.size());
1645   G1RegionToSpaceMapper* prev_bitmap_storage =
1646     create_aux_memory_mapper("Prev Bitmap", bitmap_size, G1CMBitMap::heap_map_factor());
1647   G1RegionToSpaceMapper* next_bitmap_storage =
1648     create_aux_memory_mapper("Next Bitmap", bitmap_size, G1CMBitMap::heap_map_factor());
1649 
1650   _hrm.initialize(heap_storage, prev_bitmap_storage, next_bitmap_storage, bot_storage, cardtable_storage, card_counts_storage);
1651   _card_table->initialize(cardtable_storage);
1652 
1653   // Do later initialization work for concurrent refinement.
1654   _hot_card_cache->initialize(card_counts_storage);
1655 
1656   // 6843694 - ensure that the maximum region index can fit
1657   // in the remembered set structures.
1658   const uint max_region_idx = (1U << (sizeof(RegionIdx_t)*BitsPerByte-1)) - 1;
1659   guarantee((max_reserved_regions() - 1) <= max_region_idx, "too many regions");
1660 
1661   // The G1FromCardCache reserves card with value 0 as "invalid", so the heap must not
1662   // start within the first card.
1663   guarantee((uintptr_t)(heap_rs.base()) >= G1CardTable::card_size, "Java heap must not start within the first card.");
1664   G1FromCardCache::initialize(max_reserved_regions());
1665   // Also create a G1 rem set.
1666   _rem_set = new G1RemSet(this, _card_table, _hot_card_cache);
1667   _rem_set->initialize(max_reserved_regions());
1668 
1669   size_t max_cards_per_region = ((size_t)1 << (sizeof(CardIdx_t)*BitsPerByte-1)) - 1;
1670   guarantee(HeapRegion::CardsPerRegion > 0, "make sure it's initialized");
1671   guarantee(HeapRegion::CardsPerRegion < max_cards_per_region,
1672             "too many cards per region");
1673 
1674   HeapRegionRemSet::initialize(_reserved);
1675 
1676   FreeRegionList::set_unrealistically_long_length(max_regions() + 1);
1677 
1678   _bot = new G1BlockOffsetTable(reserved(), bot_storage);
1679 
1680   {
1681     size_t granularity = HeapRegion::GrainBytes;
1682 
1683     _region_attr.initialize(reserved(), granularity);
1684     _humongous_reclaim_candidates.initialize(reserved(), granularity);
1685   }
1686 
1687   _workers = new WorkerThreads("GC Thread", ParallelGCThreads);
1688   if (_workers == NULL) {
1689     return JNI_ENOMEM;
1690   }
1691   _workers->initialize_workers();
1692 
1693   _numa->set_region_info(HeapRegion::GrainBytes, page_size);
1694 
1695   // Create the G1ConcurrentMark data structure and thread.
1696   // (Must do this late, so that "max_[reserved_]regions" is defined.)
1697   _cm = new G1ConcurrentMark(this, prev_bitmap_storage, next_bitmap_storage);
1698   _cm_thread = _cm->cm_thread();
1699 
1700   // Now expand into the initial heap size.
1701   if (!expand(init_byte_size, _workers)) {
1702     vm_shutdown_during_initialization("Failed to allocate initial heap.");
1703     return JNI_ENOMEM;
1704   }
1705 
1706   // Perform any initialization actions delegated to the policy.
1707   policy()->init(this, &_collection_set);
1708 
1709   jint ecode = initialize_concurrent_refinement();
1710   if (ecode != JNI_OK) {
1711     return ecode;
1712   }
1713 
1714   ecode = initialize_service_thread();
1715   if (ecode != JNI_OK) {
1716     return ecode;
1717   }
1718 
1719   // Initialize and schedule sampling task on service thread.
1720   _rem_set->initialize_sampling_task(service_thread());
1721 
1722   // Create and schedule the periodic gc task on the service thread.
1723   _periodic_gc_task = new G1PeriodicGCTask("Periodic GC Task");
1724   _service_thread->register_task(_periodic_gc_task);
1725 
1726   _free_segmented_array_memory_task = new G1SegmentedArrayFreeMemoryTask("Card Set Free Memory Task");
1727   _service_thread->register_task(_free_segmented_array_memory_task);
1728 
1729   {
1730     G1DirtyCardQueueSet& dcqs = G1BarrierSet::dirty_card_queue_set();
1731     dcqs.set_process_cards_threshold(concurrent_refine()->yellow_zone());
1732     dcqs.set_max_cards(concurrent_refine()->red_zone());
1733   }
1734 
1735   // Here we allocate the dummy HeapRegion that is required by the
1736   // G1AllocRegion class.
1737   HeapRegion* dummy_region = _hrm.get_dummy_region();
1738 
1739   // We'll re-use the same region whether the alloc region will
1740   // require BOT updates or not and, if it doesn't, then a non-young
1741   // region will complain that it cannot support allocations without
1742   // BOT updates. So we'll tag the dummy region as eden to avoid that.
1743   dummy_region->set_eden();
1744   // Make sure it's full.
1745   dummy_region->set_top(dummy_region->end());
1746   G1AllocRegion::setup(this, dummy_region);
1747 
1748   _allocator->init_mutator_alloc_regions();
1749 
1750   // Do create of the monitoring and management support so that
1751   // values in the heap have been properly initialized.
1752   _monitoring_support = new G1MonitoringSupport(this);
1753 
1754   _collection_set.initialize(max_reserved_regions());
1755 
1756   evac_failure_injector()->reset();
1757 
1758   G1InitLogger::print();
1759 
1760   return JNI_OK;
1761 }
1762 
1763 bool G1CollectedHeap::concurrent_mark_is_terminating() const {
1764   return _cm_thread->should_terminate();
1765 }
1766 
1767 void G1CollectedHeap::stop() {
1768   // Stop all concurrent threads. We do this to make sure these threads
1769   // do not continue to execute and access resources (e.g. logging)
1770   // that are destroyed during shutdown.
1771   _cr->stop();
1772   _service_thread->stop();
1773   _cm_thread->stop();
1774 }
1775 
1776 void G1CollectedHeap::safepoint_synchronize_begin() {
1777   SuspendibleThreadSet::synchronize();
1778 }
1779 
1780 void G1CollectedHeap::safepoint_synchronize_end() {
1781   SuspendibleThreadSet::desynchronize();
1782 }
1783 
1784 void G1CollectedHeap::post_initialize() {
1785   CollectedHeap::post_initialize();
1786   ref_processing_init();
1787 }
1788 
1789 void G1CollectedHeap::ref_processing_init() {
1790   // Reference processing in G1 currently works as follows:
1791   //
1792   // * There are two reference processor instances. One is
1793   //   used to record and process discovered references
1794   //   during concurrent marking; the other is used to
1795   //   record and process references during STW pauses
1796   //   (both full and incremental).
1797   // * Both ref processors need to 'span' the entire heap as
1798   //   the regions in the collection set may be dotted around.
1799   //
1800   // * For the concurrent marking ref processor:
1801   //   * Reference discovery is enabled at concurrent start.
1802   //   * Reference discovery is disabled and the discovered
1803   //     references processed etc during remarking.
1804   //   * Reference discovery is MT (see below).
1805   //   * Reference discovery requires a barrier (see below).
1806   //   * Reference processing may or may not be MT
1807   //     (depending on the value of ParallelRefProcEnabled
1808   //     and ParallelGCThreads).
1809   //   * A full GC disables reference discovery by the CM
1810   //     ref processor and abandons any entries on it's
1811   //     discovered lists.
1812   //
1813   // * For the STW processor:
1814   //   * Non MT discovery is enabled at the start of a full GC.
1815   //   * Processing and enqueueing during a full GC is non-MT.
1816   //   * During a full GC, references are processed after marking.
1817   //
1818   //   * Discovery (may or may not be MT) is enabled at the start
1819   //     of an incremental evacuation pause.
1820   //   * References are processed near the end of a STW evacuation pause.
1821   //   * For both types of GC:
1822   //     * Discovery is atomic - i.e. not concurrent.
1823   //     * Reference discovery will not need a barrier.
1824 
1825   // Concurrent Mark ref processor
1826   _ref_processor_cm =
1827     new ReferenceProcessor(&_is_subject_to_discovery_cm,
1828                            ParallelGCThreads,                              // degree of mt processing
1829                            // We discover with the gc worker threads during Remark, so both
1830                            // thread counts must be considered for discovery.
1831                            MAX2(ParallelGCThreads, ConcGCThreads),         // degree of mt discovery
1832                            true,                                           // Reference discovery is concurrent
1833                            &_is_alive_closure_cm);                         // is alive closure
1834 
1835   // STW ref processor
1836   _ref_processor_stw =
1837     new ReferenceProcessor(&_is_subject_to_discovery_stw,
1838                            ParallelGCThreads,                    // degree of mt processing
1839                            ParallelGCThreads,                    // degree of mt discovery
1840                            false,                                // Reference discovery is not concurrent
1841                            &_is_alive_closure_stw);              // is alive closure
1842 }
1843 
1844 SoftRefPolicy* G1CollectedHeap::soft_ref_policy() {
1845   return &_soft_ref_policy;
1846 }
1847 
1848 size_t G1CollectedHeap::capacity() const {
1849   return _hrm.length() * HeapRegion::GrainBytes;
1850 }
1851 
1852 size_t G1CollectedHeap::unused_committed_regions_in_bytes() const {
1853   return _hrm.total_free_bytes();
1854 }
1855 
1856 void G1CollectedHeap::iterate_hcc_closure(G1CardTableEntryClosure* cl, uint worker_id) {
1857   _hot_card_cache->drain(cl, worker_id);
1858 }
1859 
1860 // Computes the sum of the storage used by the various regions.
1861 size_t G1CollectedHeap::used() const {
1862   size_t result = _summary_bytes_used + _allocator->used_in_alloc_regions();
1863   assert(_archive_allocator == nullptr, "must be, should not contribute to used");
1864   return result;
1865 }
1866 
1867 size_t G1CollectedHeap::used_unlocked() const {
1868   return _summary_bytes_used;
1869 }
1870 
1871 class SumUsedClosure: public HeapRegionClosure {
1872   size_t _used;
1873 public:
1874   SumUsedClosure() : _used(0) {}
1875   bool do_heap_region(HeapRegion* r) {
1876     _used += r->used();
1877     return false;
1878   }
1879   size_t result() { return _used; }
1880 };
1881 
1882 size_t G1CollectedHeap::recalculate_used() const {
1883   SumUsedClosure blk;
1884   heap_region_iterate(&blk);
1885   return blk.result();
1886 }
1887 
1888 bool  G1CollectedHeap::is_user_requested_concurrent_full_gc(GCCause::Cause cause) {
1889   switch (cause) {
1890     case GCCause::_java_lang_system_gc:                 return ExplicitGCInvokesConcurrent;
1891     case GCCause::_dcmd_gc_run:                         return ExplicitGCInvokesConcurrent;
1892     case GCCause::_wb_conc_mark:                        return true;
1893     default :                                           return false;
1894   }
1895 }
1896 
1897 bool G1CollectedHeap::should_do_concurrent_full_gc(GCCause::Cause cause) {
1898   switch (cause) {
1899     case GCCause::_g1_humongous_allocation: return true;
1900     case GCCause::_g1_periodic_collection:  return G1PeriodicGCInvokesConcurrent;
1901     case GCCause::_wb_breakpoint:           return true;
1902     default:                                return is_user_requested_concurrent_full_gc(cause);
1903   }
1904 }
1905 
1906 #ifndef PRODUCT
1907 void G1CollectedHeap::allocate_dummy_regions() {
1908   // Let's fill up most of the region
1909   size_t word_size = HeapRegion::GrainWords - 1024;
1910   // And as a result the region we'll allocate will be humongous.
1911   guarantee(is_humongous(word_size), "sanity");
1912 
1913   // _filler_array_max_size is set to humongous object threshold
1914   // but temporarily change it to use CollectedHeap::fill_with_object().
1915   AutoModifyRestore<size_t> temporarily(_filler_array_max_size, word_size);
1916 
1917   for (uintx i = 0; i < G1DummyRegionsPerGC; ++i) {
1918     // Let's use the existing mechanism for the allocation
1919     HeapWord* dummy_obj = humongous_obj_allocate(word_size);
1920     if (dummy_obj != NULL) {
1921       MemRegion mr(dummy_obj, word_size);
1922       CollectedHeap::fill_with_object(mr);
1923     } else {
1924       // If we can't allocate once, we probably cannot allocate
1925       // again. Let's get out of the loop.
1926       break;
1927     }
1928   }
1929 }
1930 #endif // !PRODUCT
1931 
1932 void G1CollectedHeap::increment_old_marking_cycles_started() {
1933   assert(_old_marking_cycles_started == _old_marking_cycles_completed ||
1934          _old_marking_cycles_started == _old_marking_cycles_completed + 1,
1935          "Wrong marking cycle count (started: %d, completed: %d)",
1936          _old_marking_cycles_started, _old_marking_cycles_completed);
1937 
1938   _old_marking_cycles_started++;
1939 }
1940 
1941 void G1CollectedHeap::increment_old_marking_cycles_completed(bool concurrent,
1942                                                              bool whole_heap_examined) {
1943   MonitorLocker ml(G1OldGCCount_lock, Mutex::_no_safepoint_check_flag);
1944 
1945   // We assume that if concurrent == true, then the caller is a
1946   // concurrent thread that was joined the Suspendible Thread
1947   // Set. If there's ever a cheap way to check this, we should add an
1948   // assert here.
1949 
1950   // Given that this method is called at the end of a Full GC or of a
1951   // concurrent cycle, and those can be nested (i.e., a Full GC can
1952   // interrupt a concurrent cycle), the number of full collections
1953   // completed should be either one (in the case where there was no
1954   // nesting) or two (when a Full GC interrupted a concurrent cycle)
1955   // behind the number of full collections started.
1956 
1957   // This is the case for the inner caller, i.e. a Full GC.
1958   assert(concurrent ||
1959          (_old_marking_cycles_started == _old_marking_cycles_completed + 1) ||
1960          (_old_marking_cycles_started == _old_marking_cycles_completed + 2),
1961          "for inner caller (Full GC): _old_marking_cycles_started = %u "
1962          "is inconsistent with _old_marking_cycles_completed = %u",
1963          _old_marking_cycles_started, _old_marking_cycles_completed);
1964 
1965   // This is the case for the outer caller, i.e. the concurrent cycle.
1966   assert(!concurrent ||
1967          (_old_marking_cycles_started == _old_marking_cycles_completed + 1),
1968          "for outer caller (concurrent cycle): "
1969          "_old_marking_cycles_started = %u "
1970          "is inconsistent with _old_marking_cycles_completed = %u",
1971          _old_marking_cycles_started, _old_marking_cycles_completed);
1972 
1973   _old_marking_cycles_completed += 1;
1974   if (whole_heap_examined) {
1975     // Signal that we have completed a visit to all live objects.
1976     record_whole_heap_examined_timestamp();
1977   }
1978 
1979   // We need to clear the "in_progress" flag in the CM thread before
1980   // we wake up any waiters (especially when ExplicitInvokesConcurrent
1981   // is set) so that if a waiter requests another System.gc() it doesn't
1982   // incorrectly see that a marking cycle is still in progress.
1983   if (concurrent) {
1984     _cm_thread->set_idle();
1985   }
1986 
1987   // Notify threads waiting in System.gc() (with ExplicitGCInvokesConcurrent)
1988   // for a full GC to finish that their wait is over.
1989   ml.notify_all();
1990 }
1991 
1992 // Helper for collect().
1993 static G1GCCounters collection_counters(G1CollectedHeap* g1h) {
1994   MutexLocker ml(Heap_lock);
1995   return G1GCCounters(g1h);
1996 }
1997 
1998 void G1CollectedHeap::collect(GCCause::Cause cause) {
1999   try_collect(cause, collection_counters(this));
2000 }
2001 
2002 // Return true if (x < y) with allowance for wraparound.
2003 static bool gc_counter_less_than(uint x, uint y) {
2004   return (x - y) > (UINT_MAX/2);
2005 }
2006 
2007 // LOG_COLLECT_CONCURRENTLY(cause, msg, args...)
2008 // Macro so msg printing is format-checked.
2009 #define LOG_COLLECT_CONCURRENTLY(cause, ...)                            \
2010   do {                                                                  \
2011     LogTarget(Trace, gc) LOG_COLLECT_CONCURRENTLY_lt;                   \
2012     if (LOG_COLLECT_CONCURRENTLY_lt.is_enabled()) {                     \
2013       ResourceMark rm; /* For thread name. */                           \
2014       LogStream LOG_COLLECT_CONCURRENTLY_s(&LOG_COLLECT_CONCURRENTLY_lt); \
2015       LOG_COLLECT_CONCURRENTLY_s.print("%s: Try Collect Concurrently (%s): ", \
2016                                        Thread::current()->name(),       \
2017                                        GCCause::to_string(cause));      \
2018       LOG_COLLECT_CONCURRENTLY_s.print(__VA_ARGS__);                    \
2019     }                                                                   \
2020   } while (0)
2021 
2022 #define LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, result) \
2023   LOG_COLLECT_CONCURRENTLY(cause, "complete %s", BOOL_TO_STR(result))
2024 
2025 bool G1CollectedHeap::try_collect_concurrently(GCCause::Cause cause,
2026                                                uint gc_counter,
2027                                                uint old_marking_started_before) {
2028   assert_heap_not_locked();
2029   assert(should_do_concurrent_full_gc(cause),
2030          "Non-concurrent cause %s", GCCause::to_string(cause));
2031 
2032   for (uint i = 1; true; ++i) {
2033     // Try to schedule concurrent start evacuation pause that will
2034     // start a concurrent cycle.
2035     LOG_COLLECT_CONCURRENTLY(cause, "attempt %u", i);
2036     VM_G1TryInitiateConcMark op(gc_counter,
2037                                 cause,
2038                                 policy()->max_pause_time_ms());
2039     VMThread::execute(&op);
2040 
2041     // Request is trivially finished.
2042     if (cause == GCCause::_g1_periodic_collection) {
2043       LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, op.gc_succeeded());
2044       return op.gc_succeeded();
2045     }
2046 
2047     // If VMOp skipped initiating concurrent marking cycle because
2048     // we're terminating, then we're done.
2049     if (op.terminating()) {
2050       LOG_COLLECT_CONCURRENTLY(cause, "skipped: terminating");
2051       return false;
2052     }
2053 
2054     // Lock to get consistent set of values.
2055     uint old_marking_started_after;
2056     uint old_marking_completed_after;
2057     {
2058       MutexLocker ml(Heap_lock);
2059       // Update gc_counter for retrying VMOp if needed. Captured here to be
2060       // consistent with the values we use below for termination tests.  If
2061       // a retry is needed after a possible wait, and another collection
2062       // occurs in the meantime, it will cause our retry to be skipped and
2063       // we'll recheck for termination with updated conditions from that
2064       // more recent collection.  That's what we want, rather than having
2065       // our retry possibly perform an unnecessary collection.
2066       gc_counter = total_collections();
2067       old_marking_started_after = _old_marking_cycles_started;
2068       old_marking_completed_after = _old_marking_cycles_completed;
2069     }
2070 
2071     if (cause == GCCause::_wb_breakpoint) {
2072       if (op.gc_succeeded()) {
2073         LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, true);
2074         return true;
2075       }
2076       // When _wb_breakpoint there can't be another cycle or deferred.
2077       assert(!op.cycle_already_in_progress(), "invariant");
2078       assert(!op.whitebox_attached(), "invariant");
2079       // Concurrent cycle attempt might have been cancelled by some other
2080       // collection, so retry.  Unlike other cases below, we want to retry
2081       // even if cancelled by a STW full collection, because we really want
2082       // to start a concurrent cycle.
2083       if (old_marking_started_before != old_marking_started_after) {
2084         LOG_COLLECT_CONCURRENTLY(cause, "ignoring STW full GC");
2085         old_marking_started_before = old_marking_started_after;
2086       }
2087     } else if (!GCCause::is_user_requested_gc(cause)) {
2088       // For an "automatic" (not user-requested) collection, we just need to
2089       // ensure that progress is made.
2090       //
2091       // Request is finished if any of
2092       // (1) the VMOp successfully performed a GC,
2093       // (2) a concurrent cycle was already in progress,
2094       // (3) whitebox is controlling concurrent cycles,
2095       // (4) a new cycle was started (by this thread or some other), or
2096       // (5) a Full GC was performed.
2097       // Cases (4) and (5) are detected together by a change to
2098       // _old_marking_cycles_started.
2099       //
2100       // Note that (1) does not imply (4).  If we're still in the mixed
2101       // phase of an earlier concurrent collection, the request to make the
2102       // collection a concurrent start won't be honored.  If we don't check for
2103       // both conditions we'll spin doing back-to-back collections.
2104       if (op.gc_succeeded() ||
2105           op.cycle_already_in_progress() ||
2106           op.whitebox_attached() ||
2107           (old_marking_started_before != old_marking_started_after)) {
2108         LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, true);
2109         return true;
2110       }
2111     } else {                    // User-requested GC.
2112       // For a user-requested collection, we want to ensure that a complete
2113       // full collection has been performed before returning, but without
2114       // waiting for more than needed.
2115 
2116       // For user-requested GCs (unlike non-UR), a successful VMOp implies a
2117       // new cycle was started.  That's good, because it's not clear what we
2118       // should do otherwise.  Trying again just does back to back GCs.
2119       // Can't wait for someone else to start a cycle.  And returning fails
2120       // to meet the goal of ensuring a full collection was performed.
2121       assert(!op.gc_succeeded() ||
2122              (old_marking_started_before != old_marking_started_after),
2123              "invariant: succeeded %s, started before %u, started after %u",
2124              BOOL_TO_STR(op.gc_succeeded()),
2125              old_marking_started_before, old_marking_started_after);
2126 
2127       // Request is finished if a full collection (concurrent or stw)
2128       // was started after this request and has completed, e.g.
2129       // started_before < completed_after.
2130       if (gc_counter_less_than(old_marking_started_before,
2131                                old_marking_completed_after)) {
2132         LOG_COLLECT_CONCURRENTLY_COMPLETE(cause, true);
2133         return true;
2134       }
2135 
2136       if (old_marking_started_after != old_marking_completed_after) {
2137         // If there is an in-progress cycle (possibly started by us), then
2138         // wait for that cycle to complete, e.g.
2139         // while completed_now < started_after.
2140         LOG_COLLECT_CONCURRENTLY(cause, "wait");
2141         MonitorLocker ml(G1OldGCCount_lock);
2142         while (gc_counter_less_than(_old_marking_cycles_completed,
2143                                     old_marking_started_after)) {
2144           ml.wait();
2145         }
2146         // Request is finished if the collection we just waited for was
2147         // started after this request.
2148         if (old_marking_started_before != old_marking_started_after) {
2149           LOG_COLLECT_CONCURRENTLY(cause, "complete after wait");
2150           return true;
2151         }
2152       }
2153 
2154       // If VMOp was successful then it started a new cycle that the above
2155       // wait &etc should have recognized as finishing this request.  This
2156       // differs from a non-user-request, where gc_succeeded does not imply
2157       // a new cycle was started.
2158       assert(!op.gc_succeeded(), "invariant");
2159 
2160       if (op.cycle_already_in_progress()) {
2161         // If VMOp failed because a cycle was already in progress, it
2162         // is now complete.  But it didn't finish this user-requested
2163         // GC, so try again.
2164         LOG_COLLECT_CONCURRENTLY(cause, "retry after in-progress");
2165         continue;
2166       } else if (op.whitebox_attached()) {
2167         // If WhiteBox wants control, wait for notification of a state
2168         // change in the controller, then try again.  Don't wait for
2169         // release of control, since collections may complete while in
2170         // control.  Note: This won't recognize a STW full collection
2171         // while waiting; we can't wait on multiple monitors.
2172         LOG_COLLECT_CONCURRENTLY(cause, "whitebox control stall");
2173         MonitorLocker ml(ConcurrentGCBreakpoints::monitor());
2174         if (ConcurrentGCBreakpoints::is_controlled()) {
2175           ml.wait();
2176         }
2177         continue;
2178       }
2179     }
2180 
2181     // Collection failed and should be retried.
2182     assert(op.transient_failure(), "invariant");
2183 
2184     if (GCLocker::is_active_and_needs_gc()) {
2185       // If GCLocker is active, wait until clear before retrying.
2186       LOG_COLLECT_CONCURRENTLY(cause, "gc-locker stall");
2187       GCLocker::stall_until_clear();
2188     }
2189 
2190     LOG_COLLECT_CONCURRENTLY(cause, "retry");
2191   }
2192 }
2193 
2194 bool G1CollectedHeap::try_collect(GCCause::Cause cause,
2195                                   const G1GCCounters& counters_before) {
2196   if (should_do_concurrent_full_gc(cause)) {
2197     return try_collect_concurrently(cause,
2198                                     counters_before.total_collections(),
2199                                     counters_before.old_marking_cycles_started());
2200   } else if (GCLocker::should_discard(cause, counters_before.total_collections())) {
2201     // Indicate failure to be consistent with VMOp failure due to
2202     // another collection slipping in after our gc_count but before
2203     // our request is processed.
2204     return false;
2205   } else if (cause == GCCause::_gc_locker || cause == GCCause::_wb_young_gc
2206              DEBUG_ONLY(|| cause == GCCause::_scavenge_alot)) {
2207 
2208     // Schedule a standard evacuation pause. We're setting word_size
2209     // to 0 which means that we are not requesting a post-GC allocation.
2210     VM_G1CollectForAllocation op(0,     /* word_size */
2211                                  counters_before.total_collections(),
2212                                  cause,
2213                                  policy()->max_pause_time_ms());
2214     VMThread::execute(&op);
2215     return op.gc_succeeded();
2216   } else {
2217     // Schedule a Full GC.
2218     VM_G1CollectFull op(counters_before.total_collections(),
2219                         counters_before.total_full_collections(),
2220                         cause);
2221     VMThread::execute(&op);
2222     return op.gc_succeeded();
2223   }
2224 }
2225 
2226 void G1CollectedHeap::start_concurrent_gc_for_metadata_allocation(GCCause::Cause gc_cause) {
2227   GCCauseSetter x(this, gc_cause);
2228 
2229   // At this point we are supposed to start a concurrent cycle. We
2230   // will do so if one is not already in progress.
2231   bool should_start = policy()->force_concurrent_start_if_outside_cycle(gc_cause);
2232   if (should_start) {
2233     double pause_target = policy()->max_pause_time_ms();
2234     do_collection_pause_at_safepoint(pause_target);
2235   }
2236 }
2237 
2238 bool G1CollectedHeap::is_in(const void* p) const {
2239   return is_in_reserved(p) && _hrm.is_available(addr_to_region((HeapWord*)p));
2240 }
2241 
2242 // Iteration functions.
2243 
2244 // Iterates an ObjectClosure over all objects within a HeapRegion.
2245 
2246 class IterateObjectClosureRegionClosure: public HeapRegionClosure {
2247   ObjectClosure* _cl;
2248 public:
2249   IterateObjectClosureRegionClosure(ObjectClosure* cl) : _cl(cl) {}
2250   bool do_heap_region(HeapRegion* r) {
2251     if (!r->is_continues_humongous()) {
2252       r->object_iterate(_cl);
2253     }
2254     return false;
2255   }
2256 };
2257 
2258 void G1CollectedHeap::object_iterate(ObjectClosure* cl) {
2259   IterateObjectClosureRegionClosure blk(cl);
2260   heap_region_iterate(&blk);
2261 }
2262 
2263 class G1ParallelObjectIterator : public ParallelObjectIteratorImpl {
2264 private:
2265   G1CollectedHeap*  _heap;
2266   HeapRegionClaimer _claimer;
2267 
2268 public:
2269   G1ParallelObjectIterator(uint thread_num) :
2270       _heap(G1CollectedHeap::heap()),
2271       _claimer(thread_num == 0 ? G1CollectedHeap::heap()->workers()->active_workers() : thread_num) {}
2272 
2273   virtual void object_iterate(ObjectClosure* cl, uint worker_id) {
2274     _heap->object_iterate_parallel(cl, worker_id, &_claimer);
2275   }
2276 };
2277 
2278 ParallelObjectIteratorImpl* G1CollectedHeap::parallel_object_iterator(uint thread_num) {
2279   return new G1ParallelObjectIterator(thread_num);
2280 }
2281 
2282 void G1CollectedHeap::object_iterate_parallel(ObjectClosure* cl, uint worker_id, HeapRegionClaimer* claimer) {
2283   IterateObjectClosureRegionClosure blk(cl);
2284   heap_region_par_iterate_from_worker_offset(&blk, claimer, worker_id);
2285 }
2286 
2287 void G1CollectedHeap::keep_alive(oop obj) {
2288   G1BarrierSet::enqueue(obj);
2289 }
2290 
2291 void G1CollectedHeap::heap_region_iterate(HeapRegionClosure* cl) const {
2292   _hrm.iterate(cl);
2293 }
2294 
2295 void G1CollectedHeap::heap_region_par_iterate_from_worker_offset(HeapRegionClosure* cl,
2296                                                                  HeapRegionClaimer *hrclaimer,
2297                                                                  uint worker_id) const {
2298   _hrm.par_iterate(cl, hrclaimer, hrclaimer->offset_for_worker(worker_id));
2299 }
2300 
2301 void G1CollectedHeap::heap_region_par_iterate_from_start(HeapRegionClosure* cl,
2302                                                          HeapRegionClaimer *hrclaimer) const {
2303   _hrm.par_iterate(cl, hrclaimer, 0);
2304 }
2305 
2306 void G1CollectedHeap::collection_set_iterate_all(HeapRegionClosure* cl) {
2307   _collection_set.iterate(cl);
2308 }
2309 
2310 void G1CollectedHeap::collection_set_par_iterate_all(HeapRegionClosure* cl,
2311                                                      HeapRegionClaimer* hr_claimer,
2312                                                      uint worker_id) {
2313   _collection_set.par_iterate(cl, hr_claimer, worker_id);
2314 }
2315 
2316 void G1CollectedHeap::collection_set_iterate_increment_from(HeapRegionClosure *cl,
2317                                                             HeapRegionClaimer* hr_claimer,
2318                                                             uint worker_id) {
2319   _collection_set.iterate_incremental_part_from(cl, hr_claimer, worker_id);
2320 }
2321 
2322 void G1CollectedHeap::par_iterate_regions_array(HeapRegionClosure* cl,
2323                                                 HeapRegionClaimer* hr_claimer,
2324                                                 const uint regions[],
2325                                                 size_t length,
2326                                                 uint worker_id) const {
2327   assert_at_safepoint();
2328   if (length == 0) {
2329     return;
2330   }
2331   uint total_workers = workers()->active_workers();
2332 
2333   size_t start_pos = (worker_id * length) / total_workers;
2334   size_t cur_pos = start_pos;
2335 
2336   do {
2337     uint region_idx = regions[cur_pos];
2338     if (hr_claimer == NULL || hr_claimer->claim_region(region_idx)) {
2339       HeapRegion* r = region_at(region_idx);
2340       bool result = cl->do_heap_region(r);
2341       guarantee(!result, "Must not cancel iteration");
2342     }
2343 
2344     cur_pos++;
2345     if (cur_pos == length) {
2346       cur_pos = 0;
2347     }
2348   } while (cur_pos != start_pos);
2349 }
2350 
2351 HeapWord* G1CollectedHeap::block_start(const void* addr) const {
2352   HeapRegion* hr = heap_region_containing(addr);
2353   return hr->block_start(addr);
2354 }
2355 
2356 bool G1CollectedHeap::block_is_obj(const HeapWord* addr) const {
2357   HeapRegion* hr = heap_region_containing(addr);
2358   return hr->block_is_obj(addr);
2359 }
2360 
2361 size_t G1CollectedHeap::tlab_capacity(Thread* ignored) const {
2362   return (_policy->young_list_target_length() - _survivor.length()) * HeapRegion::GrainBytes;
2363 }
2364 
2365 size_t G1CollectedHeap::tlab_used(Thread* ignored) const {
2366   return _eden.length() * HeapRegion::GrainBytes;
2367 }
2368 
2369 // For G1 TLABs should not contain humongous objects, so the maximum TLAB size
2370 // must be equal to the humongous object limit.
2371 size_t G1CollectedHeap::max_tlab_size() const {
2372   return align_down(_humongous_object_threshold_in_words, MinObjAlignment);
2373 }
2374 
2375 size_t G1CollectedHeap::unsafe_max_tlab_alloc(Thread* ignored) const {
2376   return _allocator->unsafe_max_tlab_alloc();
2377 }
2378 
2379 size_t G1CollectedHeap::max_capacity() const {
2380   return max_regions() * HeapRegion::GrainBytes;
2381 }
2382 
2383 void G1CollectedHeap::prepare_for_verify() {
2384   _verifier->prepare_for_verify();
2385 }
2386 
2387 void G1CollectedHeap::verify(VerifyOption vo) {
2388   _verifier->verify(vo);
2389 }
2390 
2391 bool G1CollectedHeap::supports_concurrent_gc_breakpoints() const {
2392   return true;
2393 }
2394 
2395 bool G1CollectedHeap::is_archived_object(oop object) const {
2396   return object != NULL && heap_region_containing(object)->is_archive();
2397 }
2398 
2399 class PrintRegionClosure: public HeapRegionClosure {
2400   outputStream* _st;
2401 public:
2402   PrintRegionClosure(outputStream* st) : _st(st) {}
2403   bool do_heap_region(HeapRegion* r) {
2404     r->print_on(_st);
2405     return false;
2406   }
2407 };
2408 
2409 bool G1CollectedHeap::is_obj_dead_cond(const oop obj,
2410                                        const HeapRegion* hr,
2411                                        const VerifyOption vo) const {
2412   switch (vo) {
2413   case VerifyOption_G1UsePrevMarking: return is_obj_dead(obj, hr);
2414   case VerifyOption_G1UseNextMarking: return is_obj_ill(obj, hr);
2415   case VerifyOption_G1UseFullMarking: return is_obj_dead_full(obj, hr);
2416   default:                            ShouldNotReachHere();
2417   }
2418   return false; // keep some compilers happy
2419 }
2420 
2421 bool G1CollectedHeap::is_obj_dead_cond(const oop obj,
2422                                        const VerifyOption vo) const {
2423   switch (vo) {
2424   case VerifyOption_G1UsePrevMarking: return is_obj_dead(obj);
2425   case VerifyOption_G1UseNextMarking: return is_obj_ill(obj);
2426   case VerifyOption_G1UseFullMarking: return is_obj_dead_full(obj);
2427   default:                            ShouldNotReachHere();
2428   }
2429   return false; // keep some compilers happy
2430 }
2431 
2432 void G1CollectedHeap::print_heap_regions() const {
2433   LogTarget(Trace, gc, heap, region) lt;
2434   if (lt.is_enabled()) {
2435     LogStream ls(lt);
2436     print_regions_on(&ls);
2437   }
2438 }
2439 
2440 void G1CollectedHeap::print_on(outputStream* st) const {
2441   size_t heap_used = Heap_lock->owned_by_self() ? used() : used_unlocked();
2442   st->print(" %-20s", "garbage-first heap");
2443   st->print(" total " SIZE_FORMAT "K, used " SIZE_FORMAT "K",
2444             capacity()/K, heap_used/K);
2445   st->print(" [" PTR_FORMAT ", " PTR_FORMAT ")",
2446             p2i(_hrm.reserved().start()),
2447             p2i(_hrm.reserved().end()));
2448   st->cr();
2449   st->print("  region size " SIZE_FORMAT "K, ", HeapRegion::GrainBytes / K);
2450   uint young_regions = young_regions_count();
2451   st->print("%u young (" SIZE_FORMAT "K), ", young_regions,
2452             (size_t) young_regions * HeapRegion::GrainBytes / K);
2453   uint survivor_regions = survivor_regions_count();
2454   st->print("%u survivors (" SIZE_FORMAT "K)", survivor_regions,
2455             (size_t) survivor_regions * HeapRegion::GrainBytes / K);
2456   st->cr();
2457   if (_numa->is_enabled()) {
2458     uint num_nodes = _numa->num_active_nodes();
2459     st->print("  remaining free region(s) on each NUMA node: ");
2460     const int* node_ids = _numa->node_ids();
2461     for (uint node_index = 0; node_index < num_nodes; node_index++) {
2462       uint num_free_regions = _hrm.num_free_regions(node_index);
2463       st->print("%d=%u ", node_ids[node_index], num_free_regions);
2464     }
2465     st->cr();
2466   }
2467   MetaspaceUtils::print_on(st);
2468 }
2469 
2470 void G1CollectedHeap::print_regions_on(outputStream* st) const {
2471   st->print_cr("Heap Regions: E=young(eden), S=young(survivor), O=old, "
2472                "HS=humongous(starts), HC=humongous(continues), "
2473                "CS=collection set, F=free, "
2474                "OA=open archive, CA=closed archive, "
2475                "TAMS=top-at-mark-start (previous, next)");
2476   PrintRegionClosure blk(st);
2477   heap_region_iterate(&blk);
2478 }
2479 
2480 void G1CollectedHeap::print_extended_on(outputStream* st) const {
2481   print_on(st);
2482 
2483   // Print the per-region information.
2484   st->cr();
2485   print_regions_on(st);
2486 }
2487 
2488 void G1CollectedHeap::print_on_error(outputStream* st) const {
2489   this->CollectedHeap::print_on_error(st);
2490 
2491   if (_cm != NULL) {
2492     st->cr();
2493     _cm->print_on_error(st);
2494   }
2495 }
2496 
2497 void G1CollectedHeap::gc_threads_do(ThreadClosure* tc) const {
2498   workers()->threads_do(tc);
2499   tc->do_thread(_cm_thread);
2500   _cm->threads_do(tc);
2501   _cr->threads_do(tc);
2502   tc->do_thread(_service_thread);
2503 }
2504 
2505 void G1CollectedHeap::print_tracing_info() const {
2506   rem_set()->print_summary_info();
2507   concurrent_mark()->print_summary_info();
2508 }
2509 
2510 #ifndef PRODUCT
2511 // Helpful for debugging RSet issues.
2512 
2513 class PrintRSetsClosure : public HeapRegionClosure {
2514 private:
2515   const char* _msg;
2516   size_t _occupied_sum;
2517 
2518 public:
2519   bool do_heap_region(HeapRegion* r) {
2520     HeapRegionRemSet* hrrs = r->rem_set();
2521     size_t occupied = hrrs->occupied();
2522     _occupied_sum += occupied;
2523 
2524     tty->print_cr("Printing RSet for region " HR_FORMAT, HR_FORMAT_PARAMS(r));
2525     if (occupied == 0) {
2526       tty->print_cr("  RSet is empty");
2527     } else {
2528       hrrs->print();
2529     }
2530     tty->print_cr("----------");
2531     return false;
2532   }
2533 
2534   PrintRSetsClosure(const char* msg) : _msg(msg), _occupied_sum(0) {
2535     tty->cr();
2536     tty->print_cr("========================================");
2537     tty->print_cr("%s", msg);
2538     tty->cr();
2539   }
2540 
2541   ~PrintRSetsClosure() {
2542     tty->print_cr("Occupied Sum: " SIZE_FORMAT, _occupied_sum);
2543     tty->print_cr("========================================");
2544     tty->cr();
2545   }
2546 };
2547 
2548 void G1CollectedHeap::print_cset_rsets() {
2549   PrintRSetsClosure cl("Printing CSet RSets");
2550   collection_set_iterate_all(&cl);
2551 }
2552 
2553 void G1CollectedHeap::print_all_rsets() {
2554   PrintRSetsClosure cl("Printing All RSets");;
2555   heap_region_iterate(&cl);
2556 }
2557 #endif // PRODUCT
2558 
2559 bool G1CollectedHeap::print_location(outputStream* st, void* addr) const {
2560   return BlockLocationPrinter<G1CollectedHeap>::print_location(st, addr);
2561 }
2562 
2563 G1HeapSummary G1CollectedHeap::create_g1_heap_summary() {
2564 
2565   size_t eden_used_bytes = _eden.used_bytes();
2566   size_t survivor_used_bytes = _survivor.used_bytes();
2567   size_t heap_used = Heap_lock->owned_by_self() ? used() : used_unlocked();
2568 
2569   size_t eden_capacity_bytes =
2570     (policy()->young_list_target_length() * HeapRegion::GrainBytes) - survivor_used_bytes;
2571 
2572   VirtualSpaceSummary heap_summary = create_heap_space_summary();
2573   return G1HeapSummary(heap_summary, heap_used, eden_used_bytes,
2574                        eden_capacity_bytes, survivor_used_bytes, num_regions());
2575 }
2576 
2577 G1EvacSummary G1CollectedHeap::create_g1_evac_summary(G1EvacStats* stats) {
2578   return G1EvacSummary(stats->allocated(), stats->wasted(), stats->undo_wasted(),
2579                        stats->unused(), stats->used(), stats->region_end_waste(),
2580                        stats->regions_filled(), stats->direct_allocated(),
2581                        stats->failure_used(), stats->failure_waste());
2582 }
2583 
2584 void G1CollectedHeap::trace_heap(GCWhen::Type when, const GCTracer* gc_tracer) {
2585   const G1HeapSummary& heap_summary = create_g1_heap_summary();
2586   gc_tracer->report_gc_heap_summary(when, heap_summary);
2587 
2588   const MetaspaceSummary& metaspace_summary = create_metaspace_summary();
2589   gc_tracer->report_metaspace_summary(when, metaspace_summary);
2590 }
2591 
2592 void G1CollectedHeap::gc_prologue(bool full) {
2593   assert(InlineCacheBuffer::is_empty(), "should have cleaned up ICBuffer");
2594 
2595   // Update common counters.
2596   increment_total_collections(full /* full gc */);
2597   if (full || collector_state()->in_concurrent_start_gc()) {
2598     increment_old_marking_cycles_started();
2599   }
2600 }
2601 
2602 void G1CollectedHeap::gc_epilogue(bool full) {
2603   // Update common counters.
2604   if (full) {
2605     // Update the number of full collections that have been completed.
2606     increment_old_marking_cycles_completed(false /* concurrent */, true /* liveness_completed */);
2607   }
2608 
2609 #if COMPILER2_OR_JVMCI
2610   assert(DerivedPointerTable::is_empty(), "derived pointer present");
2611 #endif
2612 
2613   // We have just completed a GC. Update the soft reference
2614   // policy with the new heap occupancy
2615   Universe::heap()->update_capacity_and_used_at_gc();
2616 
2617   _collection_pause_end = Ticks::now();
2618 
2619   _free_segmented_array_memory_task->notify_new_stats(&_young_gen_card_set_stats,
2620                                                       &_collection_set_candidates_card_set_stats);
2621 }
2622 
2623 uint G1CollectedHeap::uncommit_regions(uint region_limit) {
2624   return _hrm.uncommit_inactive_regions(region_limit);
2625 }
2626 
2627 bool G1CollectedHeap::has_uncommittable_regions() {
2628   return _hrm.has_inactive_regions();
2629 }
2630 
2631 void G1CollectedHeap::uncommit_regions_if_necessary() {
2632   if (has_uncommittable_regions()) {
2633     G1UncommitRegionTask::enqueue();
2634   }
2635 }
2636 
2637 void G1CollectedHeap::verify_numa_regions(const char* desc) {
2638   LogTarget(Trace, gc, heap, verify) lt;
2639 
2640   if (lt.is_enabled()) {
2641     LogStream ls(lt);
2642     // Iterate all heap regions to print matching between preferred numa id and actual numa id.
2643     G1NodeIndexCheckClosure cl(desc, _numa, &ls);
2644     heap_region_iterate(&cl);
2645   }
2646 }
2647 
2648 HeapWord* G1CollectedHeap::do_collection_pause(size_t word_size,
2649                                                uint gc_count_before,
2650                                                bool* succeeded,
2651                                                GCCause::Cause gc_cause) {
2652   assert_heap_not_locked_and_not_at_safepoint();
2653   VM_G1CollectForAllocation op(word_size,
2654                                gc_count_before,
2655                                gc_cause,
2656                                policy()->max_pause_time_ms());
2657   VMThread::execute(&op);
2658 
2659   HeapWord* result = op.result();
2660   bool ret_succeeded = op.prologue_succeeded() && op.gc_succeeded();
2661   assert(result == NULL || ret_succeeded,
2662          "the result should be NULL if the VM did not succeed");
2663   *succeeded = ret_succeeded;
2664 
2665   assert_heap_not_locked();
2666   return result;
2667 }
2668 
2669 void G1CollectedHeap::start_concurrent_cycle(bool concurrent_operation_is_full_mark) {
2670   assert(!_cm_thread->in_progress(), "Can not start concurrent operation while in progress");
2671 
2672   MutexLocker x(CGC_lock, Mutex::_no_safepoint_check_flag);
2673   if (concurrent_operation_is_full_mark) {
2674     _cm->post_concurrent_mark_start();
2675     _cm_thread->start_full_mark();
2676   } else {
2677     _cm->post_concurrent_undo_start();
2678     _cm_thread->start_undo_mark();
2679   }
2680   CGC_lock->notify();
2681 }
2682 
2683 bool G1CollectedHeap::is_potential_eager_reclaim_candidate(HeapRegion* r) const {
2684   // We don't nominate objects with many remembered set entries, on
2685   // the assumption that such objects are likely still live.
2686   HeapRegionRemSet* rem_set = r->rem_set();
2687 
2688   return G1EagerReclaimHumongousObjectsWithStaleRefs ?
2689          rem_set->occupancy_less_or_equal_than(G1EagerReclaimRemSetThreshold) :
2690          G1EagerReclaimHumongousObjects && rem_set->is_empty();
2691 }
2692 
2693 #ifndef PRODUCT
2694 void G1CollectedHeap::verify_region_attr_remset_is_tracked() {
2695   class VerifyRegionAttrRemSet : public HeapRegionClosure {
2696   public:
2697     virtual bool do_heap_region(HeapRegion* r) {
2698       G1CollectedHeap* g1h = G1CollectedHeap::heap();
2699       bool const remset_is_tracked = g1h->region_attr(r->bottom()).remset_is_tracked();
2700       assert(r->rem_set()->is_tracked() == remset_is_tracked,
2701              "Region %u remset tracking status (%s) different to region attribute (%s)",
2702              r->hrm_index(), BOOL_TO_STR(r->rem_set()->is_tracked()), BOOL_TO_STR(remset_is_tracked));
2703       return false;
2704     }
2705   } cl;
2706   heap_region_iterate(&cl);
2707 }
2708 #endif
2709 
2710 class VerifyRegionRemSetClosure : public HeapRegionClosure {
2711   public:
2712     bool do_heap_region(HeapRegion* hr) {
2713       if (!hr->is_archive() && !hr->is_continues_humongous()) {
2714         hr->verify_rem_set();
2715       }
2716       return false;
2717     }
2718 };
2719 
2720 void G1CollectedHeap::start_new_collection_set() {
2721   double start = os::elapsedTime();
2722 
2723   collection_set()->start_incremental_building();
2724 
2725   clear_region_attr();
2726 
2727   guarantee(_eden.length() == 0, "eden should have been cleared");
2728   policy()->transfer_survivors_to_cset(survivor());
2729 
2730   // We redo the verification but now wrt to the new CSet which
2731   // has just got initialized after the previous CSet was freed.
2732   _cm->verify_no_collection_set_oops();
2733 
2734   phase_times()->record_start_new_cset_time_ms((os::elapsedTime() - start) * 1000.0);
2735 }
2736 
2737 G1HeapVerifier::G1VerifyType G1CollectedHeap::young_collection_verify_type() const {
2738   if (collector_state()->in_concurrent_start_gc()) {
2739     return G1HeapVerifier::G1VerifyConcurrentStart;
2740   } else if (collector_state()->in_young_only_phase()) {
2741     return G1HeapVerifier::G1VerifyYoungNormal;
2742   } else {
2743     return G1HeapVerifier::G1VerifyMixed;
2744   }
2745 }
2746 
2747 void G1CollectedHeap::verify_before_young_collection(G1HeapVerifier::G1VerifyType type) {
2748   if (!VerifyBeforeGC) {
2749     return;
2750   }
2751   Ticks start = Ticks::now();
2752   _verifier->prepare_for_verify();
2753   _verifier->verify_region_sets_optional();
2754   _verifier->verify_dirty_young_regions();
2755   if (VerifyRememberedSets) {
2756     log_info(gc, verify)("[Verifying RemSets before GC]");
2757     VerifyRegionRemSetClosure v_cl;
2758     heap_region_iterate(&v_cl);
2759   }
2760   _verifier->verify_before_gc(type);
2761   _verifier->check_bitmaps("GC Start");
2762   verify_numa_regions("GC Start");
2763   phase_times()->record_verify_before_time_ms((Ticks::now() - start).seconds() * MILLIUNITS);
2764 }
2765 
2766 void G1CollectedHeap::verify_after_young_collection(G1HeapVerifier::G1VerifyType type) {
2767   if (!VerifyAfterGC) {
2768     return;
2769   }
2770   Ticks start = Ticks::now();
2771   if (VerifyRememberedSets) {
2772     log_info(gc, verify)("[Verifying RemSets after GC]");
2773     VerifyRegionRemSetClosure v_cl;
2774     heap_region_iterate(&v_cl);
2775   }
2776   _verifier->verify_after_gc(type);
2777   _verifier->check_bitmaps("GC End");
2778   verify_numa_regions("GC End");
2779   _verifier->verify_region_sets_optional();
2780   phase_times()->record_verify_after_time_ms((Ticks::now() - start).seconds() * MILLIUNITS);
2781 }
2782 
2783 void G1CollectedHeap::expand_heap_after_young_collection(){
2784   size_t expand_bytes = _heap_sizing_policy->young_collection_expansion_amount();
2785   if (expand_bytes > 0) {
2786     // No need for an ergo logging here,
2787     // expansion_amount() does this when it returns a value > 0.
2788     double expand_ms = 0.0;
2789     if (!expand(expand_bytes, _workers, &expand_ms)) {
2790       // We failed to expand the heap. Cannot do anything about it.
2791     }
2792     phase_times()->record_expand_heap_time(expand_ms);
2793   }
2794 }
2795 
2796 bool G1CollectedHeap::do_collection_pause_at_safepoint(double target_pause_time_ms) {
2797   assert_at_safepoint_on_vm_thread();
2798   guarantee(!is_gc_active(), "collection is not reentrant");
2799 
2800   if (GCLocker::check_active_before_gc()) {
2801     return false;
2802   }
2803 
2804   do_collection_pause_at_safepoint_helper(target_pause_time_ms);
2805   return true;
2806 }
2807 
2808 G1HeapPrinterMark::G1HeapPrinterMark(G1CollectedHeap* g1h) : _g1h(g1h), _heap_transition(g1h) {
2809   // This summary needs to be printed before incrementing total collections.
2810   _g1h->rem_set()->print_periodic_summary_info("Before GC RS summary", _g1h->total_collections());
2811   _g1h->print_heap_before_gc();
2812   _g1h->print_heap_regions();
2813 }
2814 
2815 G1HeapPrinterMark::~G1HeapPrinterMark() {
2816   _g1h->policy()->print_age_table();
2817   _g1h->rem_set()->print_coarsen_stats();
2818   // We are at the end of the GC. Total collections has already been increased.
2819   _g1h->rem_set()->print_periodic_summary_info("After GC RS summary", _g1h->total_collections() - 1);
2820 
2821   _heap_transition.print();
2822   _g1h->print_heap_regions();
2823   _g1h->print_heap_after_gc();
2824   // Print NUMA statistics.
2825   _g1h->numa()->print_statistics();
2826 }
2827 
2828 G1JFRTracerMark::G1JFRTracerMark(STWGCTimer* timer, GCTracer* tracer) :
2829   _timer(timer), _tracer(tracer) {
2830 
2831   _timer->register_gc_start();
2832   _tracer->report_gc_start(G1CollectedHeap::heap()->gc_cause(), _timer->gc_start());
2833   G1CollectedHeap::heap()->trace_heap_before_gc(_tracer);
2834 }
2835 
2836 G1JFRTracerMark::~G1JFRTracerMark() {
2837   G1CollectedHeap::heap()->trace_heap_after_gc(_tracer);
2838   _timer->register_gc_end();
2839   _tracer->report_gc_end(_timer->gc_end(), _timer->time_partitions());
2840 }
2841 
2842 void G1CollectedHeap::prepare_tlabs_for_mutator() {
2843   Ticks start = Ticks::now();
2844 
2845   _survivor_evac_stats.adjust_desired_plab_sz();
2846   _old_evac_stats.adjust_desired_plab_sz();
2847 
2848   allocate_dummy_regions();
2849 
2850   _allocator->init_mutator_alloc_regions();
2851 
2852   resize_all_tlabs();
2853 
2854   phase_times()->record_resize_tlab_time_ms((Ticks::now() - start).seconds() * 1000.0);
2855 }
2856 
2857 void G1CollectedHeap::retire_tlabs() {
2858   ensure_parsability(true);
2859 }
2860 
2861 void G1CollectedHeap::do_collection_pause_at_safepoint_helper(double target_pause_time_ms) {
2862   ResourceMark rm;
2863 
2864   IsGCActiveMark active_gc_mark;
2865   GCIdMark gc_id_mark;
2866   SvcGCMarker sgcm(SvcGCMarker::MINOR);
2867 
2868   GCTraceCPUTime tcpu;
2869 
2870   _bytes_used_during_gc = 0;
2871 
2872   policy()->decide_on_concurrent_start_pause();
2873   // Record whether this pause may need to trigger a concurrent operation. Later,
2874   // when we signal the G1ConcurrentMarkThread, the collector state has already
2875   // been reset for the next pause.
2876   bool should_start_concurrent_mark_operation = collector_state()->in_concurrent_start_gc();
2877 
2878   // Perform the collection.
2879   G1YoungCollector collector(gc_cause(), target_pause_time_ms);
2880   collector.collect();
2881 
2882   // It should now be safe to tell the concurrent mark thread to start
2883   // without its logging output interfering with the logging output
2884   // that came from the pause.
2885   if (should_start_concurrent_mark_operation) {
2886     // CAUTION: after the start_concurrent_cycle() call below, the concurrent marking
2887     // thread(s) could be running concurrently with us. Make sure that anything
2888     // after this point does not assume that we are the only GC thread running.
2889     // Note: of course, the actual marking work will not start until the safepoint
2890     // itself is released in SuspendibleThreadSet::desynchronize().
2891     start_concurrent_cycle(collector.concurrent_operation_is_full_mark());
2892     ConcurrentGCBreakpoints::notify_idle_to_active();
2893   }
2894 }
2895 
2896 void G1CollectedHeap::complete_cleaning(BoolObjectClosure* is_alive,
2897                                         bool class_unloading_occurred) {
2898   uint num_workers = workers()->active_workers();
2899   G1ParallelCleaningTask unlink_task(is_alive, num_workers, class_unloading_occurred);
2900   workers()->run_task(&unlink_task);
2901 }
2902 
2903 bool G1STWSubjectToDiscoveryClosure::do_object_b(oop obj) {
2904   assert(obj != NULL, "must not be NULL");
2905   assert(_g1h->is_in_reserved(obj), "Trying to discover obj " PTR_FORMAT " not in heap", p2i(obj));
2906   // The areas the CM and STW ref processor manage must be disjoint. The is_in_cset() below
2907   // may falsely indicate that this is not the case here: however the collection set only
2908   // contains old regions when concurrent mark is not running.
2909   return _g1h->is_in_cset(obj) || _g1h->heap_region_containing(obj)->is_survivor();
2910 }
2911 
2912 void G1CollectedHeap::make_pending_list_reachable() {
2913   if (collector_state()->in_concurrent_start_gc()) {
2914     oop pll_head = Universe::reference_pending_list();
2915     if (pll_head != NULL) {
2916       // Any valid worker id is fine here as we are in the VM thread and single-threaded.
2917       _cm->mark_in_next_bitmap(0 /* worker_id */, pll_head);
2918     }
2919   }
2920 }
2921 
2922 static bool do_humongous_object_logging() {
2923   return log_is_enabled(Debug, gc, humongous);
2924 }
2925 
2926 bool G1CollectedHeap::should_do_eager_reclaim() const {
2927   // As eager reclaim logging also gives information about humongous objects in
2928   // the heap in general, always do the eager reclaim pass even without known
2929   // candidates.
2930   return (G1EagerReclaimHumongousObjects &&
2931           (has_humongous_reclaim_candidates() || do_humongous_object_logging()));
2932 }
2933 
2934 void G1CollectedHeap::set_humongous_stats(uint num_humongous_total, uint num_humongous_candidates) {
2935   _num_humongous_objects = num_humongous_total;
2936   _num_humongous_reclaim_candidates = num_humongous_candidates;
2937 }
2938 
2939 bool G1CollectedHeap::should_sample_collection_set_candidates() const {
2940   G1CollectionSetCandidates* candidates = G1CollectedHeap::heap()->collection_set()->candidates();
2941   return candidates != NULL && candidates->num_remaining() > 0;
2942 }
2943 
2944 void G1CollectedHeap::set_collection_set_candidates_stats(G1SegmentedArrayMemoryStats& stats) {
2945   _collection_set_candidates_card_set_stats = stats;
2946 }
2947 
2948 void G1CollectedHeap::set_young_gen_card_set_stats(const G1SegmentedArrayMemoryStats& stats) {
2949   _young_gen_card_set_stats = stats;
2950 }
2951 
2952 void G1CollectedHeap::record_obj_copy_mem_stats() {
2953   policy()->old_gen_alloc_tracker()->
2954     add_allocated_bytes_since_last_gc(_old_evac_stats.allocated() * HeapWordSize);
2955 
2956   _gc_tracer_stw->report_evacuation_statistics(create_g1_evac_summary(&_survivor_evac_stats),
2957                                                create_g1_evac_summary(&_old_evac_stats));
2958 }
2959 
2960 void G1CollectedHeap::free_region(HeapRegion* hr, FreeRegionList* free_list) {
2961   assert(!hr->is_free(), "the region should not be free");
2962   assert(!hr->is_empty(), "the region should not be empty");
2963   assert(_hrm.is_available(hr->hrm_index()), "region should be committed");
2964 
2965   if (G1VerifyBitmaps) {
2966     MemRegion mr(hr->bottom(), hr->end());
2967     concurrent_mark()->clear_range_in_prev_bitmap(mr);
2968   }
2969 
2970   // Clear the card counts for this region.
2971   // Note: we only need to do this if the region is not young
2972   // (since we don't refine cards in young regions).
2973   if (!hr->is_young()) {
2974     _hot_card_cache->reset_card_counts(hr);
2975   }
2976 
2977   // Reset region metadata to allow reuse.
2978   hr->hr_clear(true /* clear_space */);
2979   _policy->remset_tracker()->update_at_free(hr);
2980 
2981   if (free_list != NULL) {
2982     free_list->add_ordered(hr);
2983   }
2984 }
2985 
2986 void G1CollectedHeap::free_humongous_region(HeapRegion* hr,
2987                                             FreeRegionList* free_list) {
2988   assert(hr->is_humongous(), "this is only for humongous regions");
2989   hr->clear_humongous();
2990   free_region(hr, free_list);
2991 }
2992 
2993 void G1CollectedHeap::remove_from_old_gen_sets(const uint old_regions_removed,
2994                                                const uint archive_regions_removed,
2995                                                const uint humongous_regions_removed) {
2996   if (old_regions_removed > 0 || archive_regions_removed > 0 || humongous_regions_removed > 0) {
2997     MutexLocker x(OldSets_lock, Mutex::_no_safepoint_check_flag);
2998     _old_set.bulk_remove(old_regions_removed);
2999     _archive_set.bulk_remove(archive_regions_removed);
3000     _humongous_set.bulk_remove(humongous_regions_removed);
3001   }
3002 
3003 }
3004 
3005 void G1CollectedHeap::prepend_to_freelist(FreeRegionList* list) {
3006   assert(list != NULL, "list can't be null");
3007   if (!list->is_empty()) {
3008     MutexLocker x(FreeList_lock, Mutex::_no_safepoint_check_flag);
3009     _hrm.insert_list_into_free_list(list);
3010   }
3011 }
3012 
3013 void G1CollectedHeap::decrement_summary_bytes(size_t bytes) {
3014   decrease_used(bytes);
3015 }
3016 
3017 void G1CollectedHeap::clear_eden() {
3018   _eden.clear();
3019 }
3020 
3021 void G1CollectedHeap::clear_collection_set() {
3022   collection_set()->clear();
3023 }
3024 
3025 void G1CollectedHeap::rebuild_free_region_list() {
3026   Ticks start = Ticks::now();
3027   _hrm.rebuild_free_list(workers());
3028   phase_times()->record_total_rebuild_freelist_time_ms((Ticks::now() - start).seconds() * 1000.0);
3029 }
3030 
3031 class G1AbandonCollectionSetClosure : public HeapRegionClosure {
3032 public:
3033   virtual bool do_heap_region(HeapRegion* r) {
3034     assert(r->in_collection_set(), "Region %u must have been in collection set", r->hrm_index());
3035     G1CollectedHeap::heap()->clear_region_attr(r);
3036     r->clear_young_index_in_cset();
3037     return false;
3038   }
3039 };
3040 
3041 void G1CollectedHeap::abandon_collection_set(G1CollectionSet* collection_set) {
3042   G1AbandonCollectionSetClosure cl;
3043   collection_set_iterate_all(&cl);
3044 
3045   collection_set->clear();
3046   collection_set->stop_incremental_building();
3047 }
3048 
3049 bool G1CollectedHeap::is_old_gc_alloc_region(HeapRegion* hr) {
3050   return _allocator->is_retained_old_region(hr);
3051 }
3052 
3053 void G1CollectedHeap::set_region_short_lived_locked(HeapRegion* hr) {
3054   _eden.add(hr);
3055   _policy->set_region_eden(hr);
3056 }
3057 
3058 #ifdef ASSERT
3059 
3060 class NoYoungRegionsClosure: public HeapRegionClosure {
3061 private:
3062   bool _success;
3063 public:
3064   NoYoungRegionsClosure() : _success(true) { }
3065   bool do_heap_region(HeapRegion* r) {
3066     if (r->is_young()) {
3067       log_error(gc, verify)("Region [" PTR_FORMAT ", " PTR_FORMAT ") tagged as young",
3068                             p2i(r->bottom()), p2i(r->end()));
3069       _success = false;
3070     }
3071     return false;
3072   }
3073   bool success() { return _success; }
3074 };
3075 
3076 bool G1CollectedHeap::check_young_list_empty() {
3077   bool ret = (young_regions_count() == 0);
3078 
3079   NoYoungRegionsClosure closure;
3080   heap_region_iterate(&closure);
3081   ret = ret && closure.success();
3082 
3083   return ret;
3084 }
3085 
3086 #endif // ASSERT
3087 
3088 // Remove the given HeapRegion from the appropriate region set.
3089 void G1CollectedHeap::prepare_region_for_full_compaction(HeapRegion* hr) {
3090    if (hr->is_archive()) {
3091     _archive_set.remove(hr);
3092   } else if (hr->is_humongous()) {
3093     _humongous_set.remove(hr);
3094   } else if (hr->is_old()) {
3095     _old_set.remove(hr);
3096   } else if (hr->is_young()) {
3097     // Note that emptying the eden and survivor lists is postponed and instead
3098     // done as the first step when rebuilding the regions sets again. The reason
3099     // for this is that during a full GC string deduplication needs to know if
3100     // a collected region was young or old when the full GC was initiated.
3101     hr->uninstall_surv_rate_group();
3102   } else {
3103     // We ignore free regions, we'll empty the free list afterwards.
3104     assert(hr->is_free(), "it cannot be another type");
3105   }
3106 }
3107 
3108 void G1CollectedHeap::increase_used(size_t bytes) {
3109   _summary_bytes_used += bytes;
3110 }
3111 
3112 void G1CollectedHeap::decrease_used(size_t bytes) {
3113   assert(_summary_bytes_used >= bytes,
3114          "invariant: _summary_bytes_used: " SIZE_FORMAT " should be >= bytes: " SIZE_FORMAT,
3115          _summary_bytes_used, bytes);
3116   _summary_bytes_used -= bytes;
3117 }
3118 
3119 void G1CollectedHeap::set_used(size_t bytes) {
3120   _summary_bytes_used = bytes;
3121 }
3122 
3123 class RebuildRegionSetsClosure : public HeapRegionClosure {
3124 private:
3125   bool _free_list_only;
3126 
3127   HeapRegionSet* _old_set;
3128   HeapRegionSet* _archive_set;
3129   HeapRegionSet* _humongous_set;
3130 
3131   HeapRegionManager* _hrm;
3132 
3133   size_t _total_used;
3134 
3135 public:
3136   RebuildRegionSetsClosure(bool free_list_only,
3137                            HeapRegionSet* old_set,
3138                            HeapRegionSet* archive_set,
3139                            HeapRegionSet* humongous_set,
3140                            HeapRegionManager* hrm) :
3141     _free_list_only(free_list_only), _old_set(old_set), _archive_set(archive_set),
3142     _humongous_set(humongous_set), _hrm(hrm), _total_used(0) {
3143     assert(_hrm->num_free_regions() == 0, "pre-condition");
3144     if (!free_list_only) {
3145       assert(_old_set->is_empty(), "pre-condition");
3146       assert(_archive_set->is_empty(), "pre-condition");
3147       assert(_humongous_set->is_empty(), "pre-condition");
3148     }
3149   }
3150 
3151   bool do_heap_region(HeapRegion* r) {
3152     if (r->is_empty()) {
3153       assert(r->rem_set()->is_empty(), "Empty regions should have empty remembered sets.");
3154       // Add free regions to the free list
3155       r->set_free();
3156       _hrm->insert_into_free_list(r);
3157     } else if (!_free_list_only) {
3158       assert(r->rem_set()->is_empty(), "At this point remembered sets must have been cleared.");
3159 
3160       if (r->is_humongous()) {
3161         _humongous_set->add(r);
3162       } else if (r->is_archive()) {
3163         _archive_set->add(r);
3164       } else {
3165         assert(r->is_young() || r->is_free() || r->is_old(), "invariant");
3166         // We now move all (non-humongous, non-old, non-archive) regions to old gen,
3167         // and register them as such.
3168         r->move_to_old();
3169         _old_set->add(r);
3170       }
3171       _total_used += r->used();
3172     }
3173 
3174     return false;
3175   }
3176 
3177   size_t total_used() {
3178     return _total_used;
3179   }
3180 };
3181 
3182 void G1CollectedHeap::rebuild_region_sets(bool free_list_only) {
3183   assert_at_safepoint_on_vm_thread();
3184 
3185   if (!free_list_only) {
3186     _eden.clear();
3187     _survivor.clear();
3188   }
3189 
3190   RebuildRegionSetsClosure cl(free_list_only,
3191                               &_old_set, &_archive_set, &_humongous_set,
3192                               &_hrm);
3193   heap_region_iterate(&cl);
3194 
3195   if (!free_list_only) {
3196     set_used(cl.total_used());
3197     assert(_archive_allocator == nullptr, "must be, should not contribute to used");
3198   }
3199   assert_used_and_recalculate_used_equal(this);
3200 }
3201 
3202 // Methods for the mutator alloc region
3203 
3204 HeapRegion* G1CollectedHeap::new_mutator_alloc_region(size_t word_size,
3205                                                       bool force,
3206                                                       uint node_index) {
3207   assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
3208   bool should_allocate = policy()->should_allocate_mutator_region();
3209   if (force || should_allocate) {
3210     HeapRegion* new_alloc_region = new_region(word_size,
3211                                               HeapRegionType::Eden,
3212                                               false /* do_expand */,
3213                                               node_index);
3214     if (new_alloc_region != NULL) {
3215       set_region_short_lived_locked(new_alloc_region);
3216       _hr_printer.alloc(new_alloc_region, !should_allocate);
3217       _verifier->check_bitmaps("Mutator Region Allocation", new_alloc_region);
3218       _policy->remset_tracker()->update_at_allocate(new_alloc_region);
3219       return new_alloc_region;
3220     }
3221   }
3222   return NULL;
3223 }
3224 
3225 void G1CollectedHeap::retire_mutator_alloc_region(HeapRegion* alloc_region,
3226                                                   size_t allocated_bytes) {
3227   assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */);
3228   assert(alloc_region->is_eden(), "all mutator alloc regions should be eden");
3229 
3230   collection_set()->add_eden_region(alloc_region);
3231   increase_used(allocated_bytes);
3232   _eden.add_used_bytes(allocated_bytes);
3233   _hr_printer.retire(alloc_region);
3234 
3235   // We update the eden sizes here, when the region is retired,
3236   // instead of when it's allocated, since this is the point that its
3237   // used space has been recorded in _summary_bytes_used.
3238   monitoring_support()->update_eden_size();
3239 }
3240 
3241 // Methods for the GC alloc regions
3242 
3243 bool G1CollectedHeap::has_more_regions(G1HeapRegionAttr dest) {
3244   if (dest.is_old()) {
3245     return true;
3246   } else {
3247     return survivor_regions_count() < policy()->max_survivor_regions();
3248   }
3249 }
3250 
3251 HeapRegion* G1CollectedHeap::new_gc_alloc_region(size_t word_size, G1HeapRegionAttr dest, uint node_index) {
3252   assert(FreeList_lock->owned_by_self(), "pre-condition");
3253 
3254   if (!has_more_regions(dest)) {
3255     return NULL;
3256   }
3257 
3258   HeapRegionType type;
3259   if (dest.is_young()) {
3260     type = HeapRegionType::Survivor;
3261   } else {
3262     type = HeapRegionType::Old;
3263   }
3264 
3265   HeapRegion* new_alloc_region = new_region(word_size,
3266                                             type,
3267                                             true /* do_expand */,
3268                                             node_index);
3269 
3270   if (new_alloc_region != NULL) {
3271     if (type.is_survivor()) {
3272       new_alloc_region->set_survivor();
3273       _survivor.add(new_alloc_region);
3274       _verifier->check_bitmaps("Survivor Region Allocation", new_alloc_region);
3275       register_new_survivor_region_with_region_attr(new_alloc_region);
3276     } else {
3277       new_alloc_region->set_old();
3278       _verifier->check_bitmaps("Old Region Allocation", new_alloc_region);
3279     }
3280     _policy->remset_tracker()->update_at_allocate(new_alloc_region);
3281     register_region_with_region_attr(new_alloc_region);
3282     _hr_printer.alloc(new_alloc_region);
3283     return new_alloc_region;
3284   }
3285   return NULL;
3286 }
3287 
3288 void G1CollectedHeap::retire_gc_alloc_region(HeapRegion* alloc_region,
3289                                              size_t allocated_bytes,
3290                                              G1HeapRegionAttr dest) {
3291   _bytes_used_during_gc += allocated_bytes;
3292   if (dest.is_old()) {
3293     old_set_add(alloc_region);
3294     alloc_region->update_bot_threshold();
3295   } else {
3296     assert(dest.is_young(), "Retiring alloc region should be young (%d)", dest.type());
3297     _survivor.add_used_bytes(allocated_bytes);
3298   }
3299 
3300   bool const during_im = collector_state()->in_concurrent_start_gc();
3301   if (during_im && allocated_bytes > 0) {
3302     _cm->root_regions()->add(alloc_region->next_top_at_mark_start(), alloc_region->top());
3303   }
3304   _hr_printer.retire(alloc_region);
3305 }
3306 
3307 HeapRegion* G1CollectedHeap::alloc_highest_free_region() {
3308   bool expanded = false;
3309   uint index = _hrm.find_highest_free(&expanded);
3310 
3311   if (index != G1_NO_HRM_INDEX) {
3312     if (expanded) {
3313       log_debug(gc, ergo, heap)("Attempt heap expansion (requested address range outside heap bounds). region size: " SIZE_FORMAT "B",
3314                                 HeapRegion::GrainWords * HeapWordSize);
3315     }
3316     return _hrm.allocate_free_regions_starting_at(index, 1);
3317   }
3318   return NULL;
3319 }
3320 
3321 // Optimized nmethod scanning
3322 
3323 class RegisterNMethodOopClosure: public OopClosure {
3324   G1CollectedHeap* _g1h;
3325   nmethod* _nm;
3326 
3327 public:
3328   RegisterNMethodOopClosure(G1CollectedHeap* g1h, nmethod* nm) :
3329     _g1h(g1h), _nm(nm) {}
3330 
3331   void do_oop(oop* p) {
3332     oop heap_oop = RawAccess<>::oop_load(p);
3333     if (!CompressedOops::is_null(heap_oop)) {
3334       oop obj = CompressedOops::decode_not_null(heap_oop);
3335       HeapRegion* hr = _g1h->heap_region_containing(obj);
3336       assert(!hr->is_continues_humongous(),
3337              "trying to add code root " PTR_FORMAT " in continuation of humongous region " HR_FORMAT
3338              " starting at " HR_FORMAT,
3339              p2i(_nm), HR_FORMAT_PARAMS(hr), HR_FORMAT_PARAMS(hr->humongous_start_region()));
3340 
3341       // HeapRegion::add_strong_code_root_locked() avoids adding duplicate entries.
3342       hr->add_strong_code_root_locked(_nm);
3343     }
3344   }
3345 
3346   void do_oop(narrowOop* p) { ShouldNotReachHere(); }
3347 };
3348 
3349 class UnregisterNMethodOopClosure: public OopClosure {
3350   G1CollectedHeap* _g1h;
3351   nmethod* _nm;
3352 
3353 public:
3354   UnregisterNMethodOopClosure(G1CollectedHeap* g1h, nmethod* nm) :
3355     _g1h(g1h), _nm(nm) {}
3356 
3357   void do_oop(oop* p) {
3358     oop heap_oop = RawAccess<>::oop_load(p);
3359     if (!CompressedOops::is_null(heap_oop)) {
3360       oop obj = CompressedOops::decode_not_null(heap_oop);
3361       HeapRegion* hr = _g1h->heap_region_containing(obj);
3362       assert(!hr->is_continues_humongous(),
3363              "trying to remove code root " PTR_FORMAT " in continuation of humongous region " HR_FORMAT
3364              " starting at " HR_FORMAT,
3365              p2i(_nm), HR_FORMAT_PARAMS(hr), HR_FORMAT_PARAMS(hr->humongous_start_region()));
3366 
3367       hr->remove_strong_code_root(_nm);
3368     }
3369   }
3370 
3371   void do_oop(narrowOop* p) { ShouldNotReachHere(); }
3372 };
3373 
3374 void G1CollectedHeap::register_nmethod(nmethod* nm) {
3375   guarantee(nm != NULL, "sanity");
3376   RegisterNMethodOopClosure reg_cl(this, nm);
3377   nm->oops_do(&reg_cl);
3378 }
3379 
3380 void G1CollectedHeap::unregister_nmethod(nmethod* nm) {
3381   guarantee(nm != NULL, "sanity");
3382   UnregisterNMethodOopClosure reg_cl(this, nm);
3383   nm->oops_do(&reg_cl, true);
3384 }
3385 
3386 void G1CollectedHeap::update_used_after_gc(bool evacuation_failed) {
3387   if (evacuation_failed) {
3388     // Reset the G1EvacuationFailureALot counters and flags
3389     evac_failure_injector()->reset();
3390 
3391     set_used(recalculate_used());
3392 
3393     assert(_archive_allocator == nullptr, "must be, should not contribute to used");
3394   } else {
3395     // The "used" of the the collection set have already been subtracted
3396     // when they were freed.  Add in the bytes used.
3397     increase_used(_bytes_used_during_gc);
3398   }
3399 }
3400 
3401 void G1CollectedHeap::reset_hot_card_cache() {
3402   _hot_card_cache->reset_hot_cache();
3403   _hot_card_cache->set_use_cache(true);
3404 }
3405 
3406 void G1CollectedHeap::purge_code_root_memory() {
3407   G1CodeRootSet::purge();
3408 }
3409 
3410 class RebuildStrongCodeRootClosure: public CodeBlobClosure {
3411   G1CollectedHeap* _g1h;
3412 
3413 public:
3414   RebuildStrongCodeRootClosure(G1CollectedHeap* g1h) :
3415     _g1h(g1h) {}
3416 
3417   void do_code_blob(CodeBlob* cb) {
3418     nmethod* nm = (cb != NULL) ? cb->as_nmethod_or_null() : NULL;
3419     if (nm == NULL) {
3420       return;
3421     }
3422 
3423     _g1h->register_nmethod(nm);
3424   }
3425 };
3426 
3427 void G1CollectedHeap::rebuild_strong_code_roots() {
3428   RebuildStrongCodeRootClosure blob_cl(this);
3429   CodeCache::blobs_do(&blob_cl);
3430 }
3431 
3432 void G1CollectedHeap::initialize_serviceability() {
3433   _monitoring_support->initialize_serviceability();
3434 }
3435 
3436 MemoryUsage G1CollectedHeap::memory_usage() {
3437   return _monitoring_support->memory_usage();
3438 }
3439 
3440 GrowableArray<GCMemoryManager*> G1CollectedHeap::memory_managers() {
3441   return _monitoring_support->memory_managers();
3442 }
3443 
3444 GrowableArray<MemoryPool*> G1CollectedHeap::memory_pools() {
3445   return _monitoring_support->memory_pools();
3446 }
3447 
3448 void G1CollectedHeap::fill_with_dummy_object(HeapWord* start, HeapWord* end, bool zap) {
3449   HeapRegion* region = heap_region_containing(start);
3450   region->fill_with_dummy_object(start, pointer_delta(end, start), zap);
3451 }