1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/g1/g1MarkSweep.cpp Thu Jun 05 15:57:56 2008 -0700 1.3 @@ -0,0 +1,381 @@ 1.4 +/* 1.5 + * Copyright 2001-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#include "incls/_precompiled.incl" 1.29 +#include "incls/_g1MarkSweep.cpp.incl" 1.30 + 1.31 +class HeapRegion; 1.32 + 1.33 +void G1MarkSweep::invoke_at_safepoint(ReferenceProcessor* rp, 1.34 + bool clear_all_softrefs) { 1.35 + assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); 1.36 + 1.37 + // hook up weak ref data so it can be used during Mark-Sweep 1.38 + assert(GenMarkSweep::ref_processor() == NULL, "no stomping"); 1.39 + GenMarkSweep::_ref_processor = rp; 1.40 + assert(rp != NULL, "should be non-NULL"); 1.41 + 1.42 + // When collecting the permanent generation methodOops may be moving, 1.43 + // so we either have to flush all bcp data or convert it into bci. 1.44 + CodeCache::gc_prologue(); 1.45 + Threads::gc_prologue(); 1.46 + 1.47 + // Increment the invocation count for the permanent generation, since it is 1.48 + // implicitly collected whenever we do a full mark sweep collection. 1.49 + SharedHeap* sh = SharedHeap::heap(); 1.50 + sh->perm_gen()->stat_record()->invocations++; 1.51 + 1.52 + bool marked_for_unloading = false; 1.53 + 1.54 + allocate_stacks(); 1.55 + 1.56 + mark_sweep_phase1(marked_for_unloading, clear_all_softrefs); 1.57 + 1.58 + if (G1VerifyConcMark) { 1.59 + G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1.60 + g1h->checkConcurrentMark(); 1.61 + } 1.62 + 1.63 + mark_sweep_phase2(); 1.64 + 1.65 + // Don't add any more derived pointers during phase3 1.66 + COMPILER2_PRESENT(DerivedPointerTable::set_active(false)); 1.67 + 1.68 + mark_sweep_phase3(); 1.69 + 1.70 + mark_sweep_phase4(); 1.71 + 1.72 + GenMarkSweep::restore_marks(); 1.73 + 1.74 + GenMarkSweep::deallocate_stacks(); 1.75 + 1.76 + // We must invalidate the perm-gen rs, so that it gets rebuilt. 1.77 + GenRemSet* rs = sh->rem_set(); 1.78 + rs->invalidate(sh->perm_gen()->used_region(), true /*whole_heap*/); 1.79 + 1.80 + // "free at last gc" is calculated from these. 1.81 + // CHF: cheating for now!!! 1.82 + // Universe::set_heap_capacity_at_last_gc(Universe::heap()->capacity()); 1.83 + // Universe::set_heap_used_at_last_gc(Universe::heap()->used()); 1.84 + 1.85 + Threads::gc_epilogue(); 1.86 + CodeCache::gc_epilogue(); 1.87 + 1.88 + // refs processing: clean slate 1.89 + GenMarkSweep::_ref_processor = NULL; 1.90 +} 1.91 + 1.92 + 1.93 +void G1MarkSweep::allocate_stacks() { 1.94 + GenMarkSweep::_preserved_count_max = 0; 1.95 + GenMarkSweep::_preserved_marks = NULL; 1.96 + GenMarkSweep::_preserved_count = 0; 1.97 + GenMarkSweep::_preserved_mark_stack = NULL; 1.98 + GenMarkSweep::_preserved_oop_stack = NULL; 1.99 + 1.100 + GenMarkSweep::_marking_stack = 1.101 + new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true); 1.102 + 1.103 + size_t size = SystemDictionary::number_of_classes() * 2; 1.104 + GenMarkSweep::_revisit_klass_stack = 1.105 + new (ResourceObj::C_HEAP) GrowableArray<Klass*>((int)size, true); 1.106 +} 1.107 + 1.108 +void G1MarkSweep::mark_sweep_phase1(bool& marked_for_unloading, 1.109 + bool clear_all_softrefs) { 1.110 + // Recursively traverse all live objects and mark them 1.111 + EventMark m("1 mark object"); 1.112 + TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty); 1.113 + GenMarkSweep::trace(" 1"); 1.114 + 1.115 + SharedHeap* sh = SharedHeap::heap(); 1.116 + 1.117 + sh->process_strong_roots(true, // Collecting permanent generation. 1.118 + SharedHeap::SO_SystemClasses, 1.119 + &GenMarkSweep::follow_root_closure, 1.120 + &GenMarkSweep::follow_root_closure); 1.121 + 1.122 + // Process reference objects found during marking 1.123 + ReferencePolicy *soft_ref_policy; 1.124 + if (clear_all_softrefs) { 1.125 + soft_ref_policy = new AlwaysClearPolicy(); 1.126 + } else { 1.127 +#ifdef COMPILER2 1.128 + soft_ref_policy = new LRUMaxHeapPolicy(); 1.129 +#else 1.130 + soft_ref_policy = new LRUCurrentHeapPolicy(); 1.131 +#endif 1.132 + } 1.133 + assert(soft_ref_policy != NULL,"No soft reference policy"); 1.134 + GenMarkSweep::ref_processor()->process_discovered_references( 1.135 + soft_ref_policy, 1.136 + &GenMarkSweep::is_alive, 1.137 + &GenMarkSweep::keep_alive, 1.138 + &GenMarkSweep::follow_stack_closure, 1.139 + NULL); 1.140 + 1.141 + // Follow system dictionary roots and unload classes 1.142 + bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive); 1.143 + assert(GenMarkSweep::_marking_stack->is_empty(), 1.144 + "stack should be empty by now"); 1.145 + 1.146 + // Follow code cache roots (has to be done after system dictionary, 1.147 + // assumes all live klasses are marked) 1.148 + CodeCache::do_unloading(&GenMarkSweep::is_alive, 1.149 + &GenMarkSweep::keep_alive, 1.150 + purged_class); 1.151 + GenMarkSweep::follow_stack(); 1.152 + 1.153 + // Update subklass/sibling/implementor links of live klasses 1.154 + GenMarkSweep::follow_weak_klass_links(); 1.155 + assert(GenMarkSweep::_marking_stack->is_empty(), 1.156 + "stack should be empty by now"); 1.157 + 1.158 + // Visit symbol and interned string tables and delete unmarked oops 1.159 + SymbolTable::unlink(&GenMarkSweep::is_alive); 1.160 + StringTable::unlink(&GenMarkSweep::is_alive); 1.161 + 1.162 + assert(GenMarkSweep::_marking_stack->is_empty(), 1.163 + "stack should be empty by now"); 1.164 +} 1.165 + 1.166 +class G1PrepareCompactClosure: public HeapRegionClosure { 1.167 + ModRefBarrierSet* _mrbs; 1.168 + CompactPoint _cp; 1.169 + bool _popular_only; 1.170 + 1.171 + void free_humongous_region(HeapRegion* hr) { 1.172 + HeapWord* bot = hr->bottom(); 1.173 + HeapWord* end = hr->end(); 1.174 + assert(hr->startsHumongous(), 1.175 + "Only the start of a humongous region should be freed."); 1.176 + G1CollectedHeap::heap()->free_region(hr); 1.177 + hr->prepare_for_compaction(&_cp); 1.178 + // Also clear the part of the card table that will be unused after 1.179 + // compaction. 1.180 + _mrbs->clear(MemRegion(hr->compaction_top(), hr->end())); 1.181 + } 1.182 + 1.183 +public: 1.184 + G1PrepareCompactClosure(CompactibleSpace* cs, bool popular_only) : 1.185 + _cp(NULL, cs, cs->initialize_threshold()), 1.186 + _mrbs(G1CollectedHeap::heap()->mr_bs()), 1.187 + _popular_only(popular_only) 1.188 + {} 1.189 + bool doHeapRegion(HeapRegion* hr) { 1.190 + if (_popular_only && !hr->popular()) 1.191 + return true; // terminate early 1.192 + else if (!_popular_only && hr->popular()) 1.193 + return false; // skip this one. 1.194 + 1.195 + if (hr->isHumongous()) { 1.196 + if (hr->startsHumongous()) { 1.197 + oop obj = oop(hr->bottom()); 1.198 + if (obj->is_gc_marked()) { 1.199 + obj->forward_to(obj); 1.200 + } else { 1.201 + free_humongous_region(hr); 1.202 + } 1.203 + } else { 1.204 + assert(hr->continuesHumongous(), "Invalid humongous."); 1.205 + } 1.206 + } else { 1.207 + hr->prepare_for_compaction(&_cp); 1.208 + // Also clear the part of the card table that will be unused after 1.209 + // compaction. 1.210 + _mrbs->clear(MemRegion(hr->compaction_top(), hr->end())); 1.211 + } 1.212 + return false; 1.213 + } 1.214 +}; 1.215 +// Stolen verbatim from g1CollectedHeap.cpp 1.216 +class FindFirstRegionClosure: public HeapRegionClosure { 1.217 + HeapRegion* _a_region; 1.218 + bool _find_popular; 1.219 +public: 1.220 + FindFirstRegionClosure(bool find_popular) : 1.221 + _a_region(NULL), _find_popular(find_popular) {} 1.222 + bool doHeapRegion(HeapRegion* r) { 1.223 + if (r->popular() == _find_popular) { 1.224 + _a_region = r; 1.225 + return true; 1.226 + } else { 1.227 + return false; 1.228 + } 1.229 + } 1.230 + HeapRegion* result() { return _a_region; } 1.231 +}; 1.232 + 1.233 +void G1MarkSweep::mark_sweep_phase2() { 1.234 + // Now all live objects are marked, compute the new object addresses. 1.235 + 1.236 + // It is imperative that we traverse perm_gen LAST. If dead space is 1.237 + // allowed a range of dead object may get overwritten by a dead int 1.238 + // array. If perm_gen is not traversed last a klassOop may get 1.239 + // overwritten. This is fine since it is dead, but if the class has dead 1.240 + // instances we have to skip them, and in order to find their size we 1.241 + // need the klassOop! 1.242 + // 1.243 + // It is not required that we traverse spaces in the same order in 1.244 + // phase2, phase3 and phase4, but the ValidateMarkSweep live oops 1.245 + // tracking expects us to do so. See comment under phase4. 1.246 + 1.247 + G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1.248 + Generation* pg = g1h->perm_gen(); 1.249 + 1.250 + EventMark m("2 compute new addresses"); 1.251 + TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty); 1.252 + GenMarkSweep::trace("2"); 1.253 + 1.254 + // First we compact the popular regions. 1.255 + if (G1NumPopularRegions > 0) { 1.256 + CompactibleSpace* sp = g1h->first_compactible_space(); 1.257 + FindFirstRegionClosure cl(true /*find_popular*/); 1.258 + g1h->heap_region_iterate(&cl); 1.259 + HeapRegion *r = cl.result(); 1.260 + assert(r->popular(), "should have found a popular region."); 1.261 + assert(r == sp, "first popular heap region should " 1.262 + "== first compactible space"); 1.263 + G1PrepareCompactClosure blk(sp, true/*popular_only*/); 1.264 + g1h->heap_region_iterate(&blk); 1.265 + } 1.266 + 1.267 + // Now we do the regular regions. 1.268 + FindFirstRegionClosure cl(false /*find_popular*/); 1.269 + g1h->heap_region_iterate(&cl); 1.270 + HeapRegion *r = cl.result(); 1.271 + assert(!r->popular(), "should have founda non-popular region."); 1.272 + CompactibleSpace* sp = r; 1.273 + if (r->isHumongous() && oop(r->bottom())->is_gc_marked()) { 1.274 + sp = r->next_compaction_space(); 1.275 + } 1.276 + 1.277 + G1PrepareCompactClosure blk(sp, false/*popular_only*/); 1.278 + g1h->heap_region_iterate(&blk); 1.279 + 1.280 + CompactPoint perm_cp(pg, NULL, NULL); 1.281 + pg->prepare_for_compaction(&perm_cp); 1.282 +} 1.283 + 1.284 +class G1AdjustPointersClosure: public HeapRegionClosure { 1.285 + public: 1.286 + bool doHeapRegion(HeapRegion* r) { 1.287 + if (r->isHumongous()) { 1.288 + if (r->startsHumongous()) { 1.289 + // We must adjust the pointers on the single H object. 1.290 + oop obj = oop(r->bottom()); 1.291 + debug_only(GenMarkSweep::track_interior_pointers(obj)); 1.292 + // point all the oops to the new location 1.293 + obj->adjust_pointers(); 1.294 + debug_only(GenMarkSweep::check_interior_pointers()); 1.295 + } 1.296 + } else { 1.297 + // This really ought to be "as_CompactibleSpace"... 1.298 + r->adjust_pointers(); 1.299 + } 1.300 + return false; 1.301 + } 1.302 +}; 1.303 + 1.304 +void G1MarkSweep::mark_sweep_phase3() { 1.305 + G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1.306 + Generation* pg = g1h->perm_gen(); 1.307 + 1.308 + // Adjust the pointers to reflect the new locations 1.309 + EventMark m("3 adjust pointers"); 1.310 + TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty); 1.311 + GenMarkSweep::trace("3"); 1.312 + 1.313 + SharedHeap* sh = SharedHeap::heap(); 1.314 + 1.315 + sh->process_strong_roots(true, // Collecting permanent generation. 1.316 + SharedHeap::SO_AllClasses, 1.317 + &GenMarkSweep::adjust_root_pointer_closure, 1.318 + &GenMarkSweep::adjust_pointer_closure); 1.319 + 1.320 + g1h->ref_processor()->weak_oops_do(&GenMarkSweep::adjust_root_pointer_closure); 1.321 + 1.322 + // Now adjust pointers in remaining weak roots. (All of which should 1.323 + // have been cleared if they pointed to non-surviving objects.) 1.324 + g1h->g1_process_weak_roots(&GenMarkSweep::adjust_root_pointer_closure, 1.325 + &GenMarkSweep::adjust_pointer_closure); 1.326 + 1.327 + GenMarkSweep::adjust_marks(); 1.328 + 1.329 + G1AdjustPointersClosure blk; 1.330 + g1h->heap_region_iterate(&blk); 1.331 + pg->adjust_pointers(); 1.332 +} 1.333 + 1.334 +class G1SpaceCompactClosure: public HeapRegionClosure { 1.335 +public: 1.336 + G1SpaceCompactClosure() {} 1.337 + 1.338 + bool doHeapRegion(HeapRegion* hr) { 1.339 + if (hr->isHumongous()) { 1.340 + if (hr->startsHumongous()) { 1.341 + oop obj = oop(hr->bottom()); 1.342 + if (obj->is_gc_marked()) { 1.343 + obj->init_mark(); 1.344 + } else { 1.345 + assert(hr->is_empty(), "Should have been cleared in phase 2."); 1.346 + } 1.347 + hr->reset_during_compaction(); 1.348 + } 1.349 + } else { 1.350 + hr->compact(); 1.351 + } 1.352 + return false; 1.353 + } 1.354 +}; 1.355 + 1.356 +void G1MarkSweep::mark_sweep_phase4() { 1.357 + // All pointers are now adjusted, move objects accordingly 1.358 + 1.359 + // It is imperative that we traverse perm_gen first in phase4. All 1.360 + // classes must be allocated earlier than their instances, and traversing 1.361 + // perm_gen first makes sure that all klassOops have moved to their new 1.362 + // location before any instance does a dispatch through it's klass! 1.363 + 1.364 + // The ValidateMarkSweep live oops tracking expects us to traverse spaces 1.365 + // in the same order in phase2, phase3 and phase4. We don't quite do that 1.366 + // here (perm_gen first rather than last), so we tell the validate code 1.367 + // to use a higher index (saved from phase2) when verifying perm_gen. 1.368 + G1CollectedHeap* g1h = G1CollectedHeap::heap(); 1.369 + Generation* pg = g1h->perm_gen(); 1.370 + 1.371 + EventMark m("4 compact heap"); 1.372 + TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty); 1.373 + GenMarkSweep::trace("4"); 1.374 + 1.375 + pg->compact(); 1.376 + 1.377 + G1SpaceCompactClosure blk; 1.378 + g1h->heap_region_iterate(&blk); 1.379 + 1.380 +} 1.381 + 1.382 +// Local Variables: *** 1.383 +// c-indentation-style: gnu *** 1.384 +// End: ***