< prev index next >

src/hotspot/share/gc/g1/g1FullGCCompactionPoint.cpp

Print this page

 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/preservedMarks.inline.hpp"

 30 #include "oops/oop.inline.hpp"
 31 #include "utilities/debug.hpp"
 32 
 33 G1FullGCCompactionPoint::G1FullGCCompactionPoint(G1FullCollector* collector, PreservedMarks* preserved_stack) :
 34     _collector(collector),
 35     _current_region(nullptr),
 36     _compaction_top(nullptr),
 37     _preserved_stack(preserved_stack) {
 38   _compaction_regions = new (mtGC) GrowableArray<HeapRegion*>(32, mtGC);
 39   _compaction_region_iterator = _compaction_regions->begin();
 40 }
 41 
 42 G1FullGCCompactionPoint::~G1FullGCCompactionPoint() {
 43   delete _compaction_regions;
 44 }
 45 
 46 void G1FullGCCompactionPoint::update() {
 47   if (is_initialized()) {
 48     _collector->set_compaction_top(_current_region, _compaction_top);
 49   }

 76   return next;
 77 }
 78 
 79 GrowableArray<HeapRegion*>* G1FullGCCompactionPoint::regions() {
 80   return _compaction_regions;
 81 }
 82 
 83 bool G1FullGCCompactionPoint::object_will_fit(size_t size) {
 84   size_t space_left = pointer_delta(_current_region->end(), _compaction_top);
 85   return size <= space_left;
 86 }
 87 
 88 void G1FullGCCompactionPoint::switch_region() {
 89   // Save compaction top in the region.
 90   _collector->set_compaction_top(_current_region, _compaction_top);
 91   // Get the next region and re-initialize the values.
 92   _current_region = next_region();
 93   initialize_values();
 94 }
 95 

 96 void G1FullGCCompactionPoint::forward(oop object, size_t size) {
 97   assert(_current_region != nullptr, "Must have been initialized");
 98 
 99   // Ensure the object fit in the current region.
100   while (!object_will_fit(size)) {
101     switch_region();
102   }
103 
104   // Store a forwarding pointer if the object should be moved.
105   if (cast_from_oop<HeapWord*>(object) != _compaction_top) {
106     if (!object->is_forwarded()) {
107       preserved_stack()->push_if_necessary(object, object->mark());
108     }
109     object->forward_to(cast_to_oop(_compaction_top));
110     assert(object->is_forwarded(), "must be forwarded");
111   } else {
112     assert(!object->is_forwarded(), "must not be forwarded");
113   }
114 
115   // Update compaction values.
116   _compaction_top += size;
117   _current_region->update_bot_for_block(_compaction_top - size, _compaction_top);
118 }
119 



120 void G1FullGCCompactionPoint::add(HeapRegion* hr) {
121   _compaction_regions->append(hr);
122 }
123 
124 void G1FullGCCompactionPoint::remove_at_or_above(uint bottom) {
125   HeapRegion* cur = current_region();
126   assert(cur->hrm_index() >= bottom, "Sanity!");
127 
128   int start_index = 0;
129   for (HeapRegion* r : *_compaction_regions) {
130     if (r->hrm_index() < bottom) {
131       start_index++;
132     }
133   }
134 
135   assert(start_index >= 0, "Should have at least one region");
136   _compaction_regions->trunc_to(start_index);
137 }
138 
139 void G1FullGCCompactionPoint::add_humongous(HeapRegion* hr) {
140   assert(hr->is_starts_humongous(), "Sanity!");
141 
142   _collector->add_humongous_region(hr);
143 
144   G1CollectedHeap* g1h = G1CollectedHeap::heap();
145   g1h->humongous_obj_regions_iterate(hr,
146                                      [&] (HeapRegion* r) {
147                                        add(r);
148                                        _collector->update_from_skip_compacting_to_compacting(r->hrm_index());
149                                      });
150 }
151 

152 void G1FullGCCompactionPoint::forward_humongous(HeapRegion* hr) {
153   assert(hr->is_starts_humongous(), "Sanity!");
154 
155   oop obj = cast_to_oop(hr->bottom());
156   size_t obj_size = obj->size();
157   uint num_regions = (uint)G1CollectedHeap::humongous_obj_size_in_regions(obj_size);
158 
159   if (!has_regions()) {
160     return;
161   }
162 
163   // Find contiguous compaction target regions for the humongous object.
164   uint range_begin = find_contiguous_before(hr, num_regions);
165 
166   if (range_begin == UINT_MAX) {
167     // No contiguous compaction target regions found, so the object cannot be moved.
168     return;
169   }
170 
171   // Preserve the mark for the humongous object as the region was initially not compacting.
172   preserved_stack()->push_if_necessary(obj, obj->mark());
173 
174   HeapRegion* dest_hr = _compaction_regions->at(range_begin);
175   obj->forward_to(cast_to_oop(dest_hr->bottom()));
176   assert(obj->is_forwarded(), "Object must be forwarded!");
177 
178   // Add the humongous object regions to the compaction point.
179   add_humongous(hr);
180 
181   // Remove covered regions from compaction target candidates.
182   _compaction_regions->remove_range(range_begin, (range_begin + num_regions));
183 
184   return;
185 }
186 



