1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. 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/g1/g1CollectedHeap.hpp"
27 #include "gc/g1/g1HeapSizingPolicy.hpp"
28 #include "gc/g1/g1Analytics.hpp"
29 #include "logging/log.hpp"
30 #include "runtime/globals.hpp"
31 #include "utilities/debug.hpp"
32 #include "utilities/globalDefinitions.hpp"
33
34 G1HeapSizingPolicy* G1HeapSizingPolicy::create(const G1CollectedHeap* g1h, const G1Analytics* analytics) {
35 return new G1HeapSizingPolicy(g1h, analytics);
36 }
37
38 G1HeapSizingPolicy::G1HeapSizingPolicy(const G1CollectedHeap* g1h, const G1Analytics* analytics) :
39 _g1h(g1h),
40 _analytics(analytics),
41 _num_prev_pauses_for_heuristics(analytics->number_of_recorded_pause_times()) {
42
43 assert(MinOverThresholdForGrowth < _num_prev_pauses_for_heuristics, "Threshold must be less than %u", _num_prev_pauses_for_heuristics);
44 clear_ratio_check_data();
45 }
46
47 void G1HeapSizingPolicy::clear_ratio_check_data() {
48 _ratio_over_threshold_count = 0;
49 _ratio_over_threshold_sum = 0.0;
50 _pauses_since_start = 0;
51 }
52
53 double G1HeapSizingPolicy::scale_with_heap(double pause_time_threshold) {
54 double threshold = pause_time_threshold;
55 // If the heap is at less than half its maximum size, scale the threshold down,
56 // to a limit of 1%. Thus the smaller the heap is, the more likely it is to expand,
57 // though the scaling code will likely keep the increase small.
58 if (_g1h->capacity() <= _g1h->max_capacity() / 2) {
59 threshold *= (double)_g1h->capacity() / (double)(_g1h->max_capacity() / 2);
60 threshold = MAX2(threshold, 0.01);
61 }
62
63 return threshold;
64 }
65
66 static void log_expansion(double short_term_pause_time_ratio,
67 double long_term_pause_time_ratio,
68 double threshold,
69 double pause_time_ratio,
70 bool fully_expanded,
71 size_t resize_bytes) {
72
73 log_debug(gc, ergo, heap)("Heap expansion: "
74 "short term pause time ratio %1.2f%% long term pause time ratio %1.2f%% "
75 "threshold %1.2f%% pause time ratio %1.2f%% fully expanded %s "
76 "resize by " SIZE_FORMAT "B",
77 short_term_pause_time_ratio * 100.0,
78 long_term_pause_time_ratio * 100.0,
79 threshold * 100.0,
80 pause_time_ratio * 100.0,
81 BOOL_TO_STR(fully_expanded),
82 resize_bytes);
83 }
84
85 size_t G1HeapSizingPolicy::expansion_amount() {
86 assert(GCTimeRatio > 0, "must be");
87
88 double long_term_pause_time_ratio = _analytics->long_term_pause_time_ratio();
89 double short_term_pause_time_ratio = _analytics->short_term_pause_time_ratio();
90 const double pause_time_threshold = 1.0 / (1.0 + GCTimeRatio);
91 double threshold = scale_with_heap(pause_time_threshold);
92
93 size_t expand_bytes = 0;
94
95 if (_g1h->capacity() == _g1h->max_capacity()) {
96 log_expansion(short_term_pause_time_ratio, long_term_pause_time_ratio,
97 threshold, pause_time_threshold, true, 0);
98 clear_ratio_check_data();
99 return expand_bytes;
100 }
101
102 // If the last GC time ratio is over the threshold, increment the count of
103 // times it has been exceeded, and add this ratio to the sum of exceeded
104 // ratios.
105 if (short_term_pause_time_ratio > threshold) {
106 _ratio_over_threshold_count++;
107 _ratio_over_threshold_sum += short_term_pause_time_ratio;
108 }
109
110 log_trace(gc, ergo, heap)("Heap expansion triggers: pauses since start: %u "
111 "num prev pauses for heuristics: %u "
112 "ratio over threshold count: %u",
113 _pauses_since_start,
114 _num_prev_pauses_for_heuristics,
115 _ratio_over_threshold_count);
116
117 // Check if we've had enough GC time ratio checks that were over the
118 // threshold to trigger an expansion. We'll also expand if we've
119 // reached the end of the history buffer and the average of all entries
120 // is still over the threshold. This indicates a smaller number of GCs were
121 // long enough to make the average exceed the threshold.
122 bool filled_history_buffer = _pauses_since_start == _num_prev_pauses_for_heuristics;
123 if ((_ratio_over_threshold_count == MinOverThresholdForGrowth) ||
124 (filled_history_buffer && (long_term_pause_time_ratio > threshold))) {
125 size_t min_expand_bytes = HeapRegion::GrainBytes;
126 size_t reserved_bytes = _g1h->max_capacity();
127 size_t committed_bytes = _g1h->capacity();
128 size_t uncommitted_bytes = reserved_bytes - committed_bytes;
129 size_t expand_bytes_via_pct =
130 uncommitted_bytes * G1ExpandByPercentOfAvailable / 100;
131 double scale_factor = 1.0;
132
133 // If the current size is less than 1/4 of the Initial heap size, expand
134 // by half of the delta between the current and Initial sizes. IE, grow
135 // back quickly.
136 //
137 // Otherwise, take the current size, or G1ExpandByPercentOfAvailable % of
138 // the available expansion space, whichever is smaller, as the base
139 // expansion size. Then possibly scale this size according to how much the
140 // threshold has (on average) been exceeded by. If the delta is small
141 // (less than the StartScaleDownAt value), scale the size down linearly, but
142 // not by less than MinScaleDownFactor. If the delta is large (greater than
143 // the StartScaleUpAt value), scale up, but adding no more than MaxScaleUpFactor
144 // times the base size. The scaling will be linear in the range from
145 // StartScaleUpAt to (StartScaleUpAt + ScaleUpRange). In other words,
146 // ScaleUpRange sets the rate of scaling up.
147 if (committed_bytes < InitialHeapSize / 4) {
148 expand_bytes = (InitialHeapSize - committed_bytes) / 2;
149 } else {
150 double const MinScaleDownFactor = 0.2;
151 double const MaxScaleUpFactor = 2;
152 double const StartScaleDownAt = pause_time_threshold;
153 double const StartScaleUpAt = pause_time_threshold * 1.5;
154 double const ScaleUpRange = pause_time_threshold * 2.0;
155
156 double ratio_delta;
157 if (filled_history_buffer) {
158 ratio_delta = long_term_pause_time_ratio - threshold;
159 } else {
160 ratio_delta = (_ratio_over_threshold_sum / _ratio_over_threshold_count) - threshold;
161 }
162
163 expand_bytes = MIN2(expand_bytes_via_pct, committed_bytes);
164 if (ratio_delta < StartScaleDownAt) {
165 scale_factor = ratio_delta / StartScaleDownAt;
166 scale_factor = MAX2(scale_factor, MinScaleDownFactor);
167 } else if (ratio_delta > StartScaleUpAt) {
168 scale_factor = 1 + ((ratio_delta - StartScaleUpAt) / ScaleUpRange);
169 scale_factor = MIN2(scale_factor, MaxScaleUpFactor);
170 }
171 }
172
173 expand_bytes = static_cast<size_t>(expand_bytes * scale_factor);
174
175 // Ensure the expansion size is at least the minimum growth amount
176 // and at most the remaining uncommitted byte size.
177 expand_bytes = clamp(expand_bytes, min_expand_bytes, uncommitted_bytes);
178
179 clear_ratio_check_data();
180 } else {
181 // An expansion was not triggered. If we've started counting, increment
182 // the number of checks we've made in the current window. If we've
183 // reached the end of the window without resizing, clear the counters to
184 // start again the next time we see a ratio above the threshold.
185 if (_ratio_over_threshold_count > 0) {
186 _pauses_since_start++;
187 if (_pauses_since_start > _num_prev_pauses_for_heuristics) {
188 clear_ratio_check_data();
189 }
190 }
191 }
192
193 log_expansion(short_term_pause_time_ratio, long_term_pause_time_ratio,
194 threshold, pause_time_threshold, false, expand_bytes);
195
196 return expand_bytes;
197 }
|
1 /*
2 * Copyright (c) 2016, 2020, Oracle and/or its affiliates. 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/g1/g1_globals.hpp"
27 #include "gc/g1/g1CollectedHeap.hpp"
28 #include "gc/g1/g1HeapSizingPolicy.hpp"
29 #include "gc/g1/g1Analytics.hpp"
30 #include "logging/log.hpp"
31 #include "runtime/globals.hpp"
32 #include "utilities/debug.hpp"
33 #include "utilities/globalDefinitions.hpp"
34
35 G1HeapSizingPolicy* G1HeapSizingPolicy::create(const G1CollectedHeap* g1h, const G1Analytics* analytics) {
36 return new G1HeapSizingPolicy(g1h, analytics);
37 }
38
39 G1HeapSizingPolicy::G1HeapSizingPolicy(const G1CollectedHeap* g1h, const G1Analytics* analytics) :
40 _g1h(g1h),
41 _analytics(analytics),
42 _long_term_interval(analytics->number_of_recorded_pause_times()),
43 // Bias for expansion at startup; the +1 is to counter the first sample always
44 // being 0.0, i.e. lower than any threshold.
45 _ratio_exceeds_threshold(MinOverThresholdForExpansion / 2 + 1),
46 _recent_pause_ratios(analytics->number_of_recorded_pause_times()),
47 _long_term_count(0) {
48
49 assert(_ratio_exceeds_threshold < MinOverThresholdForExpansion,
50 "Initial ratio counter value too high.");
51 assert(_ratio_exceeds_threshold > -MinOverThresholdForExpansion,
52 "Initial ratio counter value too low.");
53 assert(MinOverThresholdForExpansion < _long_term_interval,
54 "Expansion threshold count must be less than %u", _long_term_interval);
55 assert(MinOverThresholdForShrink < _long_term_interval,
56 "Shrink threshold count must be less than %u", _long_term_interval);
57 }
58
59 void G1HeapSizingPolicy::reset_ratio_tracking_data() {
60 _long_term_count = 0;
61 _ratio_exceeds_threshold = 0;
62 // Keep the recent gc time ratio data.
63 }
64
65 void G1HeapSizingPolicy::decay_ratio_tracking_data() {
66 _long_term_count = 0;
67 _ratio_exceeds_threshold /= 2;
68 // Keep the recent gc time ratio data.
69 }
70
71 double G1HeapSizingPolicy::scale_with_heap(double pause_time_threshold) {
72 double threshold = pause_time_threshold;
73 // If the heap is at less than half its maximum size, scale the threshold down,
74 // to a limit of 1%. Thus the smaller the heap is, the more likely it is to expand,
75 // though the scaling code will likely keep the increase small.
76 if (_g1h->capacity() <= _g1h->max_capacity() / 2) {
77 threshold *= (double)_g1h->capacity() / (double)(_g1h->max_capacity() / 2);
78 threshold = MAX2(threshold, 0.01);
79 }
80
81 return threshold;
82 }
83
84 double G1HeapSizingPolicy::scale_resize_ratio_delta(double ratio_delta) {
85 // If the delta is small (less than the StartScaleDownAt value), scale the size
86 // down linearly, but not by less than MinScaleDownFactor. If the delta is large
87 // (greater than the StartScaleUpAt value), scale up, but adding no more than
88 // MaxScaleUpFactor times the base size. The scaling will be linear in the range
89 // from StartScaleUpAt to (StartScaleUpAt + ScaleUpRange). In other words,
90 // ScaleUpRange sets the rate of scaling up.
91 double const MinScaleDownFactor = 0.2;
92 double const MaxScaleUpFactor = 2.0;
93
94 double const StartScaleDownAt = 1.0;
95 double const StartScaleUpAt = 1.5;
96 double const ScaleUpRange = 4.0;
97
98 double scale_factor;
99 if (ratio_delta < StartScaleDownAt) {
100 scale_factor = ratio_delta / StartScaleDownAt;
101 scale_factor = MAX2(scale_factor, MinScaleDownFactor);
102 } else if (ratio_delta > StartScaleUpAt) {
103 scale_factor = 1 + ((ratio_delta - StartScaleUpAt) / ScaleUpRange);
104 scale_factor = MIN2(scale_factor, MaxScaleUpFactor);
105 }
106 log_error(gc)("scaling ratio %1.2f scale %1.2f", ratio_delta, scale_factor);
107 return scale_factor;
108 }
109
110 static void log_resize(double short_term_pause_time_ratio,
111 double long_term_pause_time_ratio,
112 double lower_threshold,
113 double upper_threshold,
114 double pause_time_ratio,
115 bool at_limit,
116 ssize_t resize_bytes) {
117
118 log_debug(gc, ergo, heap)("Heap resize: "
119 "short term pause time ratio %1.2f%% long term pause time ratio %1.2f%% "
120 "lower threshold %1.2f%% upper threshold %1.2f%% pause time ratio %1.2f%% "
121 "at limit %s resize by " SSIZE_FORMAT "B",
122 short_term_pause_time_ratio * 100.0,
123 long_term_pause_time_ratio * 100.0,
124 lower_threshold * 100.0,
125 upper_threshold * 100.0,
126 pause_time_ratio * 100.0,
127 BOOL_TO_STR(at_limit),
128 resize_bytes);
129 }
130
131 ssize_t G1HeapSizingPolicy::resize_amount_after_young_gc() {
132 assert(GCTimeRatio > 0, "must be");
133
134 double long_term_pause_time_ratio = _analytics->long_term_pause_time_ratio();
135 double short_term_pause_time_ratio = _analytics->short_term_pause_time_ratio();
136
137 // Calculate gc time ratio thresholds:
138 // - upper threshold, directly based on GCTimeRatio. We do not want to exceed
139 // this.
140 // - lower threshold, we do not want to go under.
141 // - mid threshold, halfway between upper and lower threshold, represents the
142 // actual target when resizing the heap.
143 const double pause_time_threshold = 1.0 / (1.0 + GCTimeRatio);
144 const double min_gc_time_ratio_ratio = G1MinimumPercentOfGCTimeRatio / 100.0;
145 double upper_threshold = scale_with_heap(pause_time_threshold);
146 double lower_threshold = upper_threshold * min_gc_time_ratio_ratio;
147
148 // Explicitly use GCTimeRatio based threshold to more quickly expand and shrink
149 // at smaller heap sizes.
150 double mid_threshold = (upper_threshold + lower_threshold) / 2;
151
152 // If the short term GC time ratio exceeds a threshold, increment the occurrence
153 // counter.
154 if (short_term_pause_time_ratio > upper_threshold) {
155 _ratio_exceeds_threshold++;
156 } else if (short_term_pause_time_ratio < lower_threshold) {
157 _ratio_exceeds_threshold--;
158 }
159 double ratio_delta = (short_term_pause_time_ratio - mid_threshold) / mid_threshold;
160 // Ignore very first sample as it is garbage.
161 if (_long_term_count != 0 || _recent_pause_ratios.num() != 0) {
162 _recent_pause_ratios.add(ratio_delta);
163 }
164 _long_term_count++;
165
166 log_trace(gc, ergo, heap)("Heap resize triggers: long term count: %u "
167 "long term interval: %u "
168 "delta: %1.2f "
169 "ratio exceeds threshold count: %d",
170 _long_term_count,
171 _long_term_interval,
172 ratio_delta,
173 _ratio_exceeds_threshold);
174
175 log_debug(gc, ergo, heap)("Heap triggers: pauses-since-start: %u num-prev-pauses-for-heuristics: %u ratio-exceeds-threshold-count: %d",
176 _recent_pause_ratios.num(), _long_term_interval, _ratio_exceeds_threshold);
177
178 // Check if there is a short- or long-term need for resizing, expansion first.
179 //
180 // Short-term resizing need is detected by exceeding the upper or lower thresholds
181 // multiple times, tracked in _ratio_exceeds_threshold. If it contains a large
182 // positive or negative (larger than the respective thresholds), we trigger
183 // resizing calculation.
184 //
185 // Slowly occurring long-term changes to the actual gc time ratios are checked
186 // only every once a while.
187 //
188 // The _ratio_exceeds_threshold value is reset after each resize, or slowly
189 // decayed if nothing happens.
190
191 ssize_t resize_bytes = 0;
192
193 bool check_long_term_resize = _long_term_count == _long_term_interval;
194
195 if ((_ratio_exceeds_threshold == MinOverThresholdForExpansion) ||
196 (check_long_term_resize && (long_term_pause_time_ratio > upper_threshold))) {
197
198 // Short-cut the case when we are already fully expanded.
199 if (_g1h->capacity() == _g1h->max_capacity()) {
200 log_resize(short_term_pause_time_ratio, long_term_pause_time_ratio,
201 lower_threshold, upper_threshold, pause_time_threshold, true, 0);
202 reset_ratio_tracking_data();
203 return resize_bytes;
204 }
205
206 size_t reserved_bytes = _g1h->max_capacity();
207 size_t committed_bytes = _g1h->capacity();
208 size_t uncommitted_bytes = reserved_bytes - committed_bytes;
209 size_t expand_bytes_via_pct =
210 uncommitted_bytes * G1ExpandByPercentOfAvailable / 100;
211 size_t min_expand_bytes = MIN2(HeapRegion::GrainBytes, uncommitted_bytes);
212 double scale_factor = 1.0;
213
214 // If the current size is less than 1/4 of the Initial heap size, expand
215 // by half of the delta between the current and Initial sizes. IE, grow
216 // back quickly.
217 //
218 // Otherwise, take the current size, or G1ExpandByPercentOfAvailable % of
219 // the available expansion space, whichever is smaller, as the base
220 // expansion size. Then possibly scale this size according to how much the
221 // threshold has (on average) been exceeded by.
222 if (committed_bytes < InitialHeapSize / 4) {
223 resize_bytes = (InitialHeapSize - committed_bytes) / 2;
224 } else {
225 double ratio_delta = _recent_pause_ratios.avg();
226 if (check_long_term_resize) {
227 ratio_delta = MAX2(ratio_delta, (long_term_pause_time_ratio - mid_threshold) / mid_threshold);
228 }
229 log_error(gc)("expand deltas long %1.2f short %1.2f check long term %u", (long_term_pause_time_ratio - mid_threshold) / mid_threshold, _recent_pause_ratios.avg(), check_long_term_resize);
230 scale_factor = scale_resize_ratio_delta(fabsd(ratio_delta));
231
232 resize_bytes = MIN2(expand_bytes_via_pct, committed_bytes);
233 }
234
235 resize_bytes = static_cast<size_t>(resize_bytes * scale_factor);
236
237 // Ensure the expansion size is at least the minimum growth amount
238 // and at most the remaining uncommitted byte size.
239 resize_bytes = clamp((size_t)resize_bytes, min_expand_bytes, uncommitted_bytes);
240
241 reset_ratio_tracking_data();
242 } else if ((_ratio_exceeds_threshold == -MinOverThresholdForShrink) ||
243 (check_long_term_resize && (long_term_pause_time_ratio < lower_threshold))) {
244
245 if (_g1h->capacity() == _g1h->min_capacity()) {
246 log_resize(short_term_pause_time_ratio, long_term_pause_time_ratio,
247 lower_threshold, upper_threshold, pause_time_threshold, true, 0);
248 reset_ratio_tracking_data();
249 return resize_bytes;
250 }
251
252 // Shrink.
253 double ratio_delta = _recent_pause_ratios.avg();
254 if (check_long_term_resize) {
255 // Intentionally use the max to limit the shrinking a bit.
256 ratio_delta = MAX2(ratio_delta, (long_term_pause_time_ratio - mid_threshold) / mid_threshold);
257 }
258 log_error(gc)("shrink deltas long %1.2f short %1.2f long term %u", (long_term_pause_time_ratio - mid_threshold) / mid_threshold, _recent_pause_ratios.avg(), check_long_term_resize);
259
260 double scale_factor = scale_resize_ratio_delta(fabsd(ratio_delta));
261 scale_factor = clamp(scale_factor, 0.0, G1ShrinkByPercentOfAvailable / 100.0);
262
263 // We are at the end of GC, so free regions are at maximum.
264 size_t free_regions = _g1h->num_free_regions() * (1 - G1ReservePercent / 100.0);
265
266 resize_bytes = -((double)HeapRegion::GrainBytes * scale_factor * free_regions);
267
268 log_debug(gc)("shrink log: filled_hist %d target ratio: %1.2f%% ratio delta: %1.2f%% scale factor %1.2f%% free_regions " SIZE_FORMAT " resize_bytes " SSIZE_FORMAT,
269 check_long_term_resize, mid_threshold * 100.0, _recent_pause_ratios.avg() * 100.0, scale_factor * 100.0, free_regions, resize_bytes);
270
271 reset_ratio_tracking_data();
272 } else if (check_long_term_resize) {
273 // A resize has not been triggered, but the long term counter overflowed.
274 decay_ratio_tracking_data();
275 }
276
277 log_resize(short_term_pause_time_ratio, long_term_pause_time_ratio,
278 lower_threshold, upper_threshold, pause_time_threshold,
279 false, resize_bytes);
280
281 return resize_bytes;
282 }
283
284 size_t G1HeapSizingPolicy::target_heap_capacity(size_t used_bytes, uintx free_ratio) const {
285 const double free_percentage = (double) free_ratio / 100.0;
286 const double used_percentage = 1.0 - free_percentage;
287
288 // We have to be careful here as these two calculations can overflow
289 // 32-bit size_t's.
290 double used_bytes_d = (double) used_bytes;
291 double desired_capacity_d = used_bytes_d / used_percentage;
292 // Let's make sure that they are both under the max heap size, which
293 // by default will make it fit into a size_t.
294 double desired_capacity_upper_bound = (double) MaxHeapSize;
295 desired_capacity_d = MIN2(desired_capacity_d, desired_capacity_upper_bound);
296 // We can now safely turn it into size_t's.
297 return (size_t) desired_capacity_d;
298 }
|