1 /* 2 * Copyright (c) 2017, 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 "code/codeCache.hpp" 27 #include "gc/g1/g1CollectedHeap.hpp" 28 #include "gc/g1/g1CollectorPolicy.hpp" 29 #include "gc/g1/g1FullCollector.hpp" 30 #include "gc/g1/g1FullGCAdjustTask.hpp" 31 #include "gc/g1/g1FullGCCompactTask.hpp" 32 #include "gc/g1/g1FullGCMarker.inline.hpp" 33 #include "gc/g1/g1FullGCMarkTask.hpp" 34 #include "gc/g1/g1FullGCPrepareTask.hpp" 35 #include "gc/g1/g1FullGCReferenceProcessorExecutor.hpp" 36 #include "gc/g1/g1FullGCScope.hpp" 37 #include "gc/g1/g1OopClosures.hpp" 38 #include "gc/g1/g1Policy.hpp" 39 #include "gc/g1/g1StringDedup.hpp" 40 #include "gc/shared/gcTraceTime.inline.hpp" 41 #include "gc/shared/preservedMarks.hpp" 42 #include "gc/shared/referenceProcessor.hpp" 43 #include "gc/shared/weakProcessor.hpp" 44 #include "logging/log.hpp" 45 #include "runtime/biasedLocking.hpp" 46 #include "runtime/handles.inline.hpp" 47 #include "utilities/debug.hpp" 48 49 static void clear_and_activate_derived_pointers() { 50 #if COMPILER2_OR_JVMCI 51 DerivedPointerTable::clear(); 52 #endif 53 } 54 55 static void deactivate_derived_pointers() { 56 #if COMPILER2_OR_JVMCI 57 DerivedPointerTable::set_active(false); 58 #endif 59 } 60 61 static void update_derived_pointers() { 62 #if COMPILER2_OR_JVMCI 63 DerivedPointerTable::update_pointers(); 64 #endif 65 } 66 67 G1CMBitMap* G1FullCollector::mark_bitmap() { 68 return _heap->concurrent_mark()->next_mark_bitmap(); 69 } 70 71 ReferenceProcessor* G1FullCollector::reference_processor() { 72 return _heap->ref_processor_stw(); 73 } 74 75 G1FullCollector::G1FullCollector(G1CollectedHeap* heap, GCMemoryManager* memory_manager, bool explicit_gc, bool clear_soft_refs) : 76 _heap(heap), 77 _scope(memory_manager, explicit_gc, clear_soft_refs), 78 _num_workers(heap->workers()->active_workers()), 79 _oop_queue_set(_num_workers), 80 _array_queue_set(_num_workers), 81 _preserved_marks_set(true), 82 _serial_compaction_point(), 83 _is_alive(heap->concurrent_mark()->next_mark_bitmap()), 84 _is_alive_mutator(heap->ref_processor_stw(), &_is_alive), 85 _always_subject_to_discovery(), 86 _is_subject_mutator(heap->ref_processor_stw(), &_always_subject_to_discovery) { 87 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 88 89 _preserved_marks_set.init(_num_workers); 90 _markers = NEW_C_HEAP_ARRAY(G1FullGCMarker*, _num_workers, mtGC); 91 _compaction_points = NEW_C_HEAP_ARRAY(G1FullGCCompactionPoint*, _num_workers, mtGC); 92 for (uint i = 0; i < _num_workers; i++) { 93 _markers[i] = new G1FullGCMarker(i, _preserved_marks_set.get(i), mark_bitmap()); 94 _compaction_points[i] = new G1FullGCCompactionPoint(); 95 _oop_queue_set.register_queue(i, marker(i)->oop_stack()); 96 _array_queue_set.register_queue(i, marker(i)->objarray_stack()); 97 } 98 } 99 100 G1FullCollector::~G1FullCollector() { 101 for (uint i = 0; i < _num_workers; i++) { 102 delete _markers[i]; 103 delete _compaction_points[i]; 104 } 105 FREE_C_HEAP_ARRAY(G1FullGCMarker*, _markers); 106 FREE_C_HEAP_ARRAY(G1FullGCCompactionPoint*, _compaction_points); 107 } 108 109 void G1FullCollector::prepare_collection() { 110 _heap->g1_policy()->record_full_collection_start(); 111 112 _heap->print_heap_before_gc(); 113 _heap->print_heap_regions(); 114 115 _heap->abort_concurrent_cycle(); 116 _heap->verify_before_full_collection(scope()->is_explicit_gc()); 117 118 _heap->gc_prologue(true); 119 _heap->prepare_heap_for_full_collection(); 120 121 reference_processor()->enable_discovery(); 122 reference_processor()->setup_policy(scope()->should_clear_soft_refs()); 123 124 // When collecting the permanent generation Method*s may be moving, 125 // so we either have to flush all bcp data or convert it into bci. 126 CodeCache::gc_prologue(); 127 128 // We should save the marks of the currently locked biased monitors. 129 // The marking doesn't preserve the marks of biased objects. 130 BiasedLocking::preserve_marks(); 131 132 // Clear and activate derived pointer collection. 133 clear_and_activate_derived_pointers(); 134 } 135 136 void G1FullCollector::collect() { 137 phase1_mark_live_objects(); 138 verify_after_marking(); 139 140 // Don't add any more derived pointers during later phases 141 deactivate_derived_pointers(); 142 143 phase2_prepare_compaction(); 144 145 phase3_adjust_pointers(); 146 147 phase4_do_compaction(); 148 } 149 150 void G1FullCollector::complete_collection() { 151 // Restore all marks. 152 restore_marks(); 153 154 // When the pointers have been adjusted and moved, we can 155 // update the derived pointer table. 156 update_derived_pointers(); 157 158 BiasedLocking::restore_marks(); 159 CodeCache::gc_epilogue(); 160 JvmtiExport::gc_epilogue(); 161 162 _heap->prepare_heap_for_mutators(); 163 164 _heap->g1_policy()->record_full_collection_end(); 165 _heap->gc_epilogue(true); 166 167 _heap->verify_after_full_collection(); 168 169 _heap->print_heap_after_full_collection(scope()->heap_transition()); 170 } 171 172 void G1FullCollector::phase1_mark_live_objects() { 173 // Recursively traverse all live objects and mark them. 174 GCTraceTime(Info, gc, phases) info("Phase 1: Mark live objects", scope()->timer()); 175 176 // Do the actual marking. 177 G1FullGCMarkTask marking_task(this); 178 run_task(&marking_task); 179 180 // Process references discovered during marking. 181 G1FullGCReferenceProcessingExecutor reference_processing(this); 182 reference_processing.execute(scope()->timer(), scope()->tracer()); 183 184 // Weak oops cleanup. 185 { 186 GCTraceTime(Debug, gc, phases) trace("Phase 1: Weak Processing", scope()->timer()); 187 WeakProcessor::weak_oops_do(&_is_alive, &do_nothing_cl); 188 } 189 190 // Class unloading and cleanup. 191 if (ClassUnloading) { 192 GCTraceTime(Debug, gc, phases) debug("Phase 1: Class Unloading and Cleanup", scope()->timer()); 193 // Unload classes and purge the SystemDictionary. 194 bool purged_class = SystemDictionary::do_unloading(&_is_alive, scope()->timer()); 195 _heap->complete_cleaning(&_is_alive, purged_class); 196 } else { 197 GCTraceTime(Debug, gc, phases) debug("Phase 1: String and Symbol Tables Cleanup", scope()->timer()); 198 // If no class unloading just clean out strings and symbols. 199 _heap->partial_cleaning(&_is_alive, true, true, G1StringDedup::is_enabled()); 200 } 201 202 scope()->tracer()->report_object_count_after_gc(&_is_alive); 203 } 204 205 void G1FullCollector::phase2_prepare_compaction() { 206 GCTraceTime(Info, gc, phases) info("Phase 2: Prepare for compaction", scope()->timer()); 207 G1FullGCPrepareTask task(this); 208 run_task(&task); 209 210 // To avoid OOM when there is memory left. 211 if (!task.has_freed_regions()) { 212 task.prepare_serial_compaction(); 213 } 214 } 215 216 void G1FullCollector::phase3_adjust_pointers() { 217 // Adjust the pointers to reflect the new locations 218 GCTraceTime(Info, gc, phases) info("Phase 3: Adjust pointers", scope()->timer()); 219 220 G1FullGCAdjustTask task(this); 221 run_task(&task); 222 } 223 224 void G1FullCollector::phase4_do_compaction() { 225 // Compact the heap using the compaction queues created in phase 2. 226 GCTraceTime(Info, gc, phases) info("Phase 4: Compact heap", scope()->timer()); 227 G1FullGCCompactTask task(this); 228 run_task(&task); 229 230 // Serial compact to avoid OOM when very few free regions. 231 if (serial_compaction_point()->has_regions()) { 232 task.serial_compaction(); 233 } 234 } 235 236 void G1FullCollector::restore_marks() { 237 SharedRestorePreservedMarksTaskExecutor task_executor(_heap->workers()); 238 _preserved_marks_set.restore(&task_executor); 239 _preserved_marks_set.reclaim(); 240 } 241 242 void G1FullCollector::run_task(AbstractGangTask* task) { 243 _heap->workers()->run_task(task, _num_workers); 244 } 245 246 void G1FullCollector::verify_after_marking() { 247 if (!VerifyDuringGC || !_heap->verifier()->should_verify(G1HeapVerifier::G1VerifyFull)) { 248 // Only do verification if VerifyDuringGC and G1VerifyFull is set. 249 return; 250 } 251 252 HandleMark hm; // handle scope 253 #if COMPILER2_OR_JVMCI 254 DerivedPointerTableDeactivate dpt_deact; 255 #endif 256 _heap->prepare_for_verify(); 257 // Note: we can verify only the heap here. When an object is 258 // marked, the previous value of the mark word (including 259 // identity hash values, ages, etc) is preserved, and the mark 260 // word is set to markOop::marked_value - effectively removing 261 // any hash values from the mark word. These hash values are 262 // used when verifying the dictionaries and so removing them 263 // from the mark word can make verification of the dictionaries 264 // fail. At the end of the GC, the original mark word values 265 // (including hash values) are restored to the appropriate 266 // objects. 267 GCTraceTime(Info, gc, verify)("Verifying During GC (full)"); 268 _heap->verify(VerifyOption_G1UseFullMarking); 269 }