1 /*
2 * Copyright (c) 2018, 2019, 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
27 #include "gc/shenandoah/heuristics/shenandoahAdaptiveHeuristics.hpp"
28 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
29 #include "gc/shenandoah/shenandoahFreeSet.hpp"
30 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
31 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
32 #include "logging/log.hpp"
33 #include "logging/logTag.hpp"
34 #include "utilities/quickSort.hpp"
35
36 // These constants are used to adjust the margin of error for the moving
37 // average of the allocation rate and cycle time. The units are standard
38 // deviations.
39 const double ShenandoahAdaptiveHeuristics::FULL_PENALTY_SD = 0.2;
40 const double ShenandoahAdaptiveHeuristics::DEGENERATE_PENALTY_SD = 0.1;
41
42 // These are used to decide if we want to make any adjustments at all
43 // at the end of a successful concurrent cycle.
44 const double ShenandoahAdaptiveHeuristics::LOWEST_EXPECTED_AVAILABLE_AT_END = -0.5;
45 const double ShenandoahAdaptiveHeuristics::HIGHEST_EXPECTED_AVAILABLE_AT_END = 0.5;
46
47 // These values are the confidence interval expressed as standard deviations.
48 // At the minimum confidence level, there is a 25% chance that the true value of
49 // the estimate (average cycle time or allocation rate) is not more than
50 // MINIMUM_CONFIDENCE standard deviations away from our estimate. Similarly, the
51 // MAXIMUM_CONFIDENCE interval here means there is a one in a thousand chance
52 // that the true value of our estimate is outside the interval. These are used
53 // as bounds on the adjustments applied at the outcome of a GC cycle.
54 const double ShenandoahAdaptiveHeuristics::MINIMUM_CONFIDENCE = 0.319; // 25%
55 const double ShenandoahAdaptiveHeuristics::MAXIMUM_CONFIDENCE = 3.291; // 99.9%
56
57 ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics(ShenandoahSpaceInfo* space_info) :
58 ShenandoahHeuristics(space_info),
59 _margin_of_error_sd(ShenandoahAdaptiveInitialConfidence),
60 _spike_threshold_sd(ShenandoahAdaptiveInitialSpikeThreshold),
61 _last_trigger(OTHER) { }
62
63 ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {}
64
65 void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
66 RegionData* data, size_t size,
67 size_t actual_free) {
68 size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;
69
70 // The logic for cset selection in adaptive is as follows:
71 //
72 // 1. We cannot get cset larger than available free space. Otherwise we guarantee OOME
73 // during evacuation, and thus guarantee full GC. In practice, we also want to let
74 // application to allocate something. This is why we limit CSet to some fraction of
75 // available space. In non-overloaded heap, max_cset would contain all plausible candidates
76 // over garbage threshold.
77 //
78 // 2. We should not get cset too low so that free threshold would not be met right
79 // after the cycle. Otherwise we get back-to-back cycles for no reason if heap is
80 // too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.
81 //
82 // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates
83 // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before
84 // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,
85 // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.
86
87 size_t capacity = _space_info->soft_max_capacity();
88 size_t max_cset = (size_t)((1.0 * capacity / 100 * ShenandoahEvacReserve) / ShenandoahEvacWaste);
89 size_t free_target = (capacity / 100 * ShenandoahMinFreeThreshold) + max_cset;
90 size_t min_garbage = (free_target > actual_free ? (free_target - actual_free) : 0);
91
92 log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "%s, Actual Free: "
93 SIZE_FORMAT "%s, Max CSet: " SIZE_FORMAT "%s, Min Garbage: " SIZE_FORMAT "%s",
94 byte_size_in_proper_unit(free_target), proper_unit_for_byte_size(free_target),
95 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free),
96 byte_size_in_proper_unit(max_cset), proper_unit_for_byte_size(max_cset),
97 byte_size_in_proper_unit(min_garbage), proper_unit_for_byte_size(min_garbage));
98
99 // Better select garbage-first regions
100 QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false);
101
102 size_t cur_cset = 0;
103 size_t cur_garbage = 0;
104
105 for (size_t idx = 0; idx < size; idx++) {
106 ShenandoahHeapRegion* r = data[idx]._region;
107
108 size_t new_cset = cur_cset + r->get_live_data_bytes();
109 size_t new_garbage = cur_garbage + r->garbage();
110
111 if (new_cset > max_cset) {
112 break;
113 }
114
115 if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) {
116 cset->add_region(r);
117 cur_cset = new_cset;
118 cur_garbage = new_garbage;
119 }
120 }
121 }
122
123 void ShenandoahAdaptiveHeuristics::record_cycle_start() {
124 ShenandoahHeuristics::record_cycle_start();
125 _allocation_rate.allocation_counter_reset();
126 }
127
128 void ShenandoahAdaptiveHeuristics::record_success_concurrent() {
129 ShenandoahHeuristics::record_success_concurrent();
130
131 size_t available = _space_info->available();
132
133 _available.add(available);
134 double z_score = 0.0;
135 if (_available.sd() > 0) {
136 z_score = (available - _available.avg()) / _available.sd();
137 }
138
139 log_debug(gc, ergo)("Available: " SIZE_FORMAT " %sB, z-score=%.3f. Average available: %.1f %sB +/- %.1f %sB.",
140 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
141 z_score,
142 byte_size_in_proper_unit(_available.avg()), proper_unit_for_byte_size(_available.avg()),
143 byte_size_in_proper_unit(_available.sd()), proper_unit_for_byte_size(_available.sd()));
144
145 // In the case when a concurrent GC cycle completes successfully but with an
146 // unusually small amount of available memory we will adjust our trigger
147 // parameters so that they are more likely to initiate a new cycle.
148 // Conversely, when a GC cycle results in an above average amount of available
149 // memory, we will adjust the trigger parameters to be less likely to initiate
150 // a GC cycle.
151 //
152 // The z-score we've computed is in no way statistically related to the
153 // trigger parameters, but it has the nice property that worse z-scores for
154 // available memory indicate making larger adjustments to the trigger
155 // parameters. It also results in fewer adjustments as the application
156 // stabilizes.
157 //
158 // In order to avoid making endless and likely unnecessary adjustments to the
159 // trigger parameters, the change in available memory (with respect to the
160 // average) at the end of a cycle must be beyond these threshold values.
161 if (z_score < LOWEST_EXPECTED_AVAILABLE_AT_END ||
162 z_score > HIGHEST_EXPECTED_AVAILABLE_AT_END) {
163 // The sign is flipped because a negative z-score indicates that the
179 ShenandoahHeuristics::record_success_degenerated();
180 // Adjust both trigger's parameters in the case of a degenerated GC because
181 // either of them should have triggered earlier to avoid this case.
182 adjust_margin_of_error(DEGENERATE_PENALTY_SD);
183 adjust_spike_threshold(DEGENERATE_PENALTY_SD);
184 }
185
186 void ShenandoahAdaptiveHeuristics::record_success_full() {
187 ShenandoahHeuristics::record_success_full();
188 // Adjust both trigger's parameters in the case of a full GC because
189 // either of them should have triggered earlier to avoid this case.
190 adjust_margin_of_error(FULL_PENALTY_SD);
191 adjust_spike_threshold(FULL_PENALTY_SD);
192 }
193
194 static double saturate(double value, double min, double max) {
195 return MAX2(MIN2(value, max), min);
196 }
197
198 bool ShenandoahAdaptiveHeuristics::should_start_gc() {
199 size_t max_capacity = _space_info->max_capacity();
200 size_t capacity = _space_info->soft_max_capacity();
201 size_t available = _space_info->available();
202 size_t allocated = _space_info->bytes_allocated_since_gc_start();
203
204 // Make sure the code below treats available without the soft tail.
205 size_t soft_tail = max_capacity - capacity;
206 available = (available > soft_tail) ? (available - soft_tail) : 0;
207
208 // Track allocation rate even if we decide to start a cycle for other reasons.
209 double rate = _allocation_rate.sample(allocated);
210 _last_trigger = OTHER;
211
212 size_t min_threshold = capacity / 100 * ShenandoahMinFreeThreshold;
213 if (available < min_threshold) {
214 log_info(gc)("Trigger: Free (" SIZE_FORMAT "%s) is below minimum threshold (" SIZE_FORMAT "%s)",
215 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
216 byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold));
217 return true;
218 }
219
220 const size_t max_learn = ShenandoahLearningSteps;
221 if (_gc_times_learned < max_learn) {
222 size_t init_threshold = capacity / 100 * ShenandoahInitFreeThreshold;
223 if (available < init_threshold) {
224 log_info(gc)("Trigger: Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "%s) is below initial threshold (" SIZE_FORMAT "%s)",
225 _gc_times_learned + 1, max_learn,
226 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
227 byte_size_in_proper_unit(init_threshold), proper_unit_for_byte_size(init_threshold));
228 return true;
229 }
230 }
231
232 // Check if allocation headroom is still okay. This also factors in:
233 // 1. Some space to absorb allocation spikes
234 // 2. Accumulated penalties from Degenerated and Full GC
235 size_t allocation_headroom = available;
236
237 size_t spike_headroom = capacity / 100 * ShenandoahAllocSpikeFactor;
238 size_t penalties = capacity / 100 * _gc_time_penalties;
239
240 allocation_headroom -= MIN2(allocation_headroom, spike_headroom);
241 allocation_headroom -= MIN2(allocation_headroom, penalties);
242
243 double avg_cycle_time = _gc_time_history->davg() + (_margin_of_error_sd * _gc_time_history->dsd());
244 double avg_alloc_rate = _allocation_rate.upper_bound(_margin_of_error_sd);
245 if (avg_cycle_time > allocation_headroom / avg_alloc_rate) {
246 log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for average allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s) (margin of error = %.2f)",
247 avg_cycle_time * 1000,
248 byte_size_in_proper_unit(avg_alloc_rate), proper_unit_for_byte_size(avg_alloc_rate),
249 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),
250 _margin_of_error_sd);
251
252 log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "%s (free) - " SIZE_FORMAT "%s (spike) - " SIZE_FORMAT "%s (penalties) = " SIZE_FORMAT "%s",
253 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
254 byte_size_in_proper_unit(spike_headroom), proper_unit_for_byte_size(spike_headroom),
255 byte_size_in_proper_unit(penalties), proper_unit_for_byte_size(penalties),
256 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom));
257
258 _last_trigger = RATE;
259 return true;
260 }
261
262 bool is_spiking = _allocation_rate.is_spiking(rate, _spike_threshold_sd);
263 if (is_spiking && avg_cycle_time > allocation_headroom / rate) {
264 log_info(gc)("Trigger: Average GC time (%.2f ms) is above the time for instantaneous allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s) (spike threshold = %.2f)",
265 avg_cycle_time * 1000,
266 byte_size_in_proper_unit(rate), proper_unit_for_byte_size(rate),
267 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),
268 _spike_threshold_sd);
269 _last_trigger = SPIKE;
270 return true;
271 }
272
273 return ShenandoahHeuristics::should_start_gc();
274 }
275
276 void ShenandoahAdaptiveHeuristics::adjust_last_trigger_parameters(double amount) {
277 switch (_last_trigger) {
278 case RATE:
279 adjust_margin_of_error(amount);
280 break;
281 case SPIKE:
282 adjust_spike_threshold(amount);
283 break;
284 case OTHER:
285 // nothing to adjust here.
286 break;
287 default:
288 ShouldNotReachHere();
289 }
290 }
291
292 void ShenandoahAdaptiveHeuristics::adjust_margin_of_error(double amount) {
293 _margin_of_error_sd = saturate(_margin_of_error_sd + amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
294 log_debug(gc, ergo)("Margin of error now %.2f", _margin_of_error_sd);
295 }
296
297 void ShenandoahAdaptiveHeuristics::adjust_spike_threshold(double amount) {
298 _spike_threshold_sd = saturate(_spike_threshold_sd - amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
299 log_debug(gc, ergo)("Spike threshold now: %.2f", _spike_threshold_sd);
300 }
301
302 ShenandoahAllocationRate::ShenandoahAllocationRate() :
303 _last_sample_time(os::elapsedTime()),
304 _last_sample_value(0),
305 _interval_sec(1.0 / ShenandoahAdaptiveSampleFrequencyHz),
306 _rate(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor),
307 _rate_avg(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor) {
308 }
309
310 double ShenandoahAllocationRate::sample(size_t allocated) {
311 double now = os::elapsedTime();
312 double rate = 0.0;
313 if (now - _last_sample_time > _interval_sec) {
314 if (allocated >= _last_sample_value) {
315 rate = instantaneous_rate(now, allocated);
316 _rate.add(rate);
317 _rate_avg.add(_rate.avg());
318 }
319
320 _last_sample_time = now;
321 _last_sample_value = allocated;
|
1 /*
2 * Copyright (c) 2018, 2019, 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 #include "precompiled.hpp"
26
27
28 #include "gc/shared/gcCause.hpp"
29 #include "gc/shenandoah/heuristics/shenandoahHeuristics.hpp"
30 #include "gc/shenandoah/heuristics/shenandoahSpaceInfo.hpp"
31 #include "gc/shenandoah/heuristics/shenandoahAdaptiveHeuristics.hpp"
32 #include "gc/shenandoah/shenandoahCollectionSet.hpp"
33 #include "gc/shenandoah/shenandoahCollectorPolicy.hpp"
34 #include "gc/shenandoah/shenandoahFreeSet.hpp"
35 #include "gc/shenandoah/shenandoahHeap.inline.hpp"
36 #include "gc/shenandoah/shenandoahHeapRegion.inline.hpp"
37 #include "logging/log.hpp"
38 #include "logging/logTag.hpp"
39 #include "runtime/globals_extension.hpp"
40 #include "utilities/quickSort.hpp"
41
42 // These constants are used to adjust the margin of error for the moving
43 // average of the allocation rate and cycle time. The units are standard
44 // deviations.
45 const double ShenandoahAdaptiveHeuristics::FULL_PENALTY_SD = 0.2;
46 const double ShenandoahAdaptiveHeuristics::DEGENERATE_PENALTY_SD = 0.1;
47
48 // These are used to decide if we want to make any adjustments at all
49 // at the end of a successful concurrent cycle.
50 const double ShenandoahAdaptiveHeuristics::LOWEST_EXPECTED_AVAILABLE_AT_END = -0.5;
51 const double ShenandoahAdaptiveHeuristics::HIGHEST_EXPECTED_AVAILABLE_AT_END = 0.5;
52
53 // These values are the confidence interval expressed as standard deviations.
54 // At the minimum confidence level, there is a 25% chance that the true value of
55 // the estimate (average cycle time or allocation rate) is not more than
56 // MINIMUM_CONFIDENCE standard deviations away from our estimate. Similarly, the
57 // MAXIMUM_CONFIDENCE interval here means there is a one in a thousand chance
58 // that the true value of our estimate is outside the interval. These are used
59 // as bounds on the adjustments applied at the outcome of a GC cycle.
60 const double ShenandoahAdaptiveHeuristics::MINIMUM_CONFIDENCE = 0.319; // 25%
61 const double ShenandoahAdaptiveHeuristics::MAXIMUM_CONFIDENCE = 3.291; // 99.9%
62
63 ShenandoahAdaptiveHeuristics::ShenandoahAdaptiveHeuristics(ShenandoahSpaceInfo* space_info) :
64 ShenandoahHeuristics(space_info),
65 _margin_of_error_sd(ShenandoahAdaptiveInitialConfidence),
66 _spike_threshold_sd(ShenandoahAdaptiveInitialSpikeThreshold),
67 _last_trigger(OTHER),
68 _available(Moving_Average_Samples, ShenandoahAdaptiveDecayFactor) { }
69
70 ShenandoahAdaptiveHeuristics::~ShenandoahAdaptiveHeuristics() {}
71
72 void ShenandoahAdaptiveHeuristics::choose_collection_set_from_regiondata(ShenandoahCollectionSet* cset,
73 RegionData* data, size_t size,
74 size_t actual_free) {
75 size_t garbage_threshold = ShenandoahHeapRegion::region_size_bytes() * ShenandoahGarbageThreshold / 100;
76
77 // The logic for cset selection in adaptive is as follows:
78 //
79 // 1. We cannot get cset larger than available free space. Otherwise we guarantee OOME
80 // during evacuation, and thus guarantee full GC. In practice, we also want to let
81 // application to allocate something. This is why we limit CSet to some fraction of
82 // available space. In non-overloaded heap, max_cset would contain all plausible candidates
83 // over garbage threshold.
84 //
85 // 2. We should not get cset too low so that free threshold would not be met right
86 // after the cycle. Otherwise we get back-to-back cycles for no reason if heap is
87 // too fragmented. In non-overloaded non-fragmented heap min_garbage would be around zero.
88 //
89 // Therefore, we start by sorting the regions by garbage. Then we unconditionally add the best candidates
90 // before we meet min_garbage. Then we add all candidates that fit with a garbage threshold before
91 // we hit max_cset. When max_cset is hit, we terminate the cset selection. Note that in this scheme,
92 // ShenandoahGarbageThreshold is the soft threshold which would be ignored until min_garbage is hit.
93
94 size_t capacity = _space_info->soft_max_capacity();
95 size_t max_cset = (size_t)((1.0 * capacity / 100 * ShenandoahEvacReserve) / ShenandoahEvacWaste);
96 size_t free_target = (capacity * ShenandoahMinFreeThreshold) / 100 + max_cset;
97 size_t min_garbage = (free_target > actual_free) ? (free_target - actual_free) : 0;
98
99 log_info(gc, ergo)("Adaptive CSet Selection. Target Free: " SIZE_FORMAT "%s, Actual Free: "
100 SIZE_FORMAT "%s, Max Evacuation: " SIZE_FORMAT "%s, Min Garbage: " SIZE_FORMAT "%s",
101 byte_size_in_proper_unit(free_target), proper_unit_for_byte_size(free_target),
102 byte_size_in_proper_unit(actual_free), proper_unit_for_byte_size(actual_free),
103 byte_size_in_proper_unit(max_cset), proper_unit_for_byte_size(max_cset),
104 byte_size_in_proper_unit(min_garbage), proper_unit_for_byte_size(min_garbage));
105
106 // Better select garbage-first regions
107 QuickSort::sort<RegionData>(data, (int)size, compare_by_garbage, false);
108
109 size_t cur_cset = 0;
110 size_t cur_garbage = 0;
111
112 for (size_t idx = 0; idx < size; idx++) {
113 ShenandoahHeapRegion* r = data[idx]._region;
114
115 size_t new_cset = cur_cset + r->get_live_data_bytes();
116 size_t new_garbage = cur_garbage + r->garbage();
117
118 if (new_cset > max_cset) {
119 break;
120 }
121
122 if ((new_garbage < min_garbage) || (r->garbage() > garbage_threshold)) {
123 cset->add_region(r);
124 cur_cset = new_cset;
125 cur_garbage = new_garbage;
126 }
127 }
128 }
129
130 void ShenandoahAdaptiveHeuristics::record_cycle_start() {
131 ShenandoahHeuristics::record_cycle_start();
132 _allocation_rate.allocation_counter_reset();
133 }
134
135 void ShenandoahAdaptiveHeuristics::record_success_concurrent(bool abbreviated) {
136 ShenandoahHeuristics::record_success_concurrent(abbreviated);
137
138 size_t available = _space_info->available();
139
140 double z_score = 0.0;
141 double available_sd = _available.sd();
142 if (available_sd > 0) {
143 double available_avg = _available.avg();
144 z_score = (double(available) - available_avg) / available_sd;
145 log_debug(gc, ergo)("%s Available: " SIZE_FORMAT " %sB, z-score=%.3f. Average available: %.1f %sB +/- %.1f %sB.",
146 _space_info->name(),
147 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
148 z_score,
149 byte_size_in_proper_unit(available_avg), proper_unit_for_byte_size(available_avg),
150 byte_size_in_proper_unit(available_sd), proper_unit_for_byte_size(available_sd));
151 }
152
153 _available.add(double(available));
154
155 // In the case when a concurrent GC cycle completes successfully but with an
156 // unusually small amount of available memory we will adjust our trigger
157 // parameters so that they are more likely to initiate a new cycle.
158 // Conversely, when a GC cycle results in an above average amount of available
159 // memory, we will adjust the trigger parameters to be less likely to initiate
160 // a GC cycle.
161 //
162 // The z-score we've computed is in no way statistically related to the
163 // trigger parameters, but it has the nice property that worse z-scores for
164 // available memory indicate making larger adjustments to the trigger
165 // parameters. It also results in fewer adjustments as the application
166 // stabilizes.
167 //
168 // In order to avoid making endless and likely unnecessary adjustments to the
169 // trigger parameters, the change in available memory (with respect to the
170 // average) at the end of a cycle must be beyond these threshold values.
171 if (z_score < LOWEST_EXPECTED_AVAILABLE_AT_END ||
172 z_score > HIGHEST_EXPECTED_AVAILABLE_AT_END) {
173 // The sign is flipped because a negative z-score indicates that the
189 ShenandoahHeuristics::record_success_degenerated();
190 // Adjust both trigger's parameters in the case of a degenerated GC because
191 // either of them should have triggered earlier to avoid this case.
192 adjust_margin_of_error(DEGENERATE_PENALTY_SD);
193 adjust_spike_threshold(DEGENERATE_PENALTY_SD);
194 }
195
196 void ShenandoahAdaptiveHeuristics::record_success_full() {
197 ShenandoahHeuristics::record_success_full();
198 // Adjust both trigger's parameters in the case of a full GC because
199 // either of them should have triggered earlier to avoid this case.
200 adjust_margin_of_error(FULL_PENALTY_SD);
201 adjust_spike_threshold(FULL_PENALTY_SD);
202 }
203
204 static double saturate(double value, double min, double max) {
205 return MAX2(MIN2(value, max), min);
206 }
207
208 bool ShenandoahAdaptiveHeuristics::should_start_gc() {
209 size_t capacity = _space_info->soft_max_capacity();
210 size_t available = _space_info->soft_available();
211 size_t allocated = _space_info->bytes_allocated_since_gc_start();
212
213 log_debug(gc)("should_start_gc (%s)? available: " SIZE_FORMAT ", soft_max_capacity: " SIZE_FORMAT
214 ", allocated: " SIZE_FORMAT,
215 _space_info->name(), available, capacity, allocated);
216
217 // Track allocation rate even if we decide to start a cycle for other reasons.
218 double rate = _allocation_rate.sample(allocated);
219 _last_trigger = OTHER;
220
221 size_t min_threshold = min_free_threshold();
222 if (available < min_threshold) {
223 log_info(gc)("Trigger (%s): Free (" SIZE_FORMAT "%s) is below minimum threshold (" SIZE_FORMAT "%s)", _space_info->name(),
224 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
225 byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold));
226 return true;
227 }
228
229 // Check if we need to learn a bit about the application
230 const size_t max_learn = ShenandoahLearningSteps;
231 if (_gc_times_learned < max_learn) {
232 size_t init_threshold = capacity / 100 * ShenandoahInitFreeThreshold;
233 if (available < init_threshold) {
234 log_info(gc)("Trigger (%s): Learning " SIZE_FORMAT " of " SIZE_FORMAT ". Free (" SIZE_FORMAT "%s) is below initial threshold (" SIZE_FORMAT "%s)",
235 _space_info->name(), _gc_times_learned + 1, max_learn,
236 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
237 byte_size_in_proper_unit(init_threshold), proper_unit_for_byte_size(init_threshold));
238 return true;
239 }
240 }
241 // Rationale:
242 // The idea is that there is an average allocation rate and there are occasional abnormal bursts (or spikes) of
243 // allocations that exceed the average allocation rate. What do these spikes look like?
244 //
245 // 1. At certain phase changes, we may discard large amounts of data and replace it with large numbers of newly
246 // allocated objects. This "spike" looks more like a phase change. We were in steady state at M bytes/sec
247 // allocation rate and now we're in a "reinitialization phase" that looks like N bytes/sec. We need the "spike"
248 // accommodation to give us enough runway to recalibrate our "average allocation rate".
249 //
250 // 2. The typical workload changes. "Suddenly", our typical workload of N TPS increases to N+delta TPS. This means
251 // our average allocation rate needs to be adjusted. Once again, we need the "spike" accomodation to give us
252 // enough runway to recalibrate our "average allocation rate".
253 //
254 // 3. Though there is an "average" allocation rate, a given workload's demand for allocation may be very bursty. We
255 // allocate a bunch of LABs during the 5 ms that follow completion of a GC, then we perform no more allocations for
256 // the next 150 ms. It seems we want the "spike" to represent the maximum divergence from average within the
257 // period of time between consecutive evaluation of the should_start_gc() service. Here's the thinking:
258 //
259 // a) Between now and the next time I ask whether should_start_gc(), we might experience a spike representing
260 // the anticipated burst of allocations. If that would put us over budget, then we should start GC immediately.
261 // b) Between now and the anticipated depletion of allocation pool, there may be two or more bursts of allocations.
262 // If there are more than one of these bursts, we can "approximate" that these will be separated by spans of
263 // time with very little or no allocations so the "average" allocation rate should be a suitable approximation
264 // of how this will behave.
265 //
266 // For cases 1 and 2, we need to "quickly" recalibrate the average allocation rate whenever we detect a change
267 // in operation mode. We want some way to decide that the average rate has changed. Make average allocation rate
268 // computations an independent effort.
269 // Check if allocation headroom is still okay. This also factors in:
270 // 1. Some space to absorb allocation spikes (ShenandoahAllocSpikeFactor)
271 // 2. Accumulated penalties from Degenerated and Full GC
272 size_t allocation_headroom = available;
273
274 size_t spike_headroom = capacity / 100 * ShenandoahAllocSpikeFactor;
275 size_t penalties = capacity / 100 * _gc_time_penalties;
276
277 allocation_headroom -= MIN2(allocation_headroom, spike_headroom);
278 allocation_headroom -= MIN2(allocation_headroom, penalties);
279
280 double avg_cycle_time = _gc_cycle_time_history->davg() + (_margin_of_error_sd * _gc_cycle_time_history->dsd());
281 double avg_alloc_rate = _allocation_rate.upper_bound(_margin_of_error_sd);
282 log_debug(gc)("%s: average GC time: %.2f ms, allocation rate: %.0f %s/s",
283 _space_info->name(),
284 avg_cycle_time * 1000, byte_size_in_proper_unit(avg_alloc_rate), proper_unit_for_byte_size(avg_alloc_rate));
285 if (avg_cycle_time > allocation_headroom / avg_alloc_rate) {
286 log_info(gc)("Trigger (%s): Average GC time (%.2f ms) is above the time for average allocation rate (%.0f %sB/s)"
287 " to deplete free headroom (" SIZE_FORMAT "%s) (margin of error = %.2f)",
288 _space_info->name(), avg_cycle_time * 1000,
289 byte_size_in_proper_unit(avg_alloc_rate), proper_unit_for_byte_size(avg_alloc_rate),
290 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),
291 _margin_of_error_sd);
292 log_info(gc, ergo)("Free headroom: " SIZE_FORMAT "%s (free) - " SIZE_FORMAT "%s (spike) - " SIZE_FORMAT "%s (penalties) = " SIZE_FORMAT "%s",
293 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
294 byte_size_in_proper_unit(spike_headroom), proper_unit_for_byte_size(spike_headroom),
295 byte_size_in_proper_unit(penalties), proper_unit_for_byte_size(penalties),
296 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom));
297 _last_trigger = RATE;
298 return true;
299 }
300
301 bool is_spiking = _allocation_rate.is_spiking(rate, _spike_threshold_sd);
302 if (is_spiking && avg_cycle_time > allocation_headroom / rate) {
303 log_info(gc)("Trigger (%s): Average GC time (%.2f ms) is above the time for instantaneous allocation rate (%.0f %sB/s) to deplete free headroom (" SIZE_FORMAT "%s) (spike threshold = %.2f)",
304 _space_info->name(), avg_cycle_time * 1000,
305 byte_size_in_proper_unit(rate), proper_unit_for_byte_size(rate),
306 byte_size_in_proper_unit(allocation_headroom), proper_unit_for_byte_size(allocation_headroom),
307 _spike_threshold_sd);
308 _last_trigger = SPIKE;
309 return true;
310 }
311
312 return ShenandoahHeuristics::should_start_gc();
313 }
314
315 void ShenandoahAdaptiveHeuristics::adjust_last_trigger_parameters(double amount) {
316 switch (_last_trigger) {
317 case RATE:
318 adjust_margin_of_error(amount);
319 break;
320 case SPIKE:
321 adjust_spike_threshold(amount);
322 break;
323 case OTHER:
324 // nothing to adjust here.
325 break;
326 default:
327 ShouldNotReachHere();
328 }
329 }
330
331 void ShenandoahAdaptiveHeuristics::adjust_margin_of_error(double amount) {
332 _margin_of_error_sd = saturate(_margin_of_error_sd + amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
333 log_debug(gc, ergo)("Margin of error now %.2f", _margin_of_error_sd);
334 }
335
336 void ShenandoahAdaptiveHeuristics::adjust_spike_threshold(double amount) {
337 _spike_threshold_sd = saturate(_spike_threshold_sd - amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
338 log_debug(gc, ergo)("Spike threshold now: %.2f", _spike_threshold_sd);
339 }
340
341 size_t ShenandoahAdaptiveHeuristics::min_free_threshold() {
342 // Note that soft_max_capacity() / 100 * min_free_threshold is smaller than max_capacity() / 100 * min_free_threshold.
343 // We want to behave conservatively here, so use max_capacity(). By returning a larger value, we cause the GC to
344 // trigger when the remaining amount of free shrinks below the larger threshold.
345 return _space_info->max_capacity() / 100 * ShenandoahMinFreeThreshold;
346 }
347
348 ShenandoahAllocationRate::ShenandoahAllocationRate() :
349 _last_sample_time(os::elapsedTime()),
350 _last_sample_value(0),
351 _interval_sec(1.0 / ShenandoahAdaptiveSampleFrequencyHz),
352 _rate(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor),
353 _rate_avg(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor) {
354 }
355
356 double ShenandoahAllocationRate::sample(size_t allocated) {
357 double now = os::elapsedTime();
358 double rate = 0.0;
359 if (now - _last_sample_time > _interval_sec) {
360 if (allocated >= _last_sample_value) {
361 rate = instantaneous_rate(now, allocated);
362 _rate.add(rate);
363 _rate_avg.add(_rate.avg());
364 }
365
366 _last_sample_time = now;
367 _last_sample_value = allocated;
|