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