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

Tue, 17 Oct 2017 12:58:25 +0800

author
aoqi
date
Tue, 17 Oct 2017 12:58:25 +0800
changeset 7994
04ff2f6cd0eb
parent 7659
38d6febe66af
parent 7535
7ae4e26cb1e0
child 9448
73d689add964
permissions
-rw-r--r--

merge

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

mercurial