1 /*
2 * Copyright (c) 2018, 2020, Red Hat, Inc. 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/shared/gcCause.hpp"
27 #include "gc/shenandoah/shenandoahCollectionSet.inline.hpp"
28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
29 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
30 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
31 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
32 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
33 #include "logging/log.hpp"
34 #include "logging/logTag.hpp"
35 #include "runtime/globals_extension.hpp"
36
37 int ShenandoahHeuristics::compare_by_garbage(RegionData a, RegionData b) {
38 if (a._garbage > b._garbage)
39 return -1;
40 else if (a._garbage < b._garbage)
41 return 1;
42 else return 0;
43 }
44
45 ShenandoahHeuristics::ShenandoahHeuristics(ShenandoahSpaceInfo* space_info) :
46 _space_info(space_info),
47 _region_data(nullptr),
48 _cycle_start(os::elapsedTime()),
49 _last_cycle_end(0),
50 _gc_times_learned(0),
51 _gc_time_penalties(0),
52 _gc_time_history(new TruncatedSeq(10, ShenandoahAdaptiveDecayFactor)),
53 _metaspace_oom()
54 {
55 size_t num_regions = ShenandoahHeap::heap()->num_regions();
56 assert(num_regions > 0, "Sanity");
57
58 _region_data = NEW_C_HEAP_ARRAY(RegionData, num_regions, mtGC);
59 }
60
61 ShenandoahHeuristics::~ShenandoahHeuristics() {
62 FREE_C_HEAP_ARRAY(RegionGarbage, _region_data);
63 }
64
65 void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {
66 assert(collection_set->count() == 0, "Must be empty");
67
68 ShenandoahHeap* heap = ShenandoahHeap::heap();
69
70 // Check all pinned regions have updated status before choosing the collection set.
71 heap->assert_pinned_region_status();
72
73 // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.
74
75 size_t num_regions = heap->num_regions();
76
77 RegionData* candidates = _region_data;
78
79 size_t cand_idx = 0;
80
81 size_t total_garbage = 0;
82
83 size_t immediate_garbage = 0;
84 size_t immediate_regions = 0;
85
86 size_t free = 0;
89 ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
90
91 for (size_t i = 0; i < num_regions; i++) {
92 ShenandoahHeapRegion* region = heap->get_region(i);
93
94 size_t garbage = region->garbage();
95 total_garbage += garbage;
96
97 if (region->is_empty()) {
98 free_regions++;
99 free += ShenandoahHeapRegion::region_size_bytes();
100 } else if (region->is_regular()) {
101 if (!region->has_live()) {
102 // We can recycle it right away and put it in the free set.
103 immediate_regions++;
104 immediate_garbage += garbage;
105 region->make_trash_immediate();
106 } else {
107 // This is our candidate for later consideration.
108 candidates[cand_idx]._region = region;
109 candidates[cand_idx]._garbage = garbage;
110 cand_idx++;
111 }
112 } else if (region->is_humongous_start()) {
113 // Reclaim humongous regions here, and count them as the immediate garbage
114 #ifdef ASSERT
115 bool reg_live = region->has_live();
116 bool bm_live = ctx->is_marked(cast_to_oop(region->bottom()));
117 assert(reg_live == bm_live,
118 "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,
119 BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words());
120 #endif
121 if (!region->has_live()) {
122 heap->trash_humongous_region_at(region);
123
124 // Count only the start. Continuations would be counted on "trash" path
125 immediate_regions++;
126 immediate_garbage += garbage;
127 }
128 } else if (region->is_trash()) {
129 // Count in just trashed collection set, during coalesced CM-with-UR
130 immediate_regions++;
131 immediate_garbage += garbage;
132 }
133 }
134
135 // Step 2. Look back at garbage statistics, and decide if we want to collect anything,
136 // given the amount of immediately reclaimable garbage. If we do, figure out the collection set.
137
138 assert (immediate_garbage <= total_garbage,
139 "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",
140 byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),
141 byte_size_in_proper_unit(total_garbage), proper_unit_for_byte_size(total_garbage));
142
143 size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);
144
145 if (immediate_percent <= ShenandoahImmediateThreshold) {
146 choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);
147 }
148
149 size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);
150
151 size_t collectable_garbage = collection_set->garbage() + immediate_garbage;
152 size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage);
153
154 log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
155 "Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
156 "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%)",
157
158 byte_size_in_proper_unit(collectable_garbage),
159 proper_unit_for_byte_size(collectable_garbage),
160 collectable_garbage_percent,
161
162 byte_size_in_proper_unit(immediate_garbage),
163 proper_unit_for_byte_size(immediate_garbage),
164 immediate_percent,
165
166 byte_size_in_proper_unit(collection_set->garbage()),
167 proper_unit_for_byte_size(collection_set->garbage()),
168 cset_percent);
169 }
170
171 void ShenandoahHeuristics::record_cycle_start() {
172 _cycle_start = os::elapsedTime();
173 }
174
175 void ShenandoahHeuristics::record_cycle_end() {
176 _last_cycle_end = os::elapsedTime();
177 }
178
179 bool ShenandoahHeuristics::should_start_gc() {
180 // Perform GC to cleanup metaspace
181 if (has_metaspace_oom()) {
182 // Some of vmTestbase/metaspace tests depend on following line to count GC cycles
183 log_info(gc)("Trigger: %s", GCCause::to_string(GCCause::_metadata_GC_threshold));
184 return true;
185 }
186
187 if (ShenandoahGuaranteedGCInterval > 0) {
188 double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;
189 if (last_time_ms > ShenandoahGuaranteedGCInterval) {
190 log_info(gc)("Trigger: Time since last GC (%.0f ms) is larger than guaranteed interval (" UINTX_FORMAT " ms)",
191 last_time_ms, ShenandoahGuaranteedGCInterval);
192 return true;
193 }
194 }
195
196 return false;
197 }
198
199 bool ShenandoahHeuristics::should_degenerate_cycle() {
200 return ShenandoahHeap::heap()->shenandoah_policy()->consecutive_degenerated_gc_count() <= ShenandoahFullGCThreshold;
201 }
202
203 void ShenandoahHeuristics::adjust_penalty(intx step) {
204 assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
205 "In range before adjustment: " INTX_FORMAT, _gc_time_penalties);
206
207 intx new_val = _gc_time_penalties + step;
208 if (new_val < 0) {
209 new_val = 0;
210 }
211 if (new_val > 100) {
212 new_val = 100;
213 }
214 _gc_time_penalties = new_val;
215
216 assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
217 "In range after adjustment: " INTX_FORMAT, _gc_time_penalties);
218 }
219
220 void ShenandoahHeuristics::record_success_concurrent() {
221 _gc_time_history->add(time_since_last_gc());
222 _gc_times_learned++;
223
224 adjust_penalty(Concurrent_Adjust);
225 }
226
227 void ShenandoahHeuristics::record_success_degenerated() {
228 adjust_penalty(Degenerated_Penalty);
229 }
230
231 void ShenandoahHeuristics::record_success_full() {
232 adjust_penalty(Full_Penalty);
233 }
234
235 void ShenandoahHeuristics::record_allocation_failure_gc() {
236 // Do nothing.
237 }
238
239 void ShenandoahHeuristics::record_requested_gc() {
240 // Assume users call System.gc() when external state changes significantly,
241 // which forces us to re-learn the GC timings and allocation rates.
242 _gc_times_learned = 0;
243 }
244
245 bool ShenandoahHeuristics::can_unload_classes() {
246 return ClassUnloading;
247 }
248
249 bool ShenandoahHeuristics::should_unload_classes() {
250 if (!can_unload_classes()) return false;
251 if (has_metaspace_oom()) return true;
252 return ClassUnloadingWithConcurrentMark;
253 }
254
255 void ShenandoahHeuristics::initialize() {
256 // Nothing to do by default.
257 }
258
259 double ShenandoahHeuristics::time_since_last_gc() const {
260 return os::elapsedTime() - _cycle_start;
261 }
|
1 /*
2 * Copyright (c) 2018, 2020, Red Hat, Inc. All rights reserved.
3 * Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
26 #include "precompiled.hpp"
27 #include "gc/shared/gcCause.hpp"
28 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
29 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
30 #include "gc/shenandoah/shenandoahMarkingContext.inline.hpp"
31 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
32 #include "logging/log.hpp"
33 #include "logging/logTag.hpp"
34 #include "runtime/globals_extension.hpp"
35 #include "utilities/quickSort.hpp"
36
37 // sort by decreasing garbage (so most garbage comes first)
38 int ShenandoahHeuristics::compare_by_garbage(RegionData a, RegionData b) {
39 if (a._u._garbage > b._u._garbage)
40 return -1;
41 else if (a._u._garbage < b._u._garbage)
42 return 1;
43 else return 0;
44 }
45
46 ShenandoahHeuristics::ShenandoahHeuristics(ShenandoahSpaceInfo* space_info) :
47 _space_info(space_info),
48 _region_data(nullptr),
49 _guaranteed_gc_interval(0),
50 _cycle_start(os::elapsedTime()),
51 _last_cycle_end(0),
52 _gc_times_learned(0),
53 _gc_time_penalties(0),
54 _gc_cycle_time_history(new TruncatedSeq(Moving_Average_Samples, ShenandoahAdaptiveDecayFactor)),
55 _metaspace_oom()
56 {
57 size_t num_regions = ShenandoahHeap::heap()->num_regions();
58 assert(num_regions > 0, "Sanity");
59
60 _region_data = NEW_C_HEAP_ARRAY(RegionData, num_regions, mtGC);
61 }
62
63 ShenandoahHeuristics::~ShenandoahHeuristics() {
64 FREE_C_HEAP_ARRAY(RegionGarbage, _region_data);
65 }
66
67 void ShenandoahHeuristics::choose_collection_set(ShenandoahCollectionSet* collection_set) {
68 assert(collection_set->is_empty(), "Must be empty");
69
70 ShenandoahHeap* heap = ShenandoahHeap::heap();
71
72 // Check all pinned regions have updated status before choosing the collection set.
73 heap->assert_pinned_region_status();
74
75 // Step 1. Build up the region candidates we care about, rejecting losers and accepting winners right away.
76
77 size_t num_regions = heap->num_regions();
78
79 RegionData* candidates = _region_data;
80
81 size_t cand_idx = 0;
82
83 size_t total_garbage = 0;
84
85 size_t immediate_garbage = 0;
86 size_t immediate_regions = 0;
87
88 size_t free = 0;
91 ShenandoahMarkingContext* const ctx = heap->complete_marking_context();
92
93 for (size_t i = 0; i < num_regions; i++) {
94 ShenandoahHeapRegion* region = heap->get_region(i);
95
96 size_t garbage = region->garbage();
97 total_garbage += garbage;
98
99 if (region->is_empty()) {
100 free_regions++;
101 free += ShenandoahHeapRegion::region_size_bytes();
102 } else if (region->is_regular()) {
103 if (!region->has_live()) {
104 // We can recycle it right away and put it in the free set.
105 immediate_regions++;
106 immediate_garbage += garbage;
107 region->make_trash_immediate();
108 } else {
109 // This is our candidate for later consideration.
110 candidates[cand_idx]._region = region;
111 candidates[cand_idx]._u._garbage = garbage;
112 cand_idx++;
113 }
114 } else if (region->is_humongous_start()) {
115 // Reclaim humongous regions here, and count them as the immediate garbage
116 #ifdef ASSERT
117 bool reg_live = region->has_live();
118 bool bm_live = ctx->is_marked(cast_to_oop(region->bottom()));
119 assert(reg_live == bm_live,
120 "Humongous liveness and marks should agree. Region live: %s; Bitmap live: %s; Region Live Words: " SIZE_FORMAT,
121 BOOL_TO_STR(reg_live), BOOL_TO_STR(bm_live), region->get_live_data_words());
122 #endif
123 if (!region->has_live()) {
124 heap->trash_humongous_region_at(region);
125
126 // Count only the start. Continuations would be counted on "trash" path
127 immediate_regions++;
128 immediate_garbage += garbage;
129 }
130 } else if (region->is_trash()) {
131 // Count in just trashed collection set, during coalesced CM-with-UR
132 immediate_regions++;
133 immediate_garbage += garbage;
134 }
135 }
136
137 // Step 2. Look back at garbage statistics, and decide if we want to collect anything,
138 // given the amount of immediately reclaimable garbage. If we do, figure out the collection set.
139
140 assert (immediate_garbage <= total_garbage,
141 "Cannot have more immediate garbage than total garbage: " SIZE_FORMAT "%s vs " SIZE_FORMAT "%s",
142 byte_size_in_proper_unit(immediate_garbage), proper_unit_for_byte_size(immediate_garbage),
143 byte_size_in_proper_unit(total_garbage), proper_unit_for_byte_size(total_garbage));
144
145 size_t immediate_percent = (total_garbage == 0) ? 0 : (immediate_garbage * 100 / total_garbage);
146
147 if (immediate_percent <= ShenandoahImmediateThreshold) {
148 choose_collection_set_from_regiondata(collection_set, candidates, cand_idx, immediate_garbage + free);
149 }
150
151 size_t cset_percent = (total_garbage == 0) ? 0 : (collection_set->garbage() * 100 / total_garbage);
152 size_t collectable_garbage = collection_set->garbage() + immediate_garbage;
153 size_t collectable_garbage_percent = (total_garbage == 0) ? 0 : (collectable_garbage * 100 / total_garbage);
154
155 log_info(gc, ergo)("Collectable Garbage: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), "
156 "Immediate: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions, "
157 "CSet: " SIZE_FORMAT "%s (" SIZE_FORMAT "%%), " SIZE_FORMAT " regions",
158
159 byte_size_in_proper_unit(collectable_garbage),
160 proper_unit_for_byte_size(collectable_garbage),
161 collectable_garbage_percent,
162
163 byte_size_in_proper_unit(immediate_garbage),
164 proper_unit_for_byte_size(immediate_garbage),
165 immediate_percent,
166 immediate_regions,
167
168 byte_size_in_proper_unit(collection_set->garbage()),
169 proper_unit_for_byte_size(collection_set->garbage()),
170 cset_percent,
171 collection_set->count());
172 }
173
174 void ShenandoahHeuristics::record_cycle_start() {
175 _cycle_start = os::elapsedTime();
176 }
177
178 void ShenandoahHeuristics::record_cycle_end() {
179 _last_cycle_end = os::elapsedTime();
180 }
181
182 bool ShenandoahHeuristics::should_start_gc() {
183 // Perform GC to cleanup metaspace
184 if (has_metaspace_oom()) {
185 // Some of vmTestbase/metaspace tests depend on following line to count GC cycles
186 log_info(gc)("Trigger: %s", GCCause::to_string(GCCause::_metadata_GC_threshold));
187 return true;
188 }
189
190 if (_guaranteed_gc_interval > 0) {
191 double last_time_ms = (os::elapsedTime() - _last_cycle_end) * 1000;
192 if (last_time_ms > _guaranteed_gc_interval) {
193 log_info(gc)("Trigger (%s): Time since last GC (%.0f ms) is larger than guaranteed interval (" UINTX_FORMAT " ms)",
194 _space_info->name(), last_time_ms, _guaranteed_gc_interval);
195 return true;
196 }
197 }
198
199 return false;
200 }
201
202 bool ShenandoahHeuristics::should_degenerate_cycle() {
203 return ShenandoahHeap::heap()->shenandoah_policy()->consecutive_degenerated_gc_count() <= ShenandoahFullGCThreshold;
204 }
205
206 void ShenandoahHeuristics::adjust_penalty(intx step) {
207 assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
208 "In range before adjustment: " INTX_FORMAT, _gc_time_penalties);
209
210 intx new_val = _gc_time_penalties + step;
211 if (new_val < 0) {
212 new_val = 0;
213 }
214 if (new_val > 100) {
215 new_val = 100;
216 }
217 _gc_time_penalties = new_val;
218
219 assert(0 <= _gc_time_penalties && _gc_time_penalties <= 100,
220 "In range after adjustment: " INTX_FORMAT, _gc_time_penalties);
221 }
222
223 void ShenandoahHeuristics::record_success_concurrent(bool abbreviated) {
224 _gc_times_learned++;
225
226 adjust_penalty(Concurrent_Adjust);
227
228 if (_gc_times_learned <= ShenandoahLearningSteps || !(abbreviated && ShenandoahAdaptiveIgnoreShortCycles)) {
229 _gc_cycle_time_history->add(elapsed_cycle_time());
230 }
231 }
232
233 void ShenandoahHeuristics::record_success_degenerated() {
234 adjust_penalty(Degenerated_Penalty);
235 }
236
237 void ShenandoahHeuristics::record_success_full() {
238 adjust_penalty(Full_Penalty);
239 }
240
241 void ShenandoahHeuristics::record_allocation_failure_gc() {
242 // Do nothing.
243 }
244
245 void ShenandoahHeuristics::record_requested_gc() {
246 // Assume users call System.gc() when external state changes significantly,
247 // which forces us to re-learn the GC timings and allocation rates.
248 _gc_times_learned = 0;
249 }
250
251 bool ShenandoahHeuristics::can_unload_classes() {
252 return ClassUnloading;
253 }
254
255 bool ShenandoahHeuristics::should_unload_classes() {
256 if (!can_unload_classes()) return false;
257 if (has_metaspace_oom()) return true;
258 return ClassUnloadingWithConcurrentMark;
259 }
260
261 void ShenandoahHeuristics::initialize() {
262 // Nothing to do by default.
263 }
264
265 double ShenandoahHeuristics::elapsed_cycle_time() const {
266 return os::elapsedTime() - _cycle_start;
267 }
|