1 /* 2 * Copyright (c) 2001, 2012, 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 "classfile/javaClasses.hpp" 27 #include "classfile/symbolTable.hpp" 28 #include "classfile/systemDictionary.hpp" 29 #include "classfile/vmSymbols.hpp" 30 #include "code/codeCache.hpp" 31 #include "code/icBuffer.hpp" 32 #include "gc_implementation/g1/g1Log.hpp" 33 #include "gc_implementation/g1/g1MarkSweep.hpp" 34 #include "gc_implementation/shared/gcHeapSummary.hpp" 35 #include "gc_implementation/shared/gcTimer.hpp" 36 #include "gc_implementation/shared/gcTrace.hpp" 37 #include "gc_implementation/shared/gcTraceTime.hpp" 38 #include "memory/gcLocker.hpp" 39 #include "memory/genCollectedHeap.hpp" 40 #include "memory/modRefBarrierSet.hpp" 41 #include "memory/referencePolicy.hpp" 42 #include "memory/space.hpp" 43 #include "oops/instanceRefKlass.hpp" 44 #include "oops/oop.inline.hpp" 45 #include "prims/jvmtiExport.hpp" 46 #include "runtime/aprofiler.hpp" 47 #include "runtime/biasedLocking.hpp" 48 #include "runtime/fprofiler.hpp" 49 #include "runtime/synchronizer.hpp" 50 #include "runtime/thread.hpp" 51 #include "runtime/vmThread.hpp" 52 #include "utilities/copy.hpp" 53 #include "utilities/events.hpp" 54 55 class HeapRegion; 56 57 void G1MarkSweep::invoke_at_safepoint(ReferenceProcessor* rp, 58 bool clear_all_softrefs) { 59 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 60 61 SharedHeap* sh = SharedHeap::heap(); 62 #ifdef ASSERT 63 if (sh->collector_policy()->should_clear_all_soft_refs()) { 64 assert(clear_all_softrefs, "Policy should have been checked earler"); 65 } 66 #endif 67 // hook up weak ref data so it can be used during Mark-Sweep 68 assert(GenMarkSweep::ref_processor() == NULL, "no stomping"); 69 assert(rp != NULL, "should be non-NULL"); 70 assert(rp == G1CollectedHeap::heap()->ref_processor_stw(), "Precondition"); 71 72 GenMarkSweep::_ref_processor = rp; 73 rp->setup_policy(clear_all_softrefs); 74 75 // When collecting the permanent generation methodOops may be moving, 76 // so we either have to flush all bcp data or convert it into bci. 77 CodeCache::gc_prologue(); 78 Threads::gc_prologue(); 79 80 // Increment the invocation count for the permanent generation, since it is 81 // implicitly collected whenever we do a full mark sweep collection. 82 sh->perm_gen()->stat_record()->invocations++; 83 84 bool marked_for_unloading = false; 85 86 allocate_stacks(); 87 88 // We should save the marks of the currently locked biased monitors. 89 // The marking doesn't preserve the marks of biased objects. 90 BiasedLocking::preserve_marks(); 91 92 mark_sweep_phase1(marked_for_unloading, clear_all_softrefs); 93 94 mark_sweep_phase2(); 95 96 // Don't add any more derived pointers during phase3 97 COMPILER2_PRESENT(DerivedPointerTable::set_active(false)); 98 99 mark_sweep_phase3(); 100 101 mark_sweep_phase4(); 102 103 GenMarkSweep::restore_marks(); 104 BiasedLocking::restore_marks(); 105 GenMarkSweep::deallocate_stacks(); 106 107 // We must invalidate the perm-gen rs, so that it gets rebuilt. 108 GenRemSet* rs = sh->rem_set(); 109 rs->invalidate(sh->perm_gen()->used_region(), true /*whole_heap*/); 110 111 // "free at last gc" is calculated from these. 112 // CHF: cheating for now!!! 113 // Universe::set_heap_capacity_at_last_gc(Universe::heap()->capacity()); 114 // Universe::set_heap_used_at_last_gc(Universe::heap()->used()); 115 116 Threads::gc_epilogue(); 117 CodeCache::gc_epilogue(); 118 JvmtiExport::gc_epilogue(); 119 120 // refs processing: clean slate 121 GenMarkSweep::_ref_processor = NULL; 122 } 123 124 125 void G1MarkSweep::allocate_stacks() { 126 GenMarkSweep::_preserved_count_max = 0; 127 GenMarkSweep::_preserved_marks = NULL; 128 GenMarkSweep::_preserved_count = 0; 129 } 130 131 void G1MarkSweep::mark_sweep_phase1(bool& marked_for_unloading, 132 bool clear_all_softrefs) { 133 // Recursively traverse all live objects and mark them 134 GCTraceTime tm("phase 1", G1Log::fine() && Verbose, true, gc_timer()); 135 GenMarkSweep::trace(" 1"); 136 137 SharedHeap* sh = SharedHeap::heap(); 138 139 sh->process_strong_roots(true, // activeate StrongRootsScope 140 true, // Collecting permanent generation. 141 SharedHeap::SO_SystemClasses, 142 &GenMarkSweep::follow_root_closure, 143 &GenMarkSweep::follow_code_root_closure, 144 &GenMarkSweep::follow_root_closure); 145 146 // Process reference objects found during marking 147 ReferenceProcessor* rp = GenMarkSweep::ref_processor(); 148 assert(rp == G1CollectedHeap::heap()->ref_processor_stw(), "Sanity"); 149 150 rp->setup_policy(clear_all_softrefs); 151 const ReferenceProcessorStats& stats = 152 rp->process_discovered_references(&GenMarkSweep::is_alive, 153 &GenMarkSweep::keep_alive, 154 &GenMarkSweep::follow_stack_closure, 155 NULL, 156 gc_timer()); 157 gc_tracer()->report_gc_reference_stats(stats); 158 159 // Follow system dictionary roots and unload classes 160 bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive); 161 assert(GenMarkSweep::_marking_stack.is_empty(), 162 "stack should be empty by now"); 163 164 // Follow code cache roots (has to be done after system dictionary, 165 // assumes all live klasses are marked) 166 CodeCache::do_unloading(&GenMarkSweep::is_alive, 167 &GenMarkSweep::keep_alive, 168 purged_class); 169 GenMarkSweep::follow_stack(); 170 171 // Update subklass/sibling/implementor links of live klasses 172 GenMarkSweep::follow_weak_klass_links(); 173 assert(GenMarkSweep::_marking_stack.is_empty(), 174 "stack should be empty by now"); 175 176 // Visit memoized MDO's and clear any unmarked weak refs 177 GenMarkSweep::follow_mdo_weak_refs(); 178 assert(GenMarkSweep::_marking_stack.is_empty(), "just drained"); 179 180 // Visit interned string tables and delete unmarked oops 181 StringTable::unlink(&GenMarkSweep::is_alive); 182 // Clean up unreferenced symbols in symbol table. 183 SymbolTable::unlink(); 184 185 assert(GenMarkSweep::_marking_stack.is_empty(), 186 "stack should be empty by now"); 187 188 if (VerifyDuringGC) { 189 HandleMark hm; // handle scope 190 COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact); 191 gclog_or_tty->print(" VerifyDuringGC:(full)[Verifying "); 192 Universe::heap()->prepare_for_verify(); 193 // Note: we can verify only the heap here. When an object is 194 // marked, the previous value of the mark word (including 195 // identity hash values, ages, etc) is preserved, and the mark 196 // word is set to markOop::marked_value - effectively removing 197 // any hash values from the mark word. These hash values are 198 // used when verifying the dictionaries and so removing them 199 // from the mark word can make verification of the dictionaries 200 // fail. At the end of the GC, the orginal mark word values 201 // (including hash values) are restored to the appropriate 202 // objects. 203 Universe::heap()->verify(/* silent */ false, 204 /* option */ VerifyOption_G1UseMarkWord); 205 206 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 207 gclog_or_tty->print_cr("]"); 208 } 209 } 210 211 class G1PrepareCompactClosure: public HeapRegionClosure { 212 G1CollectedHeap* _g1h; 213 ModRefBarrierSet* _mrbs; 214 CompactPoint _cp; 215 HumongousRegionSet _humongous_proxy_set; 216 217 void free_humongous_region(HeapRegion* hr) { 218 HeapWord* end = hr->end(); 219 size_t dummy_pre_used; 220 FreeRegionList dummy_free_list("Dummy Free List for G1MarkSweep"); 221 222 assert(hr->startsHumongous(), 223 "Only the start of a humongous region should be freed."); 224 _g1h->free_humongous_region(hr, &dummy_pre_used, &dummy_free_list, 225 &_humongous_proxy_set, false /* par */); 226 hr->prepare_for_compaction(&_cp); 227 // Also clear the part of the card table that will be unused after 228 // compaction. 229 _mrbs->clear(MemRegion(hr->compaction_top(), end)); 230 dummy_free_list.remove_all(); 231 } 232 233 public: 234 G1PrepareCompactClosure(CompactibleSpace* cs) 235 : _g1h(G1CollectedHeap::heap()), 236 _mrbs(G1CollectedHeap::heap()->mr_bs()), 237 _cp(NULL, cs, cs->initialize_threshold()), 238 _humongous_proxy_set("G1MarkSweep Humongous Proxy Set") { } 239 240 void update_sets() { 241 // We'll recalculate total used bytes and recreate the free list 242 // at the end of the GC, so no point in updating those values here. 243 _g1h->update_sets_after_freeing_regions(0, /* pre_used */ 244 NULL, /* free_list */ 245 NULL, /* old_proxy_set */ 246 &_humongous_proxy_set, 247 false /* par */); 248 } 249 250 bool doHeapRegion(HeapRegion* hr) { 251 if (hr->isHumongous()) { 252 if (hr->startsHumongous()) { 253 oop obj = oop(hr->bottom()); 254 if (obj->is_gc_marked()) { 255 obj->forward_to(obj); 256 } else { 257 free_humongous_region(hr); 258 } 259 } else { 260 assert(hr->continuesHumongous(), "Invalid humongous."); 261 } 262 } else { 263 hr->prepare_for_compaction(&_cp); 264 // Also clear the part of the card table that will be unused after 265 // compaction. 266 _mrbs->clear(MemRegion(hr->compaction_top(), hr->end())); 267 } 268 return false; 269 } 270 }; 271 272 void G1MarkSweep::mark_sweep_phase2() { 273 // Now all live objects are marked, compute the new object addresses. 274 275 // It is imperative that we traverse perm_gen LAST. If dead space is 276 // allowed a range of dead object may get overwritten by a dead int 277 // array. If perm_gen is not traversed last a klassOop may get 278 // overwritten. This is fine since it is dead, but if the class has dead 279 // instances we have to skip them, and in order to find their size we 280 // need the klassOop! 281 // 282 // It is not required that we traverse spaces in the same order in 283 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops 284 // tracking expects us to do so. See comment under phase4. 285 286 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 287 Generation* pg = g1h->perm_gen(); 288 289 GCTraceTime tm("phase 2", G1Log::fine() && Verbose, true, gc_timer()); 290 GenMarkSweep::trace("2"); 291 292 // find the first region 293 HeapRegion* r = g1h->region_at(0); 294 CompactibleSpace* sp = r; 295 if (r->isHumongous() && oop(r->bottom())->is_gc_marked()) { 296 sp = r->next_compaction_space(); 297 } 298 299 G1PrepareCompactClosure blk(sp); 300 g1h->heap_region_iterate(&blk); 301 blk.update_sets(); 302 303 CompactPoint perm_cp(pg, NULL, NULL); 304 pg->prepare_for_compaction(&perm_cp); 305 } 306 307 class G1AdjustPointersClosure: public HeapRegionClosure { 308 public: 309 bool doHeapRegion(HeapRegion* r) { 310 if (r->isHumongous()) { 311 if (r->startsHumongous()) { 312 // We must adjust the pointers on the single H object. 313 oop obj = oop(r->bottom()); 314 debug_only(GenMarkSweep::track_interior_pointers(obj)); 315 // point all the oops to the new location 316 obj->adjust_pointers(); 317 debug_only(GenMarkSweep::check_interior_pointers()); 318 } 319 } else { 320 // This really ought to be "as_CompactibleSpace"... 321 r->adjust_pointers(); 322 } 323 return false; 324 } 325 }; 326 327 void G1MarkSweep::mark_sweep_phase3() { 328 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 329 Generation* pg = g1h->perm_gen(); 330 331 // Adjust the pointers to reflect the new locations 332 GCTraceTime tm("phase 3", G1Log::fine() && Verbose, true, gc_timer()); 333 GenMarkSweep::trace("3"); 334 335 SharedHeap* sh = SharedHeap::heap(); 336 337 sh->process_strong_roots(true, // activate StrongRootsScope 338 true, // Collecting permanent generation. 339 SharedHeap::SO_AllClasses, 340 &GenMarkSweep::adjust_root_pointer_closure, 341 NULL, // do not touch code cache here 342 &GenMarkSweep::adjust_pointer_closure); 343 344 assert(GenMarkSweep::ref_processor() == g1h->ref_processor_stw(), "Sanity"); 345 g1h->ref_processor_stw()->weak_oops_do(&GenMarkSweep::adjust_root_pointer_closure); 346 347 // Now adjust pointers in remaining weak roots. (All of which should 348 // have been cleared if they pointed to non-surviving objects.) 349 g1h->g1_process_weak_roots(&GenMarkSweep::adjust_root_pointer_closure, 350 &GenMarkSweep::adjust_pointer_closure); 351 352 GenMarkSweep::adjust_marks(); 353 354 G1AdjustPointersClosure blk; 355 g1h->heap_region_iterate(&blk); 356 pg->adjust_pointers(); 357 } 358 359 class G1SpaceCompactClosure: public HeapRegionClosure { 360 public: 361 G1SpaceCompactClosure() {} 362 363 bool doHeapRegion(HeapRegion* hr) { 364 if (hr->isHumongous()) { 365 if (hr->startsHumongous()) { 366 oop obj = oop(hr->bottom()); 367 if (obj->is_gc_marked()) { 368 obj->init_mark(); 369 } else { 370 assert(hr->is_empty(), "Should have been cleared in phase 2."); 371 } 372 hr->reset_during_compaction(); 373 } 374 } else { 375 hr->compact(); 376 } 377 return false; 378 } 379 }; 380 381 void G1MarkSweep::mark_sweep_phase4() { 382 // All pointers are now adjusted, move objects accordingly 383 384 // It is imperative that we traverse perm_gen first in phase4. All 385 // classes must be allocated earlier than their instances, and traversing 386 // perm_gen first makes sure that all klassOops have moved to their new 387 // location before any instance does a dispatch through it's klass! 388 389 // The ValidateMarkSweep live oops tracking expects us to traverse spaces 390 // in the same order in phase2, phase3 and phase4. We don't quite do that 391 // here (perm_gen first rather than last), so we tell the validate code 392 // to use a higher index (saved from phase2) when verifying perm_gen. 393 G1CollectedHeap* g1h = G1CollectedHeap::heap(); 394 Generation* pg = g1h->perm_gen(); 395 396 GCTraceTime tm("phase 4", G1Log::fine() && Verbose, true, gc_timer()); 397 GenMarkSweep::trace("4"); 398 399 pg->compact(); 400 401 G1SpaceCompactClosure blk; 402 g1h->heap_region_iterate(&blk); 403 404 }