src/share/vm/gc_implementation/g1/g1MarkSweep.cpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/javaClasses.hpp"
aoqi@0 27 #include "classfile/symbolTable.hpp"
aoqi@0 28 #include "classfile/systemDictionary.hpp"
aoqi@0 29 #include "classfile/vmSymbols.hpp"
aoqi@0 30 #include "code/codeCache.hpp"
aoqi@0 31 #include "code/icBuffer.hpp"
aoqi@0 32 #include "gc_implementation/g1/g1Log.hpp"
aoqi@0 33 #include "gc_implementation/g1/g1MarkSweep.hpp"
aoqi@0 34 #include "gc_implementation/g1/g1StringDedup.hpp"
aoqi@0 35 #include "gc_implementation/shared/gcHeapSummary.hpp"
aoqi@0 36 #include "gc_implementation/shared/gcTimer.hpp"
aoqi@0 37 #include "gc_implementation/shared/gcTrace.hpp"
aoqi@0 38 #include "gc_implementation/shared/gcTraceTime.hpp"
aoqi@0 39 #include "memory/gcLocker.hpp"
aoqi@0 40 #include "memory/genCollectedHeap.hpp"
aoqi@0 41 #include "memory/modRefBarrierSet.hpp"
aoqi@0 42 #include "memory/referencePolicy.hpp"
aoqi@0 43 #include "memory/space.hpp"
aoqi@0 44 #include "oops/instanceRefKlass.hpp"
aoqi@0 45 #include "oops/oop.inline.hpp"
aoqi@0 46 #include "prims/jvmtiExport.hpp"
aoqi@0 47 #include "runtime/biasedLocking.hpp"
aoqi@0 48 #include "runtime/fprofiler.hpp"
aoqi@0 49 #include "runtime/synchronizer.hpp"
aoqi@0 50 #include "runtime/thread.hpp"
aoqi@0 51 #include "runtime/vmThread.hpp"
aoqi@0 52 #include "utilities/copy.hpp"
aoqi@0 53 #include "utilities/events.hpp"
aoqi@0 54
aoqi@0 55 class HeapRegion;
aoqi@0 56
aoqi@0 57 void G1MarkSweep::invoke_at_safepoint(ReferenceProcessor* rp,
aoqi@0 58 bool clear_all_softrefs) {
aoqi@0 59 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
aoqi@0 60
aoqi@0 61 SharedHeap* sh = SharedHeap::heap();
aoqi@0 62 #ifdef ASSERT
aoqi@0 63 if (sh->collector_policy()->should_clear_all_soft_refs()) {
aoqi@0 64 assert(clear_all_softrefs, "Policy should have been checked earler");
aoqi@0 65 }
aoqi@0 66 #endif
aoqi@0 67 // hook up weak ref data so it can be used during Mark-Sweep
aoqi@0 68 assert(GenMarkSweep::ref_processor() == NULL, "no stomping");
aoqi@0 69 assert(rp != NULL, "should be non-NULL");
aoqi@0 70 assert(rp == G1CollectedHeap::heap()->ref_processor_stw(), "Precondition");
aoqi@0 71
aoqi@0 72 GenMarkSweep::_ref_processor = rp;
aoqi@0 73 rp->setup_policy(clear_all_softrefs);
aoqi@0 74
aoqi@0 75 // When collecting the permanent generation Method*s may be moving,
aoqi@0 76 // so we either have to flush all bcp data or convert it into bci.
aoqi@0 77 CodeCache::gc_prologue();
aoqi@0 78 Threads::gc_prologue();
aoqi@0 79
aoqi@0 80 bool marked_for_unloading = false;
aoqi@0 81
aoqi@0 82 allocate_stacks();
aoqi@0 83
aoqi@0 84 // We should save the marks of the currently locked biased monitors.
aoqi@0 85 // The marking doesn't preserve the marks of biased objects.
aoqi@0 86 BiasedLocking::preserve_marks();
aoqi@0 87
aoqi@0 88 mark_sweep_phase1(marked_for_unloading, clear_all_softrefs);
aoqi@0 89
aoqi@0 90 mark_sweep_phase2();
aoqi@0 91
aoqi@0 92 // Don't add any more derived pointers during phase3
aoqi@0 93 COMPILER2_PRESENT(DerivedPointerTable::set_active(false));
aoqi@0 94
aoqi@0 95 mark_sweep_phase3();
aoqi@0 96
aoqi@0 97 mark_sweep_phase4();
aoqi@0 98
aoqi@0 99 GenMarkSweep::restore_marks();
aoqi@0 100 BiasedLocking::restore_marks();
aoqi@0 101 GenMarkSweep::deallocate_stacks();
aoqi@0 102
aoqi@0 103 // "free at last gc" is calculated from these.
aoqi@0 104 // CHF: cheating for now!!!
aoqi@0 105 // Universe::set_heap_capacity_at_last_gc(Universe::heap()->capacity());
aoqi@0 106 // Universe::set_heap_used_at_last_gc(Universe::heap()->used());
aoqi@0 107
aoqi@0 108 Threads::gc_epilogue();
aoqi@0 109 CodeCache::gc_epilogue();
aoqi@0 110 JvmtiExport::gc_epilogue();
aoqi@0 111
aoqi@0 112 // refs processing: clean slate
aoqi@0 113 GenMarkSweep::_ref_processor = NULL;
aoqi@0 114 }
aoqi@0 115
aoqi@0 116
aoqi@0 117 void G1MarkSweep::allocate_stacks() {
aoqi@0 118 GenMarkSweep::_preserved_count_max = 0;
aoqi@0 119 GenMarkSweep::_preserved_marks = NULL;
aoqi@0 120 GenMarkSweep::_preserved_count = 0;
aoqi@0 121 }
aoqi@0 122
aoqi@0 123 void G1MarkSweep::mark_sweep_phase1(bool& marked_for_unloading,
aoqi@0 124 bool clear_all_softrefs) {
aoqi@0 125 // Recursively traverse all live objects and mark them
aoqi@0 126 GCTraceTime tm("phase 1", G1Log::fine() && Verbose, true, gc_timer());
aoqi@0 127 GenMarkSweep::trace(" 1");
aoqi@0 128
aoqi@0 129 SharedHeap* sh = SharedHeap::heap();
aoqi@0 130
aoqi@0 131 // Need cleared claim bits for the strong roots processing
aoqi@0 132 ClassLoaderDataGraph::clear_claimed_marks();
aoqi@0 133
aoqi@0 134 sh->process_strong_roots(true, // activate StrongRootsScope
aoqi@0 135 false, // not scavenging.
aoqi@0 136 SharedHeap::SO_SystemClasses,
aoqi@0 137 &GenMarkSweep::follow_root_closure,
aoqi@0 138 &GenMarkSweep::follow_code_root_closure,
aoqi@0 139 &GenMarkSweep::follow_klass_closure);
aoqi@0 140
aoqi@0 141 // Process reference objects found during marking
aoqi@0 142 ReferenceProcessor* rp = GenMarkSweep::ref_processor();
aoqi@0 143 assert(rp == G1CollectedHeap::heap()->ref_processor_stw(), "Sanity");
aoqi@0 144
aoqi@0 145 rp->setup_policy(clear_all_softrefs);
aoqi@0 146 const ReferenceProcessorStats& stats =
aoqi@0 147 rp->process_discovered_references(&GenMarkSweep::is_alive,
aoqi@0 148 &GenMarkSweep::keep_alive,
aoqi@0 149 &GenMarkSweep::follow_stack_closure,
aoqi@0 150 NULL,
aoqi@0 151 gc_timer());
aoqi@0 152 gc_tracer()->report_gc_reference_stats(stats);
aoqi@0 153
aoqi@0 154
aoqi@0 155 // This is the point where the entire marking should have completed.
aoqi@0 156 assert(GenMarkSweep::_marking_stack.is_empty(), "Marking should have completed");
aoqi@0 157
aoqi@0 158 // Unload classes and purge the SystemDictionary.
aoqi@0 159 bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive);
aoqi@0 160
aoqi@0 161 // Unload nmethods.
aoqi@0 162 CodeCache::do_unloading(&GenMarkSweep::is_alive, purged_class);
aoqi@0 163
aoqi@0 164 // Prune dead klasses from subklass/sibling/implementor lists.
aoqi@0 165 Klass::clean_weak_klass_links(&GenMarkSweep::is_alive);
aoqi@0 166
aoqi@0 167 // Delete entries for dead interned string and clean up unreferenced symbols in symbol table.
aoqi@0 168 G1CollectedHeap::heap()->unlink_string_and_symbol_table(&GenMarkSweep::is_alive);
aoqi@0 169
aoqi@0 170 if (VerifyDuringGC) {
aoqi@0 171 HandleMark hm; // handle scope
aoqi@0 172 COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact);
aoqi@0 173 Universe::heap()->prepare_for_verify();
aoqi@0 174 // Note: we can verify only the heap here. When an object is
aoqi@0 175 // marked, the previous value of the mark word (including
aoqi@0 176 // identity hash values, ages, etc) is preserved, and the mark
aoqi@0 177 // word is set to markOop::marked_value - effectively removing
aoqi@0 178 // any hash values from the mark word. These hash values are
aoqi@0 179 // used when verifying the dictionaries and so removing them
aoqi@0 180 // from the mark word can make verification of the dictionaries
aoqi@0 181 // fail. At the end of the GC, the orginal mark word values
aoqi@0 182 // (including hash values) are restored to the appropriate
aoqi@0 183 // objects.
aoqi@0 184 if (!VerifySilently) {
aoqi@0 185 gclog_or_tty->print(" VerifyDuringGC:(full)[Verifying ");
aoqi@0 186 }
aoqi@0 187 Universe::heap()->verify(VerifySilently, VerifyOption_G1UseMarkWord);
aoqi@0 188 if (!VerifySilently) {
aoqi@0 189 gclog_or_tty->print_cr("]");
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 gc_tracer()->report_object_count_after_gc(&GenMarkSweep::is_alive);
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 class G1PrepareCompactClosure: public HeapRegionClosure {
aoqi@0 197 G1CollectedHeap* _g1h;
aoqi@0 198 ModRefBarrierSet* _mrbs;
aoqi@0 199 CompactPoint _cp;
aoqi@0 200 HeapRegionSetCount _humongous_regions_removed;
aoqi@0 201
aoqi@0 202 void free_humongous_region(HeapRegion* hr) {
aoqi@0 203 HeapWord* end = hr->end();
aoqi@0 204 FreeRegionList dummy_free_list("Dummy Free List for G1MarkSweep");
aoqi@0 205
aoqi@0 206 assert(hr->startsHumongous(),
aoqi@0 207 "Only the start of a humongous region should be freed.");
aoqi@0 208
aoqi@0 209 hr->set_containing_set(NULL);
aoqi@0 210 _humongous_regions_removed.increment(1u, hr->capacity());
aoqi@0 211
aoqi@0 212 _g1h->free_humongous_region(hr, &dummy_free_list, false /* par */);
aoqi@0 213 hr->prepare_for_compaction(&_cp);
aoqi@0 214 // Also clear the part of the card table that will be unused after
aoqi@0 215 // compaction.
aoqi@0 216 _mrbs->clear(MemRegion(hr->compaction_top(), end));
aoqi@0 217 dummy_free_list.remove_all();
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 public:
aoqi@0 221 G1PrepareCompactClosure(CompactibleSpace* cs)
aoqi@0 222 : _g1h(G1CollectedHeap::heap()),
aoqi@0 223 _mrbs(_g1h->g1_barrier_set()),
aoqi@0 224 _cp(NULL, cs, cs->initialize_threshold()),
aoqi@0 225 _humongous_regions_removed() { }
aoqi@0 226
aoqi@0 227 void update_sets() {
aoqi@0 228 // We'll recalculate total used bytes and recreate the free list
aoqi@0 229 // at the end of the GC, so no point in updating those values here.
aoqi@0 230 HeapRegionSetCount empty_set;
aoqi@0 231 _g1h->remove_from_old_sets(empty_set, _humongous_regions_removed);
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 bool doHeapRegion(HeapRegion* hr) {
aoqi@0 235 if (hr->isHumongous()) {
aoqi@0 236 if (hr->startsHumongous()) {
aoqi@0 237 oop obj = oop(hr->bottom());
aoqi@0 238 if (obj->is_gc_marked()) {
aoqi@0 239 obj->forward_to(obj);
aoqi@0 240 } else {
aoqi@0 241 free_humongous_region(hr);
aoqi@0 242 }
aoqi@0 243 } else {
aoqi@0 244 assert(hr->continuesHumongous(), "Invalid humongous.");
aoqi@0 245 }
aoqi@0 246 } else {
aoqi@0 247 hr->prepare_for_compaction(&_cp);
aoqi@0 248 // Also clear the part of the card table that will be unused after
aoqi@0 249 // compaction.
aoqi@0 250 _mrbs->clear(MemRegion(hr->compaction_top(), hr->end()));
aoqi@0 251 }
aoqi@0 252 return false;
aoqi@0 253 }
aoqi@0 254 };
aoqi@0 255
aoqi@0 256 void G1MarkSweep::mark_sweep_phase2() {
aoqi@0 257 // Now all live objects are marked, compute the new object addresses.
aoqi@0 258
aoqi@0 259 // It is not required that we traverse spaces in the same order in
aoqi@0 260 // phase2, phase3 and phase4, but the ValidateMarkSweep live oops
aoqi@0 261 // tracking expects us to do so. See comment under phase4.
aoqi@0 262
aoqi@0 263 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 264
aoqi@0 265 GCTraceTime tm("phase 2", G1Log::fine() && Verbose, true, gc_timer());
aoqi@0 266 GenMarkSweep::trace("2");
aoqi@0 267
aoqi@0 268 // find the first region
aoqi@0 269 HeapRegion* r = g1h->region_at(0);
aoqi@0 270 CompactibleSpace* sp = r;
aoqi@0 271 if (r->isHumongous() && oop(r->bottom())->is_gc_marked()) {
aoqi@0 272 sp = r->next_compaction_space();
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 G1PrepareCompactClosure blk(sp);
aoqi@0 276 g1h->heap_region_iterate(&blk);
aoqi@0 277 blk.update_sets();
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 class G1AdjustPointersClosure: public HeapRegionClosure {
aoqi@0 281 public:
aoqi@0 282 bool doHeapRegion(HeapRegion* r) {
aoqi@0 283 if (r->isHumongous()) {
aoqi@0 284 if (r->startsHumongous()) {
aoqi@0 285 // We must adjust the pointers on the single H object.
aoqi@0 286 oop obj = oop(r->bottom());
aoqi@0 287 // point all the oops to the new location
aoqi@0 288 obj->adjust_pointers();
aoqi@0 289 }
aoqi@0 290 } else {
aoqi@0 291 // This really ought to be "as_CompactibleSpace"...
aoqi@0 292 r->adjust_pointers();
aoqi@0 293 }
aoqi@0 294 return false;
aoqi@0 295 }
aoqi@0 296 };
aoqi@0 297
aoqi@0 298 void G1MarkSweep::mark_sweep_phase3() {
aoqi@0 299 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 300
aoqi@0 301 // Adjust the pointers to reflect the new locations
aoqi@0 302 GCTraceTime tm("phase 3", G1Log::fine() && Verbose, true, gc_timer());
aoqi@0 303 GenMarkSweep::trace("3");
aoqi@0 304
aoqi@0 305 SharedHeap* sh = SharedHeap::heap();
aoqi@0 306
aoqi@0 307 // Need cleared claim bits for the strong roots processing
aoqi@0 308 ClassLoaderDataGraph::clear_claimed_marks();
aoqi@0 309
aoqi@0 310 sh->process_strong_roots(true, // activate StrongRootsScope
aoqi@0 311 false, // not scavenging.
aoqi@0 312 SharedHeap::SO_AllClasses,
aoqi@0 313 &GenMarkSweep::adjust_pointer_closure,
aoqi@0 314 NULL, // do not touch code cache here
aoqi@0 315 &GenMarkSweep::adjust_klass_closure);
aoqi@0 316
aoqi@0 317 assert(GenMarkSweep::ref_processor() == g1h->ref_processor_stw(), "Sanity");
aoqi@0 318 g1h->ref_processor_stw()->weak_oops_do(&GenMarkSweep::adjust_pointer_closure);
aoqi@0 319
aoqi@0 320 // Now adjust pointers in remaining weak roots. (All of which should
aoqi@0 321 // have been cleared if they pointed to non-surviving objects.)
aoqi@0 322 g1h->g1_process_weak_roots(&GenMarkSweep::adjust_pointer_closure);
aoqi@0 323
aoqi@0 324 if (G1StringDedup::is_enabled()) {
aoqi@0 325 G1StringDedup::oops_do(&GenMarkSweep::adjust_pointer_closure);
aoqi@0 326 }
aoqi@0 327
aoqi@0 328 GenMarkSweep::adjust_marks();
aoqi@0 329
aoqi@0 330 G1AdjustPointersClosure blk;
aoqi@0 331 g1h->heap_region_iterate(&blk);
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 class G1SpaceCompactClosure: public HeapRegionClosure {
aoqi@0 335 public:
aoqi@0 336 G1SpaceCompactClosure() {}
aoqi@0 337
aoqi@0 338 bool doHeapRegion(HeapRegion* hr) {
aoqi@0 339 if (hr->isHumongous()) {
aoqi@0 340 if (hr->startsHumongous()) {
aoqi@0 341 oop obj = oop(hr->bottom());
aoqi@0 342 if (obj->is_gc_marked()) {
aoqi@0 343 obj->init_mark();
aoqi@0 344 } else {
aoqi@0 345 assert(hr->is_empty(), "Should have been cleared in phase 2.");
aoqi@0 346 }
aoqi@0 347 hr->reset_during_compaction();
aoqi@0 348 }
aoqi@0 349 } else {
aoqi@0 350 hr->compact();
aoqi@0 351 }
aoqi@0 352 return false;
aoqi@0 353 }
aoqi@0 354 };
aoqi@0 355
aoqi@0 356 void G1MarkSweep::mark_sweep_phase4() {
aoqi@0 357 // All pointers are now adjusted, move objects accordingly
aoqi@0 358
aoqi@0 359 // The ValidateMarkSweep live oops tracking expects us to traverse spaces
aoqi@0 360 // in the same order in phase2, phase3 and phase4. We don't quite do that
aoqi@0 361 // here (code and comment not fixed for perm removal), so we tell the validate code
aoqi@0 362 // to use a higher index (saved from phase2) when verifying perm_gen.
aoqi@0 363 G1CollectedHeap* g1h = G1CollectedHeap::heap();
aoqi@0 364
aoqi@0 365 GCTraceTime tm("phase 4", G1Log::fine() && Verbose, true, gc_timer());
aoqi@0 366 GenMarkSweep::trace("4");
aoqi@0 367
aoqi@0 368 G1SpaceCompactClosure blk;
aoqi@0 369 g1h->heap_region_iterate(&blk);
aoqi@0 370
aoqi@0 371 }

mercurial