1 /*
   2  * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2013, 2022, Red Hat, Inc. All rights reserved.
   4  * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
   5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6  *
   7  * This code is free software; you can redistribute it and/or modify it
   8  * under the terms of the GNU General Public License version 2 only, as
   9  * published by the Free Software Foundation.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  *
  25  */
  26 
  27 #include "precompiled.hpp"
  28 
  29 #include "cds/archiveHeapWriter.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "code/codeCache.hpp"
  32 
  33 #include "gc/shared/classUnloadingContext.hpp"
  34 #include "gc/shared/fullGCForwarding.hpp"
  35 #include "gc/shared/gcArguments.hpp"
  36 #include "gc/shared/gcTimer.hpp"
  37 #include "gc/shared/gcTraceTime.inline.hpp"
  38 #include "gc/shared/locationPrinter.inline.hpp"
  39 #include "gc/shared/memAllocator.hpp"
  40 #include "gc/shared/plab.hpp"
  41 #include "gc/shared/tlab_globals.hpp"
  42 
  43 #include "gc/shenandoah/heuristics/shenandoahOldHeuristics.hpp"
  44 #include "gc/shenandoah/heuristics/shenandoahYoungHeuristics.hpp"
  45 #include "gc/shenandoah/shenandoahAllocRequest.hpp"
  46 #include "gc/shenandoah/shenandoahBarrierSet.hpp"
  47 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  48 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
  49 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
  50 #include "gc/shenandoah/shenandoahConcurrentMark.hpp"
  51 #include "gc/shenandoah/shenandoahControlThread.hpp"
  52 #include "gc/shenandoah/shenandoahClosures.inline.hpp"
  53 #include "gc/shenandoah/shenandoahFreeSet.hpp"
  54 #include "gc/shenandoah/shenandoahGenerationalEvacuationTask.hpp"
  55 #include "gc/shenandoah/shenandoahGenerationalHeap.hpp"
  56 #include "gc/shenandoah/shenandoahGlobalGeneration.hpp"
  57 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
  58 #include "gc/shenandoah/shenandoahHeapRegionClosures.hpp"
  59 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
  60 #include "gc/shenandoah/shenandoahHeapRegionSet.hpp"
  61 #include "gc/shenandoah/shenandoahInitLogger.hpp"
  62 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
  63 #include "gc/shenandoah/shenandoahMemoryPool.hpp"
  64 #include "gc/shenandoah/shenandoahMonitoringSupport.hpp"
  65 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
  66 #include "gc/shenandoah/shenandoahPacer.inline.hpp"
  67 #include "gc/shenandoah/shenandoahPadding.hpp"
  68 #include "gc/shenandoah/shenandoahParallelCleaning.inline.hpp"
  69 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  70 #include "gc/shenandoah/shenandoahReferenceProcessor.hpp"
  71 #include "gc/shenandoah/shenandoahRootProcessor.inline.hpp"
  72 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
  73 #include "gc/shenandoah/shenandoahSTWMark.hpp"
  74 #include "gc/shenandoah/shenandoahUncommitThread.hpp"
  75 #include "gc/shenandoah/shenandoahUtils.hpp"
  76 #include "gc/shenandoah/shenandoahVerifier.hpp"
  77 #include "gc/shenandoah/shenandoahVMOperations.hpp"
  78 #include "gc/shenandoah/shenandoahWorkGroup.hpp"
  79 #include "gc/shenandoah/shenandoahWorkerPolicy.hpp"
  80 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
  81 #include "gc/shenandoah/mode/shenandoahGenerationalMode.hpp"
  82 #include "gc/shenandoah/mode/shenandoahPassiveMode.hpp"
  83 #include "gc/shenandoah/mode/shenandoahSATBMode.hpp"
  84 
  85 #if INCLUDE_JFR
  86 #include "gc/shenandoah/shenandoahJfrSupport.hpp"
  87 #endif
  88 
  89 
  90 #include "memory/allocation.hpp"
  91 #include "memory/classLoaderMetaspace.hpp"
  92 #include "memory/metaspaceUtils.hpp"
  93 #include "memory/universe.hpp"
  94 #include "nmt/mallocTracker.hpp"
  95 #include "nmt/memTracker.hpp"
  96 #include "oops/compressedOops.inline.hpp"
  97 #include "prims/jvmtiTagMap.hpp"
  98 #include "runtime/atomic.hpp"
  99 #include "runtime/globals.hpp"
 100 #include "runtime/interfaceSupport.inline.hpp"
 101 #include "runtime/java.hpp"
 102 #include "runtime/orderAccess.hpp"
 103 #include "runtime/safepointMechanism.hpp"
 104 #include "runtime/stackWatermarkSet.hpp"
 105 #include "runtime/vmThread.hpp"
 106 #include "utilities/globalDefinitions.hpp"
 107 #include "utilities/events.hpp"
 108 #include "utilities/powerOfTwo.hpp"
 109 
 110 class ShenandoahPretouchHeapTask : public WorkerTask {
 111 private:
 112   ShenandoahRegionIterator _regions;
 113   const size_t _page_size;
 114 public:
 115   ShenandoahPretouchHeapTask(size_t page_size) :
 116     WorkerTask("Shenandoah Pretouch Heap"),
 117     _page_size(page_size) {}
 118 
 119   virtual void work(uint worker_id) {
 120     ShenandoahHeapRegion* r = _regions.next();
 121     while (r != nullptr) {
 122       if (r->is_committed()) {
 123         os::pretouch_memory(r->bottom(), r->end(), _page_size);
 124       }
 125       r = _regions.next();
 126     }
 127   }
 128 };
 129 
 130 class ShenandoahPretouchBitmapTask : public WorkerTask {
 131 private:
 132   ShenandoahRegionIterator _regions;
 133   char* _bitmap_base;
 134   const size_t _bitmap_size;
 135   const size_t _page_size;
 136 public:
 137   ShenandoahPretouchBitmapTask(char* bitmap_base, size_t bitmap_size, size_t page_size) :
 138     WorkerTask("Shenandoah Pretouch Bitmap"),
 139     _bitmap_base(bitmap_base),
 140     _bitmap_size(bitmap_size),
 141     _page_size(page_size) {}
 142 
 143   virtual void work(uint worker_id) {
 144     ShenandoahHeapRegion* r = _regions.next();
 145     while (r != nullptr) {
 146       size_t start = r->index()       * ShenandoahHeapRegion::region_size_bytes() / MarkBitMap::heap_map_factor();
 147       size_t end   = (r->index() + 1) * ShenandoahHeapRegion::region_size_bytes() / MarkBitMap::heap_map_factor();
 148       assert (end <= _bitmap_size, "end is sane: " SIZE_FORMAT " < " SIZE_FORMAT, end, _bitmap_size);
 149 
 150       if (r->is_committed()) {
 151         os::pretouch_memory(_bitmap_base + start, _bitmap_base + end, _page_size);
 152       }
 153 
 154       r = _regions.next();
 155     }
 156   }
 157 };
 158 
 159 jint ShenandoahHeap::initialize() {
 160   //
 161   // Figure out heap sizing
 162   //
 163 
 164   size_t init_byte_size = InitialHeapSize;
 165   size_t min_byte_size  = MinHeapSize;
 166   size_t max_byte_size  = MaxHeapSize;
 167   size_t heap_alignment = HeapAlignment;
 168 
 169   size_t reg_size_bytes = ShenandoahHeapRegion::region_size_bytes();
 170 
 171   Universe::check_alignment(max_byte_size,  reg_size_bytes, "Shenandoah heap");
 172   Universe::check_alignment(init_byte_size, reg_size_bytes, "Shenandoah heap");
 173 
 174   _num_regions = ShenandoahHeapRegion::region_count();
 175   assert(_num_regions == (max_byte_size / reg_size_bytes),
 176          "Regions should cover entire heap exactly: " SIZE_FORMAT " != " SIZE_FORMAT "/" SIZE_FORMAT,
 177          _num_regions, max_byte_size, reg_size_bytes);
 178 
 179   size_t num_committed_regions = init_byte_size / reg_size_bytes;
 180   num_committed_regions = MIN2(num_committed_regions, _num_regions);
 181   assert(num_committed_regions <= _num_regions, "sanity");
 182   _initial_size = num_committed_regions * reg_size_bytes;
 183 
 184   size_t num_min_regions = min_byte_size / reg_size_bytes;
 185   num_min_regions = MIN2(num_min_regions, _num_regions);
 186   assert(num_min_regions <= _num_regions, "sanity");
 187   _minimum_size = num_min_regions * reg_size_bytes;
 188 
 189   // Default to max heap size.
 190   _soft_max_size = _num_regions * reg_size_bytes;
 191 
 192   _committed = _initial_size;
 193 
 194   size_t heap_page_size   = UseLargePages ? os::large_page_size() : os::vm_page_size();
 195   size_t bitmap_page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();
 196   size_t region_page_size = UseLargePages ? os::large_page_size() : os::vm_page_size();
 197 
 198   //
 199   // Reserve and commit memory for heap
 200   //
 201 
 202   ReservedHeapSpace heap_rs = Universe::reserve_heap(max_byte_size, heap_alignment);
 203   initialize_reserved_region(heap_rs);
 204   _heap_region = MemRegion((HeapWord*)heap_rs.base(), heap_rs.size() / HeapWordSize);
 205   _heap_region_special = heap_rs.special();
 206 
 207   assert((((size_t) base()) & ShenandoahHeapRegion::region_size_bytes_mask()) == 0,
 208          "Misaligned heap: " PTR_FORMAT, p2i(base()));
 209   os::trace_page_sizes_for_requested_size("Heap",
 210                                           max_byte_size, heap_alignment,
 211                                           heap_rs.base(),
 212                                           heap_rs.size(), heap_rs.page_size());
 213 
 214 #if SHENANDOAH_OPTIMIZED_MARKTASK
 215   // The optimized ShenandoahMarkTask takes some bits away from the full object bits.
 216   // Fail if we ever attempt to address more than we can.
 217   if ((uintptr_t)heap_rs.end() >= ShenandoahMarkTask::max_addressable()) {
 218     FormatBuffer<512> buf("Shenandoah reserved [" PTR_FORMAT ", " PTR_FORMAT") for the heap, \n"
 219                           "but max object address is " PTR_FORMAT ". Try to reduce heap size, or try other \n"
 220                           "VM options that allocate heap at lower addresses (HeapBaseMinAddress, AllocateHeapAt, etc).",
 221                 p2i(heap_rs.base()), p2i(heap_rs.end()), ShenandoahMarkTask::max_addressable());
 222     vm_exit_during_initialization("Fatal Error", buf);
 223   }
 224 #endif
 225 
 226   ReservedSpace sh_rs = heap_rs.first_part(max_byte_size);
 227   if (!_heap_region_special) {
 228     os::commit_memory_or_exit(sh_rs.base(), _initial_size, heap_alignment, false,
 229                               "Cannot commit heap memory");
 230   }
 231 
 232   BarrierSet::set_barrier_set(new ShenandoahBarrierSet(this, _heap_region));
 233 
 234   // Now we know the number of regions and heap sizes, initialize the heuristics.
 235   initialize_heuristics();
 236 
 237   assert(_heap_region.byte_size() == heap_rs.size(), "Need to know reserved size for card table");
 238 
 239   //
 240   // Worker threads must be initialized after the barrier is configured
 241   //
 242   _workers = new ShenandoahWorkerThreads("Shenandoah GC Threads", _max_workers);
 243   if (_workers == nullptr) {
 244     vm_exit_during_initialization("Failed necessary allocation.");
 245   } else {
 246     _workers->initialize_workers();
 247   }
 248 
 249   if (ParallelGCThreads > 1) {
 250     _safepoint_workers = new ShenandoahWorkerThreads("Safepoint Cleanup Thread", ParallelGCThreads);
 251     _safepoint_workers->initialize_workers();
 252   }
 253 
 254   //
 255   // Reserve and commit memory for bitmap(s)
 256   //
 257 
 258   size_t bitmap_size_orig = ShenandoahMarkBitMap::compute_size(heap_rs.size());
 259   _bitmap_size = align_up(bitmap_size_orig, bitmap_page_size);
 260 
 261   size_t bitmap_bytes_per_region = reg_size_bytes / ShenandoahMarkBitMap::heap_map_factor();
 262 
 263   guarantee(bitmap_bytes_per_region != 0,
 264             "Bitmap bytes per region should not be zero");
 265   guarantee(is_power_of_2(bitmap_bytes_per_region),
 266             "Bitmap bytes per region should be power of two: " SIZE_FORMAT, bitmap_bytes_per_region);
 267 
 268   if (bitmap_page_size > bitmap_bytes_per_region) {
 269     _bitmap_regions_per_slice = bitmap_page_size / bitmap_bytes_per_region;
 270     _bitmap_bytes_per_slice = bitmap_page_size;
 271   } else {
 272     _bitmap_regions_per_slice = 1;
 273     _bitmap_bytes_per_slice = bitmap_bytes_per_region;
 274   }
 275 
 276   guarantee(_bitmap_regions_per_slice >= 1,
 277             "Should have at least one region per slice: " SIZE_FORMAT,
 278             _bitmap_regions_per_slice);
 279 
 280   guarantee(((_bitmap_bytes_per_slice) % bitmap_page_size) == 0,
 281             "Bitmap slices should be page-granular: bps = " SIZE_FORMAT ", page size = " SIZE_FORMAT,
 282             _bitmap_bytes_per_slice, bitmap_page_size);
 283 
 284   ReservedSpace bitmap(_bitmap_size, bitmap_page_size);
 285   os::trace_page_sizes_for_requested_size("Mark Bitmap",
 286                                           bitmap_size_orig, bitmap_page_size,
 287                                           bitmap.base(),
 288                                           bitmap.size(), bitmap.page_size());
 289   MemTracker::record_virtual_memory_tag(bitmap.base(), mtGC);
 290   _bitmap_region = MemRegion((HeapWord*) bitmap.base(), bitmap.size() / HeapWordSize);
 291   _bitmap_region_special = bitmap.special();
 292 
 293   size_t bitmap_init_commit = _bitmap_bytes_per_slice *
 294     align_up(num_committed_regions, _bitmap_regions_per_slice) / _bitmap_regions_per_slice;
 295   bitmap_init_commit = MIN2(_bitmap_size, bitmap_init_commit);
 296   if (!_bitmap_region_special) {
 297     os::commit_memory_or_exit((char *) _bitmap_region.start(), bitmap_init_commit, bitmap_page_size, false,
 298                               "Cannot commit bitmap memory");
 299   }
 300 
 301   _marking_context = new ShenandoahMarkingContext(_heap_region, _bitmap_region, _num_regions);
 302 
 303   if (ShenandoahVerify) {
 304     ReservedSpace verify_bitmap(_bitmap_size, bitmap_page_size);
 305     os::trace_page_sizes_for_requested_size("Verify Bitmap",
 306                                             bitmap_size_orig, bitmap_page_size,
 307                                             verify_bitmap.base(),
 308                                             verify_bitmap.size(), verify_bitmap.page_size());
 309     if (!verify_bitmap.special()) {
 310       os::commit_memory_or_exit(verify_bitmap.base(), verify_bitmap.size(), bitmap_page_size, false,
 311                                 "Cannot commit verification bitmap memory");
 312     }
 313     MemTracker::record_virtual_memory_tag(verify_bitmap.base(), mtGC);
 314     MemRegion verify_bitmap_region = MemRegion((HeapWord *) verify_bitmap.base(), verify_bitmap.size() / HeapWordSize);
 315     _verification_bit_map.initialize(_heap_region, verify_bitmap_region);
 316     _verifier = new ShenandoahVerifier(this, &_verification_bit_map);
 317   }
 318 
 319   // Reserve aux bitmap for use in object_iterate(). We don't commit it here.
 320   size_t aux_bitmap_page_size = bitmap_page_size;
 321 
 322   ReservedSpace aux_bitmap(_bitmap_size, aux_bitmap_page_size);
 323   os::trace_page_sizes_for_requested_size("Aux Bitmap",
 324                                           bitmap_size_orig, aux_bitmap_page_size,
 325                                           aux_bitmap.base(),
 326                                           aux_bitmap.size(), aux_bitmap.page_size());
 327   MemTracker::record_virtual_memory_tag(aux_bitmap.base(), mtGC);
 328   _aux_bitmap_region = MemRegion((HeapWord*) aux_bitmap.base(), aux_bitmap.size() / HeapWordSize);
 329   _aux_bitmap_region_special = aux_bitmap.special();
 330   _aux_bit_map.initialize(_heap_region, _aux_bitmap_region);
 331 
 332   //
 333   // Create regions and region sets
 334   //
 335   size_t region_align = align_up(sizeof(ShenandoahHeapRegion), SHENANDOAH_CACHE_LINE_SIZE);
 336   size_t region_storage_size_orig = region_align * _num_regions;
 337   size_t region_storage_size = align_up(region_storage_size_orig,
 338                                         MAX2(region_page_size, os::vm_allocation_granularity()));
 339 
 340   ReservedSpace region_storage(region_storage_size, region_page_size);
 341   os::trace_page_sizes_for_requested_size("Region Storage",
 342                                           region_storage_size_orig, region_page_size,
 343                                           region_storage.base(),
 344                                           region_storage.size(), region_storage.page_size());
 345   MemTracker::record_virtual_memory_tag(region_storage.base(), mtGC);
 346   if (!region_storage.special()) {
 347     os::commit_memory_or_exit(region_storage.base(), region_storage_size, region_page_size, false,
 348                               "Cannot commit region memory");
 349   }
 350 
 351   // Try to fit the collection set bitmap at lower addresses. This optimizes code generation for cset checks.
 352   // Go up until a sensible limit (subject to encoding constraints) and try to reserve the space there.
 353   // If not successful, bite a bullet and allocate at whatever address.
 354   {
 355     const size_t cset_align = MAX2<size_t>(os::vm_page_size(), os::vm_allocation_granularity());
 356     const size_t cset_size = align_up(((size_t) sh_rs.base() + sh_rs.size()) >> ShenandoahHeapRegion::region_size_bytes_shift(), cset_align);
 357     const size_t cset_page_size = os::vm_page_size();
 358 
 359     uintptr_t min = round_up_power_of_2(cset_align);
 360     uintptr_t max = (1u << 30u);
 361     ReservedSpace cset_rs;
 362 
 363     for (uintptr_t addr = min; addr <= max; addr <<= 1u) {
 364       char* req_addr = (char*)addr;
 365       assert(is_aligned(req_addr, cset_align), "Should be aligned");
 366       cset_rs = ReservedSpace(cset_size, cset_align, cset_page_size, req_addr);
 367       if (cset_rs.is_reserved()) {
 368         assert(cset_rs.base() == req_addr, "Allocated where requested: " PTR_FORMAT ", " PTR_FORMAT, p2i(cset_rs.base()), addr);
 369         _collection_set = new ShenandoahCollectionSet(this, cset_rs, sh_rs.base());
 370         break;
 371       }
 372     }
 373 
 374     if (_collection_set == nullptr) {
 375       cset_rs = ReservedSpace(cset_size, cset_align, os::vm_page_size());
 376       _collection_set = new ShenandoahCollectionSet(this, cset_rs, sh_rs.base());
 377     }
 378     os::trace_page_sizes_for_requested_size("Collection Set",
 379                                             cset_size, cset_page_size,
 380                                             cset_rs.base(),
 381                                             cset_rs.size(), cset_rs.page_size());
 382   }
 383 
 384   _regions = NEW_C_HEAP_ARRAY(ShenandoahHeapRegion*, _num_regions, mtGC);
 385   _affiliations = NEW_C_HEAP_ARRAY(uint8_t, _num_regions, mtGC);
 386   _free_set = new ShenandoahFreeSet(this, _num_regions);
 387 
 388   {
 389     ShenandoahHeapLocker locker(lock());
 390 
 391     for (size_t i = 0; i < _num_regions; i++) {
 392       HeapWord* start = (HeapWord*)sh_rs.base() + ShenandoahHeapRegion::region_size_words() * i;
 393       bool is_committed = i < num_committed_regions;
 394       void* loc = region_storage.base() + i * region_align;
 395 
 396       ShenandoahHeapRegion* r = new (loc) ShenandoahHeapRegion(start, i, is_committed);
 397       assert(is_aligned(r, SHENANDOAH_CACHE_LINE_SIZE), "Sanity");
 398 
 399       _marking_context->initialize_top_at_mark_start(r);
 400       _regions[i] = r;
 401       assert(!collection_set()->is_in(i), "New region should not be in collection set");
 402 
 403       _affiliations[i] = ShenandoahAffiliation::FREE;
 404     }
 405 
 406     // Initialize to complete
 407     _marking_context->mark_complete();
 408     size_t young_cset_regions, old_cset_regions;
 409 
 410     // We are initializing free set.  We ignore cset region tallies.
 411     size_t first_old, last_old, num_old;
 412     _free_set->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old, last_old, num_old);
 413     _free_set->finish_rebuild(young_cset_regions, old_cset_regions, num_old);
 414   }
 415 
 416   if (AlwaysPreTouch) {
 417     // For NUMA, it is important to pre-touch the storage under bitmaps with worker threads,
 418     // before initialize() below zeroes it with initializing thread. For any given region,
 419     // we touch the region and the corresponding bitmaps from the same thread.
 420     ShenandoahPushWorkerScope scope(workers(), _max_workers, false);
 421 
 422     _pretouch_heap_page_size = heap_page_size;
 423     _pretouch_bitmap_page_size = bitmap_page_size;
 424 
 425     // OS memory managers may want to coalesce back-to-back pages. Make their jobs
 426     // simpler by pre-touching continuous spaces (heap and bitmap) separately.
 427 
 428     ShenandoahPretouchBitmapTask bcl(bitmap.base(), _bitmap_size, _pretouch_bitmap_page_size);
 429     _workers->run_task(&bcl);
 430 
 431     ShenandoahPretouchHeapTask hcl(_pretouch_heap_page_size);
 432     _workers->run_task(&hcl);
 433   }
 434 
 435   //
 436   // Initialize the rest of GC subsystems
 437   //
 438 
 439   _liveness_cache = NEW_C_HEAP_ARRAY(ShenandoahLiveData*, _max_workers, mtGC);
 440   for (uint worker = 0; worker < _max_workers; worker++) {
 441     _liveness_cache[worker] = NEW_C_HEAP_ARRAY(ShenandoahLiveData, _num_regions, mtGC);
 442     Copy::fill_to_bytes(_liveness_cache[worker], _num_regions * sizeof(ShenandoahLiveData));
 443   }
 444 
 445   // There should probably be Shenandoah-specific options for these,
 446   // just as there are G1-specific options.
 447   {
 448     ShenandoahSATBMarkQueueSet& satbqs = ShenandoahBarrierSet::satb_mark_queue_set();
 449     satbqs.set_process_completed_buffers_threshold(20); // G1SATBProcessCompletedThreshold
 450     satbqs.set_buffer_enqueue_threshold_percentage(60); // G1SATBBufferEnqueueingThresholdPercent
 451   }
 452 
 453   _monitoring_support = new ShenandoahMonitoringSupport(this);
 454   _phase_timings = new ShenandoahPhaseTimings(max_workers());
 455   ShenandoahCodeRoots::initialize();
 456 
 457   if (ShenandoahPacing) {
 458     _pacer = new ShenandoahPacer(this);
 459     _pacer->setup_for_idle();
 460   }
 461 
 462   initialize_controller();
 463 
 464   if (ShenandoahUncommit) {
 465     _uncommit_thread = new ShenandoahUncommitThread(this);
 466   }
 467 
 468   print_init_logger();
 469 
 470   FullGCForwarding::initialize(_heap_region);
 471 
 472   return JNI_OK;
 473 }
 474 
 475 void ShenandoahHeap::initialize_controller() {
 476   _control_thread = new ShenandoahControlThread();
 477 }
 478 
 479 void ShenandoahHeap::print_init_logger() const {
 480   ShenandoahInitLogger::print();
 481 }
 482 
 483 void ShenandoahHeap::initialize_mode() {
 484   if (ShenandoahGCMode != nullptr) {
 485     if (strcmp(ShenandoahGCMode, "satb") == 0) {
 486       _gc_mode = new ShenandoahSATBMode();
 487     } else if (strcmp(ShenandoahGCMode, "passive") == 0) {
 488       _gc_mode = new ShenandoahPassiveMode();
 489     } else if (strcmp(ShenandoahGCMode, "generational") == 0) {
 490       _gc_mode = new ShenandoahGenerationalMode();
 491     } else {
 492       vm_exit_during_initialization("Unknown -XX:ShenandoahGCMode option");
 493     }
 494   } else {
 495     vm_exit_during_initialization("Unknown -XX:ShenandoahGCMode option (null)");
 496   }
 497   _gc_mode->initialize_flags();
 498   if (_gc_mode->is_diagnostic() && !UnlockDiagnosticVMOptions) {
 499     vm_exit_during_initialization(
 500             err_msg("GC mode \"%s\" is diagnostic, and must be enabled via -XX:+UnlockDiagnosticVMOptions.",
 501                     _gc_mode->name()));
 502   }
 503   if (_gc_mode->is_experimental() && !UnlockExperimentalVMOptions) {
 504     vm_exit_during_initialization(
 505             err_msg("GC mode \"%s\" is experimental, and must be enabled via -XX:+UnlockExperimentalVMOptions.",
 506                     _gc_mode->name()));
 507   }
 508 }
 509 
 510 void ShenandoahHeap::initialize_heuristics() {
 511   _global_generation = new ShenandoahGlobalGeneration(mode()->is_generational(), max_workers(), max_capacity(), max_capacity());
 512   _global_generation->initialize_heuristics(mode());
 513 }
 514 
 515 #ifdef _MSC_VER
 516 #pragma warning( push )
 517 #pragma warning( disable:4355 ) // 'this' : used in base member initializer list
 518 #endif
 519 
 520 ShenandoahHeap::ShenandoahHeap(ShenandoahCollectorPolicy* policy) :
 521   CollectedHeap(),
 522   _gc_generation(nullptr),
 523   _active_generation(nullptr),
 524   _initial_size(0),
 525   _committed(0),
 526   _max_workers(MAX3(ConcGCThreads, ParallelGCThreads, 1U)),
 527   _workers(nullptr),
 528   _safepoint_workers(nullptr),
 529   _heap_region_special(false),
 530   _num_regions(0),
 531   _regions(nullptr),
 532   _affiliations(nullptr),
 533   _gc_state_changed(false),
 534   _gc_no_progress_count(0),
 535   _cancel_requested_time(0),
 536   _update_refs_iterator(this),
 537   _global_generation(nullptr),
 538   _control_thread(nullptr),
 539   _uncommit_thread(nullptr),
 540   _young_generation(nullptr),
 541   _old_generation(nullptr),
 542   _shenandoah_policy(policy),
 543   _gc_mode(nullptr),
 544   _free_set(nullptr),
 545   _pacer(nullptr),
 546   _verifier(nullptr),
 547   _phase_timings(nullptr),
 548   _mmu_tracker(),
 549   _monitoring_support(nullptr),
 550   _memory_pool(nullptr),
 551   _stw_memory_manager("Shenandoah Pauses"),
 552   _cycle_memory_manager("Shenandoah Cycles"),
 553   _gc_timer(new ConcurrentGCTimer()),
 554   _log_min_obj_alignment_in_bytes(LogMinObjAlignmentInBytes),
 555   _marking_context(nullptr),
 556   _bitmap_size(0),
 557   _bitmap_regions_per_slice(0),
 558   _bitmap_bytes_per_slice(0),
 559   _bitmap_region_special(false),
 560   _aux_bitmap_region_special(false),
 561   _liveness_cache(nullptr),
 562   _collection_set(nullptr)
 563 {
 564   // Initialize GC mode early, many subsequent initialization procedures depend on it
 565   initialize_mode();
 566 }
 567 
 568 #ifdef _MSC_VER
 569 #pragma warning( pop )
 570 #endif
 571 
 572 void ShenandoahHeap::print_on(outputStream* st) const {
 573   st->print_cr("Shenandoah Heap");
 574   st->print_cr(" " SIZE_FORMAT "%s max, " SIZE_FORMAT "%s soft max, " SIZE_FORMAT "%s committed, " SIZE_FORMAT "%s used",
 575                byte_size_in_proper_unit(max_capacity()), proper_unit_for_byte_size(max_capacity()),
 576                byte_size_in_proper_unit(soft_max_capacity()), proper_unit_for_byte_size(soft_max_capacity()),
 577                byte_size_in_proper_unit(committed()),    proper_unit_for_byte_size(committed()),
 578                byte_size_in_proper_unit(used()),         proper_unit_for_byte_size(used()));
 579   st->print_cr(" " SIZE_FORMAT " x " SIZE_FORMAT"%s regions",
 580                num_regions(),
 581                byte_size_in_proper_unit(ShenandoahHeapRegion::region_size_bytes()),
 582                proper_unit_for_byte_size(ShenandoahHeapRegion::region_size_bytes()));
 583 
 584   st->print("Status: ");
 585   if (has_forwarded_objects())                 st->print("has forwarded objects, ");
 586   if (!mode()->is_generational()) {
 587     if (is_concurrent_mark_in_progress())      st->print("marking,");
 588   } else {
 589     if (is_concurrent_old_mark_in_progress())    st->print("old marking, ");
 590     if (is_concurrent_young_mark_in_progress())  st->print("young marking, ");
 591   }
 592   if (is_evacuation_in_progress())             st->print("evacuating, ");
 593   if (is_update_refs_in_progress())            st->print("updating refs, ");
 594   if (is_degenerated_gc_in_progress())         st->print("degenerated gc, ");
 595   if (is_full_gc_in_progress())                st->print("full gc, ");
 596   if (is_full_gc_move_in_progress())           st->print("full gc move, ");
 597   if (is_concurrent_weak_root_in_progress())   st->print("concurrent weak roots, ");
 598   if (is_concurrent_strong_root_in_progress() &&
 599       !is_concurrent_weak_root_in_progress())  st->print("concurrent strong roots, ");
 600 
 601   if (cancelled_gc()) {
 602     st->print("cancelled");
 603   } else {
 604     st->print("not cancelled");
 605   }
 606   st->cr();
 607 
 608   st->print_cr("Reserved region:");
 609   st->print_cr(" - [" PTR_FORMAT ", " PTR_FORMAT ") ",
 610                p2i(reserved_region().start()),
 611                p2i(reserved_region().end()));
 612 
 613   ShenandoahCollectionSet* cset = collection_set();
 614   st->print_cr("Collection set:");
 615   if (cset != nullptr) {
 616     st->print_cr(" - map (vanilla): " PTR_FORMAT, p2i(cset->map_address()));
 617     st->print_cr(" - map (biased):  " PTR_FORMAT, p2i(cset->biased_map_address()));
 618   } else {
 619     st->print_cr(" (null)");
 620   }
 621 
 622   st->cr();
 623   MetaspaceUtils::print_on(st);
 624 
 625   if (Verbose) {
 626     st->cr();
 627     print_heap_regions_on(st);
 628   }
 629 }
 630 
 631 class ShenandoahInitWorkerGCLABClosure : public ThreadClosure {
 632 public:
 633   void do_thread(Thread* thread) {
 634     assert(thread != nullptr, "Sanity");
 635     assert(thread->is_Worker_thread(), "Only worker thread expected");
 636     ShenandoahThreadLocalData::initialize_gclab(thread);
 637   }
 638 };
 639 
 640 void ShenandoahHeap::post_initialize() {
 641   CollectedHeap::post_initialize();
 642   _mmu_tracker.initialize();
 643 
 644   MutexLocker ml(Threads_lock);
 645 
 646   ShenandoahInitWorkerGCLABClosure init_gclabs;
 647   _workers->threads_do(&init_gclabs);
 648 
 649   // gclab can not be initialized early during VM startup, as it can not determinate its max_size.
 650   // Now, we will let WorkerThreads to initialize gclab when new worker is created.
 651   _workers->set_initialize_gclab();
 652   if (_safepoint_workers != nullptr) {
 653     _safepoint_workers->threads_do(&init_gclabs);
 654     _safepoint_workers->set_initialize_gclab();
 655   }
 656 
 657   JFR_ONLY(ShenandoahJFRSupport::register_jfr_type_serializers();)
 658 }
 659 
 660 ShenandoahHeuristics* ShenandoahHeap::heuristics() {
 661   return _global_generation->heuristics();
 662 }
 663 
 664 size_t ShenandoahHeap::used() const {
 665   return global_generation()->used();
 666 }
 667 
 668 size_t ShenandoahHeap::committed() const {
 669   return Atomic::load(&_committed);
 670 }
 671 
 672 void ShenandoahHeap::increase_committed(size_t bytes) {
 673   shenandoah_assert_heaplocked_or_safepoint();
 674   _committed += bytes;
 675 }
 676 
 677 void ShenandoahHeap::decrease_committed(size_t bytes) {
 678   shenandoah_assert_heaplocked_or_safepoint();
 679   _committed -= bytes;
 680 }
 681 
 682 // For tracking usage based on allocations, it should be the case that:
 683 // * The sum of regions::used == heap::used
 684 // * The sum of a generation's regions::used == generation::used
 685 // * The sum of a generation's humongous regions::free == generation::humongous_waste
 686 // These invariants are checked by the verifier on GC safepoints.
 687 //
 688 // Additional notes:
 689 // * When a mutator's allocation request causes a region to be retired, the
 690 //   free memory left in that region is considered waste. It does not contribute
 691 //   to the usage, but it _does_ contribute to allocation rate.
 692 // * The bottom of a PLAB must be aligned on card size. In some cases this will
 693 //   require padding in front of the PLAB (a filler object). Because this padding
 694 //   is included in the region's used memory we include the padding in the usage
 695 //   accounting as waste.
 696 // * Mutator allocations are used to compute an allocation rate. They are also
 697 //   sent to the Pacer for those purposes.
 698 // * There are three sources of waste:
 699 //  1. The padding used to align a PLAB on card size
 700 //  2. Region's free is less than minimum TLAB size and is retired
 701 //  3. The unused portion of memory in the last region of a humongous object
 702 void ShenandoahHeap::increase_used(const ShenandoahAllocRequest& req) {
 703   size_t actual_bytes = req.actual_size() * HeapWordSize;
 704   size_t wasted_bytes = req.waste() * HeapWordSize;
 705   ShenandoahGeneration* generation = generation_for(req.affiliation());
 706 
 707   if (req.is_gc_alloc()) {
 708     assert(wasted_bytes == 0 || req.type() == ShenandoahAllocRequest::_alloc_plab, "Only PLABs have waste");
 709     increase_used(generation, actual_bytes + wasted_bytes);
 710   } else {
 711     assert(req.is_mutator_alloc(), "Expected mutator alloc here");
 712     // padding and actual size both count towards allocation counter
 713     generation->increase_allocated(actual_bytes + wasted_bytes);
 714 
 715     // only actual size counts toward usage for mutator allocations
 716     increase_used(generation, actual_bytes);
 717 
 718     // notify pacer of both actual size and waste
 719     notify_mutator_alloc_words(req.actual_size(), req.waste());
 720 
 721     if (wasted_bytes > 0 && ShenandoahHeapRegion::requires_humongous(req.actual_size())) {
 722       increase_humongous_waste(generation,wasted_bytes);
 723     }
 724   }
 725 }
 726 
 727 void ShenandoahHeap::increase_humongous_waste(ShenandoahGeneration* generation, size_t bytes) {
 728   generation->increase_humongous_waste(bytes);
 729   if (!generation->is_global()) {
 730     global_generation()->increase_humongous_waste(bytes);
 731   }
 732 }
 733 
 734 void ShenandoahHeap::decrease_humongous_waste(ShenandoahGeneration* generation, size_t bytes) {
 735   generation->decrease_humongous_waste(bytes);
 736   if (!generation->is_global()) {
 737     global_generation()->decrease_humongous_waste(bytes);
 738   }
 739 }
 740 
 741 void ShenandoahHeap::increase_used(ShenandoahGeneration* generation, size_t bytes) {
 742   generation->increase_used(bytes);
 743   if (!generation->is_global()) {
 744     global_generation()->increase_used(bytes);
 745   }
 746 }
 747 
 748 void ShenandoahHeap::decrease_used(ShenandoahGeneration* generation, size_t bytes) {
 749   generation->decrease_used(bytes);
 750   if (!generation->is_global()) {
 751     global_generation()->decrease_used(bytes);
 752   }
 753 }
 754 
 755 void ShenandoahHeap::notify_mutator_alloc_words(size_t words, size_t waste) {
 756   if (ShenandoahPacing) {
 757     control_thread()->pacing_notify_alloc(words);
 758     if (waste > 0) {
 759       pacer()->claim_for_alloc<true>(waste);
 760     }
 761   }
 762 }
 763 
 764 size_t ShenandoahHeap::capacity() const {
 765   return committed();
 766 }
 767 
 768 size_t ShenandoahHeap::max_capacity() const {
 769   return _num_regions * ShenandoahHeapRegion::region_size_bytes();
 770 }
 771 
 772 size_t ShenandoahHeap::soft_max_capacity() const {
 773   size_t v = Atomic::load(&_soft_max_size);
 774   assert(min_capacity() <= v && v <= max_capacity(),
 775          "Should be in bounds: " SIZE_FORMAT " <= " SIZE_FORMAT " <= " SIZE_FORMAT,
 776          min_capacity(), v, max_capacity());
 777   return v;
 778 }
 779 
 780 void ShenandoahHeap::set_soft_max_capacity(size_t v) {
 781   assert(min_capacity() <= v && v <= max_capacity(),
 782          "Should be in bounds: " SIZE_FORMAT " <= " SIZE_FORMAT " <= " SIZE_FORMAT,
 783          min_capacity(), v, max_capacity());
 784   Atomic::store(&_soft_max_size, v);
 785 }
 786 
 787 size_t ShenandoahHeap::min_capacity() const {
 788   return _minimum_size;
 789 }
 790 
 791 size_t ShenandoahHeap::initial_capacity() const {
 792   return _initial_size;
 793 }
 794 
 795 bool ShenandoahHeap::is_in(const void* p) const {
 796   if (is_in_reserved(p)) {
 797     if (is_full_gc_move_in_progress()) {
 798       // Full GC move is running, we do not have a consistent region
 799       // information yet. But we know the pointer is in heap.
 800       return true;
 801     }
 802     // Now check if we point to a live section in active region.
 803     ShenandoahHeapRegion* r = heap_region_containing(p);
 804     return (r->is_active() && p < r->top());
 805   } else {
 806     return false;
 807   }
 808 }
 809 
 810 void ShenandoahHeap::notify_soft_max_changed() {
 811   if (_uncommit_thread != nullptr) {
 812     _uncommit_thread->notify_soft_max_changed();
 813   }
 814 }
 815 
 816 void ShenandoahHeap::notify_explicit_gc_requested() {
 817   if (_uncommit_thread != nullptr) {
 818     _uncommit_thread->notify_explicit_gc_requested();
 819   }
 820 }
 821 
 822 bool ShenandoahHeap::check_soft_max_changed() {
 823   size_t new_soft_max = Atomic::load(&SoftMaxHeapSize);
 824   size_t old_soft_max = soft_max_capacity();
 825   if (new_soft_max != old_soft_max) {
 826     new_soft_max = MAX2(min_capacity(), new_soft_max);
 827     new_soft_max = MIN2(max_capacity(), new_soft_max);
 828     if (new_soft_max != old_soft_max) {
 829       log_info(gc)("Soft Max Heap Size: " SIZE_FORMAT "%s -> " SIZE_FORMAT "%s",
 830                    byte_size_in_proper_unit(old_soft_max), proper_unit_for_byte_size(old_soft_max),
 831                    byte_size_in_proper_unit(new_soft_max), proper_unit_for_byte_size(new_soft_max)
 832       );
 833       set_soft_max_capacity(new_soft_max);
 834       return true;
 835     }
 836   }
 837   return false;
 838 }
 839 
 840 void ShenandoahHeap::notify_heap_changed() {
 841   // Update monitoring counters when we took a new region. This amortizes the
 842   // update costs on slow path.
 843   monitoring_support()->notify_heap_changed();
 844   _heap_changed.try_set();
 845 }
 846 
 847 void ShenandoahHeap::set_forced_counters_update(bool value) {
 848   monitoring_support()->set_forced_counters_update(value);
 849 }
 850 
 851 void ShenandoahHeap::handle_force_counters_update() {
 852   monitoring_support()->handle_force_counters_update();
 853 }
 854 
 855 HeapWord* ShenandoahHeap::allocate_from_gclab_slow(Thread* thread, size_t size) {
 856   // New object should fit the GCLAB size
 857   size_t min_size = MAX2(size, PLAB::min_size());
 858 
 859   // Figure out size of new GCLAB, looking back at heuristics. Expand aggressively.
 860   size_t new_size = ShenandoahThreadLocalData::gclab_size(thread) * 2;
 861 
 862   new_size = MIN2(new_size, PLAB::max_size());
 863   new_size = MAX2(new_size, PLAB::min_size());
 864 
 865   // Record new heuristic value even if we take any shortcut. This captures
 866   // the case when moderately-sized objects always take a shortcut. At some point,
 867   // heuristics should catch up with them.
 868   log_debug(gc, free)("Set new GCLAB size: " SIZE_FORMAT, new_size);
 869   ShenandoahThreadLocalData::set_gclab_size(thread, new_size);
 870 
 871   if (new_size < size) {
 872     // New size still does not fit the object. Fall back to shared allocation.
 873     // This avoids retiring perfectly good GCLABs, when we encounter a large object.
 874     log_debug(gc, free)("New gclab size (" SIZE_FORMAT ") is too small for " SIZE_FORMAT, new_size, size);
 875     return nullptr;
 876   }
 877 
 878   // Retire current GCLAB, and allocate a new one.
 879   PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
 880   gclab->retire();
 881 
 882   size_t actual_size = 0;
 883   HeapWord* gclab_buf = allocate_new_gclab(min_size, new_size, &actual_size);
 884   if (gclab_buf == nullptr) {
 885     return nullptr;
 886   }
 887 
 888   assert (size <= actual_size, "allocation should fit");
 889 
 890   // ...and clear or zap just allocated TLAB, if needed.
 891   if (ZeroTLAB) {
 892     Copy::zero_to_words(gclab_buf, actual_size);
 893   } else if (ZapTLAB) {
 894     // Skip mangling the space corresponding to the object header to
 895     // ensure that the returned space is not considered parsable by
 896     // any concurrent GC thread.
 897     size_t hdr_size = oopDesc::header_size();
 898     Copy::fill_to_words(gclab_buf + hdr_size, actual_size - hdr_size, badHeapWordVal);
 899   }
 900   gclab->set_buf(gclab_buf, actual_size);
 901   return gclab->allocate(size);
 902 }
 903 
 904 // Called from stubs in JIT code or interpreter
 905 HeapWord* ShenandoahHeap::allocate_new_tlab(size_t min_size,
 906                                             size_t requested_size,
 907                                             size_t* actual_size) {
 908   ShenandoahAllocRequest req = ShenandoahAllocRequest::for_tlab(min_size, requested_size);
 909   HeapWord* res = allocate_memory(req);
 910   if (res != nullptr) {
 911     *actual_size = req.actual_size();
 912   } else {
 913     *actual_size = 0;
 914   }
 915   return res;
 916 }
 917 
 918 HeapWord* ShenandoahHeap::allocate_new_gclab(size_t min_size,
 919                                              size_t word_size,
 920                                              size_t* actual_size) {
 921   ShenandoahAllocRequest req = ShenandoahAllocRequest::for_gclab(min_size, word_size);
 922   HeapWord* res = allocate_memory(req);
 923   if (res != nullptr) {
 924     *actual_size = req.actual_size();
 925   } else {
 926     *actual_size = 0;
 927   }
 928   return res;
 929 }
 930 
 931 HeapWord* ShenandoahHeap::allocate_memory(ShenandoahAllocRequest& req) {
 932   intptr_t pacer_epoch = 0;
 933   bool in_new_region = false;
 934   HeapWord* result = nullptr;
 935 
 936   if (req.is_mutator_alloc()) {
 937     if (ShenandoahPacing) {
 938       pacer()->pace_for_alloc(req.size());
 939       pacer_epoch = pacer()->epoch();
 940     }
 941 
 942     if (!ShenandoahAllocFailureALot || !should_inject_alloc_failure()) {
 943       result = allocate_memory_under_lock(req, in_new_region);
 944     }
 945 
 946     // Check that gc overhead is not exceeded.
 947     //
 948     // Shenandoah will grind along for quite a while allocating one
 949     // object at a time using shared (non-tlab) allocations. This check
 950     // is testing that the GC overhead limit has not been exceeded.
 951     // This will notify the collector to start a cycle, but will raise
 952     // an OOME to the mutator if the last Full GCs have not made progress.
 953     // gc_no_progress_count is incremented following each degen or full GC that fails to achieve is_good_progress().
 954     if (result == nullptr && !req.is_lab_alloc() && get_gc_no_progress_count() > ShenandoahNoProgressThreshold) {
 955       control_thread()->handle_alloc_failure(req, false);
 956       req.set_actual_size(0);
 957       return nullptr;
 958     }
 959 
 960     if (result == nullptr) {
 961       // Block until control thread reacted, then retry allocation.
 962       //
 963       // It might happen that one of the threads requesting allocation would unblock
 964       // way later after GC happened, only to fail the second allocation, because
 965       // other threads have already depleted the free storage. In this case, a better
 966       // strategy is to try again, until at least one full GC has completed.
 967       //
 968       // Stop retrying and return nullptr to cause OOMError exception if our allocation failed even after:
 969       //   a) We experienced a GC that had good progress, or
 970       //   b) We experienced at least one Full GC (whether or not it had good progress)
 971 
 972       size_t original_count = shenandoah_policy()->full_gc_count();
 973       while ((result == nullptr) && (original_count == shenandoah_policy()->full_gc_count())) {
 974         control_thread()->handle_alloc_failure(req, true);
 975         result = allocate_memory_under_lock(req, in_new_region);
 976       }
 977       if (result != nullptr) {
 978         // If our allocation request has been satisifed after it initially failed, we count this as good gc progress
 979         notify_gc_progress();
 980       }
 981       if (log_develop_is_enabled(Debug, gc, alloc)) {
 982         ResourceMark rm;
 983         log_debug(gc, alloc)("Thread: %s, Result: " PTR_FORMAT ", Request: %s, Size: " SIZE_FORMAT
 984                              ", Original: " SIZE_FORMAT ", Latest: " SIZE_FORMAT,
 985                              Thread::current()->name(), p2i(result), req.type_string(), req.size(),
 986                              original_count, get_gc_no_progress_count());
 987       }
 988     }
 989   } else {
 990     assert(req.is_gc_alloc(), "Can only accept GC allocs here");
 991     result = allocate_memory_under_lock(req, in_new_region);
 992     // Do not call handle_alloc_failure() here, because we cannot block.
 993     // The allocation failure would be handled by the LRB slowpath with handle_alloc_failure_evac().
 994   }
 995 
 996   if (in_new_region) {
 997     notify_heap_changed();
 998   }
 999 
1000   if (result == nullptr) {
1001     req.set_actual_size(0);
1002   }
1003 
1004   // This is called regardless of the outcome of the allocation to account
1005   // for any waste created by retiring regions with this request.
1006   increase_used(req);
1007 
1008   if (result != nullptr) {
1009     size_t requested = req.size();
1010     size_t actual = req.actual_size();
1011 
1012     assert (req.is_lab_alloc() || (requested == actual),
1013             "Only LAB allocations are elastic: %s, requested = " SIZE_FORMAT ", actual = " SIZE_FORMAT,
1014             ShenandoahAllocRequest::alloc_type_to_string(req.type()), requested, actual);
1015 
1016     if (req.is_mutator_alloc()) {
1017       // If we requested more than we were granted, give the rest back to pacer.
1018       // This only matters if we are in the same pacing epoch: do not try to unpace
1019       // over the budget for the other phase.
1020       if (ShenandoahPacing && (pacer_epoch > 0) && (requested > actual)) {
1021         pacer()->unpace_for_alloc(pacer_epoch, requested - actual);
1022       }
1023     }
1024   }
1025 
1026   return result;
1027 }
1028 
1029 HeapWord* ShenandoahHeap::allocate_memory_under_lock(ShenandoahAllocRequest& req, bool& in_new_region) {
1030   // If we are dealing with mutator allocation, then we may need to block for safepoint.
1031   // We cannot block for safepoint for GC allocations, because there is a high chance
1032   // we are already running at safepoint or from stack watermark machinery, and we cannot
1033   // block again.
1034   ShenandoahHeapLocker locker(lock(), req.is_mutator_alloc());
1035 
1036   // Make sure the old generation has room for either evacuations or promotions before trying to allocate.
1037   if (req.is_old() && !old_generation()->can_allocate(req)) {
1038     return nullptr;
1039   }
1040 
1041   // If TLAB request size is greater than available, allocate() will attempt to downsize request to fit within available
1042   // memory.
1043   HeapWord* result = _free_set->allocate(req, in_new_region);
1044 
1045   // Record the plab configuration for this result and register the object.
1046   if (result != nullptr && req.is_old()) {
1047     old_generation()->configure_plab_for_current_thread(req);
1048     if (req.type() == ShenandoahAllocRequest::_alloc_shared_gc) {
1049       // Register the newly allocated object while we're holding the global lock since there's no synchronization
1050       // built in to the implementation of register_object().  There are potential races when multiple independent
1051       // threads are allocating objects, some of which might span the same card region.  For example, consider
1052       // a card table's memory region within which three objects are being allocated by three different threads:
1053       //
1054       // objects being "concurrently" allocated:
1055       //    [-----a------][-----b-----][--------------c------------------]
1056       //            [---- card table memory range --------------]
1057       //
1058       // Before any objects are allocated, this card's memory range holds no objects.  Note that allocation of object a
1059       // wants to set the starts-object, first-start, and last-start attributes of the preceding card region.
1060       // Allocation of object b wants to set the starts-object, first-start, and last-start attributes of this card region.
1061       // Allocation of object c also wants to set the starts-object, first-start, and last-start attributes of this
1062       // card region.
1063       //
1064       // The thread allocating b and the thread allocating c can "race" in various ways, resulting in confusion, such as
1065       // last-start representing object b while first-start represents object c.  This is why we need to require all
1066       // register_object() invocations to be "mutually exclusive" with respect to each card's memory range.
1067       old_generation()->card_scan()->register_object(result);
1068     }
1069   }
1070 
1071   return result;
1072 }
1073 
1074 HeapWord* ShenandoahHeap::mem_allocate(size_t size,
1075                                         bool*  gc_overhead_limit_was_exceeded) {
1076   ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared(size);
1077   return allocate_memory(req);
1078 }
1079 
1080 MetaWord* ShenandoahHeap::satisfy_failed_metadata_allocation(ClassLoaderData* loader_data,
1081                                                              size_t size,
1082                                                              Metaspace::MetadataType mdtype) {
1083   MetaWord* result;
1084 
1085   // Inform metaspace OOM to GC heuristics if class unloading is possible.
1086   ShenandoahHeuristics* h = global_generation()->heuristics();
1087   if (h->can_unload_classes()) {
1088     h->record_metaspace_oom();
1089   }
1090 
1091   // Expand and retry allocation
1092   result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);
1093   if (result != nullptr) {
1094     return result;
1095   }
1096 
1097   // Start full GC
1098   collect(GCCause::_metadata_GC_clear_soft_refs);
1099 
1100   // Retry allocation
1101   result = loader_data->metaspace_non_null()->allocate(size, mdtype);
1102   if (result != nullptr) {
1103     return result;
1104   }
1105 
1106   // Expand and retry allocation
1107   result = loader_data->metaspace_non_null()->expand_and_allocate(size, mdtype);
1108   if (result != nullptr) {
1109     return result;
1110   }
1111 
1112   // Out of memory
1113   return nullptr;
1114 }
1115 
1116 class ShenandoahConcurrentEvacuateRegionObjectClosure : public ObjectClosure {
1117 private:
1118   ShenandoahHeap* const _heap;
1119   Thread* const _thread;
1120 public:
1121   ShenandoahConcurrentEvacuateRegionObjectClosure(ShenandoahHeap* heap) :
1122     _heap(heap), _thread(Thread::current()) {}
1123 
1124   void do_object(oop p) {
1125     shenandoah_assert_marked(nullptr, p);
1126     if (!p->is_forwarded()) {
1127       _heap->evacuate_object(p, _thread);
1128     }
1129   }
1130 };
1131 
1132 class ShenandoahEvacuationTask : public WorkerTask {
1133 private:
1134   ShenandoahHeap* const _sh;
1135   ShenandoahCollectionSet* const _cs;
1136   bool _concurrent;
1137 public:
1138   ShenandoahEvacuationTask(ShenandoahHeap* sh,
1139                            ShenandoahCollectionSet* cs,
1140                            bool concurrent) :
1141     WorkerTask("Shenandoah Evacuation"),
1142     _sh(sh),
1143     _cs(cs),
1144     _concurrent(concurrent)
1145   {}
1146 
1147   void work(uint worker_id) {
1148     if (_concurrent) {
1149       ShenandoahConcurrentWorkerSession worker_session(worker_id);
1150       ShenandoahSuspendibleThreadSetJoiner stsj;
1151       ShenandoahEvacOOMScope oom_evac_scope;
1152       do_work();
1153     } else {
1154       ShenandoahParallelWorkerSession worker_session(worker_id);
1155       ShenandoahEvacOOMScope oom_evac_scope;
1156       do_work();
1157     }
1158   }
1159 
1160 private:
1161   void do_work() {
1162     ShenandoahConcurrentEvacuateRegionObjectClosure cl(_sh);
1163     ShenandoahHeapRegion* r;
1164     while ((r =_cs->claim_next()) != nullptr) {
1165       assert(r->has_live(), "Region " SIZE_FORMAT " should have been reclaimed early", r->index());
1166       _sh->marked_object_iterate(r, &cl);
1167 
1168       if (ShenandoahPacing) {
1169         _sh->pacer()->report_evac(r->used() >> LogHeapWordSize);
1170       }
1171 
1172       if (_sh->check_cancelled_gc_and_yield(_concurrent)) {
1173         break;
1174       }
1175     }
1176   }
1177 };
1178 
1179 void ShenandoahHeap::evacuate_collection_set(bool concurrent) {
1180   ShenandoahEvacuationTask task(this, _collection_set, concurrent);
1181   workers()->run_task(&task);
1182 }
1183 
1184 oop ShenandoahHeap::evacuate_object(oop p, Thread* thread) {
1185   assert(thread == Thread::current(), "Expected thread parameter to be current thread.");
1186   if (ShenandoahThreadLocalData::is_oom_during_evac(thread)) {
1187     // This thread went through the OOM during evac protocol. It is safe to return
1188     // the forward pointer. It must not attempt to evacuate any other objects.
1189     return ShenandoahBarrierSet::resolve_forwarded(p);
1190   }
1191 
1192   assert(ShenandoahThreadLocalData::is_evac_allowed(thread), "must be enclosed in oom-evac scope");
1193 
1194   ShenandoahHeapRegion* r = heap_region_containing(p);
1195   assert(!r->is_humongous(), "never evacuate humongous objects");
1196 
1197   ShenandoahAffiliation target_gen = r->affiliation();
1198   return try_evacuate_object(p, thread, r, target_gen);
1199 }
1200 
1201 oop ShenandoahHeap::try_evacuate_object(oop p, Thread* thread, ShenandoahHeapRegion* from_region,
1202                                                ShenandoahAffiliation target_gen) {
1203   assert(target_gen == YOUNG_GENERATION, "Only expect evacuations to young in this mode");
1204   assert(from_region->is_young(), "Only expect evacuations from young in this mode");
1205   bool alloc_from_lab = true;
1206   HeapWord* copy = nullptr;
1207 
1208   markWord mark = p->mark();
1209   if (ShenandoahForwarding::is_forwarded(mark)) {
1210     return ShenandoahForwarding::get_forwardee(p);
1211   }
1212   size_t old_size = ShenandoahForwarding::size(p);
1213   size_t size = p->copy_size(old_size, mark);
1214 
1215 #ifdef ASSERT
1216   if (ShenandoahOOMDuringEvacALot &&
1217       (os::random() & 1) == 0) { // Simulate OOM every ~2nd slow-path call
1218     copy = nullptr;
1219   } else {
1220 #endif
1221     if (UseTLAB) {
1222       copy = allocate_from_gclab(thread, size);
1223     }
1224     if (copy == nullptr) {
1225       // If we failed to allocate in LAB, we'll try a shared allocation.
1226       ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared_gc(size, target_gen);
1227       copy = allocate_memory(req);
1228       alloc_from_lab = false;
1229     }
1230 #ifdef ASSERT
1231   }
1232 #endif
1233 
1234   if (copy == nullptr) {
1235     control_thread()->handle_alloc_failure_evac(size);
1236 
1237     _oom_evac_handler.handle_out_of_memory_during_evacuation();
1238 
1239     return ShenandoahBarrierSet::resolve_forwarded(p);
1240   }
1241 
1242   // Copy the object:
1243   Copy::aligned_disjoint_words(cast_from_oop<HeapWord*>(p), copy, old_size);
1244 
1245   // Try to install the new forwarding pointer.
1246   oop copy_val = cast_to_oop(copy);
1247   oop result = ShenandoahForwarding::try_update_forwardee(p, copy_val);
1248   if (result == copy_val) {
1249     // Successfully evacuated. Our copy is now the public one!
1250     copy_val->initialize_hash_if_necessary(p);
1251     ContinuationGCSupport::relativize_stack_chunk(copy_val);
1252     shenandoah_assert_correct(nullptr, copy_val);
1253     return copy_val;
1254   }  else {
1255     // Failed to evacuate. We need to deal with the object that is left behind. Since this
1256     // new allocation is certainly after TAMS, it will be considered live in the next cycle.
1257     // But if it happens to contain references to evacuated regions, those references would
1258     // not get updated for this stale copy during this cycle, and we will crash while scanning
1259     // it the next cycle.
1260     if (alloc_from_lab) {
1261       // For LAB allocations, it is enough to rollback the allocation ptr. Either the next
1262       // object will overwrite this stale copy, or the filler object on LAB retirement will
1263       // do this.
1264       ShenandoahThreadLocalData::gclab(thread)->undo_allocation(copy, size);
1265     } else {
1266       // For non-LAB allocations, we have no way to retract the allocation, and
1267       // have to explicitly overwrite the copy with the filler object. With that overwrite,
1268       // we have to keep the fwdptr initialized and pointing to our (stale) copy.
1269       assert(size >= ShenandoahHeap::min_fill_size(), "previously allocated object known to be larger than min_size");
1270       fill_with_object(copy, size);
1271       shenandoah_assert_correct(nullptr, copy_val);
1272       // For non-LAB allocations, the object has already been registered
1273     }
1274     shenandoah_assert_correct(nullptr, result);
1275     return result;
1276   }
1277 }
1278 
1279 void ShenandoahHeap::trash_cset_regions() {
1280   ShenandoahHeapLocker locker(lock());
1281 
1282   ShenandoahCollectionSet* set = collection_set();
1283   ShenandoahHeapRegion* r;
1284   set->clear_current_index();
1285   while ((r = set->next()) != nullptr) {
1286     r->make_trash();
1287   }
1288   collection_set()->clear();
1289 }
1290 
1291 void ShenandoahHeap::print_heap_regions_on(outputStream* st) const {
1292   st->print_cr("Heap Regions:");
1293   st->print_cr("Region state: EU=empty-uncommitted, EC=empty-committed, R=regular, H=humongous start, HP=pinned humongous start");
1294   st->print_cr("              HC=humongous continuation, CS=collection set, TR=trash, P=pinned, CSP=pinned collection set");
1295   st->print_cr("BTE=bottom/top/end, TAMS=top-at-mark-start");
1296   st->print_cr("UWM=update watermark, U=used");
1297   st->print_cr("T=TLAB allocs, G=GCLAB allocs");
1298   st->print_cr("S=shared allocs, L=live data");
1299   st->print_cr("CP=critical pins");
1300 
1301   for (size_t i = 0; i < num_regions(); i++) {
1302     get_region(i)->print_on(st);
1303   }
1304 }
1305 
1306 size_t ShenandoahHeap::trash_humongous_region_at(ShenandoahHeapRegion* start) {
1307   assert(start->is_humongous_start(), "reclaim regions starting with the first one");
1308 
1309   oop humongous_obj = cast_to_oop(start->bottom());
1310   size_t size = humongous_obj->size();
1311   size_t required_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);
1312   size_t index = start->index() + required_regions - 1;
1313 
1314   assert(!start->has_live(), "liveness must be zero");
1315 
1316   for(size_t i = 0; i < required_regions; i++) {
1317     // Reclaim from tail. Otherwise, assertion fails when printing region to trace log,
1318     // as it expects that every region belongs to a humongous region starting with a humongous start region.
1319     ShenandoahHeapRegion* region = get_region(index --);
1320 
1321     assert(region->is_humongous(), "expect correct humongous start or continuation");
1322     assert(!region->is_cset(), "Humongous region should not be in collection set");
1323 
1324     region->make_trash_immediate();
1325   }
1326   return required_regions;
1327 }
1328 
1329 class ShenandoahCheckCleanGCLABClosure : public ThreadClosure {
1330 public:
1331   ShenandoahCheckCleanGCLABClosure() {}
1332   void do_thread(Thread* thread) {
1333     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
1334     assert(gclab != nullptr, "GCLAB should be initialized for %s", thread->name());
1335     assert(gclab->words_remaining() == 0, "GCLAB should not need retirement");
1336 
1337     if (ShenandoahHeap::heap()->mode()->is_generational()) {
1338       PLAB* plab = ShenandoahThreadLocalData::plab(thread);
1339       assert(plab != nullptr, "PLAB should be initialized for %s", thread->name());
1340       assert(plab->words_remaining() == 0, "PLAB should not need retirement");
1341     }
1342   }
1343 };
1344 
1345 class ShenandoahRetireGCLABClosure : public ThreadClosure {
1346 private:
1347   bool const _resize;
1348 public:
1349   ShenandoahRetireGCLABClosure(bool resize) : _resize(resize) {}
1350   void do_thread(Thread* thread) {
1351     PLAB* gclab = ShenandoahThreadLocalData::gclab(thread);
1352     assert(gclab != nullptr, "GCLAB should be initialized for %s", thread->name());
1353     gclab->retire();
1354     if (_resize && ShenandoahThreadLocalData::gclab_size(thread) > 0) {
1355       ShenandoahThreadLocalData::set_gclab_size(thread, 0);
1356     }
1357 
1358     if (ShenandoahHeap::heap()->mode()->is_generational()) {
1359       PLAB* plab = ShenandoahThreadLocalData::plab(thread);
1360       assert(plab != nullptr, "PLAB should be initialized for %s", thread->name());
1361 
1362       // There are two reasons to retire all plabs between old-gen evacuation passes.
1363       //  1. We need to make the plab memory parsable by remembered-set scanning.
1364       //  2. We need to establish a trustworthy UpdateWaterMark value within each old-gen heap region
1365       ShenandoahGenerationalHeap::heap()->retire_plab(plab, thread);
1366       if (_resize && ShenandoahThreadLocalData::plab_size(thread) > 0) {
1367         ShenandoahThreadLocalData::set_plab_size(thread, 0);
1368       }
1369     }
1370   }
1371 };
1372 
1373 void ShenandoahHeap::labs_make_parsable() {
1374   assert(UseTLAB, "Only call with UseTLAB");
1375 
1376   ShenandoahRetireGCLABClosure cl(false);
1377 
1378   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
1379     ThreadLocalAllocBuffer& tlab = t->tlab();
1380     tlab.make_parsable();
1381     cl.do_thread(t);
1382   }
1383 
1384   workers()->threads_do(&cl);
1385 }
1386 
1387 void ShenandoahHeap::tlabs_retire(bool resize) {
1388   assert(UseTLAB, "Only call with UseTLAB");
1389   assert(!resize || ResizeTLAB, "Only call for resize when ResizeTLAB is enabled");
1390 
1391   ThreadLocalAllocStats stats;
1392 
1393   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
1394     ThreadLocalAllocBuffer& tlab = t->tlab();
1395     tlab.retire(&stats);
1396     if (resize) {
1397       tlab.resize();
1398     }
1399   }
1400 
1401   stats.publish();
1402 
1403 #ifdef ASSERT
1404   ShenandoahCheckCleanGCLABClosure cl;
1405   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
1406     cl.do_thread(t);
1407   }
1408   workers()->threads_do(&cl);
1409 #endif
1410 }
1411 
1412 void ShenandoahHeap::gclabs_retire(bool resize) {
1413   assert(UseTLAB, "Only call with UseTLAB");
1414   assert(!resize || ResizeTLAB, "Only call for resize when ResizeTLAB is enabled");
1415 
1416   ShenandoahRetireGCLABClosure cl(resize);
1417   for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
1418     cl.do_thread(t);
1419   }
1420   workers()->threads_do(&cl);
1421 
1422   if (safepoint_workers() != nullptr) {
1423     safepoint_workers()->threads_do(&cl);
1424   }
1425 }
1426 
1427 // Returns size in bytes
1428 size_t ShenandoahHeap::unsafe_max_tlab_alloc(Thread *thread) const {
1429   // Return the max allowed size, and let the allocation path
1430   // figure out the safe size for current allocation.
1431   return ShenandoahHeapRegion::max_tlab_size_bytes();
1432 }
1433 
1434 size_t ShenandoahHeap::max_tlab_size() const {
1435   // Returns size in words
1436   return ShenandoahHeapRegion::max_tlab_size_words();
1437 }
1438 
1439 void ShenandoahHeap::collect(GCCause::Cause cause) {
1440   control_thread()->request_gc(cause);
1441 }
1442 
1443 void ShenandoahHeap::do_full_collection(bool clear_all_soft_refs) {
1444   //assert(false, "Shouldn't need to do full collections");
1445 }
1446 
1447 HeapWord* ShenandoahHeap::block_start(const void* addr) const {
1448   ShenandoahHeapRegion* r = heap_region_containing(addr);
1449   if (r != nullptr) {
1450     return r->block_start(addr);
1451   }
1452   return nullptr;
1453 }
1454 
1455 bool ShenandoahHeap::block_is_obj(const HeapWord* addr) const {
1456   ShenandoahHeapRegion* r = heap_region_containing(addr);
1457   return r->block_is_obj(addr);
1458 }
1459 
1460 bool ShenandoahHeap::print_location(outputStream* st, void* addr) const {
1461   return BlockLocationPrinter<ShenandoahHeap>::print_location(st, addr);
1462 }
1463 
1464 void ShenandoahHeap::prepare_for_verify() {
1465   if (SafepointSynchronize::is_at_safepoint() && UseTLAB) {
1466     labs_make_parsable();
1467   }
1468 }
1469 
1470 void ShenandoahHeap::gc_threads_do(ThreadClosure* tcl) const {
1471   if (_shenandoah_policy->is_at_shutdown()) {
1472     return;
1473   }
1474 
1475   if (_control_thread != nullptr) {
1476     tcl->do_thread(_control_thread);
1477   }
1478 
1479   if (_uncommit_thread != nullptr) {
1480     tcl->do_thread(_uncommit_thread);
1481   }
1482 
1483   workers()->threads_do(tcl);
1484   if (_safepoint_workers != nullptr) {
1485     _safepoint_workers->threads_do(tcl);
1486   }
1487 }
1488 
1489 void ShenandoahHeap::print_tracing_info() const {
1490   LogTarget(Info, gc, stats) lt;
1491   if (lt.is_enabled()) {
1492     ResourceMark rm;
1493     LogStream ls(lt);
1494 
1495     phase_timings()->print_global_on(&ls);
1496 
1497     ls.cr();
1498     ls.cr();
1499 
1500     shenandoah_policy()->print_gc_stats(&ls);
1501 
1502     ls.cr();
1503     ls.cr();
1504   }
1505 }
1506 
1507 void ShenandoahHeap::set_gc_generation(ShenandoahGeneration* generation) {
1508   shenandoah_assert_control_or_vm_thread_at_safepoint();
1509   _gc_generation = generation;
1510 }
1511 
1512 // Active generation may only be set by the VM thread at a safepoint.
1513 void ShenandoahHeap::set_active_generation() {
1514   assert(Thread::current()->is_VM_thread(), "Only the VM Thread");
1515   assert(SafepointSynchronize::is_at_safepoint(), "Only at a safepoint!");
1516   assert(_gc_generation != nullptr, "Will set _active_generation to nullptr");
1517   _active_generation = _gc_generation;
1518 }
1519 
1520 void ShenandoahHeap::on_cycle_start(GCCause::Cause cause, ShenandoahGeneration* generation) {
1521   shenandoah_policy()->record_collection_cause(cause);
1522 
1523   assert(gc_cause()  == GCCause::_no_gc, "Over-writing cause");
1524   assert(_gc_generation == nullptr, "Over-writing _gc_generation");
1525 
1526   set_gc_cause(cause);
1527   set_gc_generation(generation);
1528 
1529   generation->heuristics()->record_cycle_start();
1530 }
1531 
1532 void ShenandoahHeap::on_cycle_end(ShenandoahGeneration* generation) {
1533   assert(gc_cause() != GCCause::_no_gc, "cause wasn't set");
1534   assert(_gc_generation != nullptr, "_gc_generation wasn't set");
1535 
1536   generation->heuristics()->record_cycle_end();
1537   if (mode()->is_generational() && generation->is_global()) {
1538     // If we just completed a GLOBAL GC, claim credit for completion of young-gen and old-gen GC as well
1539     young_generation()->heuristics()->record_cycle_end();
1540     old_generation()->heuristics()->record_cycle_end();
1541   }
1542 
1543   set_gc_generation(nullptr);
1544   set_gc_cause(GCCause::_no_gc);
1545 }
1546 
1547 void ShenandoahHeap::verify(VerifyOption vo) {
1548   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
1549     if (ShenandoahVerify) {
1550       verifier()->verify_generic(vo);
1551     } else {
1552       // TODO: Consider allocating verification bitmaps on demand,
1553       // and turn this on unconditionally.
1554     }
1555   }
1556 }
1557 size_t ShenandoahHeap::tlab_capacity(Thread *thr) const {
1558   return _free_set->capacity();
1559 }
1560 
1561 class ObjectIterateScanRootClosure : public BasicOopIterateClosure {
1562 private:
1563   MarkBitMap* _bitmap;
1564   ShenandoahScanObjectStack* _oop_stack;
1565   ShenandoahHeap* const _heap;
1566   ShenandoahMarkingContext* const _marking_context;
1567 
1568   template <class T>
1569   void do_oop_work(T* p) {
1570     T o = RawAccess<>::oop_load(p);
1571     if (!CompressedOops::is_null(o)) {
1572       oop obj = CompressedOops::decode_not_null(o);
1573       if (_heap->is_concurrent_weak_root_in_progress() && !_marking_context->is_marked(obj)) {
1574         // There may be dead oops in weak roots in concurrent root phase, do not touch them.
1575         return;
1576       }
1577       obj = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(obj);
1578 
1579       assert(oopDesc::is_oop(obj), "must be a valid oop");
1580       if (!_bitmap->is_marked(obj)) {
1581         _bitmap->mark(obj);
1582         _oop_stack->push(obj);
1583       }
1584     }
1585   }
1586 public:
1587   ObjectIterateScanRootClosure(MarkBitMap* bitmap, ShenandoahScanObjectStack* oop_stack) :
1588     _bitmap(bitmap), _oop_stack(oop_stack), _heap(ShenandoahHeap::heap()),
1589     _marking_context(_heap->marking_context()) {}
1590   void do_oop(oop* p)       { do_oop_work(p); }
1591   void do_oop(narrowOop* p) { do_oop_work(p); }
1592 };
1593 
1594 /*
1595  * This is public API, used in preparation of object_iterate().
1596  * Since we don't do linear scan of heap in object_iterate() (see comment below), we don't
1597  * need to make the heap parsable. For Shenandoah-internal linear heap scans that we can
1598  * control, we call SH::tlabs_retire, SH::gclabs_retire.
1599  */
1600 void ShenandoahHeap::ensure_parsability(bool retire_tlabs) {
1601   // No-op.
1602 }
1603 
1604 /*
1605  * Iterates objects in the heap. This is public API, used for, e.g., heap dumping.
1606  *
1607  * We cannot safely iterate objects by doing a linear scan at random points in time. Linear
1608  * scanning needs to deal with dead objects, which may have dead Klass* pointers (e.g.
1609  * calling oopDesc::size() would crash) or dangling reference fields (crashes) etc. Linear
1610  * scanning therefore depends on having a valid marking bitmap to support it. However, we only
1611  * have a valid marking bitmap after successful marking. In particular, we *don't* have a valid
1612  * marking bitmap during marking, after aborted marking or during/after cleanup (when we just
1613  * wiped the bitmap in preparation for next marking).
1614  *
1615  * For all those reasons, we implement object iteration as a single marking traversal, reporting
1616  * objects as we mark+traverse through the heap, starting from GC roots. JVMTI IterateThroughHeap
1617  * is allowed to report dead objects, but is not required to do so.
1618  */
1619 void ShenandoahHeap::object_iterate(ObjectClosure* cl) {
1620   // Reset bitmap
1621   if (!prepare_aux_bitmap_for_iteration())
1622     return;
1623 
1624   ShenandoahScanObjectStack oop_stack;
1625   ObjectIterateScanRootClosure oops(&_aux_bit_map, &oop_stack);
1626   // Seed the stack with root scan
1627   scan_roots_for_iteration(&oop_stack, &oops);
1628 
1629   // Work through the oop stack to traverse heap
1630   while (! oop_stack.is_empty()) {
1631     oop obj = oop_stack.pop();
1632     assert(oopDesc::is_oop(obj), "must be a valid oop");
1633     cl->do_object(obj);
1634     obj->oop_iterate(&oops);
1635   }
1636 
1637   assert(oop_stack.is_empty(), "should be empty");
1638   // Reclaim bitmap
1639   reclaim_aux_bitmap_for_iteration();
1640 }
1641 
1642 bool ShenandoahHeap::prepare_aux_bitmap_for_iteration() {
1643   assert(SafepointSynchronize::is_at_safepoint(), "safe iteration is only available during safepoints");
1644 
1645   if (!_aux_bitmap_region_special && !os::commit_memory((char*)_aux_bitmap_region.start(), _aux_bitmap_region.byte_size(), false)) {
1646     log_warning(gc)("Could not commit native memory for auxiliary marking bitmap for heap iteration");
1647     return false;
1648   }
1649   // Reset bitmap
1650   _aux_bit_map.clear();
1651   return true;
1652 }
1653 
1654 void ShenandoahHeap::scan_roots_for_iteration(ShenandoahScanObjectStack* oop_stack, ObjectIterateScanRootClosure* oops) {
1655   // Process GC roots according to current GC cycle
1656   // This populates the work stack with initial objects
1657   // It is important to relinquish the associated locks before diving
1658   // into heap dumper
1659   uint n_workers = safepoint_workers() != nullptr ? safepoint_workers()->active_workers() : 1;
1660   ShenandoahHeapIterationRootScanner rp(n_workers);
1661   rp.roots_do(oops);
1662 }
1663 
1664 void ShenandoahHeap::reclaim_aux_bitmap_for_iteration() {
1665   if (!_aux_bitmap_region_special && !os::uncommit_memory((char*)_aux_bitmap_region.start(), _aux_bitmap_region.byte_size())) {
1666     log_warning(gc)("Could not uncommit native memory for auxiliary marking bitmap for heap iteration");
1667   }
1668 }
1669 
1670 // Closure for parallelly iterate objects
1671 class ShenandoahObjectIterateParScanClosure : public BasicOopIterateClosure {
1672 private:
1673   MarkBitMap* _bitmap;
1674   ShenandoahObjToScanQueue* _queue;
1675   ShenandoahHeap* const _heap;
1676   ShenandoahMarkingContext* const _marking_context;
1677 
1678   template <class T>
1679   void do_oop_work(T* p) {
1680     T o = RawAccess<>::oop_load(p);
1681     if (!CompressedOops::is_null(o)) {
1682       oop obj = CompressedOops::decode_not_null(o);
1683       if (_heap->is_concurrent_weak_root_in_progress() && !_marking_context->is_marked(obj)) {
1684         // There may be dead oops in weak roots in concurrent root phase, do not touch them.
1685         return;
1686       }
1687       obj = ShenandoahBarrierSet::barrier_set()->load_reference_barrier(obj);
1688 
1689       assert(oopDesc::is_oop(obj), "Must be a valid oop");
1690       if (_bitmap->par_mark(obj)) {
1691         _queue->push(ShenandoahMarkTask(obj));
1692       }
1693     }
1694   }
1695 public:
1696   ShenandoahObjectIterateParScanClosure(MarkBitMap* bitmap, ShenandoahObjToScanQueue* q) :
1697     _bitmap(bitmap), _queue(q), _heap(ShenandoahHeap::heap()),
1698     _marking_context(_heap->marking_context()) {}
1699   void do_oop(oop* p)       { do_oop_work(p); }
1700   void do_oop(narrowOop* p) { do_oop_work(p); }
1701 };
1702 
1703 // Object iterator for parallel heap iteraion.
1704 // The root scanning phase happenes in construction as a preparation of
1705 // parallel marking queues.
1706 // Every worker processes it's own marking queue. work-stealing is used
1707 // to balance workload.
1708 class ShenandoahParallelObjectIterator : public ParallelObjectIteratorImpl {
1709 private:
1710   uint                         _num_workers;
1711   bool                         _init_ready;
1712   MarkBitMap*                  _aux_bit_map;
1713   ShenandoahHeap*              _heap;
1714   ShenandoahScanObjectStack    _roots_stack; // global roots stack
1715   ShenandoahObjToScanQueueSet* _task_queues;
1716 public:
1717   ShenandoahParallelObjectIterator(uint num_workers, MarkBitMap* bitmap) :
1718         _num_workers(num_workers),
1719         _init_ready(false),
1720         _aux_bit_map(bitmap),
1721         _heap(ShenandoahHeap::heap()) {
1722     // Initialize bitmap
1723     _init_ready = _heap->prepare_aux_bitmap_for_iteration();
1724     if (!_init_ready) {
1725       return;
1726     }
1727 
1728     ObjectIterateScanRootClosure oops(_aux_bit_map, &_roots_stack);
1729     _heap->scan_roots_for_iteration(&_roots_stack, &oops);
1730 
1731     _init_ready = prepare_worker_queues();
1732   }
1733 
1734   ~ShenandoahParallelObjectIterator() {
1735     // Reclaim bitmap
1736     _heap->reclaim_aux_bitmap_for_iteration();
1737     // Reclaim queue for workers
1738     if (_task_queues!= nullptr) {
1739       for (uint i = 0; i < _num_workers; ++i) {
1740         ShenandoahObjToScanQueue* q = _task_queues->queue(i);
1741         if (q != nullptr) {
1742           delete q;
1743           _task_queues->register_queue(i, nullptr);
1744         }
1745       }
1746       delete _task_queues;
1747       _task_queues = nullptr;
1748     }
1749   }
1750 
1751   virtual void object_iterate(ObjectClosure* cl, uint worker_id) {
1752     if (_init_ready) {
1753       object_iterate_parallel(cl, worker_id, _task_queues);
1754     }
1755   }
1756 
1757 private:
1758   // Divide global root_stack into worker queues
1759   bool prepare_worker_queues() {
1760     _task_queues = new ShenandoahObjToScanQueueSet((int) _num_workers);
1761     // Initialize queues for every workers
1762     for (uint i = 0; i < _num_workers; ++i) {
1763       ShenandoahObjToScanQueue* task_queue = new ShenandoahObjToScanQueue();
1764       _task_queues->register_queue(i, task_queue);
1765     }
1766     // Divide roots among the workers. Assume that object referencing distribution
1767     // is related with root kind, use round-robin to make every worker have same chance
1768     // to process every kind of roots
1769     size_t roots_num = _roots_stack.size();
1770     if (roots_num == 0) {
1771       // No work to do
1772       return false;
1773     }
1774 
1775     for (uint j = 0; j < roots_num; j++) {
1776       uint stack_id = j % _num_workers;
1777       oop obj = _roots_stack.pop();
1778       _task_queues->queue(stack_id)->push(ShenandoahMarkTask(obj));
1779     }
1780     return true;
1781   }
1782 
1783   void object_iterate_parallel(ObjectClosure* cl,
1784                                uint worker_id,
1785                                ShenandoahObjToScanQueueSet* queue_set) {
1786     assert(SafepointSynchronize::is_at_safepoint(), "safe iteration is only available during safepoints");
1787     assert(queue_set != nullptr, "task queue must not be null");
1788 
1789     ShenandoahObjToScanQueue* q = queue_set->queue(worker_id);
1790     assert(q != nullptr, "object iterate queue must not be null");
1791 
1792     ShenandoahMarkTask t;
1793     ShenandoahObjectIterateParScanClosure oops(_aux_bit_map, q);
1794 
1795     // Work through the queue to traverse heap.
1796     // Steal when there is no task in queue.
1797     while (q->pop(t) || queue_set->steal(worker_id, t)) {
1798       oop obj = t.obj();
1799       assert(oopDesc::is_oop(obj), "must be a valid oop");
1800       cl->do_object(obj);
1801       obj->oop_iterate(&oops);
1802     }
1803     assert(q->is_empty(), "should be empty");
1804   }
1805 };
1806 
1807 ParallelObjectIteratorImpl* ShenandoahHeap::parallel_object_iterator(uint workers) {
1808   return new ShenandoahParallelObjectIterator(workers, &_aux_bit_map);
1809 }
1810 
1811 // Keep alive an object that was loaded with AS_NO_KEEPALIVE.
1812 void ShenandoahHeap::keep_alive(oop obj) {
1813   if (is_concurrent_mark_in_progress() && (obj != nullptr)) {
1814     ShenandoahBarrierSet::barrier_set()->enqueue(obj);
1815   }
1816 }
1817 
1818 void ShenandoahHeap::heap_region_iterate(ShenandoahHeapRegionClosure* blk) const {
1819   for (size_t i = 0; i < num_regions(); i++) {
1820     ShenandoahHeapRegion* current = get_region(i);
1821     blk->heap_region_do(current);
1822   }
1823 }
1824 
1825 class ShenandoahParallelHeapRegionTask : public WorkerTask {
1826 private:
1827   ShenandoahHeap* const _heap;
1828   ShenandoahHeapRegionClosure* const _blk;
1829   size_t const _stride;
1830 
1831   shenandoah_padding(0);
1832   volatile size_t _index;
1833   shenandoah_padding(1);
1834 
1835 public:
1836   ShenandoahParallelHeapRegionTask(ShenandoahHeapRegionClosure* blk, size_t stride) :
1837           WorkerTask("Shenandoah Parallel Region Operation"),
1838           _heap(ShenandoahHeap::heap()), _blk(blk), _stride(stride), _index(0) {}
1839 
1840   void work(uint worker_id) {
1841     ShenandoahParallelWorkerSession worker_session(worker_id);
1842     size_t stride = _stride;
1843 
1844     size_t max = _heap->num_regions();
1845     while (Atomic::load(&_index) < max) {
1846       size_t cur = Atomic::fetch_then_add(&_index, stride, memory_order_relaxed);
1847       size_t start = cur;
1848       size_t end = MIN2(cur + stride, max);
1849       if (start >= max) break;
1850 
1851       for (size_t i = cur; i < end; i++) {
1852         ShenandoahHeapRegion* current = _heap->get_region(i);
1853         _blk->heap_region_do(current);
1854       }
1855     }
1856   }
1857 };
1858 
1859 void ShenandoahHeap::parallel_heap_region_iterate(ShenandoahHeapRegionClosure* blk) const {
1860   assert(blk->is_thread_safe(), "Only thread-safe closures here");
1861   const uint active_workers = workers()->active_workers();
1862   const size_t n_regions = num_regions();
1863   size_t stride = ShenandoahParallelRegionStride;
1864   if (stride == 0 && active_workers > 1) {
1865     // Automatically derive the stride to balance the work between threads
1866     // evenly. Do not try to split work if below the reasonable threshold.
1867     constexpr size_t threshold = 4096;
1868     stride = n_regions <= threshold ?
1869             threshold :
1870             (n_regions + active_workers - 1) / active_workers;
1871   }
1872 
1873   if (n_regions > stride && active_workers > 1) {
1874     ShenandoahParallelHeapRegionTask task(blk, stride);
1875     workers()->run_task(&task);
1876   } else {
1877     heap_region_iterate(blk);
1878   }
1879 }
1880 
1881 class ShenandoahRendezvousClosure : public HandshakeClosure {
1882 public:
1883   inline ShenandoahRendezvousClosure(const char* name) : HandshakeClosure(name) {}
1884   inline void do_thread(Thread* thread) {}
1885 };
1886 
1887 void ShenandoahHeap::rendezvous_threads(const char* name) {
1888   ShenandoahRendezvousClosure cl(name);
1889   Handshake::execute(&cl);
1890 }
1891 
1892 void ShenandoahHeap::recycle_trash() {
1893   free_set()->recycle_trash();
1894 }
1895 
1896 void ShenandoahHeap::do_class_unloading() {
1897   _unloader.unload();
1898   if (mode()->is_generational()) {
1899     old_generation()->set_parsable(false);
1900   }
1901 }
1902 
1903 void ShenandoahHeap::stw_weak_refs(bool full_gc) {
1904   // Weak refs processing
1905   ShenandoahPhaseTimings::Phase phase = full_gc ? ShenandoahPhaseTimings::full_gc_weakrefs
1906                                                 : ShenandoahPhaseTimings::degen_gc_weakrefs;
1907   ShenandoahTimingsTracker t(phase);
1908   ShenandoahGCWorkerPhase worker_phase(phase);
1909   shenandoah_assert_generations_reconciled();
1910   gc_generation()->ref_processor()->process_references(phase, workers(), false /* concurrent */);
1911 }
1912 
1913 void ShenandoahHeap::prepare_update_heap_references(bool concurrent) {
1914   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "must be at safepoint");
1915 
1916   // Evacuation is over, no GCLABs are needed anymore. GCLABs are under URWM, so we need to
1917   // make them parsable for update code to work correctly. Plus, we can compute new sizes
1918   // for future GCLABs here.
1919   if (UseTLAB) {
1920     ShenandoahGCPhase phase(concurrent ?
1921                             ShenandoahPhaseTimings::init_update_refs_manage_gclabs :
1922                             ShenandoahPhaseTimings::degen_gc_init_update_refs_manage_gclabs);
1923     gclabs_retire(ResizeTLAB);
1924   }
1925 
1926   _update_refs_iterator.reset();
1927 }
1928 
1929 void ShenandoahHeap::propagate_gc_state_to_java_threads() {
1930   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at Shenandoah safepoint");
1931   if (_gc_state_changed) {
1932     _gc_state_changed = false;
1933     char state = gc_state();
1934     for (JavaThreadIteratorWithHandle jtiwh; JavaThread *t = jtiwh.next(); ) {
1935       ShenandoahThreadLocalData::set_gc_state(t, state);
1936     }
1937   }
1938 }
1939 
1940 void ShenandoahHeap::set_gc_state(uint mask, bool value) {
1941   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Must be at Shenandoah safepoint");
1942   _gc_state.set_cond(mask, value);
1943   _gc_state_changed = true;
1944 }
1945 
1946 void ShenandoahHeap::set_concurrent_young_mark_in_progress(bool in_progress) {
1947   uint mask;
1948   assert(!has_forwarded_objects(), "Young marking is not concurrent with evacuation");
1949   if (!in_progress && is_concurrent_old_mark_in_progress()) {
1950     assert(mode()->is_generational(), "Only generational GC has old marking");
1951     assert(_gc_state.is_set(MARKING), "concurrent_old_marking_in_progress implies MARKING");
1952     // If old-marking is in progress when we turn off YOUNG_MARKING, leave MARKING (and OLD_MARKING) on
1953     mask = YOUNG_MARKING;
1954   } else {
1955     mask = MARKING | YOUNG_MARKING;
1956   }
1957   set_gc_state(mask, in_progress);
1958   manage_satb_barrier(in_progress);
1959 }
1960 
1961 void ShenandoahHeap::set_concurrent_old_mark_in_progress(bool in_progress) {
1962 #ifdef ASSERT
1963   // has_forwarded_objects() iff UPDATEREFS or EVACUATION
1964   bool has_forwarded = has_forwarded_objects();
1965   bool updating_or_evacuating = _gc_state.is_set(UPDATEREFS | EVACUATION);
1966   bool evacuating = _gc_state.is_set(EVACUATION);
1967   assert ((has_forwarded == updating_or_evacuating) || (evacuating && !has_forwarded && collection_set()->is_empty()),
1968           "Updating or evacuating iff has forwarded objects, or if evacuation phase is promoting in place without forwarding");
1969 #endif
1970   if (!in_progress && is_concurrent_young_mark_in_progress()) {
1971     // If young-marking is in progress when we turn off OLD_MARKING, leave MARKING (and YOUNG_MARKING) on
1972     assert(_gc_state.is_set(MARKING), "concurrent_young_marking_in_progress implies MARKING");
1973     set_gc_state(OLD_MARKING, in_progress);
1974   } else {
1975     set_gc_state(MARKING | OLD_MARKING, in_progress);
1976   }
1977   manage_satb_barrier(in_progress);
1978 }
1979 
1980 bool ShenandoahHeap::is_prepare_for_old_mark_in_progress() const {
1981   return old_generation()->is_preparing_for_mark();
1982 }
1983 
1984 void ShenandoahHeap::manage_satb_barrier(bool active) {
1985   if (is_concurrent_mark_in_progress()) {
1986     // Ignore request to deactivate barrier while concurrent mark is in progress.
1987     // Do not attempt to re-activate the barrier if it is already active.
1988     if (active && !ShenandoahBarrierSet::satb_mark_queue_set().is_active()) {
1989       ShenandoahBarrierSet::satb_mark_queue_set().set_active_all_threads(active, !active);
1990     }
1991   } else {
1992     // No concurrent marking is in progress so honor request to deactivate,
1993     // but only if the barrier is already active.
1994     if (!active && ShenandoahBarrierSet::satb_mark_queue_set().is_active()) {
1995       ShenandoahBarrierSet::satb_mark_queue_set().set_active_all_threads(active, !active);
1996     }
1997   }
1998 }
1999 
2000 void ShenandoahHeap::set_evacuation_in_progress(bool in_progress) {
2001   assert(ShenandoahSafepoint::is_at_shenandoah_safepoint(), "Only call this at safepoint");
2002   set_gc_state(EVACUATION, in_progress);
2003 }
2004 
2005 void ShenandoahHeap::set_concurrent_strong_root_in_progress(bool in_progress) {
2006   if (in_progress) {
2007     _concurrent_strong_root_in_progress.set();
2008   } else {
2009     _concurrent_strong_root_in_progress.unset();
2010   }
2011 }
2012 
2013 void ShenandoahHeap::set_concurrent_weak_root_in_progress(bool cond) {
2014   set_gc_state(WEAK_ROOTS, cond);
2015 }
2016 
2017 GCTracer* ShenandoahHeap::tracer() {
2018   return shenandoah_policy()->tracer();
2019 }
2020 
2021 size_t ShenandoahHeap::tlab_used(Thread* thread) const {
2022   return _free_set->used();
2023 }
2024 
2025 bool ShenandoahHeap::try_cancel_gc() {
2026   jbyte prev = _cancelled_gc.cmpxchg(CANCELLED, CANCELLABLE);
2027   return prev == CANCELLABLE;
2028 }
2029 
2030 void ShenandoahHeap::cancel_concurrent_mark() {
2031   if (mode()->is_generational()) {
2032     young_generation()->cancel_marking();
2033     old_generation()->cancel_marking();
2034   }
2035 
2036   global_generation()->cancel_marking();
2037 
2038   ShenandoahBarrierSet::satb_mark_queue_set().abandon_partial_marking();
2039 }
2040 
2041 void ShenandoahHeap::cancel_gc(GCCause::Cause cause) {
2042   if (try_cancel_gc()) {
2043     FormatBuffer<> msg("Cancelling GC: %s", GCCause::to_string(cause));
2044     log_info(gc)("%s", msg.buffer());
2045     Events::log(Thread::current(), "%s", msg.buffer());
2046     _cancel_requested_time = os::elapsedTime();
2047   }
2048 }
2049 
2050 uint ShenandoahHeap::max_workers() {
2051   return _max_workers;
2052 }
2053 
2054 void ShenandoahHeap::stop() {
2055   // The shutdown sequence should be able to terminate when GC is running.
2056 
2057   // Step 0. Notify policy to disable event recording and prevent visiting gc threads during shutdown
2058   _shenandoah_policy->record_shutdown();
2059 
2060   // Step 1. Notify control thread that we are in shutdown.
2061   // Note that we cannot do that with stop(), because stop() is blocking and waits for the actual shutdown.
2062   // Doing stop() here would wait for the normal GC cycle to complete, never falling through to cancel below.
2063   control_thread()->prepare_for_graceful_shutdown();
2064 
2065   // Step 2. Notify GC workers that we are cancelling GC.
2066   cancel_gc(GCCause::_shenandoah_stop_vm);
2067 
2068   // Step 3. Wait until GC worker exits normally.
2069   control_thread()->stop();
2070 
2071   // Stop 4. Shutdown uncommit thread.
2072   if (_uncommit_thread != nullptr) {
2073     _uncommit_thread->stop();
2074   }
2075 }
2076 
2077 void ShenandoahHeap::stw_unload_classes(bool full_gc) {
2078   if (!unload_classes()) return;
2079   ClassUnloadingContext ctx(_workers->active_workers(),
2080                             true /* unregister_nmethods_during_purge */,
2081                             false /* lock_nmethod_free_separately */);
2082 
2083   // Unload classes and purge SystemDictionary.
2084   {
2085     ShenandoahPhaseTimings::Phase phase = full_gc ?
2086                                           ShenandoahPhaseTimings::full_gc_purge_class_unload :
2087                                           ShenandoahPhaseTimings::degen_gc_purge_class_unload;
2088     ShenandoahIsAliveSelector is_alive;
2089     {
2090       CodeCache::UnlinkingScope scope(is_alive.is_alive_closure());
2091       ShenandoahGCPhase gc_phase(phase);
2092       ShenandoahGCWorkerPhase worker_phase(phase);
2093       bool unloading_occurred = SystemDictionary::do_unloading(gc_timer());
2094 
2095       uint num_workers = _workers->active_workers();
2096       ShenandoahClassUnloadingTask unlink_task(phase, num_workers, unloading_occurred);
2097       _workers->run_task(&unlink_task);
2098     }
2099     // Release unloaded nmethods's memory.
2100     ClassUnloadingContext::context()->purge_and_free_nmethods();
2101   }
2102 
2103   {
2104     ShenandoahGCPhase phase(full_gc ?
2105                             ShenandoahPhaseTimings::full_gc_purge_cldg :
2106                             ShenandoahPhaseTimings::degen_gc_purge_cldg);
2107     ClassLoaderDataGraph::purge(true /* at_safepoint */);
2108   }
2109   // Resize and verify metaspace
2110   MetaspaceGC::compute_new_size();
2111   DEBUG_ONLY(MetaspaceUtils::verify();)
2112 }
2113 
2114 // Weak roots are either pre-evacuated (final mark) or updated (final updaterefs),
2115 // so they should not have forwarded oops.
2116 // However, we do need to "null" dead oops in the roots, if can not be done
2117 // in concurrent cycles.
2118 void ShenandoahHeap::stw_process_weak_roots(bool full_gc) {
2119   uint num_workers = _workers->active_workers();
2120   ShenandoahPhaseTimings::Phase timing_phase = full_gc ?
2121                                                ShenandoahPhaseTimings::full_gc_purge_weak_par :
2122                                                ShenandoahPhaseTimings::degen_gc_purge_weak_par;
2123   ShenandoahGCPhase phase(timing_phase);
2124   ShenandoahGCWorkerPhase worker_phase(timing_phase);
2125   // Cleanup weak roots
2126   if (has_forwarded_objects()) {
2127     ShenandoahForwardedIsAliveClosure is_alive;
2128     ShenandoahNonConcUpdateRefsClosure keep_alive;
2129     ShenandoahParallelWeakRootsCleaningTask<ShenandoahForwardedIsAliveClosure, ShenandoahNonConcUpdateRefsClosure>
2130       cleaning_task(timing_phase, &is_alive, &keep_alive, num_workers);
2131     _workers->run_task(&cleaning_task);
2132   } else {
2133     ShenandoahIsAliveClosure is_alive;
2134 #ifdef ASSERT
2135     ShenandoahAssertNotForwardedClosure verify_cl;
2136     ShenandoahParallelWeakRootsCleaningTask<ShenandoahIsAliveClosure, ShenandoahAssertNotForwardedClosure>
2137       cleaning_task(timing_phase, &is_alive, &verify_cl, num_workers);
2138 #else
2139     ShenandoahParallelWeakRootsCleaningTask<ShenandoahIsAliveClosure, DoNothingClosure>
2140       cleaning_task(timing_phase, &is_alive, &do_nothing_cl, num_workers);
2141 #endif
2142     _workers->run_task(&cleaning_task);
2143   }
2144 }
2145 
2146 void ShenandoahHeap::parallel_cleaning(bool full_gc) {
2147   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
2148   assert(is_stw_gc_in_progress(), "Only for Degenerated and Full GC");
2149   ShenandoahGCPhase phase(full_gc ?
2150                           ShenandoahPhaseTimings::full_gc_purge :
2151                           ShenandoahPhaseTimings::degen_gc_purge);
2152   stw_weak_refs(full_gc);
2153   stw_process_weak_roots(full_gc);
2154   stw_unload_classes(full_gc);
2155 }
2156 
2157 void ShenandoahHeap::set_has_forwarded_objects(bool cond) {
2158   set_gc_state(HAS_FORWARDED, cond);
2159 }
2160 
2161 void ShenandoahHeap::set_unload_classes(bool uc) {
2162   _unload_classes.set_cond(uc);
2163 }
2164 
2165 bool ShenandoahHeap::unload_classes() const {
2166   return _unload_classes.is_set();
2167 }
2168 
2169 address ShenandoahHeap::in_cset_fast_test_addr() {
2170   ShenandoahHeap* heap = ShenandoahHeap::heap();
2171   assert(heap->collection_set() != nullptr, "Sanity");
2172   return (address) heap->collection_set()->biased_map_address();
2173 }
2174 
2175 void ShenandoahHeap::reset_bytes_allocated_since_gc_start() {
2176   if (mode()->is_generational()) {
2177     young_generation()->reset_bytes_allocated_since_gc_start();
2178     old_generation()->reset_bytes_allocated_since_gc_start();
2179   }
2180 
2181   global_generation()->reset_bytes_allocated_since_gc_start();
2182 }
2183 
2184 void ShenandoahHeap::set_degenerated_gc_in_progress(bool in_progress) {
2185   _degenerated_gc_in_progress.set_cond(in_progress);
2186 }
2187 
2188 void ShenandoahHeap::set_full_gc_in_progress(bool in_progress) {
2189   _full_gc_in_progress.set_cond(in_progress);
2190 }
2191 
2192 void ShenandoahHeap::set_full_gc_move_in_progress(bool in_progress) {
2193   assert (is_full_gc_in_progress(), "should be");
2194   _full_gc_move_in_progress.set_cond(in_progress);
2195 }
2196 
2197 void ShenandoahHeap::set_update_refs_in_progress(bool in_progress) {
2198   set_gc_state(UPDATEREFS, in_progress);
2199 }
2200 
2201 void ShenandoahHeap::register_nmethod(nmethod* nm) {
2202   ShenandoahCodeRoots::register_nmethod(nm);
2203 }
2204 
2205 void ShenandoahHeap::unregister_nmethod(nmethod* nm) {
2206   ShenandoahCodeRoots::unregister_nmethod(nm);
2207 }
2208 
2209 void ShenandoahHeap::pin_object(JavaThread* thr, oop o) {
2210   heap_region_containing(o)->record_pin();
2211 }
2212 
2213 void ShenandoahHeap::unpin_object(JavaThread* thr, oop o) {
2214   ShenandoahHeapRegion* r = heap_region_containing(o);
2215   assert(r != nullptr, "Sanity");
2216   assert(r->pin_count() > 0, "Region " SIZE_FORMAT " should have non-zero pins", r->index());
2217   r->record_unpin();
2218 }
2219 
2220 void ShenandoahHeap::sync_pinned_region_status() {
2221   ShenandoahHeapLocker locker(lock());
2222 
2223   for (size_t i = 0; i < num_regions(); i++) {
2224     ShenandoahHeapRegion *r = get_region(i);
2225     if (r->is_active()) {
2226       if (r->is_pinned()) {
2227         if (r->pin_count() == 0) {
2228           r->make_unpinned();
2229         }
2230       } else {
2231         if (r->pin_count() > 0) {
2232           r->make_pinned();
2233         }
2234       }
2235     }
2236   }
2237 
2238   assert_pinned_region_status();
2239 }
2240 
2241 #ifdef ASSERT
2242 void ShenandoahHeap::assert_pinned_region_status() {
2243   for (size_t i = 0; i < num_regions(); i++) {
2244     ShenandoahHeapRegion* r = get_region(i);
2245     shenandoah_assert_generations_reconciled();
2246     if (gc_generation()->contains(r)) {
2247       assert((r->is_pinned() && r->pin_count() > 0) || (!r->is_pinned() && r->pin_count() == 0),
2248              "Region " SIZE_FORMAT " pinning status is inconsistent", i);
2249     }
2250   }
2251 }
2252 #endif
2253 
2254 ConcurrentGCTimer* ShenandoahHeap::gc_timer() const {
2255   return _gc_timer;
2256 }
2257 
2258 void ShenandoahHeap::prepare_concurrent_roots() {
2259   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
2260   assert(!is_stw_gc_in_progress(), "Only concurrent GC");
2261   set_concurrent_strong_root_in_progress(!collection_set()->is_empty());
2262   set_concurrent_weak_root_in_progress(true);
2263   if (unload_classes()) {
2264     _unloader.prepare();
2265   }
2266 }
2267 
2268 void ShenandoahHeap::finish_concurrent_roots() {
2269   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
2270   assert(!is_stw_gc_in_progress(), "Only concurrent GC");
2271   if (unload_classes()) {
2272     _unloader.finish();
2273   }
2274 }
2275 
2276 #ifdef ASSERT
2277 void ShenandoahHeap::assert_gc_workers(uint nworkers) {
2278   assert(nworkers > 0 && nworkers <= max_workers(), "Sanity");
2279 
2280   if (ShenandoahSafepoint::is_at_shenandoah_safepoint()) {
2281     // Use ParallelGCThreads inside safepoints
2282     assert(nworkers == ParallelGCThreads, "Use ParallelGCThreads (%u) within safepoint, not %u",
2283            ParallelGCThreads, nworkers);
2284   } else {
2285     // Use ConcGCThreads outside safepoints
2286     assert(nworkers == ConcGCThreads, "Use ConcGCThreads (%u) outside safepoints, %u",
2287            ConcGCThreads, nworkers);
2288   }
2289 }
2290 #endif
2291 
2292 ShenandoahVerifier* ShenandoahHeap::verifier() {
2293   guarantee(ShenandoahVerify, "Should be enabled");
2294   assert (_verifier != nullptr, "sanity");
2295   return _verifier;
2296 }
2297 
2298 template<bool CONCURRENT>
2299 class ShenandoahUpdateHeapRefsTask : public WorkerTask {
2300 private:
2301   ShenandoahHeap* _heap;
2302   ShenandoahRegionIterator* _regions;
2303 public:
2304   explicit ShenandoahUpdateHeapRefsTask(ShenandoahRegionIterator* regions) :
2305     WorkerTask("Shenandoah Update References"),
2306     _heap(ShenandoahHeap::heap()),
2307     _regions(regions) {
2308   }
2309 
2310   void work(uint worker_id) {
2311     if (CONCURRENT) {
2312       ShenandoahConcurrentWorkerSession worker_session(worker_id);
2313       ShenandoahSuspendibleThreadSetJoiner stsj;
2314       do_work<ShenandoahConcUpdateRefsClosure>(worker_id);
2315     } else {
2316       ShenandoahParallelWorkerSession worker_session(worker_id);
2317       do_work<ShenandoahNonConcUpdateRefsClosure>(worker_id);
2318     }
2319   }
2320 
2321 private:
2322   template<class T>
2323   void do_work(uint worker_id) {
2324     if (CONCURRENT && (worker_id == 0)) {
2325       // We ask the first worker to replenish the Mutator free set by moving regions previously reserved to hold the
2326       // results of evacuation.  These reserves are no longer necessary because evacuation has completed.
2327       size_t cset_regions = _heap->collection_set()->count();
2328 
2329       // Now that evacuation is done, we can reassign any regions that had been reserved to hold the results of evacuation
2330       // to the mutator free set.  At the end of GC, we will have cset_regions newly evacuated fully empty regions from
2331       // which we will be able to replenish the Collector free set and the OldCollector free set in preparation for the
2332       // next GC cycle.
2333       _heap->free_set()->move_regions_from_collector_to_mutator(cset_regions);
2334     }
2335     // If !CONCURRENT, there's no value in expanding Mutator free set
2336     T cl;
2337     ShenandoahHeapRegion* r = _regions->next();
2338     while (r != nullptr) {
2339       HeapWord* update_watermark = r->get_update_watermark();
2340       assert (update_watermark >= r->bottom(), "sanity");
2341       if (r->is_active() && !r->is_cset()) {
2342         _heap->marked_object_oop_iterate(r, &cl, update_watermark);
2343         if (ShenandoahPacing) {
2344           _heap->pacer()->report_updaterefs(pointer_delta(update_watermark, r->bottom()));
2345         }
2346       }
2347       if (_heap->check_cancelled_gc_and_yield(CONCURRENT)) {
2348         return;
2349       }
2350       r = _regions->next();
2351     }
2352   }
2353 };
2354 
2355 void ShenandoahHeap::update_heap_references(bool concurrent) {
2356   assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
2357 
2358   if (concurrent) {
2359     ShenandoahUpdateHeapRefsTask<true> task(&_update_refs_iterator);
2360     workers()->run_task(&task);
2361   } else {
2362     ShenandoahUpdateHeapRefsTask<false> task(&_update_refs_iterator);
2363     workers()->run_task(&task);
2364   }
2365 }
2366 
2367 void ShenandoahHeap::update_heap_region_states(bool concurrent) {
2368   assert(SafepointSynchronize::is_at_safepoint(), "Must be at a safepoint");
2369   assert(!is_full_gc_in_progress(), "Only for concurrent and degenerated GC");
2370 
2371   {
2372     ShenandoahGCPhase phase(concurrent ?
2373                             ShenandoahPhaseTimings::final_update_refs_update_region_states :
2374                             ShenandoahPhaseTimings::degen_gc_final_update_refs_update_region_states);
2375 
2376     final_update_refs_update_region_states();
2377 
2378     assert_pinned_region_status();
2379   }
2380 
2381   {
2382     ShenandoahGCPhase phase(concurrent ?
2383                             ShenandoahPhaseTimings::final_update_refs_trash_cset :
2384                             ShenandoahPhaseTimings::degen_gc_final_update_refs_trash_cset);
2385     trash_cset_regions();
2386   }
2387 }
2388 
2389 void ShenandoahHeap::final_update_refs_update_region_states() {
2390   ShenandoahSynchronizePinnedRegionStates cl;
2391   parallel_heap_region_iterate(&cl);
2392 }
2393 
2394 void ShenandoahHeap::rebuild_free_set(bool concurrent) {
2395   ShenandoahGCPhase phase(concurrent ?
2396                           ShenandoahPhaseTimings::final_update_refs_rebuild_freeset :
2397                           ShenandoahPhaseTimings::degen_gc_final_update_refs_rebuild_freeset);
2398   ShenandoahHeapLocker locker(lock());
2399   size_t young_cset_regions, old_cset_regions;
2400   size_t first_old_region, last_old_region, old_region_count;
2401   _free_set->prepare_to_rebuild(young_cset_regions, old_cset_regions, first_old_region, last_old_region, old_region_count);
2402   // If there are no old regions, first_old_region will be greater than last_old_region
2403   assert((first_old_region > last_old_region) ||
2404          ((last_old_region + 1 - first_old_region >= old_region_count) &&
2405           get_region(first_old_region)->is_old() && get_region(last_old_region)->is_old()),
2406          "sanity: old_region_count: " SIZE_FORMAT ", first_old_region: " SIZE_FORMAT ", last_old_region: " SIZE_FORMAT,
2407          old_region_count, first_old_region, last_old_region);
2408 
2409   if (mode()->is_generational()) {
2410 #ifdef ASSERT
2411     if (ShenandoahVerify) {
2412       verifier()->verify_before_rebuilding_free_set();
2413     }
2414 #endif
2415 
2416     // The computation of bytes_of_allocation_runway_before_gc_trigger is quite conservative so consider all of this
2417     // available for transfer to old. Note that transfer of humongous regions does not impact available.
2418     ShenandoahGenerationalHeap* gen_heap = ShenandoahGenerationalHeap::heap();
2419     size_t allocation_runway = gen_heap->young_generation()->heuristics()->bytes_of_allocation_runway_before_gc_trigger(young_cset_regions);
2420     gen_heap->compute_old_generation_balance(allocation_runway, old_cset_regions);
2421 
2422     // Total old_available may have been expanded to hold anticipated promotions.  We trigger if the fragmented available
2423     // memory represents more than 16 regions worth of data.  Note that fragmentation may increase when we promote regular
2424     // regions in place when many of these regular regions have an abundant amount of available memory within them.  Fragmentation
2425     // will decrease as promote-by-copy consumes the available memory within these partially consumed regions.
2426     //
2427     // We consider old-gen to have excessive fragmentation if more than 12.5% of old-gen is free memory that resides
2428     // within partially consumed regions of memory.
2429   }
2430   // Rebuild free set based on adjusted generation sizes.
2431   _free_set->finish_rebuild(young_cset_regions, old_cset_regions, old_region_count);
2432 
2433   if (mode()->is_generational()) {
2434     ShenandoahGenerationalHeap* gen_heap = ShenandoahGenerationalHeap::heap();
2435     ShenandoahOldGeneration* old_gen = gen_heap->old_generation();
2436     old_gen->heuristics()->evaluate_triggers(first_old_region, last_old_region, old_region_count, num_regions());
2437   }
2438 }
2439 
2440 void ShenandoahHeap::print_extended_on(outputStream *st) const {
2441   print_on(st);
2442   st->cr();
2443   print_heap_regions_on(st);
2444 }
2445 
2446 bool ShenandoahHeap::is_bitmap_slice_committed(ShenandoahHeapRegion* r, bool skip_self) {
2447   size_t slice = r->index() / _bitmap_regions_per_slice;
2448 
2449   size_t regions_from = _bitmap_regions_per_slice * slice;
2450   size_t regions_to   = MIN2(num_regions(), _bitmap_regions_per_slice * (slice + 1));
2451   for (size_t g = regions_from; g < regions_to; g++) {
2452     assert (g / _bitmap_regions_per_slice == slice, "same slice");
2453     if (skip_self && g == r->index()) continue;
2454     if (get_region(g)->is_committed()) {
2455       return true;
2456     }
2457   }
2458   return false;
2459 }
2460 
2461 bool ShenandoahHeap::commit_bitmap_slice(ShenandoahHeapRegion* r) {
2462   shenandoah_assert_heaplocked();
2463 
2464   // Bitmaps in special regions do not need commits
2465   if (_bitmap_region_special) {
2466     return true;
2467   }
2468 
2469   if (is_bitmap_slice_committed(r, true)) {
2470     // Some other region from the group is already committed, meaning the bitmap
2471     // slice is already committed, we exit right away.
2472     return true;
2473   }
2474 
2475   // Commit the bitmap slice:
2476   size_t slice = r->index() / _bitmap_regions_per_slice;
2477   size_t off = _bitmap_bytes_per_slice * slice;
2478   size_t len = _bitmap_bytes_per_slice;
2479   char* start = (char*) _bitmap_region.start() + off;
2480 
2481   if (!os::commit_memory(start, len, false)) {
2482     return false;
2483   }
2484 
2485   if (AlwaysPreTouch) {
2486     os::pretouch_memory(start, start + len, _pretouch_bitmap_page_size);
2487   }
2488 
2489   return true;
2490 }
2491 
2492 bool ShenandoahHeap::uncommit_bitmap_slice(ShenandoahHeapRegion *r) {
2493   shenandoah_assert_heaplocked();
2494 
2495   // Bitmaps in special regions do not need uncommits
2496   if (_bitmap_region_special) {
2497     return true;
2498   }
2499 
2500   if (is_bitmap_slice_committed(r, true)) {
2501     // Some other region from the group is still committed, meaning the bitmap
2502     // slice should stay committed, exit right away.
2503     return true;
2504   }
2505 
2506   // Uncommit the bitmap slice:
2507   size_t slice = r->index() / _bitmap_regions_per_slice;
2508   size_t off = _bitmap_bytes_per_slice * slice;
2509   size_t len = _bitmap_bytes_per_slice;
2510   if (!os::uncommit_memory((char*)_bitmap_region.start() + off, len)) {
2511     return false;
2512   }
2513   return true;
2514 }
2515 
2516 void ShenandoahHeap::forbid_uncommit() {
2517   if (_uncommit_thread != nullptr) {
2518     _uncommit_thread->forbid_uncommit();
2519   }
2520 }
2521 
2522 void ShenandoahHeap::allow_uncommit() {
2523   if (_uncommit_thread != nullptr) {
2524     _uncommit_thread->allow_uncommit();
2525   }
2526 }
2527 
2528 #ifdef ASSERT
2529 bool ShenandoahHeap::is_uncommit_in_progress() {
2530   if (_uncommit_thread != nullptr) {
2531     return _uncommit_thread->is_uncommit_in_progress();
2532   }
2533   return false;
2534 }
2535 #endif
2536 
2537 void ShenandoahHeap::safepoint_synchronize_begin() {
2538   StackWatermarkSet::safepoint_synchronize_begin();
2539   SuspendibleThreadSet::synchronize();
2540 }
2541 
2542 void ShenandoahHeap::safepoint_synchronize_end() {
2543   SuspendibleThreadSet::desynchronize();
2544 }
2545 
2546 void ShenandoahHeap::try_inject_alloc_failure() {
2547   if (ShenandoahAllocFailureALot && !cancelled_gc() && ((os::random() % 1000) > 950)) {
2548     _inject_alloc_failure.set();
2549     os::naked_short_sleep(1);
2550     if (cancelled_gc()) {
2551       log_info(gc)("Allocation failure was successfully injected");
2552     }
2553   }
2554 }
2555 
2556 bool ShenandoahHeap::should_inject_alloc_failure() {
2557   return _inject_alloc_failure.is_set() && _inject_alloc_failure.try_unset();
2558 }
2559 
2560 void ShenandoahHeap::initialize_serviceability() {
2561   _memory_pool = new ShenandoahMemoryPool(this);
2562   _cycle_memory_manager.add_pool(_memory_pool);
2563   _stw_memory_manager.add_pool(_memory_pool);
2564 }
2565 
2566 GrowableArray<GCMemoryManager*> ShenandoahHeap::memory_managers() {
2567   GrowableArray<GCMemoryManager*> memory_managers(2);
2568   memory_managers.append(&_cycle_memory_manager);
2569   memory_managers.append(&_stw_memory_manager);
2570   return memory_managers;
2571 }
2572 
2573 GrowableArray<MemoryPool*> ShenandoahHeap::memory_pools() {
2574   GrowableArray<MemoryPool*> memory_pools(1);
2575   memory_pools.append(_memory_pool);
2576   return memory_pools;
2577 }
2578 
2579 MemoryUsage ShenandoahHeap::memory_usage() {
2580   return MemoryUsage(_initial_size, used(), committed(), max_capacity());
2581 }
2582 
2583 ShenandoahRegionIterator::ShenandoahRegionIterator() :
2584   _heap(ShenandoahHeap::heap()),
2585   _index(0) {}
2586 
2587 ShenandoahRegionIterator::ShenandoahRegionIterator(ShenandoahHeap* heap) :
2588   _heap(heap),
2589   _index(0) {}
2590 
2591 void ShenandoahRegionIterator::reset() {
2592   _index = 0;
2593 }
2594 
2595 bool ShenandoahRegionIterator::has_next() const {
2596   return _index < _heap->num_regions();
2597 }
2598 
2599 char ShenandoahHeap::gc_state() const {
2600   return _gc_state.raw_value();
2601 }
2602 
2603 ShenandoahLiveData* ShenandoahHeap::get_liveness_cache(uint worker_id) {
2604 #ifdef ASSERT
2605   assert(_liveness_cache != nullptr, "sanity");
2606   assert(worker_id < _max_workers, "sanity");
2607   for (uint i = 0; i < num_regions(); i++) {
2608     assert(_liveness_cache[worker_id][i] == 0, "liveness cache should be empty");
2609   }
2610 #endif
2611   return _liveness_cache[worker_id];
2612 }
2613 
2614 void ShenandoahHeap::flush_liveness_cache(uint worker_id) {
2615   assert(worker_id < _max_workers, "sanity");
2616   assert(_liveness_cache != nullptr, "sanity");
2617   ShenandoahLiveData* ld = _liveness_cache[worker_id];
2618   for (uint i = 0; i < num_regions(); i++) {
2619     ShenandoahLiveData live = ld[i];
2620     if (live > 0) {
2621       ShenandoahHeapRegion* r = get_region(i);
2622       r->increase_live_data_gc_words(live);
2623       ld[i] = 0;
2624     }
2625   }
2626 }
2627 
2628 bool ShenandoahHeap::requires_barriers(stackChunkOop obj) const {
2629   if (is_idle()) return false;
2630 
2631   // Objects allocated after marking start are implicitly alive, don't need any barriers during
2632   // marking phase.
2633   if (is_concurrent_mark_in_progress() &&
2634      !marking_context()->allocated_after_mark_start(obj)) {
2635     return true;
2636   }
2637 
2638   // Can not guarantee obj is deeply good.
2639   if (has_forwarded_objects()) {
2640     return true;
2641   }
2642 
2643   return false;
2644 }
2645 
2646 HeapWord* ShenandoahHeap::allocate_loaded_archive_space(size_t size) {
2647 #if INCLUDE_CDS_JAVA_HEAP
2648   // CDS wants a continuous memory range to load a bunch of objects.
2649   // This effectively bypasses normal allocation paths, and requires
2650   // a bit of massaging to unbreak GC invariants.
2651 
2652   ShenandoahAllocRequest req = ShenandoahAllocRequest::for_shared(size);
2653 
2654   // Easy case: a single regular region, no further adjustments needed.
2655   if (!ShenandoahHeapRegion::requires_humongous(size)) {
2656     return allocate_memory(req);
2657   }
2658 
2659   // Hard case: the requested size would cause a humongous allocation.
2660   // We need to make sure it looks like regular allocation to the rest of GC.
2661 
2662   // CDS code would guarantee no objects straddle multiple regions, as long as
2663   // regions are as large as MIN_GC_REGION_ALIGNMENT. It is impractical at this
2664   // point to deal with case when Shenandoah runs with smaller regions.
2665   // TODO: This check can be dropped once MIN_GC_REGION_ALIGNMENT agrees more with Shenandoah.
2666   if (ShenandoahHeapRegion::region_size_bytes() < ArchiveHeapWriter::MIN_GC_REGION_ALIGNMENT) {
2667     return nullptr;
2668   }
2669 
2670   HeapWord* mem = allocate_memory(req);
2671   size_t start_idx = heap_region_index_containing(mem);
2672   size_t num_regions = ShenandoahHeapRegion::required_regions(size * HeapWordSize);
2673 
2674   // Flip humongous -> regular.
2675   {
2676     ShenandoahHeapLocker locker(lock(), false);
2677     for (size_t c = start_idx; c < start_idx + num_regions; c++) {
2678       get_region(c)->make_regular_bypass();
2679     }
2680   }
2681 
2682   return mem;
2683 #else
2684   assert(false, "Archive heap loader should not be available, should not be here");
2685   return nullptr;
2686 #endif // INCLUDE_CDS_JAVA_HEAP
2687 }
2688 
2689 void ShenandoahHeap::complete_loaded_archive_space(MemRegion archive_space) {
2690   // Nothing to do here, except checking that heap looks fine.
2691 #ifdef ASSERT
2692   HeapWord* start = archive_space.start();
2693   HeapWord* end = archive_space.end();
2694 
2695   // No unclaimed space between the objects.
2696   // Objects are properly allocated in correct regions.
2697   HeapWord* cur = start;
2698   while (cur < end) {
2699     oop oop = cast_to_oop(cur);
2700     shenandoah_assert_in_correct_region(nullptr, oop);
2701     cur += oop->size();
2702   }
2703 
2704   // No unclaimed tail at the end of archive space.
2705   assert(cur == end,
2706          "Archive space should be fully used: " PTR_FORMAT " " PTR_FORMAT,
2707          p2i(cur), p2i(end));
2708 
2709   // Region bounds are good.
2710   ShenandoahHeapRegion* begin_reg = heap_region_containing(start);
2711   ShenandoahHeapRegion* end_reg = heap_region_containing(end);
2712   assert(begin_reg->is_regular(), "Must be");
2713   assert(end_reg->is_regular(), "Must be");
2714   assert(begin_reg->bottom() == start,
2715          "Must agree: archive-space-start: " PTR_FORMAT ", begin-region-bottom: " PTR_FORMAT,
2716          p2i(start), p2i(begin_reg->bottom()));
2717   assert(end_reg->top() == end,
2718          "Must agree: archive-space-end: " PTR_FORMAT ", end-region-top: " PTR_FORMAT,
2719          p2i(end), p2i(end_reg->top()));
2720 #endif
2721 }
2722 
2723 ShenandoahGeneration* ShenandoahHeap::generation_for(ShenandoahAffiliation affiliation) const {
2724   if (!mode()->is_generational()) {
2725     return global_generation();
2726   } else if (affiliation == YOUNG_GENERATION) {
2727     return young_generation();
2728   } else if (affiliation == OLD_GENERATION) {
2729     return old_generation();
2730   }
2731 
2732   ShouldNotReachHere();
2733   return nullptr;
2734 }
2735 
2736 void ShenandoahHeap::log_heap_status(const char* msg) const {
2737   if (mode()->is_generational()) {
2738     young_generation()->log_status(msg);
2739     old_generation()->log_status(msg);
2740   } else {
2741     global_generation()->log_status(msg);
2742   }
2743 }
2744