1 /*
2 * Copyright (c) 2005, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/classLoaderDataGraph.hpp"
26 #include "classfile/javaClasses.inline.hpp"
27 #include "classfile/stringTable.hpp"
28 #include "classfile/symbolTable.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "code/codeCache.hpp"
31 #include "code/nmethod.hpp"
32 #include "compiler/oopMap.hpp"
33 #include "cppstdlib/new.hpp"
34 #include "gc/parallel/objectStartArray.inline.hpp"
35 #include "gc/parallel/parallelArguments.hpp"
36 #include "gc/parallel/parallelScavengeHeap.inline.hpp"
37 #include "gc/parallel/parMarkBitMap.inline.hpp"
38 #include "gc/parallel/psAdaptiveSizePolicy.hpp"
39 #include "gc/parallel/psCompactionManager.inline.hpp"
40 #include "gc/parallel/psOldGen.hpp"
41 #include "gc/parallel/psParallelCompact.inline.hpp"
42 #include "gc/parallel/psPromotionManager.inline.hpp"
43 #include "gc/parallel/psRootType.hpp"
44 #include "gc/parallel/psScavenge.hpp"
45 #include "gc/parallel/psStringDedup.hpp"
46 #include "gc/parallel/psYoungGen.hpp"
47 #include "gc/shared/classUnloadingContext.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "gc/shared/fullGCForwarding.inline.hpp"
50 #include "gc/shared/gcCause.hpp"
51 #include "gc/shared/gcHeapSummary.hpp"
52 #include "gc/shared/gcId.hpp"
53 #include "gc/shared/gcLocker.hpp"
54 #include "gc/shared/gcTimer.hpp"
55 #include "gc/shared/gcTrace.hpp"
56 #include "gc/shared/gcTraceTime.inline.hpp"
57 #include "gc/shared/gcVMOperations.hpp"
58 #include "gc/shared/isGCActiveMark.hpp"
59 #include "gc/shared/oopStorage.inline.hpp"
60 #include "gc/shared/oopStorageSet.inline.hpp"
61 #include "gc/shared/oopStorageSetParState.inline.hpp"
62 #include "gc/shared/parallelCleaning.hpp"
63 #include "gc/shared/preservedMarks.inline.hpp"
64 #include "gc/shared/referencePolicy.hpp"
65 #include "gc/shared/referenceProcessor.hpp"
66 #include "gc/shared/referenceProcessorPhaseTimes.hpp"
67 #include "gc/shared/spaceDecorator.hpp"
68 #include "gc/shared/taskTerminator.hpp"
69 #include "gc/shared/weakProcessor.inline.hpp"
70 #include "gc/shared/workerPolicy.hpp"
71 #include "gc/shared/workerThread.hpp"
72 #include "gc/shared/workerUtils.hpp"
73 #include "logging/log.hpp"
74 #include "memory/iterator.inline.hpp"
75 #include "memory/memoryReserver.hpp"
76 #include "memory/metaspaceUtils.hpp"
77 #include "memory/resourceArea.hpp"
78 #include "memory/universe.hpp"
79 #include "nmt/memTracker.hpp"
80 #include "oops/access.inline.hpp"
81 #include "oops/flatArrayKlass.inline.hpp"
82 #include "oops/instanceClassLoaderKlass.inline.hpp"
83 #include "oops/instanceKlass.inline.hpp"
84 #include "oops/instanceMirrorKlass.inline.hpp"
85 #include "oops/methodData.hpp"
86 #include "oops/objArrayKlass.inline.hpp"
87 #include "oops/oop.inline.hpp"
88 #include "runtime/arguments.hpp"
89 #include "runtime/handles.inline.hpp"
90 #include "runtime/java.hpp"
91 #include "runtime/safepoint.hpp"
92 #include "runtime/threads.hpp"
93 #include "runtime/vmThread.hpp"
94 #include "services/memoryService.hpp"
95 #include "utilities/align.hpp"
96 #include "utilities/debug.hpp"
97 #include "utilities/events.hpp"
98 #include "utilities/formatBuffer.hpp"
99 #include "utilities/macros.hpp"
100 #include "utilities/stack.inline.hpp"
101
102 #include <math.h>
103
104 // All sizes are in HeapWords.
105 const size_t ParallelCompactData::Log2RegionSize = 16; // 64K words
106 const size_t ParallelCompactData::RegionSize = (size_t)1 << Log2RegionSize;
107 static_assert(ParallelCompactData::RegionSize >= BitsPerWord, "region-start bit word-aligned");
108 const size_t ParallelCompactData::RegionSizeBytes =
109 RegionSize << LogHeapWordSize;
110 const size_t ParallelCompactData::RegionSizeOffsetMask = RegionSize - 1;
111 const size_t ParallelCompactData::RegionAddrOffsetMask = RegionSizeBytes - 1;
112 const size_t ParallelCompactData::RegionAddrMask = ~RegionAddrOffsetMask;
113
114 const ParallelCompactData::RegionData::region_sz_t
115 ParallelCompactData::RegionData::dc_shift = 27;
116
117 const ParallelCompactData::RegionData::region_sz_t
118 ParallelCompactData::RegionData::dc_mask = ~0U << dc_shift;
119
120 const ParallelCompactData::RegionData::region_sz_t
121 ParallelCompactData::RegionData::dc_one = 0x1U << dc_shift;
122
123 const ParallelCompactData::RegionData::region_sz_t
124 ParallelCompactData::RegionData::los_mask = ~dc_mask;
125
126 const ParallelCompactData::RegionData::region_sz_t
127 ParallelCompactData::RegionData::dc_claimed = 0x8U << dc_shift;
128
129 const ParallelCompactData::RegionData::region_sz_t
130 ParallelCompactData::RegionData::dc_completed = 0xcU << dc_shift;
131
132 bool ParallelCompactData::RegionData::is_clear() {
133 return (_destination == nullptr) &&
134 (_source_region == 0) &&
135 (_partial_obj_addr == nullptr) &&
136 (_partial_obj_size == 0) &&
137 (dc_and_los() == 0) &&
138 (shadow_state() == 0);
139 }
140
141 #ifdef ASSERT
142 void ParallelCompactData::RegionData::verify_clear() {
143 assert(_destination == nullptr, "inv");
144 assert(_source_region == 0, "inv");
145 assert(_partial_obj_addr == nullptr, "inv");
146 assert(_partial_obj_size == 0, "inv");
147 assert(dc_and_los() == 0, "inv");
148 assert(shadow_state() == 0, "inv");
149 }
150 #endif
151
152 SpaceInfo PSParallelCompact::_space_info[PSParallelCompact::last_space_id];
153
154 SpanSubjectToDiscoveryClosure PSParallelCompact::_span_based_discoverer;
155 ReferenceProcessor* PSParallelCompact::_ref_processor = nullptr;
156
157 void SplitInfo::record(size_t split_region_idx, HeapWord* split_point, size_t preceding_live_words) {
158 assert(split_region_idx != 0, "precondition");
159
160 // Obj denoted by split_point will be deferred to the next space.
161 assert(split_point != nullptr, "precondition");
162
163 const ParallelCompactData& sd = PSParallelCompact::summary_data();
164
165 PSParallelCompact::RegionData* split_region_ptr = sd.region(split_region_idx);
166 assert(preceding_live_words < split_region_ptr->data_size(), "inv");
167
168 HeapWord* preceding_destination = split_region_ptr->destination();
169 assert(preceding_destination != nullptr, "inv");
170
171 // How many regions does the preceding part occupy
172 uint preceding_destination_count;
173 if (preceding_live_words == 0) {
174 preceding_destination_count = 0;
175 } else {
176 // -1 so that the ending address doesn't fall on the region-boundary
177 if (sd.region_align_down(preceding_destination) ==
178 sd.region_align_down(preceding_destination + preceding_live_words - 1)) {
179 preceding_destination_count = 1;
180 } else {
181 preceding_destination_count = 2;
182 }
183 }
184
185 _split_region_idx = split_region_idx;
186 _split_point = split_point;
187 _preceding_live_words = preceding_live_words;
188 _preceding_destination = preceding_destination;
189 _preceding_destination_count = preceding_destination_count;
190 }
191
192 void SplitInfo::clear()
193 {
194 _split_region_idx = 0;
195 _split_point = nullptr;
196 _preceding_live_words = 0;
197 _preceding_destination = nullptr;
198 _preceding_destination_count = 0;
199 assert(!is_valid(), "sanity");
200 }
201
202 #ifdef ASSERT
203 void SplitInfo::verify_clear()
204 {
205 assert(_split_region_idx == 0, "not clear");
206 assert(_split_point == nullptr, "not clear");
207 assert(_preceding_live_words == 0, "not clear");
208 assert(_preceding_destination == nullptr, "not clear");
209 assert(_preceding_destination_count == 0, "not clear");
210 }
211 #endif // #ifdef ASSERT
212
213
214 void PSParallelCompact::print_on(outputStream* st) {
215 _mark_bitmap.print_on(st);
216 }
217
218 ParallelCompactData::ParallelCompactData() :
219 _heap_start(nullptr),
220 DEBUG_ONLY(_heap_end(nullptr) COMMA)
221 _region_vspace(nullptr),
222 _reserved_byte_size(0),
223 _region_data(nullptr),
224 _region_count(0) {}
225
226 bool ParallelCompactData::initialize(MemRegion reserved_heap)
227 {
228 _heap_start = reserved_heap.start();
229 const size_t heap_size = reserved_heap.word_size();
230 DEBUG_ONLY(_heap_end = _heap_start + heap_size;)
231
232 assert(region_align_down(_heap_start) == _heap_start,
233 "region start not aligned");
234 assert(is_aligned(heap_size, RegionSize), "precondition");
235
236 const size_t count = heap_size >> Log2RegionSize;
237 const size_t raw_bytes = count * sizeof(RegionData);
238 const size_t page_sz = os::page_size_for_region_aligned(raw_bytes, 10);
239 const size_t granularity = os::vm_allocation_granularity();
240 const size_t rs_align = MAX2(page_sz, granularity);
241
242 _reserved_byte_size = align_up(raw_bytes, rs_align);
243
244 ReservedSpace rs = MemoryReserver::reserve(_reserved_byte_size,
245 rs_align,
246 page_sz,
247 mtGC);
248
249 if (!rs.is_reserved()) {
250 // Failed to reserve memory.
251 return false;
252 }
253
254 os::trace_page_sizes("Parallel Compact Data", raw_bytes, raw_bytes, rs.base(),
255 rs.size(), page_sz);
256
257 MemTracker::record_virtual_memory_tag(rs, mtGC);
258
259 PSVirtualSpace* region_vspace = new PSVirtualSpace(rs, page_sz);
260
261 if (!region_vspace->expand_by(_reserved_byte_size)) {
262 // Failed to commit memory.
263
264 delete region_vspace;
265
266 // Release memory reserved in the space.
267 MemoryReserver::release(rs);
268
269 return false;
270 }
271
272 _region_vspace = region_vspace;
273 _region_data = (RegionData*)_region_vspace->reserved_low_addr();
274 _region_count = count;
275 return true;
276 }
277
278 void ParallelCompactData::clear_range(size_t beg_region, size_t end_region) {
279 assert(beg_region <= _region_count, "beg_region out of range");
280 assert(end_region <= _region_count, "end_region out of range");
281
282 for (size_t i = beg_region; i < end_region; i++) {
283 ::new (&_region_data[i]) RegionData{};
284 }
285 }
286
287 // The total live words on src_region would overflow the target space, so find
288 // the overflowing object and record the split point. The invariant is that an
289 // obj should not cross space boundary.
290 HeapWord* ParallelCompactData::summarize_split_space(size_t src_region,
291 SplitInfo& split_info,
292 HeapWord* const destination,
293 HeapWord* const target_end,
294 HeapWord** target_next) {
295 assert(destination <= target_end, "sanity");
296 assert(destination + _region_data[src_region].data_size() > target_end,
297 "region should not fit into target space");
298 assert(is_region_aligned(target_end), "sanity");
299
300 size_t partial_obj_size = _region_data[src_region].partial_obj_size();
301
302 if (destination + partial_obj_size > target_end) {
303 assert(partial_obj_size > 0, "inv");
304 // The overflowing obj is from a previous region.
305 //
306 // source-regions:
307 //
308 // ***************
309 // | A|AA |
310 // ***************
311 // ^
312 // | split-point
313 //
314 // dest-region:
315 //
316 // ********
317 // |~~~~A |
318 // ********
319 // ^^
320 // || target-space-end
321 // |
322 // | destination
323 //
324 // AAA would overflow target-space.
325 //
326 HeapWord* overflowing_obj = _region_data[src_region].partial_obj_addr();
327 size_t split_region = addr_to_region_idx(overflowing_obj);
328
329 // The number of live words before the overflowing object on this split region
330 size_t preceding_live_words;
331 if (is_region_aligned(overflowing_obj)) {
332 preceding_live_words = 0;
333 } else {
334 // Words accounted by the overflowing object on the split region
335 size_t overflowing_size = pointer_delta(region_align_up(overflowing_obj), overflowing_obj);
336 preceding_live_words = region(split_region)->data_size() - overflowing_size;
337 }
338
339 split_info.record(split_region, overflowing_obj, preceding_live_words);
340
341 // The [overflowing_obj, src_region_start) part has been accounted for, so
342 // must move back the new_top, now that this overflowing obj is deferred.
343 HeapWord* new_top = destination - pointer_delta(region_to_addr(src_region), overflowing_obj);
344
345 // If the overflowing obj was relocated to its original destination,
346 // those destination regions would have their source_region set. Now that
347 // this overflowing obj is relocated somewhere else, reset the
348 // source_region.
349 {
350 size_t range_start = addr_to_region_idx(region_align_up(new_top));
351 size_t range_end = addr_to_region_idx(region_align_up(destination));
352 for (size_t i = range_start; i < range_end; ++i) {
353 region(i)->set_source_region(0);
354 }
355 }
356
357 // Update new top of target space
358 *target_next = new_top;
359
360 return overflowing_obj;
361 }
362
363 // Obj-iteration to locate the overflowing obj
364 HeapWord* region_start = region_to_addr(src_region);
365 HeapWord* region_end = region_start + RegionSize;
366 HeapWord* cur_addr = region_start + partial_obj_size;
367 size_t live_words = partial_obj_size;
368
369 while (true) {
370 assert(cur_addr < region_end, "inv");
371 cur_addr = PSParallelCompact::mark_bitmap()->find_obj_beg(cur_addr, region_end);
372 // There must be an overflowing obj in this region
373 assert(cur_addr < region_end, "inv");
374
375 oop obj = cast_to_oop(cur_addr);
376 size_t obj_size = obj->size();
377 if (destination + live_words + obj_size > target_end) {
378 // Found the overflowing obj
379 split_info.record(src_region, cur_addr, live_words);
380 *target_next = destination + live_words;
381 return cur_addr;
382 }
383
384 live_words += obj_size;
385 cur_addr += obj_size;
386 }
387 }
388
389 size_t ParallelCompactData::live_words_in_space(const MutableSpace* space,
390 HeapWord** full_region_prefix_end) {
391 size_t cur_region = addr_to_region_idx(space->bottom());
392 const size_t end_region = addr_to_region_idx(region_align_up(space->top()));
393 size_t live_words = 0;
394 if (full_region_prefix_end == nullptr) {
395 for (/* empty */; cur_region < end_region; ++cur_region) {
396 live_words += _region_data[cur_region].data_size();
397 }
398 } else {
399 bool first_set = false;
400 for (/* empty */; cur_region < end_region; ++cur_region) {
401 size_t live_words_in_region = _region_data[cur_region].data_size();
402 if (!first_set && live_words_in_region < RegionSize) {
403 *full_region_prefix_end = region_to_addr(cur_region);
404 first_set = true;
405 }
406 live_words += live_words_in_region;
407 }
408 if (!first_set) {
409 // All regions are full of live objs.
410 assert(is_region_aligned(space->top()), "inv");
411 *full_region_prefix_end = space->top();
412 }
413 assert(*full_region_prefix_end != nullptr, "postcondition");
414 assert(is_region_aligned(*full_region_prefix_end), "inv");
415 assert(*full_region_prefix_end >= space->bottom(), "in-range");
416 assert(*full_region_prefix_end <= space->top(), "in-range");
417 }
418 return live_words;
419 }
420
421 bool ParallelCompactData::summarize(SplitInfo& split_info,
422 HeapWord* source_beg, HeapWord* source_end,
423 HeapWord** source_next,
424 HeapWord* target_beg, HeapWord* target_end,
425 HeapWord** target_next)
426 {
427 HeapWord* const source_next_val = source_next == nullptr ? nullptr : *source_next;
428 log_develop_trace(gc, compaction)(
429 "sb=" PTR_FORMAT " se=" PTR_FORMAT " sn=" PTR_FORMAT
430 "tb=" PTR_FORMAT " te=" PTR_FORMAT " tn=" PTR_FORMAT,
431 p2i(source_beg), p2i(source_end), p2i(source_next_val),
432 p2i(target_beg), p2i(target_end), p2i(*target_next));
433
434 size_t cur_region = addr_to_region_idx(source_beg);
435 const size_t end_region = addr_to_region_idx(region_align_up(source_end));
436
437 HeapWord *dest_addr = target_beg;
438 for (/* empty */; cur_region < end_region; cur_region++) {
439 size_t words = _region_data[cur_region].data_size();
440
441 // Skip empty ones
442 if (words == 0) {
443 continue;
444 }
445
446 if (split_info.is_split(cur_region)) {
447 assert(words > split_info.preceding_live_words(), "inv");
448 words -= split_info.preceding_live_words();
449 }
450
451 _region_data[cur_region].set_destination(dest_addr);
452
453 // If cur_region does not fit entirely into the target space, find a point
454 // at which the source space can be 'split' so that part is copied to the
455 // target space and the rest is copied elsewhere.
456 if (dest_addr + words > target_end) {
457 assert(source_next != nullptr, "source_next is null when splitting");
458 *source_next = summarize_split_space(cur_region, split_info, dest_addr,
459 target_end, target_next);
460 return false;
461 }
462
463 uint destination_count = split_info.is_split(cur_region)
464 ? split_info.preceding_destination_count()
465 : 0;
466
467 HeapWord* const last_addr = dest_addr + words - 1;
468 const size_t dest_region_1 = addr_to_region_idx(dest_addr);
469 const size_t dest_region_2 = addr_to_region_idx(last_addr);
470
471 // Initially assume that the destination regions will be the same and
472 // adjust the value below if necessary. Under this assumption, if
473 // cur_region == dest_region_2, then cur_region will be compacted
474 // completely into itself.
475 destination_count += cur_region == dest_region_2 ? 0 : 1;
476 if (dest_region_1 != dest_region_2) {
477 // Destination regions differ; adjust destination_count.
478 destination_count += 1;
479 // Data from cur_region will be copied to the start of dest_region_2.
480 _region_data[dest_region_2].set_source_region(cur_region);
481 } else if (is_region_aligned(dest_addr)) {
482 // Data from cur_region will be copied to the start of the destination
483 // region.
484 _region_data[dest_region_1].set_source_region(cur_region);
485 }
486
487 _region_data[cur_region].set_destination_count(destination_count);
488 dest_addr += words;
489 }
490
491 *target_next = dest_addr;
492 return true;
493 }
494
495 #ifdef ASSERT
496 void ParallelCompactData::verify_clear() {
497 for (uint cur_idx = 0; cur_idx < region_count(); ++cur_idx) {
498 if (!region(cur_idx)->is_clear()) {
499 log_warning(gc)("Uncleared Region: %u", cur_idx);
500 region(cur_idx)->verify_clear();
501 }
502 }
503 }
504 #endif // #ifdef ASSERT
505
506 STWGCTimer PSParallelCompact::_gc_timer;
507 ParallelOldTracer PSParallelCompact::_gc_tracer;
508 elapsedTimer PSParallelCompact::_accumulated_time;
509 unsigned int PSParallelCompact::_maximum_compaction_gc_num = 0;
510 CollectorCounters* PSParallelCompact::_counters = nullptr;
511 ParMarkBitMap PSParallelCompact::_mark_bitmap;
512 ParallelCompactData PSParallelCompact::_summary_data;
513
514 PSParallelCompact::IsAliveClosure PSParallelCompact::_is_alive_closure;
515
516 class PCAdjustPointerClosure: public BasicOopIterateClosure {
517 template <typename T>
518 void do_oop_work(T* p) { PSParallelCompact::adjust_pointer(p); }
519
520 public:
521 virtual void do_oop(oop* p) { do_oop_work(p); }
522 virtual void do_oop(narrowOop* p) { do_oop_work(p); }
523
524 virtual ReferenceIterationMode reference_iteration_mode() { return DO_FIELDS; }
525 };
526
527 static PCAdjustPointerClosure pc_adjust_pointer_closure;
528
529 bool PSParallelCompact::IsAliveClosure::do_object_b(oop p) { return mark_bitmap()->is_marked(p); }
530
531 void PSParallelCompact::post_initialize() {
532 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
533 _span_based_discoverer.set_span(heap->reserved_region());
534 _ref_processor =
535 new ReferenceProcessor(&_span_based_discoverer,
536 ParallelGCThreads, // mt processing degree
537 ParallelGCThreads, // mt discovery degree
538 false, // concurrent_discovery
539 &_is_alive_closure); // non-header is alive closure
540
541 _counters = new CollectorCounters("Parallel full collection pauses", 1);
542
543 // Initialize static fields in ParCompactionManager.
544 ParCompactionManager::initialize(mark_bitmap());
545 }
546
547 bool PSParallelCompact::initialize_aux_data() {
548 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
549 MemRegion mr = heap->reserved_region();
550 assert(mr.byte_size() != 0, "heap should be reserved");
551
552 initialize_space_info();
553
554 if (!_mark_bitmap.initialize(mr)) {
555 vm_shutdown_during_initialization(
556 err_msg("Unable to allocate %zuKB bitmaps for parallel "
557 "garbage collection for the requested %zuKB heap.",
558 _mark_bitmap.reserved_byte_size()/K, mr.byte_size()/K));
559 return false;
560 }
561
562 if (!_summary_data.initialize(mr)) {
563 vm_shutdown_during_initialization(
564 err_msg("Unable to allocate %zuKB card tables for parallel "
565 "garbage collection for the requested %zuKB heap.",
566 _summary_data.reserved_byte_size()/K, mr.byte_size()/K));
567 return false;
568 }
569
570 return true;
571 }
572
573 void PSParallelCompact::initialize_space_info()
574 {
575 memset(&_space_info, 0, sizeof(_space_info));
576
577 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
578 PSYoungGen* young_gen = heap->young_gen();
579
580 _space_info[old_space_id].set_space(heap->old_gen()->object_space());
581 _space_info[eden_space_id].set_space(young_gen->eden_space());
582 _space_info[from_space_id].set_space(young_gen->from_space());
583 _space_info[to_space_id].set_space(young_gen->to_space());
584
585 _space_info[old_space_id].set_start_array(heap->old_gen()->start_array());
586 }
587
588 void
589 PSParallelCompact::clear_data_covering_space(SpaceId id)
590 {
591 // At this point, top is the value before GC, new_top() is the value that will
592 // be set at the end of GC. The marking bitmap is cleared to top; nothing
593 // should be marked above top. The summary data is cleared to the larger of
594 // top & new_top.
595 MutableSpace* const space = _space_info[id].space();
596 HeapWord* const bot = space->bottom();
597 HeapWord* const top = space->top();
598 HeapWord* const max_top = MAX2(top, _space_info[id].new_top());
599
600 _mark_bitmap.clear_range(bot, top);
601
602 const size_t beg_region = _summary_data.addr_to_region_idx(bot);
603 const size_t end_region =
604 _summary_data.addr_to_region_idx(_summary_data.region_align_up(max_top));
605 _summary_data.clear_range(beg_region, end_region);
606
607 // Clear the data used to 'split' regions.
608 SplitInfo& split_info = _space_info[id].split_info();
609 if (split_info.is_valid()) {
610 split_info.clear();
611 }
612 DEBUG_ONLY(split_info.verify_clear();)
613 }
614
615 void PSParallelCompact::pre_compact()
616 {
617 // Update the from & to space pointers in space_info, since they are swapped
618 // at each young gen gc. Do the update unconditionally (even though a
619 // promotion failure does not swap spaces) because an unknown number of young
620 // collections will have swapped the spaces an unknown number of times.
621 GCTraceTime(Debug, gc, phases) tm("Pre Compact", &_gc_timer);
622 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
623 _space_info[from_space_id].set_space(heap->young_gen()->from_space());
624 _space_info[to_space_id].set_space(heap->young_gen()->to_space());
625
626 heap->increment_total_collections(true);
627
628 CodeCache::on_gc_marking_cycle_start();
629
630 heap->print_before_gc();
631 heap->trace_heap_before_gc(&_gc_tracer);
632
633 // Fill in TLABs
634 heap->ensure_parsability(true); // retire TLABs
635
636 if (VerifyBeforeGC && heap->total_collections() >= VerifyGCStartAt) {
637 Universe::verify("Before GC");
638 }
639
640 DEBUG_ONLY(mark_bitmap()->verify_clear();)
641 DEBUG_ONLY(summary_data().verify_clear();)
642 }
643
644 void PSParallelCompact::post_compact()
645 {
646 GCTraceTime(Info, gc, phases) tm("Post Compact", &_gc_timer);
647 ParCompactionManager::remove_all_shadow_regions();
648
649 CodeCache::on_gc_marking_cycle_finish();
650 CodeCache::arm_all_nmethods();
651
652 // Need to clear claim bits for the next full-gc (marking and adjust-pointers).
653 ClassLoaderDataGraph::clear_claimed_marks();
654
655 for (unsigned int id = old_space_id; id < last_space_id; ++id) {
656 // Clear the marking bitmap, summary data and split info.
657 clear_data_covering_space(SpaceId(id));
658 {
659 MutableSpace* space = _space_info[id].space();
660 HeapWord* top = space->top();
661 HeapWord* new_top = _space_info[id].new_top();
662 if (ZapUnusedHeapArea && new_top < top) {
663 space->mangle_region(MemRegion(new_top, top));
664 }
665 // Update top(). Must be done after clearing the bitmap and summary data.
666 space->set_top(new_top);
667 }
668 }
669
670 #ifdef ASSERT
671 {
672 mark_bitmap()->verify_clear();
673 summary_data().verify_clear();
674 }
675 #endif
676
677 ParCompactionManager::flush_all_string_dedup_requests();
678
679 MutableSpace* const eden_space = _space_info[eden_space_id].space();
680 MutableSpace* const from_space = _space_info[from_space_id].space();
681 MutableSpace* const to_space = _space_info[to_space_id].space();
682
683 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
684 bool eden_empty = eden_space->is_empty();
685
686 // Update heap occupancy information which is used as input to the soft ref
687 // clearing policy at the next gc.
688 Universe::heap()->update_capacity_and_used_at_gc();
689
690 bool young_gen_empty = eden_empty && from_space->is_empty() &&
691 to_space->is_empty();
692
693 PSCardTable* ct = heap->card_table();
694 MemRegion old_mr = heap->old_gen()->committed();
695 if (young_gen_empty) {
696 ct->clear_MemRegion(old_mr);
697 } else {
698 ct->dirty_MemRegion(old_mr);
699 }
700
701 heap->prune_scavengable_nmethods();
702
703 #ifdef COMPILER2
704 DerivedPointerTable::update_pointers();
705 #endif // COMPILER2
706
707 // Signal that we have completed a visit to all live objects.
708 Universe::heap()->record_whole_heap_examined_timestamp();
709 }
710
711 HeapWord* PSParallelCompact::compute_dense_prefix_for_old_space(MutableSpace* old_space,
712 HeapWord* full_region_prefix_end) {
713 const size_t region_size = ParallelCompactData::RegionSize;
714 const ParallelCompactData& sd = summary_data();
715
716 // Iteration starts with the region *after* the full-region-prefix-end.
717 const RegionData* const start_region = sd.addr_to_region_ptr(full_region_prefix_end);
718 // If final region is not full, iteration stops before that region,
719 // because fill_dense_prefix_end assumes that prefix_end <= top.
720 const RegionData* const end_region = sd.addr_to_region_ptr(old_space->top());
721 assert(start_region <= end_region, "inv");
722
723 size_t max_waste = old_space->capacity_in_words() * (MarkSweepDeadRatio / 100.0);
724 const RegionData* cur_region = start_region;
725 for (/* empty */; cur_region < end_region; ++cur_region) {
726 assert(region_size >= cur_region->data_size(), "inv");
727 size_t dead_size = region_size - cur_region->data_size();
728 if (max_waste < dead_size) {
729 break;
730 }
731 max_waste -= dead_size;
732 }
733
734 HeapWord* const prefix_end = sd.region_to_addr(cur_region);
735 assert(sd.is_region_aligned(prefix_end), "postcondition");
736 assert(prefix_end >= full_region_prefix_end, "in-range");
737 assert(prefix_end <= old_space->top(), "in-range");
738 return prefix_end;
739 }
740
741 void PSParallelCompact::fill_dense_prefix_end(SpaceId id) {
742 // Comparing two sizes to decide if filling is required:
743 //
744 // The size of the filler (min-obj-size) is 2 heap words with the default
745 // MinObjAlignment, since both markword and klass take 1 heap word.
746 // With +UseCompactObjectHeaders, the minimum filler size is only one word,
747 // because the Klass* gets encoded in the mark-word.
748 //
749 // The size of the gap (if any) right before dense-prefix-end is
750 // MinObjAlignment.
751 //
752 // Need to fill in the gap only if it's smaller than min-obj-size, and the
753 // filler obj will extend to next region.
754
755 if (MinObjAlignment >= checked_cast<int>(CollectedHeap::min_fill_size())) {
756 return;
757 }
758
759 assert(!UseCompactObjectHeaders, "Compact headers can allocate small objects");
760 assert(CollectedHeap::min_fill_size() == 2, "inv");
761 HeapWord* const dense_prefix_end = dense_prefix(id);
762 assert(_summary_data.is_region_aligned(dense_prefix_end), "precondition");
763 assert(dense_prefix_end <= space(id)->top(), "precondition");
764 if (dense_prefix_end == space(id)->top()) {
765 // Must not have single-word gap right before prefix-end/top.
766 return;
767 }
768 RegionData* const region_after_dense_prefix = _summary_data.addr_to_region_ptr(dense_prefix_end);
769
770 if (region_after_dense_prefix->partial_obj_size() != 0 ||
771 _mark_bitmap.is_marked(dense_prefix_end)) {
772 // The region after the dense prefix starts with live bytes.
773 return;
774 }
775
776 HeapWord* block_start = start_array(id)->block_start_reaching_into_card(dense_prefix_end);
777 if (block_start == dense_prefix_end - 1) {
778 assert(!_mark_bitmap.is_marked(block_start), "inv");
779 // There is exactly one heap word gap right before the dense prefix end, so we need a filler object.
780 // The filler object will extend into region_after_dense_prefix.
781 const size_t obj_len = 2; // min-fill-size
782 HeapWord* const obj_beg = dense_prefix_end - 1;
783 CollectedHeap::fill_with_object(obj_beg, obj_len);
784 _mark_bitmap.mark_obj(obj_beg);
785 _summary_data.addr_to_region_ptr(obj_beg)->add_live_obj(1);
786 region_after_dense_prefix->set_partial_obj_size(1);
787 region_after_dense_prefix->set_partial_obj_addr(obj_beg);
788 assert(start_array(id) != nullptr, "sanity");
789 start_array(id)->update_for_block(obj_beg, obj_beg + obj_len);
790 }
791 }
792
793 bool PSParallelCompact::check_maximum_compaction(bool should_do_max_compaction,
794 size_t total_live_words,
795 MutableSpace* const old_space,
796 HeapWord* full_region_prefix_end) {
797
798 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
799
800 // Check System.GC
801 bool is_max_on_system_gc = UseMaximumCompactionOnSystemGC
802 && GCCause::is_user_requested_gc(heap->gc_cause());
803
804 // Check if all live objs are too much for old-gen.
805 const bool is_old_gen_too_full = (total_live_words >= old_space->capacity_in_words());
806
807 // If all regions in old-gen are full
808 const bool is_region_full =
809 full_region_prefix_end >= _summary_data.region_align_down(old_space->top());
810
811 return should_do_max_compaction
812 || is_max_on_system_gc
813 || is_old_gen_too_full
814 || is_region_full;
815 }
816
817 void PSParallelCompact::summary_phase(bool should_do_max_compaction)
818 {
819 GCTraceTime(Info, gc, phases) tm("Summary Phase", &_gc_timer);
820
821 MutableSpace* const old_space = _space_info[old_space_id].space();
822 {
823 size_t total_live_words = 0;
824 HeapWord* full_region_prefix_end = nullptr;
825 {
826 // old-gen
827 size_t live_words = _summary_data.live_words_in_space(old_space,
828 &full_region_prefix_end);
829 total_live_words += live_words;
830 }
831 // young-gen
832 for (uint i = eden_space_id; i < last_space_id; ++i) {
833 const MutableSpace* space = _space_info[i].space();
834 size_t live_words = _summary_data.live_words_in_space(space);
835 total_live_words += live_words;
836 _space_info[i].set_new_top(space->bottom() + live_words);
837 _space_info[i].set_dense_prefix(space->bottom());
838 }
839
840 should_do_max_compaction = check_maximum_compaction(should_do_max_compaction,
841 total_live_words,
842 old_space,
843 full_region_prefix_end);
844 {
845 GCTraceTime(Info, gc, phases) tm("Summary Phase: expand", &_gc_timer);
846 // Try to expand old-gen in order to fit all live objs and waste.
847 size_t target_capacity_bytes = total_live_words * HeapWordSize
848 + old_space->capacity_in_bytes() * (MarkSweepDeadRatio / 100);
849 ParallelScavengeHeap::heap()->old_gen()->try_expand_till_size(target_capacity_bytes);
850 }
851
852 HeapWord* dense_prefix_end = should_do_max_compaction
853 ? full_region_prefix_end
854 : compute_dense_prefix_for_old_space(old_space,
855 full_region_prefix_end);
856 SpaceId id = old_space_id;
857 _space_info[id].set_dense_prefix(dense_prefix_end);
858
859 if (dense_prefix_end != old_space->bottom()) {
860 fill_dense_prefix_end(id);
861 }
862
863 // Compacting objs in [dense_prefix_end, old_space->top())
864 _summary_data.summarize(_space_info[id].split_info(),
865 dense_prefix_end, old_space->top(), nullptr,
866 dense_prefix_end, old_space->end(),
867 _space_info[id].new_top_addr());
868 }
869
870 // Summarize the remaining spaces in the young gen. The initial target space
871 // is the old gen. If a space does not fit entirely into the target, then the
872 // remainder is compacted into the space itself and that space becomes the new
873 // target.
874 SpaceId dst_space_id = old_space_id;
875 HeapWord* dst_space_end = old_space->end();
876 HeapWord** new_top_addr = _space_info[dst_space_id].new_top_addr();
877 for (unsigned int id = eden_space_id; id < last_space_id; ++id) {
878 const MutableSpace* space = _space_info[id].space();
879 const size_t live = pointer_delta(_space_info[id].new_top(),
880 space->bottom());
881 const size_t available = pointer_delta(dst_space_end, *new_top_addr);
882
883 if (live > 0 && live <= available) {
884 // All the live data will fit.
885 bool done = _summary_data.summarize(_space_info[id].split_info(),
886 space->bottom(), space->top(),
887 nullptr,
888 *new_top_addr, dst_space_end,
889 new_top_addr);
890 assert(done, "space must fit into old gen");
891
892 // Reset the new_top value for the space.
893 _space_info[id].set_new_top(space->bottom());
894 } else if (live > 0) {
895 // Attempt to fit part of the source space into the target space.
896 HeapWord* next_src_addr = nullptr;
897 bool done = _summary_data.summarize(_space_info[id].split_info(),
898 space->bottom(), space->top(),
899 &next_src_addr,
900 *new_top_addr, dst_space_end,
901 new_top_addr);
902 assert(!done, "space should not fit into old gen");
903 assert(next_src_addr != nullptr, "sanity");
904
905 // The source space becomes the new target, so the remainder is compacted
906 // within the space itself.
907 dst_space_id = SpaceId(id);
908 dst_space_end = space->end();
909 new_top_addr = _space_info[id].new_top_addr();
910 done = _summary_data.summarize(_space_info[id].split_info(),
911 next_src_addr, space->top(),
912 nullptr,
913 space->bottom(), dst_space_end,
914 new_top_addr);
915 assert(done, "space must fit when compacted into itself");
916 assert(*new_top_addr <= space->top(), "usage should not grow");
917 }
918 }
919 }
920
921 void PSParallelCompact::report_object_count_after_gc() {
922 GCTraceTime(Debug, gc, phases) tm("Report Object Count", &_gc_timer);
923 // The heap is compacted, all objects are iterable. However there may be
924 // filler objects in the heap which we should ignore.
925 class SkipFillerObjectClosure : public BoolObjectClosure {
926 public:
927 bool do_object_b(oop obj) override { return !CollectedHeap::is_filler_object(obj); }
928 } cl;
929 _gc_tracer.report_object_count_after_gc(&cl, &ParallelScavengeHeap::heap()->workers());
930 }
931
932 bool PSParallelCompact::invoke(bool clear_all_soft_refs, bool should_do_max_compaction) {
933 assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
934 assert(Thread::current() == (Thread*)VMThread::vm_thread(),
935 "should be in vm thread");
936 assert(ref_processor() != nullptr, "Sanity");
937
938 SvcGCMarker sgcm(SvcGCMarker::FULL);
939 IsSTWGCActiveMark mark;
940
941 ParallelScavengeHeap* heap = ParallelScavengeHeap::heap();
942
943 GCIdMark gc_id_mark;
944 _gc_timer.register_gc_start();
945 _gc_tracer.report_gc_start(heap->gc_cause(), _gc_timer.gc_start());
946
947 GCCause::Cause gc_cause = heap->gc_cause();
948 PSOldGen* old_gen = heap->old_gen();
949 PSAdaptiveSizePolicy* size_policy = heap->size_policy();
950
951 // Make sure data structures are sane, make the heap parsable, and do other
952 // miscellaneous bookkeeping.
953 pre_compact();
954
955 const PreGenGCValues pre_gc_values = heap->get_pre_gc_values();
956
957 {
958 const uint active_workers =
959 WorkerPolicy::calc_active_workers(ParallelScavengeHeap::heap()->workers().max_workers(),
960 ParallelScavengeHeap::heap()->workers().active_workers(),
961 Threads::number_of_non_daemon_threads());
962 ParallelScavengeHeap::heap()->workers().set_active_workers(active_workers);
963
964 GCTraceCPUTime tcpu(&_gc_tracer);
965 GCTraceTime(Info, gc) tm("Pause Full", nullptr, gc_cause, true);
966
967 heap->pre_full_gc_dump(&_gc_timer);
968
969 TraceCollectorStats tcs(counters());
970 TraceMemoryManagerStats tms(heap->old_gc_manager(), gc_cause, "end of major GC");
971
972 if (log_is_enabled(Debug, gc, heap, exit)) {
973 accumulated_time()->start();
974 }
975
976 // Let the size policy know we're starting
977 size_policy->major_collection_begin();
978
979 #ifdef COMPILER2
980 DerivedPointerTable::clear();
981 #endif // COMPILER2
982
983 ref_processor()->start_discovery(clear_all_soft_refs);
984
985 marking_phase(&_gc_tracer);
986
987 summary_phase(should_do_max_compaction);
988
989 #ifdef COMPILER2
990 assert(DerivedPointerTable::is_active(), "Sanity");
991 DerivedPointerTable::set_active(false);
992 #endif // COMPILER2
993
994 forward_to_new_addr();
995
996 adjust_pointers();
997
998 compact();
999
1000 ParCompactionManager::_preserved_marks_set->restore(&ParallelScavengeHeap::heap()->workers());
1001
1002 ParCompactionManager::verify_all_region_stack_empty();
1003
1004 // Reset the mark bitmap, summary data, and do other bookkeeping. Must be
1005 // done before resizing.
1006 post_compact();
1007
1008 size_policy->major_collection_end();
1009
1010 size_policy->sample_old_gen_used_bytes(MAX2(pre_gc_values.old_gen_used(), old_gen->used_in_bytes()));
1011
1012 if (UseAdaptiveSizePolicy) {
1013 heap->resize_after_full_gc();
1014 }
1015
1016 heap->resize_all_tlabs();
1017
1018 // Resize the metaspace capacity after a collection
1019 MetaspaceGC::compute_new_size();
1020
1021 if (log_is_enabled(Debug, gc, heap, exit)) {
1022 accumulated_time()->stop();
1023 }
1024
1025 heap->print_heap_change(pre_gc_values);
1026
1027 report_object_count_after_gc();
1028
1029 // Track memory usage and detect low memory
1030 MemoryService::track_memory_usage();
1031 heap->update_counters();
1032
1033 heap->post_full_gc_dump(&_gc_timer);
1034
1035 size_policy->record_gc_pause_end_instant();
1036 }
1037
1038 heap->gc_epilogue(true);
1039
1040 if (VerifyAfterGC && heap->total_collections() >= VerifyGCStartAt) {
1041 Universe::verify("After GC");
1042 }
1043
1044 heap->print_after_gc();
1045 heap->trace_heap_after_gc(&_gc_tracer);
1046
1047 _gc_timer.register_gc_end();
1048
1049 _gc_tracer.report_dense_prefix(dense_prefix(old_space_id));
1050 _gc_tracer.report_gc_end(_gc_timer.gc_end(), _gc_timer.time_partitions());
1051
1052 return true;
1053 }
1054
1055 class PCAddThreadRootsMarkingTaskClosure : public ThreadClosure {
1056 ParCompactionManager* _cm;
1057
1058 public:
1059 PCAddThreadRootsMarkingTaskClosure(ParCompactionManager* cm) : _cm(cm) { }
1060 void do_thread(Thread* thread) {
1061 ResourceMark rm;
1062
1063 MarkingNMethodClosure mark_and_push_in_blobs(&_cm->_mark_and_push_closure);
1064
1065 thread->oops_do(&_cm->_mark_and_push_closure, &mark_and_push_in_blobs);
1066
1067 // Do the real work
1068 _cm->follow_marking_stacks();
1069 }
1070 };
1071
1072 void steal_marking_work(TaskTerminator& terminator, uint worker_id) {
1073 assert(ParallelScavengeHeap::heap()->is_stw_gc_active(), "called outside gc");
1074
1075 ParCompactionManager* cm =
1076 ParCompactionManager::gc_thread_compaction_manager(worker_id);
1077
1078 do {
1079 ScannerTask task;
1080 if (ParCompactionManager::steal(worker_id, task)) {
1081 cm->follow_contents(task, true);
1082 }
1083 cm->follow_marking_stacks();
1084 } while (!terminator.offer_termination());
1085 }
1086
1087 class MarkFromRootsTask : public WorkerTask {
1088 NMethodMarkingScope _nmethod_marking_scope;
1089 ThreadsClaimTokenScope _threads_claim_token_scope;
1090 OopStorageSetStrongParState<false /* concurrent */, false /* is_const */> _oop_storage_set_par_state;
1091 TaskTerminator _terminator;
1092 uint _active_workers;
1093
1094 public:
1095 MarkFromRootsTask(uint active_workers) :
1096 WorkerTask("MarkFromRootsTask"),
1097 _nmethod_marking_scope(),
1098 _threads_claim_token_scope(),
1099 _terminator(active_workers, ParCompactionManager::marking_stacks()),
1100 _active_workers(active_workers) {}
1101
1102 virtual void work(uint worker_id) {
1103 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1104 cm->create_marking_stats_cache();
1105 {
1106 CLDToOopClosure cld_closure(&cm->_mark_and_push_closure, ClassLoaderData::_claim_stw_fullgc_mark);
1107 ClassLoaderDataGraph::always_strong_cld_do(&cld_closure);
1108
1109 // Do the real work
1110 cm->follow_marking_stacks();
1111 }
1112
1113 {
1114 PCAddThreadRootsMarkingTaskClosure closure(cm);
1115 Threads::possibly_parallel_threads_do(_active_workers > 1 /* is_par */, &closure);
1116 }
1117
1118 // Mark from OopStorages
1119 {
1120 _oop_storage_set_par_state.oops_do(&cm->_mark_and_push_closure);
1121 // Do the real work
1122 cm->follow_marking_stacks();
1123 }
1124
1125 if (_active_workers > 1) {
1126 steal_marking_work(_terminator, worker_id);
1127 }
1128 }
1129 };
1130
1131 class ParallelCompactRefProcProxyTask : public RefProcProxyTask {
1132 TaskTerminator _terminator;
1133
1134 public:
1135 ParallelCompactRefProcProxyTask(uint max_workers)
1136 : RefProcProxyTask("ParallelCompactRefProcProxyTask", max_workers),
1137 _terminator(_max_workers, ParCompactionManager::marking_stacks()) {}
1138
1139 void work(uint worker_id) override {
1140 assert(worker_id < _max_workers, "sanity");
1141 ParCompactionManager* cm = (_tm == RefProcThreadModel::Single) ? ParCompactionManager::get_vmthread_cm() : ParCompactionManager::gc_thread_compaction_manager(worker_id);
1142 BarrierEnqueueDiscoveredFieldClosure enqueue;
1143 ParCompactionManager::FollowStackClosure complete_gc(cm, (_tm == RefProcThreadModel::Single) ? nullptr : &_terminator, worker_id);
1144 _rp_task->rp_work(worker_id, PSParallelCompact::is_alive_closure(), &cm->_mark_and_push_closure, &enqueue, &complete_gc);
1145 }
1146
1147 void prepare_run_task_hook() override {
1148 _terminator.reset_for_reuse(_queue_count);
1149 }
1150 };
1151
1152 static void flush_marking_stats_cache(const uint num_workers) {
1153 for (uint i = 0; i < num_workers; ++i) {
1154 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(i);
1155 cm->flush_and_destroy_marking_stats_cache();
1156 }
1157 }
1158
1159 class PSParallelCleaningTask : public WorkerTask {
1160 bool _unloading_occurred;
1161 CodeCacheUnloadingTask _code_cache_task;
1162 // Prune dead klasses from subklass/sibling/implementor lists.
1163 KlassCleaningTask _klass_cleaning_task;
1164
1165 public:
1166 PSParallelCleaningTask(bool unloading_occurred) :
1167 WorkerTask("PS Parallel Cleaning"),
1168 _unloading_occurred(unloading_occurred),
1169 _code_cache_task(unloading_occurred),
1170 _klass_cleaning_task() {}
1171
1172 void work(uint worker_id) {
1173 // Do first pass of code cache cleaning.
1174 _code_cache_task.work(worker_id);
1175
1176 // Clean all klasses that were not unloaded.
1177 // The weak metadata in klass doesn't need to be
1178 // processed if there was no unloading.
1179 if (_unloading_occurred) {
1180 _klass_cleaning_task.work();
1181 }
1182 }
1183 };
1184
1185 void PSParallelCompact::marking_phase(ParallelOldTracer *gc_tracer) {
1186 // Recursively traverse all live objects and mark them
1187 GCTraceTime(Info, gc, phases) tm("Marking Phase", &_gc_timer);
1188
1189 uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers();
1190
1191 ClassLoaderDataGraph::verify_claimed_marks_cleared(ClassLoaderData::_claim_stw_fullgc_mark);
1192 {
1193 GCTraceTime(Debug, gc, phases) tm("Par Mark", &_gc_timer);
1194
1195 MarkFromRootsTask task(active_gc_threads);
1196 ParallelScavengeHeap::heap()->workers().run_task(&task);
1197 }
1198
1199 // Process reference objects found during marking
1200 {
1201 GCTraceTime(Debug, gc, phases) tm("Reference Processing", &_gc_timer);
1202
1203 ReferenceProcessorStats stats;
1204 ReferenceProcessorPhaseTimes pt(&_gc_timer, ref_processor()->max_num_queues());
1205
1206 ParallelCompactRefProcProxyTask task(ref_processor()->max_num_queues());
1207 stats = ref_processor()->process_discovered_references(task, &ParallelScavengeHeap::heap()->workers(), pt);
1208
1209 gc_tracer->report_gc_reference_stats(stats);
1210 pt.print_all_references();
1211 }
1212
1213 {
1214 GCTraceTime(Debug, gc, phases) tm("Flush Marking Stats", &_gc_timer);
1215
1216 flush_marking_stats_cache(active_gc_threads);
1217 }
1218
1219 // This is the point where the entire marking should have completed.
1220 ParCompactionManager::verify_all_marking_stack_empty();
1221
1222 {
1223 GCTraceTime(Debug, gc, phases) tm("Weak Processing", &_gc_timer);
1224 WeakProcessor::weak_oops_do(&ParallelScavengeHeap::heap()->workers(),
1225 is_alive_closure(),
1226 &do_nothing_cl,
1227 1);
1228 }
1229
1230 {
1231 GCTraceTime(Debug, gc, phases) tm_m("Class Unloading", &_gc_timer);
1232
1233 ClassUnloadingContext ctx(active_gc_threads /* num_nmethod_unlink_workers */,
1234 false /* unregister_nmethods_during_purge */,
1235 false /* lock_nmethod_free_separately */);
1236
1237 {
1238 CodeCache::UnlinkingScope scope(is_alive_closure());
1239
1240 // Follow system dictionary roots and unload classes.
1241 bool unloading_occurred = SystemDictionary::do_unloading(&_gc_timer);
1242
1243 PSParallelCleaningTask task{unloading_occurred};
1244 ParallelScavengeHeap::heap()->workers().run_task(&task);
1245 }
1246
1247 {
1248 GCTraceTime(Debug, gc, phases) t("Purge Unlinked NMethods", gc_timer());
1249 // Release unloaded nmethod's memory.
1250 ctx.purge_nmethods();
1251 }
1252 {
1253 GCTraceTime(Debug, gc, phases) ur("Unregister NMethods", &_gc_timer);
1254 ParallelScavengeHeap::heap()->prune_unlinked_nmethods();
1255 }
1256 {
1257 GCTraceTime(Debug, gc, phases) t("Free Code Blobs", gc_timer());
1258 ctx.free_nmethods();
1259 }
1260 {
1261 // Delete metaspaces for unloaded class loaders and clean up loader_data graph
1262 GCTraceTime(Debug, gc, phases) t("Purge Class Loader Data", gc_timer());
1263 ClassLoaderDataGraph::purge(true /* at_safepoint */);
1264 DEBUG_ONLY(MetaspaceUtils::verify();)
1265 }
1266 }
1267
1268 #if TASKQUEUE_STATS
1269 ParCompactionManager::print_and_reset_taskqueue_stats();
1270 #endif
1271 }
1272
1273 template<typename Func>
1274 void PSParallelCompact::adjust_in_space_helper(SpaceId id, Atomic<uint>* claim_counter, Func&& on_stripe) {
1275 MutableSpace* sp = PSParallelCompact::space(id);
1276 HeapWord* const bottom = sp->bottom();
1277 HeapWord* const top = sp->top();
1278 if (bottom == top) {
1279 return;
1280 }
1281
1282 const uint num_regions_per_stripe = 2;
1283 const size_t region_size = ParallelCompactData::RegionSize;
1284 const size_t stripe_size = num_regions_per_stripe * region_size;
1285
1286 while (true) {
1287 uint counter = claim_counter->fetch_then_add(num_regions_per_stripe);
1288 HeapWord* cur_stripe = bottom + counter * region_size;
1289 if (cur_stripe >= top) {
1290 break;
1291 }
1292 HeapWord* stripe_end = MIN2(cur_stripe + stripe_size, top);
1293 on_stripe(cur_stripe, stripe_end);
1294 }
1295 }
1296
1297 void PSParallelCompact::adjust_in_old_space(Atomic<uint>* claim_counter) {
1298 // Regions in old-space shouldn't be split.
1299 assert(!_space_info[old_space_id].split_info().is_valid(), "inv");
1300
1301 auto scan_obj_with_limit = [&] (HeapWord* obj_start, HeapWord* left, HeapWord* right) {
1302 assert(mark_bitmap()->is_marked(obj_start), "inv");
1303 oop obj = cast_to_oop(obj_start);
1304 return obj->oop_iterate_size(&pc_adjust_pointer_closure, MemRegion(left, right));
1305 };
1306
1307 adjust_in_space_helper(old_space_id, claim_counter, [&] (HeapWord* stripe_start, HeapWord* stripe_end) {
1308 assert(_summary_data.is_region_aligned(stripe_start), "inv");
1309 RegionData* cur_region = _summary_data.addr_to_region_ptr(stripe_start);
1310 HeapWord* obj_start;
1311 if (cur_region->partial_obj_size() != 0) {
1312 obj_start = cur_region->partial_obj_addr();
1313 obj_start += scan_obj_with_limit(obj_start, stripe_start, stripe_end);
1314 } else {
1315 obj_start = stripe_start;
1316 }
1317
1318 while (obj_start < stripe_end) {
1319 obj_start = mark_bitmap()->find_obj_beg(obj_start, stripe_end);
1320 if (obj_start >= stripe_end) {
1321 break;
1322 }
1323 obj_start += scan_obj_with_limit(obj_start, stripe_start, stripe_end);
1324 }
1325 });
1326 }
1327
1328 void PSParallelCompact::adjust_in_young_space(SpaceId id, Atomic<uint>* claim_counter) {
1329 adjust_in_space_helper(id, claim_counter, [](HeapWord* stripe_start, HeapWord* stripe_end) {
1330 HeapWord* obj_start = stripe_start;
1331 while (obj_start < stripe_end) {
1332 obj_start = mark_bitmap()->find_obj_beg(obj_start, stripe_end);
1333 if (obj_start >= stripe_end) {
1334 break;
1335 }
1336 oop obj = cast_to_oop(obj_start);
1337 obj_start += obj->oop_iterate_size(&pc_adjust_pointer_closure);
1338 }
1339 });
1340 }
1341
1342 void PSParallelCompact::adjust_pointers_in_spaces(uint worker_id, Atomic<uint>* claim_counters) {
1343 auto start_time = Ticks::now();
1344 adjust_in_old_space(&claim_counters[0]);
1345 for (uint id = eden_space_id; id < last_space_id; ++id) {
1346 adjust_in_young_space(SpaceId(id), &claim_counters[id]);
1347 }
1348 log_trace(gc, phases)("adjust_pointers_in_spaces worker %u: %.3f ms", worker_id, (Ticks::now() - start_time).seconds() * 1000);
1349 }
1350
1351 class PSAdjustTask final : public WorkerTask {
1352 ThreadsClaimTokenScope _threads_claim_token_scope;
1353 WeakProcessor::Task _weak_proc_task;
1354 OopStorageSetStrongParState<false, false> _oop_storage_iter;
1355 uint _nworkers;
1356 Atomic<bool> _code_cache_claimed;
1357 Atomic<uint> _claim_counters[PSParallelCompact::last_space_id];
1358
1359 bool try_claim_code_cache_task() {
1360 return _code_cache_claimed.load_relaxed() == false
1361 && _code_cache_claimed.compare_set(false, true);
1362 }
1363
1364 public:
1365 PSAdjustTask(uint nworkers) :
1366 WorkerTask("PSAdjust task"),
1367 _threads_claim_token_scope(),
1368 _weak_proc_task(nworkers),
1369 _oop_storage_iter(),
1370 _nworkers(nworkers),
1371 _code_cache_claimed(false),
1372 _claim_counters{} {
1373
1374 ClassLoaderDataGraph::verify_claimed_marks_cleared(ClassLoaderData::_claim_stw_fullgc_adjust);
1375 }
1376
1377 void work(uint worker_id) {
1378 {
1379 // Pointers in heap.
1380 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1381 cm->preserved_marks()->adjust_during_full_gc();
1382
1383 PSParallelCompact::adjust_pointers_in_spaces(worker_id, _claim_counters);
1384 }
1385
1386 {
1387 // All (strong and weak) CLDs.
1388 CLDToOopClosure cld_closure(&pc_adjust_pointer_closure, ClassLoaderData::_claim_stw_fullgc_adjust);
1389 ClassLoaderDataGraph::cld_do(&cld_closure);
1390 }
1391
1392 {
1393 // Threads stack frames. No need to visit on-stack nmethods, because all
1394 // nmethods are visited in one go via CodeCache::nmethods_do.
1395 ResourceMark rm;
1396 Threads::possibly_parallel_oops_do(_nworkers > 1, &pc_adjust_pointer_closure, nullptr);
1397 if (try_claim_code_cache_task()) {
1398 NMethodToOopClosure adjust_code(&pc_adjust_pointer_closure, NMethodToOopClosure::FixRelocations);
1399 CodeCache::nmethods_do(&adjust_code);
1400 }
1401 }
1402
1403 {
1404 // VM internal strong and weak roots.
1405 _oop_storage_iter.oops_do(&pc_adjust_pointer_closure);
1406 AlwaysTrueClosure always_alive;
1407 _weak_proc_task.work(worker_id, &always_alive, &pc_adjust_pointer_closure);
1408 }
1409 }
1410 };
1411
1412 void PSParallelCompact::adjust_pointers() {
1413 // Adjust the pointers to reflect the new locations
1414 GCTraceTime(Info, gc, phases) tm("Adjust Pointers", &_gc_timer);
1415 uint nworkers = ParallelScavengeHeap::heap()->workers().active_workers();
1416 PSAdjustTask task(nworkers);
1417 ParallelScavengeHeap::heap()->workers().run_task(&task);
1418 }
1419
1420 // Split [start, end) evenly for a number of workers and return the
1421 // range for worker_id.
1422 static void split_regions_for_worker(size_t start, size_t end,
1423 uint worker_id, uint num_workers,
1424 size_t* worker_start, size_t* worker_end) {
1425 assert(start < end, "precondition");
1426 assert(num_workers > 0, "precondition");
1427 assert(worker_id < num_workers, "precondition");
1428
1429 size_t num_regions = end - start;
1430 size_t num_regions_per_worker = num_regions / num_workers;
1431 size_t remainder = num_regions % num_workers;
1432 // The first few workers will get one extra.
1433 *worker_start = start + worker_id * num_regions_per_worker
1434 + MIN2(checked_cast<size_t>(worker_id), remainder);
1435 *worker_end = *worker_start + num_regions_per_worker
1436 + (worker_id < remainder ? 1 : 0);
1437 }
1438
1439 static bool safe_to_read_header(size_t words) {
1440 precond(words > 0);
1441
1442 // Safe to read if we have enough words for the full header, i.e., both
1443 // markWord and Klass pointer.
1444 const bool safe = words >= (size_t)oopDesc::header_size();
1445
1446 // If using Compact Object Headers, the full header is inside the markWord,
1447 // so will always be safe to read
1448 assert(!UseCompactObjectHeaders || safe, "Compact Object Headers should always be safe");
1449
1450 return safe;
1451 }
1452
1453 void PSParallelCompact::forward_to_new_addr() {
1454 GCTraceTime(Info, gc, phases) tm("Forward", &_gc_timer);
1455 uint nworkers = ParallelScavengeHeap::heap()->workers().active_workers();
1456
1457 struct ForwardTask final : public WorkerTask {
1458 uint _num_workers;
1459
1460 explicit ForwardTask(uint num_workers) :
1461 WorkerTask("PSForward task"),
1462 _num_workers(num_workers) {}
1463
1464 static bool should_preserve_mark(oop obj, HeapWord* end_addr) {
1465 size_t remaining_words = pointer_delta(end_addr, cast_from_oop<HeapWord*>(obj));
1466
1467 if (Arguments::is_valhalla_enabled() && !safe_to_read_header(remaining_words)) {
1468 // When using Valhalla, it might be necessary to preserve the Valhalla-
1469 // specific bits in the markWord. If the entire object header is
1470 // copied, the correct markWord (with the appropriate Valhalla bits)
1471 // can be safely read from the Klass. However, if the full header is
1472 // not copied, we cannot safely read the Klass to obtain this information.
1473 // In such cases, we always preserve the markWord to ensure that all
1474 // relevant bits, including Valhalla-specific ones, are retained.
1475 return true;
1476 } else {
1477 return obj->mark().must_be_preserved();
1478 }
1479 }
1480
1481 static void forward_objs_in_range(ParCompactionManager* cm,
1482 HeapWord* start,
1483 HeapWord* end,
1484 HeapWord* destination) {
1485 HeapWord* cur_addr = start;
1486 HeapWord* new_addr = destination;
1487
1488 while (cur_addr < end) {
1489 cur_addr = mark_bitmap()->find_obj_beg(cur_addr, end);
1490 if (cur_addr >= end) {
1491 return;
1492 }
1493 assert(mark_bitmap()->is_marked(cur_addr), "inv");
1494 oop obj = cast_to_oop(cur_addr);
1495
1496 if (new_addr != cur_addr) {
1497 if (should_preserve_mark(obj, end)) {
1498 cm->preserved_marks()->push_always(obj, obj->mark());
1499 }
1500
1501 FullGCForwarding::forward_to(obj, cast_to_oop(new_addr));
1502 }
1503 size_t obj_size = obj->size();
1504 new_addr += obj_size;
1505 cur_addr += obj_size;
1506 }
1507 }
1508
1509 void work(uint worker_id) override {
1510 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1511 for (uint id = old_space_id; id < last_space_id; ++id) {
1512 MutableSpace* sp = PSParallelCompact::space(SpaceId(id));
1513 HeapWord* dense_prefix_addr = dense_prefix(SpaceId(id));
1514 HeapWord* top = sp->top();
1515
1516 if (dense_prefix_addr == top) {
1517 // Empty space
1518 continue;
1519 }
1520
1521 const SplitInfo& split_info = _space_info[SpaceId(id)].split_info();
1522 size_t dense_prefix_region = _summary_data.addr_to_region_idx(dense_prefix_addr);
1523 size_t top_region = _summary_data.addr_to_region_idx(_summary_data.region_align_up(top));
1524 size_t start_region;
1525 size_t end_region;
1526 split_regions_for_worker(dense_prefix_region, top_region,
1527 worker_id, _num_workers,
1528 &start_region, &end_region);
1529 for (size_t cur_region = start_region; cur_region < end_region; ++cur_region) {
1530 RegionData* region_ptr = _summary_data.region(cur_region);
1531 size_t partial_obj_size = region_ptr->partial_obj_size();
1532
1533 if (partial_obj_size == ParallelCompactData::RegionSize) {
1534 // No obj-start
1535 continue;
1536 }
1537
1538 HeapWord* region_start = _summary_data.region_to_addr(cur_region);
1539 HeapWord* region_end = region_start + ParallelCompactData::RegionSize;
1540
1541 if (split_info.is_split(cur_region)) {
1542 // Part 1: will be relocated to space-1
1543 HeapWord* preceding_destination = split_info.preceding_destination();
1544 HeapWord* split_point = split_info.split_point();
1545 forward_objs_in_range(cm, region_start + partial_obj_size, split_point, preceding_destination + partial_obj_size);
1546
1547 // Part 2: will be relocated to space-2
1548 HeapWord* destination = region_ptr->destination();
1549 forward_objs_in_range(cm, split_point, region_end, destination);
1550 } else {
1551 HeapWord* destination = region_ptr->destination();
1552 forward_objs_in_range(cm, region_start + partial_obj_size, region_end, destination + partial_obj_size);
1553 }
1554 }
1555 }
1556 }
1557 } task(nworkers);
1558
1559 ParallelScavengeHeap::heap()->workers().run_task(&task);
1560 DEBUG_ONLY(verify_forward();)
1561 }
1562
1563 #ifdef ASSERT
1564 void PSParallelCompact::verify_forward() {
1565 HeapWord* const old_dense_prefix_addr = dense_prefix(SpaceId(old_space_id));
1566 // The destination addr for the first live obj after dense-prefix.
1567 HeapWord* bump_ptr = old_dense_prefix_addr
1568 + _summary_data.addr_to_region_ptr(old_dense_prefix_addr)->partial_obj_size();
1569 SpaceId bump_ptr_space = old_space_id;
1570
1571 for (uint id = old_space_id; id < last_space_id; ++id) {
1572 MutableSpace* sp = PSParallelCompact::space(SpaceId(id));
1573 // Only verify objs after dense-prefix, because those before dense-prefix are not moved (forwarded).
1574 HeapWord* cur_addr = dense_prefix(SpaceId(id));
1575 HeapWord* top = sp->top();
1576
1577 while (cur_addr < top) {
1578 cur_addr = mark_bitmap()->find_obj_beg(cur_addr, top);
1579 if (cur_addr >= top) {
1580 break;
1581 }
1582 assert(mark_bitmap()->is_marked(cur_addr), "inv");
1583 assert(bump_ptr <= _space_info[bump_ptr_space].new_top(), "inv");
1584 // Move to the space containing cur_addr
1585 if (bump_ptr == _space_info[bump_ptr_space].new_top()) {
1586 bump_ptr = space(space_id(cur_addr))->bottom();
1587 bump_ptr_space = space_id(bump_ptr);
1588 }
1589 oop obj = cast_to_oop(cur_addr);
1590 if (cur_addr == bump_ptr) {
1591 assert(!FullGCForwarding::is_forwarded(obj), "inv");
1592 } else {
1593 assert(FullGCForwarding::forwardee(obj) == cast_to_oop(bump_ptr), "inv");
1594 }
1595 bump_ptr += obj->size();
1596 cur_addr += obj->size();
1597 }
1598 }
1599 }
1600 #endif
1601
1602 // Helper class to print 8 region numbers per line and then print the total at the end.
1603 class FillableRegionLogger : public StackObj {
1604 private:
1605 Log(gc, compaction) log;
1606 static const int LineLength = 8;
1607 size_t _regions[LineLength];
1608 int _next_index;
1609 bool _enabled;
1610 size_t _total_regions;
1611 public:
1612 FillableRegionLogger() : _next_index(0), _enabled(log_develop_is_enabled(Trace, gc, compaction)), _total_regions(0) { }
1613 ~FillableRegionLogger() {
1614 log.trace("%zu initially fillable regions", _total_regions);
1615 }
1616
1617 void print_line() {
1618 if (!_enabled || _next_index == 0) {
1619 return;
1620 }
1621 FormatBuffer<> line("Fillable: ");
1622 for (int i = 0; i < _next_index; i++) {
1623 line.append(" %7zu", _regions[i]);
1624 }
1625 log.trace("%s", line.buffer());
1626 _next_index = 0;
1627 }
1628
1629 void handle(size_t region) {
1630 if (!_enabled) {
1631 return;
1632 }
1633 _regions[_next_index++] = region;
1634 if (_next_index == LineLength) {
1635 print_line();
1636 }
1637 _total_regions++;
1638 }
1639 };
1640
1641 void PSParallelCompact::prepare_region_draining_tasks(uint parallel_gc_threads)
1642 {
1643 GCTraceTime(Trace, gc, phases) tm("Drain Task Setup", &_gc_timer);
1644
1645 // Find the threads that are active
1646 uint worker_id = 0;
1647
1648 // Find all regions that are available (can be filled immediately) and
1649 // distribute them to the thread stacks. The iteration is done in reverse
1650 // order (high to low) so the regions will be removed in ascending order.
1651
1652 const ParallelCompactData& sd = PSParallelCompact::summary_data();
1653
1654 // id + 1 is used to test termination so unsigned can
1655 // be used with an old_space_id == 0.
1656 FillableRegionLogger region_logger;
1657 for (unsigned int id = last_space_id - 1; id + 1 > old_space_id; --id) {
1658 SpaceInfo* const space_info = _space_info + id;
1659 HeapWord* const new_top = space_info->new_top();
1660
1661 const size_t beg_region = sd.addr_to_region_idx(space_info->dense_prefix());
1662 const size_t end_region =
1663 sd.addr_to_region_idx(sd.region_align_up(new_top));
1664
1665 for (size_t cur = end_region - 1; cur + 1 > beg_region; --cur) {
1666 if (sd.region(cur)->claim_unsafe()) {
1667 ParCompactionManager* cm = ParCompactionManager::gc_thread_compaction_manager(worker_id);
1668 bool result = sd.region(cur)->mark_normal();
1669 assert(result, "Must succeed at this point.");
1670 cm->region_stack()->push(cur);
1671 region_logger.handle(cur);
1672 // Assign regions to tasks in round-robin fashion.
1673 if (++worker_id == parallel_gc_threads) {
1674 worker_id = 0;
1675 }
1676 }
1677 }
1678 region_logger.print_line();
1679 }
1680 }
1681
1682 static void compaction_with_stealing_work(TaskTerminator* terminator, uint worker_id) {
1683 assert(ParallelScavengeHeap::heap()->is_stw_gc_active(), "called outside gc");
1684
1685 ParCompactionManager* cm =
1686 ParCompactionManager::gc_thread_compaction_manager(worker_id);
1687
1688 // Drain the stacks that have been preloaded with regions
1689 // that are ready to fill.
1690
1691 cm->drain_region_stacks();
1692
1693 guarantee(cm->region_stack()->is_empty(), "Not empty");
1694
1695 size_t region_index = 0;
1696
1697 while (true) {
1698 if (ParCompactionManager::steal(worker_id, region_index)) {
1699 PSParallelCompact::fill_and_update_region(cm, region_index);
1700 cm->drain_region_stacks();
1701 } else if (PSParallelCompact::steal_unavailable_region(cm, region_index)) {
1702 // Fill and update an unavailable region with the help of a shadow region
1703 PSParallelCompact::fill_and_update_shadow_region(cm, region_index);
1704 cm->drain_region_stacks();
1705 } else {
1706 if (terminator->offer_termination()) {
1707 break;
1708 }
1709 // Go around again.
1710 }
1711 }
1712 }
1713
1714 class FillDensePrefixAndCompactionTask: public WorkerTask {
1715 TaskTerminator _terminator;
1716
1717 public:
1718 FillDensePrefixAndCompactionTask(uint active_workers) :
1719 WorkerTask("FillDensePrefixAndCompactionTask"),
1720 _terminator(active_workers, ParCompactionManager::region_task_queues()) {
1721 }
1722
1723 virtual void work(uint worker_id) {
1724 if (worker_id == 0) {
1725 auto start = Ticks::now();
1726 PSParallelCompact::fill_dead_objs_in_dense_prefix();
1727 log_trace(gc, phases)("Fill dense prefix by worker 0: %.3f ms", (Ticks::now() - start).seconds() * 1000);
1728 }
1729 compaction_with_stealing_work(&_terminator, worker_id);
1730 }
1731 };
1732
1733 void PSParallelCompact::fill_range_in_dense_prefix(HeapWord* start, HeapWord* end) {
1734 #ifdef ASSERT
1735 {
1736 assert(start < end, "precondition");
1737 assert(mark_bitmap()->find_obj_beg(start, end) == end, "precondition");
1738 HeapWord* bottom = _space_info[old_space_id].space()->bottom();
1739 if (start != bottom) {
1740 // The preceding live obj.
1741 HeapWord* obj_start = mark_bitmap()->find_obj_beg_reverse(bottom, start);
1742 HeapWord* obj_end = obj_start + cast_to_oop(obj_start)->size();
1743 assert(obj_end == start, "precondition");
1744 }
1745 }
1746 #endif
1747
1748 CollectedHeap::fill_with_objects(start, pointer_delta(end, start));
1749 HeapWord* addr = start;
1750 do {
1751 size_t size = cast_to_oop(addr)->size();
1752 start_array(old_space_id)->update_for_block(addr, addr + size);
1753 addr += size;
1754 } while (addr < end);
1755 }
1756
1757 void PSParallelCompact::fill_dead_objs_in_dense_prefix() {
1758 ParMarkBitMap* bitmap = mark_bitmap();
1759
1760 HeapWord* const bottom = _space_info[old_space_id].space()->bottom();
1761 HeapWord* const prefix_end = dense_prefix(old_space_id);
1762
1763 const size_t region_size = ParallelCompactData::RegionSize;
1764
1765 // Fill dead space in [start_addr, end_addr)
1766 HeapWord* const start_addr = bottom;
1767 HeapWord* const end_addr = prefix_end;
1768
1769 for (HeapWord* cur_addr = start_addr; cur_addr < end_addr; /* empty */) {
1770 RegionData* cur_region_ptr = _summary_data.addr_to_region_ptr(cur_addr);
1771 if (cur_region_ptr->data_size() == region_size) {
1772 // Full; no dead space. Next region.
1773 if (_summary_data.is_region_aligned(cur_addr)) {
1774 cur_addr += region_size;
1775 } else {
1776 cur_addr = _summary_data.region_align_up(cur_addr);
1777 }
1778 continue;
1779 }
1780
1781 // Fill dead space inside cur_region.
1782 if (_summary_data.is_region_aligned(cur_addr)) {
1783 cur_addr += cur_region_ptr->partial_obj_size();
1784 }
1785
1786 HeapWord* region_end_addr = _summary_data.region_align_up(cur_addr + 1);
1787 assert(region_end_addr <= end_addr, "inv");
1788 while (cur_addr < region_end_addr) {
1789 // Use end_addr to allow filler-obj to cross region boundary.
1790 HeapWord* live_start = bitmap->find_obj_beg(cur_addr, end_addr);
1791 if (cur_addr != live_start) {
1792 // Found dead space [cur_addr, live_start).
1793 fill_range_in_dense_prefix(cur_addr, live_start);
1794 }
1795 if (live_start >= region_end_addr) {
1796 cur_addr = live_start;
1797 break;
1798 }
1799 assert(bitmap->is_marked(live_start), "inv");
1800 cur_addr = live_start + cast_to_oop(live_start)->size();
1801 }
1802 }
1803 }
1804
1805 void PSParallelCompact::compact() {
1806 GCTraceTime(Info, gc, phases) tm("Compaction Phase", &_gc_timer);
1807
1808 uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers();
1809
1810 initialize_shadow_regions(active_gc_threads);
1811 prepare_region_draining_tasks(active_gc_threads);
1812
1813 {
1814 GCTraceTime(Trace, gc, phases) tm("Par Compact", &_gc_timer);
1815
1816 FillDensePrefixAndCompactionTask task(active_gc_threads);
1817 ParallelScavengeHeap::heap()->workers().run_task(&task);
1818
1819 #ifdef ASSERT
1820 verify_filler_in_dense_prefix();
1821
1822 // Verify that all regions have been processed.
1823 for (unsigned int id = old_space_id; id < last_space_id; ++id) {
1824 verify_complete(SpaceId(id));
1825 }
1826 #endif
1827 }
1828 }
1829
1830 #ifdef ASSERT
1831 void PSParallelCompact::verify_filler_in_dense_prefix() {
1832 HeapWord* bottom = _space_info[old_space_id].space()->bottom();
1833 HeapWord* dense_prefix_end = dense_prefix(old_space_id);
1834
1835 const size_t region_size = ParallelCompactData::RegionSize;
1836
1837 for (HeapWord* cur_addr = bottom; cur_addr < dense_prefix_end; /* empty */) {
1838 RegionData* cur_region_ptr = _summary_data.addr_to_region_ptr(cur_addr);
1839 if (cur_region_ptr->data_size() == region_size) {
1840 // Full; no dead space. Next region.
1841 if (_summary_data.is_region_aligned(cur_addr)) {
1842 cur_addr += region_size;
1843 } else {
1844 cur_addr = _summary_data.region_align_up(cur_addr);
1845 }
1846 continue;
1847 }
1848
1849 // This region contains filler objs.
1850 if (_summary_data.is_region_aligned(cur_addr)) {
1851 cur_addr += cur_region_ptr->partial_obj_size();
1852 }
1853
1854 HeapWord* region_end_addr = _summary_data.region_align_up(cur_addr + 1);
1855 assert(region_end_addr <= dense_prefix_end, "inv");
1856
1857 while (cur_addr < region_end_addr) {
1858 oop obj = cast_to_oop(cur_addr);
1859 oopDesc::verify(obj);
1860 if (!mark_bitmap()->is_marked(cur_addr)) {
1861 assert(CollectedHeap::is_filler_object(cast_to_oop(cur_addr)), "inv");
1862 }
1863 cur_addr += obj->size();
1864 }
1865 }
1866 }
1867
1868 void PSParallelCompact::verify_complete(SpaceId space_id) {
1869 // All Regions served as compaction targets, from dense_prefix() to
1870 // new_top(), should be marked as filled and all Regions between new_top()
1871 // and top() should be available (i.e., should have been emptied).
1872 ParallelCompactData& sd = summary_data();
1873 SpaceInfo si = _space_info[space_id];
1874 HeapWord* new_top_addr = sd.region_align_up(si.new_top());
1875 HeapWord* old_top_addr = sd.region_align_up(si.space()->top());
1876 const size_t beg_region = sd.addr_to_region_idx(si.dense_prefix());
1877 const size_t new_top_region = sd.addr_to_region_idx(new_top_addr);
1878 const size_t old_top_region = sd.addr_to_region_idx(old_top_addr);
1879
1880 size_t cur_region;
1881 for (cur_region = beg_region; cur_region < new_top_region; ++cur_region) {
1882 const RegionData* const c = sd.region(cur_region);
1883 assert(c->completed(), "region %zu not filled: destination_count=%u",
1884 cur_region, c->destination_count());
1885 }
1886
1887 for (cur_region = new_top_region; cur_region < old_top_region; ++cur_region) {
1888 const RegionData* const c = sd.region(cur_region);
1889 assert(c->available(), "region %zu not empty: destination_count=%u",
1890 cur_region, c->destination_count());
1891 }
1892 }
1893 #endif // #ifdef ASSERT
1894
1895 // Return the SpaceId for the space containing addr. If addr is not in the
1896 // heap, last_space_id is returned. In debug mode it expects the address to be
1897 // in the heap and asserts such.
1898 PSParallelCompact::SpaceId PSParallelCompact::space_id(HeapWord* addr) {
1899 assert(ParallelScavengeHeap::heap()->is_in_reserved(addr), "addr not in the heap");
1900
1901 for (unsigned int id = old_space_id; id < last_space_id; ++id) {
1902 if (_space_info[id].space()->contains(addr)) {
1903 return SpaceId(id);
1904 }
1905 }
1906
1907 assert(false, "no space contains the addr");
1908 return last_space_id;
1909 }
1910
1911 // Skip over count live words starting from beg, and return the address of the
1912 // next live word. Callers must also ensure that there are enough live words in
1913 // the range [beg, end) to skip.
1914 HeapWord* PSParallelCompact::skip_live_words(HeapWord* beg, HeapWord* end, size_t count)
1915 {
1916 ParMarkBitMap* m = mark_bitmap();
1917 HeapWord* cur_addr = beg;
1918 while (true) {
1919 cur_addr = m->find_obj_beg(cur_addr, end);
1920 assert(cur_addr < end, "inv");
1921 size_t obj_size = cast_to_oop(cur_addr)->size();
1922 // Strictly greater-than
1923 if (obj_size > count) {
1924 return cur_addr + count;
1925 }
1926 count -= obj_size;
1927 cur_addr += obj_size;
1928 }
1929 }
1930
1931 // On starting to fill a destination region (dest-region), we need to know the
1932 // location of the word that will be at the start of the dest-region after
1933 // compaction. A dest-region can have one or more source regions, but only the
1934 // first source-region contains this location. This location is retrieved by
1935 // calling `first_src_addr` on a dest-region.
1936 // Conversely, a source-region has a dest-region which holds the destination of
1937 // the first live word on this source-region, based on which the destination
1938 // for the rest of live words can be derived.
1939 //
1940 // Note:
1941 // There is some complication due to space-boundary-fragmentation (an obj can't
1942 // cross space-boundary) -- a source-region may be split and behave like two
1943 // distinct regions with their own dest-region, as depicted below.
1944 //
1945 // source-region: region-n
1946 //
1947 // **********************
1948 // | A|A~~~~B|B |
1949 // **********************
1950 // n-1 n n+1
1951 //
1952 // AA, BB denote two live objs. ~~~~ denotes unknown number of live objs.
1953 //
1954 // Assuming the dest-region for region-n is the final region before
1955 // old-space-end and its first-live-word is the middle of AA, the heap content
1956 // will look like the following after compaction:
1957 //
1958 // ************** *************
1959 // A|A~~~~ | |BB |
1960 // ************** *************
1961 // ^ ^
1962 // | old-space-end | eden-space-start
1963 //
1964 // Therefore, in this example, region-n will have two dest-regions:
1965 // 1. the final region in old-space
1966 // 2. the first region in eden-space.
1967 // To handle this special case, we introduce the concept of split-region, whose
1968 // contents are relocated to two spaces. `SplitInfo` captures all necessary
1969 // info about the split, the first part, spliting-point, and the second part.
1970 HeapWord* PSParallelCompact::first_src_addr(HeapWord* const dest_addr,
1971 SpaceId src_space_id,
1972 size_t src_region_idx)
1973 {
1974 const size_t RegionSize = ParallelCompactData::RegionSize;
1975 const ParallelCompactData& sd = summary_data();
1976 assert(sd.is_region_aligned(dest_addr), "precondition");
1977
1978 const RegionData* const src_region_ptr = sd.region(src_region_idx);
1979 assert(src_region_ptr->data_size() > 0, "src region cannot be empty");
1980
1981 const size_t partial_obj_size = src_region_ptr->partial_obj_size();
1982 HeapWord* const src_region_destination = src_region_ptr->destination();
1983
1984 HeapWord* const region_start = sd.region_to_addr(src_region_idx);
1985 HeapWord* const region_end = sd.region_to_addr(src_region_idx) + RegionSize;
1986
1987 // Identify the actual destination for the first live words on this region,
1988 // taking split-region into account.
1989 HeapWord* region_start_destination;
1990 const SplitInfo& split_info = _space_info[src_space_id].split_info();
1991 if (split_info.is_split(src_region_idx)) {
1992 // The second part of this split region; use the recorded split point.
1993 if (dest_addr == src_region_destination) {
1994 return split_info.split_point();
1995 }
1996 region_start_destination = split_info.preceding_destination();
1997 } else {
1998 region_start_destination = src_region_destination;
1999 }
2000
2001 // Calculate the offset to be skipped
2002 size_t words_to_skip = pointer_delta(dest_addr, region_start_destination);
2003
2004 HeapWord* result;
2005 if (partial_obj_size > words_to_skip) {
2006 result = region_start + words_to_skip;
2007 } else {
2008 words_to_skip -= partial_obj_size;
2009 result = skip_live_words(region_start + partial_obj_size, region_end, words_to_skip);
2010 }
2011
2012 if (split_info.is_split(src_region_idx)) {
2013 assert(result < split_info.split_point(), "postcondition");
2014 } else {
2015 assert(result < region_end, "postcondition");
2016 }
2017
2018 return result;
2019 }
2020
2021 void PSParallelCompact::decrement_destination_counts(ParCompactionManager* cm,
2022 SpaceId src_space_id,
2023 size_t beg_region,
2024 HeapWord* end_addr)
2025 {
2026 ParallelCompactData& sd = summary_data();
2027
2028 #ifdef ASSERT
2029 MutableSpace* const src_space = _space_info[src_space_id].space();
2030 HeapWord* const beg_addr = sd.region_to_addr(beg_region);
2031 assert(src_space->contains(beg_addr) || beg_addr == src_space->end(),
2032 "src_space_id does not match beg_addr");
2033 assert(src_space->contains(end_addr) || end_addr == src_space->end(),
2034 "src_space_id does not match end_addr");
2035 #endif // #ifdef ASSERT
2036
2037 RegionData* const beg = sd.region(beg_region);
2038 RegionData* const end = sd.addr_to_region_ptr(sd.region_align_up(end_addr));
2039
2040 // Regions up to new_top() are enqueued if they become available.
2041 HeapWord* const new_top = _space_info[src_space_id].new_top();
2042 RegionData* const enqueue_end =
2043 sd.addr_to_region_ptr(sd.region_align_up(new_top));
2044
2045 for (RegionData* cur = beg; cur < end; ++cur) {
2046 assert(cur->data_size() > 0, "region must have live data");
2047 cur->decrement_destination_count();
2048 if (cur < enqueue_end && cur->available() && cur->claim()) {
2049 if (cur->mark_normal()) {
2050 cm->push_region(sd.region(cur));
2051 } else if (cur->mark_copied()) {
2052 // Try to copy the content of the shadow region back to its corresponding
2053 // heap region if the shadow region is filled. Otherwise, the GC thread
2054 // fills the shadow region will copy the data back (see
2055 // MoveAndUpdateShadowClosure::complete_region).
2056 copy_back(sd.region_to_addr(cur->shadow_region()), sd.region_to_addr(cur));
2057 ParCompactionManager::push_shadow_region_mt_safe(cur->shadow_region());
2058 cur->set_completed();
2059 }
2060 }
2061 }
2062 }
2063
2064 size_t PSParallelCompact::next_src_region(MoveAndUpdateClosure& closure,
2065 SpaceId& src_space_id,
2066 HeapWord*& src_space_top,
2067 HeapWord* end_addr)
2068 {
2069 ParallelCompactData& sd = PSParallelCompact::summary_data();
2070
2071 size_t src_region_idx = 0;
2072
2073 // Skip empty regions (if any) up to the top of the space.
2074 HeapWord* const src_aligned_up = sd.region_align_up(end_addr);
2075 RegionData* src_region_ptr = sd.addr_to_region_ptr(src_aligned_up);
2076 HeapWord* const top_aligned_up = sd.region_align_up(src_space_top);
2077 const RegionData* const top_region_ptr = sd.addr_to_region_ptr(top_aligned_up);
2078
2079 while (src_region_ptr < top_region_ptr && src_region_ptr->data_size() == 0) {
2080 ++src_region_ptr;
2081 }
2082
2083 if (src_region_ptr < top_region_ptr) {
2084 // Found the first non-empty region in the same space.
2085 src_region_idx = sd.region(src_region_ptr);
2086 closure.set_source(sd.region_to_addr(src_region_idx));
2087 return src_region_idx;
2088 }
2089
2090 // Switch to a new source space and find the first non-empty region.
2091 uint space_id = src_space_id + 1;
2092 assert(space_id < last_space_id, "not enough spaces");
2093
2094 for (/* empty */; space_id < last_space_id; ++space_id) {
2095 HeapWord* bottom = _space_info[space_id].space()->bottom();
2096 HeapWord* top = _space_info[space_id].space()->top();
2097 // Skip empty space
2098 if (bottom == top) {
2099 continue;
2100 }
2101
2102 // Identify the first region that contains live words in this space
2103 size_t cur_region = sd.addr_to_region_idx(bottom);
2104 size_t end_region = sd.addr_to_region_idx(sd.region_align_up(top));
2105
2106 for (/* empty */ ; cur_region < end_region; ++cur_region) {
2107 RegionData* cur = sd.region(cur_region);
2108 if (cur->live_obj_size() > 0) {
2109 HeapWord* region_start_addr = sd.region_to_addr(cur_region);
2110
2111 src_space_id = SpaceId(space_id);
2112 src_space_top = top;
2113 closure.set_source(region_start_addr);
2114 return cur_region;
2115 }
2116 }
2117 }
2118
2119 ShouldNotReachHere();
2120 }
2121
2122 HeapWord* PSParallelCompact::partial_obj_end(HeapWord* region_start_addr) {
2123 ParallelCompactData& sd = summary_data();
2124 assert(sd.is_region_aligned(region_start_addr), "precondition");
2125
2126 // Use per-region partial_obj_size to locate the end of the obj, that extends
2127 // to region_start_addr.
2128 size_t start_region_idx = sd.addr_to_region_idx(region_start_addr);
2129 size_t end_region_idx = sd.region_count();
2130 size_t accumulated_size = 0;
2131 for (size_t region_idx = start_region_idx; region_idx < end_region_idx; ++region_idx) {
2132 size_t cur_partial_obj_size = sd.region(region_idx)->partial_obj_size();
2133 accumulated_size += cur_partial_obj_size;
2134 if (cur_partial_obj_size != ParallelCompactData::RegionSize) {
2135 break;
2136 }
2137 }
2138 return region_start_addr + accumulated_size;
2139 }
2140
2141 static markWord safe_mark_word_prototype(HeapWord* cur_addr, HeapWord* end_addr) {
2142 // If the original markWord contains bits that cannot be reconstructed because
2143 // the header cannot be safely read, a placeholder is used. In this case,
2144 // the correct markWord is preserved before compaction and restored after
2145 // compaction completes.
2146 size_t remaining_words = pointer_delta(end_addr, cur_addr);
2147
2148 if (UseCompactObjectHeaders || (Arguments::is_valhalla_enabled() && safe_to_read_header(remaining_words))) {
2149 return cast_to_oop(cur_addr)->klass()->prototype_header();
2150 } else {
2151 return markWord::prototype();
2152 }
2153 }
2154
2155 // Use region_idx as the destination region, and evacuate all live objs on its
2156 // source regions to this destination region.
2157 void PSParallelCompact::fill_region(ParCompactionManager* cm, MoveAndUpdateClosure& closure, size_t region_idx)
2158 {
2159 ParMarkBitMap* const bitmap = mark_bitmap();
2160 ParallelCompactData& sd = summary_data();
2161 RegionData* const region_ptr = sd.region(region_idx);
2162
2163 // Get the source region and related info.
2164 size_t src_region_idx = region_ptr->source_region();
2165 SpaceId src_space_id = space_id(sd.region_to_addr(src_region_idx));
2166 HeapWord* src_space_top = _space_info[src_space_id].space()->top();
2167 HeapWord* dest_addr = sd.region_to_addr(region_idx);
2168
2169 closure.set_source(first_src_addr(dest_addr, src_space_id, src_region_idx));
2170
2171 // Adjust src_region_idx to prepare for decrementing destination counts (the
2172 // destination count is not decremented when a region is copied to itself).
2173 if (src_region_idx == region_idx) {
2174 src_region_idx += 1;
2175 }
2176
2177 // source-region:
2178 //
2179 // **********
2180 // | ~~~ |
2181 // **********
2182 // ^
2183 // |-- closure.source() / first_src_addr
2184 //
2185 //
2186 // ~~~ : live words
2187 //
2188 // destination-region:
2189 //
2190 // **********
2191 // | |
2192 // **********
2193 // ^
2194 // |-- region-start
2195 if (bitmap->is_unmarked(closure.source())) {
2196 // An object overflows the previous destination region, so this
2197 // destination region should copy the remainder of the object or as much as
2198 // will fit.
2199 HeapWord* const old_src_addr = closure.source();
2200 {
2201 HeapWord* region_start = sd.region_align_down(closure.source());
2202 HeapWord* obj_start = bitmap->find_obj_beg_reverse(region_start, closure.source());
2203 HeapWord* obj_end;
2204 if (obj_start != closure.source()) {
2205 assert(bitmap->is_marked(obj_start), "inv");
2206 // Found the actual obj-start, try to find the obj-end using either
2207 // size() if this obj is completely contained in the current region.
2208 HeapWord* next_region_start = region_start + ParallelCompactData::RegionSize;
2209 HeapWord* partial_obj_start = (next_region_start >= src_space_top)
2210 ? nullptr
2211 : sd.addr_to_region_ptr(next_region_start)->partial_obj_addr();
2212 // This obj extends to next region iff partial_obj_addr of the *next*
2213 // region is the same as obj-start.
2214 if (partial_obj_start == obj_start) {
2215 // This obj extends to next region.
2216 obj_end = partial_obj_end(next_region_start);
2217 } else {
2218 // Completely contained in this region; safe to use size().
2219 obj_end = obj_start + cast_to_oop(obj_start)->size();
2220 }
2221 } else {
2222 // This obj extends to current region.
2223 obj_end = partial_obj_end(region_start);
2224 }
2225 size_t partial_obj_size = pointer_delta(obj_end, closure.source());
2226 closure.copy_partial_obj(partial_obj_size);
2227 }
2228
2229 if (closure.is_full()) {
2230 decrement_destination_counts(cm, src_space_id, src_region_idx, closure.source());
2231 closure.complete_region(dest_addr, region_ptr);
2232 return;
2233 }
2234
2235 // Finished copying without using up the current destination-region
2236 HeapWord* const end_addr = sd.region_align_down(closure.source());
2237 if (sd.region_align_down(old_src_addr) != end_addr) {
2238 assert(sd.region_align_up(old_src_addr) == end_addr, "only one region");
2239 // The partial object was copied from more than one source region.
2240 decrement_destination_counts(cm, src_space_id, src_region_idx, end_addr);
2241
2242 // Move to the next source region, possibly switching spaces as well. All
2243 // args except end_addr may be modified.
2244 src_region_idx = next_src_region(closure, src_space_id, src_space_top, end_addr);
2245 }
2246 }
2247
2248 // Handle the rest obj-by-obj, where we know obj-start.
2249 do {
2250 HeapWord* cur_addr = closure.source();
2251 HeapWord* const end_addr = MIN2(sd.region_align_up(cur_addr + 1),
2252 src_space_top);
2253 // To handle the case where the final obj in source region extends to next region.
2254 HeapWord* final_obj_start = (end_addr == src_space_top)
2255 ? nullptr
2256 : sd.addr_to_region_ptr(end_addr)->partial_obj_addr();
2257 // Apply closure on objs inside [cur_addr, end_addr)
2258 do {
2259 cur_addr = bitmap->find_obj_beg(cur_addr, end_addr);
2260 if (cur_addr == end_addr) {
2261 break;
2262 }
2263 size_t obj_size;
2264 if (final_obj_start == cur_addr) {
2265 obj_size = pointer_delta(partial_obj_end(end_addr), cur_addr);
2266 } else {
2267 // This obj doesn't extend into next region; size() is safe to use.
2268 obj_size = cast_to_oop(cur_addr)->size();
2269 }
2270
2271 markWord mark = safe_mark_word_prototype(cur_addr, end_addr);
2272
2273 // Perform the move and update of the object
2274 closure.do_addr(cur_addr, obj_size, mark);
2275
2276 cur_addr += obj_size;
2277 } while (cur_addr < end_addr && !closure.is_full());
2278
2279 if (closure.is_full()) {
2280 decrement_destination_counts(cm, src_space_id, src_region_idx, closure.source());
2281 closure.complete_region(dest_addr, region_ptr);
2282 return;
2283 }
2284
2285 decrement_destination_counts(cm, src_space_id, src_region_idx, end_addr);
2286
2287 // Move to the next source region, possibly switching spaces as well. All
2288 // args except end_addr may be modified.
2289 src_region_idx = next_src_region(closure, src_space_id, src_space_top, end_addr);
2290 } while (true);
2291 }
2292
2293 void PSParallelCompact::fill_and_update_region(ParCompactionManager* cm, size_t region_idx)
2294 {
2295 MoveAndUpdateClosure cl(mark_bitmap(), region_idx);
2296 fill_region(cm, cl, region_idx);
2297 }
2298
2299 void PSParallelCompact::fill_and_update_shadow_region(ParCompactionManager* cm, size_t region_idx)
2300 {
2301 // Get a shadow region first
2302 ParallelCompactData& sd = summary_data();
2303 RegionData* const region_ptr = sd.region(region_idx);
2304 size_t shadow_region = ParCompactionManager::pop_shadow_region_mt_safe(region_ptr);
2305 // The InvalidShadow return value indicates the corresponding heap region is available,
2306 // so use MoveAndUpdateClosure to fill the normal region. Otherwise, use
2307 // MoveAndUpdateShadowClosure to fill the acquired shadow region.
2308 if (shadow_region == ParCompactionManager::InvalidShadow) {
2309 MoveAndUpdateClosure cl(mark_bitmap(), region_idx);
2310 region_ptr->shadow_to_normal();
2311 return fill_region(cm, cl, region_idx);
2312 } else {
2313 MoveAndUpdateShadowClosure cl(mark_bitmap(), region_idx, shadow_region);
2314 return fill_region(cm, cl, region_idx);
2315 }
2316 }
2317
2318 void PSParallelCompact::copy_back(HeapWord *shadow_addr, HeapWord *region_addr)
2319 {
2320 Copy::aligned_conjoint_words(shadow_addr, region_addr, _summary_data.RegionSize);
2321 }
2322
2323 bool PSParallelCompact::steal_unavailable_region(ParCompactionManager* cm, size_t ®ion_idx)
2324 {
2325 size_t next = cm->next_shadow_region();
2326 ParallelCompactData& sd = summary_data();
2327 size_t old_new_top = sd.addr_to_region_idx(_space_info[old_space_id].new_top());
2328 uint active_gc_threads = ParallelScavengeHeap::heap()->workers().active_workers();
2329
2330 while (next < old_new_top) {
2331 if (sd.region(next)->mark_shadow()) {
2332 region_idx = next;
2333 return true;
2334 }
2335 next = cm->move_next_shadow_region_by(active_gc_threads);
2336 }
2337
2338 return false;
2339 }
2340
2341 // The shadow region is an optimization to address region dependencies in full GC. The basic
2342 // idea is making more regions available by temporally storing their live objects in empty
2343 // shadow regions to resolve dependencies between them and the destination regions. Therefore,
2344 // GC threads need not wait destination regions to be available before processing sources.
2345 //
2346 // A typical workflow would be:
2347 // After draining its own stack and failing to steal from others, a GC worker would pick an
2348 // unavailable region (destination count > 0) and get a shadow region. Then the worker fills
2349 // the shadow region by copying live objects from source regions of the unavailable one. Once
2350 // the unavailable region becomes available, the data in the shadow region will be copied back.
2351 // Shadow regions are empty regions in the to-space and regions between top and end of other spaces.
2352 void PSParallelCompact::initialize_shadow_regions(uint parallel_gc_threads)
2353 {
2354 const ParallelCompactData& sd = PSParallelCompact::summary_data();
2355
2356 for (unsigned int id = old_space_id; id < last_space_id; ++id) {
2357 SpaceInfo* const space_info = _space_info + id;
2358 MutableSpace* const space = space_info->space();
2359
2360 const size_t beg_region =
2361 sd.addr_to_region_idx(sd.region_align_up(MAX2(space_info->new_top(), space->top())));
2362 const size_t end_region =
2363 sd.addr_to_region_idx(sd.region_align_down(space->end()));
2364
2365 for (size_t cur = beg_region; cur < end_region; ++cur) {
2366 ParCompactionManager::push_shadow_region(cur);
2367 }
2368 }
2369
2370 size_t beg_region = sd.addr_to_region_idx(_space_info[old_space_id].dense_prefix());
2371 for (uint i = 0; i < parallel_gc_threads; i++) {
2372 ParCompactionManager *cm = ParCompactionManager::gc_thread_compaction_manager(i);
2373 cm->set_next_shadow_region(beg_region + i);
2374 }
2375 }
2376
2377 void MoveAndUpdateClosure::copy_partial_obj(size_t partial_obj_size)
2378 {
2379 size_t words = MIN2(partial_obj_size, words_remaining());
2380
2381 // This test is necessary; if omitted, the pointer updates to a partial object
2382 // that crosses the dense prefix boundary could be overwritten.
2383 if (source() != copy_destination()) {
2384 DEBUG_ONLY(PSParallelCompact::check_new_location(source(), destination());)
2385 Copy::aligned_conjoint_words(source(), copy_destination(), words);
2386 }
2387 update_state(words);
2388 }
2389
2390 void MoveAndUpdateClosure::complete_region(HeapWord* dest_addr, PSParallelCompact::RegionData* region_ptr) {
2391 assert(region_ptr->shadow_state() == ParallelCompactData::RegionData::NormalRegion, "Region should be finished");
2392 region_ptr->set_completed();
2393 }
2394
2395 void MoveAndUpdateClosure::do_addr(HeapWord* addr, size_t words, markWord mark) {
2396 assert(destination() != nullptr, "sanity");
2397 _source = addr;
2398
2399 // The start_array must be updated even if the object is not moving.
2400 if (_start_array != nullptr) {
2401 _start_array->update_for_block(destination(), destination() + words);
2402 }
2403
2404 // Avoid overflow
2405 words = MIN2(words, words_remaining());
2406 assert(words > 0, "inv");
2407
2408 if (copy_destination() != source()) {
2409 DEBUG_ONLY(PSParallelCompact::check_new_location(source(), destination());)
2410 assert(source() != destination(), "inv");
2411 assert(FullGCForwarding::is_forwarded(cast_to_oop(source())), "inv");
2412 assert(FullGCForwarding::forwardee(cast_to_oop(source())) == cast_to_oop(destination()), "inv");
2413 Copy::aligned_conjoint_words(source(), copy_destination(), words);
2414 cast_to_oop(copy_destination())->set_mark(mark);
2415 }
2416
2417 update_state(words);
2418 }
2419
2420 void MoveAndUpdateShadowClosure::complete_region(HeapWord* dest_addr, PSParallelCompact::RegionData* region_ptr) {
2421 assert(region_ptr->shadow_state() == ParallelCompactData::RegionData::ShadowRegion, "Region should be shadow");
2422 // Record the shadow region index
2423 region_ptr->set_shadow_region(_shadow);
2424 // Mark the shadow region as filled to indicate the data is ready to be
2425 // copied back
2426 region_ptr->mark_filled();
2427 // Try to copy the content of the shadow region back to its corresponding
2428 // heap region if available; the GC thread that decreases the destination
2429 // count to zero will do the copying otherwise (see
2430 // PSParallelCompact::decrement_destination_counts).
2431 if (((region_ptr->available() && region_ptr->claim()) || region_ptr->claimed()) && region_ptr->mark_copied()) {
2432 region_ptr->set_completed();
2433 PSParallelCompact::copy_back(PSParallelCompact::summary_data().region_to_addr(_shadow), dest_addr);
2434 ParCompactionManager::push_shadow_region_mt_safe(_shadow);
2435 }
2436 }