1 /*
   2  * Copyright (c) 2017, 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 "code/codeCache.hpp"
  27 #include "gc/g1/g1CollectedHeap.hpp"
  28 #include "gc/g1/g1CollectorPolicy.hpp"
  29 #include "gc/g1/g1FullCollector.hpp"
  30 #include "gc/g1/g1FullGCAdjustTask.hpp"
  31 #include "gc/g1/g1FullGCCompactTask.hpp"
  32 #include "gc/g1/g1FullGCMarker.inline.hpp"
  33 #include "gc/g1/g1FullGCMarkTask.hpp"
  34 #include "gc/g1/g1FullGCPrepareTask.hpp"
  35 #include "gc/g1/g1FullGCReferenceProcessorExecutor.hpp"
  36 #include "gc/g1/g1FullGCScope.hpp"
  37 #include "gc/g1/g1OopClosures.hpp"
  38 #include "gc/g1/g1Policy.hpp"
  39 #include "gc/g1/g1StringDedup.hpp"
  40 #include "gc/shared/gcTraceTime.inline.hpp"
  41 #include "gc/shared/preservedMarks.hpp"
  42 #include "gc/shared/referenceProcessor.hpp"
  43 #include "gc/shared/weakProcessor.hpp"
  44 #include "logging/log.hpp"
  45 #include "runtime/biasedLocking.hpp"
  46 #include "utilities/debug.hpp"
  47 
  48 static void clear_and_activate_derived_pointers() {
  49 #if COMPILER2_OR_JVMCI
  50   DerivedPointerTable::clear();
  51 #endif
  52 }
  53 
  54 static void deactivate_derived_pointers() {
  55 #if COMPILER2_OR_JVMCI
  56   DerivedPointerTable::set_active(false);
  57 #endif
  58 }
  59 
  60 static void update_derived_pointers() {
  61 #if COMPILER2_OR_JVMCI
  62   DerivedPointerTable::update_pointers();
  63 #endif
  64 }
  65 
  66 G1CMBitMap* G1FullCollector::mark_bitmap() {
  67   return _heap->concurrent_mark()->next_mark_bitmap();
  68 }
  69 
  70 ReferenceProcessor* G1FullCollector::reference_processor() {
  71   return _heap->ref_processor_stw();
  72 }
  73 
  74 G1FullCollector::G1FullCollector(G1CollectedHeap* heap, GCMemoryManager* mem_mgr, bool explicit_gc, bool clear_soft_refs) :
  75     _heap(heap),
  76     _scope(mem_mgr, explicit_gc, clear_soft_refs),
  77     _num_workers(heap->workers()->active_workers()),
  78     _oop_queue_set(_num_workers),
  79     _array_queue_set(_num_workers),
  80     _preserved_marks_set(true),
  81     _serial_compaction_point(),
  82     _is_alive(heap->concurrent_mark()->next_mark_bitmap()),
  83     _is_alive_mutator(heap->ref_processor_stw(), &_is_alive) {
  84   assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
  85 
  86   _preserved_marks_set.init(_num_workers);
  87   _markers = NEW_C_HEAP_ARRAY(G1FullGCMarker*, _num_workers, mtGC);
  88   _compaction_points = NEW_C_HEAP_ARRAY(G1FullGCCompactionPoint*, _num_workers, mtGC);
  89   for (uint i = 0; i < _num_workers; i++) {
  90     _markers[i] = new G1FullGCMarker(i, _preserved_marks_set.get(i), mark_bitmap());
  91     _compaction_points[i] = new G1FullGCCompactionPoint();
  92     _oop_queue_set.register_queue(i, marker(i)->oop_stack());
  93     _array_queue_set.register_queue(i, marker(i)->objarray_stack());
  94   }
  95 }
  96 
  97 G1FullCollector::~G1FullCollector() {
  98   for (uint i = 0; i < _num_workers; i++) {
  99     delete _markers[i];
 100     delete _compaction_points[i];
 101   }
 102   FREE_C_HEAP_ARRAY(G1FullGCMarker*, _markers);
 103   FREE_C_HEAP_ARRAY(G1FullGCCompactionPoint*, _compaction_points);
 104 }
 105 
 106 void G1FullCollector::prepare_collection() {
 107   _heap->g1_policy()->record_full_collection_start();
 108 
 109   _heap->print_heap_before_gc();
 110   _heap->print_heap_regions();
 111 
 112   _heap->abort_concurrent_cycle();
 113   _heap->verify_before_full_collection(scope()->is_explicit_gc());
 114 
 115   _heap->gc_prologue(true);
 116   _heap->prepare_heap_for_full_collection();
 117 
 118   reference_processor()->enable_discovery();
 119   reference_processor()->setup_policy(scope()->should_clear_soft_refs());
 120 
 121   // When collecting the permanent generation Method*s may be moving,
 122   // so we either have to flush all bcp data or convert it into bci.
 123   CodeCache::gc_prologue();
 124 
 125   // We should save the marks of the currently locked biased monitors.
 126   // The marking doesn't preserve the marks of biased objects.
 127   BiasedLocking::preserve_marks();
 128 
 129   // Clear and activate derived pointer collection.
 130   clear_and_activate_derived_pointers();
 131 }
 132 
 133 void G1FullCollector::collect() {
 134   phase1_mark_live_objects();
 135   verify_after_marking();
 136 
 137   // Don't add any more derived pointers during later phases
 138   deactivate_derived_pointers();
 139 
 140   phase2_prepare_compaction();
 141 
 142   phase3_adjust_pointers();
 143 
 144   phase4_do_compaction();
 145 }
 146 
 147 void G1FullCollector::complete_collection() {
 148   // Restore all marks.
 149   restore_marks();
 150 
 151   // When the pointers have been adjusted and moved, we can
 152   // update the derived pointer table.
 153   update_derived_pointers();
 154 
 155   BiasedLocking::restore_marks();
 156   CodeCache::gc_epilogue();
 157   JvmtiExport::gc_epilogue();
 158 
 159   _heap->prepare_heap_for_mutators();
 160 
 161   _heap->g1_policy()->record_full_collection_end();
 162   _heap->gc_epilogue(true);
 163 
 164   _heap->verify_after_full_collection();
 165 
 166   _heap->print_heap_after_full_collection(scope()->heap_transition());
 167 }
 168 
 169 void G1FullCollector::phase1_mark_live_objects() {
 170   // Recursively traverse all live objects and mark them.
 171   GCTraceTime(Info, gc, phases) info("Phase 1: Mark live objects", scope()->timer());
 172 
 173   // Do the actual marking.
 174   G1FullGCMarkTask marking_task(this);
 175   run_task(&marking_task);
 176 
 177   // Process references discovered during marking.
 178   G1FullGCReferenceProcessingExecutor reference_processing(this);
 179   reference_processing.execute(scope()->timer(), scope()->tracer());
 180 
 181   // Weak oops cleanup.
 182   {
 183     GCTraceTime(Debug, gc, phases) trace("Phase 1: Weak Processing", scope()->timer());
 184     WeakProcessor::weak_oops_do(&_is_alive, &do_nothing_cl);
 185   }
 186 
 187   // Class unloading and cleanup.
 188   if (ClassUnloading) {
 189     GCTraceTime(Debug, gc, phases) debug("Phase 1: Class Unloading and Cleanup", scope()->timer());
 190     // Unload classes and purge the SystemDictionary.
 191     bool purged_class = SystemDictionary::do_unloading(&_is_alive, scope()->timer());
 192     _heap->complete_cleaning(&_is_alive, purged_class);
 193   } else {
 194     GCTraceTime(Debug, gc, phases) debug("Phase 1: String and Symbol Tables Cleanup", scope()->timer());
 195     // If no class unloading just clean out strings and symbols.
 196     _heap->partial_cleaning(&_is_alive, true, true, G1StringDedup::is_enabled());
 197   }
 198 
 199   scope()->tracer()->report_object_count_after_gc(&_is_alive);
 200 }
 201 
 202 void G1FullCollector::prepare_compaction_common() {
 203   G1FullGCPrepareTask task(this);
 204   run_task(&task);
 205 
 206   // To avoid OOM when there is memory left.
 207   if (!task.has_freed_regions()) {
 208     task.prepare_serial_compaction();
 209   }
 210 }
 211 
 212 void G1FullCollector::phase2_prepare_compaction() {
 213   GCTraceTime(Info, gc, phases) info("Phase 2: Prepare for compaction", scope()->timer());
 214   prepare_compaction_ext(); // Will call prepare_compaction_common() above.
 215 }
 216 
 217 void G1FullCollector::phase3_adjust_pointers() {
 218   // Adjust the pointers to reflect the new locations
 219   GCTraceTime(Info, gc, phases) info("Phase 3: Adjust pointers and remembered sets", scope()->timer());
 220 
 221   G1FullGCAdjustTask task(this);
 222   run_task(&task);
 223 }
 224 
 225 void G1FullCollector::phase4_do_compaction() {
 226   // Compact the heap using the compaction queues created in phase 2.
 227   GCTraceTime(Info, gc, phases) info("Phase 4: Compact heap", scope()->timer());
 228   G1FullGCCompactTask task(this);
 229   run_task(&task);
 230 
 231   // Serial compact to avoid OOM when very few free regions.
 232   if (serial_compaction_point()->has_regions()) {
 233     task.serial_compaction();
 234   }
 235 }
 236 
 237 void G1FullCollector::restore_marks() {
 238   SharedRestorePreservedMarksTaskExecutor task_executor(_heap->workers());
 239   _preserved_marks_set.restore(&task_executor);
 240   _preserved_marks_set.reclaim();
 241 }
 242 
 243 void G1FullCollector::run_task(AbstractGangTask* task) {
 244   _heap->workers()->run_task(task, _num_workers);
 245 }
 246 
 247 void G1FullCollector::verify_after_marking() {
 248   if (!VerifyDuringGC) {
 249     //Only do verification if VerifyDuringGC is set.
 250     return;
 251   }
 252 
 253   HandleMark hm;  // handle scope
 254 #if COMPILER2_OR_JVMCI
 255   DerivedPointerTableDeactivate dpt_deact;
 256 #endif
 257   _heap->prepare_for_verify();
 258   // Note: we can verify only the heap here. When an object is
 259   // marked, the previous value of the mark word (including
 260   // identity hash values, ages, etc) is preserved, and the mark
 261   // word is set to markOop::marked_value - effectively removing
 262   // any hash values from the mark word. These hash values are
 263   // used when verifying the dictionaries and so removing them
 264   // from the mark word can make verification of the dictionaries
 265   // fail. At the end of the GC, the original mark word values
 266   // (including hash values) are restored to the appropriate
 267   // objects.
 268   GCTraceTime(Info, gc, verify)("During GC (full)");
 269   _heap->verify(VerifyOption_G1UseFullMarking);
 270 }