1 /*
  2  * Copyright (c) 2017, 2022, 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.inline.hpp"
 27 #include "gc/g1/g1ConcurrentMarkBitMap.inline.hpp"
 28 #include "gc/g1/g1FullCollector.inline.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.inline.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 "gc/shared/slidingForwarding.inline.hpp"
 38 #include "logging/log.hpp"
 39 #include "memory/iterator.inline.hpp"
 40 #include "oops/oop.inline.hpp"
 41 #include "utilities/ticks.hpp"
 42 
 43 G1DetermineCompactionQueueClosure::G1DetermineCompactionQueueClosure(G1FullCollector* collector) :
 44   _g1h(G1CollectedHeap::heap()),
 45   _collector(collector),
 46   _cur_worker(0) { }
 47 
 48 bool G1FullGCPrepareTask::G1CalculatePointersClosure::do_heap_region(HeapRegion* hr) {
 49   uint region_idx = hr->hrm_index();
 50   assert(_collector->is_compaction_target(region_idx), "must be");
 51 
 52   assert(!hr->is_pinned(), "must be");
 53   assert(!hr->is_closed_archive(), "must be");
 54   assert(!hr->is_open_archive(), "must be");
 55 
 56   prepare_for_compaction(hr);
 57 
 58   return false;
 59 }
 60 
 61 G1FullGCPrepareTask::G1FullGCPrepareTask(G1FullCollector* collector) :
 62     G1FullGCTask("G1 Prepare Compact Task", collector),
 63     _has_free_compaction_targets(false),
 64     _hrclaimer(collector->workers()) {
 65 }
 66 
 67 void G1FullGCPrepareTask::set_has_free_compaction_targets() {
 68   if (!_has_free_compaction_targets) {
 69     _has_free_compaction_targets = true;
 70   }
 71 }
 72 
 73 bool G1FullGCPrepareTask::has_free_compaction_targets() {
 74   return _has_free_compaction_targets;
 75 }
 76 
 77 void G1FullGCPrepareTask::work(uint worker_id) {
 78   Ticks start = Ticks::now();
 79   // Calculate the target locations for the objects in the non-free regions of
 80   // the compaction queues provided by the associate compaction point.
 81   {
 82     G1FullGCCompactionPoint* compaction_point = collector()->compaction_point(worker_id);
 83     G1CalculatePointersClosure closure(collector(), compaction_point);
 84 
 85     for (GrowableArrayIterator<HeapRegion*> it = compaction_point->regions()->begin();
 86          it != compaction_point->regions()->end();
 87          ++it) {
 88       closure.do_heap_region(*it);
 89     }
 90     compaction_point->update();
 91     // Determine if there are any unused compaction targets. This is only the case if
 92     // there are
 93     // - any regions in queue, so no free ones either.
 94     // - and the current region is not the last one in the list.
 95     if (compaction_point->has_regions() &&
 96         compaction_point->current_region() != compaction_point->regions()->last()) {
 97       set_has_free_compaction_targets();
 98     }
 99   }
100 
101   // Clear region metadata that is invalid after GC for all regions.
102   {
103     G1ResetMetadataClosure closure(collector());
104     G1CollectedHeap::heap()->heap_region_par_iterate_from_start(&closure, &_hrclaimer);
105   }
106   log_task("Prepare compaction task", worker_id, start);
107 }
108 
109 G1FullGCPrepareTask::G1CalculatePointersClosure::G1CalculatePointersClosure(G1FullCollector* collector,
110                                                                             G1FullGCCompactionPoint* cp) :
111   _g1h(G1CollectedHeap::heap()),
112   _collector(collector),
113   _bitmap(collector->mark_bitmap()),
114   _cp(cp) { }
115 
116 G1FullGCPrepareTask::G1ResetMetadataClosure::G1ResetMetadataClosure(G1FullCollector* collector) :
117   _g1h(G1CollectedHeap::heap()),
118   _collector(collector) { }
119 
120 void G1FullGCPrepareTask::G1ResetMetadataClosure::reset_region_metadata(HeapRegion* hr) {
121   hr->rem_set()->clear();
122   hr->clear_cardtable();
123 
124   G1HotCardCache* hcc = _g1h->hot_card_cache();
125   if (hcc->use_cache()) {
126     hcc->reset_card_counts(hr);
127   }
128 }
129 
130 bool G1FullGCPrepareTask::G1ResetMetadataClosure::do_heap_region(HeapRegion* hr) {
131   uint const region_idx = hr->hrm_index();
132   if (!_collector->is_compaction_target(region_idx)) {
133     assert(!hr->is_free(), "all free regions should be compaction targets");
134     assert(_collector->is_skip_compacting(region_idx) || hr->is_closed_archive(), "must be");
135     if (hr->is_young()) {
136       // G1 updates the BOT for old region contents incrementally, but young regions
137       // lack BOT information for performance reasons.
138       // Recreate BOT information of high live ratio young regions here to keep expected
139       // performance during scanning their card tables in the collection pauses later.
140       hr->update_bot();
141     }
142   }
143 
144   // Reset data structures not valid after Full GC.
145   reset_region_metadata(hr);
146 
147   return false;
148 }
149 
150 G1FullGCPrepareTask::G1PrepareCompactLiveClosure::G1PrepareCompactLiveClosure(G1FullGCCompactionPoint* cp) :
151     _cp(cp), _forwarding(G1CollectedHeap::heap()->forwarding()) { }
152 
153 size_t G1FullGCPrepareTask::G1PrepareCompactLiveClosure::apply(oop object) {
154   size_t size = object->size();
155   _cp->forward(_forwarding, object, size);
156   return size;
157 }
158 
159 void G1FullGCPrepareTask::G1CalculatePointersClosure::prepare_for_compaction(HeapRegion* hr) {
160   if (!_collector->is_free(hr->hrm_index())) {
161     G1PrepareCompactLiveClosure prepare_compact(_cp);
162     hr->apply_to_marked_objects(_bitmap, &prepare_compact);
163   }
164 }