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