187 uint G1FullGCCompactionPoint::find_contiguous_before(HeapRegion* hr, uint num_regions) {
188   assert(num_regions > 0, "Sanity!");
189   assert(has_regions(), "Sanity!");
190 
191   if (num_regions == 1) {
192     // If only one region, return the first region.
193     return 0;
194   }
195 
196   uint contiguous_region_count = 1;
197 
198   uint range_end = 1;
199   uint range_limit = (uint)_compaction_regions->length();
200 
201   for (; range_end < range_limit; range_end++) {
202     if (contiguous_region_count == num_regions) {
203       break;
204     }
205     // Check if the current region and the previous region are contiguous.
206     bool regions_are_contiguous = (_compaction_regions->at(range_end)->hrm_index() - _compaction_regions->at(range_end - 1)->hrm_index()) == 1;

 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/preservedMarks.inline.hpp"
 30 #include "gc/shared/slidingForwarding.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<HeapRegion*>(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   }

 77   return next;
 78 }
 79 
 80 GrowableArray<HeapRegion*>* 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 template <bool ALT_FWD>
 98 void G1FullGCCompactionPoint::forward(oop object, size_t size) {
 99   assert(_current_region != nullptr, "Must have been initialized");
100 
101   // Ensure the object fit in the current region.
102   while (!object_will_fit(size)) {
103     switch_region();
104   }
105 
106   // Store a forwarding pointer if the object should be moved.
107   if (cast_from_oop<HeapWord*>(object) != _compaction_top) {
108     if (!object->is_forwarded()) {
109       preserved_stack()->push_if_necessary(object, object->mark());
110     }
111     SlidingForwarding::forward_to<ALT_FWD>(object, cast_to_oop(_compaction_top));
112     assert(SlidingForwarding::is_forwarded(object), "must be forwarded");
113   } else {
114     assert(SlidingForwarding::is_not_forwarded(object), "must not be forwarded");
115   }
116 
117   // Update compaction values.
118   _compaction_top += size;
119   _current_region->update_bot_for_block(_compaction_top - size, _compaction_top);
120 }
121 
122 template void G1FullGCCompactionPoint::forward<true>(oop object, size_t size);
123 template void G1FullGCCompactionPoint::forward<false>(oop object, size_t size);
124 
125 void G1FullGCCompactionPoint::add(HeapRegion* hr) {
126   _compaction_regions->append(hr);
127 }
128 
129 void G1FullGCCompactionPoint::remove_at_or_above(uint bottom) {
130   HeapRegion* cur = current_region();
131   assert(cur->hrm_index() >= bottom, "Sanity!");
132 
133   int start_index = 0;
134   for (HeapRegion* r : *_compaction_regions) {
135     if (r->hrm_index() < bottom) {
136       start_index++;
137     }
138   }
139 
140   assert(start_index >= 0, "Should have at least one region");
141   _compaction_regions->trunc_to(start_index);
142 }
143 
144 void G1FullGCCompactionPoint::add_humongous(HeapRegion* hr) {
145   assert(hr->is_starts_humongous(), "Sanity!");
146 
147   _collector->add_humongous_region(hr);
148 
149   G1CollectedHeap* g1h = G1CollectedHeap::heap();
150   g1h->humongous_obj_regions_iterate(hr,
151                                      [&] (HeapRegion* r) {
152                                        add(r);
153                                        _collector->update_from_skip_compacting_to_compacting(r->hrm_index());
154                                      });
155 }
156 
157 template <bool ALT_FWD>
158 void G1FullGCCompactionPoint::forward_humongous(HeapRegion* hr) {
159   assert(hr->is_starts_humongous(), "Sanity!");
160 
161   oop obj = cast_to_oop(hr->bottom());
162   size_t obj_size = obj->size();
163   uint num_regions = (uint)G1CollectedHeap::humongous_obj_size_in_regions(obj_size);
164 
165   if (!has_regions()) {
166     return;
167   }
168 
169   // Find contiguous compaction target regions for the humongous object.
170   uint range_begin = find_contiguous_before(hr, num_regions);
171 
172   if (range_begin == UINT_MAX) {
173     // No contiguous compaction target regions found, so the object cannot be moved.
174     return;
175   }
176 
177   // Preserve the mark for the humongous object as the region was initially not compacting.
178   preserved_stack()->push_if_necessary(obj, obj->mark());
179 
180   HeapRegion* dest_hr = _compaction_regions->at(range_begin);
181   SlidingForwarding::forward_to<ALT_FWD>(obj, cast_to_oop(dest_hr->bottom()));
182   assert(SlidingForwarding::is_forwarded(obj), "Object must be forwarded!");
183 
184   // Add the humongous object regions to the compaction point.
185   add_humongous(hr);
186 
187   // Remove covered regions from compaction target candidates.
188   _compaction_regions->remove_range(range_begin, (range_begin + num_regions));
189 
190   return;
191 }
192 
193 template void G1FullGCCompactionPoint::forward_humongous<true>(HeapRegion* hr);
194 template void G1FullGCCompactionPoint::forward_humongous<false>(HeapRegion* hr);
195 
196 uint G1FullGCCompactionPoint::find_contiguous_before(HeapRegion* hr, uint num_regions) {
197   assert(num_regions > 0, "Sanity!");
198   assert(has_regions(), "Sanity!");
199 
200   if (num_regions == 1) {
201     // If only one region, return the first region.
202     return 0;
203   }
204 
205   uint contiguous_region_count = 1;
206 
207   uint range_end = 1;
208   uint range_limit = (uint)_compaction_regions->length();
209 
210   for (; range_end < range_limit; range_end++) {
211     if (contiguous_region_count == num_regions) {
212       break;
213     }
214     // Check if the current region and the previous region are contiguous.
215     bool regions_are_contiguous = (_compaction_regions->at(range_end)->hrm_index() - _compaction_regions->at(range_end - 1)->hrm_index()) == 1;
< prev index next >