1 /*
  2  * Copyright (c) 2017, 2024, 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/g1FullCollector.inline.hpp"
 27 #include "gc/g1/g1FullGCCompactionPoint.hpp"
 28 #include "gc/g1/g1HeapRegion.hpp"
 29 #include "gc/shared/fullGCForwarding.inline.hpp"
 30 #include "gc/shared/preservedMarks.inline.hpp"
 31 #include "oops/oop.inline.hpp"
 32 #include "utilities/debug.hpp"
 33 
 34 G1FullGCCompactionPoint::G1FullGCCompactionPoint(G1FullCollector* collector, PreservedMarks* preserved_stack) :
 35     _collector(collector),
 36     _current_region(nullptr),
 37     _compaction_top(nullptr),
 38     _preserved_stack(preserved_stack) {
 39   _compaction_regions = new (mtGC) GrowableArray<G1HeapRegion*>(32, mtGC);
 40   _compaction_region_iterator = _compaction_regions->begin();
 41 }
 42 
 43 G1FullGCCompactionPoint::~G1FullGCCompactionPoint() {
 44   delete _compaction_regions;
 45 }
 46 
 47 void G1FullGCCompactionPoint::update() {
 48   if (is_initialized()) {
 49     _collector->set_compaction_top(_current_region, _compaction_top);
 50   }
 51 }
 52 
 53 void G1FullGCCompactionPoint::initialize_values() {
 54   _compaction_top = _collector->compaction_top(_current_region);
 55 }
 56 
 57 bool G1FullGCCompactionPoint::has_regions() {
 58   return !_compaction_regions->is_empty();
 59 }
 60 
 61 bool G1FullGCCompactionPoint::is_initialized() {
 62   return _current_region != nullptr;
 63 }
 64 
 65 void G1FullGCCompactionPoint::initialize(G1HeapRegion* hr) {
 66   _current_region = hr;
 67   initialize_values();
 68 }
 69 
 70 G1HeapRegion* G1FullGCCompactionPoint::current_region() {
 71   return *_compaction_region_iterator;
 72 }
 73 
 74 G1HeapRegion* G1FullGCCompactionPoint::next_region() {
 75   G1HeapRegion* next = *(++_compaction_region_iterator);
 76   assert(next != nullptr, "Must return valid region");
 77   return next;
 78 }
 79 
 80 GrowableArray<G1HeapRegion*>* G1FullGCCompactionPoint::regions() {
 81   return _compaction_regions;
 82 }
 83 
 84 bool G1FullGCCompactionPoint::object_will_fit(size_t size) {
 85   size_t space_left = pointer_delta(_current_region->end(), _compaction_top);
 86   return size <= space_left;
 87 }
 88 
 89 void G1FullGCCompactionPoint::switch_region() {
 90   // Save compaction top in the region.
 91   _collector->set_compaction_top(_current_region, _compaction_top);
 92   // Get the next region and re-initialize the values.
 93   _current_region = next_region();
 94   initialize_values();
 95 }
 96 
 97 void G1FullGCCompactionPoint::forward(oop object, size_t size) {
 98   assert(_current_region != nullptr, "Must have been initialized");
 99 
100   size_t old_size = size;
101   size_t new_size = object->copy_size(old_size, object->mark());
102   size = cast_from_oop<HeapWord*>(object) != _compaction_top ? new_size : old_size;
103 
104   // Ensure the object fit in the current region.
105   while (!object_will_fit(size)) {
106     switch_region();
107     size = cast_from_oop<HeapWord*>(object) != _compaction_top ? new_size : old_size;
108   }
109 
110   // Store a forwarding pointer if the object should be moved.
111   if (cast_from_oop<HeapWord*>(object) != _compaction_top) {
112     if (!object->is_forwarded()) {
113       preserved_stack()->push_if_necessary(object, object->mark());
114     }
115     FullGCForwarding::forward_to(object, cast_to_oop(_compaction_top));
116     assert(FullGCForwarding::is_forwarded(object), "must be forwarded");
117   } else {
118     assert(!FullGCForwarding::is_forwarded(object), "must not be forwarded");
119   }
120 
121   // Update compaction values.
122   _compaction_top += size;
123   _current_region->update_bot_for_block(_compaction_top - size, _compaction_top);
124 }
125 
126 void G1FullGCCompactionPoint::add(G1HeapRegion* hr) {
127   _compaction_regions->append(hr);
128 }
129 
130 void G1FullGCCompactionPoint::remove_at_or_above(uint bottom) {
131   G1HeapRegion* cur = current_region();
132   assert(cur->hrm_index() >= bottom, "Sanity!");
133 
134   int start_index = 0;
135   for (G1HeapRegion* r : *_compaction_regions) {
136     if (r->hrm_index() < bottom) {
137       start_index++;
138     }
139   }
140 
141   assert(start_index >= 0, "Should have at least one region");
142   _compaction_regions->trunc_to(start_index);
143 }
144 
145 void G1FullGCCompactionPoint::add_humongous(G1HeapRegion* hr) {
146   assert(hr->is_starts_humongous(), "Sanity!");
147 
148   _collector->add_humongous_region(hr);
149 
150   G1CollectedHeap* g1h = G1CollectedHeap::heap();
151   g1h->humongous_obj_regions_iterate(hr,
152                                      [&] (G1HeapRegion* r) {
153                                        add(r);
154                                        _collector->update_from_skip_compacting_to_compacting(r->hrm_index());
155                                      });
156 }
157 
158 void G1FullGCCompactionPoint::forward_humongous(G1HeapRegion* hr) {
159   assert(hr->is_starts_humongous(), "Sanity!");
160 
161   oop obj = cast_to_oop(hr->bottom());
162   size_t old_size = obj->size();
163   size_t new_size = obj->copy_size(old_size, obj->mark());
164   uint num_regions = (uint)G1CollectedHeap::humongous_obj_size_in_regions(new_size);
165 
166   if (!has_regions()) {
167     return;
168   }
169 
170   // Find contiguous compaction target regions for the humongous object.
171   uint range_begin = find_contiguous_before(hr, num_regions);
172 
173   if (range_begin == UINT_MAX) {
174     // No contiguous compaction target regions found, so the object cannot be moved.
175     return;
176   }
177 
178   // Preserve the mark for the humongous object as the region was initially not compacting.
179   preserved_stack()->push_if_necessary(obj, obj->mark());
180 
181   G1HeapRegion* dest_hr = _compaction_regions->at(range_begin);
182   assert(hr->bottom() != dest_hr->bottom(), "assuming actual humongous move");
183   FullGCForwarding::forward_to(obj, cast_to_oop(dest_hr->bottom()));
184   assert(FullGCForwarding::is_forwarded(obj), "Object must be forwarded!");
185 
186   // Add the humongous object regions to the compaction point.
187   add_humongous(hr);
188 
189   // Remove covered regions from compaction target candidates.
190   _compaction_regions->remove_range(range_begin, (range_begin + num_regions));
191 
192   return;
193 }
194 
195 uint G1FullGCCompactionPoint::find_contiguous_before(G1HeapRegion* hr, uint num_regions) {
196   assert(num_regions > 0, "Sanity!");
197   assert(has_regions(), "Sanity!");
198 
199   if (num_regions == 1) {
200     // If only one region, return the first region.
201     return 0;
202   }
203 
204   uint contiguous_region_count = 1;
205 
206   uint range_end = 1;
207   uint range_limit = (uint)_compaction_regions->length();
208 
209   for (; range_end < range_limit; range_end++) {
210     if (contiguous_region_count == num_regions) {
211       break;
212     }
213     // Check if the current region and the previous region are contiguous.
214     bool regions_are_contiguous = (_compaction_regions->at(range_end)->hrm_index() - _compaction_regions->at(range_end - 1)->hrm_index()) == 1;
215     contiguous_region_count = regions_are_contiguous ? contiguous_region_count + 1 : 1;
216   }
217 
218   if (contiguous_region_count < num_regions &&
219       hr->hrm_index() - _compaction_regions->at(range_end-1)->hrm_index() != 1) {
220     // We reached the end but the final region is not contiguous with the target region;
221     // no contiguous regions to move to.
222     return UINT_MAX;
223   }
224   // Return the index of the first region in the range of contiguous regions.
225   return range_end - contiguous_region_count;
226 }