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