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