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 / 100 * ShenandoahMinFreeThreshold) + 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: %zu%s, Actual Free: "
100 "%zu%s, Max Evacuation: %zu%s, Min Garbage: %zu%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(data, size, compare_by_garbage);
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].get_region();
114
216 // 2. The typical workload changes. "Suddenly", our typical workload of N TPS increases to N+delta TPS. This means
217 // our average allocation rate needs to be adjusted. Once again, we need the "spike" accomodation to give us
218 // enough runway to recalibrate our "average allocation rate".
219 //
220 // 3. Though there is an "average" allocation rate, a given workload's demand for allocation may be very bursty. We
221 // allocate a bunch of LABs during the 5 ms that follow completion of a GC, then we perform no more allocations for
222 // the next 150 ms. It seems we want the "spike" to represent the maximum divergence from average within the
223 // period of time between consecutive evaluation of the should_start_gc() service. Here's the thinking:
224 //
225 // a) Between now and the next time I ask whether should_start_gc(), we might experience a spike representing
226 // the anticipated burst of allocations. If that would put us over budget, then we should start GC immediately.
227 // b) Between now and the anticipated depletion of allocation pool, there may be two or more bursts of allocations.
228 // If there are more than one of these bursts, we can "approximate" that these will be separated by spans of
229 // time with very little or no allocations so the "average" allocation rate should be a suitable approximation
230 // of how this will behave.
231 //
232 // For cases 1 and 2, we need to "quickly" recalibrate the average allocation rate whenever we detect a change
233 // in operation mode. We want some way to decide that the average rate has changed, while keeping average
234 // allocation rate computation independent.
235 bool ShenandoahAdaptiveHeuristics::should_start_gc() {
236 size_t capacity = _space_info->soft_max_capacity();
237 size_t available = _space_info->soft_available();
238 size_t allocated = _space_info->bytes_allocated_since_gc_start();
239
240 log_debug(gc)("should_start_gc? available: %zu, soft_max_capacity: %zu"
241 ", allocated: %zu", available, capacity, allocated);
242
243 if (_start_gc_is_pending) {
244 log_trigger("GC start is already pending");
245 return true;
246 }
247
248 // Track allocation rate even if we decide to start a cycle for other reasons.
249 double rate = _allocation_rate.sample(allocated);
250 _last_trigger = OTHER;
251
252 size_t min_threshold = min_free_threshold();
253 if (available < min_threshold) {
254 log_trigger("Free (%zu%s) is below minimum threshold (%zu%s)",
255 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
256 byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold));
332 break;
333 case OTHER:
334 // nothing to adjust here.
335 break;
336 default:
337 ShouldNotReachHere();
338 }
339 }
340
341 void ShenandoahAdaptiveHeuristics::adjust_margin_of_error(double amount) {
342 _margin_of_error_sd = saturate(_margin_of_error_sd + amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
343 log_debug(gc, ergo)("Margin of error now %.2f", _margin_of_error_sd);
344 }
345
346 void ShenandoahAdaptiveHeuristics::adjust_spike_threshold(double amount) {
347 _spike_threshold_sd = saturate(_spike_threshold_sd - amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
348 log_debug(gc, ergo)("Spike threshold now: %.2f", _spike_threshold_sd);
349 }
350
351 size_t ShenandoahAdaptiveHeuristics::min_free_threshold() {
352 // Note that soft_max_capacity() / 100 * min_free_threshold is smaller than max_capacity() / 100 * min_free_threshold.
353 // We want to behave conservatively here, so use max_capacity(). By returning a larger value, we cause the GC to
354 // trigger when the remaining amount of free shrinks below the larger threshold.
355 return _space_info->max_capacity() / 100 * ShenandoahMinFreeThreshold;
356 }
357
358 ShenandoahAllocationRate::ShenandoahAllocationRate() :
359 _last_sample_time(os::elapsedTime()),
360 _last_sample_value(0),
361 _interval_sec(1.0 / ShenandoahAdaptiveSampleFrequencyHz),
362 _rate(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor),
363 _rate_avg(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor) {
364 }
365
366 double ShenandoahAllocationRate::sample(size_t allocated) {
367 double now = os::elapsedTime();
368 double rate = 0.0;
369 if (now - _last_sample_time > _interval_sec) {
370 if (allocated >= _last_sample_value) {
371 rate = instantaneous_rate(now, allocated);
372 _rate.add(rate);
373 _rate_avg.add(_rate.avg());
374 }
375
|
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 = ShenandoahHeap::heap()->soft_max_capacity();
95 size_t max_cset = (size_t)((1.0 * capacity / 100 * ShenandoahEvacReserve) / ShenandoahEvacWaste);
96 size_t free_target = (capacity / 100 * ShenandoahMinFreeThreshold) + 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: %zu%s, Actual Free: "
100 "%zu%s, Max Evacuation: %zu%s, Min Garbage: %zu%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(data, size, compare_by_garbage);
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].get_region();
114
216 // 2. The typical workload changes. "Suddenly", our typical workload of N TPS increases to N+delta TPS. This means
217 // our average allocation rate needs to be adjusted. Once again, we need the "spike" accomodation to give us
218 // enough runway to recalibrate our "average allocation rate".
219 //
220 // 3. Though there is an "average" allocation rate, a given workload's demand for allocation may be very bursty. We
221 // allocate a bunch of LABs during the 5 ms that follow completion of a GC, then we perform no more allocations for
222 // the next 150 ms. It seems we want the "spike" to represent the maximum divergence from average within the
223 // period of time between consecutive evaluation of the should_start_gc() service. Here's the thinking:
224 //
225 // a) Between now and the next time I ask whether should_start_gc(), we might experience a spike representing
226 // the anticipated burst of allocations. If that would put us over budget, then we should start GC immediately.
227 // b) Between now and the anticipated depletion of allocation pool, there may be two or more bursts of allocations.
228 // If there are more than one of these bursts, we can "approximate" that these will be separated by spans of
229 // time with very little or no allocations so the "average" allocation rate should be a suitable approximation
230 // of how this will behave.
231 //
232 // For cases 1 and 2, we need to "quickly" recalibrate the average allocation rate whenever we detect a change
233 // in operation mode. We want some way to decide that the average rate has changed, while keeping average
234 // allocation rate computation independent.
235 bool ShenandoahAdaptiveHeuristics::should_start_gc() {
236 size_t capacity = ShenandoahHeap::heap()->soft_max_capacity();
237 size_t available = _space_info->soft_available();
238 size_t allocated = _space_info->bytes_allocated_since_gc_start();
239
240 log_debug(gc)("should_start_gc? available: %zu, soft_max_capacity: %zu"
241 ", allocated: %zu", available, capacity, allocated);
242
243 if (_start_gc_is_pending) {
244 log_trigger("GC start is already pending");
245 return true;
246 }
247
248 // Track allocation rate even if we decide to start a cycle for other reasons.
249 double rate = _allocation_rate.sample(allocated);
250 _last_trigger = OTHER;
251
252 size_t min_threshold = min_free_threshold();
253 if (available < min_threshold) {
254 log_trigger("Free (%zu%s) is below minimum threshold (%zu%s)",
255 byte_size_in_proper_unit(available), proper_unit_for_byte_size(available),
256 byte_size_in_proper_unit(min_threshold), proper_unit_for_byte_size(min_threshold));
332 break;
333 case OTHER:
334 // nothing to adjust here.
335 break;
336 default:
337 ShouldNotReachHere();
338 }
339 }
340
341 void ShenandoahAdaptiveHeuristics::adjust_margin_of_error(double amount) {
342 _margin_of_error_sd = saturate(_margin_of_error_sd + amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
343 log_debug(gc, ergo)("Margin of error now %.2f", _margin_of_error_sd);
344 }
345
346 void ShenandoahAdaptiveHeuristics::adjust_spike_threshold(double amount) {
347 _spike_threshold_sd = saturate(_spike_threshold_sd - amount, MINIMUM_CONFIDENCE, MAXIMUM_CONFIDENCE);
348 log_debug(gc, ergo)("Spike threshold now: %.2f", _spike_threshold_sd);
349 }
350
351 size_t ShenandoahAdaptiveHeuristics::min_free_threshold() {
352 return ShenandoahHeap::heap()->soft_max_capacity() / 100 * ShenandoahMinFreeThreshold;
353 }
354
355 ShenandoahAllocationRate::ShenandoahAllocationRate() :
356 _last_sample_time(os::elapsedTime()),
357 _last_sample_value(0),
358 _interval_sec(1.0 / ShenandoahAdaptiveSampleFrequencyHz),
359 _rate(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor),
360 _rate_avg(int(ShenandoahAdaptiveSampleSizeSeconds * ShenandoahAdaptiveSampleFrequencyHz), ShenandoahAdaptiveDecayFactor) {
361 }
362
363 double ShenandoahAllocationRate::sample(size_t allocated) {
364 double now = os::elapsedTime();
365 double rate = 0.0;
366 if (now - _last_sample_time > _interval_sec) {
367 if (allocated >= _last_sample_value) {
368 rate = instantaneous_rate(now, allocated);
369 _rate.add(rate);
370 _rate_avg.add(_rate.avg());
371 }
372
|