1 /*
2 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2013, 2019, 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 "gc/shared/space.inline.hpp"
28 #include "gc/shared/tlab_globals.hpp"
29 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
32 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
33 #include "jfr/jfrEvents.hpp"
34 #include "memory/allocation.hpp"
35 #include "memory/iterator.inline.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "memory/universe.hpp"
38 #include "oops/oop.inline.hpp"
39 #include "runtime/atomic.hpp"
40 #include "runtime/globals_extension.hpp"
41 #include "runtime/java.hpp"
42 #include "runtime/mutexLocker.hpp"
43 #include "runtime/os.hpp"
44 #include "runtime/safepoint.hpp"
45 #include "utilities/powerOfTwo.hpp"
46
47 size_t ShenandoahHeapRegion::RegionCount = 0;
48 size_t ShenandoahHeapRegion::RegionSizeBytes = 0;
49 size_t ShenandoahHeapRegion::RegionSizeWords = 0;
50 size_t ShenandoahHeapRegion::RegionSizeBytesShift = 0;
51 size_t ShenandoahHeapRegion::RegionSizeWordsShift = 0;
52 size_t ShenandoahHeapRegion::RegionSizeBytesMask = 0;
53 size_t ShenandoahHeapRegion::RegionSizeWordsMask = 0;
54 size_t ShenandoahHeapRegion::HumongousThresholdBytes = 0;
55 size_t ShenandoahHeapRegion::HumongousThresholdWords = 0;
56 size_t ShenandoahHeapRegion::MaxTLABSizeBytes = 0;
57 size_t ShenandoahHeapRegion::MaxTLABSizeWords = 0;
58
59 ShenandoahHeapRegion::ShenandoahHeapRegion(HeapWord* start, size_t index, bool committed) :
60 _index(index),
61 _bottom(start),
62 _end(start + RegionSizeWords),
63 _new_top(nullptr),
64 _empty_time(os::elapsedTime()),
65 _state(committed ? _empty_committed : _empty_uncommitted),
66 _top(start),
67 _tlab_allocs(0),
68 _gclab_allocs(0),
69 _live_data(0),
70 _critical_pins(0),
71 _update_watermark(start) {
72
73 assert(Universe::on_page_boundary(_bottom) && Universe::on_page_boundary(_end),
74 "invalid space boundaries");
75 if (ZapUnusedHeapArea && committed) {
76 SpaceMangler::mangle_region(MemRegion(_bottom, _end));
77 }
78 }
79
80 void ShenandoahHeapRegion::report_illegal_transition(const char *method) {
81 stringStream ss;
82 ss.print("Illegal region state transition from \"%s\", at %s\n ", region_state_to_string(_state), method);
83 print_on(&ss);
84 fatal("%s", ss.freeze());
85 }
86
87 void ShenandoahHeapRegion::make_regular_allocation() {
88 shenandoah_assert_heaplocked();
89
90 switch (_state) {
91 case _empty_uncommitted:
92 do_commit();
93 case _empty_committed:
94 set_state(_regular);
95 case _regular:
96 case _pinned:
97 return;
98 default:
99 report_illegal_transition("regular allocation");
100 }
101 }
102
103 void ShenandoahHeapRegion::make_regular_bypass() {
104 shenandoah_assert_heaplocked();
105 assert (ShenandoahHeap::heap()->is_full_gc_in_progress() || ShenandoahHeap::heap()->is_degenerated_gc_in_progress(),
106 "only for full or degen GC");
107
108 switch (_state) {
109 case _empty_uncommitted:
110 do_commit();
111 case _empty_committed:
112 case _cset:
113 case _humongous_start:
114 case _humongous_cont:
115 set_state(_regular);
116 return;
117 case _pinned_cset:
118 set_state(_pinned);
119 return;
120 case _regular:
121 case _pinned:
122 return;
123 default:
124 report_illegal_transition("regular bypass");
125 }
126 }
127
128 void ShenandoahHeapRegion::make_humongous_start() {
129 shenandoah_assert_heaplocked();
130 switch (_state) {
131 case _empty_uncommitted:
132 do_commit();
133 case _empty_committed:
134 set_state(_humongous_start);
135 return;
136 default:
137 report_illegal_transition("humongous start allocation");
138 }
139 }
140
141 void ShenandoahHeapRegion::make_humongous_start_bypass() {
142 shenandoah_assert_heaplocked();
143 assert (ShenandoahHeap::heap()->is_full_gc_in_progress(), "only for full GC");
144
145 switch (_state) {
146 case _empty_committed:
147 case _regular:
148 case _humongous_start:
149 case _humongous_cont:
150 set_state(_humongous_start);
151 return;
152 default:
153 report_illegal_transition("humongous start bypass");
154 }
155 }
156
157 void ShenandoahHeapRegion::make_humongous_cont() {
158 shenandoah_assert_heaplocked();
159 switch (_state) {
160 case _empty_uncommitted:
161 do_commit();
162 case _empty_committed:
163 set_state(_humongous_cont);
164 return;
165 default:
166 report_illegal_transition("humongous continuation allocation");
167 }
168 }
169
170 void ShenandoahHeapRegion::make_humongous_cont_bypass() {
171 shenandoah_assert_heaplocked();
172 assert (ShenandoahHeap::heap()->is_full_gc_in_progress(), "only for full GC");
173
174 switch (_state) {
175 case _empty_committed:
176 case _regular:
177 case _humongous_start:
178 case _humongous_cont:
179 set_state(_humongous_cont);
180 return;
181 default:
182 report_illegal_transition("humongous continuation bypass");
183 }
184 }
185
186 void ShenandoahHeapRegion::make_pinned() {
187 shenandoah_assert_heaplocked();
188 assert(pin_count() > 0, "Should have pins: " SIZE_FORMAT, pin_count());
189
190 switch (_state) {
191 case _regular:
192 set_state(_pinned);
193 case _pinned_cset:
194 case _pinned:
195 return;
196 case _humongous_start:
197 set_state(_pinned_humongous_start);
198 case _pinned_humongous_start:
199 return;
200 case _cset:
201 _state = _pinned_cset;
202 return;
203 default:
204 report_illegal_transition("pinning");
205 }
206 }
207
208 void ShenandoahHeapRegion::make_unpinned() {
209 shenandoah_assert_heaplocked();
210 assert(pin_count() == 0, "Should not have pins: " SIZE_FORMAT, pin_count());
211
212 switch (_state) {
213 case _pinned:
214 set_state(_regular);
215 return;
216 case _regular:
217 case _humongous_start:
218 return;
219 case _pinned_cset:
220 set_state(_cset);
221 return;
222 case _pinned_humongous_start:
223 set_state(_humongous_start);
224 return;
225 default:
226 report_illegal_transition("unpinning");
227 }
228 }
229
230 void ShenandoahHeapRegion::make_cset() {
231 shenandoah_assert_heaplocked();
232 switch (_state) {
233 case _regular:
234 set_state(_cset);
235 case _cset:
236 return;
237 default:
238 report_illegal_transition("cset");
239 }
240 }
241
242 void ShenandoahHeapRegion::make_trash() {
243 shenandoah_assert_heaplocked();
244 switch (_state) {
245 case _cset:
246 // Reclaiming cset regions
247 case _humongous_start:
248 case _humongous_cont:
249 // Reclaiming humongous regions
250 case _regular:
251 // Immediate region reclaim
252 set_state(_trash);
253 return;
254 default:
255 report_illegal_transition("trashing");
256 }
257 }
258
259 void ShenandoahHeapRegion::make_trash_immediate() {
260 make_trash();
261
262 // On this path, we know there are no marked objects in the region,
263 // tell marking context about it to bypass bitmap resets.
264 ShenandoahHeap::heap()->complete_marking_context()->reset_top_bitmap(this);
265 }
266
267 void ShenandoahHeapRegion::make_empty() {
268 shenandoah_assert_heaplocked();
269 switch (_state) {
270 case _trash:
271 set_state(_empty_committed);
272 _empty_time = os::elapsedTime();
273 return;
274 default:
275 report_illegal_transition("emptying");
276 }
277 }
278
279 void ShenandoahHeapRegion::make_uncommitted() {
280 shenandoah_assert_heaplocked();
281 switch (_state) {
282 case _empty_committed:
283 do_uncommit();
284 set_state(_empty_uncommitted);
285 return;
286 default:
287 report_illegal_transition("uncommiting");
288 }
289 }
290
291 void ShenandoahHeapRegion::make_committed_bypass() {
292 shenandoah_assert_heaplocked();
293 assert (ShenandoahHeap::heap()->is_full_gc_in_progress(), "only for full GC");
294
295 switch (_state) {
296 case _empty_uncommitted:
297 do_commit();
298 set_state(_empty_committed);
299 return;
300 default:
301 report_illegal_transition("commit bypass");
302 }
303 }
304
305 void ShenandoahHeapRegion::reset_alloc_metadata() {
306 _tlab_allocs = 0;
307 _gclab_allocs = 0;
308 }
309
310 size_t ShenandoahHeapRegion::get_shared_allocs() const {
311 return used() - (_tlab_allocs + _gclab_allocs) * HeapWordSize;
312 }
313
314 size_t ShenandoahHeapRegion::get_tlab_allocs() const {
315 return _tlab_allocs * HeapWordSize;
316 }
317
318 size_t ShenandoahHeapRegion::get_gclab_allocs() const {
319 return _gclab_allocs * HeapWordSize;
320 }
321
322 void ShenandoahHeapRegion::set_live_data(size_t s) {
323 assert(Thread::current()->is_VM_thread(), "by VM thread");
324 _live_data = (s >> LogHeapWordSize);
325 }
326
327 void ShenandoahHeapRegion::print_on(outputStream* st) const {
328 st->print("|");
329 st->print(SIZE_FORMAT_W(5), this->_index);
330
331 switch (_state) {
332 case _empty_uncommitted:
333 st->print("|EU ");
334 break;
335 case _empty_committed:
336 st->print("|EC ");
337 break;
338 case _regular:
339 st->print("|R ");
340 break;
341 case _humongous_start:
342 st->print("|H ");
343 break;
344 case _pinned_humongous_start:
345 st->print("|HP ");
346 break;
347 case _humongous_cont:
348 st->print("|HC ");
349 break;
350 case _cset:
351 st->print("|CS ");
352 break;
353 case _trash:
354 st->print("|TR ");
355 break;
356 case _pinned:
357 st->print("|P ");
358 break;
359 case _pinned_cset:
360 st->print("|CSP");
361 break;
362 default:
363 ShouldNotReachHere();
364 }
365
366 #define SHR_PTR_FORMAT "%12" PRIxPTR
367
368 st->print("|BTE " SHR_PTR_FORMAT ", " SHR_PTR_FORMAT ", " SHR_PTR_FORMAT,
369 p2i(bottom()), p2i(top()), p2i(end()));
370 st->print("|TAMS " SHR_PTR_FORMAT,
371 p2i(ShenandoahHeap::heap()->marking_context()->top_at_mark_start(const_cast<ShenandoahHeapRegion*>(this))));
372 st->print("|UWM " SHR_PTR_FORMAT,
373 p2i(_update_watermark));
374 st->print("|U " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(used()), proper_unit_for_byte_size(used()));
375 st->print("|T " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_tlab_allocs()), proper_unit_for_byte_size(get_tlab_allocs()));
376 st->print("|G " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_gclab_allocs()), proper_unit_for_byte_size(get_gclab_allocs()));
377 st->print("|S " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_shared_allocs()), proper_unit_for_byte_size(get_shared_allocs()));
378 st->print("|L " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_live_data_bytes()), proper_unit_for_byte_size(get_live_data_bytes()));
379 st->print("|CP " SIZE_FORMAT_W(3), pin_count());
380 st->cr();
381
382 #undef SHR_PTR_FORMAT
383 }
384
385 void ShenandoahHeapRegion::oop_iterate(OopIterateClosure* blk) {
386 if (!is_active()) return;
387 if (is_humongous()) {
388 oop_iterate_humongous(blk);
389 } else {
390 oop_iterate_objects(blk);
391 }
392 }
393
394 void ShenandoahHeapRegion::oop_iterate_objects(OopIterateClosure* blk) {
395 assert(! is_humongous(), "no humongous region here");
396 HeapWord* obj_addr = bottom();
397 HeapWord* t = top();
398 // Could call objects iterate, but this is easier.
399 while (obj_addr < t) {
400 oop obj = cast_to_oop(obj_addr);
401 obj_addr += obj->oop_iterate_size(blk);
402 }
403 }
404
405 void ShenandoahHeapRegion::oop_iterate_humongous(OopIterateClosure* blk) {
406 assert(is_humongous(), "only humongous region here");
407 // Find head.
408 ShenandoahHeapRegion* r = humongous_start_region();
409 assert(r->is_humongous_start(), "need humongous head here");
410 oop obj = cast_to_oop(r->bottom());
411 obj->oop_iterate(blk, MemRegion(bottom(), top()));
412 }
413
414 ShenandoahHeapRegion* ShenandoahHeapRegion::humongous_start_region() const {
415 ShenandoahHeap* heap = ShenandoahHeap::heap();
416 assert(is_humongous(), "Must be a part of the humongous region");
417 size_t i = index();
418 ShenandoahHeapRegion* r = const_cast<ShenandoahHeapRegion*>(this);
419 while (!r->is_humongous_start()) {
420 assert(i > 0, "Sanity");
421 i--;
422 r = heap->get_region(i);
423 assert(r->is_humongous(), "Must be a part of the humongous region");
424 }
425 assert(r->is_humongous_start(), "Must be");
426 return r;
427 }
428
429 void ShenandoahHeapRegion::recycle() {
430 set_top(bottom());
431 clear_live_data();
432
433 reset_alloc_metadata();
434
435 ShenandoahHeap::heap()->marking_context()->reset_top_at_mark_start(this);
436 set_update_watermark(bottom());
437
438 make_empty();
439
440 if (ZapUnusedHeapArea) {
441 SpaceMangler::mangle_region(MemRegion(bottom(), end()));
442 }
443 }
444
445 HeapWord* ShenandoahHeapRegion::block_start(const void* p) const {
446 assert(MemRegion(bottom(), end()).contains(p),
447 "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
448 p2i(p), p2i(bottom()), p2i(end()));
449 if (p >= top()) {
450 return top();
451 } else {
452 HeapWord* last = bottom();
453 HeapWord* cur = last;
454 while (cur <= p) {
455 last = cur;
456 cur += cast_to_oop(cur)->size();
457 }
458 shenandoah_assert_correct(nullptr, cast_to_oop(last));
459 return last;
460 }
461 }
463 size_t ShenandoahHeapRegion::block_size(const HeapWord* p) const {
464 assert(MemRegion(bottom(), end()).contains(p),
465 "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
466 p2i(p), p2i(bottom()), p2i(end()));
467 if (p < top()) {
468 return cast_to_oop(p)->size();
469 } else {
470 assert(p == top(), "just checking");
471 return pointer_delta(end(), (HeapWord*) p);
472 }
473 }
474
475 size_t ShenandoahHeapRegion::setup_sizes(size_t max_heap_size) {
476 // Absolute minimums we should not ever break.
477 static const size_t MIN_REGION_SIZE = 256*K;
478
479 if (FLAG_IS_DEFAULT(ShenandoahMinRegionSize)) {
480 FLAG_SET_DEFAULT(ShenandoahMinRegionSize, MIN_REGION_SIZE);
481 }
482
483 size_t region_size;
484 if (FLAG_IS_DEFAULT(ShenandoahRegionSize)) {
485 if (ShenandoahMinRegionSize > max_heap_size / MIN_NUM_REGIONS) {
486 err_msg message("Max heap size (" SIZE_FORMAT "%s) is too low to afford the minimum number "
487 "of regions (" SIZE_FORMAT ") of minimum region size (" SIZE_FORMAT "%s).",
488 byte_size_in_proper_unit(max_heap_size), proper_unit_for_byte_size(max_heap_size),
489 MIN_NUM_REGIONS,
490 byte_size_in_proper_unit(ShenandoahMinRegionSize), proper_unit_for_byte_size(ShenandoahMinRegionSize));
491 vm_exit_during_initialization("Invalid -XX:ShenandoahMinRegionSize option", message);
492 }
493 if (ShenandoahMinRegionSize < MIN_REGION_SIZE) {
494 err_msg message("" SIZE_FORMAT "%s should not be lower than minimum region size (" SIZE_FORMAT "%s).",
495 byte_size_in_proper_unit(ShenandoahMinRegionSize), proper_unit_for_byte_size(ShenandoahMinRegionSize),
496 byte_size_in_proper_unit(MIN_REGION_SIZE), proper_unit_for_byte_size(MIN_REGION_SIZE));
497 vm_exit_during_initialization("Invalid -XX:ShenandoahMinRegionSize option", message);
498 }
499 if (ShenandoahMinRegionSize < MinTLABSize) {
500 err_msg message("" SIZE_FORMAT "%s should not be lower than TLAB size size (" SIZE_FORMAT "%s).",
501 byte_size_in_proper_unit(ShenandoahMinRegionSize), proper_unit_for_byte_size(ShenandoahMinRegionSize),
502 byte_size_in_proper_unit(MinTLABSize), proper_unit_for_byte_size(MinTLABSize));
581 RegionSizeBytesShift = (size_t)region_size_log;
582
583 guarantee(RegionSizeWordsShift == 0, "we should only set it once");
584 RegionSizeWordsShift = RegionSizeBytesShift - LogHeapWordSize;
585
586 guarantee(RegionSizeBytes == 0, "we should only set it once");
587 RegionSizeBytes = region_size;
588 RegionSizeWords = RegionSizeBytes >> LogHeapWordSize;
589 assert (RegionSizeWords*HeapWordSize == RegionSizeBytes, "sanity");
590
591 guarantee(RegionSizeWordsMask == 0, "we should only set it once");
592 RegionSizeWordsMask = RegionSizeWords - 1;
593
594 guarantee(RegionSizeBytesMask == 0, "we should only set it once");
595 RegionSizeBytesMask = RegionSizeBytes - 1;
596
597 guarantee(RegionCount == 0, "we should only set it once");
598 RegionCount = align_up(max_heap_size, RegionSizeBytes) / RegionSizeBytes;
599 guarantee(RegionCount >= MIN_NUM_REGIONS, "Should have at least minimum regions");
600
601 guarantee(HumongousThresholdWords == 0, "we should only set it once");
602 HumongousThresholdWords = RegionSizeWords * ShenandoahHumongousThreshold / 100;
603 HumongousThresholdWords = align_down(HumongousThresholdWords, MinObjAlignment);
604 assert (HumongousThresholdWords <= RegionSizeWords, "sanity");
605
606 guarantee(HumongousThresholdBytes == 0, "we should only set it once");
607 HumongousThresholdBytes = HumongousThresholdWords * HeapWordSize;
608 assert (HumongousThresholdBytes <= RegionSizeBytes, "sanity");
609
610 guarantee(MaxTLABSizeWords == 0, "we should only set it once");
611 MaxTLABSizeWords = MIN2(RegionSizeWords, HumongousThresholdWords);
612 MaxTLABSizeWords = align_down(MaxTLABSizeWords, MinObjAlignment);
613
614 guarantee(MaxTLABSizeBytes == 0, "we should only set it once");
615 MaxTLABSizeBytes = MaxTLABSizeWords * HeapWordSize;
616 assert (MaxTLABSizeBytes > MinTLABSize, "should be larger");
617
618 return max_heap_size;
619 }
620
621 void ShenandoahHeapRegion::do_commit() {
622 ShenandoahHeap* heap = ShenandoahHeap::heap();
623 if (!heap->is_heap_region_special() && !os::commit_memory((char *) bottom(), RegionSizeBytes, false)) {
624 report_java_out_of_memory("Unable to commit region");
625 }
626 if (!heap->commit_bitmap_slice(this)) {
627 report_java_out_of_memory("Unable to commit bitmaps for region");
628 }
629 if (AlwaysPreTouch) {
630 os::pretouch_memory(bottom(), end(), heap->pretouch_heap_page_size());
631 }
632 heap->increase_committed(ShenandoahHeapRegion::region_size_bytes());
633 }
634
635 void ShenandoahHeapRegion::do_uncommit() {
636 ShenandoahHeap* heap = ShenandoahHeap::heap();
637 if (!heap->is_heap_region_special() && !os::uncommit_memory((char *) bottom(), RegionSizeBytes)) {
638 report_java_out_of_memory("Unable to uncommit region");
639 }
640 if (!heap->uncommit_bitmap_slice(this)) {
641 report_java_out_of_memory("Unable to uncommit bitmaps for region");
642 }
643 heap->decrease_committed(ShenandoahHeapRegion::region_size_bytes());
644 }
645
646 void ShenandoahHeapRegion::set_state(RegionState to) {
647 EventShenandoahHeapRegionStateChange evt;
648 if (evt.should_commit()){
649 evt.set_index((unsigned) index());
650 evt.set_start((uintptr_t)bottom());
651 evt.set_used(used());
652 evt.set_from(_state);
653 evt.set_to(to);
654 evt.commit();
655 }
656 _state = to;
657 }
658
659 void ShenandoahHeapRegion::record_pin() {
660 Atomic::add(&_critical_pins, (size_t)1);
661 }
662
663 void ShenandoahHeapRegion::record_unpin() {
664 assert(pin_count() > 0, "Region " SIZE_FORMAT " should have non-zero pins", index());
665 Atomic::sub(&_critical_pins, (size_t)1);
666 }
667
668 size_t ShenandoahHeapRegion::pin_count() const {
669 return Atomic::load(&_critical_pins);
670 }
|
1 /*
2 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2013, 2020, 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 "gc/shared/cardTable.hpp"
29 #include "gc/shared/space.inline.hpp"
30 #include "gc/shared/tlab_globals.hpp"
31 #include "gc/shenandoah/shenandoahCardTable.hpp"
32 #include "gc/shenandoah/shenandoahFreeSet.hpp"
33 #include "gc/shenandoah/shenandoahHeapRegionSet.inline.hpp"
34 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
35 #include "gc/shenandoah/shenandoahHeapRegion.hpp"
36 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
37 #include "gc/shenandoah/shenandoahOldGeneration.hpp"
38 #include "gc/shenandoah/shenandoahGeneration.hpp"
39 #include "gc/shenandoah/shenandoahYoungGeneration.hpp"
40 #include "gc/shenandoah/shenandoahScanRemembered.inline.hpp"
41 #include "jfr/jfrEvents.hpp"
42 #include "memory/allocation.hpp"
43 #include "memory/iterator.inline.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "memory/universe.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "runtime/atomic.hpp"
48 #include "runtime/globals_extension.hpp"
49 #include "runtime/java.hpp"
50 #include "runtime/mutexLocker.hpp"
51 #include "runtime/os.hpp"
52 #include "runtime/safepoint.hpp"
53 #include "utilities/powerOfTwo.hpp"
54
55
56 size_t ShenandoahHeapRegion::RegionCount = 0;
57 size_t ShenandoahHeapRegion::RegionSizeBytes = 0;
58 size_t ShenandoahHeapRegion::RegionSizeWords = 0;
59 size_t ShenandoahHeapRegion::RegionSizeBytesShift = 0;
60 size_t ShenandoahHeapRegion::RegionSizeWordsShift = 0;
61 size_t ShenandoahHeapRegion::RegionSizeBytesMask = 0;
62 size_t ShenandoahHeapRegion::RegionSizeWordsMask = 0;
63 size_t ShenandoahHeapRegion::MaxTLABSizeBytes = 0;
64 size_t ShenandoahHeapRegion::MaxTLABSizeWords = 0;
65
66 ShenandoahHeapRegion::ShenandoahHeapRegion(HeapWord* start, size_t index, bool committed) :
67 _index(index),
68 _bottom(start),
69 _end(start + RegionSizeWords),
70 _new_top(nullptr),
71 _empty_time(os::elapsedTime()),
72 _top_before_promoted(nullptr),
73 _state(committed ? _empty_committed : _empty_uncommitted),
74 _top(start),
75 _tlab_allocs(0),
76 _gclab_allocs(0),
77 _plab_allocs(0),
78 _live_data(0),
79 _critical_pins(0),
80 _update_watermark(start),
81 _age(0),
82 #ifdef SHENANDOAH_CENSUS_NOISE
83 _youth(0),
84 #endif // SHENANDOAH_CENSUS_NOISE
85 _needs_bitmap_reset(false)
86 {
87
88 assert(Universe::on_page_boundary(_bottom) && Universe::on_page_boundary(_end),
89 "invalid space boundaries");
90 if (ZapUnusedHeapArea && committed) {
91 SpaceMangler::mangle_region(MemRegion(_bottom, _end));
92 }
93 _recycling.unset();
94 }
95
96 void ShenandoahHeapRegion::report_illegal_transition(const char *method) {
97 stringStream ss;
98 ss.print("Illegal region state transition from \"%s\", at %s\n ", region_state_to_string(state()), method);
99 print_on(&ss);
100 fatal("%s", ss.freeze());
101 }
102
103 void ShenandoahHeapRegion::make_regular_allocation(ShenandoahAffiliation affiliation) {
104 shenandoah_assert_heaplocked();
105 reset_age();
106 switch (state()) {
107 case _empty_uncommitted:
108 do_commit();
109 case _empty_committed:
110 assert(this->affiliation() == affiliation, "Region affiliation should already be established");
111 set_state(_regular);
112 case _regular:
113 case _pinned:
114 return;
115 default:
116 report_illegal_transition("regular allocation");
117 }
118 }
119
120 // Change affiliation to YOUNG_GENERATION if _state is not _pinned_cset, _regular, or _pinned. This implements
121 // behavior previously performed as a side effect of make_regular_bypass(). This is used by Full GC in non-generational
122 // modes to transition regions from FREE. Note that all non-free regions in single-generational modes are young.
123 void ShenandoahHeapRegion::make_affiliated_maybe() {
124 shenandoah_assert_heaplocked();
125 assert(!ShenandoahHeap::heap()->mode()->is_generational(), "Only call if non-generational");
126 switch (state()) {
127 case _empty_uncommitted:
128 case _empty_committed:
129 case _cset:
130 case _humongous_start:
131 case _humongous_cont:
132 if (affiliation() != YOUNG_GENERATION) {
133 set_affiliation(YOUNG_GENERATION);
134 }
135 return;
136 case _pinned_cset:
137 case _regular:
138 case _pinned:
139 return;
140 default:
141 assert(false, "Unexpected _state in make_affiliated_maybe");
142 }
143 }
144
145 void ShenandoahHeapRegion::make_regular_bypass() {
146 shenandoah_assert_heaplocked();
147 assert (ShenandoahHeap::heap()->is_full_gc_in_progress() || ShenandoahHeap::heap()->is_degenerated_gc_in_progress(),
148 "only for full or degen GC");
149 reset_age();
150 auto cur_state = state();
151 switch (cur_state) {
152 case _empty_uncommitted:
153 do_commit();
154 case _empty_committed:
155 case _cset:
156 case _humongous_start:
157 case _humongous_cont:
158 if (cur_state == _humongous_start || cur_state == _humongous_cont) {
159 // CDS allocates chunks of the heap to fill with regular objects. The allocator
160 // will dutifully track any waste in the unused portion of the last region. Once
161 // CDS has finished initializing the objects, it will convert these regions to
162 // regular regions. The 'waste' in the last region is no longer wasted at this point,
163 // so we must stop treating it as such.
164 decrement_humongous_waste();
165 }
166 set_state(_regular);
167 return;
168 case _pinned_cset:
169 set_state(_pinned);
170 return;
171 case _regular:
172 case _pinned:
173 return;
174 default:
175 report_illegal_transition("regular bypass");
176 }
177 }
178
179 void ShenandoahHeapRegion::make_humongous_start() {
180 shenandoah_assert_heaplocked();
181 reset_age();
182 switch (state()) {
183 case _empty_uncommitted:
184 do_commit();
185 case _empty_committed:
186 set_state(_humongous_start);
187 return;
188 default:
189 report_illegal_transition("humongous start allocation");
190 }
191 }
192
193 void ShenandoahHeapRegion::make_humongous_start_bypass(ShenandoahAffiliation affiliation) {
194 shenandoah_assert_heaplocked();
195 assert (ShenandoahHeap::heap()->is_full_gc_in_progress(), "only for full GC");
196 // Don't bother to account for affiliated regions during Full GC. We recompute totals at end.
197 set_affiliation(affiliation);
198 reset_age();
199 switch (state()) {
200 case _empty_committed:
201 case _regular:
202 case _humongous_start:
203 case _humongous_cont:
204 set_state(_humongous_start);
205 return;
206 default:
207 report_illegal_transition("humongous start bypass");
208 }
209 }
210
211 void ShenandoahHeapRegion::make_humongous_cont() {
212 shenandoah_assert_heaplocked();
213 reset_age();
214 switch (state()) {
215 case _empty_uncommitted:
216 do_commit();
217 case _empty_committed:
218 set_state(_humongous_cont);
219 return;
220 default:
221 report_illegal_transition("humongous continuation allocation");
222 }
223 }
224
225 void ShenandoahHeapRegion::make_humongous_cont_bypass(ShenandoahAffiliation affiliation) {
226 shenandoah_assert_heaplocked();
227 assert (ShenandoahHeap::heap()->is_full_gc_in_progress(), "only for full GC");
228 set_affiliation(affiliation);
229 // Don't bother to account for affiliated regions during Full GC. We recompute totals at end.
230 reset_age();
231 switch (state()) {
232 case _empty_committed:
233 case _regular:
234 case _humongous_start:
235 case _humongous_cont:
236 set_state(_humongous_cont);
237 return;
238 default:
239 report_illegal_transition("humongous continuation bypass");
240 }
241 }
242
243 void ShenandoahHeapRegion::make_pinned() {
244 shenandoah_assert_heaplocked();
245 assert(pin_count() > 0, "Should have pins: " SIZE_FORMAT, pin_count());
246
247 switch (state()) {
248 case _regular:
249 set_state(_pinned);
250 case _pinned_cset:
251 case _pinned:
252 return;
253 case _humongous_start:
254 set_state(_pinned_humongous_start);
255 case _pinned_humongous_start:
256 return;
257 case _cset:
258 set_state(_pinned_cset);
259 return;
260 default:
261 report_illegal_transition("pinning");
262 }
263 }
264
265 void ShenandoahHeapRegion::make_unpinned() {
266 shenandoah_assert_heaplocked();
267 assert(pin_count() == 0, "Should not have pins: " SIZE_FORMAT, pin_count());
268
269 switch (state()) {
270 case _pinned:
271 assert(is_affiliated(), "Pinned region should be affiliated");
272 set_state(_regular);
273 return;
274 case _regular:
275 case _humongous_start:
276 return;
277 case _pinned_cset:
278 set_state(_cset);
279 return;
280 case _pinned_humongous_start:
281 set_state(_humongous_start);
282 return;
283 default:
284 report_illegal_transition("unpinning");
285 }
286 }
287
288 void ShenandoahHeapRegion::make_cset() {
289 shenandoah_assert_heaplocked();
290 // Leave age untouched. We need to consult the age when we are deciding whether to promote evacuated objects.
291 switch (state()) {
292 case _regular:
293 set_state(_cset);
294 case _cset:
295 return;
296 default:
297 report_illegal_transition("cset");
298 }
299 }
300
301 void ShenandoahHeapRegion::make_trash() {
302 shenandoah_assert_heaplocked();
303 reset_age();
304 switch (state()) {
305 case _humongous_start:
306 case _humongous_cont:
307 {
308 // Reclaiming humongous regions and reclaim humongous waste. When this region is eventually recycled, we'll reclaim
309 // its used memory. At recycle time, we no longer recognize this as a humongous region.
310 decrement_humongous_waste();
311 }
312 case _cset:
313 // Reclaiming cset regions
314 case _regular:
315 // Immediate region reclaim
316 set_state(_trash);
317 return;
318 default:
319 report_illegal_transition("trashing");
320 }
321 }
322
323 void ShenandoahHeapRegion::make_trash_immediate() {
324 make_trash();
325
326 // On this path, we know there are no marked objects in the region,
327 // tell marking context about it to bypass bitmap resets.
328 assert(ShenandoahHeap::heap()->gc_generation()->is_mark_complete(), "Marking should be complete here.");
329 shenandoah_assert_generations_reconciled();
330 ShenandoahHeap::heap()->marking_context()->reset_top_bitmap(this);
331 }
332
333 void ShenandoahHeapRegion::make_empty() {
334 reset_age();
335 CENSUS_NOISE(clear_youth();)
336 switch (state()) {
337 case _trash:
338 set_state(_empty_committed);
339 _empty_time = os::elapsedTime();
340 return;
341 default:
342 report_illegal_transition("emptying");
343 }
344 }
345
346 void ShenandoahHeapRegion::make_uncommitted() {
347 shenandoah_assert_heaplocked();
348 switch (state()) {
349 case _empty_committed:
350 do_uncommit();
351 set_state(_empty_uncommitted);
352 return;
353 default:
354 report_illegal_transition("uncommiting");
355 }
356 }
357
358 void ShenandoahHeapRegion::make_committed_bypass() {
359 shenandoah_assert_heaplocked();
360 assert (ShenandoahHeap::heap()->is_full_gc_in_progress(), "only for full GC");
361
362 switch (state()) {
363 case _empty_uncommitted:
364 do_commit();
365 set_state(_empty_committed);
366 return;
367 default:
368 report_illegal_transition("commit bypass");
369 }
370 }
371
372 void ShenandoahHeapRegion::reset_alloc_metadata() {
373 _tlab_allocs = 0;
374 _gclab_allocs = 0;
375 _plab_allocs = 0;
376 }
377
378 size_t ShenandoahHeapRegion::get_shared_allocs() const {
379 return used() - (_tlab_allocs + _gclab_allocs + _plab_allocs) * HeapWordSize;
380 }
381
382 size_t ShenandoahHeapRegion::get_tlab_allocs() const {
383 return _tlab_allocs * HeapWordSize;
384 }
385
386 size_t ShenandoahHeapRegion::get_gclab_allocs() const {
387 return _gclab_allocs * HeapWordSize;
388 }
389
390 size_t ShenandoahHeapRegion::get_plab_allocs() const {
391 return _plab_allocs * HeapWordSize;
392 }
393
394 void ShenandoahHeapRegion::set_live_data(size_t s) {
395 assert(Thread::current()->is_VM_thread(), "by VM thread");
396 _live_data = (s >> LogHeapWordSize);
397 }
398
399 void ShenandoahHeapRegion::print_on(outputStream* st) const {
400 st->print("|");
401 st->print(SIZE_FORMAT_W(5), this->_index);
402
403 switch (state()) {
404 case _empty_uncommitted:
405 st->print("|EU ");
406 break;
407 case _empty_committed:
408 st->print("|EC ");
409 break;
410 case _regular:
411 st->print("|R ");
412 break;
413 case _humongous_start:
414 st->print("|H ");
415 break;
416 case _pinned_humongous_start:
417 st->print("|HP ");
418 break;
419 case _humongous_cont:
420 st->print("|HC ");
421 break;
422 case _cset:
423 st->print("|CS ");
424 break;
425 case _trash:
426 st->print("|TR ");
427 break;
428 case _pinned:
429 st->print("|P ");
430 break;
431 case _pinned_cset:
432 st->print("|CSP");
433 break;
434 default:
435 ShouldNotReachHere();
436 }
437
438 st->print("|%s", shenandoah_affiliation_code(affiliation()));
439
440 #define SHR_PTR_FORMAT "%12" PRIxPTR
441
442 st->print("|BTE " SHR_PTR_FORMAT ", " SHR_PTR_FORMAT ", " SHR_PTR_FORMAT,
443 p2i(bottom()), p2i(top()), p2i(end()));
444 st->print("|TAMS " SHR_PTR_FORMAT,
445 p2i(ShenandoahHeap::heap()->marking_context()->top_at_mark_start(const_cast<ShenandoahHeapRegion*>(this))));
446 st->print("|UWM " SHR_PTR_FORMAT,
447 p2i(_update_watermark));
448 st->print("|U " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(used()), proper_unit_for_byte_size(used()));
449 st->print("|T " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_tlab_allocs()), proper_unit_for_byte_size(get_tlab_allocs()));
450 st->print("|G " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_gclab_allocs()), proper_unit_for_byte_size(get_gclab_allocs()));
451 if (ShenandoahHeap::heap()->mode()->is_generational()) {
452 st->print("|P " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_plab_allocs()), proper_unit_for_byte_size(get_plab_allocs()));
453 }
454 st->print("|S " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_shared_allocs()), proper_unit_for_byte_size(get_shared_allocs()));
455 st->print("|L " SIZE_FORMAT_W(5) "%1s", byte_size_in_proper_unit(get_live_data_bytes()), proper_unit_for_byte_size(get_live_data_bytes()));
456 st->print("|CP " SIZE_FORMAT_W(3), pin_count());
457 st->cr();
458
459 #undef SHR_PTR_FORMAT
460 }
461
462 // oop_iterate without closure, return true if completed without cancellation
463 bool ShenandoahHeapRegion::oop_coalesce_and_fill(bool cancellable) {
464
465 assert(!is_humongous(), "No need to fill or coalesce humongous regions");
466 if (!is_active()) {
467 end_preemptible_coalesce_and_fill();
468 return true;
469 }
470
471 ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
472 ShenandoahMarkingContext* marking_context = heap->marking_context();
473
474 // Expect marking to be completed before these threads invoke this service.
475 assert(heap->gc_generation()->is_mark_complete(), "sanity");
476 shenandoah_assert_generations_reconciled();
477
478 // All objects above TAMS are considered live even though their mark bits will not be set. Note that young-
479 // gen evacuations that interrupt a long-running old-gen concurrent mark may promote objects into old-gen
480 // while the old-gen concurrent marking is ongoing. These newly promoted objects will reside above TAMS
481 // and will be treated as live during the current old-gen marking pass, even though they will not be
482 // explicitly marked.
483 HeapWord* t = marking_context->top_at_mark_start(this);
484
485 // Resume coalesce and fill from this address
486 HeapWord* obj_addr = resume_coalesce_and_fill();
487
488 while (obj_addr < t) {
489 oop obj = cast_to_oop(obj_addr);
490 if (marking_context->is_marked(obj)) {
491 assert(obj->klass() != nullptr, "klass should not be nullptr");
492 obj_addr += obj->size();
493 } else {
494 // Object is not marked. Coalesce and fill dead object with dead neighbors.
495 HeapWord* next_marked_obj = marking_context->get_next_marked_addr(obj_addr, t);
496 assert(next_marked_obj <= t, "next marked object cannot exceed top");
497 size_t fill_size = next_marked_obj - obj_addr;
498 assert(fill_size >= ShenandoahHeap::min_fill_size(), "previously allocated object known to be larger than min_size");
499 ShenandoahHeap::fill_with_object(obj_addr, fill_size);
500 heap->old_generation()->card_scan()->coalesce_objects(obj_addr, fill_size);
501 obj_addr = next_marked_obj;
502 }
503 if (cancellable && heap->cancelled_gc()) {
504 suspend_coalesce_and_fill(obj_addr);
505 return false;
506 }
507 }
508 // Mark that this region has been coalesced and filled
509 end_preemptible_coalesce_and_fill();
510 return true;
511 }
512
513 size_t get_card_count(size_t words) {
514 assert(words % CardTable::card_size_in_words() == 0, "Humongous iteration must span whole number of cards");
515 assert(CardTable::card_size_in_words() * (words / CardTable::card_size_in_words()) == words,
516 "slice must be integral number of cards");
517 return words / CardTable::card_size_in_words();
518 }
519
520 void ShenandoahHeapRegion::oop_iterate_humongous_slice_dirty(OopIterateClosure* blk,
521 HeapWord* start, size_t words, bool write_table) const {
522 assert(is_humongous(), "only humongous region here");
523
524 ShenandoahHeapRegion* r = humongous_start_region();
525 oop obj = cast_to_oop(r->bottom());
526 size_t num_cards = get_card_count(words);
527
528 ShenandoahGenerationalHeap* heap = ShenandoahGenerationalHeap::heap();
529 ShenandoahScanRemembered* scanner = heap->old_generation()->card_scan();
530 size_t card_index = scanner->card_index_for_addr(start);
531 if (write_table) {
532 while (num_cards-- > 0) {
533 if (scanner->is_write_card_dirty(card_index++)) {
534 obj->oop_iterate(blk, MemRegion(start, start + CardTable::card_size_in_words()));
535 }
536 start += CardTable::card_size_in_words();
537 }
538 } else {
539 while (num_cards-- > 0) {
540 if (scanner->is_card_dirty(card_index++)) {
541 obj->oop_iterate(blk, MemRegion(start, start + CardTable::card_size_in_words()));
542 }
543 start += CardTable::card_size_in_words();
544 }
545 }
546 }
547
548 void ShenandoahHeapRegion::oop_iterate_humongous_slice_all(OopIterateClosure* cl, HeapWord* start, size_t words) const {
549 assert(is_humongous(), "only humongous region here");
550
551 ShenandoahHeapRegion* r = humongous_start_region();
552 oop obj = cast_to_oop(r->bottom());
553
554 // Scan all data, regardless of whether cards are dirty
555 obj->oop_iterate(cl, MemRegion(start, start + words));
556 }
557
558 ShenandoahHeapRegion* ShenandoahHeapRegion::humongous_start_region() const {
559 ShenandoahHeap* heap = ShenandoahHeap::heap();
560 assert(is_humongous(), "Must be a part of the humongous region");
561 size_t i = index();
562 ShenandoahHeapRegion* r = const_cast<ShenandoahHeapRegion*>(this);
563 while (!r->is_humongous_start()) {
564 assert(i > 0, "Sanity");
565 i--;
566 r = heap->get_region(i);
567 assert(r->is_humongous(), "Must be a part of the humongous region");
568 }
569 assert(r->is_humongous_start(), "Must be");
570 return r;
571 }
572
573
574 void ShenandoahHeapRegion::recycle_internal() {
575 assert(_recycling.is_set() && is_trash(), "Wrong state");
576 ShenandoahHeap* heap = ShenandoahHeap::heap();
577
578 set_top(bottom());
579 clear_live_data();
580 reset_alloc_metadata();
581 heap->marking_context()->reset_top_at_mark_start(this);
582 set_update_watermark(bottom());
583 if (ZapUnusedHeapArea) {
584 SpaceMangler::mangle_region(MemRegion(bottom(), end()));
585 }
586
587 make_empty();
588 set_affiliation(FREE);
589 }
590
591 void ShenandoahHeapRegion::try_recycle_under_lock() {
592 shenandoah_assert_heaplocked();
593 if (is_trash() && _recycling.try_set()) {
594 if (is_trash()) {
595 ShenandoahHeap* heap = ShenandoahHeap::heap();
596 ShenandoahGeneration* generation = heap->generation_for(affiliation());
597
598 heap->decrease_used(generation, used());
599 generation->decrement_affiliated_region_count();
600
601 recycle_internal();
602 }
603 _recycling.unset();
604 } else {
605 // Ensure recycling is unset before returning to mutator to continue memory allocation.
606 while (_recycling.is_set()) {
607 if (os::is_MP()) {
608 SpinPause();
609 } else {
610 os::naked_yield();
611 }
612 }
613 }
614 }
615
616 void ShenandoahHeapRegion::try_recycle() {
617 shenandoah_assert_not_heaplocked();
618 if (is_trash() && _recycling.try_set()) {
619 // Double check region state after win the race to set recycling flag
620 if (is_trash()) {
621 ShenandoahHeap* heap = ShenandoahHeap::heap();
622 ShenandoahGeneration* generation = heap->generation_for(affiliation());
623 heap->decrease_used(generation, used());
624 generation->decrement_affiliated_region_count_without_lock();
625
626 recycle_internal();
627 }
628 _recycling.unset();
629 }
630 }
631
632 HeapWord* ShenandoahHeapRegion::block_start(const void* p) const {
633 assert(MemRegion(bottom(), end()).contains(p),
634 "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
635 p2i(p), p2i(bottom()), p2i(end()));
636 if (p >= top()) {
637 return top();
638 } else {
639 HeapWord* last = bottom();
640 HeapWord* cur = last;
641 while (cur <= p) {
642 last = cur;
643 cur += cast_to_oop(cur)->size();
644 }
645 shenandoah_assert_correct(nullptr, cast_to_oop(last));
646 return last;
647 }
648 }
650 size_t ShenandoahHeapRegion::block_size(const HeapWord* p) const {
651 assert(MemRegion(bottom(), end()).contains(p),
652 "p (" PTR_FORMAT ") not in space [" PTR_FORMAT ", " PTR_FORMAT ")",
653 p2i(p), p2i(bottom()), p2i(end()));
654 if (p < top()) {
655 return cast_to_oop(p)->size();
656 } else {
657 assert(p == top(), "just checking");
658 return pointer_delta(end(), (HeapWord*) p);
659 }
660 }
661
662 size_t ShenandoahHeapRegion::setup_sizes(size_t max_heap_size) {
663 // Absolute minimums we should not ever break.
664 static const size_t MIN_REGION_SIZE = 256*K;
665
666 if (FLAG_IS_DEFAULT(ShenandoahMinRegionSize)) {
667 FLAG_SET_DEFAULT(ShenandoahMinRegionSize, MIN_REGION_SIZE);
668 }
669
670 // Generational Shenandoah needs this alignment for card tables.
671 if (strcmp(ShenandoahGCMode, "generational") == 0) {
672 max_heap_size = align_up(max_heap_size , CardTable::ct_max_alignment_constraint());
673 }
674
675 size_t region_size;
676 if (FLAG_IS_DEFAULT(ShenandoahRegionSize)) {
677 if (ShenandoahMinRegionSize > max_heap_size / MIN_NUM_REGIONS) {
678 err_msg message("Max heap size (" SIZE_FORMAT "%s) is too low to afford the minimum number "
679 "of regions (" SIZE_FORMAT ") of minimum region size (" SIZE_FORMAT "%s).",
680 byte_size_in_proper_unit(max_heap_size), proper_unit_for_byte_size(max_heap_size),
681 MIN_NUM_REGIONS,
682 byte_size_in_proper_unit(ShenandoahMinRegionSize), proper_unit_for_byte_size(ShenandoahMinRegionSize));
683 vm_exit_during_initialization("Invalid -XX:ShenandoahMinRegionSize option", message);
684 }
685 if (ShenandoahMinRegionSize < MIN_REGION_SIZE) {
686 err_msg message("" SIZE_FORMAT "%s should not be lower than minimum region size (" SIZE_FORMAT "%s).",
687 byte_size_in_proper_unit(ShenandoahMinRegionSize), proper_unit_for_byte_size(ShenandoahMinRegionSize),
688 byte_size_in_proper_unit(MIN_REGION_SIZE), proper_unit_for_byte_size(MIN_REGION_SIZE));
689 vm_exit_during_initialization("Invalid -XX:ShenandoahMinRegionSize option", message);
690 }
691 if (ShenandoahMinRegionSize < MinTLABSize) {
692 err_msg message("" SIZE_FORMAT "%s should not be lower than TLAB size size (" SIZE_FORMAT "%s).",
693 byte_size_in_proper_unit(ShenandoahMinRegionSize), proper_unit_for_byte_size(ShenandoahMinRegionSize),
694 byte_size_in_proper_unit(MinTLABSize), proper_unit_for_byte_size(MinTLABSize));
773 RegionSizeBytesShift = (size_t)region_size_log;
774
775 guarantee(RegionSizeWordsShift == 0, "we should only set it once");
776 RegionSizeWordsShift = RegionSizeBytesShift - LogHeapWordSize;
777
778 guarantee(RegionSizeBytes == 0, "we should only set it once");
779 RegionSizeBytes = region_size;
780 RegionSizeWords = RegionSizeBytes >> LogHeapWordSize;
781 assert (RegionSizeWords*HeapWordSize == RegionSizeBytes, "sanity");
782
783 guarantee(RegionSizeWordsMask == 0, "we should only set it once");
784 RegionSizeWordsMask = RegionSizeWords - 1;
785
786 guarantee(RegionSizeBytesMask == 0, "we should only set it once");
787 RegionSizeBytesMask = RegionSizeBytes - 1;
788
789 guarantee(RegionCount == 0, "we should only set it once");
790 RegionCount = align_up(max_heap_size, RegionSizeBytes) / RegionSizeBytes;
791 guarantee(RegionCount >= MIN_NUM_REGIONS, "Should have at least minimum regions");
792
793 guarantee(MaxTLABSizeWords == 0, "we should only set it once");
794 MaxTLABSizeWords = align_down(RegionSizeWords, MinObjAlignment);
795
796 guarantee(MaxTLABSizeBytes == 0, "we should only set it once");
797 MaxTLABSizeBytes = MaxTLABSizeWords * HeapWordSize;
798 assert (MaxTLABSizeBytes > MinTLABSize, "should be larger");
799
800 return max_heap_size;
801 }
802
803 void ShenandoahHeapRegion::do_commit() {
804 ShenandoahHeap* heap = ShenandoahHeap::heap();
805 if (!heap->is_heap_region_special() && !os::commit_memory((char *) bottom(), RegionSizeBytes, false)) {
806 report_java_out_of_memory("Unable to commit region");
807 }
808 if (!heap->commit_bitmap_slice(this)) {
809 report_java_out_of_memory("Unable to commit bitmaps for region");
810 }
811 if (AlwaysPreTouch) {
812 os::pretouch_memory(bottom(), end(), heap->pretouch_heap_page_size());
813 }
814 heap->increase_committed(ShenandoahHeapRegion::region_size_bytes());
815 }
816
817 void ShenandoahHeapRegion::do_uncommit() {
818 ShenandoahHeap* heap = ShenandoahHeap::heap();
819 if (!heap->is_heap_region_special() && !os::uncommit_memory((char *) bottom(), RegionSizeBytes)) {
820 report_java_out_of_memory("Unable to uncommit region");
821 }
822 if (!heap->uncommit_bitmap_slice(this)) {
823 report_java_out_of_memory("Unable to uncommit bitmaps for region");
824 }
825 heap->decrease_committed(ShenandoahHeapRegion::region_size_bytes());
826 }
827
828 void ShenandoahHeapRegion::set_state(RegionState to) {
829 EventShenandoahHeapRegionStateChange evt;
830 if (evt.should_commit()){
831 evt.set_index((unsigned) index());
832 evt.set_start((uintptr_t)bottom());
833 evt.set_used(used());
834 evt.set_from(state());
835 evt.set_to(to);
836 evt.commit();
837 }
838 Atomic::store(&_state, to);
839 }
840
841 void ShenandoahHeapRegion::record_pin() {
842 Atomic::add(&_critical_pins, (size_t)1);
843 }
844
845 void ShenandoahHeapRegion::record_unpin() {
846 assert(pin_count() > 0, "Region " SIZE_FORMAT " should have non-zero pins", index());
847 Atomic::sub(&_critical_pins, (size_t)1);
848 }
849
850 size_t ShenandoahHeapRegion::pin_count() const {
851 return Atomic::load(&_critical_pins);
852 }
853
854 void ShenandoahHeapRegion::set_affiliation(ShenandoahAffiliation new_affiliation) {
855 ShenandoahHeap* heap = ShenandoahHeap::heap();
856
857 ShenandoahAffiliation region_affiliation = heap->region_affiliation(this);
858 {
859 ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
860 log_debug(gc)("Setting affiliation of Region " SIZE_FORMAT " from %s to %s, top: " PTR_FORMAT ", TAMS: " PTR_FORMAT
861 ", watermark: " PTR_FORMAT ", top_bitmap: " PTR_FORMAT,
862 index(), shenandoah_affiliation_name(region_affiliation), shenandoah_affiliation_name(new_affiliation),
863 p2i(top()), p2i(ctx->top_at_mark_start(this)), p2i(_update_watermark), p2i(ctx->top_bitmap(this)));
864 }
865
866 #ifdef ASSERT
867 {
868 // During full gc, heap->complete_marking_context() is not valid, may equal nullptr.
869 ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
870 size_t idx = this->index();
871 HeapWord* top_bitmap = ctx->top_bitmap(this);
872
873 assert(ctx->is_bitmap_range_within_region_clear(top_bitmap, _end),
874 "Region " SIZE_FORMAT ", bitmap should be clear between top_bitmap: " PTR_FORMAT " and end: " PTR_FORMAT, idx,
875 p2i(top_bitmap), p2i(_end));
876 }
877 #endif
878
879 if (region_affiliation == new_affiliation) {
880 return;
881 }
882
883 if (!heap->mode()->is_generational()) {
884 log_trace(gc)("Changing affiliation of region %zu from %s to %s",
885 index(), affiliation_name(), shenandoah_affiliation_name(new_affiliation));
886 heap->set_affiliation(this, new_affiliation);
887 return;
888 }
889
890 switch (new_affiliation) {
891 case FREE:
892 assert(!has_live(), "Free region should not have live data");
893 break;
894 case YOUNG_GENERATION:
895 reset_age();
896 break;
897 case OLD_GENERATION:
898 break;
899 default:
900 ShouldNotReachHere();
901 return;
902 }
903 heap->set_affiliation(this, new_affiliation);
904 }
905
906 void ShenandoahHeapRegion::decrement_humongous_waste() const {
907 assert(is_humongous(), "Should only use this for humongous regions");
908 size_t waste_bytes = free();
909 if (waste_bytes > 0) {
910 ShenandoahHeap* heap = ShenandoahHeap::heap();
911 ShenandoahGeneration* generation = heap->generation_for(affiliation());
912 heap->decrease_humongous_waste(generation, waste_bytes);
913 }
914 }
|