Tue, 15 Sep 2009 21:53:47 -0700
6863023: need non-perm oops in code cache for JSR 292
Summary: Make a special root-list for those few nmethods which might contain non-perm oops.
Reviewed-by: twisti, kvn, never, jmasa, ysr
ysr@777 | 1 | /* |
xdono@1279 | 2 | * Copyright 2001-2009 Sun Microsystems, Inc. All Rights Reserved. |
ysr@777 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ysr@777 | 4 | * |
ysr@777 | 5 | * This code is free software; you can redistribute it and/or modify it |
ysr@777 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ysr@777 | 7 | * published by the Free Software Foundation. |
ysr@777 | 8 | * |
ysr@777 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ysr@777 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ysr@777 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ysr@777 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
ysr@777 | 13 | * accompanied this code). |
ysr@777 | 14 | * |
ysr@777 | 15 | * You should have received a copy of the GNU General Public License version |
ysr@777 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
ysr@777 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ysr@777 | 18 | * |
ysr@777 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
ysr@777 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
ysr@777 | 21 | * have any questions. |
ysr@777 | 22 | * |
ysr@777 | 23 | */ |
ysr@777 | 24 | |
ysr@777 | 25 | #include "incls/_precompiled.incl" |
ysr@777 | 26 | #include "incls/_g1MarkSweep.cpp.incl" |
ysr@777 | 27 | |
ysr@777 | 28 | class HeapRegion; |
ysr@777 | 29 | |
ysr@777 | 30 | void G1MarkSweep::invoke_at_safepoint(ReferenceProcessor* rp, |
ysr@777 | 31 | bool clear_all_softrefs) { |
ysr@777 | 32 | assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); |
ysr@777 | 33 | |
ysr@777 | 34 | // hook up weak ref data so it can be used during Mark-Sweep |
ysr@777 | 35 | assert(GenMarkSweep::ref_processor() == NULL, "no stomping"); |
ysr@888 | 36 | assert(rp != NULL, "should be non-NULL"); |
ysr@777 | 37 | GenMarkSweep::_ref_processor = rp; |
ysr@892 | 38 | rp->setup_policy(clear_all_softrefs); |
ysr@777 | 39 | |
ysr@777 | 40 | // When collecting the permanent generation methodOops may be moving, |
ysr@777 | 41 | // so we either have to flush all bcp data or convert it into bci. |
ysr@777 | 42 | CodeCache::gc_prologue(); |
ysr@777 | 43 | Threads::gc_prologue(); |
ysr@777 | 44 | |
ysr@777 | 45 | // Increment the invocation count for the permanent generation, since it is |
ysr@777 | 46 | // implicitly collected whenever we do a full mark sweep collection. |
ysr@777 | 47 | SharedHeap* sh = SharedHeap::heap(); |
ysr@777 | 48 | sh->perm_gen()->stat_record()->invocations++; |
ysr@777 | 49 | |
ysr@777 | 50 | bool marked_for_unloading = false; |
ysr@777 | 51 | |
ysr@777 | 52 | allocate_stacks(); |
ysr@777 | 53 | |
iveresov@793 | 54 | // We should save the marks of the currently locked biased monitors. |
iveresov@793 | 55 | // The marking doesn't preserve the marks of biased objects. |
iveresov@793 | 56 | BiasedLocking::preserve_marks(); |
iveresov@793 | 57 | |
ysr@777 | 58 | mark_sweep_phase1(marked_for_unloading, clear_all_softrefs); |
ysr@777 | 59 | |
johnc@1186 | 60 | if (VerifyDuringGC) { |
ysr@777 | 61 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
ysr@777 | 62 | g1h->checkConcurrentMark(); |
ysr@777 | 63 | } |
ysr@777 | 64 | |
ysr@777 | 65 | mark_sweep_phase2(); |
ysr@777 | 66 | |
ysr@777 | 67 | // Don't add any more derived pointers during phase3 |
ysr@777 | 68 | COMPILER2_PRESENT(DerivedPointerTable::set_active(false)); |
ysr@777 | 69 | |
ysr@777 | 70 | mark_sweep_phase3(); |
ysr@777 | 71 | |
ysr@777 | 72 | mark_sweep_phase4(); |
ysr@777 | 73 | |
ysr@777 | 74 | GenMarkSweep::restore_marks(); |
iveresov@793 | 75 | BiasedLocking::restore_marks(); |
ysr@777 | 76 | GenMarkSweep::deallocate_stacks(); |
ysr@777 | 77 | |
ysr@777 | 78 | // We must invalidate the perm-gen rs, so that it gets rebuilt. |
ysr@777 | 79 | GenRemSet* rs = sh->rem_set(); |
ysr@777 | 80 | rs->invalidate(sh->perm_gen()->used_region(), true /*whole_heap*/); |
ysr@777 | 81 | |
ysr@777 | 82 | // "free at last gc" is calculated from these. |
ysr@777 | 83 | // CHF: cheating for now!!! |
ysr@777 | 84 | // Universe::set_heap_capacity_at_last_gc(Universe::heap()->capacity()); |
ysr@777 | 85 | // Universe::set_heap_used_at_last_gc(Universe::heap()->used()); |
ysr@777 | 86 | |
ysr@777 | 87 | Threads::gc_epilogue(); |
ysr@777 | 88 | CodeCache::gc_epilogue(); |
ysr@777 | 89 | |
ysr@777 | 90 | // refs processing: clean slate |
ysr@777 | 91 | GenMarkSweep::_ref_processor = NULL; |
ysr@777 | 92 | } |
ysr@777 | 93 | |
ysr@777 | 94 | |
ysr@777 | 95 | void G1MarkSweep::allocate_stacks() { |
ysr@777 | 96 | GenMarkSweep::_preserved_count_max = 0; |
ysr@777 | 97 | GenMarkSweep::_preserved_marks = NULL; |
ysr@777 | 98 | GenMarkSweep::_preserved_count = 0; |
ysr@777 | 99 | GenMarkSweep::_preserved_mark_stack = NULL; |
ysr@777 | 100 | GenMarkSweep::_preserved_oop_stack = NULL; |
ysr@777 | 101 | |
ysr@777 | 102 | GenMarkSweep::_marking_stack = |
ysr@777 | 103 | new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true); |
ysr@777 | 104 | |
ysr@777 | 105 | size_t size = SystemDictionary::number_of_classes() * 2; |
ysr@777 | 106 | GenMarkSweep::_revisit_klass_stack = |
ysr@777 | 107 | new (ResourceObj::C_HEAP) GrowableArray<Klass*>((int)size, true); |
ysr@777 | 108 | } |
ysr@777 | 109 | |
ysr@777 | 110 | void G1MarkSweep::mark_sweep_phase1(bool& marked_for_unloading, |
ysr@777 | 111 | bool clear_all_softrefs) { |
ysr@777 | 112 | // Recursively traverse all live objects and mark them |
ysr@777 | 113 | EventMark m("1 mark object"); |
ysr@777 | 114 | TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty); |
ysr@777 | 115 | GenMarkSweep::trace(" 1"); |
ysr@777 | 116 | |
ysr@777 | 117 | SharedHeap* sh = SharedHeap::heap(); |
ysr@777 | 118 | |
jrose@1424 | 119 | sh->process_strong_roots(true, // activeate StrongRootsScope |
jrose@1424 | 120 | true, // Collecting permanent generation. |
ysr@777 | 121 | SharedHeap::SO_SystemClasses, |
ysr@777 | 122 | &GenMarkSweep::follow_root_closure, |
jrose@1424 | 123 | &GenMarkSweep::follow_code_root_closure, |
ysr@777 | 124 | &GenMarkSweep::follow_root_closure); |
ysr@777 | 125 | |
ysr@777 | 126 | // Process reference objects found during marking |
ysr@888 | 127 | ReferenceProcessor* rp = GenMarkSweep::ref_processor(); |
ysr@892 | 128 | rp->setup_policy(clear_all_softrefs); |
ysr@888 | 129 | rp->process_discovered_references(&GenMarkSweep::is_alive, |
ysr@888 | 130 | &GenMarkSweep::keep_alive, |
ysr@888 | 131 | &GenMarkSweep::follow_stack_closure, |
ysr@888 | 132 | NULL); |
ysr@777 | 133 | |
ysr@777 | 134 | // Follow system dictionary roots and unload classes |
ysr@777 | 135 | bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive); |
ysr@777 | 136 | assert(GenMarkSweep::_marking_stack->is_empty(), |
ysr@777 | 137 | "stack should be empty by now"); |
ysr@777 | 138 | |
ysr@777 | 139 | // Follow code cache roots (has to be done after system dictionary, |
ysr@777 | 140 | // assumes all live klasses are marked) |
ysr@777 | 141 | CodeCache::do_unloading(&GenMarkSweep::is_alive, |
ysr@777 | 142 | &GenMarkSweep::keep_alive, |
ysr@777 | 143 | purged_class); |
ysr@777 | 144 | GenMarkSweep::follow_stack(); |
ysr@777 | 145 | |
ysr@777 | 146 | // Update subklass/sibling/implementor links of live klasses |
ysr@777 | 147 | GenMarkSweep::follow_weak_klass_links(); |
ysr@777 | 148 | assert(GenMarkSweep::_marking_stack->is_empty(), |
ysr@777 | 149 | "stack should be empty by now"); |
ysr@777 | 150 | |
ysr@777 | 151 | // Visit symbol and interned string tables and delete unmarked oops |
ysr@777 | 152 | SymbolTable::unlink(&GenMarkSweep::is_alive); |
ysr@777 | 153 | StringTable::unlink(&GenMarkSweep::is_alive); |
ysr@777 | 154 | |
ysr@777 | 155 | assert(GenMarkSweep::_marking_stack->is_empty(), |
ysr@777 | 156 | "stack should be empty by now"); |
ysr@777 | 157 | } |
ysr@777 | 158 | |
ysr@777 | 159 | class G1PrepareCompactClosure: public HeapRegionClosure { |
ysr@777 | 160 | ModRefBarrierSet* _mrbs; |
ysr@777 | 161 | CompactPoint _cp; |
ysr@777 | 162 | |
ysr@777 | 163 | void free_humongous_region(HeapRegion* hr) { |
ysr@777 | 164 | HeapWord* bot = hr->bottom(); |
ysr@777 | 165 | HeapWord* end = hr->end(); |
ysr@777 | 166 | assert(hr->startsHumongous(), |
ysr@777 | 167 | "Only the start of a humongous region should be freed."); |
ysr@777 | 168 | G1CollectedHeap::heap()->free_region(hr); |
ysr@777 | 169 | hr->prepare_for_compaction(&_cp); |
ysr@777 | 170 | // Also clear the part of the card table that will be unused after |
ysr@777 | 171 | // compaction. |
ysr@777 | 172 | _mrbs->clear(MemRegion(hr->compaction_top(), hr->end())); |
ysr@777 | 173 | } |
ysr@777 | 174 | |
ysr@777 | 175 | public: |
apetrusenko@1112 | 176 | G1PrepareCompactClosure(CompactibleSpace* cs) : |
ysr@777 | 177 | _cp(NULL, cs, cs->initialize_threshold()), |
apetrusenko@1112 | 178 | _mrbs(G1CollectedHeap::heap()->mr_bs()) |
ysr@777 | 179 | {} |
ysr@777 | 180 | bool doHeapRegion(HeapRegion* hr) { |
ysr@777 | 181 | if (hr->isHumongous()) { |
ysr@777 | 182 | if (hr->startsHumongous()) { |
ysr@777 | 183 | oop obj = oop(hr->bottom()); |
ysr@777 | 184 | if (obj->is_gc_marked()) { |
ysr@777 | 185 | obj->forward_to(obj); |
ysr@777 | 186 | } else { |
ysr@777 | 187 | free_humongous_region(hr); |
ysr@777 | 188 | } |
ysr@777 | 189 | } else { |
ysr@777 | 190 | assert(hr->continuesHumongous(), "Invalid humongous."); |
ysr@777 | 191 | } |
ysr@777 | 192 | } else { |
ysr@777 | 193 | hr->prepare_for_compaction(&_cp); |
ysr@777 | 194 | // Also clear the part of the card table that will be unused after |
ysr@777 | 195 | // compaction. |
ysr@777 | 196 | _mrbs->clear(MemRegion(hr->compaction_top(), hr->end())); |
ysr@777 | 197 | } |
ysr@777 | 198 | return false; |
ysr@777 | 199 | } |
ysr@777 | 200 | }; |
apetrusenko@1112 | 201 | |
apetrusenko@1112 | 202 | // Finds the first HeapRegion. |
ysr@777 | 203 | class FindFirstRegionClosure: public HeapRegionClosure { |
ysr@777 | 204 | HeapRegion* _a_region; |
ysr@777 | 205 | public: |
apetrusenko@1112 | 206 | FindFirstRegionClosure() : _a_region(NULL) {} |
ysr@777 | 207 | bool doHeapRegion(HeapRegion* r) { |
apetrusenko@1112 | 208 | _a_region = r; |
apetrusenko@1112 | 209 | return true; |
ysr@777 | 210 | } |
ysr@777 | 211 | HeapRegion* result() { return _a_region; } |
ysr@777 | 212 | }; |
ysr@777 | 213 | |
ysr@777 | 214 | void G1MarkSweep::mark_sweep_phase2() { |
ysr@777 | 215 | // Now all live objects are marked, compute the new object addresses. |
ysr@777 | 216 | |
ysr@777 | 217 | // It is imperative that we traverse perm_gen LAST. If dead space is |
ysr@777 | 218 | // allowed a range of dead object may get overwritten by a dead int |
ysr@777 | 219 | // array. If perm_gen is not traversed last a klassOop may get |
ysr@777 | 220 | // overwritten. This is fine since it is dead, but if the class has dead |
ysr@777 | 221 | // instances we have to skip them, and in order to find their size we |
ysr@777 | 222 | // need the klassOop! |
ysr@777 | 223 | // |
ysr@777 | 224 | // It is not required that we traverse spaces in the same order in |
ysr@777 | 225 | // phase2, phase3 and phase4, but the ValidateMarkSweep live oops |
ysr@777 | 226 | // tracking expects us to do so. See comment under phase4. |
ysr@777 | 227 | |
ysr@777 | 228 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
ysr@777 | 229 | Generation* pg = g1h->perm_gen(); |
ysr@777 | 230 | |
ysr@777 | 231 | EventMark m("2 compute new addresses"); |
ysr@777 | 232 | TraceTime tm("phase 2", PrintGC && Verbose, true, gclog_or_tty); |
ysr@777 | 233 | GenMarkSweep::trace("2"); |
ysr@777 | 234 | |
apetrusenko@1112 | 235 | FindFirstRegionClosure cl; |
ysr@777 | 236 | g1h->heap_region_iterate(&cl); |
ysr@777 | 237 | HeapRegion *r = cl.result(); |
ysr@777 | 238 | CompactibleSpace* sp = r; |
ysr@777 | 239 | if (r->isHumongous() && oop(r->bottom())->is_gc_marked()) { |
ysr@777 | 240 | sp = r->next_compaction_space(); |
ysr@777 | 241 | } |
ysr@777 | 242 | |
apetrusenko@1112 | 243 | G1PrepareCompactClosure blk(sp); |
ysr@777 | 244 | g1h->heap_region_iterate(&blk); |
ysr@777 | 245 | |
ysr@777 | 246 | CompactPoint perm_cp(pg, NULL, NULL); |
ysr@777 | 247 | pg->prepare_for_compaction(&perm_cp); |
ysr@777 | 248 | } |
ysr@777 | 249 | |
ysr@777 | 250 | class G1AdjustPointersClosure: public HeapRegionClosure { |
ysr@777 | 251 | public: |
ysr@777 | 252 | bool doHeapRegion(HeapRegion* r) { |
ysr@777 | 253 | if (r->isHumongous()) { |
ysr@777 | 254 | if (r->startsHumongous()) { |
ysr@777 | 255 | // We must adjust the pointers on the single H object. |
ysr@777 | 256 | oop obj = oop(r->bottom()); |
ysr@777 | 257 | debug_only(GenMarkSweep::track_interior_pointers(obj)); |
ysr@777 | 258 | // point all the oops to the new location |
ysr@777 | 259 | obj->adjust_pointers(); |
ysr@777 | 260 | debug_only(GenMarkSweep::check_interior_pointers()); |
ysr@777 | 261 | } |
ysr@777 | 262 | } else { |
ysr@777 | 263 | // This really ought to be "as_CompactibleSpace"... |
ysr@777 | 264 | r->adjust_pointers(); |
ysr@777 | 265 | } |
ysr@777 | 266 | return false; |
ysr@777 | 267 | } |
ysr@777 | 268 | }; |
ysr@777 | 269 | |
ysr@777 | 270 | void G1MarkSweep::mark_sweep_phase3() { |
ysr@777 | 271 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
ysr@777 | 272 | Generation* pg = g1h->perm_gen(); |
ysr@777 | 273 | |
ysr@777 | 274 | // Adjust the pointers to reflect the new locations |
ysr@777 | 275 | EventMark m("3 adjust pointers"); |
ysr@777 | 276 | TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty); |
ysr@777 | 277 | GenMarkSweep::trace("3"); |
ysr@777 | 278 | |
ysr@777 | 279 | SharedHeap* sh = SharedHeap::heap(); |
ysr@777 | 280 | |
jrose@1424 | 281 | sh->process_strong_roots(true, // activate StrongRootsScope |
jrose@1424 | 282 | true, // Collecting permanent generation. |
ysr@777 | 283 | SharedHeap::SO_AllClasses, |
ysr@777 | 284 | &GenMarkSweep::adjust_root_pointer_closure, |
jrose@1424 | 285 | NULL, // do not touch code cache here |
ysr@777 | 286 | &GenMarkSweep::adjust_pointer_closure); |
ysr@777 | 287 | |
ysr@777 | 288 | g1h->ref_processor()->weak_oops_do(&GenMarkSweep::adjust_root_pointer_closure); |
ysr@777 | 289 | |
ysr@777 | 290 | // Now adjust pointers in remaining weak roots. (All of which should |
ysr@777 | 291 | // have been cleared if they pointed to non-surviving objects.) |
ysr@777 | 292 | g1h->g1_process_weak_roots(&GenMarkSweep::adjust_root_pointer_closure, |
ysr@777 | 293 | &GenMarkSweep::adjust_pointer_closure); |
ysr@777 | 294 | |
ysr@777 | 295 | GenMarkSweep::adjust_marks(); |
ysr@777 | 296 | |
ysr@777 | 297 | G1AdjustPointersClosure blk; |
ysr@777 | 298 | g1h->heap_region_iterate(&blk); |
ysr@777 | 299 | pg->adjust_pointers(); |
ysr@777 | 300 | } |
ysr@777 | 301 | |
ysr@777 | 302 | class G1SpaceCompactClosure: public HeapRegionClosure { |
ysr@777 | 303 | public: |
ysr@777 | 304 | G1SpaceCompactClosure() {} |
ysr@777 | 305 | |
ysr@777 | 306 | bool doHeapRegion(HeapRegion* hr) { |
ysr@777 | 307 | if (hr->isHumongous()) { |
ysr@777 | 308 | if (hr->startsHumongous()) { |
ysr@777 | 309 | oop obj = oop(hr->bottom()); |
ysr@777 | 310 | if (obj->is_gc_marked()) { |
ysr@777 | 311 | obj->init_mark(); |
ysr@777 | 312 | } else { |
ysr@777 | 313 | assert(hr->is_empty(), "Should have been cleared in phase 2."); |
ysr@777 | 314 | } |
ysr@777 | 315 | hr->reset_during_compaction(); |
ysr@777 | 316 | } |
ysr@777 | 317 | } else { |
ysr@777 | 318 | hr->compact(); |
ysr@777 | 319 | } |
ysr@777 | 320 | return false; |
ysr@777 | 321 | } |
ysr@777 | 322 | }; |
ysr@777 | 323 | |
ysr@777 | 324 | void G1MarkSweep::mark_sweep_phase4() { |
ysr@777 | 325 | // All pointers are now adjusted, move objects accordingly |
ysr@777 | 326 | |
ysr@777 | 327 | // It is imperative that we traverse perm_gen first in phase4. All |
ysr@777 | 328 | // classes must be allocated earlier than their instances, and traversing |
ysr@777 | 329 | // perm_gen first makes sure that all klassOops have moved to their new |
ysr@777 | 330 | // location before any instance does a dispatch through it's klass! |
ysr@777 | 331 | |
ysr@777 | 332 | // The ValidateMarkSweep live oops tracking expects us to traverse spaces |
ysr@777 | 333 | // in the same order in phase2, phase3 and phase4. We don't quite do that |
ysr@777 | 334 | // here (perm_gen first rather than last), so we tell the validate code |
ysr@777 | 335 | // to use a higher index (saved from phase2) when verifying perm_gen. |
ysr@777 | 336 | G1CollectedHeap* g1h = G1CollectedHeap::heap(); |
ysr@777 | 337 | Generation* pg = g1h->perm_gen(); |
ysr@777 | 338 | |
ysr@777 | 339 | EventMark m("4 compact heap"); |
ysr@777 | 340 | TraceTime tm("phase 4", PrintGC && Verbose, true, gclog_or_tty); |
ysr@777 | 341 | GenMarkSweep::trace("4"); |
ysr@777 | 342 | |
ysr@777 | 343 | pg->compact(); |
ysr@777 | 344 | |
ysr@777 | 345 | G1SpaceCompactClosure blk; |
ysr@777 | 346 | g1h->heap_region_iterate(&blk); |
ysr@777 | 347 | |
ysr@777 | 348 | } |
ysr@777 | 349 | |
ysr@777 | 350 | // Local Variables: *** |
ysr@777 | 351 | // c-indentation-style: gnu *** |
ysr@777 | 352 | // End: *** |