1 /*
   2  * Copyright (c) 2017, 2018, 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 "gc/g1/g1CollectedHeap.hpp"
  27 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
  28 #include "gc/g1/g1FullCollector.hpp"
  29 #include "gc/g1/g1FullGCCompactionPoint.hpp"
  30 #include "gc/g1/g1FullGCMarker.hpp"
  31 #include "gc/g1/g1FullGCOopClosures.inline.hpp"
  32 #include "gc/g1/g1FullGCPrepareTask.hpp"
  33 #include "gc/g1/g1HotCardCache.hpp"
  34 #include "gc/g1/heapRegion.inline.hpp"
  35 #include "gc/shared/gcTraceTime.inline.hpp"
  36 #include "gc/shared/referenceProcessor.hpp"
  37 #include "logging/log.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "utilities/ticks.inline.hpp"
  40 
  41 bool G1FullGCPrepareTask::G1CalculatePointersClosure::do_heap_region(HeapRegion* hr) {
  42   if (hr->is_humongous()) {
  43     oop obj = oop(hr->humongous_start_region()->bottom());
  44     if (_bitmap->is_marked(obj)) {
  45       if (hr->is_starts_humongous()) {
  46         obj->forward_to(obj);
  47       }
  48     } else {
  49       free_humongous_region(hr);
  50     }
  51   } else if (!hr->is_pinned()) {
  52     prepare_for_compaction(hr);
  53   }
  54 
  55   // Reset data structures not valid after Full GC.
  56   reset_region_metadata(hr);
  57 
  58   return false;
  59 }
  60 
  61 G1FullGCPrepareTask::G1FullGCPrepareTask(G1FullCollector* collector) :
  62     G1FullGCTask("G1 Prepare Compact Task", collector),
  63     _hrclaimer(collector->workers()),
  64     _freed_regions(false) {
  65 }
  66 
  67 void G1FullGCPrepareTask::set_freed_regions() {
  68   if (!_freed_regions) {
  69     _freed_regions = true;
  70   }
  71 }
  72 
  73 bool G1FullGCPrepareTask::has_freed_regions() {
  74   return _freed_regions;
  75 }
  76 
  77 void G1FullGCPrepareTask::work(uint worker_id) {
  78   Ticks start = Ticks::now();
  79   G1FullGCCompactionPoint* compaction_point = collector()->compaction_point(worker_id);
  80   G1CalculatePointersClosure closure(collector()->mark_bitmap(), compaction_point);
  81   G1CollectedHeap::heap()->heap_region_par_iterate_from_start(&closure, &_hrclaimer);
  82 
  83   // Update humongous region sets
  84   closure.update_sets();
  85   compaction_point->update();
  86 
  87   // Check if any regions was freed by this worker and store in task.
  88   if (closure.freed_regions()) {
  89     set_freed_regions();
  90   }
  91   log_task("Prepare compaction task", worker_id, start);
  92 }
  93 
  94 G1FullGCPrepareTask::G1CalculatePointersClosure::G1CalculatePointersClosure(G1CMBitMap* bitmap,
  95                                                                             G1FullGCCompactionPoint* cp) :
  96     _g1h(G1CollectedHeap::heap()),
  97     _bitmap(bitmap),
  98     _cp(cp),
  99     _humongous_regions_removed(0) { }
 100 
 101 void G1FullGCPrepareTask::G1CalculatePointersClosure::free_humongous_region(HeapRegion* hr) {
 102   FreeRegionList dummy_free_list("Dummy Free List for G1MarkSweep");
 103 
 104   hr->set_containing_set(NULL);
 105   _humongous_regions_removed++;
 106 
 107   _g1h->free_humongous_region(hr, &dummy_free_list, false /* skip_remset */);
 108   prepare_for_compaction(hr);
 109   dummy_free_list.remove_all();
 110 }
 111 
 112 void G1FullGCPrepareTask::G1CalculatePointersClosure::reset_region_metadata(HeapRegion* hr) {
 113   hr->reset_gc_time_stamp();
 114   hr->rem_set()->clear();
 115 
 116   _g1h->card_table()->clear(MemRegion(hr->bottom(), hr->end()));
 117 
 118   if (_g1h->g1_hot_card_cache()->use_cache()) {
 119     _g1h->g1_hot_card_cache()->reset_card_counts(hr);
 120   }
 121 }
 122 
 123 G1FullGCPrepareTask::G1PrepareCompactLiveClosure::G1PrepareCompactLiveClosure(G1FullGCCompactionPoint* cp) :
 124     _cp(cp) { }
 125 
 126 size_t G1FullGCPrepareTask::G1PrepareCompactLiveClosure::apply(oop object) {
 127   size_t size = object->size();
 128   _cp->forward(object, size);
 129   return size;
 130 }
 131 
 132 size_t G1FullGCPrepareTask::G1RePrepareClosure::apply(oop obj) {
 133   // We only re-prepare objects forwarded within the current region, so
 134   // skip objects that are already forwarded to another region.
 135   oop forwarded_to = obj->forwardee();
 136   if (forwarded_to != NULL && !_current->is_in(forwarded_to)) {
 137     return obj->size();
 138   }
 139 
 140   // Get size and forward.
 141   size_t size = obj->size();
 142   _cp->forward(obj, size);
 143 
 144   return size;
 145 }
 146 
 147 void G1FullGCPrepareTask::G1CalculatePointersClosure::prepare_for_compaction_work(G1FullGCCompactionPoint* cp,
 148                                                                                   HeapRegion* hr) {
 149   G1PrepareCompactLiveClosure prepare_compact(cp);
 150   hr->set_compaction_top(hr->bottom());
 151   hr->apply_to_marked_objects(_bitmap, &prepare_compact);
 152 }
 153 
 154 void G1FullGCPrepareTask::G1CalculatePointersClosure::prepare_for_compaction(HeapRegion* hr) {
 155   if (!_cp->is_initialized()) {
 156     hr->set_compaction_top(hr->bottom());
 157     _cp->initialize(hr, true);
 158   }
 159   // Add region to the compaction queue and prepare it.
 160   _cp->add(hr);
 161   prepare_for_compaction_work(_cp, hr);
 162 }
 163 
 164 void G1FullGCPrepareTask::prepare_serial_compaction() {
 165   GCTraceTime(Debug, gc, phases) debug("Phase 2: Prepare Serial Compaction", collector()->scope()->timer());
 166   // At this point we know that no regions were completely freed by
 167   // the parallel compaction. That means that the last region of
 168   // all compaction queues still have data in them. We try to compact
 169   // these regions in serial to avoid a premature OOM.
 170   for (uint i = 0; i < collector()->workers(); i++) {
 171     G1FullGCCompactionPoint* cp = collector()->compaction_point(i);
 172     if (cp->has_regions()) {
 173       collector()->serial_compaction_point()->add(cp->remove_last());
 174     }
 175   }
 176 
 177   // Update the forwarding information for the regions in the serial
 178   // compaction point.
 179   G1FullGCCompactionPoint* cp = collector()->serial_compaction_point();
 180   for (GrowableArrayIterator<HeapRegion*> it = cp->regions()->begin(); it != cp->regions()->end(); ++it) {
 181     HeapRegion* current = *it;
 182     if (!cp->is_initialized()) {
 183       // Initialize the compaction point. Nothing more is needed for the first heap region
 184       // since it is already prepared for compaction.
 185       cp->initialize(current, false);
 186     } else {
 187       assert(!current->is_humongous(), "Should be no humongous regions in compaction queue");
 188       G1RePrepareClosure re_prepare(cp, current);
 189       current->set_compaction_top(current->bottom());
 190       current->apply_to_marked_objects(collector()->mark_bitmap(), &re_prepare);
 191     }
 192   }
 193   cp->update();
 194 }
 195 
 196 void G1FullGCPrepareTask::G1CalculatePointersClosure::update_sets() {
 197   // We'll recalculate total used bytes and recreate the free list
 198   // at the end of the GC, so no point in updating those values here.
 199   _g1h->remove_from_old_sets(0, _humongous_regions_removed);
 200 }
 201 
 202 bool G1FullGCPrepareTask::G1CalculatePointersClosure::freed_regions() {
 203   if (_humongous_regions_removed > 0) {
 204     // Free regions from dead humongous regions.
 205     return true;
 206   }
 207 
 208   if (!_cp->has_regions()) {
 209     // No regions in queue, so no free ones either.
 210     return false;
 211   }
 212 
 213   if (_cp->current_region() != _cp->regions()->last()) {
 214     // The current region used for compaction is not the last in the
 215     // queue. That means there is at least one free region in the queue.
 216     return true;
 217   }
 218 
 219   // No free regions in the queue.
 220   return false;
 221 }