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