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