1 /* 2 * Copyright (c) 2017, 2022, 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 "precompiled.hpp" 26 #include "gc/g1/g1CollectedHeap.hpp" 27 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp" 28 #include "gc/g1/g1FullCollector.inline.hpp" 29 #include "gc/g1/g1FullGCCompactionPoint.hpp" 30 #include "gc/g1/g1FullGCCompactTask.hpp" 31 #include "gc/g1/heapRegion.inline.hpp" 32 #include "gc/shared/gcTraceTime.inline.hpp" 33 #include "logging/log.hpp" 34 #include "oops/oop.inline.hpp" 35 #include "utilities/ticks.hpp" 36 37 // Do work for all skip-compacting regions. 38 class G1ResetSkipCompactingClosure : public HeapRegionClosure { 39 G1FullCollector* _collector; 40 41 public: 42 G1ResetSkipCompactingClosure(G1FullCollector* collector) : _collector(collector) { } 43 44 bool do_heap_region(HeapRegion* r) { 45 uint region_index = r->hrm_index(); 46 // Only for skip-compaction regions; early return otherwise. 47 if (!_collector->is_skip_compacting(region_index)) { 48 return false; 49 } 50 #ifdef ASSERT 51 if (r->is_humongous()) { 52 oop obj = cast_to_oop(r->humongous_start_region()->bottom()); 53 assert(_collector->mark_bitmap()->is_marked(obj), "must be live"); 54 } else if (r->is_open_archive()) { 55 bool is_empty = (_collector->live_words(r->hrm_index()) == 0); 56 assert(!is_empty, "should contain at least one live obj"); 57 } else if (r->is_closed_archive()) { 58 // should early-return above 59 ShouldNotReachHere(); 60 } else { 61 assert(_collector->live_words(region_index) > _collector->scope()->region_compaction_threshold(), 62 "should be quite full"); 63 } 64 #endif 65 assert(_collector->compaction_top(r) == nullptr, 66 "region %u compaction_top " PTR_FORMAT " must not be different from bottom " PTR_FORMAT, 67 r->hrm_index(), p2i(_collector->compaction_top(r)), p2i(r->bottom())); 68 69 r->reset_skip_compacting_after_full_gc(); 70 return false; 71 } 72 }; 73 74 void G1FullGCCompactTask::G1CompactRegionClosure::clear_in_bitmap(oop obj) { 75 assert(_bitmap->is_marked(obj), "Should only compact marked objects"); 76 _bitmap->clear(obj); 77 } 78 79 size_t G1FullGCCompactTask::G1CompactRegionClosure::apply(oop obj) { 80 size_t size = obj->size(); 81 if (obj->is_forwarded()) { 82 HeapWord* destination = cast_from_oop<HeapWord*>(obj->forwardee()); 83 84 // copy object and reinit its mark 85 HeapWord* obj_addr = cast_from_oop<HeapWord*>(obj); 86 assert(obj_addr != destination, "everything in this pass should be moving"); 87 Copy::aligned_conjoint_words(obj_addr, destination, size); 88 89 // There is no need to transform stack chunks - marking already did that. 90 cast_to_oop(destination)->init_mark(); 91 assert(cast_to_oop(destination)->klass() != NULL, "should have a class"); 92 } 93 94 // Clear the mark for the compacted object to allow reuse of the 95 // bitmap without an additional clearing step. 96 clear_in_bitmap(obj); 97 return size; 98 } 99 100 void G1FullGCCompactTask::compact_region(HeapRegion* hr) { 101 assert(!hr->is_pinned(), "Should be no pinned region in compaction queue"); 102 assert(!hr->is_humongous(), "Should be no humongous regions in compaction queue"); 103 104 if (!collector()->is_free(hr->hrm_index())) { 105 // The compaction closure not only copies the object to the new 106 // location, but also clears the bitmap for it. This is needed 107 // for bitmap verification and to be able to use the bitmap 108 // for evacuation failures in the next young collection. Testing 109 // showed that it was better overall to clear bit by bit, compared 110 // to clearing the whole region at the end. This difference was 111 // clearly seen for regions with few marks. 112 G1CompactRegionClosure compact(collector()->mark_bitmap()); 113 hr->apply_to_marked_objects(collector()->mark_bitmap(), &compact); 114 } 115 116 hr->reset_compacted_after_full_gc(_collector->compaction_top(hr)); 117 } 118 119 void G1FullGCCompactTask::work(uint worker_id) { 120 Ticks start = Ticks::now(); 121 GrowableArray<HeapRegion*>* compaction_queue = collector()->compaction_point(worker_id)->regions(); 122 for (GrowableArrayIterator<HeapRegion*> it = compaction_queue->begin(); 123 it != compaction_queue->end(); 124 ++it) { 125 compact_region(*it); 126 } 127 128 G1ResetSkipCompactingClosure hc(collector()); 129 G1CollectedHeap::heap()->heap_region_par_iterate_from_worker_offset(&hc, &_claimer, worker_id); 130 log_task("Compaction task", worker_id, start); 131 } 132 133 void G1FullGCCompactTask::serial_compaction() { 134 GCTraceTime(Debug, gc, phases) tm("Phase 4: Serial Compaction", collector()->scope()->timer()); 135 GrowableArray<HeapRegion*>* compaction_queue = collector()->serial_compaction_point()->regions(); 136 for (GrowableArrayIterator<HeapRegion*> it = compaction_queue->begin(); 137 it != compaction_queue->end(); 138 ++it) { 139 compact_region(*it); 140 } 141 }