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