1 /*
  2  * Copyright (c) 2017, 2023, 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/heapRegion.inline.hpp"
 34 #include "gc/shared/gcTraceTime.inline.hpp"
 35 #include "gc/shared/referenceProcessor.hpp"
 36 #include "logging/log.hpp"
 37 #include "memory/iterator.inline.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "utilities/ticks.hpp"
 40 
 41 G1DetermineCompactionQueueClosure::G1DetermineCompactionQueueClosure(G1FullCollector* collector) :
 42   _g1h(G1CollectedHeap::heap()),
 43   _collector(collector),
 44   _cur_worker(0) { }
 45 
 46 bool G1FullGCPrepareTask::G1CalculatePointersClosure::do_heap_region(HeapRegion* hr) {
 47   uint region_idx = hr->hrm_index();
 48   assert(_collector->is_compaction_target(region_idx), "must be");
 49 
 50   assert(!hr->is_humongous(), "must be");
 51 
 52   prepare_for_compaction(hr);
 53 
 54   return false;
 55 }
 56 
 57 G1FullGCPrepareTask::G1FullGCPrepareTask(G1FullCollector* collector) :
 58     G1FullGCTask("G1 Prepare Compact Task", collector),
 59     _has_free_compaction_targets(false),
 60     _hrclaimer(collector->workers()) {
 61 }
 62 
 63 void G1FullGCPrepareTask::set_has_free_compaction_targets() {
 64   if (!_has_free_compaction_targets) {
 65     _has_free_compaction_targets = true;
 66   }
 67 }
 68 
 69 bool G1FullGCPrepareTask::has_free_compaction_targets() {
 70   return _has_free_compaction_targets;
 71 }
 72 
 73 void G1FullGCPrepareTask::work(uint worker_id) {
 74   Ticks start = Ticks::now();
 75   // Calculate the target locations for the objects in the non-free regions of
 76   // the compaction queues provided by the associate compaction point.
 77   {
 78     G1FullGCCompactionPoint* compaction_point = collector()->compaction_point(worker_id);
 79     G1CalculatePointersClosure closure(collector(), compaction_point);
 80 
 81     for (GrowableArrayIterator<HeapRegion*> it = compaction_point->regions()->begin();
 82          it != compaction_point->regions()->end();
 83          ++it) {
 84       closure.do_heap_region(*it);
 85     }
 86     compaction_point->update();
 87     // Determine if there are any unused compaction targets. This is only the case if
 88     // there are
 89     // - any regions in queue, so no free ones either.
 90     // - and the current region is not the last one in the list.
 91     if (compaction_point->has_regions() &&
 92         compaction_point->current_region() != compaction_point->regions()->last()) {
 93       set_has_free_compaction_targets();
 94     }
 95   }
 96   log_task("Prepare compaction task", worker_id, start);
 97 }
 98 
 99 G1FullGCPrepareTask::G1CalculatePointersClosure::G1CalculatePointersClosure(G1FullCollector* collector,
100                                                                             G1FullGCCompactionPoint* cp) :
101   _g1h(G1CollectedHeap::heap()),
102   _collector(collector),
103   _bitmap(collector->mark_bitmap()),
104   _cp(cp) { }
105 
106 
107 template <bool ALT_FWD>
108 G1FullGCPrepareTask::G1PrepareCompactLiveClosure<ALT_FWD>::G1PrepareCompactLiveClosure(G1FullGCCompactionPoint* cp) :
109     _cp(cp) { }
110 
111 template <bool ALT_FWD>
112 size_t G1FullGCPrepareTask::G1PrepareCompactLiveClosure<ALT_FWD>::apply(oop object) {
113   size_t size = object->size();
114   _cp->forward<ALT_FWD>(object, size);
115   return size;
116 }
117 
118 void G1FullGCPrepareTask::G1CalculatePointersClosure::prepare_for_compaction(HeapRegion* hr) {
119   if (!_collector->is_free(hr->hrm_index())) {
120     if (UseAltGCForwarding) {
121       G1PrepareCompactLiveClosure<true> prepare_compact(_cp);
122       hr->apply_to_marked_objects(_bitmap, &prepare_compact);
123     } else {
124       G1PrepareCompactLiveClosure<false> prepare_compact(_cp);
125       hr->apply_to_marked_objects(_bitmap, &prepare_compact);
126     }
127   }
128 }