Tue, 23 Nov 2010 13:22:55 -0800
6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg
1 /*
2 * Copyright (c) 2004, 2010, 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 */
25 #include "precompiled.hpp"
26 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
27 #include "gc_interface/gcCause.hpp"
28 #include "memory/collectorPolicy.hpp"
29 #include "runtime/timer.hpp"
30 #include "utilities/ostream.hpp"
31 elapsedTimer AdaptiveSizePolicy::_minor_timer;
32 elapsedTimer AdaptiveSizePolicy::_major_timer;
34 // The throughput goal is implemented as
35 // _throughput_goal = 1 - ( 1 / (1 + gc_cost_ratio))
36 // gc_cost_ratio is the ratio
37 // application cost / gc cost
38 // For example a gc_cost_ratio of 4 translates into a
39 // throughput goal of .80
41 AdaptiveSizePolicy::AdaptiveSizePolicy(size_t init_eden_size,
42 size_t init_promo_size,
43 size_t init_survivor_size,
44 double gc_pause_goal_sec,
45 uint gc_cost_ratio) :
46 _eden_size(init_eden_size),
47 _promo_size(init_promo_size),
48 _survivor_size(init_survivor_size),
49 _gc_pause_goal_sec(gc_pause_goal_sec),
50 _throughput_goal(1.0 - double(1.0 / (1.0 + (double) gc_cost_ratio))),
51 _gc_overhead_limit_exceeded(false),
52 _print_gc_overhead_limit_would_be_exceeded(false),
53 _gc_overhead_limit_count(0),
54 _latest_minor_mutator_interval_seconds(0),
55 _threshold_tolerance_percent(1.0 + ThresholdTolerance/100.0),
56 _young_gen_change_for_minor_throughput(0),
57 _old_gen_change_for_major_throughput(0) {
58 assert(AdaptiveSizePolicyGCTimeLimitThreshold > 0,
59 "No opportunity to clear SoftReferences before GC overhead limit");
60 _avg_minor_pause =
61 new AdaptivePaddedAverage(AdaptiveTimeWeight, PausePadding);
62 _avg_minor_interval = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
63 _avg_minor_gc_cost = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
64 _avg_major_gc_cost = new AdaptiveWeightedAverage(AdaptiveTimeWeight);
66 _avg_young_live = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
67 _avg_old_live = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
68 _avg_eden_live = new AdaptiveWeightedAverage(AdaptiveSizePolicyWeight);
70 _avg_survived = new AdaptivePaddedAverage(AdaptiveSizePolicyWeight,
71 SurvivorPadding);
72 _avg_pretenured = new AdaptivePaddedNoZeroDevAverage(
73 AdaptiveSizePolicyWeight,
74 SurvivorPadding);
76 _minor_pause_old_estimator =
77 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
78 _minor_pause_young_estimator =
79 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
80 _minor_collection_estimator =
81 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
82 _major_collection_estimator =
83 new LinearLeastSquareFit(AdaptiveSizePolicyWeight);
85 // Start the timers
86 _minor_timer.start();
88 _young_gen_policy_is_ready = false;
89 }
91 bool AdaptiveSizePolicy::tenuring_threshold_change() const {
92 return decrement_tenuring_threshold_for_gc_cost() ||
93 increment_tenuring_threshold_for_gc_cost() ||
94 decrement_tenuring_threshold_for_survivor_limit();
95 }
97 void AdaptiveSizePolicy::minor_collection_begin() {
98 // Update the interval time
99 _minor_timer.stop();
100 // Save most recent collection time
101 _latest_minor_mutator_interval_seconds = _minor_timer.seconds();
102 _minor_timer.reset();
103 _minor_timer.start();
104 }
106 void AdaptiveSizePolicy::update_minor_pause_young_estimator(
107 double minor_pause_in_ms) {
108 double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
109 _minor_pause_young_estimator->update(eden_size_in_mbytes,
110 minor_pause_in_ms);
111 }
113 void AdaptiveSizePolicy::minor_collection_end(GCCause::Cause gc_cause) {
114 // Update the pause time.
115 _minor_timer.stop();
117 if (gc_cause != GCCause::_java_lang_system_gc ||
118 UseAdaptiveSizePolicyWithSystemGC) {
119 double minor_pause_in_seconds = _minor_timer.seconds();
120 double minor_pause_in_ms = minor_pause_in_seconds * MILLIUNITS;
122 // Sample for performance counter
123 _avg_minor_pause->sample(minor_pause_in_seconds);
125 // Cost of collection (unit-less)
126 double collection_cost = 0.0;
127 if ((_latest_minor_mutator_interval_seconds > 0.0) &&
128 (minor_pause_in_seconds > 0.0)) {
129 double interval_in_seconds =
130 _latest_minor_mutator_interval_seconds + minor_pause_in_seconds;
131 collection_cost =
132 minor_pause_in_seconds / interval_in_seconds;
133 _avg_minor_gc_cost->sample(collection_cost);
134 // Sample for performance counter
135 _avg_minor_interval->sample(interval_in_seconds);
136 }
138 // The policy does not have enough data until at least some
139 // minor collections have been done.
140 _young_gen_policy_is_ready =
141 (_avg_minor_gc_cost->count() >= AdaptiveSizePolicyReadyThreshold);
143 // Calculate variables used to estimate pause time vs. gen sizes
144 double eden_size_in_mbytes = ((double)_eden_size)/((double)M);
145 update_minor_pause_young_estimator(minor_pause_in_ms);
146 update_minor_pause_old_estimator(minor_pause_in_ms);
148 if (PrintAdaptiveSizePolicy && Verbose) {
149 gclog_or_tty->print("AdaptiveSizePolicy::minor_collection_end: "
150 "minor gc cost: %f average: %f", collection_cost,
151 _avg_minor_gc_cost->average());
152 gclog_or_tty->print_cr(" minor pause: %f minor period %f",
153 minor_pause_in_ms,
154 _latest_minor_mutator_interval_seconds * MILLIUNITS);
155 }
157 // Calculate variable used to estimate collection cost vs. gen sizes
158 assert(collection_cost >= 0.0, "Expected to be non-negative");
159 _minor_collection_estimator->update(eden_size_in_mbytes, collection_cost);
160 }
162 // Interval times use this timer to measure the mutator time.
163 // Reset the timer after the GC pause.
164 _minor_timer.reset();
165 _minor_timer.start();
166 }
168 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden,
169 uint percent_change) {
170 size_t eden_heap_delta;
171 eden_heap_delta = cur_eden / 100 * percent_change;
172 return eden_heap_delta;
173 }
175 size_t AdaptiveSizePolicy::eden_increment(size_t cur_eden) {
176 return eden_increment(cur_eden, YoungGenerationSizeIncrement);
177 }
179 size_t AdaptiveSizePolicy::eden_decrement(size_t cur_eden) {
180 size_t eden_heap_delta = eden_increment(cur_eden) /
181 AdaptiveSizeDecrementScaleFactor;
182 return eden_heap_delta;
183 }
185 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo,
186 uint percent_change) {
187 size_t promo_heap_delta;
188 promo_heap_delta = cur_promo / 100 * percent_change;
189 return promo_heap_delta;
190 }
192 size_t AdaptiveSizePolicy::promo_increment(size_t cur_promo) {
193 return promo_increment(cur_promo, TenuredGenerationSizeIncrement);
194 }
196 size_t AdaptiveSizePolicy::promo_decrement(size_t cur_promo) {
197 size_t promo_heap_delta = promo_increment(cur_promo);
198 promo_heap_delta = promo_heap_delta / AdaptiveSizeDecrementScaleFactor;
199 return promo_heap_delta;
200 }
202 double AdaptiveSizePolicy::time_since_major_gc() const {
203 _major_timer.stop();
204 double result = _major_timer.seconds();
205 _major_timer.start();
206 return result;
207 }
209 // Linear decay of major gc cost
210 double AdaptiveSizePolicy::decaying_major_gc_cost() const {
211 double major_interval = major_gc_interval_average_for_decay();
212 double major_gc_cost_average = major_gc_cost();
213 double decayed_major_gc_cost = major_gc_cost_average;
214 if(time_since_major_gc() > 0.0) {
215 decayed_major_gc_cost = major_gc_cost() *
216 (((double) AdaptiveSizeMajorGCDecayTimeScale) * major_interval)
217 / time_since_major_gc();
218 }
220 // The decayed cost should always be smaller than the
221 // average cost but the vagaries of finite arithmetic could
222 // produce a larger value in decayed_major_gc_cost so protect
223 // against that.
224 return MIN2(major_gc_cost_average, decayed_major_gc_cost);
225 }
227 // Use a value of the major gc cost that has been decayed
228 // by the factor
229 //
230 // average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale /
231 // time-since-last-major-gc
232 //
233 // if the average-interval-between-major-gc * AdaptiveSizeMajorGCDecayTimeScale
234 // is less than time-since-last-major-gc.
235 //
236 // In cases where there are initial major gc's that
237 // are of a relatively high cost but no later major
238 // gc's, the total gc cost can remain high because
239 // the major gc cost remains unchanged (since there are no major
240 // gc's). In such a situation the value of the unchanging
241 // major gc cost can keep the mutator throughput below
242 // the goal when in fact the major gc cost is becoming diminishingly
243 // small. Use the decaying gc cost only to decide whether to
244 // adjust for throughput. Using it also to determine the adjustment
245 // to be made for throughput also seems reasonable but there is
246 // no test case to use to decide if it is the right thing to do
247 // don't do it yet.
249 double AdaptiveSizePolicy::decaying_gc_cost() const {
250 double decayed_major_gc_cost = major_gc_cost();
251 double avg_major_interval = major_gc_interval_average_for_decay();
252 if (UseAdaptiveSizeDecayMajorGCCost &&
253 (AdaptiveSizeMajorGCDecayTimeScale > 0) &&
254 (avg_major_interval > 0.00)) {
255 double time_since_last_major_gc = time_since_major_gc();
257 // Decay the major gc cost?
258 if (time_since_last_major_gc >
259 ((double) AdaptiveSizeMajorGCDecayTimeScale) * avg_major_interval) {
261 // Decay using the time-since-last-major-gc
262 decayed_major_gc_cost = decaying_major_gc_cost();
263 if (PrintGCDetails && Verbose) {
264 gclog_or_tty->print_cr("\ndecaying_gc_cost: major interval average:"
265 " %f time since last major gc: %f",
266 avg_major_interval, time_since_last_major_gc);
267 gclog_or_tty->print_cr(" major gc cost: %f decayed major gc cost: %f",
268 major_gc_cost(), decayed_major_gc_cost);
269 }
270 }
271 }
272 double result = MIN2(1.0, decayed_major_gc_cost + minor_gc_cost());
273 return result;
274 }
277 void AdaptiveSizePolicy::clear_generation_free_space_flags() {
278 set_change_young_gen_for_min_pauses(0);
279 set_change_old_gen_for_maj_pauses(0);
281 set_change_old_gen_for_throughput(0);
282 set_change_young_gen_for_throughput(0);
283 set_decrease_for_footprint(0);
284 set_decide_at_full_gc(0);
285 }
287 void AdaptiveSizePolicy::check_gc_overhead_limit(
288 size_t young_live,
289 size_t eden_live,
290 size_t max_old_gen_size,
291 size_t max_eden_size,
292 bool is_full_gc,
293 GCCause::Cause gc_cause,
294 CollectorPolicy* collector_policy) {
296 // Ignore explicit GC's. Exiting here does not set the flag and
297 // does not reset the count. Updating of the averages for system
298 // GC's is still controlled by UseAdaptiveSizePolicyWithSystemGC.
299 if (GCCause::is_user_requested_gc(gc_cause) ||
300 GCCause::is_serviceability_requested_gc(gc_cause)) {
301 return;
302 }
303 // eden_limit is the upper limit on the size of eden based on
304 // the maximum size of the young generation and the sizes
305 // of the survivor space.
306 // The question being asked is whether the gc costs are high
307 // and the space being recovered by a collection is low.
308 // free_in_young_gen is the free space in the young generation
309 // after a collection and promo_live is the free space in the old
310 // generation after a collection.
311 //
312 // Use the minimum of the current value of the live in the
313 // young gen or the average of the live in the young gen.
314 // If the current value drops quickly, that should be taken
315 // into account (i.e., don't trigger if the amount of free
316 // space has suddenly jumped up). If the current is much
317 // higher than the average, use the average since it represents
318 // the longer term behavor.
319 const size_t live_in_eden =
320 MIN2(eden_live, (size_t) avg_eden_live()->average());
321 const size_t free_in_eden = max_eden_size > live_in_eden ?
322 max_eden_size - live_in_eden : 0;
323 const size_t free_in_old_gen = (size_t)(max_old_gen_size - avg_old_live()->average());
324 const size_t total_free_limit = free_in_old_gen + free_in_eden;
325 const size_t total_mem = max_old_gen_size + max_eden_size;
326 const double mem_free_limit = total_mem * (GCHeapFreeLimit/100.0);
327 const double mem_free_old_limit = max_old_gen_size * (GCHeapFreeLimit/100.0);
328 const double mem_free_eden_limit = max_eden_size * (GCHeapFreeLimit/100.0);
329 const double gc_cost_limit = GCTimeLimit/100.0;
330 size_t promo_limit = (size_t)(max_old_gen_size - avg_old_live()->average());
331 // But don't force a promo size below the current promo size. Otherwise,
332 // the promo size will shrink for no good reason.
333 promo_limit = MAX2(promo_limit, _promo_size);
336 if (PrintAdaptiveSizePolicy && (Verbose ||
337 (free_in_old_gen < (size_t) mem_free_old_limit &&
338 free_in_eden < (size_t) mem_free_eden_limit))) {
339 gclog_or_tty->print_cr(
340 "PSAdaptiveSizePolicy::compute_generation_free_space limits:"
341 " promo_limit: " SIZE_FORMAT
342 " max_eden_size: " SIZE_FORMAT
343 " total_free_limit: " SIZE_FORMAT
344 " max_old_gen_size: " SIZE_FORMAT
345 " max_eden_size: " SIZE_FORMAT
346 " mem_free_limit: " SIZE_FORMAT,
347 promo_limit, max_eden_size, total_free_limit,
348 max_old_gen_size, max_eden_size,
349 (size_t) mem_free_limit);
350 }
352 bool print_gc_overhead_limit_would_be_exceeded = false;
353 if (is_full_gc) {
354 if (gc_cost() > gc_cost_limit &&
355 free_in_old_gen < (size_t) mem_free_old_limit &&
356 free_in_eden < (size_t) mem_free_eden_limit) {
357 // Collections, on average, are taking too much time, and
358 // gc_cost() > gc_cost_limit
359 // we have too little space available after a full gc.
360 // total_free_limit < mem_free_limit
361 // where
362 // total_free_limit is the free space available in
363 // both generations
364 // total_mem is the total space available for allocation
365 // in both generations (survivor spaces are not included
366 // just as they are not included in eden_limit).
367 // mem_free_limit is a fraction of total_mem judged to be an
368 // acceptable amount that is still unused.
369 // The heap can ask for the value of this variable when deciding
370 // whether to thrown an OutOfMemory error.
371 // Note that the gc time limit test only works for the collections
372 // of the young gen + tenured gen and not for collections of the
373 // permanent gen. That is because the calculation of the space
374 // freed by the collection is the free space in the young gen +
375 // tenured gen.
376 // At this point the GC overhead limit is being exceeded.
377 inc_gc_overhead_limit_count();
378 if (UseGCOverheadLimit) {
379 if (gc_overhead_limit_count() >=
380 AdaptiveSizePolicyGCTimeLimitThreshold){
381 // All conditions have been met for throwing an out-of-memory
382 set_gc_overhead_limit_exceeded(true);
383 // Avoid consecutive OOM due to the gc time limit by resetting
384 // the counter.
385 reset_gc_overhead_limit_count();
386 } else {
387 // The required consecutive collections which exceed the
388 // GC time limit may or may not have been reached. We
389 // are approaching that condition and so as not to
390 // throw an out-of-memory before all SoftRef's have been
391 // cleared, set _should_clear_all_soft_refs in CollectorPolicy.
392 // The clearing will be done on the next GC.
393 bool near_limit = gc_overhead_limit_near();
394 if (near_limit) {
395 collector_policy->set_should_clear_all_soft_refs(true);
396 if (PrintGCDetails && Verbose) {
397 gclog_or_tty->print_cr(" Nearing GC overhead limit, "
398 "will be clearing all SoftReference");
399 }
400 }
401 }
402 }
403 // Set this even when the overhead limit will not
404 // cause an out-of-memory. Diagnostic message indicating
405 // that the overhead limit is being exceeded is sometimes
406 // printed.
407 print_gc_overhead_limit_would_be_exceeded = true;
409 } else {
410 // Did not exceed overhead limits
411 reset_gc_overhead_limit_count();
412 }
413 }
415 if (UseGCOverheadLimit && PrintGCDetails && Verbose) {
416 if (gc_overhead_limit_exceeded()) {
417 gclog_or_tty->print_cr(" GC is exceeding overhead limit "
418 "of %d%%", GCTimeLimit);
419 reset_gc_overhead_limit_count();
420 } else if (print_gc_overhead_limit_would_be_exceeded) {
421 assert(gc_overhead_limit_count() > 0, "Should not be printing");
422 gclog_or_tty->print_cr(" GC would exceed overhead limit "
423 "of %d%% %d consecutive time(s)",
424 GCTimeLimit, gc_overhead_limit_count());
425 }
426 }
427 }
428 // Printing
430 bool AdaptiveSizePolicy::print_adaptive_size_policy_on(outputStream* st) const {
432 // Should only be used with adaptive size policy turned on.
433 // Otherwise, there may be variables that are undefined.
434 if (!UseAdaptiveSizePolicy) return false;
436 // Print goal for which action is needed.
437 char* action = NULL;
438 bool change_for_pause = false;
439 if ((change_old_gen_for_maj_pauses() ==
440 decrease_old_gen_for_maj_pauses_true) ||
441 (change_young_gen_for_min_pauses() ==
442 decrease_young_gen_for_min_pauses_true)) {
443 action = (char*) " *** pause time goal ***";
444 change_for_pause = true;
445 } else if ((change_old_gen_for_throughput() ==
446 increase_old_gen_for_throughput_true) ||
447 (change_young_gen_for_throughput() ==
448 increase_young_gen_for_througput_true)) {
449 action = (char*) " *** throughput goal ***";
450 } else if (decrease_for_footprint()) {
451 action = (char*) " *** reduced footprint ***";
452 } else {
453 // No actions were taken. This can legitimately be the
454 // situation if not enough data has been gathered to make
455 // decisions.
456 return false;
457 }
459 // Pauses
460 // Currently the size of the old gen is only adjusted to
461 // change the major pause times.
462 char* young_gen_action = NULL;
463 char* tenured_gen_action = NULL;
465 char* shrink_msg = (char*) "(attempted to shrink)";
466 char* grow_msg = (char*) "(attempted to grow)";
467 char* no_change_msg = (char*) "(no change)";
468 if (change_young_gen_for_min_pauses() ==
469 decrease_young_gen_for_min_pauses_true) {
470 young_gen_action = shrink_msg;
471 } else if (change_for_pause) {
472 young_gen_action = no_change_msg;
473 }
475 if (change_old_gen_for_maj_pauses() == decrease_old_gen_for_maj_pauses_true) {
476 tenured_gen_action = shrink_msg;
477 } else if (change_for_pause) {
478 tenured_gen_action = no_change_msg;
479 }
481 // Throughput
482 if (change_old_gen_for_throughput() == increase_old_gen_for_throughput_true) {
483 assert(change_young_gen_for_throughput() ==
484 increase_young_gen_for_througput_true,
485 "Both generations should be growing");
486 young_gen_action = grow_msg;
487 tenured_gen_action = grow_msg;
488 } else if (change_young_gen_for_throughput() ==
489 increase_young_gen_for_througput_true) {
490 // Only the young generation may grow at start up (before
491 // enough full collections have been done to grow the old generation).
492 young_gen_action = grow_msg;
493 tenured_gen_action = no_change_msg;
494 }
496 // Minimum footprint
497 if (decrease_for_footprint() != 0) {
498 young_gen_action = shrink_msg;
499 tenured_gen_action = shrink_msg;
500 }
502 st->print_cr(" UseAdaptiveSizePolicy actions to meet %s", action);
503 st->print_cr(" GC overhead (%%)");
504 st->print_cr(" Young generation: %7.2f\t %s",
505 100.0 * avg_minor_gc_cost()->average(),
506 young_gen_action);
507 st->print_cr(" Tenured generation: %7.2f\t %s",
508 100.0 * avg_major_gc_cost()->average(),
509 tenured_gen_action);
510 return true;
511 }
513 bool AdaptiveSizePolicy::print_adaptive_size_policy_on(
514 outputStream* st,
515 int tenuring_threshold_arg) const {
516 if (!AdaptiveSizePolicy::print_adaptive_size_policy_on(st)) {
517 return false;
518 }
520 // Tenuring threshold
521 bool tenuring_threshold_changed = true;
522 if (decrement_tenuring_threshold_for_survivor_limit()) {
523 st->print(" Tenuring threshold: (attempted to decrease to avoid"
524 " survivor space overflow) = ");
525 } else if (decrement_tenuring_threshold_for_gc_cost()) {
526 st->print(" Tenuring threshold: (attempted to decrease to balance"
527 " GC costs) = ");
528 } else if (increment_tenuring_threshold_for_gc_cost()) {
529 st->print(" Tenuring threshold: (attempted to increase to balance"
530 " GC costs) = ");
531 } else {
532 tenuring_threshold_changed = false;
533 assert(!tenuring_threshold_change(), "(no change was attempted)");
534 }
535 if (tenuring_threshold_changed) {
536 st->print_cr("%d", tenuring_threshold_arg);
537 }
538 return true;
539 }